blob: ce209dd4167c2211fdc80649be4dca7a48b3b487 [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
torbjorng172f0092015-10-07 04:57:55 -070024#if SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025
26#include "webrtc/base/opensslidentity.h"
27
torbjorng172f0092015-10-07 04:57:55 -070028#endif // SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029
30namespace rtc {
31
32const 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
Henrik Boström9b5476d2015-09-22 14:12:57 +020036KeyType IntKeyTypeFamilyToKeyType(int key_type_family) {
37 return static_cast<KeyType>(key_type_family);
38}
39
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040bool SSLIdentity::PemToDer(const std::string& pem_type,
41 const std::string& pem_string,
42 std::string* der) {
43 // Find the inner body. We need this to fulfill the contract of
44 // returning pem_length.
45 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
46 if (header == std::string::npos)
47 return false;
48
49 size_t body = pem_string.find("\n", header);
50 if (body == std::string::npos)
51 return false;
52
53 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
54 if (trailer == std::string::npos)
55 return false;
56
57 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
58
59 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
60 Base64::DO_PAD_ANY |
61 Base64::DO_TERM_BUFFER);
62 return true;
63}
64
65std::string SSLIdentity::DerToPem(const std::string& pem_type,
66 const unsigned char* data,
67 size_t length) {
68 std::stringstream result;
69
70 result << "-----BEGIN " << pem_type << "-----\n";
71
72 std::string b64_encoded;
73 Base64::EncodeFromArray(data, length, &b64_encoded);
74
75 // Divide the Base-64 encoded data into 64-character chunks, as per
76 // 4.3.2.4 of RFC 1421.
77 static const size_t kChunkSize = 64;
78 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
79 for (size_t i = 0, chunk_offset = 0; i < chunks;
80 ++i, chunk_offset += kChunkSize) {
81 result << b64_encoded.substr(chunk_offset, kChunkSize);
82 result << "\n";
83 }
84
85 result << "-----END " << pem_type << "-----\n";
86
87 return result.str();
88}
89
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000090SSLCertChain::SSLCertChain(const std::vector<SSLCertificate*>& certs) {
91 ASSERT(!certs.empty());
92 certs_.resize(certs.size());
93 std::transform(certs.begin(), certs.end(), certs_.begin(), DupCert);
94}
95
96SSLCertChain::SSLCertChain(const SSLCertificate* cert) {
97 certs_.push_back(cert->GetReference());
98}
99
100SSLCertChain::~SSLCertChain() {
101 std::for_each(certs_.begin(), certs_.end(), DeleteCert);
102}
103
torbjorng172f0092015-10-07 04:57:55 -0700104#if SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105
106SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
107 return OpenSSLCertificate::FromPEMString(pem_string);
108}
109
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200110SSLIdentity* SSLIdentity::Generate(const std::string& common_name,
111 KeyType key_type) {
112 return OpenSSLIdentity::Generate(common_name, key_type);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000113}
114
115SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
116 return OpenSSLIdentity::GenerateForTest(params);
117}
118
119SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
120 const std::string& certificate) {
121 return OpenSSLIdentity::FromPEMStrings(private_key, certificate);
122}
123
torbjorng172f0092015-10-07 04:57:55 -0700124#else // !SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125
126#error "No SSL implementation"
127
torbjorng172f0092015-10-07 04:57:55 -0700128#endif // SSL_USE_OPENSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000129
130} // namespace rtc