You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
8.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 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. #include "rtc_base/openssl_certificate.h"
  11. #if defined(WEBRTC_WIN)
  12. // Must be included first before openssl headers.
  13. #include "rtc_base/win32.h" // NOLINT
  14. #endif // WEBRTC_WIN
  15. #include <openssl/bio.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/pem.h>
  18. #include <time.h>
  19. #include <memory>
  20. #include "rtc_base/checks.h"
  21. #include "rtc_base/helpers.h"
  22. #include "rtc_base/logging.h"
  23. #include "rtc_base/message_digest.h"
  24. #include "rtc_base/openssl_digest.h"
  25. #include "rtc_base/openssl_identity.h"
  26. #include "rtc_base/openssl_utility.h"
  27. namespace rtc {
  28. namespace {
  29. // Random bits for certificate serial number
  30. static const int SERIAL_RAND_BITS = 64;
  31. #if !defined(NDEBUG)
  32. // Print a certificate to the log, for debugging.
  33. static void PrintCert(X509* x509) {
  34. BIO* temp_memory_bio = BIO_new(BIO_s_mem());
  35. if (!temp_memory_bio) {
  36. RTC_DLOG_F(LS_ERROR) << "Failed to allocate temporary memory bio";
  37. return;
  38. }
  39. X509_print_ex(temp_memory_bio, x509, XN_FLAG_SEP_CPLUS_SPC, 0);
  40. BIO_write(temp_memory_bio, "\0", 1);
  41. char* buffer;
  42. BIO_get_mem_data(temp_memory_bio, &buffer);
  43. RTC_DLOG(LS_VERBOSE) << buffer;
  44. BIO_free(temp_memory_bio);
  45. }
  46. #endif
  47. // Generate a self-signed certificate, with the public key from the
  48. // given key pair. Caller is responsible for freeing the returned object.
  49. static X509* MakeCertificate(EVP_PKEY* pkey, const SSLIdentityParams& params) {
  50. RTC_LOG(LS_INFO) << "Making certificate for " << params.common_name;
  51. ASN1_INTEGER* asn1_serial_number = nullptr;
  52. BIGNUM* serial_number = nullptr;
  53. X509* x509 = nullptr;
  54. X509_NAME* name = nullptr;
  55. time_t epoch_off = 0; // Time offset since epoch.
  56. if ((x509 = X509_new()) == nullptr) {
  57. goto error;
  58. }
  59. if (!X509_set_pubkey(x509, pkey)) {
  60. goto error;
  61. }
  62. // serial number - temporary reference to serial number inside x509 struct
  63. if ((serial_number = BN_new()) == nullptr ||
  64. !BN_pseudo_rand(serial_number, SERIAL_RAND_BITS, 0, 0) ||
  65. (asn1_serial_number = X509_get_serialNumber(x509)) == nullptr ||
  66. !BN_to_ASN1_INTEGER(serial_number, asn1_serial_number)) {
  67. goto error;
  68. }
  69. // Set version to X509.V3
  70. if (!X509_set_version(x509, 2L)) {
  71. goto error;
  72. }
  73. // There are a lot of possible components for the name entries. In
  74. // our P2P SSL mode however, the certificates are pre-exchanged
  75. // (through the secure XMPP channel), and so the certificate
  76. // identification is arbitrary. It can't be empty, so we set some
  77. // arbitrary common_name. Note that this certificate goes out in
  78. // clear during SSL negotiation, so there may be a privacy issue in
  79. // putting anything recognizable here.
  80. if ((name = X509_NAME_new()) == nullptr ||
  81. !X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  82. (unsigned char*)params.common_name.c_str(),
  83. -1, -1, 0) ||
  84. !X509_set_subject_name(x509, name) || !X509_set_issuer_name(x509, name)) {
  85. goto error;
  86. }
  87. if (!X509_time_adj(X509_get_notBefore(x509), params.not_before, &epoch_off) ||
  88. !X509_time_adj(X509_get_notAfter(x509), params.not_after, &epoch_off)) {
  89. goto error;
  90. }
  91. if (!X509_sign(x509, pkey, EVP_sha256())) {
  92. goto error;
  93. }
  94. BN_free(serial_number);
  95. X509_NAME_free(name);
  96. RTC_LOG(LS_INFO) << "Returning certificate";
  97. return x509;
  98. error:
  99. BN_free(serial_number);
  100. X509_NAME_free(name);
  101. X509_free(x509);
  102. return nullptr;
  103. }
  104. } // namespace
  105. OpenSSLCertificate::OpenSSLCertificate(X509* x509) : x509_(x509) {
  106. RTC_DCHECK(x509_ != nullptr);
  107. X509_up_ref(x509_);
  108. }
  109. std::unique_ptr<OpenSSLCertificate> OpenSSLCertificate::Generate(
  110. OpenSSLKeyPair* key_pair,
  111. const SSLIdentityParams& params) {
  112. SSLIdentityParams actual_params(params);
  113. if (actual_params.common_name.empty()) {
  114. // Use a random string, arbitrarily 8chars long.
  115. actual_params.common_name = CreateRandomString(8);
  116. }
  117. X509* x509 = MakeCertificate(key_pair->pkey(), actual_params);
  118. if (!x509) {
  119. openssl::LogSSLErrors("Generating certificate");
  120. return nullptr;
  121. }
  122. #if !defined(NDEBUG)
  123. PrintCert(x509);
  124. #endif
  125. auto ret = std::make_unique<OpenSSLCertificate>(x509);
  126. X509_free(x509);
  127. return ret;
  128. }
  129. std::unique_ptr<OpenSSLCertificate> OpenSSLCertificate::FromPEMString(
  130. const std::string& pem_string) {
  131. BIO* bio = BIO_new_mem_buf(const_cast<char*>(pem_string.c_str()), -1);
  132. if (!bio) {
  133. return nullptr;
  134. }
  135. BIO_set_mem_eof_return(bio, 0);
  136. X509* x509 =
  137. PEM_read_bio_X509(bio, nullptr, nullptr, const_cast<char*>("\0"));
  138. BIO_free(bio); // Frees the BIO, but not the pointed-to string.
  139. if (!x509) {
  140. return nullptr;
  141. }
  142. auto ret = std::make_unique<OpenSSLCertificate>(x509);
  143. X509_free(x509);
  144. return ret;
  145. }
  146. // NOTE: This implementation only functions correctly after InitializeSSL
  147. // and before CleanupSSL.
  148. bool OpenSSLCertificate::GetSignatureDigestAlgorithm(
  149. std::string* algorithm) const {
  150. int nid = X509_get_signature_nid(x509_);
  151. switch (nid) {
  152. case NID_md5WithRSA:
  153. case NID_md5WithRSAEncryption:
  154. *algorithm = DIGEST_MD5;
  155. break;
  156. case NID_ecdsa_with_SHA1:
  157. case NID_dsaWithSHA1:
  158. case NID_dsaWithSHA1_2:
  159. case NID_sha1WithRSA:
  160. case NID_sha1WithRSAEncryption:
  161. *algorithm = DIGEST_SHA_1;
  162. break;
  163. case NID_ecdsa_with_SHA224:
  164. case NID_sha224WithRSAEncryption:
  165. case NID_dsa_with_SHA224:
  166. *algorithm = DIGEST_SHA_224;
  167. break;
  168. case NID_ecdsa_with_SHA256:
  169. case NID_sha256WithRSAEncryption:
  170. case NID_dsa_with_SHA256:
  171. *algorithm = DIGEST_SHA_256;
  172. break;
  173. case NID_ecdsa_with_SHA384:
  174. case NID_sha384WithRSAEncryption:
  175. *algorithm = DIGEST_SHA_384;
  176. break;
  177. case NID_ecdsa_with_SHA512:
  178. case NID_sha512WithRSAEncryption:
  179. *algorithm = DIGEST_SHA_512;
  180. break;
  181. default:
  182. // Unknown algorithm. There are several unhandled options that are less
  183. // common and more complex.
  184. RTC_LOG(LS_ERROR) << "Unknown signature algorithm NID: " << nid;
  185. algorithm->clear();
  186. return false;
  187. }
  188. return true;
  189. }
  190. bool OpenSSLCertificate::ComputeDigest(const std::string& algorithm,
  191. unsigned char* digest,
  192. size_t size,
  193. size_t* length) const {
  194. return ComputeDigest(x509_, algorithm, digest, size, length);
  195. }
  196. bool OpenSSLCertificate::ComputeDigest(const X509* x509,
  197. const std::string& algorithm,
  198. unsigned char* digest,
  199. size_t size,
  200. size_t* length) {
  201. const EVP_MD* md = nullptr;
  202. unsigned int n = 0;
  203. if (!OpenSSLDigest::GetDigestEVP(algorithm, &md)) {
  204. return false;
  205. }
  206. if (size < static_cast<size_t>(EVP_MD_size(md))) {
  207. return false;
  208. }
  209. X509_digest(x509, md, digest, &n);
  210. *length = n;
  211. return true;
  212. }
  213. OpenSSLCertificate::~OpenSSLCertificate() {
  214. X509_free(x509_);
  215. }
  216. std::unique_ptr<SSLCertificate> OpenSSLCertificate::Clone() const {
  217. return std::make_unique<OpenSSLCertificate>(x509_);
  218. }
  219. std::string OpenSSLCertificate::ToPEMString() const {
  220. BIO* bio = BIO_new(BIO_s_mem());
  221. if (!bio) {
  222. FATAL() << "Unreachable code.";
  223. }
  224. if (!PEM_write_bio_X509(bio, x509_)) {
  225. BIO_free(bio);
  226. FATAL() << "Unreachable code.";
  227. }
  228. BIO_write(bio, "\0", 1);
  229. char* buffer;
  230. BIO_get_mem_data(bio, &buffer);
  231. std::string ret(buffer);
  232. BIO_free(bio);
  233. return ret;
  234. }
  235. void OpenSSLCertificate::ToDER(Buffer* der_buffer) const {
  236. // In case of failure, make sure to leave the buffer empty.
  237. der_buffer->SetSize(0);
  238. // Calculates the DER representation of the certificate, from scratch.
  239. BIO* bio = BIO_new(BIO_s_mem());
  240. if (!bio) {
  241. FATAL() << "Unreachable code.";
  242. }
  243. if (!i2d_X509_bio(bio, x509_)) {
  244. BIO_free(bio);
  245. FATAL() << "Unreachable code.";
  246. }
  247. char* data = nullptr;
  248. size_t length = BIO_get_mem_data(bio, &data);
  249. der_buffer->SetData(data, length);
  250. BIO_free(bio);
  251. }
  252. bool OpenSSLCertificate::operator==(const OpenSSLCertificate& other) const {
  253. return X509_cmp(x509_, other.x509_) == 0;
  254. }
  255. bool OpenSSLCertificate::operator!=(const OpenSSLCertificate& other) const {
  256. return !(*this == other);
  257. }
  258. int64_t OpenSSLCertificate::CertificateExpirationTime() const {
  259. ASN1_TIME* expire_time = X509_get_notAfter(x509_);
  260. bool long_format;
  261. if (expire_time->type == V_ASN1_UTCTIME) {
  262. long_format = false;
  263. } else if (expire_time->type == V_ASN1_GENERALIZEDTIME) {
  264. long_format = true;
  265. } else {
  266. return -1;
  267. }
  268. return ASN1TimeToSec(expire_time->data, expire_time->length, long_format);
  269. }
  270. } // namespace rtc