fake_rtc_certificate_generator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. #ifndef PC_TEST_FAKE_RTC_CERTIFICATE_GENERATOR_H_
  11. #define PC_TEST_FAKE_RTC_CERTIFICATE_GENERATOR_H_
  12. #include <memory>
  13. #include <string>
  14. #include <utility>
  15. #include "api/peer_connection_interface.h"
  16. #include "rtc_base/rtc_certificate.h"
  17. #include "rtc_base/rtc_certificate_generator.h"
  18. // RSA with mod size 1024, pub exp 0x10001.
  19. static const rtc::RTCCertificatePEM kRsaPems[] = {
  20. rtc::RTCCertificatePEM(
  21. "-----BEGIN RSA PRIVATE KEY-----\n"
  22. "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMYRkbhmI7kVA/rM\n"
  23. "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n"
  24. "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n"
  25. "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAECgYAvgOs4FJcgvp+TuREx7YtiYVsH\n"
  26. "mwQPTum2z/8VzWGwR8BBHBvIpVe1MbD/Y4seyI2aco/7UaisatSgJhsU46/9Y4fq\n"
  27. "2TwXH9QANf4at4d9n/R6rzwpAJOpgwZgKvdQjkfrKTtgLV+/dawvpxUYkRH4JZM1\n"
  28. "CVGukMfKNrSVH4Ap4QJBAOJmGV1ASPnB4r4nc99at7JuIJmd7fmuVUwUgYi4XgaR\n"
  29. "WhScBsgYwZ/JoywdyZJgnbcrTDuVcWG56B3vXbhdpMsCQQDf9zeJrjnPZ3Cqm79y\n"
  30. "kdqANep0uwZciiNiWxsQrCHztywOvbFhdp8iYVFG9EK8DMY41Y5TxUwsHD+67zao\n"
  31. "ZNqJAkEA1suLUP/GvL8IwuRneQd2tWDqqRQ/Td3qq03hP7e77XtF/buya3Ghclo5\n"
  32. "54czUR89QyVfJEC6278nzA7n2h1uVQJAcG6mztNL6ja/dKZjYZye2CY44QjSlLo0\n"
  33. "MTgTSjdfg/28fFn2Jjtqf9Pi/X+50LWI/RcYMC2no606wRk9kyOuIQJBAK6VSAim\n"
  34. "1pOEjsYQn0X5KEIrz1G3bfCbB848Ime3U2/FWlCHMr6ch8kCZ5d1WUeJD3LbwMNG\n"
  35. "UCXiYxSsu20QNVw=\n"
  36. "-----END RSA PRIVATE KEY-----\n",
  37. "-----BEGIN CERTIFICATE-----\n"
  38. "MIIBmTCCAQKgAwIBAgIEbzBSAjANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDEwZX\n"
  39. "ZWJSVEMwHhcNMTQwMTAyMTgyNDQ3WhcNMTQwMjAxMTgyNDQ3WjARMQ8wDQYDVQQD\n"
  40. "EwZXZWJSVEMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMYRkbhmI7kVA/rM\n"
  41. "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n"
  42. "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8dz\n"
  43. "5aTSMsbbkZ+C/OzTnbiMqLL/vg6jAgMBAAEwDQYJKoZIhvcNAQELBQADgYEAUflI\n"
  44. "VUe5Krqf5RVa5C3u/UTAOAUJBiDS3VANTCLBxjuMsvqOG0WvaYWP3HYPgrz0jXK2\n"
  45. "LJE/mGw3MyFHEqi81jh95J+ypl6xKW6Rm8jKLR87gUvCaVYn/Z4/P3AqcQTB7wOv\n"
  46. "UD0A8qfhfDM+LK6rPAnCsVN0NRDY3jvd6rzix9M=\n"
  47. "-----END CERTIFICATE-----\n"),
  48. rtc::RTCCertificatePEM(
  49. "-----BEGIN RSA PRIVATE KEY-----\n"
  50. "MIICXQIBAAKBgQDeYqlyJ1wuiMsi905e3X81/WA/G3ym50PIDZBVtSwZi7JVQPgj\n"
  51. "Bl8CPZMvDh9EwB4Ji9ytA8dZZbQ4WbJWPr73zPpJSCvQqz6sOXSlenBRi72acNaQ\n"
  52. "sOR/qPvviJx5I6Hqo4qemfnjZhAW85a5BpgrAwKgMLIQTHCTLWwVSyrDrwIDAQAB\n"
  53. "AoGARni9eY8/hv+SX+I+05EdXt6MQXNUbQ+cSykBNCfVccLzIFEWUQMT2IHqwl6X\n"
  54. "ShIXcq7/n1QzOAEiuzixauM3YHg4xZ1Um2Ha9a7ig5Xg4v6b43bmMkNE6LkoAtYs\n"
  55. "qnQdfMh442b1liDud6IMb1Qk0amt3fSrgRMc547TZQVx4QECQQDxUeDm94r3p4ng\n"
  56. "5rCLLC1K5/6HSTZsh7jatKPlz7GfP/IZlYV7iE5784/n0wRiCjZOS7hQRy/8m2Gp\n"
  57. "pf4aZq+DAkEA6+np4d36FYikydvUrupLT3FkdRHGn/v83qOll/VmeNh+L1xMZlIP\n"
  58. "tM26hAXCcQb7O5+J9y3cx2CAQsBS11ZXZQJAfGgTo76WG9p5UEJdXUInD2jOZPwv\n"
  59. "XIATolxh6kXKcijLLLlSmT7KB0inNYIpzkkpee+7U1d/u6B3FriGaSHq9QJBAM/J\n"
  60. "ICnDdLCgwNvWVraVQC3BpwSB2pswvCFwq7py94V60XFvbw80Ogc6qIv98qvQxVlX\n"
  61. "hJIEgA/PjEi+0ng94Q0CQQDm8XSDby35gmjO+6eRmJtAjtB7nguLvrPXM6CPXRmD\n"
  62. "sRoBocpHw6j9UdzZ6qYG0FkdXZghezXFY58ro2BYYRR3\n"
  63. "-----END RSA PRIVATE KEY-----\n",
  64. "-----BEGIN CERTIFICATE-----\n"
  65. "MIICWDCCAcGgAwIBAgIJALgDjxMbBOhbMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV\n"
  66. "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\n"
  67. "aWRnaXRzIFB0eSBMdGQwHhcNMTUxMTEzMjIzMjEzWhcNMTYxMTEyMjIzMjEzWjBF\n"
  68. "MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\n"
  69. "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
  70. "gQDeYqlyJ1wuiMsi905e3X81/WA/G3ym50PIDZBVtSwZi7JVQPgjBl8CPZMvDh9E\n"
  71. "wB4Ji9ytA8dZZbQ4WbJWPr73zPpJSCvQqz6sOXSlenBRi72acNaQsOR/qPvviJx5\n"
  72. "I6Hqo4qemfnjZhAW85a5BpgrAwKgMLIQTHCTLWwVSyrDrwIDAQABo1AwTjAdBgNV\n"
  73. "HQ4EFgQUx2tbJdlcSTCepn09UdYORXKuSTAwHwYDVR0jBBgwFoAUx2tbJdlcSTCe\n"
  74. "pn09UdYORXKuSTAwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQAmp9Id\n"
  75. "E716gHMqeBG4S2FCgVFCr0a0ugkaneQAN/c2L9CbMemEN9W6jvucUIVOtYd90dDW\n"
  76. "lXuowWmT/JctPe3D2qt4yvYW3puECHk2tVQmrJOZiZiTRtWm6HxkmoUYHYp/DtaS\n"
  77. "1Xe29gSTnZtI5sQCrGMzk3SGRSSs7ejLKiVDBQ==\n"
  78. "-----END CERTIFICATE-----\n")};
  79. // ECDSA with EC_NIST_P256.
  80. // These PEM strings were created by generating an identity with
  81. // |SSLIdentity::Generate| and invoking |identity->PrivateKeyToPEMString()|,
  82. // |identity->PublicKeyToPEMString()| and
  83. // |identity->certificate().ToPEMString()|.
  84. static const rtc::RTCCertificatePEM kEcdsaPems[] = {
  85. rtc::RTCCertificatePEM(
  86. "-----BEGIN PRIVATE KEY-----\n"
  87. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg+qaRsR5uHtqG689M\n"
  88. "A3PHSJNeVpyi5wUKCft62h0UWy+hRANCAAS5Mjc85q9fVq4ln+zOPlaEC/Rzj5Pb\n"
  89. "MVZtf1x/8k2KsbmyZoAMDX2yer/atEuXmItMe3yd6/DXnvboU//D3Lyt\n"
  90. "-----END PRIVATE KEY-----\n",
  91. "-----BEGIN CERTIFICATE-----\n"
  92. "MIIBFTCBu6ADAgECAgkA30tGY5XG7oowCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n"
  93. "dGVzdDMwHhcNMTYwNTA5MDkxODA4WhcNMTYwNjA5MDkxODA4WjAQMQ4wDAYDVQQD\n"
  94. "DAV0ZXN0MzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABLkyNzzmr19WriWf7M4+\n"
  95. "VoQL9HOPk9sxVm1/XH/yTYqxubJmgAwNfbJ6v9q0S5eYi0x7fJ3r8Nee9uhT/8Pc\n"
  96. "vK0wCgYIKoZIzj0EAwIDSQAwRgIhAIIc3+CqfkZ9lLwTj1PvUtt3KhnqF2kD0War\n"
  97. "cCoTBbCxAiEAyp9Cn4vo2ZBhRIVDKyoxmwak8Z0PAVhJAQaWCgoY2D4=\n"
  98. "-----END CERTIFICATE-----\n"),
  99. rtc::RTCCertificatePEM(
  100. "-----BEGIN PRIVATE KEY-----\n"
  101. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQghL/G4JRYnuDNbQuh\n"
  102. "LqkytcE39Alsq6FItDVFgOesfCmhRANCAATd53FjPLyVUcwYguEPbSJM03fP6Rx5\n"
  103. "GY1dEZ00+ZykjJI83VfDAyvmpRuGahNtBH0hc+7xkDCbeo6TM0tN35xr\n"
  104. "-----END PRIVATE KEY-----\n",
  105. "-----BEGIN CERTIFICATE-----\n"
  106. "MIIBFDCBu6ADAgECAgkArZYdXMyJ5rswCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n"
  107. "dGVzdDQwHhcNMTYwNTA5MDkxODA4WhcNMTYwNjA5MDkxODA4WjAQMQ4wDAYDVQQD\n"
  108. "DAV0ZXN0NDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABN3ncWM8vJVRzBiC4Q9t\n"
  109. "IkzTd8/pHHkZjV0RnTT5nKSMkjzdV8MDK+alG4ZqE20EfSFz7vGQMJt6jpMzS03f\n"
  110. "nGswCgYIKoZIzj0EAwIDSAAwRQIgb/LBc8OtsC5lEDyjCP6M9xt5mwzUNrQBOFWZ\n"
  111. "1fE/g68CIQD7uoFfbiq6dTp8ZwzbwQ8jJf08KjriamqA9OW/4268Dw==\n"
  112. "-----END CERTIFICATE-----\n")};
  113. class FakeRTCCertificateGenerator
  114. : public rtc::RTCCertificateGeneratorInterface,
  115. public rtc::MessageHandlerAutoCleanup {
  116. public:
  117. typedef rtc::TypedMessageData<
  118. rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback> >
  119. MessageData;
  120. FakeRTCCertificateGenerator() : should_fail_(false), should_wait_(false) {}
  121. void set_should_fail(bool should_fail) { should_fail_ = should_fail; }
  122. // If set to true, stalls the generation of the fake certificate until it is
  123. // set to false.
  124. void set_should_wait(bool should_wait) { should_wait_ = should_wait; }
  125. void use_original_key() { key_index_ = 0; }
  126. void use_alternate_key() { key_index_ = 1; }
  127. int generated_certificates() { return generated_certificates_; }
  128. int generated_failures() { return generated_failures_; }
  129. void GenerateCertificateAsync(
  130. const rtc::KeyParams& key_params,
  131. const absl::optional<uint64_t>& expires_ms,
  132. const rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>& callback)
  133. override {
  134. // The certificates are created from constant PEM strings and use its coded
  135. // expiration time, we do not support modifying it.
  136. RTC_DCHECK(!expires_ms);
  137. MessageData* msg = new MessageData(
  138. rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback>(callback));
  139. uint32_t msg_id;
  140. // Only supports RSA-1024-0x10001 and ECDSA-P256.
  141. if (should_fail_) {
  142. msg_id = MSG_FAILURE;
  143. } else if (key_params.type() == rtc::KT_RSA) {
  144. RTC_DCHECK_EQ(key_params.rsa_params().mod_size, 1024);
  145. RTC_DCHECK_EQ(key_params.rsa_params().pub_exp, 0x10001);
  146. msg_id = MSG_SUCCESS_RSA;
  147. } else {
  148. RTC_DCHECK_EQ(key_params.type(), rtc::KT_ECDSA);
  149. RTC_DCHECK_EQ(key_params.ec_curve(), rtc::EC_NIST_P256);
  150. msg_id = MSG_SUCCESS_ECDSA;
  151. }
  152. rtc::Thread::Current()->Post(RTC_FROM_HERE, this, msg_id, msg);
  153. }
  154. static rtc::scoped_refptr<rtc::RTCCertificate> GenerateCertificate() {
  155. switch (rtc::KT_DEFAULT) {
  156. case rtc::KT_RSA:
  157. return rtc::RTCCertificate::FromPEM(kRsaPems[0]);
  158. case rtc::KT_ECDSA:
  159. return rtc::RTCCertificate::FromPEM(kEcdsaPems[0]);
  160. default:
  161. RTC_NOTREACHED();
  162. return nullptr;
  163. }
  164. }
  165. private:
  166. enum {
  167. MSG_SUCCESS_RSA,
  168. MSG_SUCCESS_ECDSA,
  169. MSG_FAILURE,
  170. };
  171. const rtc::RTCCertificatePEM& get_pem(const rtc::KeyType& key_type) const {
  172. switch (key_type) {
  173. case rtc::KT_RSA:
  174. return kRsaPems[key_index_];
  175. case rtc::KT_ECDSA:
  176. return kEcdsaPems[key_index_];
  177. default:
  178. RTC_NOTREACHED();
  179. return kEcdsaPems[key_index_];
  180. }
  181. }
  182. const std::string& get_key(const rtc::KeyType& key_type) const {
  183. return get_pem(key_type).private_key();
  184. }
  185. const std::string& get_cert(const rtc::KeyType& key_type) const {
  186. return get_pem(key_type).certificate();
  187. }
  188. // rtc::MessageHandler implementation.
  189. void OnMessage(rtc::Message* msg) override {
  190. // If the certificate generation should be stalled, re-post this same
  191. // message to the queue with a small delay so as to wait in a loop until
  192. // set_should_wait(false) is called.
  193. if (should_wait_) {
  194. rtc::Thread::Current()->PostDelayed(RTC_FROM_HERE, 1, this,
  195. msg->message_id, msg->pdata);
  196. return;
  197. }
  198. MessageData* message_data = static_cast<MessageData*>(msg->pdata);
  199. rtc::scoped_refptr<rtc::RTCCertificateGeneratorCallback> callback =
  200. message_data->data();
  201. rtc::scoped_refptr<rtc::RTCCertificate> certificate;
  202. switch (msg->message_id) {
  203. case MSG_SUCCESS_RSA:
  204. case MSG_SUCCESS_ECDSA: {
  205. rtc::KeyType key_type =
  206. msg->message_id == MSG_SUCCESS_RSA ? rtc::KT_RSA : rtc::KT_ECDSA;
  207. certificate = rtc::RTCCertificate::FromPEM(get_pem(key_type));
  208. RTC_DCHECK(certificate);
  209. ++generated_certificates_;
  210. callback->OnSuccess(certificate);
  211. break;
  212. }
  213. case MSG_FAILURE:
  214. ++generated_failures_;
  215. callback->OnFailure();
  216. break;
  217. }
  218. delete message_data;
  219. }
  220. bool should_fail_;
  221. bool should_wait_;
  222. int key_index_ = 0;
  223. int generated_certificates_ = 0;
  224. int generated_failures_ = 0;
  225. };
  226. #endif // PC_TEST_FAKE_RTC_CERTIFICATE_GENERATOR_H_