henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
deadbeef | 9eb1365 | 2015-09-05 04:39:15 -0700 | [diff] [blame] | 30 | #elif SSL_USE_NSS // !SSL_USE_SCHANNEL && !SSL_USE_OPENSSL |
| 31 | |
| 32 | #include "webrtc/base/nssidentity.h" |
| 33 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | #endif // SSL_USE_SCHANNEL |
| 35 | |
| 36 | namespace rtc { |
| 37 | |
| 38 | const char kPemTypeCertificate[] = "CERTIFICATE"; |
| 39 | const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY"; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 40 | const char kPemTypeEcPrivateKey[] = "EC PRIVATE KEY"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | |
Henrik Boström | 9b5476d | 2015-09-22 14:12:57 +0200 | [diff] [blame^] | 42 | KeyType IntKeyTypeFamilyToKeyType(int key_type_family) { |
| 43 | return static_cast<KeyType>(key_type_family); |
| 44 | } |
| 45 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | bool 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 | |
| 71 | std::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.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 96 | SSLCertChain::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 | |
| 102 | SSLCertChain::SSLCertChain(const SSLCertificate* cert) { |
| 103 | certs_.push_back(cert->GetReference()); |
| 104 | } |
| 105 | |
| 106 | SSLCertChain::~SSLCertChain() { |
| 107 | std::for_each(certs_.begin(), certs_.end(), DeleteCert); |
| 108 | } |
| 109 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 110 | #if SSL_USE_SCHANNEL |
| 111 | |
| 112 | SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { |
| 113 | return NULL; |
| 114 | } |
| 115 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 116 | SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
| 117 | KeyType key_type) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | SSLIdentity* GenerateForTest(const SSLIdentityParams& params) { |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | SSLIdentity* 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 | |
| 132 | SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { |
| 133 | return OpenSSLCertificate::FromPEMString(pem_string); |
| 134 | } |
| 135 | |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 136 | SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
| 137 | KeyType key_type) { |
| 138 | return OpenSSLIdentity::Generate(common_name, key_type); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { |
| 142 | return OpenSSLIdentity::GenerateForTest(params); |
| 143 | } |
| 144 | |
| 145 | SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key, |
| 146 | const std::string& certificate) { |
| 147 | return OpenSSLIdentity::FromPEMStrings(private_key, certificate); |
| 148 | } |
| 149 | |
deadbeef | 9eb1365 | 2015-09-05 04:39:15 -0700 | [diff] [blame] | 150 | #elif SSL_USE_NSS // !SSL_USE_OPENSSL && !SSL_USE_SCHANNEL |
| 151 | |
| 152 | SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) { |
| 153 | return NSSCertificate::FromPEMString(pem_string); |
| 154 | } |
| 155 | |
| 156 | SSLIdentity* SSLIdentity::Generate(const std::string& common_name, |
| 157 | KeyType key_type) { |
| 158 | return NSSIdentity::Generate(common_name, key_type); |
| 159 | } |
| 160 | |
| 161 | SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) { |
| 162 | return NSSIdentity::GenerateForTest(params); |
| 163 | } |
| 164 | |
| 165 | SSLIdentity* 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | |
| 172 | #error "No SSL implementation" |
| 173 | |
| 174 | #endif // SSL_USE_SCHANNEL |
| 175 | |
| 176 | } // namespace rtc |