OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 13 matching lines...) Expand all Loading... | |
24 const char kIdentityName[] = "WebRTC"; | 24 const char kIdentityName[] = "WebRTC"; |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 enum { | 28 enum { |
29 MSG_DESTROY, | 29 MSG_DESTROY, |
30 MSG_GENERATE_IDENTITY, | 30 MSG_GENERATE_IDENTITY, |
31 MSG_GENERATE_IDENTITY_RESULT | 31 MSG_GENERATE_IDENTITY_RESULT |
32 }; | 32 }; |
33 | 33 |
34 // A |DtlsIdentityRequestObserver| that informs an | |
35 // |RTCCertificateGeneratorCallback| of the result of an identity request. On | |
36 // success, a certificate is created using the identity before passing it to | |
37 // the callback. | |
38 class RTCCertificateStoreCallbackObserver | |
39 : public webrtc::DtlsIdentityRequestObserver { | |
40 public: | |
41 static rtc::scoped_refptr<DtlsIdentityRequestObserver> Create( | |
42 const rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>& | |
43 callback) { | |
44 rtc::scoped_refptr<RTCCertificateStoreCallbackObserver> observer( | |
45 new rtc::RefCountedObject<RTCCertificateStoreCallbackObserver>( | |
46 callback)); | |
47 observer->this_ = observer; | |
48 return observer; | |
49 } | |
50 | |
51 void OnFailure(int error) override { | |
52 Callback(nullptr); | |
hta-webrtc
2016/05/23 13:50:38
The error is lost to the world here. Should you at
hbos
2016/05/24 08:49:43
Done, LOG(LS_WARNING).
| |
53 } | |
54 void OnSuccess(const std::string& der_cert, | |
55 const std::string& der_private_key) override { | |
56 std::string pem_cert = rtc::SSLIdentity::DerToPem( | |
57 rtc::kPemTypeCertificate, | |
58 reinterpret_cast<const unsigned char*>(der_cert.data()), | |
59 der_cert.length()); | |
60 std::string pem_key = rtc::SSLIdentity::DerToPem( | |
61 rtc::kPemTypeRsaPrivateKey, | |
62 reinterpret_cast<const unsigned char*>(der_private_key.data()), | |
63 der_private_key.length()); | |
64 std::unique_ptr<rtc::SSLIdentity> identity( | |
65 rtc::SSLIdentity::FromPEMStrings(pem_key, pem_cert)); | |
66 OnSuccess(std::move(identity)); | |
67 } | |
68 void OnSuccess(std::unique_ptr<rtc::SSLIdentity> identity) override { | |
69 Callback(rtc::RTCCertificate::Create(std::move(identity))); | |
70 } | |
71 | |
72 protected: | |
73 RTCCertificateStoreCallbackObserver( | |
74 const rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>& callback) | |
75 : callback_(callback) {} | |
76 | |
77 void Callback(rtc::scoped_refptr<rtc::RTCCertificate> certificate) { | |
78 if (certificate) | |
79 callback_->OnSuccess(certificate); | |
80 else | |
81 callback_->OnFailure(); | |
82 // If |this_| is the last reference to this observer the following line | |
83 // leads to its destruction - do not touch member variables afterwards. | |
84 this_ = nullptr; | |
85 } | |
86 | |
87 rtc::scoped_refptr<DtlsIdentityRequestObserver> this_; | |
hta-webrtc
2016/05/23 13:50:38
can you call it something other than "this_"?
"pre
hbos
2016/05/24 08:49:43
On second thought this self-referencing thing is c
| |
88 rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback> callback_; | |
89 }; | |
90 | |
34 } // namespace | 91 } // namespace |
35 | 92 |
36 // This class runs on the worker thread to generate the identity. It's necessary | 93 // This class runs on the worker thread to generate the identity. It's necessary |
37 // to separate this class from DtlsIdentityStore so that it can live on the | 94 // to separate this class from DtlsIdentityStore so that it can live on the |
38 // worker thread after DtlsIdentityStore is destroyed. | 95 // worker thread after DtlsIdentityStore is destroyed. |
39 class DtlsIdentityStoreImpl::WorkerTask : public sigslot::has_slots<>, | 96 class DtlsIdentityStoreImpl::WorkerTask : public sigslot::has_slots<>, |
40 public rtc::MessageHandler { | 97 public rtc::MessageHandler { |
41 public: | 98 public: |
42 WorkerTask(DtlsIdentityStoreImpl* store, rtc::KeyType key_type) | 99 WorkerTask(DtlsIdentityStoreImpl* store, rtc::KeyType key_type) |
43 : signaling_thread_(rtc::Thread::Current()), | 100 : signaling_thread_(rtc::Thread::Current()), |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 } | 198 } |
142 | 199 |
143 bool DtlsIdentityStoreImpl::HasFreeIdentityForTesting( | 200 bool DtlsIdentityStoreImpl::HasFreeIdentityForTesting( |
144 rtc::KeyType key_type) const { | 201 rtc::KeyType key_type) const { |
145 RTC_DCHECK(signaling_thread_->IsCurrent()); | 202 RTC_DCHECK(signaling_thread_->IsCurrent()); |
146 return request_info_[key_type].free_identity_.get() != nullptr; | 203 return request_info_[key_type].free_identity_.get() != nullptr; |
147 } | 204 } |
148 | 205 |
149 void DtlsIdentityStoreImpl::GenerateIdentity( | 206 void DtlsIdentityStoreImpl::GenerateIdentity( |
150 rtc::KeyType key_type, | 207 rtc::KeyType key_type, |
151 const rtc::scoped_refptr<webrtc::DtlsIdentityRequestObserver>& observer) { | 208 const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) { |
152 RTC_DCHECK(signaling_thread_->IsCurrent()); | 209 RTC_DCHECK(signaling_thread_->IsCurrent()); |
153 | 210 |
154 // Enqueue observer to be informed when generation of |key_type| is completed. | 211 // Enqueue observer to be informed when generation of |key_type| is completed. |
155 if (observer.get()) { | 212 if (observer.get()) { |
156 request_info_[key_type].request_observers_.push(observer); | 213 request_info_[key_type].request_observers_.push(observer); |
157 | 214 |
158 // Already have a free identity generated? | 215 // Already have a free identity generated? |
159 if (request_info_[key_type].free_identity_.get()) { | 216 if (request_info_[key_type].free_identity_.get()) { |
160 // Return identity async - post even though we are on |signaling_thread_|. | 217 // Return identity async - post even though we are on |signaling_thread_|. |
161 LOG(LS_VERBOSE) << "Using a free DTLS identity."; | 218 LOG(LS_VERBOSE) << "Using a free DTLS identity."; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
221 if (worker_thread_ != signaling_thread_ && // Only do in background thread. | 278 if (worker_thread_ != signaling_thread_ && // Only do in background thread. |
222 key_type == rtc::KT_RSA && // Only necessary for RSA. | 279 key_type == rtc::KT_RSA && // Only necessary for RSA. |
223 !request_info_[key_type].free_identity_.get() && | 280 !request_info_[key_type].free_identity_.get() && |
224 request_info_[key_type].request_observers_.size() == | 281 request_info_[key_type].request_observers_.size() == |
225 request_info_[key_type].gen_in_progress_counts_) { | 282 request_info_[key_type].gen_in_progress_counts_) { |
226 GenerateIdentity(key_type, nullptr); | 283 GenerateIdentity(key_type, nullptr); |
227 } | 284 } |
228 } | 285 } |
229 } | 286 } |
230 | 287 |
288 RTCCertificateGeneratorStoreWrapper::RTCCertificateGeneratorStoreWrapper( | |
289 std::unique_ptr<DtlsIdentityStoreInterface> store) | |
290 : store_(std::move(store)) { | |
291 RTC_DCHECK(store_); | |
292 } | |
293 | |
294 void RTCCertificateGeneratorStoreWrapper::GenerateCertificateAsync( | |
295 const rtc::KeyParams& key_params, | |
296 const rtc::Optional<uint64_t>& expires_ms, | |
297 const rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>& callback) { | |
298 store_->RequestIdentity( | |
299 key_params, | |
300 expires_ms, | |
301 RTCCertificateStoreCallbackObserver::Create(callback)); | |
302 } | |
303 | |
231 } // namespace webrtc | 304 } // namespace webrtc |
OLD | NEW |