blob: 96bfcc3b092c8062eeb073027e6a5be007402997 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 * Copyright 2012, RTFM, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
wu@webrtc.org4551b792013-10-09 15:37:36 +000029#include <algorithm>
30#include <string>
31#include <vector>
32
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033#if HAVE_CONFIG_H
34#include "config.h"
35#endif // HAVE_CONFIG_H
36
37#if HAVE_NSS_SSL_H
38
39#include "talk/base/nssidentity.h"
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "cert.h"
42#include "cryptohi.h"
43#include "keyhi.h"
44#include "nss.h"
45#include "pk11pub.h"
46#include "sechash.h"
47
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048#include "talk/base/logging.h"
49#include "talk/base/helpers.h"
50#include "talk/base/nssstreamadapter.h"
51
52namespace talk_base {
53
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054NSSKeyPair::~NSSKeyPair() {
55 if (privkey_)
56 SECKEY_DestroyPrivateKey(privkey_);
57 if (pubkey_)
58 SECKEY_DestroyPublicKey(pubkey_);
59}
60
61NSSKeyPair *NSSKeyPair::Generate() {
62 SECKEYPrivateKey *privkey = NULL;
63 SECKEYPublicKey *pubkey = NULL;
64 PK11RSAGenParams rsaparams;
65 rsaparams.keySizeInBits = 1024;
66 rsaparams.pe = 0x010001; // 65537 -- a common RSA public exponent.
67
68 privkey = PK11_GenerateKeyPair(NSSContext::GetSlot(),
69 CKM_RSA_PKCS_KEY_PAIR_GEN,
70 &rsaparams, &pubkey, PR_FALSE /*permanent*/,
71 PR_FALSE /*sensitive*/, NULL);
72 if (!privkey) {
73 LOG(LS_ERROR) << "Couldn't generate key pair";
74 return NULL;
75 }
76
77 return new NSSKeyPair(privkey, pubkey);
78}
79
80// Just make a copy.
81NSSKeyPair *NSSKeyPair::GetReference() {
82 SECKEYPrivateKey *privkey = SECKEY_CopyPrivateKey(privkey_);
83 if (!privkey)
84 return NULL;
85
86 SECKEYPublicKey *pubkey = SECKEY_CopyPublicKey(pubkey_);
87 if (!pubkey) {
88 SECKEY_DestroyPrivateKey(privkey);
89 return NULL;
90 }
91
92 return new NSSKeyPair(privkey, pubkey);
93}
94
wu@webrtc.org4551b792013-10-09 15:37:36 +000095NSSCertificate::NSSCertificate(CERTCertificate* cert)
96 : certificate_(CERT_DupCertificate(cert)) {
97 ASSERT(certificate_ != NULL);
98}
99
100static void DeleteCert(SSLCertificate* cert) {
101 delete cert;
102}
103
104NSSCertificate::NSSCertificate(CERTCertList* cert_list) {
105 // Copy the first cert into certificate_.
106 CERTCertListNode* node = CERT_LIST_HEAD(cert_list);
107 certificate_ = CERT_DupCertificate(node->cert);
108
109 // Put any remaining certificates into the chain.
110 node = CERT_LIST_NEXT(node);
111 std::vector<SSLCertificate*> certs;
112 for (; !CERT_LIST_END(node, cert_list); node = CERT_LIST_NEXT(node)) {
113 certs.push_back(new NSSCertificate(node->cert));
114 }
115
116 if (!certs.empty())
117 chain_.reset(new SSLCertChain(certs));
118
119 // The SSLCertChain constructor copies its input, so now we have to delete
120 // the originals.
121 std::for_each(certs.begin(), certs.end(), DeleteCert);
122}
123
124NSSCertificate::NSSCertificate(CERTCertificate* cert, SSLCertChain* chain)
125 : certificate_(CERT_DupCertificate(cert)) {
126 ASSERT(certificate_ != NULL);
127 if (chain)
128 chain_.reset(chain->Copy());
129}
130
131
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132NSSCertificate *NSSCertificate::FromPEMString(const std::string &pem_string) {
133 std::string der;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000134 if (!SSLIdentity::PemToDer(kPemTypeCertificate, pem_string, &der))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 return NULL;
136
137 SECItem der_cert;
138 der_cert.data = reinterpret_cast<unsigned char *>(const_cast<char *>(
139 der.data()));
140 der_cert.len = der.size();
141 CERTCertificate *cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(),
142 &der_cert, NULL, PR_FALSE, PR_TRUE);
143
144 if (!cert)
145 return NULL;
146
wu@webrtc.org4551b792013-10-09 15:37:36 +0000147 NSSCertificate* ret = new NSSCertificate(cert);
148 CERT_DestroyCertificate(cert);
149 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150}
151
152NSSCertificate *NSSCertificate::GetReference() const {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000153 return new NSSCertificate(certificate_, chain_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154}
155
156std::string NSSCertificate::ToPEMString() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000157 return SSLIdentity::DerToPem(kPemTypeCertificate,
158 certificate_->derCert.data,
159 certificate_->derCert.len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160}
161
wu@webrtc.org4551b792013-10-09 15:37:36 +0000162void NSSCertificate::ToDER(Buffer* der_buffer) const {
163 der_buffer->SetData(certificate_->derCert.data, certificate_->derCert.len);
164}
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166bool NSSCertificate::GetDigestLength(const std::string &algorithm,
167 std::size_t *length) {
168 const SECHashObject *ho;
169
170 if (!GetDigestObject(algorithm, &ho))
171 return false;
172
173 *length = ho->length;
174
175 return true;
176}
177
178bool NSSCertificate::ComputeDigest(const std::string &algorithm,
179 unsigned char *digest, std::size_t size,
180 std::size_t *length) const {
181 const SECHashObject *ho;
182
183 if (!GetDigestObject(algorithm, &ho))
184 return false;
185
186 if (size < ho->length) // Sanity check for fit
187 return false;
188
189 SECStatus rv = HASH_HashBuf(ho->type, digest,
190 certificate_->derCert.data,
191 certificate_->derCert.len);
192 if (rv != SECSuccess)
193 return false;
194
195 *length = ho->length;
196
197 return true;
198}
199
wu@webrtc.org4551b792013-10-09 15:37:36 +0000200bool NSSCertificate::GetChain(SSLCertChain** chain) const {
201 if (!chain_)
202 return false;
203
204 *chain = chain_->Copy();
205 return true;
206}
207
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208bool NSSCertificate::Equals(const NSSCertificate *tocompare) const {
209 if (!certificate_->derCert.len)
210 return false;
211 if (!tocompare->certificate_->derCert.len)
212 return false;
213
214 if (certificate_->derCert.len != tocompare->certificate_->derCert.len)
215 return false;
216
217 return memcmp(certificate_->derCert.data,
218 tocompare->certificate_->derCert.data,
219 certificate_->derCert.len) == 0;
220}
221
222
223bool NSSCertificate::GetDigestObject(const std::string &algorithm,
224 const SECHashObject **hop) {
225 const SECHashObject *ho;
226 HASH_HashType hash_type;
227
228 if (algorithm == DIGEST_SHA_1) {
229 hash_type = HASH_AlgSHA1;
230 // HASH_AlgSHA224 is not supported in the chromium linux build system.
231#if 0
232 } else if (algorithm == DIGEST_SHA_224) {
233 hash_type = HASH_AlgSHA224;
234#endif
235 } else if (algorithm == DIGEST_SHA_256) {
236 hash_type = HASH_AlgSHA256;
237 } else if (algorithm == DIGEST_SHA_384) {
238 hash_type = HASH_AlgSHA384;
239 } else if (algorithm == DIGEST_SHA_512) {
240 hash_type = HASH_AlgSHA512;
241 } else {
242 return false;
243 }
244
245 ho = HASH_GetHashObject(hash_type);
246
247 ASSERT(ho->length >= 20); // Can't happen
248 *hop = ho;
249
250 return true;
251}
252
253
254NSSIdentity *NSSIdentity::Generate(const std::string &common_name) {
255 std::string subject_name_string = "CN=" + common_name;
256 CERTName *subject_name = CERT_AsciiToName(
257 const_cast<char *>(subject_name_string.c_str()));
258 NSSIdentity *identity = NULL;
259 CERTSubjectPublicKeyInfo *spki = NULL;
260 CERTCertificateRequest *certreq = NULL;
261 CERTValidity *validity;
262 CERTCertificate *certificate = NULL;
263 NSSKeyPair *keypair = NSSKeyPair::Generate();
264 SECItem inner_der;
265 SECStatus rv;
266 PLArenaPool* arena;
267 SECItem signed_cert;
268 PRTime not_before, not_after;
269 PRTime now = PR_Now();
270 PRTime one_day;
271
272 inner_der.len = 0;
273 inner_der.data = NULL;
274
275 if (!keypair) {
276 LOG(LS_ERROR) << "Couldn't generate key pair";
277 goto fail;
278 }
279
280 if (!subject_name) {
281 LOG(LS_ERROR) << "Couldn't convert subject name " << subject_name;
282 goto fail;
283 }
284
285 spki = SECKEY_CreateSubjectPublicKeyInfo(keypair->pubkey());
286 if (!spki) {
287 LOG(LS_ERROR) << "Couldn't create SPKI";
288 goto fail;
289 }
290
291 certreq = CERT_CreateCertificateRequest(subject_name, spki, NULL);
292 if (!certreq) {
293 LOG(LS_ERROR) << "Couldn't create certificate signing request";
294 goto fail;
295 }
296
297 one_day = 86400;
298 one_day *= PR_USEC_PER_SEC;
299 not_before = now - one_day;
300 not_after = now + 30 * one_day;
301
302 validity = CERT_CreateValidity(not_before, not_after);
303 if (!validity) {
304 LOG(LS_ERROR) << "Couldn't create validity";
305 goto fail;
306 }
307
308 unsigned long serial;
309 // Note: This serial in principle could collide, but it's unlikely
310 rv = PK11_GenerateRandom(reinterpret_cast<unsigned char *>(&serial),
311 sizeof(serial));
312 if (rv != SECSuccess) {
313 LOG(LS_ERROR) << "Couldn't generate random serial";
314 goto fail;
315 }
316
317 certificate = CERT_CreateCertificate(serial, subject_name, validity, certreq);
318 if (!certificate) {
319 LOG(LS_ERROR) << "Couldn't create certificate";
320 goto fail;
321 }
322
323 arena = certificate->arena;
324
325 rv = SECOID_SetAlgorithmID(arena, &certificate->signature,
326 SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION, NULL);
327 if (rv != SECSuccess)
328 goto fail;
329
330 // Set version to X509v3.
331 *(certificate->version.data) = 2;
332 certificate->version.len = 1;
333
334 if (!SEC_ASN1EncodeItem(arena, &inner_der, certificate,
335 SEC_ASN1_GET(CERT_CertificateTemplate)))
336 goto fail;
337
338 rv = SEC_DerSignData(arena, &signed_cert, inner_der.data, inner_der.len,
339 keypair->privkey(),
340 SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION);
341 if (rv != SECSuccess) {
342 LOG(LS_ERROR) << "Couldn't sign certificate";
343 goto fail;
344 }
345 certificate->derCert = signed_cert;
346
347 identity = new NSSIdentity(keypair, new NSSCertificate(certificate));
348
349 goto done;
350
351 fail:
352 delete keypair;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353
354 done:
wu@webrtc.org4551b792013-10-09 15:37:36 +0000355 if (certificate) CERT_DestroyCertificate(certificate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356 if (subject_name) CERT_DestroyName(subject_name);
357 if (spki) SECKEY_DestroySubjectPublicKeyInfo(spki);
358 if (certreq) CERT_DestroyCertificateRequest(certreq);
359 if (validity) CERT_DestroyValidity(validity);
360 return identity;
361}
362
363SSLIdentity* NSSIdentity::FromPEMStrings(const std::string& private_key,
364 const std::string& certificate) {
365 std::string private_key_der;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000366 if (!SSLIdentity::PemToDer(
367 kPemTypeRsaPrivateKey, private_key, &private_key_der))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 return NULL;
369
370 SECItem private_key_item;
371 private_key_item.data =
372 reinterpret_cast<unsigned char *>(
373 const_cast<char *>(private_key_der.c_str()));
374 private_key_item.len = private_key_der.size();
375
376 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT |
377 KU_DIGITAL_SIGNATURE;
378
379 SECKEYPrivateKey* privkey = NULL;
380 SECStatus rv =
381 PK11_ImportDERPrivateKeyInfoAndReturnKey(NSSContext::GetSlot(),
382 &private_key_item,
383 NULL, NULL, PR_FALSE, PR_FALSE,
384 key_usage, &privkey, NULL);
385 if (rv != SECSuccess) {
386 LOG(LS_ERROR) << "Couldn't import private key";
387 return NULL;
388 }
389
390 SECKEYPublicKey *pubkey = SECKEY_ConvertToPublicKey(privkey);
391 if (rv != SECSuccess) {
392 SECKEY_DestroyPrivateKey(privkey);
393 LOG(LS_ERROR) << "Couldn't convert private key to public key";
394 return NULL;
395 }
396
397 // Assign to a scoped_ptr so we don't leak on error.
398 scoped_ptr<NSSKeyPair> keypair(new NSSKeyPair(privkey, pubkey));
399
400 scoped_ptr<NSSCertificate> cert(NSSCertificate::FromPEMString(certificate));
401 if (!cert) {
402 LOG(LS_ERROR) << "Couldn't parse certificate";
403 return NULL;
404 }
405
406 // TODO(ekr@rtfm.com): Check the public key against the certificate.
407
408 return new NSSIdentity(keypair.release(), cert.release());
409}
410
411NSSIdentity *NSSIdentity::GetReference() const {
412 NSSKeyPair *keypair = keypair_->GetReference();
413 if (!keypair)
414 return NULL;
415
416 NSSCertificate *certificate = certificate_->GetReference();
417 if (!certificate) {
418 delete keypair;
419 return NULL;
420 }
421
422 return new NSSIdentity(keypair, certificate);
423}
424
425
426NSSCertificate &NSSIdentity::certificate() const {
427 return *certificate_;
428}
429
430
431} // talk_base namespace
432
433#endif // HAVE_NSS_SSL_H
434