blob: 0da254fcaa15666607ab9a9deb11d5fdab221de7 [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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030#endif // SSL_USE_SCHANNEL
31
32namespace rtc {
33
34const char kPemTypeCertificate[] = "CERTIFICATE";
35const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020036const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037
38bool SSLIdentity::PemToDer(const std::string& pem_type,
39 const std::string& pem_string,
40 std::string* der) {
41 // Find the inner body. We need this to fulfill the contract of
42 // returning pem_length.
43 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
44 if (header == std::string::npos)
45 return false;
46
47 size_t body = pem_string.find("\n", header);
48 if (body == std::string::npos)
49 return false;
50
51 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
52 if (trailer == std::string::npos)
53 return false;
54
55 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
56
57 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
58 Base64::DO_PAD_ANY |
59 Base64::DO_TERM_BUFFER);
60 return true;
61}
62
63std::string SSLIdentity::DerToPem(const std::string& pem_type,
64 const unsigned char* data,
65 size_t length) {
66 std::stringstream result;
67
68 result << "-----BEGIN " << pem_type << "-----\n";
69
70 std::string b64_encoded;
71 Base64::EncodeFromArray(data, length, &b64_encoded);
72
73 // Divide the Base-64 encoded data into 64-character chunks, as per
74 // 4.3.2.4 of RFC 1421.
75 static const size_t kChunkSize = 64;
76 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
77 for (size_t i = 0, chunk_offset = 0; i < chunks;
78 ++i, chunk_offset += kChunkSize) {
79 result << b64_encoded.substr(chunk_offset, kChunkSize);
80 result << "\n";
81 }
82
83 result << "-----END " << pem_type << "-----\n";
84
85 return result.str();
86}
87
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000088SSLCertChain::SSLCertChain(const std::vector<SSLCertificate*>& certs) {
89 ASSERT(!certs.empty());
90 certs_.resize(certs.size());
91 std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert);
92}
93
94SSLCertChain::SSLCertChain(const SSLCertificate* cert) {
95 certs_.push_back(cert->GetReference());
96}
97
98SSLCertChain::~SSLCertChain() {
99 std::for_each(certs_.begin(), certs_.end(), DeleteCert);
100}
101
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102#if SSL_USE_SCHANNEL
103
104SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
105 return NULL;
106}
107
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200108SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
109 KeyType key_type) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000110 return NULL;
111}
112
113SSLIdentity* GenerateForTest(const SSLIdentityParams& params) {
114 return NULL;
115}
116
117SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
118 const std::string& certificate) {
119 return NULL;
120}
121
122#elif SSL_USE_OPENSSL // !SSL_USE_SCHANNEL
123
124SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
125 return OpenSSLCertificate::FromPEMString(pem_string);
126}
127
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200128SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
129 KeyType key_type) {
130 return OpenSSLIdentity::Generate(common_name, key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131}
132
133SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
134 return OpenSSLIdentity::GenerateForTest(params);
135}
136
137SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
138 const std::string& certificate) {
139 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
140}
141
torbjorng5647a2c2015-09-04 08:11:51 -0700142#else // !SSL_USE_OPENSSL && !SSL_USE_SCHANNEL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143
144#error "No SSL implementation"
145
146#endif // SSL_USE_SCHANNEL
147
148} // namespace rtc