OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/base/x509_certificate.h" | 5 #include "net/base/x509_certificate.h" |
6 | 6 |
7 #include <openssl/asn1.h> | 7 #include <openssl/asn1.h> |
8 #include <openssl/crypto.h> | 8 #include <openssl/crypto.h> |
9 #include <openssl/obj_mac.h> | 9 #include <openssl/obj_mac.h> |
10 #include <openssl/pem.h> | 10 #include <openssl/pem.h> |
11 #include <openssl/pkcs7.h> | 11 #include <openssl/pkcs7.h> |
12 #include <openssl/sha.h> | 12 #include <openssl/sha.h> |
13 #include <openssl/ssl.h> | 13 #include <openssl/ssl.h> |
14 #include <openssl/x509v3.h> | 14 #include <openssl/x509v3.h> |
15 | 15 |
16 #include "base/memory/singleton.h" | 16 #include "base/memory/singleton.h" |
17 #include "base/pickle.h" | 17 #include "base/pickle.h" |
18 #include "base/sha1.h" | 18 #include "base/sha1.h" |
19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
20 #include "base/string_util.h" | |
20 #include "crypto/openssl_util.h" | 21 #include "crypto/openssl_util.h" |
21 #include "net/base/asn1_util.h" | 22 #include "net/base/asn1_util.h" |
22 #include "net/base/cert_status_flags.h" | 23 #include "net/base/cert_status_flags.h" |
23 #include "net/base/cert_verify_result.h" | 24 #include "net/base/cert_verify_result.h" |
24 #include "net/base/net_errors.h" | 25 #include "net/base/net_errors.h" |
25 #include "net/base/x509_util_openssl.h" | 26 #include "net/base/x509_util_openssl.h" |
26 | 27 |
27 namespace net { | 28 namespace net { |
28 | 29 |
29 namespace { | 30 namespace { |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 // free the memory and any application-specific data associated with the | 321 // free the memory and any application-specific data associated with the |
321 // certificate. | 322 // certificate. |
322 X509_free(cert_handle); | 323 X509_free(cert_handle); |
323 } | 324 } |
324 | 325 |
325 void X509Certificate::Initialize() { | 326 void X509Certificate::Initialize() { |
326 crypto::EnsureOpenSSLInit(); | 327 crypto::EnsureOpenSSLInit(); |
327 fingerprint_ = CalculateFingerprint(cert_handle_); | 328 fingerprint_ = CalculateFingerprint(cert_handle_); |
328 chain_fingerprint_ = CalculateChainFingerprint(); | 329 chain_fingerprint_ = CalculateChainFingerprint(); |
329 | 330 |
330 ASN1_INTEGER* num = X509_get_serialNumber(cert_handle_); | 331 ASN1_INTEGER* serial_num = X509_get_serialNumber(cert_handle_); |
331 if (num) { | 332 if (serial_num) { |
332 serial_number_ = std::string( | 333 // ASN1_INTEGERS represent the decoded number, in a format internal to |
333 reinterpret_cast<char*>(num->data), | 334 // OpenSSL. Most notably, this may have leading zeroes stripped off for |
334 num->length); | 335 // numbers whose first byte is >= 0x80. Thus, it is necessary to |
336 // re-encoded the integer back into DER, which is what the interface | |
337 // of X509Certificate exposes, to ensure callers get the proper (DER) | |
338 // value. | |
joth
2011/11/01 09:16:33
I think the comment already answers this, but is t
| |
339 int bytes_required = i2c_ASN1_INTEGER(serial_num, NULL); | |
340 unsigned char* buffer = reinterpret_cast<unsigned char*>( | |
341 WriteInto(&serial_number_, bytes_required + 1)); | |
342 int bytes_written = i2c_ASN1_INTEGER(serial_num, &buffer); | |
343 DCHECK_EQ(static_cast<size_t>(bytes_written), serial_number_.size()); | |
335 } | 344 } |
336 | 345 |
337 ParsePrincipal(cert_handle_, X509_get_subject_name(cert_handle_), &subject_); | 346 ParsePrincipal(cert_handle_, X509_get_subject_name(cert_handle_), &subject_); |
338 ParsePrincipal(cert_handle_, X509_get_issuer_name(cert_handle_), &issuer_); | 347 ParsePrincipal(cert_handle_, X509_get_issuer_name(cert_handle_), &issuer_); |
339 x509_util::ParseDate(X509_get_notBefore(cert_handle_), &valid_start_); | 348 x509_util::ParseDate(X509_get_notBefore(cert_handle_), &valid_start_); |
340 x509_util::ParseDate(X509_get_notAfter(cert_handle_), &valid_expiry_); | 349 x509_util::ParseDate(X509_get_notAfter(cert_handle_), &valid_expiry_); |
341 } | 350 } |
342 | 351 |
343 // static | 352 // static |
344 void X509Certificate::ResetCertStore() { | 353 void X509Certificate::ResetCertStore() { |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
574 DERCache der_cache; | 583 DERCache der_cache; |
575 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) | 584 if (!GetDERAndCacheIfNeeded(cert_handle, &der_cache)) |
576 return false; | 585 return false; |
577 | 586 |
578 return pickle->WriteData( | 587 return pickle->WriteData( |
579 reinterpret_cast<const char*>(der_cache.data), | 588 reinterpret_cast<const char*>(der_cache.data), |
580 der_cache.data_length); | 589 der_cache.data_length); |
581 } | 590 } |
582 | 591 |
583 } // namespace net | 592 } // namespace net |
OLD | NEW |