blob: 180e60c58bbdeea6f67bab7331d93d6cba88dda6 [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.
12#if HAVE_CONFIG_H
13#include "config.h"
14#endif // HAVE_CONFIG_H
15
16#include "webrtc/base/sslidentity.h"
17
18#include <string>
19
20#include "webrtc/base/base64.h"
torbjorng4e572472015-10-08 09:42:49 -070021#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#include "webrtc/base/logging.h"
23#include "webrtc/base/sslconfig.h"
24
torbjorng172f0092015-10-07 04:57:55 -070025#if SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
27#include "webrtc/base/opensslidentity.h"
28
torbjorng172f0092015-10-07 04:57:55 -070029#endif // SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030
31namespace rtc {
32
33const char kPemTypeCertificate[] = "CERTIFICATE";
34const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020035const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036
torbjorng4e572472015-10-08 09:42:49 -070037KeyParams::KeyParams(KeyType key_type) {
38 if (key_type == KT_ECDSA) {
39 type_ = KT_ECDSA;
40 params_.curve = EC_NIST_P256;
41 } else if (key_type == KT_RSA) {
42 type_ = KT_RSA;
43 params_.rsa.mod_size = kRsaDefaultModSize;
44 params_.rsa.pub_exp = kRsaDefaultExponent;
45 } else {
46 RTC_NOTREACHED();
47 }
48}
49
50// static
51KeyParams KeyParams::RSA(int mod_size, int pub_exp) {
52 KeyParams kt(KT_RSA);
53 kt.params_.rsa.mod_size = mod_size;
54 kt.params_.rsa.pub_exp = pub_exp;
55 return kt;
56}
57
58// static
59KeyParams KeyParams::ECDSA(ECCurve curve) {
60 KeyParams kt(KT_ECDSA);
61 kt.params_.curve = curve;
62 return kt;
63}
64
65bool KeyParams::IsValid() const {
66 if (type_ == KT_RSA) {
67 return (params_.rsa.mod_size >= kRsaMinModSize &&
68 params_.rsa.mod_size <= kRsaMaxModSize &&
69 params_.rsa.pub_exp > params_.rsa.mod_size);
70 } else if (type_ == KT_ECDSA) {
71 return (params_.curve == EC_NIST_P256);
72 }
73 return false;
74}
75
76RSAParams KeyParams::rsa_params() const {
77 RTC_DCHECK(type_ == KT_RSA);
78 return params_.rsa;
79}
80
81ECCurve KeyParams::ec_curve() const {
82 RTC_DCHECK(type_ == KT_ECDSA);
83 return params_.curve;
84}
85
Henrik Boström9b5476d2015-09-22 14:12:57 +020086KeyType IntKeyTypeFamilyToKeyType(int key_type_family) {
87 return static_cast<KeyType>(key_type_family);
88}
89
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000090bool SSLIdentity::PemToDer(const std::string& pem_type,
91 const std::string& pem_string,
92 std::string* der) {
93 // Find the inner body. We need this to fulfill the contract of
94 // returning pem_length.
95 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
96 if (header == std::string::npos)
97 return false;
98
99 size_t body = pem_string.find("\n", header);
100 if (body == std::string::npos)
101 return false;
102
103 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
104 if (trailer == std::string::npos)
105 return false;
106
107 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
108
109 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
110 Base64::DO_PAD_ANY |
111 Base64::DO_TERM_BUFFER);
112 return true;
113}
114
115std::string SSLIdentity::DerToPem(const std::string& pem_type,
116 const unsigned char* data,
117 size_t length) {
118 std::stringstream result;
119
120 result << "-----BEGIN " << pem_type << "-----\n";
121
122 std::string b64_encoded;
123 Base64::EncodeFromArray(data, length, &b64_encoded);
124
125 // Divide the Base-64 encoded data into 64-character chunks, as per
126 // 4.3.2.4 of RFC 1421.
127 static const size_t kChunkSize = 64;
128 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
129 for (size_t i = 0, chunk_offset = 0; i < chunks;
130 ++i, chunk_offset += kChunkSize) {
131 result << b64_encoded.substr(chunk_offset, kChunkSize);
132 result << "\n";
133 }
134
135 result << "-----END " << pem_type << "-----\n";
136
137 return result.str();
138}
139
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000140SSLCertChain::SSLCertChain(const std::vector<SSLCertificate*>& certs) {
141 ASSERT(!certs.empty());
142 certs_.resize(certs.size());
143 std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert);
144}
145
146SSLCertChain::SSLCertChain(const SSLCertificate* cert) {
147 certs_.push_back(cert->GetReference());
148}
149
150SSLCertChain::~SSLCertChain() {
151 std::for_each(certs_.begin(), certs_.end(), DeleteCert);
152}
153
torbjorng172f0092015-10-07 04:57:55 -0700154#if SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155
156SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
157 return OpenSSLCertificate::FromPEMString(pem_string);
158}
159
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200160SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
torbjorng4e572472015-10-08 09:42:49 -0700161 const KeyParams& key_params) {
162 return OpenSSLIdentity::Generate(common_name, key_params);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000163}
164
165SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
166 return OpenSSLIdentity::GenerateForTest(params);
167}
168
169SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
170 const std::string& certificate) {
171 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
172}
173
torbjorng172f0092015-10-07 04:57:55 -0700174#else // !SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175
176#error "No SSL implementation"
177
torbjorng172f0092015-10-07 04:57:55 -0700178#endif // SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000179
180} // namespace rtc