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