blob: d2d2b11f173c18e3d4d0274a94a3c392f24a99f0 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// Handling of certificates and keypairs for SSLStreamAdapter's peer mode.
29#if HAVE_CONFIG_H
30#include "config.h"
31#endif // HAVE_CONFIG_H
32
33#include "talk/base/sslidentity.h"
34
35#include <string>
36
wu@webrtc.org5aed3bb2013-08-10 07:18:04 +000037#include "talk/base/base64.h"
38#include "talk/base/logging.h"
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000039#include "talk/base/sslconfig.h"
40
41#if SSL_USE_SCHANNEL
42
43#elif SSL_USE_OPENSSL // !SSL_USE_SCHANNEL
44
45#include "talk/base/opensslidentity.h"
46
47#elif SSL_USE_NSS // !SSL_USE_SCHANNEL && !SSL_USE_OPENSSL
48
49#include "talk/base/nssidentity.h"
50
51#endif // SSL_USE_SCHANNEL
52
53namespace talk_base {
54
wu@webrtc.org5aed3bb2013-08-10 07:18:04 +000055const char kPemTypeCertificate[] = "CERTIFICATE";
56const char kPemTypeRsaPrivateKey[] = "RSA PRIVATE KEY";
57
58bool SSLIdentity::PemToDer(const std::string& pem_type,
59 const std::string& pem_string,
60 std::string* der) {
61 // Find the inner body. We need this to fulfill the contract of
62 // returning pem_length.
63 size_t header = pem_string.find("-----BEGIN " + pem_type + "-----");
64 if (header == std::string::npos)
65 return false;
66
67 size_t body = pem_string.find("\n", header);
68 if (body == std::string::npos)
69 return false;
70
71 size_t trailer = pem_string.find("-----END " + pem_type + "-----");
72 if (trailer == std::string::npos)
73 return false;
74
75 std::string inner = pem_string.substr(body + 1, trailer - (body + 1));
76
77 *der = Base64::Decode(inner, Base64::DO_PARSE_WHITE |
78 Base64::DO_PAD_ANY |
79 Base64::DO_TERM_BUFFER);
80 return true;
81}
82
83std::string SSLIdentity::DerToPem(const std::string& pem_type,
84 const unsigned char* data,
85 size_t length) {
86 std::stringstream result;
87
88 result << "-----BEGIN " << pem_type << "-----\n";
89
90 std::string b64_encoded;
91 Base64::EncodeFromArray(data, length, &b64_encoded);
92
93 // Divide the Base-64 encoded data into 64-character chunks, as per
94 // 4.3.2.4 of RFC 1421.
95 static const size_t kChunkSize = 64;
96 size_t chunks = (b64_encoded.size() + (kChunkSize - 1)) / kChunkSize;
97 for (size_t i = 0, chunk_offset = 0; i < chunks;
98 ++i, chunk_offset += kChunkSize) {
99 result << b64_encoded.substr(chunk_offset, kChunkSize);
100 result << "\n";
101 }
102
103 result << "-----END " << pem_type << "-----\n";
104
105 return result.str();
106}
107
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000108#if SSL_USE_SCHANNEL
109
110SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
111 return NULL;
112}
113
114SSLIdentity* SSLIdentity::Generate(const std::string& common_name) {
115 return NULL;
116}
117
wu@webrtc.org2a81a382014-01-03 22:08:47 +0000118SSLIdentity* GenerateForTest(const SSLIdentityParams& params) {
119 return NULL;
120}
121
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000122SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
123 const std::string& certificate) {
124 return NULL;
125}
126
127#elif SSL_USE_OPENSSL // !SSL_USE_SCHANNEL
128
129SSLCertificate* SSLCertificate::FromPEMString(const std::string& pem_string) {
130 return OpenSSLCertificate::FromPEMString(pem_string);
131}
132
133SSLIdentity* SSLIdentity::Generate(const std::string& common_name) {
134 return OpenSSLIdentity::Generate(common_name);
135}
136
wu@webrtc.org2a81a382014-01-03 22:08:47 +0000137SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
138 return OpenSSLIdentity::GenerateForTest(params);
139}
140
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000141SSLIdentity* 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
152SSLIdentity* SSLIdentity::Generate(const std::string& common_name) {
153 return NSSIdentity::Generate(common_name);
154}
155
wu@webrtc.org2a81a382014-01-03 22:08:47 +0000156SSLIdentity* SSLIdentity::GenerateForTest(const SSLIdentityParams& params) {
157 return NSSIdentity::GenerateForTest(params);
158}
159
henrike@webrtc.org0e118e72013-07-10 00:45:36 +0000160SSLIdentity* SSLIdentity::FromPEMStrings(const std::string& private_key,
161 const std::string& certificate) {
162 return NSSIdentity::FromPEMStrings(private_key, certificate);
163}
164
165#else // !SSL_USE_OPENSSL && !SSL_USE_SCHANNEL && !SSL_USE_NSS
166
167#error "No SSL implementation"
168
169#endif // SSL_USE_SCHANNEL
170
171} // namespace talk_base