jiayl@webrtc.org | 2548406 | 2015-02-18 23:58:16 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 2 | * Copyright 2015 The WebRTC project authors. All Rights Reserved. |
jiayl@webrtc.org | 2548406 | 2015-02-18 23:58:16 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame^] | 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
jiayl@webrtc.org | 2548406 | 2015-02-18 23:58:16 +0000 | [diff] [blame] | 9 | */ |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #ifndef WEBRTC_API_DTLSIDENTITYSTORE_H_ |
| 12 | #define WEBRTC_API_DTLSIDENTITYSTORE_H_ |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 13 | |
| 14 | #include <queue> |
| 15 | #include <string> |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 16 | #include <utility> |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 17 | |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 18 | #include "webrtc/base/messagehandler.h" |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame] | 19 | #include "webrtc/base/messagequeue.h" |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 20 | #include "webrtc/base/refcount.h" |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 21 | #include "webrtc/base/scoped_ptr.h" |
| 22 | #include "webrtc/base/scoped_ref_ptr.h" |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 23 | #include "webrtc/base/sslidentity.h" |
| 24 | #include "webrtc/base/thread.h" |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
glaznev | 97579a4 | 2015-09-01 11:31:27 -0700 | [diff] [blame] | 27 | |
| 28 | // Passed to SSLIdentity::Generate. |
| 29 | extern const char kIdentityName[]; |
| 30 | |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 31 | class SSLIdentity; |
| 32 | class Thread; |
| 33 | |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 34 | // Used to receive callbacks of DTLS identity requests. |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 35 | class DtlsIdentityRequestObserver : public rtc::RefCountInterface { |
| 36 | public: |
| 37 | virtual void OnFailure(int error) = 0; |
| 38 | // TODO(hbos): Unify the OnSuccess method once Chrome code is updated. |
| 39 | virtual void OnSuccess(const std::string& der_cert, |
| 40 | const std::string& der_private_key) = 0; |
| 41 | // |identity| is a scoped_ptr because rtc::SSLIdentity is not copyable and the |
| 42 | // client has to get the ownership of the object to make use of it. |
| 43 | virtual void OnSuccess(rtc::scoped_ptr<rtc::SSLIdentity> identity) = 0; |
| 44 | |
| 45 | protected: |
| 46 | virtual ~DtlsIdentityRequestObserver() {} |
| 47 | }; |
| 48 | |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 49 | // This interface defines an in-memory DTLS identity store, which generates DTLS |
| 50 | // identities. |
| 51 | // APIs calls must be made on the signaling thread and the callbacks are also |
| 52 | // called on the signaling thread. |
| 53 | class DtlsIdentityStoreInterface { |
| 54 | public: |
| 55 | virtual ~DtlsIdentityStoreInterface() { } |
| 56 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 57 | // The |observer| will be called when the requested identity is ready, or when |
| 58 | // identity generation fails. |
hbos | 3b7c793 | 2015-10-21 01:44:21 -0700 | [diff] [blame] | 59 | // TODO(torbjorng,hbos): The following RequestIdentity is about to be removed, |
| 60 | // see below todo. |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 61 | virtual void RequestIdentity( |
| 62 | rtc::KeyType key_type, |
hbos | 3b7c793 | 2015-10-21 01:44:21 -0700 | [diff] [blame] | 63 | const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) { |
| 64 | // Add default parameterization. |
| 65 | RequestIdentity(rtc::KeyParams(key_type), observer); |
| 66 | } |
| 67 | // TODO(torbjorng,hbos): Parameterized key types! The following |
| 68 | // RequestIdentity should replace the old one that takes rtc::KeyType. When |
| 69 | // the new one is implemented by Chromium and WebRTC the old one should be |
| 70 | // removed. crbug.com/544902, webrtc:5092. |
| 71 | virtual void RequestIdentity( |
| 72 | rtc::KeyParams key_params, |
| 73 | const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) { |
| 74 | // Drop parameterization. |
| 75 | RequestIdentity(key_params.type(), observer); |
| 76 | } |
Henrik Boström | 5b4ce33 | 2015-08-05 16:55:22 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 79 | // The WebRTC default implementation of DtlsIdentityStoreInterface. |
| 80 | // Identity generation is performed on the worker thread. |
| 81 | class DtlsIdentityStoreImpl : public DtlsIdentityStoreInterface, |
| 82 | public rtc::MessageHandler { |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 83 | public: |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 84 | // This will start to preemptively generating an RSA identity in the |
| 85 | // background if the worker thread is not the same as the signaling thread. |
| 86 | DtlsIdentityStoreImpl(rtc::Thread* signaling_thread, |
| 87 | rtc::Thread* worker_thread); |
| 88 | ~DtlsIdentityStoreImpl() override; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 89 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 90 | // DtlsIdentityStoreInterface override; |
| 91 | void RequestIdentity( |
| 92 | rtc::KeyType key_type, |
| 93 | const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer) override; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 94 | |
| 95 | // rtc::MessageHandler override; |
| 96 | void OnMessage(rtc::Message* msg) override; |
| 97 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 98 | // Returns true if there is a free RSA identity, used for unit tests. |
| 99 | bool HasFreeIdentityForTesting(rtc::KeyType key_type) const; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 100 | |
| 101 | private: |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 102 | void GenerateIdentity( |
| 103 | rtc::KeyType key_type, |
| 104 | const rtc::scoped_refptr<DtlsIdentityRequestObserver>& observer); |
| 105 | void OnIdentityGenerated(rtc::KeyType key_type, |
| 106 | rtc::scoped_ptr<rtc::SSLIdentity> identity); |
| 107 | |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame] | 108 | class WorkerTask; |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 109 | typedef rtc::ScopedMessageData<DtlsIdentityStoreImpl::WorkerTask> |
| 110 | WorkerTaskMessageData; |
jiayl@webrtc.org | d83f4ef | 2015-03-13 21:26:12 +0000 | [diff] [blame] | 111 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 112 | // A key type-identity pair. |
| 113 | struct IdentityResult { |
| 114 | IdentityResult(rtc::KeyType key_type, |
| 115 | rtc::scoped_ptr<rtc::SSLIdentity> identity) |
kwiberg | 0eb15ed | 2015-12-17 03:04:15 -0800 | [diff] [blame] | 116 | : key_type_(key_type), identity_(std::move(identity)) {} |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 117 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 118 | rtc::KeyType key_type_; |
| 119 | rtc::scoped_ptr<rtc::SSLIdentity> identity_; |
| 120 | }; |
| 121 | |
| 122 | typedef rtc::ScopedMessageData<IdentityResult> IdentityResultMessageData; |
| 123 | |
| 124 | sigslot::signal0<> SignalDestroyed; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 125 | |
Tommi | 532caea | 2015-06-09 17:33:06 +0200 | [diff] [blame] | 126 | rtc::Thread* const signaling_thread_; |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 127 | // TODO(hbos): RSA generation is slow and would be VERY slow if we switch over |
| 128 | // to 2048, DtlsIdentityStore should use a new thread and not the "general |
| 129 | // purpose" worker thread. |
Tommi | 532caea | 2015-06-09 17:33:06 +0200 | [diff] [blame] | 130 | rtc::Thread* const worker_thread_; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 131 | |
Henrik Boström | 5e56c59 | 2015-08-11 10:33:13 +0200 | [diff] [blame] | 132 | struct RequestInfo { |
| 133 | RequestInfo() |
| 134 | : request_observers_(), gen_in_progress_counts_(0), free_identity_() {} |
| 135 | |
| 136 | std::queue<rtc::scoped_refptr<DtlsIdentityRequestObserver>> |
| 137 | request_observers_; |
| 138 | size_t gen_in_progress_counts_; |
| 139 | rtc::scoped_ptr<rtc::SSLIdentity> free_identity_; |
| 140 | }; |
| 141 | |
| 142 | // One RequestInfo per KeyType. Only touch on the |signaling_thread_|. |
| 143 | RequestInfo request_info_[rtc::KT_LAST]; |
jiayl@webrtc.org | 61e00b0 | 2015-03-04 22:17:38 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | } // namespace webrtc |
| 147 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 148 | #endif // WEBRTC_API_DTLSIDENTITYSTORE_H_ |