OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2013 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 |
11 #ifndef WEBRTC_API_TEST_FAKEDTLSIDENTITYSERVICE_H_ | 11 #ifndef WEBRTC_API_TEST_FAKERTCCERTIFICATEGENERATOR_H_ |
12 #define WEBRTC_API_TEST_FAKEDTLSIDENTITYSERVICE_H_ | 12 #define WEBRTC_API_TEST_FAKERTCCERTIFICATEGENERATOR_H_ |
13 | 13 |
14 #include <memory> | 14 #include <memory> |
15 #include <string> | 15 #include <string> |
16 #include <utility> | 16 #include <utility> |
17 | 17 |
18 #include "webrtc/api/dtlsidentitystore.h" | 18 #include "webrtc/api/dtlsidentitystore.h" |
19 #include "webrtc/api/peerconnectioninterface.h" | 19 #include "webrtc/api/peerconnectioninterface.h" |
20 #include "webrtc/base/rtccertificate.h" | 20 #include "webrtc/base/rtccertificate.h" |
21 | 21 |
22 // RSA with mod size 1024, pub exp 0x10001. | 22 // RSA with mod size 1024, pub exp 0x10001. |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 "-----BEGIN CERTIFICATE-----\n" | 111 "-----BEGIN CERTIFICATE-----\n" |
112 "MIIBFDCBu6ADAgECAgkArZYdXMyJ5rswCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n" | 112 "MIIBFDCBu6ADAgECAgkArZYdXMyJ5rswCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n" |
113 "dGVzdDQwHhcNMTYwNTA5MDkxODA4WhcNMTYwNjA5MDkxODA4WjAQMQ4wDAYDVQQD\n" | 113 "dGVzdDQwHhcNMTYwNTA5MDkxODA4WhcNMTYwNjA5MDkxODA4WjAQMQ4wDAYDVQQD\n" |
114 "DAV0ZXN0NDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABN3ncWM8vJVRzBiC4Q9t\n" | 114 "DAV0ZXN0NDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABN3ncWM8vJVRzBiC4Q9t\n" |
115 "IkzTd8/pHHkZjV0RnTT5nKSMkjzdV8MDK+alG4ZqE20EfSFz7vGQMJt6jpMzS03f\n" | 115 "IkzTd8/pHHkZjV0RnTT5nKSMkjzdV8MDK+alG4ZqE20EfSFz7vGQMJt6jpMzS03f\n" |
116 "nGswCgYIKoZIzj0EAwIDSAAwRQIgb/LBc8OtsC5lEDyjCP6M9xt5mwzUNrQBOFWZ\n" | 116 "nGswCgYIKoZIzj0EAwIDSAAwRQIgb/LBc8OtsC5lEDyjCP6M9xt5mwzUNrQBOFWZ\n" |
117 "1fE/g68CIQD7uoFfbiq6dTp8ZwzbwQ8jJf08KjriamqA9OW/4268Dw==\n" | 117 "1fE/g68CIQD7uoFfbiq6dTp8ZwzbwQ8jJf08KjriamqA9OW/4268Dw==\n" |
118 "-----END CERTIFICATE-----\n") | 118 "-----END CERTIFICATE-----\n") |
119 }; | 119 }; |
120 | 120 |
121 class FakeDtlsIdentityStore : public webrtc::DtlsIdentityStoreInterface, | 121 class FakeRTCCertificateGenerator |
122 public rtc::MessageHandler { | 122 : public rtc::RTCCertificateGeneratorInterface, |
| 123 public rtc::MessageHandler { |
123 public: | 124 public: |
124 typedef rtc::TypedMessageData<rtc::scoped_refptr< | 125 typedef rtc::TypedMessageData<rtc::scoped_refptr< |
125 webrtc::DtlsIdentityRequestObserver> > MessageData; | 126 rtc::RTCCertificateGeneratorCallback> > MessageData; |
126 | 127 |
127 FakeDtlsIdentityStore() : should_fail_(false) {} | 128 FakeRTCCertificateGenerator() : should_fail_(false) {} |
128 | 129 |
129 void set_should_fail(bool should_fail) { | 130 void set_should_fail(bool should_fail) { |
130 should_fail_ = should_fail; | 131 should_fail_ = should_fail; |
131 } | 132 } |
132 | 133 |
133 void use_original_key() { key_index_ = 0; } | 134 void use_original_key() { key_index_ = 0; } |
134 void use_alternate_key() { key_index_ = 1; } | 135 void use_alternate_key() { key_index_ = 1; } |
135 | 136 |
136 void RequestIdentity( | 137 void GenerateCertificateAsync( |
137 const rtc::KeyParams& key_params, | 138 const rtc::KeyParams& key_params, |
138 const rtc::Optional<uint64_t>& expires_ms, | 139 const rtc::Optional<uint64_t>& expires_ms, |
139 const rtc::scoped_refptr<webrtc::DtlsIdentityRequestObserver>& | 140 const rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>& |
140 observer) override { | 141 callback) override { |
141 // The certificates are created from constant PEM strings and use its coded | 142 // The certificates are created from constant PEM strings and use its coded |
142 // expiration time, we do not support modifying it. | 143 // expiration time, we do not support modifying it. |
143 RTC_DCHECK(!expires_ms); | 144 RTC_DCHECK(!expires_ms); |
144 MessageData* msg = new MessageData( | 145 MessageData* msg = new MessageData( |
145 rtc::scoped_refptr<webrtc::DtlsIdentityRequestObserver>(observer)); | 146 rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>(callback)); |
146 uint32_t msg_id; | 147 uint32_t msg_id; |
147 // Only supports RSA-1024-0x10001 and ECDSA-P256. | 148 // Only supports RSA-1024-0x10001 and ECDSA-P256. |
148 if (should_fail_) { | 149 if (should_fail_) { |
149 msg_id = MSG_FAILURE; | 150 msg_id = MSG_FAILURE; |
150 } else if (key_params.type() == rtc::KT_RSA) { | 151 } else if (key_params.type() == rtc::KT_RSA) { |
151 RTC_DCHECK(key_params.rsa_params().mod_size == 1024 && | 152 RTC_DCHECK(key_params.rsa_params().mod_size == 1024 && |
152 key_params.rsa_params().pub_exp == 0x10001); | 153 key_params.rsa_params().pub_exp == 0x10001); |
153 msg_id = MSG_SUCCESS_RSA; | 154 msg_id = MSG_SUCCESS_RSA; |
154 } else { | 155 } else { |
155 RTC_DCHECK(key_params.type() == rtc::KT_ECDSA && | 156 RTC_DCHECK(key_params.type() == rtc::KT_ECDSA && |
156 key_params.ec_curve() == rtc::EC_NIST_P256); | 157 key_params.ec_curve() == rtc::EC_NIST_P256); |
157 msg_id = MSG_SUCCESS_ECDSA; | 158 msg_id = MSG_SUCCESS_ECDSA; |
158 } | 159 } |
159 rtc::Thread::Current()->Post(this, msg_id, msg); | 160 rtc::Thread::Current()->Post(this, msg_id, msg); |
160 } | 161 } |
161 | 162 |
162 static rtc::scoped_refptr<rtc::RTCCertificate> GenerateCertificate() { | 163 static rtc::scoped_refptr<rtc::RTCCertificate> GenerateCertificate() { |
163 std::unique_ptr<rtc::SSLIdentity> identity; | |
164 switch (rtc::KT_DEFAULT) { | 164 switch (rtc::KT_DEFAULT) { |
165 case rtc::KT_RSA: | 165 case rtc::KT_RSA: |
166 identity.reset( | 166 return rtc::RTCCertificate::FromPEM(kRsaPems[0]); |
167 rtc::SSLIdentity::FromPEMStrings(kRsaPems[0].private_key(), | |
168 kRsaPems[0].certificate())); | |
169 break; | |
170 case rtc::KT_ECDSA: | 167 case rtc::KT_ECDSA: |
171 identity.reset( | 168 return rtc::RTCCertificate::FromPEM(kEcdsaPems[0]); |
172 rtc::SSLIdentity::FromPEMStrings(kEcdsaPems[0].private_key(), | |
173 kEcdsaPems[0].certificate())); | |
174 break; | |
175 default: | 169 default: |
176 RTC_NOTREACHED(); | 170 RTC_NOTREACHED(); |
| 171 return nullptr; |
177 } | 172 } |
178 return rtc::RTCCertificate::Create(std::move(identity)); | |
179 } | 173 } |
180 | 174 |
181 private: | 175 private: |
182 enum { | 176 enum { |
183 MSG_SUCCESS_RSA, | 177 MSG_SUCCESS_RSA, |
184 MSG_SUCCESS_ECDSA, | 178 MSG_SUCCESS_ECDSA, |
185 MSG_FAILURE, | 179 MSG_FAILURE, |
186 }; | 180 }; |
187 | 181 |
188 const rtc::RTCCertificatePEM& get_pem(const rtc::KeyType& key_type) const { | 182 const rtc::RTCCertificatePEM& get_pem(const rtc::KeyType& key_type) const { |
(...skipping 10 matching lines...) Expand all Loading... |
199 const std::string& get_key(const rtc::KeyType& key_type) const { | 193 const std::string& get_key(const rtc::KeyType& key_type) const { |
200 return get_pem(key_type).private_key(); | 194 return get_pem(key_type).private_key(); |
201 } | 195 } |
202 const std::string& get_cert(const rtc::KeyType& key_type) const { | 196 const std::string& get_cert(const rtc::KeyType& key_type) const { |
203 return get_pem(key_type).certificate(); | 197 return get_pem(key_type).certificate(); |
204 } | 198 } |
205 | 199 |
206 // rtc::MessageHandler implementation. | 200 // rtc::MessageHandler implementation. |
207 void OnMessage(rtc::Message* msg) override { | 201 void OnMessage(rtc::Message* msg) override { |
208 MessageData* message_data = static_cast<MessageData*>(msg->pdata); | 202 MessageData* message_data = static_cast<MessageData*>(msg->pdata); |
209 rtc::scoped_refptr<webrtc::DtlsIdentityRequestObserver> observer = | 203 rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback> callback = |
210 message_data->data(); | 204 message_data->data(); |
| 205 rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
211 switch (msg->message_id) { | 206 switch (msg->message_id) { |
212 case MSG_SUCCESS_RSA: | 207 case MSG_SUCCESS_RSA: |
213 case MSG_SUCCESS_ECDSA: { | 208 case MSG_SUCCESS_ECDSA: { |
214 rtc::KeyType key_type = | 209 rtc::KeyType key_type = |
215 msg->message_id == MSG_SUCCESS_RSA ? rtc::KT_RSA : rtc::KT_ECDSA; | 210 msg->message_id == MSG_SUCCESS_RSA ? rtc::KT_RSA : rtc::KT_ECDSA; |
216 std::unique_ptr<rtc::SSLIdentity> identity( | 211 certificate = rtc::RTCCertificate::FromPEM(get_pem(key_type)); |
217 rtc::SSLIdentity::FromPEMStrings(get_key(key_type), | 212 RTC_DCHECK(certificate); |
218 get_cert(key_type))); | 213 callback->OnSuccess(certificate); |
219 observer->OnSuccess(std::move(identity)); | |
220 break; | 214 break; |
221 } | 215 } |
222 case MSG_FAILURE: | 216 case MSG_FAILURE: |
223 observer->OnFailure(0); | 217 callback->OnFailure(); |
224 break; | 218 break; |
225 } | 219 } |
226 delete message_data; | 220 delete message_data; |
227 } | 221 } |
228 | 222 |
229 bool should_fail_; | 223 bool should_fail_; |
230 int key_index_ = 0; | 224 int key_index_ = 0; |
231 }; | 225 }; |
232 | 226 |
233 #endif // WEBRTC_API_TEST_FAKEDTLSIDENTITYSERVICE_H_ | 227 #endif // WEBRTC_API_TEST_FAKERTCCERTIFICATEGENERATOR_H_ |
OLD | NEW |