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