blob: 7bacb444613d2cef6f3249e8394a529077ef7a5a [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
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +000054// Certificate validity lifetime in seconds.
55static const int CERTIFICATE_LIFETIME = 60*60*24*30; // 30 days, arbitrarily
56// Certificate validity window in seconds.
57// This is to compensate for slightly incorrect system clocks.
58static const int CERTIFICATE_WINDOW = -60*60*24;
59
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060NSSKeyPair::~NSSKeyPair() {
61 if (privkey_)
62 SECKEY_DestroyPrivateKey(privkey_);
63 if (pubkey_)
64 SECKEY_DestroyPublicKey(pubkey_);
65}
66
67NSSKeyPair *NSSKeyPair::Generate() {
68 SECKEYPrivateKey *privkey = NULL;
69 SECKEYPublicKey *pubkey = NULL;
70 PK11RSAGenParams rsaparams;
71 rsaparams.keySizeInBits = 1024;
72 rsaparams.pe = 0x010001; // 65537 -- a common RSA public exponent.
73
74 privkey = PK11_GenerateKeyPair(NSSContext::GetSlot(),
75 CKM_RSA_PKCS_KEY_PAIR_GEN,
76 &rsaparams, &pubkey, PR_FALSE /*permanent*/,
77 PR_FALSE /*sensitive*/, NULL);
78 if (!privkey) {
79 LOG(LS_ERROR) << "Couldn't generate key pair";
80 return NULL;
81 }
82
83 return new NSSKeyPair(privkey, pubkey);
84}
85
86// Just make a copy.
87NSSKeyPair *NSSKeyPair::GetReference() {
88 SECKEYPrivateKey *privkey = SECKEY_CopyPrivateKey(privkey_);
89 if (!privkey)
90 return NULL;
91
92 SECKEYPublicKey *pubkey = SECKEY_CopyPublicKey(pubkey_);
93 if (!pubkey) {
94 SECKEY_DestroyPrivateKey(privkey);
95 return NULL;
96 }
97
98 return new NSSKeyPair(privkey, pubkey);
99}
100
wu@webrtc.org4551b792013-10-09 15:37:36 +0000101NSSCertificate::NSSCertificate(CERTCertificate* cert)
102 : certificate_(CERT_DupCertificate(cert)) {
103 ASSERT(certificate_ != NULL);
104}
105
106static void DeleteCert(SSLCertificate* cert) {
107 delete cert;
108}
109
110NSSCertificate::NSSCertificate(CERTCertList* cert_list) {
111 // Copy the first cert into certificate_.
112 CERTCertListNode* node = CERT_LIST_HEAD(cert_list);
113 certificate_ = CERT_DupCertificate(node->cert);
114
115 // Put any remaining certificates into the chain.
116 node = CERT_LIST_NEXT(node);
117 std::vector<SSLCertificate*> certs;
118 for (; !CERT_LIST_END(node, cert_list); node = CERT_LIST_NEXT(node)) {
119 certs.push_back(new NSSCertificate(node->cert));
120 }
121
122 if (!certs.empty())
123 chain_.reset(new SSLCertChain(certs));
124
125 // The SSLCertChain constructor copies its input, so now we have to delete
126 // the originals.
127 std::for_each(certs.begin(), certs.end(), DeleteCert);
128}
129
130NSSCertificate::NSSCertificate(CERTCertificate* cert, SSLCertChain* chain)
131 : certificate_(CERT_DupCertificate(cert)) {
132 ASSERT(certificate_ != NULL);
133 if (chain)
134 chain_.reset(chain->Copy());
135}
136
137
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138NSSCertificate *NSSCertificate::FromPEMString(const std::string &pem_string) {
139 std::string der;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000140 if (!SSLIdentity::PemToDer(kPemTypeCertificate, pem_string, &der))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141 return NULL;
142
143 SECItem der_cert;
144 der_cert.data = reinterpret_cast<unsigned char *>(const_cast<char *>(
145 der.data()));
146 der_cert.len = der.size();
147 CERTCertificate *cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(),
148 &der_cert, NULL, PR_FALSE, PR_TRUE);
149
150 if (!cert)
151 return NULL;
152
wu@webrtc.org4551b792013-10-09 15:37:36 +0000153 NSSCertificate* ret = new NSSCertificate(cert);
154 CERT_DestroyCertificate(cert);
155 return ret;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156}
157
158NSSCertificate *NSSCertificate::GetReference() const {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000159 return new NSSCertificate(certificate_, chain_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160}
161
162std::string NSSCertificate::ToPEMString() const {
wu@webrtc.org91053e72013-08-10 07:18:04 +0000163 return SSLIdentity::DerToPem(kPemTypeCertificate,
164 certificate_->derCert.data,
165 certificate_->derCert.len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166}
167
wu@webrtc.org4551b792013-10-09 15:37:36 +0000168void NSSCertificate::ToDER(Buffer* der_buffer) const {
169 der_buffer->SetData(certificate_->derCert.data, certificate_->derCert.len);
170}
171
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000172static bool Certifies(CERTCertificate* parent, CERTCertificate* child) {
173 // TODO(bemasc): Identify stricter validation checks to use here. In the
174 // context of some future identity standard, it might make sense to check
175 // the certificates' roles, expiration dates, self-signatures (if
176 // self-signed), certificate transparency logging, or many other attributes.
177 // NOTE: Future changes to this validation may reject some previously allowed
178 // certificate chains. Users should be advised not to deploy chained
179 // certificates except in controlled environments until the validity
180 // requirements are finalized.
181
182 // Check that the parent's name is the same as the child's claimed issuer.
183 SECComparison name_status =
184 CERT_CompareName(&child->issuer, &parent->subject);
185 if (name_status != SECEqual)
186 return false;
187
188 // Extract the parent's public key, or fail if the key could not be read
189 // (e.g. certificate is corrupted).
190 SECKEYPublicKey* parent_key = CERT_ExtractPublicKey(parent);
191 if (!parent_key)
192 return false;
193
194 // Check that the parent's privkey was actually used to generate the child's
195 // signature.
196 SECStatus verified = CERT_VerifySignedDataWithPublicKey(
197 &child->signatureWrap, parent_key, NULL);
198 SECKEY_DestroyPublicKey(parent_key);
199 return verified == SECSuccess;
200}
201
202bool NSSCertificate::IsValidChain(const CERTCertList* cert_list) {
203 CERTCertListNode* child = CERT_LIST_HEAD(cert_list);
204 for (CERTCertListNode* parent = CERT_LIST_NEXT(child);
205 !CERT_LIST_END(parent, cert_list);
206 child = parent, parent = CERT_LIST_NEXT(parent)) {
207 if (!Certifies(parent->cert, child->cert))
208 return false;
209 }
210 return true;
211}
212
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213bool NSSCertificate::GetDigestLength(const std::string &algorithm,
214 std::size_t *length) {
215 const SECHashObject *ho;
216
217 if (!GetDigestObject(algorithm, &ho))
218 return false;
219
220 *length = ho->length;
221
222 return true;
223}
224
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000225bool NSSCertificate::GetSignatureDigestAlgorithm(std::string* algorithm) const {
226 // The function sec_DecodeSigAlg in NSS provides this mapping functionality.
227 // Unfortunately it is private, so the functionality must be duplicated here.
228 // See https://bugzilla.mozilla.org/show_bug.cgi?id=925165 .
229 SECOidTag sig_alg = SECOID_GetAlgorithmTag(&certificate_->signature);
230 switch (sig_alg) {
231 case SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION:
232 *algorithm = DIGEST_MD5;
233 break;
234 case SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION:
235 case SEC_OID_ISO_SHA_WITH_RSA_SIGNATURE:
236 case SEC_OID_ISO_SHA1_WITH_RSA_SIGNATURE:
237 case SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST:
238 case SEC_OID_BOGUS_DSA_SIGNATURE_WITH_SHA1_DIGEST:
239 case SEC_OID_ANSIX962_ECDSA_SHA1_SIGNATURE:
240 case SEC_OID_MISSI_DSS:
241 case SEC_OID_MISSI_KEA_DSS:
242 case SEC_OID_MISSI_KEA_DSS_OLD:
243 case SEC_OID_MISSI_DSS_OLD:
244 *algorithm = DIGEST_SHA_1;
245 break;
246 case SEC_OID_ANSIX962_ECDSA_SHA224_SIGNATURE:
247 case SEC_OID_PKCS1_SHA224_WITH_RSA_ENCRYPTION:
248 case SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA224_DIGEST:
249 *algorithm = DIGEST_SHA_224;
250 break;
251 case SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE:
252 case SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION:
253 case SEC_OID_NIST_DSA_SIGNATURE_WITH_SHA256_DIGEST:
254 *algorithm = DIGEST_SHA_256;
255 break;
256 case SEC_OID_ANSIX962_ECDSA_SHA384_SIGNATURE:
257 case SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION:
258 *algorithm = DIGEST_SHA_384;
259 break;
260 case SEC_OID_ANSIX962_ECDSA_SHA512_SIGNATURE:
261 case SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION:
262 *algorithm = DIGEST_SHA_512;
263 break;
264 default:
265 // Unknown algorithm. There are several unhandled options that are less
266 // common and more complex.
267 algorithm->clear();
268 return false;
269 }
270 return true;
271}
272
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000273bool NSSCertificate::ComputeDigest(const std::string &algorithm,
274 unsigned char *digest, std::size_t size,
275 std::size_t *length) const {
276 const SECHashObject *ho;
277
278 if (!GetDigestObject(algorithm, &ho))
279 return false;
280
281 if (size < ho->length) // Sanity check for fit
282 return false;
283
284 SECStatus rv = HASH_HashBuf(ho->type, digest,
285 certificate_->derCert.data,
286 certificate_->derCert.len);
287 if (rv != SECSuccess)
288 return false;
289
290 *length = ho->length;
291
292 return true;
293}
294
wu@webrtc.org4551b792013-10-09 15:37:36 +0000295bool NSSCertificate::GetChain(SSLCertChain** chain) const {
296 if (!chain_)
297 return false;
298
299 *chain = chain_->Copy();
300 return true;
301}
302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303bool NSSCertificate::Equals(const NSSCertificate *tocompare) const {
304 if (!certificate_->derCert.len)
305 return false;
306 if (!tocompare->certificate_->derCert.len)
307 return false;
308
309 if (certificate_->derCert.len != tocompare->certificate_->derCert.len)
310 return false;
311
312 return memcmp(certificate_->derCert.data,
313 tocompare->certificate_->derCert.data,
314 certificate_->derCert.len) == 0;
315}
316
317
318bool NSSCertificate::GetDigestObject(const std::string &algorithm,
319 const SECHashObject **hop) {
320 const SECHashObject *ho;
321 HASH_HashType hash_type;
322
323 if (algorithm == DIGEST_SHA_1) {
324 hash_type = HASH_AlgSHA1;
325 // HASH_AlgSHA224 is not supported in the chromium linux build system.
326#if 0
327 } else if (algorithm == DIGEST_SHA_224) {
328 hash_type = HASH_AlgSHA224;
329#endif
330 } else if (algorithm == DIGEST_SHA_256) {
331 hash_type = HASH_AlgSHA256;
332 } else if (algorithm == DIGEST_SHA_384) {
333 hash_type = HASH_AlgSHA384;
334 } else if (algorithm == DIGEST_SHA_512) {
335 hash_type = HASH_AlgSHA512;
336 } else {
337 return false;
338 }
339
340 ho = HASH_GetHashObject(hash_type);
341
342 ASSERT(ho->length >= 20); // Can't happen
343 *hop = ho;
344
345 return true;
346}
347
348
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000349NSSIdentity* NSSIdentity::GenerateInternal(const SSLIdentityParams& params) {
350 std::string subject_name_string = "CN=" + params.common_name;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351 CERTName *subject_name = CERT_AsciiToName(
352 const_cast<char *>(subject_name_string.c_str()));
353 NSSIdentity *identity = NULL;
354 CERTSubjectPublicKeyInfo *spki = NULL;
355 CERTCertificateRequest *certreq = NULL;
356 CERTValidity *validity;
357 CERTCertificate *certificate = NULL;
358 NSSKeyPair *keypair = NSSKeyPair::Generate();
359 SECItem inner_der;
360 SECStatus rv;
361 PLArenaPool* arena;
362 SECItem signed_cert;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 PRTime now = PR_Now();
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000364 PRTime not_before =
365 now + static_cast<PRTime>(params.not_before) * PR_USEC_PER_SEC;
366 PRTime not_after =
367 now + static_cast<PRTime>(params.not_after) * PR_USEC_PER_SEC;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368
369 inner_der.len = 0;
370 inner_der.data = NULL;
371
372 if (!keypair) {
373 LOG(LS_ERROR) << "Couldn't generate key pair";
374 goto fail;
375 }
376
377 if (!subject_name) {
378 LOG(LS_ERROR) << "Couldn't convert subject name " << subject_name;
379 goto fail;
380 }
381
382 spki = SECKEY_CreateSubjectPublicKeyInfo(keypair->pubkey());
383 if (!spki) {
384 LOG(LS_ERROR) << "Couldn't create SPKI";
385 goto fail;
386 }
387
388 certreq = CERT_CreateCertificateRequest(subject_name, spki, NULL);
389 if (!certreq) {
390 LOG(LS_ERROR) << "Couldn't create certificate signing request";
391 goto fail;
392 }
393
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 validity = CERT_CreateValidity(not_before, not_after);
395 if (!validity) {
396 LOG(LS_ERROR) << "Couldn't create validity";
397 goto fail;
398 }
399
400 unsigned long serial;
401 // Note: This serial in principle could collide, but it's unlikely
402 rv = PK11_GenerateRandom(reinterpret_cast<unsigned char *>(&serial),
403 sizeof(serial));
404 if (rv != SECSuccess) {
405 LOG(LS_ERROR) << "Couldn't generate random serial";
406 goto fail;
407 }
408
409 certificate = CERT_CreateCertificate(serial, subject_name, validity, certreq);
410 if (!certificate) {
411 LOG(LS_ERROR) << "Couldn't create certificate";
412 goto fail;
413 }
414
415 arena = certificate->arena;
416
417 rv = SECOID_SetAlgorithmID(arena, &certificate->signature,
418 SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION, NULL);
419 if (rv != SECSuccess)
420 goto fail;
421
422 // Set version to X509v3.
423 *(certificate->version.data) = 2;
424 certificate->version.len = 1;
425
426 if (!SEC_ASN1EncodeItem(arena, &inner_der, certificate,
427 SEC_ASN1_GET(CERT_CertificateTemplate)))
428 goto fail;
429
430 rv = SEC_DerSignData(arena, &signed_cert, inner_der.data, inner_der.len,
431 keypair->privkey(),
432 SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION);
433 if (rv != SECSuccess) {
434 LOG(LS_ERROR) << "Couldn't sign certificate";
435 goto fail;
436 }
437 certificate->derCert = signed_cert;
438
439 identity = new NSSIdentity(keypair, new NSSCertificate(certificate));
440
441 goto done;
442
443 fail:
444 delete keypair;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445
446 done:
wu@webrtc.org4551b792013-10-09 15:37:36 +0000447 if (certificate) CERT_DestroyCertificate(certificate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 if (subject_name) CERT_DestroyName(subject_name);
449 if (spki) SECKEY_DestroySubjectPublicKeyInfo(spki);
450 if (certreq) CERT_DestroyCertificateRequest(certreq);
451 if (validity) CERT_DestroyValidity(validity);
452 return identity;
453}
454
wu@webrtc.orgf6d6ed02014-01-03 22:08:47 +0000455NSSIdentity* NSSIdentity::Generate(const std::string &common_name) {
456 SSLIdentityParams params;
457 params.common_name = common_name;
458 params.not_before = CERTIFICATE_WINDOW;
459 params.not_after = CERTIFICATE_LIFETIME;
460 return GenerateInternal(params);
461}
462
463NSSIdentity* NSSIdentity::GenerateForTest(const SSLIdentityParams& params) {
464 return GenerateInternal(params);
465}
466
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467SSLIdentity* NSSIdentity::FromPEMStrings(const std::string& private_key,
468 const std::string& certificate) {
469 std::string private_key_der;
wu@webrtc.org91053e72013-08-10 07:18:04 +0000470 if (!SSLIdentity::PemToDer(
471 kPemTypeRsaPrivateKey, private_key, &private_key_der))
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 return NULL;
473
474 SECItem private_key_item;
475 private_key_item.data =
476 reinterpret_cast<unsigned char *>(
477 const_cast<char *>(private_key_der.c_str()));
478 private_key_item.len = private_key_der.size();
479
480 const unsigned int key_usage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT |
481 KU_DIGITAL_SIGNATURE;
482
483 SECKEYPrivateKey* privkey = NULL;
484 SECStatus rv =
485 PK11_ImportDERPrivateKeyInfoAndReturnKey(NSSContext::GetSlot(),
486 &private_key_item,
487 NULL, NULL, PR_FALSE, PR_FALSE,
488 key_usage, &privkey, NULL);
489 if (rv != SECSuccess) {
490 LOG(LS_ERROR) << "Couldn't import private key";
491 return NULL;
492 }
493
494 SECKEYPublicKey *pubkey = SECKEY_ConvertToPublicKey(privkey);
495 if (rv != SECSuccess) {
496 SECKEY_DestroyPrivateKey(privkey);
497 LOG(LS_ERROR) << "Couldn't convert private key to public key";
498 return NULL;
499 }
500
501 // Assign to a scoped_ptr so we don't leak on error.
502 scoped_ptr<NSSKeyPair> keypair(new NSSKeyPair(privkey, pubkey));
503
504 scoped_ptr<NSSCertificate> cert(NSSCertificate::FromPEMString(certificate));
505 if (!cert) {
506 LOG(LS_ERROR) << "Couldn't parse certificate";
507 return NULL;
508 }
509
510 // TODO(ekr@rtfm.com): Check the public key against the certificate.
511
512 return new NSSIdentity(keypair.release(), cert.release());
513}
514
515NSSIdentity *NSSIdentity::GetReference() const {
516 NSSKeyPair *keypair = keypair_->GetReference();
517 if (!keypair)
518 return NULL;
519
520 NSSCertificate *certificate = certificate_->GetReference();
521 if (!certificate) {
522 delete keypair;
523 return NULL;
524 }
525
526 return new NSSIdentity(keypair, certificate);
527}
528
529
530NSSCertificate &NSSIdentity::certificate() const {
531 return *certificate_;
532}
533
534
535} // talk_base namespace
536
537#endif // HAVE_NSS_SSL_H
538