blob: 823fc388a3cef1b8150f60cf698b8736a1642b37 [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
Karl Wiberg918f50c2018-07-05 11:40:33 +020018#include "absl/memory/memory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
21#include "rtc_base/opensslidentity.h"
22#include "rtc_base/sslfingerprint.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020023#include "rtc_base/strings/string_builder.h"
Artem Titova76af0c2018-07-23 17:38:12 +020024#include "rtc_base/third_party/base64/base64.h"
deadbeeff33491e2017-01-20 17:01:45 -080025
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026namespace rtc {
27
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070028//////////////////////////////////////////////////////////////////////
29// KeyParams
30//////////////////////////////////////////////////////////////////////
31
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032const char kPemTypeCertificate[] = "CERTIFICATE";
33const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020034const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
torbjorng4e572472015-10-08 09:42:49 -070036KeyParams::KeyParams(KeyType key_type) {
37 if (key_type == KT_ECDSA) {
38 type_ = KT_ECDSA;
39 params_.curve = EC_NIST_P256;
40 } else if (key_type == KT_RSA) {
41 type_ = KT_RSA;
42 params_.rsa.mod_size = kRsaDefaultModSize;
43 params_.rsa.pub_exp = kRsaDefaultExponent;
44 } else {
45 RTC_NOTREACHED();
46 }
47}
48
49// static
50KeyParams KeyParams::RSA(int mod_size, int pub_exp) {
51 KeyParams kt(KT_RSA);
52 kt.params_.rsa.mod_size = mod_size;
53 kt.params_.rsa.pub_exp = pub_exp;
54 return kt;
55}
56
57// static
58KeyParams KeyParams::ECDSA(ECCurve curve) {
59 KeyParams kt(KT_ECDSA);
60 kt.params_.curve = curve;
61 return kt;
62}
63
64bool KeyParams::IsValid() const {
65 if (type_ == KT_RSA) {
66 return (params_.rsa.mod_size >= kRsaMinModSize &&
67 params_.rsa.mod_size <= kRsaMaxModSize &&
68 params_.rsa.pub_exp > params_.rsa.mod_size);
69 } else if (type_ == KT_ECDSA) {
70 return (params_.curve == EC_NIST_P256);
71 }
72 return false;
73}
74
75RSAParams KeyParams::rsa_params() const {
76 RTC_DCHECK(type_ == KT_RSA);
77 return params_.rsa;
78}
79
80ECCurve KeyParams::ec_curve() const {
81 RTC_DCHECK(type_ == KT_ECDSA);
82 return params_.curve;
83}
84
Henrik Boström9b5476d2015-09-22 14:12:57 +020085KeyType IntKeyTypeFamilyToKeyType(int key_type_family) {
86 return static_cast<KeyType>(key_type_family);
87}
88
Benjamin Wrightd6f86e82018-05-08 13:12:25 -070089//////////////////////////////////////////////////////////////////////
90// SSLIdentity
91//////////////////////////////////////////////////////////////////////
92
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093bool SSLIdentity::PemToDer(const std::string& pem_type,
94 const std::string& pem_string,
95 std::string* der) {
96 // Find the inner body. We need this to fulfill the contract of
97 // returning pem_length.
98 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
99 if (header == std::string::npos)
100 return false;
101
102 size_t body = pem_string.find("\n", header);
103 if (body == std::string::npos)
104 return false;
105
106 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
107 if (trailer == std::string::npos)
108 return false;
109
110 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
111
Yves Gerey665174f2018-06-19 15:03:05 +0200112 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE | Base64::DO_PAD_ANY |
113 Base64::DO_TERM_BUFFER);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 return true;
115}
116
117std::string SSLIdentity::DerToPem(const std::string& pem_type,
118 const unsigned char* data,
119 size_t length) {
Jonas Olsson366a50c2018-09-06 13:41:30 +0200120 rtc::StringBuilder result;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121
122 result << "-----BEGIN " << pem_type << "-----\n";
123
124 std::string b64_encoded;
125 Base64::EncodeFromArray(data, length, &b64_encoded);
126
127 // Divide the Base-64 encoded data into 64-character chunks, as per
128 // 4.3.2.4 of RFC 1421.
129 static const size_t kChunkSize = 64;
130 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
131 for (size_t i = 0, chunk_offset = 0; i < chunks;
132 ++i, chunk_offset += kChunkSize) {
133 result << b64_encoded.substr(chunk_offset, kChunkSize);
134 result << "\n";
135 }
136
137 result << "-----END " << pem_type << "-----\n";
138
Jonas Olsson84df1c72018-09-14 16:59:32 +0200139 return result.Release();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000140}
141
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100142// static
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200143SSLIdentity* SSLIdentity::GenerateWithExpiration(const std::string& common_name,
144 const KeyParams& key_params,
145 time_t certificate_lifetime) {
146 return OpenSSLIdentity::GenerateWithExpiration(common_name, key_params,
147 certificate_lifetime);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148}
149
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100150// static
151SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
152 const KeyParams& key_params) {
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200153 return OpenSSLIdentity::GenerateWithExpiration(
154 common_name, key_params, kDefaultCertificateLifetimeInSeconds);
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100155}
156
157// static
158SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
159 KeyType key_type) {
Torbjorn Granlund1d846b22016-03-31 16:21:04 +0200160 return OpenSSLIdentity::GenerateWithExpiration(
161 common_name, KeyParams(key_type), kDefaultCertificateLifetimeInSeconds);
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100162}
163
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000164SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
165 return OpenSSLIdentity::GenerateForTest(params);
166}
167
Torbjorn Granlunda3dc79e2016-02-16 13:33:53 +0100168// static
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
170 const std::string& certificate) {
171 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
172}
173
Jian Cui0a8798b2017-11-16 16:58:02 -0800174// static
175SSLIdentity* SSLIdentity::FromPEMChainStrings(
176 const std::string& private_key,
177 const std::string& certificate_chain) {
178 return OpenSSLIdentity::FromPEMChainStrings(private_key, certificate_chain);
179}
180
hbos6b470a92016-04-28 05:14:21 -0700181bool operator==(const SSLIdentity& a, const SSLIdentity& b) {
182 return static_cast<const OpenSSLIdentity&>(a) ==
183 static_cast<const OpenSSLIdentity&>(b);
184}
185bool operator!=(const SSLIdentity& a, const SSLIdentity& b) {
186 return !(a == b);
187}
188
Benjamin Wrightd6f86e82018-05-08 13:12:25 -0700189//////////////////////////////////////////////////////////////////////
190// Helper Functions
191//////////////////////////////////////////////////////////////////////
192
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100193// Read |n| bytes from ASN1 number string at *|pp| and return the numeric value.
194// Update *|pp| and *|np| to reflect number of read bytes.
195static inline int ASN1ReadInt(const unsigned char** pp, size_t* np, size_t n) {
196 const unsigned char* p = *pp;
197 int x = 0;
198 for (size_t i = 0; i < n; i++)
199 x = 10 * x + p[i] - '0';
200 *pp = p + n;
201 *np = *np - n;
202 return x;
203}
204
205int64_t ASN1TimeToSec(const unsigned char* s, size_t length, bool long_format) {
206 size_t bytes_left = length;
207
208 // Make sure the string ends with Z. Doing it here protects the strspn call
209 // from running off the end of the string in Z's absense.
210 if (length == 0 || s[length - 1] != 'Z')
211 return -1;
212
213 // Make sure we only have ASCII digits so that we don't need to clutter the
214 // code below and ASN1ReadInt with error checking.
215 size_t n = strspn(reinterpret_cast<const char*>(s), "0123456789");
216 if (n + 1 != length)
217 return -1;
218
219 int year;
220
221 // Read out ASN1 year, in either 2-char "UTCTIME" or 4-char "GENERALIZEDTIME"
222 // format. Both format use UTC in this context.
223 if (long_format) {
224 // ASN1 format: yyyymmddhh[mm[ss[.fff]]]Z where the Z is literal, but
225 // RFC 5280 requires us to only support exactly yyyymmddhhmmssZ.
226
227 if (bytes_left < 11)
228 return -1;
229
230 year = ASN1ReadInt(&s, &bytes_left, 4);
231 year -= 1900;
232 } else {
233 // ASN1 format: yymmddhhmm[ss]Z where the Z is literal, but RFC 5280
234 // requires us to only support exactly yymmddhhmmssZ.
235
236 if (bytes_left < 9)
237 return -1;
238
239 year = ASN1ReadInt(&s, &bytes_left, 2);
240 if (year < 50) // Per RFC 5280 4.1.2.5.1
241 year += 100;
242 }
243
244 std::tm tm;
245 tm.tm_year = year;
246
247 // Read out remaining ASN1 time data and store it in |tm| in documented
248 // std::tm format.
249 tm.tm_mon = ASN1ReadInt(&s, &bytes_left, 2) - 1;
250 tm.tm_mday = ASN1ReadInt(&s, &bytes_left, 2);
251 tm.tm_hour = ASN1ReadInt(&s, &bytes_left, 2);
252 tm.tm_min = ASN1ReadInt(&s, &bytes_left, 2);
253 tm.tm_sec = ASN1ReadInt(&s, &bytes_left, 2);
254
255 if (bytes_left != 1) {
256 // Now just Z should remain. Its existence was asserted above.
257 return -1;
258 }
259
260 return TmToSeconds(tm);
261}
262
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000263} // namespace rtc