blob: 6fd7ad5bbd9608104f875521eb8f9996d8b73d0e [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>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/base64.h"
18#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/opensslidentity.h"
21#include "rtc_base/sslfingerprint.h"
deadbeeff33491e2017-01-20 17:01:45 -080022
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023namespace rtc {
24
25const char kPemTypeCertificate[] = "CERTIFICATE";
26const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020027const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
hbose29352b2016-08-25 03:52:38 -070029SSLCertificateStats::SSLCertificateStats(
30 std::string&& fingerprint,
31 std::string&& fingerprint_algorithm,
32 std::string&& base64_certificate,
33 std::unique_ptr<SSLCertificateStats>&& issuer)
34 : fingerprint(std::move(fingerprint)),
35 fingerprint_algorithm(std::move(fingerprint_algorithm)),
36 base64_certificate(std::move(base64_certificate)),
37 issuer(std::move(issuer)) {
38}
39
40SSLCertificateStats::~SSLCertificateStats() {
41}
42
43std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats() const {
44 // We have a certificate and optionally a chain of certificates. This forms a
45 // linked list, starting with |this|, then the first element of |chain| and
46 // ending with the last element of |chain|. The "issuer" of a certificate is
47 // the next certificate in the chain. Stats are produced for each certificate
48 // in the list. Here, the "issuer" is the issuer's stats.
49 std::unique_ptr<SSLCertChain> chain = GetChain();
50 std::unique_ptr<SSLCertificateStats> issuer;
51 if (chain) {
52 // The loop runs in reverse so that the |issuer| is known before the
53 // |cert|'s stats.
54 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
55 const SSLCertificate* cert = &chain->Get(i);
56 issuer = cert->GetStats(std::move(issuer));
57 }
58 }
59 return GetStats(std::move(issuer));
60}
61
62std::unique_ptr<SSLCertificateStats> SSLCertificate::GetStats(
63 std::unique_ptr<SSLCertificateStats> issuer) const {
64 // TODO(bemasc): Move this computation to a helper class that caches these
65 // values to reduce CPU use in |StatsCollector::GetStats|. This will require
66 // adding a fast |SSLCertificate::Equals| to detect certificate changes.
67 std::string digest_algorithm;
68 if (!GetSignatureDigestAlgorithm(&digest_algorithm))
69 return nullptr;
70
71 // |SSLFingerprint::Create| can fail if the algorithm returned by
72 // |SSLCertificate::GetSignatureDigestAlgorithm| is not supported by the
73 // implementation of |SSLCertificate::ComputeDigest|. This currently happens
74 // with MD5- and SHA-224-signed certificates when linked to libNSS.
75 std::unique_ptr<SSLFingerprint> ssl_fingerprint(
76 SSLFingerprint::Create(digest_algorithm, this));
77 if (!ssl_fingerprint)
78 return nullptr;
79 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
80
81 Buffer der_buffer;
82 ToDER(&der_buffer);
83 std::string der_base64;
84 Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(), &der_base64);
85
86 return std::unique_ptr<SSLCertificateStats>(new SSLCertificateStats(
87 std::move(fingerprint),
88 std::move(digest_algorithm),
89 std::move(der_base64),
90 std::move(issuer)));
91}
92
torbjorng4e572472015-10-08 09:42:49 -070093KeyParams::KeyParams(KeyType key_type) {
94 if (key_type == KT_ECDSA) {
95 type_ = KT_ECDSA;
96 params_.curve = EC_NIST_P256;
97 } else if (key_type == KT_RSA) {
98 type_ = KT_RSA;
99 params_.rsa.mod_size = kRsaDefaultModSize;
100 params_.rsa.pub_exp = kRsaDefaultExponent;
101 } else {
102 RTC_NOTREACHED();
103 }
104}
105
106// static
107KeyParams KeyParams::RSA(int mod_size, int pub_exp) {
108 KeyParams kt(KT_RSA);
109 kt.params_.rsa.mod_size = mod_size;
110 kt.params_.rsa.pub_exp = pub_exp;
111 return kt;
112}
113
114// static
115KeyParams KeyParams::ECDSA(ECCurve curve) {
116 KeyParams kt(KT_ECDSA);
117 kt.params_.curve = curve;
118 return kt;
119}
120
121bool KeyParams::IsValid() const {
122 if (type_ == KT_RSA) {
123 return (params_.rsa.mod_size >= kRsaMinModSize &&
124 params_.rsa.mod_size <= kRsaMaxModSize &&
125 params_.rsa.pub_exp > params_.rsa.mod_size);
126 } else if (type_ == KT_ECDSA) {
127 return (params_.curve == EC_NIST_P256);
128 }
129 return false;
130}
131
132RSAParams KeyParams::rsa_params() const {
133 RTC_DCHECK(type_ == KT_RSA);
134 return params_.rsa;
135}
136
137ECCurve KeyParams::ec_curve() const {
138 RTC_DCHECK(type_ == KT_ECDSA);
139 return params_.curve;
140}
141
Henrik Boström9b5476d2015-09-22 14:12:57 +0200142KeyType IntKeyTypeFamilyToKeyType(int key_type_family) {
143 return static_cast<KeyType>(key_type_family);
144}
145
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146bool SSLIdentity::PemToDer(const std::string& pem_type,
147 const std::string& pem_string,
148 std::string* der) {
149 // Find the inner body. We need this to fulfill the contract of
150 // returning pem_length.
151 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
152 if (header == std::string::npos)
153 return false;
154
155 size_t body = pem_string.find("\n", header);
156 if (body == std::string::npos)
157 return false;
158
159 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
160 if (trailer == std::string::npos)
161 return false;
162
163 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
164
165 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
166 Base64::DO_PAD_ANY |
167 Base64::DO_TERM_BUFFER);
168 return true;
169}
170
171std::string SSLIdentity::DerToPem(const std::string& pem_type,
172 const unsigned char* data,
173 size_t length) {
174 std::stringstream result;
175
176 result << "-----BEGIN " << pem_type << "-----\n";
177
178 std::string b64_encoded;
179 Base64::EncodeFromArray(data, length, &b64_encoded);
180
181 // Divide the Base-64 encoded data into 64-character chunks, as per
182 // 4.3.2.4 of RFC 1421.
183 static const size_t kChunkSize = 64;
184 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
185 for (size_t i = 0, chunk_offset = 0; i < chunks;
186 ++i, chunk_offset += kChunkSize) {
187 result << b64_encoded.substr(chunk_offset, kChunkSize);
188 result << "\n";
189 }
190
191 result << "-----END " << pem_type << "-----\n";
192
193 return result.str();
194}
195
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000196SSLCertChain::SSLCertChain(const std::vector<SSLCertificate*>& certs) {
nisseede5da42017-01-12 05:15:36 -0800197 RTC_DCHECK(!certs.empty());
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000198 certs_.resize(certs.size());
199 std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert);
200}
201
202SSLCertChain::SSLCertChain(const SSLCertificate* cert) {
203 certs_.push_back(cert->GetReference());
204}
205
206SSLCertChain::~SSLCertChain() {
207 std::for_each(certs_.begin(), certs_.end(), DeleteCert);
208}
209
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100210// static
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
212 return OpenSSLCertificate::FromPEMString(pem_string);
213}
214
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100215// static
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200216SSLIdentity* SSLIdentity::GenerateWithExpiration(const std::string& common_name,
217 const KeyParams& key_params,
218 time_t certificate_lifetime) {
219 return OpenSSLIdentity::GenerateWithExpiration(common_name, key_params,
220 certificate_lifetime);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221}
222
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100223// static
224SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
225 const KeyParams& key_params) {
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200226 return OpenSSLIdentity::GenerateWithExpiration(
227 common_name, key_params, kDefaultCertificateLifetimeInSeconds);
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100228}
229
230// static
231SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
232 KeyType key_type) {
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200233 return OpenSSLIdentity::GenerateWithExpiration(
234 common_name, KeyParams(key_type), kDefaultCertificateLifetimeInSeconds);
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100235}
236
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
238 return OpenSSLIdentity::GenerateForTest(params);
239}
240
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100241// static
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000242SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
243 const std::string& certificate) {
244 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
245}
246
hbos6b470a92016-04-28 05:14:21 -0700247bool operator==(const SSLIdentity& a, const SSLIdentity& b) {
248 return static_cast<const OpenSSLIdentity&>(a) ==
249 static_cast<const OpenSSLIdentity&>(b);
250}
251bool operator!=(const SSLIdentity& a, const SSLIdentity& b) {
252 return !(a == b);
253}
254
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100255// Read |n| bytes from ASN1 number string at *|pp| and return the numeric value.
256// Update *|pp| and *|np| to reflect number of read bytes.
257static inline int ASN1ReadInt(const unsigned char** pp, size_t* np, size_t n) {
258 const unsigned char* p = *pp;
259 int x = 0;
260 for (size_t i = 0; i < n; i++)
261 x = 10 * x + p[i] - '0';
262 *pp = p + n;
263 *np = *np - n;
264 return x;
265}
266
267int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format) {
268 size_t bytes_left = length;
269
270 // Make sure the string ends with Z. Doing it here protects the strspn call
271 // from running off the end of the string in Z's absense.
272 if (length == 0 || s[length - 1] != 'Z')
273 return -1;
274
275 // Make sure we only have ASCII digits so that we don't need to clutter the
276 // code below and ASN1ReadInt with error checking.
277 size_t n = strspn(reinterpret_cast<const char*>(s), "0123456789");
278 if (n + 1 != length)
279 return -1;
280
281 int year;
282
283 // Read out ASN1 year, in either 2-char "UTCTIME" or 4-char "GENERALIZEDTIME"
284 // format. Both format use UTC in this context.
285 if (long_format) {
286 // ASN1 format: yyyymmddhh[mm[ss[.fff]]]Z where the Z is literal, but
287 // RFC 5280 requires us to only support exactly yyyymmddhhmmssZ.
288
289 if (bytes_left < 11)
290 return -1;
291
292 year = ASN1ReadInt(&s, &bytes_left, 4);
293 year -= 1900;
294 } else {
295 // ASN1 format: yymmddhhmm[ss]Z where the Z is literal, but RFC 5280
296 // requires us to only support exactly yymmddhhmmssZ.
297
298 if (bytes_left < 9)
299 return -1;
300
301 year = ASN1ReadInt(&s, &bytes_left, 2);
302 if (year < 50) // Per RFC 5280 4.1.2.5.1
303 year += 100;
304 }
305
306 std::tm tm;
307 tm.tm_year = year;
308
309 // Read out remaining ASN1 time data and store it in |tm| in documented
310 // std::tm format.
311 tm.tm_mon = ASN1ReadInt(&s, &bytes_left, 2) - 1;
312 tm.tm_mday = ASN1ReadInt(&s, &bytes_left, 2);
313 tm.tm_hour = ASN1ReadInt(&s, &bytes_left, 2);
314 tm.tm_min = ASN1ReadInt(&s, &bytes_left, 2);
315 tm.tm_sec = ASN1ReadInt(&s, &bytes_left, 2);
316
317 if (bytes_left != 1) {
318 // Now just Z should remain. Its existence was asserted above.
319 return -1;
320 }
321
322 return TmToSeconds(tm);
323}
324
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000325} // namespace rtc