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.

259 lines
8.2 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. // Handling of certificates and keypairs for SSLStreamAdapter's peer mode.
  11. #include "rtc_base/ssl_identity.h"
  12. #include <string.h>
  13. #include <time.h>
  14. #include <string>
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/openssl_identity.h"
  17. #include "rtc_base/ssl_certificate.h"
  18. #include "rtc_base/strings/string_builder.h"
  19. #include "rtc_base/third_party/base64/base64.h"
  20. #include "rtc_base/time_utils.h"
  21. namespace rtc {
  22. //////////////////////////////////////////////////////////////////////
  23. // Helper Functions
  24. //////////////////////////////////////////////////////////////////////
  25. namespace {
  26. // Read |n| bytes from ASN1 number string at *|pp| and return the numeric value.
  27. // Update *|pp| and *|np| to reflect number of read bytes.
  28. // TODO(bugs.webrtc.org/9860) - Remove this code.
  29. inline int ASN1ReadInt(const unsigned char** pp, size_t* np, size_t n) {
  30. const unsigned char* p = *pp;
  31. int x = 0;
  32. for (size_t i = 0; i < n; i++) {
  33. x = 10 * x + p[i] - '0';
  34. }
  35. *pp = p + n;
  36. *np = *np - n;
  37. return x;
  38. }
  39. } // namespace
  40. // TODO(bugs.webrtc.org/9860) - Remove this code.
  41. int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format) {
  42. size_t bytes_left = length;
  43. // Make sure the string ends with Z. Doing it here protects the strspn call
  44. // from running off the end of the string in Z's absense.
  45. if (length == 0 || s[length - 1] != 'Z') {
  46. return -1;
  47. }
  48. // Make sure we only have ASCII digits so that we don't need to clutter the
  49. // code below and ASN1ReadInt with error checking.
  50. size_t n = strspn(reinterpret_cast<const char*>(s), "0123456789");
  51. if (n + 1 != length) {
  52. return -1;
  53. }
  54. // Read out ASN1 year, in either 2-char "UTCTIME" or 4-char "GENERALIZEDTIME"
  55. // format. Both format use UTC in this context.
  56. int year = 0;
  57. if (long_format) {
  58. // ASN1 format: yyyymmddhh[mm[ss[.fff]]]Z where the Z is literal, but
  59. // RFC 5280 requires us to only support exactly yyyymmddhhmmssZ.
  60. if (bytes_left < 11) {
  61. return -1;
  62. }
  63. year = ASN1ReadInt(&s, &bytes_left, 4);
  64. year -= 1900;
  65. } else {
  66. // ASN1 format: yymmddhhmm[ss]Z where the Z is literal, but RFC 5280
  67. // requires us to only support exactly yymmddhhmmssZ.
  68. if (bytes_left < 9) {
  69. return -1;
  70. }
  71. year = ASN1ReadInt(&s, &bytes_left, 2);
  72. // Per RFC 5280 4.1.2.5.1
  73. if (year < 50) {
  74. year += 100;
  75. }
  76. }
  77. // Read out remaining ASN1 time data and store it in |tm| in documented
  78. // std::tm format.
  79. tm tm;
  80. tm.tm_year = year;
  81. tm.tm_mon = ASN1ReadInt(&s, &bytes_left, 2) - 1;
  82. tm.tm_mday = ASN1ReadInt(&s, &bytes_left, 2);
  83. tm.tm_hour = ASN1ReadInt(&s, &bytes_left, 2);
  84. tm.tm_min = ASN1ReadInt(&s, &bytes_left, 2);
  85. tm.tm_sec = ASN1ReadInt(&s, &bytes_left, 2);
  86. // Now just Z should remain. Its existence was asserted above.
  87. if (bytes_left != 1) {
  88. return -1;
  89. }
  90. return TmToSeconds(tm);
  91. }
  92. //////////////////////////////////////////////////////////////////////
  93. // KeyParams
  94. //////////////////////////////////////////////////////////////////////
  95. const char kPemTypeCertificate[] = "CERTIFICATE";
  96. const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
  97. const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
  98. KeyParams::KeyParams(KeyType key_type) {
  99. if (key_type == KT_ECDSA) {
  100. type_ = KT_ECDSA;
  101. params_.curve = EC_NIST_P256;
  102. } else if (key_type == KT_RSA) {
  103. type_ = KT_RSA;
  104. params_.rsa.mod_size = kRsaDefaultModSize;
  105. params_.rsa.pub_exp = kRsaDefaultExponent;
  106. } else {
  107. RTC_NOTREACHED();
  108. }
  109. }
  110. // static
  111. KeyParams KeyParams::RSA(int mod_size, int pub_exp) {
  112. KeyParams kt(KT_RSA);
  113. kt.params_.rsa.mod_size = mod_size;
  114. kt.params_.rsa.pub_exp = pub_exp;
  115. return kt;
  116. }
  117. // static
  118. KeyParams KeyParams::ECDSA(ECCurve curve) {
  119. KeyParams kt(KT_ECDSA);
  120. kt.params_.curve = curve;
  121. return kt;
  122. }
  123. bool KeyParams::IsValid() const {
  124. if (type_ == KT_RSA) {
  125. return (params_.rsa.mod_size >= kRsaMinModSize &&
  126. params_.rsa.mod_size <= kRsaMaxModSize &&
  127. params_.rsa.pub_exp > params_.rsa.mod_size);
  128. } else if (type_ == KT_ECDSA) {
  129. return (params_.curve == EC_NIST_P256);
  130. }
  131. return false;
  132. }
  133. RSAParams KeyParams::rsa_params() const {
  134. RTC_DCHECK(type_ == KT_RSA);
  135. return params_.rsa;
  136. }
  137. ECCurve KeyParams::ec_curve() const {
  138. RTC_DCHECK(type_ == KT_ECDSA);
  139. return params_.curve;
  140. }
  141. KeyType IntKeyTypeFamilyToKeyType(int key_type_family) {
  142. return static_cast<KeyType>(key_type_family);
  143. }
  144. //////////////////////////////////////////////////////////////////////
  145. // SSLIdentity
  146. //////////////////////////////////////////////////////////////////////
  147. bool SSLIdentity::PemToDer(const std::string& pem_type,
  148. const std::string& pem_string,
  149. std::string* der) {
  150. // Find the inner body. We need this to fulfill the contract of returning
  151. // pem_length.
  152. size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
  153. if (header == std::string::npos) {
  154. return false;
  155. }
  156. size_t body = pem_string.find('\n', header);
  157. if (body == std::string::npos) {
  158. return false;
  159. }
  160. size_t trailer = pem_string.find("-----END " + pem_type + "-----");
  161. if (trailer == std::string::npos) {
  162. return false;
  163. }
  164. std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
  165. *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE | Base64::DO_PAD_ANY |
  166. Base64::DO_TERM_BUFFER);
  167. return true;
  168. }
  169. std::string SSLIdentity::DerToPem(const std::string& pem_type,
  170. const unsigned char* data,
  171. size_t length) {
  172. rtc::StringBuilder result;
  173. result << "-----BEGIN " << pem_type << "-----\n";
  174. std::string b64_encoded;
  175. Base64::EncodeFromArray(data, length, &b64_encoded);
  176. // Divide the Base-64 encoded data into 64-character chunks, as per 4.3.2.4
  177. // of RFC 1421.
  178. static const size_t kChunkSize = 64;
  179. size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
  180. for (size_t i = 0, chunk_offset = 0; i < chunks;
  181. ++i, chunk_offset += kChunkSize) {
  182. result << b64_encoded.substr(chunk_offset, kChunkSize);
  183. result << "\n";
  184. }
  185. result << "-----END " << pem_type << "-----\n";
  186. return result.Release();
  187. }
  188. // static
  189. SSLIdentity* SSLIdentity::GenerateWithExpiration(const std::string& common_name,
  190. const KeyParams& key_params,
  191. time_t certificate_lifetime) {
  192. return OpenSSLIdentity::GenerateWithExpiration(common_name, key_params,
  193. certificate_lifetime);
  194. }
  195. // static
  196. SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
  197. const KeyParams& key_params) {
  198. return OpenSSLIdentity::GenerateWithExpiration(
  199. common_name, key_params, kDefaultCertificateLifetimeInSeconds);
  200. }
  201. // static
  202. SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
  203. KeyType key_type) {
  204. return OpenSSLIdentity::GenerateWithExpiration(
  205. common_name, KeyParams(key_type), kDefaultCertificateLifetimeInSeconds);
  206. }
  207. SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
  208. return OpenSSLIdentity::GenerateForTest(params);
  209. }
  210. // static
  211. SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
  212. const std::string& certificate) {
  213. return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
  214. }
  215. // static
  216. SSLIdentity* SSLIdentity::FromPEMChainStrings(
  217. const std::string& private_key,
  218. const std::string& certificate_chain) {
  219. return OpenSSLIdentity::FromPEMChainStrings(private_key, certificate_chain);
  220. }
  221. bool operator==(const SSLIdentity& a, const SSLIdentity& b) {
  222. return static_cast<const OpenSSLIdentity&>(a) ==
  223. static_cast<const OpenSSLIdentity&>(b);
  224. }
  225. bool operator!=(const SSLIdentity& a, const SSLIdentity& b) {
  226. return !(a == b);
  227. }
  228. } // namespace rtc