blob: 33b7387588633e8106817396e9b6e1f583397390 [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
30#elif SSL_USE_NSS // !SSL_USE_SCHANNEL && !SSL_USE_OPENSSL
31
32#include "webrtc/base/nssidentity.h"
33
34#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
42bool SSLIdentity::PemToDer(const std::string& pem_type,
43 const std::string& pem_string,
44 std::string* der) {
45 // Find the inner body. We need this to fulfill the contract of
46 // returning pem_length.
47 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
48 if (header == std::string::npos)
49 return false;
50
51 size_t body = pem_string.find("\n", header);
52 if (body == std::string::npos)
53 return false;
54
55 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
56 if (trailer == std::string::npos)
57 return false;
58
59 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
60
61 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
62 Base64::DO_PAD_ANY |
63 Base64::DO_TERM_BUFFER);
64 return true;
65}
66
67std::string SSLIdentity::DerToPem(const std::string& pem_type,
68 const unsigned char* data,
69 size_t length) {
70 std::stringstream result;
71
72 result << "-----BEGIN " << pem_type << "-----\n";
73
74 std::string b64_encoded;
75 Base64::EncodeFromArray(data, length, &b64_encoded);
76
77 // Divide the Base-64 encoded data into 64-character chunks, as per
78 // 4.3.2.4 of RFC 1421.
79 static const size_t kChunkSize = 64;
80 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
81 for (size_t i = 0, chunk_offset = 0; i < chunks;
82 ++i, chunk_offset += kChunkSize) {
83 result << b64_encoded.substr(chunk_offset, kChunkSize);
84 result << "\n";
85 }
86
87 result << "-----END " << pem_type << "-----\n";
88
89 return result.str();
90}
91
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000092SSLCertChain::SSLCertChain(const std::vector<SSLCertificate*>& certs) {
93 ASSERT(!certs.empty());
94 certs_.resize(certs.size());
95 std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert);
96}
97
98SSLCertChain::SSLCertChain(const SSLCertificate* cert) {
99 certs_.push_back(cert->GetReference());
100}
101
102SSLCertChain::~SSLCertChain() {
103 std::for_each(certs_.begin(), certs_.end(), DeleteCert);
104}
105
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000106#if SSL_USE_SCHANNEL
107
108SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
109 return NULL;
110}
111
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200112SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
113 KeyType key_type) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 return NULL;
115}
116
117SSLIdentity* GenerateForTest(const SSLIdentityParams& params) {
118 return NULL;
119}
120
121SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
122 const std::string& certificate) {
123 return NULL;
124}
125
126#elif SSL_USE_OPENSSL // !SSL_USE_SCHANNEL
127
128SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
129 return OpenSSLCertificate::FromPEMString(pem_string);
130}
131
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200132SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
133 KeyType key_type) {
134 return OpenSSLIdentity::Generate(common_name, key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135}
136
137SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
138 return OpenSSLIdentity::GenerateForTest(params);
139}
140
141SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
142 const std::string& certificate) {
143 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
144}
145
146#elif SSL_USE_NSS // !SSL_USE_OPENSSL && !SSL_USE_SCHANNEL
147
148SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
149 return NSSCertificate::FromPEMString(pem_string);
150}
151
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200152SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
153 KeyType key_type) {
154 return NSSIdentity::Generate(common_name, key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000155}
156
157SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
158 return NSSIdentity::GenerateForTest(params);
159}
160
161SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
162 const std::string& certificate) {
163 return NSSIdentity::FromPEMStrings(private_key, certificate);
164}
165
166#else // !SSL_USE_OPENSSL && !SSL_USE_SCHANNEL && !SSL_USE_NSS
167
168#error "No SSL implementation"
169
170#endif // SSL_USE_SCHANNEL
171
172} // namespace rtc