OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/cert/x509_certificate.h" | 5 #include "net/cert/x509_certificate.h" |
6 | 6 |
7 #include <blapi.h> // Implement CalculateChainFingerprint() with NSS. | |
8 | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
11 #include "base/pickle.h" | 9 #include "base/pickle.h" |
12 #include "base/sha1.h" | 10 #include "base/sha1.h" |
13 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
15 #include "crypto/capi_util.h" | 13 #include "crypto/capi_util.h" |
16 #include "crypto/scoped_capi_types.h" | 14 #include "crypto/scoped_capi_types.h" |
17 #include "crypto/sha2.h" | 15 #include "crypto/sha2.h" |
18 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
19 | 17 |
| 18 // Implement CalculateChainFingerprint() with our native crypto library. |
| 19 #if defined(USE_OPENSSL) |
| 20 #include <openssl/sha.h> |
| 21 #else |
| 22 #include <blapi.h> |
| 23 #endif |
| 24 |
20 #pragma comment(lib, "crypt32.lib") | 25 #pragma comment(lib, "crypt32.lib") |
21 | 26 |
22 using base::Time; | 27 using base::Time; |
23 | 28 |
24 namespace net { | 29 namespace net { |
25 | 30 |
26 namespace { | 31 namespace { |
27 | 32 |
28 typedef crypto::ScopedCAPIHandle< | 33 typedef crypto::ScopedCAPIHandle< |
29 HCERTSTORE, | 34 HCERTSTORE, |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // Use crypto::SHA256HashString for two reasons: | 332 // Use crypto::SHA256HashString for two reasons: |
328 // * < Windows Vista does not have universal SHA-256 support. | 333 // * < Windows Vista does not have universal SHA-256 support. |
329 // * More efficient on Windows > Vista (less overhead since non-default CSP | 334 // * More efficient on Windows > Vista (less overhead since non-default CSP |
330 // is not needed). | 335 // is not needed). |
331 base::StringPiece der_cert(reinterpret_cast<const char*>(cert->pbCertEncoded), | 336 base::StringPiece der_cert(reinterpret_cast<const char*>(cert->pbCertEncoded), |
332 cert->cbCertEncoded); | 337 cert->cbCertEncoded); |
333 crypto::SHA256HashString(der_cert, sha256.data, sha256_size); | 338 crypto::SHA256HashString(der_cert, sha256.data, sha256_size); |
334 return sha256; | 339 return sha256; |
335 } | 340 } |
336 | 341 |
337 // TODO(wtc): This function is implemented with NSS low-level hash | |
338 // functions to ensure it is fast. Reimplement this function with | |
339 // CryptoAPI. May need to cache the HCRYPTPROV to reduce the overhead. | |
340 // static | |
341 SHA1HashValue X509Certificate::CalculateCAFingerprint( | 342 SHA1HashValue X509Certificate::CalculateCAFingerprint( |
342 const OSCertHandles& intermediates) { | 343 const OSCertHandles& intermediates) { |
343 SHA1HashValue sha1; | 344 SHA1HashValue sha1; |
344 memset(sha1.data, 0, sizeof(sha1.data)); | 345 memset(sha1.data, 0, sizeof(sha1.data)); |
345 | 346 |
| 347 #if defined(USE_OPENSSL) |
| 348 SHA_CTX ctx; |
| 349 if (!SHA1_Init(&ctx)) |
| 350 return sha1; |
| 351 for (size_t i = 0; i < intermediates.size(); ++i) { |
| 352 PCCERT_CONTEXT ca_cert = intermediates[i]; |
| 353 if (!SHA1_Update(&ctx, ca_cert->pbCertEncoded, ca_cert->cbCertEncoded)) |
| 354 return sha1; |
| 355 } |
| 356 SHA1_Final(sha1.data, &ctx); |
| 357 #else // !USE_OPENSSL |
346 SHA1Context* sha1_ctx = SHA1_NewContext(); | 358 SHA1Context* sha1_ctx = SHA1_NewContext(); |
347 if (!sha1_ctx) | 359 if (!sha1_ctx) |
348 return sha1; | 360 return sha1; |
349 SHA1_Begin(sha1_ctx); | 361 SHA1_Begin(sha1_ctx); |
350 for (size_t i = 0; i < intermediates.size(); ++i) { | 362 for (size_t i = 0; i < intermediates.size(); ++i) { |
351 PCCERT_CONTEXT ca_cert = intermediates[i]; | 363 PCCERT_CONTEXT ca_cert = intermediates[i]; |
352 SHA1_Update(sha1_ctx, ca_cert->pbCertEncoded, ca_cert->cbCertEncoded); | 364 SHA1_Update(sha1_ctx, ca_cert->pbCertEncoded, ca_cert->cbCertEncoded); |
353 } | 365 } |
354 unsigned int result_len; | 366 unsigned int result_len; |
355 SHA1_End(sha1_ctx, sha1.data, &result_len, SHA1_LENGTH); | 367 SHA1_End(sha1_ctx, sha1.data, &result_len, SHA1_LENGTH); |
356 SHA1_DestroyContext(sha1_ctx, PR_TRUE); | 368 SHA1_DestroyContext(sha1_ctx, PR_TRUE); |
| 369 #endif // USE_OPENSSL |
357 | 370 |
358 return sha1; | 371 return sha1; |
359 } | 372 } |
360 | 373 |
361 // static | 374 // static |
362 X509Certificate::OSCertHandle | 375 X509Certificate::OSCertHandle |
363 X509Certificate::ReadOSCertHandleFromPickle(PickleIterator* pickle_iter) { | 376 X509Certificate::ReadOSCertHandleFromPickle(PickleIterator* pickle_iter) { |
364 const char* data; | 377 const char* data; |
365 int length; | 378 int length; |
366 if (!pickle_iter->ReadData(&data, &length)) | 379 if (!pickle_iter->ReadData(&data, &length)) |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 it != intermediate_ca_certs_.end(); ++it) { | 478 it != intermediate_ca_certs_.end(); ++it) { |
466 if (IsCertNameBlobInIssuerList(&(*it)->pCertInfo->Issuer, | 479 if (IsCertNameBlobInIssuerList(&(*it)->pCertInfo->Issuer, |
467 valid_issuers)) { | 480 valid_issuers)) { |
468 return true; | 481 return true; |
469 } | 482 } |
470 } | 483 } |
471 | 484 |
472 return false; | 485 return false; |
473 } | 486 } |
474 | 487 |
| 488 // static |
| 489 bool X509Certificate::IsSelfSigned(OSCertHandle cert_handle) { |
| 490 return !!CryptVerifyCertificateSignatureEx( |
| 491 NULL, |
| 492 X509_ASN_ENCODING, |
| 493 CRYPT_VERIFY_CERT_SIGN_SUBJECT_CERT, |
| 494 reinterpret_cast<void*>(const_cast<PCERT_CONTEXT>(cert_handle)), |
| 495 CRYPT_VERIFY_CERT_SIGN_ISSUER_CERT, |
| 496 reinterpret_cast<void*>(const_cast<PCERT_CONTEXT>(cert_handle)), |
| 497 0, |
| 498 NULL); |
| 499 } |
| 500 |
475 } // namespace net | 501 } // namespace net |
OLD | NEW |