blob: 5e565beb8a8c54a7810cba5e3275afd6eec949b2 [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"
21#include "webrtc/base/logging.h"
22#include "webrtc/base/sslconfig.h"
23
24#if SSL_USE_SCHANNEL
25
26#elif SSL_USE_OPENSSL // !SSL_USE_SCHANNEL
27
28#include "webrtc/base/opensslidentity.h"
29
deadbeef9eb13652015-09-05 04:39:15 -070030#elif SSL_USE_NSS // !SSL_USE_SCHANNEL && !SSL_USE_OPENSSL
31
32#include "webrtc/base/nssidentity.h"
33
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034#endif // SSL_USE_SCHANNEL
35
36namespace rtc {
37
38const char kPemTypeCertificate[] = "CERTIFICATE";
39const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020040const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
Henrik Boström9b5476d2015-09-22 14:12:57 +020042KeyType IntKeyTypeFamilyToKeyType(int key_type_family) {
43 return static_cast<KeyType>(key_type_family);
44}
45
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046bool SSLIdentity::PemToDer(const std::string& pem_type,
47 const std::string& pem_string,
48 std::string* der) {
49 // Find the inner body. We need this to fulfill the contract of
50 // returning pem_length.
51 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
52 if (header == std::string::npos)
53 return false;
54
55 size_t body = pem_string.find("\n", header);
56 if (body == std::string::npos)
57 return false;
58
59 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
60 if (trailer == std::string::npos)
61 return false;
62
63 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
64
65 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
66 Base64::DO_PAD_ANY |
67 Base64::DO_TERM_BUFFER);
68 return true;
69}
70
71std::string SSLIdentity::DerToPem(const std::string& pem_type,
72 const unsigned char* data,
73 size_t length) {
74 std::stringstream result;
75
76 result << "-----BEGIN " << pem_type << "-----\n";
77
78 std::string b64_encoded;
79 Base64::EncodeFromArray(data, length, &b64_encoded);
80
81 // Divide the Base-64 encoded data into 64-character chunks, as per
82 // 4.3.2.4 of RFC 1421.
83 static const size_t kChunkSize = 64;
84 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
85 for (size_t i = 0, chunk_offset = 0; i < chunks;
86 ++i, chunk_offset += kChunkSize) {
87 result << b64_encoded.substr(chunk_offset, kChunkSize);
88 result << "\n";
89 }
90
91 result << "-----END " << pem_type << "-----\n";
92
93 return result.str();
94}
95
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000096SSLCertChain::SSLCertChain(const std::vector<SSLCertificate*>& certs) {
97 ASSERT(!certs.empty());
98 certs_.resize(certs.size());
99 std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert);
100}
101
102SSLCertChain::SSLCertChain(const SSLCertificate* cert) {
103 certs_.push_back(cert->GetReference());
104}
105
106SSLCertChain::~SSLCertChain() {
107 std::for_each(certs_.begin(), certs_.end(), DeleteCert);
108}
109
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110#if SSL_USE_SCHANNEL
111
112SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
113 return NULL;
114}
115
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200116SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
117 KeyType key_type) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000118 return NULL;
119}
120
121SSLIdentity* GenerateForTest(const SSLIdentityParams& params) {
122 return NULL;
123}
124
125SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
126 const std::string& certificate) {
127 return NULL;
128}
129
130#elif SSL_USE_OPENSSL // !SSL_USE_SCHANNEL
131
132SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
133 return OpenSSLCertificate::FromPEMString(pem_string);
134}
135
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200136SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
137 KeyType key_type) {
138 return OpenSSLIdentity::Generate(common_name, key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139}
140
141SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
142 return OpenSSLIdentity::GenerateForTest(params);
143}
144
145SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
146 const std::string& certificate) {
147 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
148}
149
deadbeef9eb13652015-09-05 04:39:15 -0700150#elif SSL_USE_NSS // !SSL_USE_OPENSSL && !SSL_USE_SCHANNEL
151
152SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
153 return NSSCertificate::FromPEMString(pem_string);
154}
155
156SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
157 KeyType key_type) {
158 return NSSIdentity::Generate(common_name, key_type);
159}
160
161SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
162 return NSSIdentity::GenerateForTest(params);
163}
164
165SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
166 const std::string& certificate) {
167 return NSSIdentity::FromPEMStrings(private_key, certificate);
168}
169
170#else // !SSL_USE_OPENSSL && !SSL_USE_SCHANNEL && !SSL_USE_NSS
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171
172#error "No SSL implementation"
173
174#endif // SSL_USE_SCHANNEL
175
176} // namespace rtc