blob: ca4cd1ce4ff4573bc53c6f334789392e4fca756e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
11// Handling of certificates and keypairs for SSLStreamAdapter's peer mode.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "rtc_base/sslidentity.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +010014#include <ctime>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <string>
David Benjamin3df76b12017-09-29 12:27:18 -040016#include <utility>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/base64.h"
19#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
21#include "rtc_base/opensslidentity.h"
David Benjamin3df76b12017-09-29 12:27:18 -040022#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/sslfingerprint.h"
deadbeeff33491e2017-01-20 17:01:45 -080024
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025namespace rtc {
26
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070027//////////////////////////////////////////////////////////////////////
28// KeyParams
29//////////////////////////////////////////////////////////////////////
30
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031const char kPemTypeCertificate[] = "CERTIFICATE";
32const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020033const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034
torbjorng4e572472015-10-08 09:42:49 -070035KeyParams::KeyParams(KeyType key_type) {
36 if (key_type == KT_ECDSA) {
37 type_ = KT_ECDSA;
38 params_.curve = EC_NIST_P256;
39 } else if (key_type == KT_RSA) {
40 type_ = KT_RSA;
41 params_.rsa.mod_size = kRsaDefaultModSize;
42 params_.rsa.pub_exp = kRsaDefaultExponent;
43 } else {
44 RTC_NOTREACHED();
45 }
46}
47
48// static
49KeyParams KeyParams::RSA(int mod_size, int pub_exp) {
50 KeyParams kt(KT_RSA);
51 kt.params_.rsa.mod_size = mod_size;
52 kt.params_.rsa.pub_exp = pub_exp;
53 return kt;
54}
55
56// static
57KeyParams KeyParams::ECDSA(ECCurve curve) {
58 KeyParams kt(KT_ECDSA);
59 kt.params_.curve = curve;
60 return kt;
61}
62
63bool KeyParams::IsValid() const {
64 if (type_ == KT_RSA) {
65 return (params_.rsa.mod_size >= kRsaMinModSize &&
66 params_.rsa.mod_size <= kRsaMaxModSize &&
67 params_.rsa.pub_exp > params_.rsa.mod_size);
68 } else if (type_ == KT_ECDSA) {
69 return (params_.curve == EC_NIST_P256);
70 }
71 return false;
72}
73
74RSAParams KeyParams::rsa_params() const {
75 RTC_DCHECK(type_ == KT_RSA);
76 return params_.rsa;
77}
78
79ECCurve KeyParams::ec_curve() const {
80 RTC_DCHECK(type_ == KT_ECDSA);
81 return params_.curve;
82}
83
Henrik Boström9b5476d2015-09-22 14:12:57 +020084KeyType IntKeyTypeFamilyToKeyType(int key_type_family) {
85 return static_cast<KeyType>(key_type_family);
86}
87
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070088//////////////////////////////////////////////////////////////////////
89// SSLIdentity
90//////////////////////////////////////////////////////////////////////
91
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000092bool SSLIdentity::PemToDer(const std::string& pem_type,
93 const std::string& pem_string,
94 std::string* der) {
95 // Find the inner body. We need this to fulfill the contract of
96 // returning pem_length.
97 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
98 if (header == std::string::npos)
99 return false;
100
101 size_t body = pem_string.find("\n", header);
102 if (body == std::string::npos)
103 return false;
104
105 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
106 if (trailer == std::string::npos)
107 return false;
108
109 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
110
Yves Gerey665174f2018-06-19 15:03:05 +0200111 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE | Base64::DO_PAD_ANY |
112 Base64::DO_TERM_BUFFER);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113 return true;
114}
115
116std::string SSLIdentity::DerToPem(const std::string& pem_type,
117 const unsigned char* data,
118 size_t length) {
119 std::stringstream result;
120
121 result << "-----BEGIN " << pem_type << "-----\n";
122
123 std::string b64_encoded;
124 Base64::EncodeFromArray(data, length, &b64_encoded);
125
126 // Divide the Base-64 encoded data into 64-character chunks, as per
127 // 4.3.2.4 of RFC 1421.
128 static const size_t kChunkSize = 64;
129 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
130 for (size_t i = 0, chunk_offset = 0; i < chunks;
131 ++i, chunk_offset += kChunkSize) {
132 result << b64_encoded.substr(chunk_offset, kChunkSize);
133 result << "\n";
134 }
135
136 result << "-----END " << pem_type << "-----\n";
137
138 return result.str();
139}
140
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100141// static
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200142SSLIdentity* SSLIdentity::GenerateWithExpiration(const std::string& common_name,
143 const KeyParams& key_params,
144 time_t certificate_lifetime) {
145 return OpenSSLIdentity::GenerateWithExpiration(common_name, key_params,
146 certificate_lifetime);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147}
148
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100149// static
150SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
151 const KeyParams& key_params) {
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200152 return OpenSSLIdentity::GenerateWithExpiration(
153 common_name, key_params, kDefaultCertificateLifetimeInSeconds);
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100154}
155
156// static
157SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
158 KeyType key_type) {
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200159 return OpenSSLIdentity::GenerateWithExpiration(
160 common_name, KeyParams(key_type), kDefaultCertificateLifetimeInSeconds);
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100161}
162
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
164 return OpenSSLIdentity::GenerateForTest(params);
165}
166
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100167// static
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
169 const std::string& certificate) {
170 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
171}
172
Jian Cui0a8798b2017-11-16 16:58:02 -0800173// static
174SSLIdentity* SSLIdentity::FromPEMChainStrings(
175 const std::string& private_key,
176 const std::string& certificate_chain) {
177 return OpenSSLIdentity::FromPEMChainStrings(private_key, certificate_chain);
178}
179
hbos6b470a92016-04-28 05:14:21 -0700180bool operator==(const SSLIdentity& a, const SSLIdentity& b) {
181 return static_cast<const OpenSSLIdentity&>(a) ==
182 static_cast<const OpenSSLIdentity&>(b);
183}
184bool operator!=(const SSLIdentity& a, const SSLIdentity& b) {
185 return !(a == b);
186}
187
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700188//////////////////////////////////////////////////////////////////////
189// Helper Functions
190//////////////////////////////////////////////////////////////////////
191
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100192// Read |n| bytes from ASN1 number string at *|pp| and return the numeric value.
193// Update *|pp| and *|np| to reflect number of read bytes.
194static inline int ASN1ReadInt(const unsigned char** pp, size_t* np, size_t n) {
195 const unsigned char* p = *pp;
196 int x = 0;
197 for (size_t i = 0; i < n; i++)
198 x = 10 * x + p[i] - '0';
199 *pp = p + n;
200 *np = *np - n;
201 return x;
202}
203
204int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format) {
205 size_t bytes_left = length;
206
207 // Make sure the string ends with Z. Doing it here protects the strspn call
208 // from running off the end of the string in Z's absense.
209 if (length == 0 || s[length - 1] != 'Z')
210 return -1;
211
212 // Make sure we only have ASCII digits so that we don't need to clutter the
213 // code below and ASN1ReadInt with error checking.
214 size_t n = strspn(reinterpret_cast<const char*>(s), "0123456789");
215 if (n + 1 != length)
216 return -1;
217
218 int year;
219
220 // Read out ASN1 year, in either 2-char "UTCTIME" or 4-char "GENERALIZEDTIME"
221 // format. Both format use UTC in this context.
222 if (long_format) {
223 // ASN1 format: yyyymmddhh[mm[ss[.fff]]]Z where the Z is literal, but
224 // RFC 5280 requires us to only support exactly yyyymmddhhmmssZ.
225
226 if (bytes_left < 11)
227 return -1;
228
229 year = ASN1ReadInt(&s, &bytes_left, 4);
230 year -= 1900;
231 } else {
232 // ASN1 format: yymmddhhmm[ss]Z where the Z is literal, but RFC 5280
233 // requires us to only support exactly yymmddhhmmssZ.
234
235 if (bytes_left < 9)
236 return -1;
237
238 year = ASN1ReadInt(&s, &bytes_left, 2);
239 if (year < 50) // Per RFC 5280 4.1.2.5.1
240 year += 100;
241 }
242
243 std::tm tm;
244 tm.tm_year = year;
245
246 // Read out remaining ASN1 time data and store it in |tm| in documented
247 // std::tm format.
248 tm.tm_mon = ASN1ReadInt(&s, &bytes_left, 2) - 1;
249 tm.tm_mday = ASN1ReadInt(&s, &bytes_left, 2);
250 tm.tm_hour = ASN1ReadInt(&s, &bytes_left, 2);
251 tm.tm_min = ASN1ReadInt(&s, &bytes_left, 2);
252 tm.tm_sec = ASN1ReadInt(&s, &bytes_left, 2);
253
254 if (bytes_left != 1) {
255 // Now just Z should remain. Its existence was asserted above.
256 return -1;
257 }
258
259 return TmToSeconds(tm);
260}
261
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000262} // namespace rtc