Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: net/cert/x509_certificate.cc

Issue 412263003: Add SHA-256 fingerprint functions to x509 certs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add comment to CalculateCAFingerprint256 Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <map> 10 #include <map>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/base64.h" 14 #include "base/base64.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/singleton.h" 18 #include "base/memory/singleton.h"
18 #include "base/metrics/histogram.h" 19 #include "base/metrics/histogram.h"
19 #include "base/pickle.h" 20 #include "base/pickle.h"
20 #include "base/sha1.h" 21 #include "base/sha1.h"
21 #include "base/strings/string_piece.h" 22 #include "base/strings/string_piece.h"
22 #include "base/strings/string_util.h" 23 #include "base/strings/string_util.h"
23 #include "base/synchronization/lock.h" 24 #include "base/synchronization/lock.h"
24 #include "base/time/time.h" 25 #include "base/time/time.h"
26 #include "crypto/secure_hash.h"
25 #include "net/base/net_util.h" 27 #include "net/base/net_util.h"
26 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
27 #include "net/cert/pem_tokenizer.h" 29 #include "net/cert/pem_tokenizer.h"
28 #include "url/url_canon.h" 30 #include "url/url_canon.h"
29 31
30 namespace net { 32 namespace net {
31 33
32 namespace { 34 namespace {
33 35
34 // Indicates the order to use when trying to decode binary data, which is 36 // Indicates the order to use when trying to decode binary data, which is
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 encoded_chain.push_back(pem_data); 700 encoded_chain.push_back(pem_data);
699 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) { 701 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) {
700 if (!GetPEMEncoded(intermediate_ca_certs_[i], &pem_data)) 702 if (!GetPEMEncoded(intermediate_ca_certs_[i], &pem_data))
701 return false; 703 return false;
702 encoded_chain.push_back(pem_data); 704 encoded_chain.push_back(pem_data);
703 } 705 }
704 pem_encoded->swap(encoded_chain); 706 pem_encoded->swap(encoded_chain);
705 return true; 707 return true;
706 } 708 }
707 709
710 // static
711 SHA256HashValue X509Certificate::CalculateCAFingerprint256(
wtc 2014/07/26 01:25:12 You can move the code of this function to your cod
jww 2014/07/26 01:30:47 sleevi suggested that we want this as a generic wa
712 const OSCertHandles& intermediates) {
713 SHA256HashValue sha256;
714 memset(sha256.data, 0, sizeof(sha256.data));
715
716 scoped_ptr<crypto::SecureHash> hash(
717 crypto::SecureHash::Create(crypto::SecureHash::Algorithm::SHA256));
718
719 for (size_t i = 0; i < intermediates.size(); ++i) {
720 std::string der_encoded;
721 if (!GetDEREncoded(intermediates[i], &der_encoded))
Ryan Sleevi 2014/07/25 00:21:34 The only benefit to the platform implementation (a
722 return sha256;
723 hash->Update(der_encoded.c_str(), der_encoded.length());
724 }
725 hash->Finish(sha256.data, sizeof(sha256.data));
726
727 return sha256;
728 }
729
730 // static
731 SHA256HashValue X509Certificate::CalculateFullChainFingerprint256(
732 const OSCertHandle& leaf,
733 const OSCertHandles& intermediates) {
734 OSCertHandles chain;
735 chain.push_back(leaf);
736 chain.insert(chain.end(), intermediates.begin(), intermediates.end());
737
738 return CalculateCAFingerprint256(chain);
739 }
740
708 X509Certificate::X509Certificate(OSCertHandle cert_handle, 741 X509Certificate::X509Certificate(OSCertHandle cert_handle,
709 const OSCertHandles& intermediates) 742 const OSCertHandles& intermediates)
710 : cert_handle_(DupOSCertHandle(cert_handle)) { 743 : cert_handle_(DupOSCertHandle(cert_handle)) {
711 InsertOrUpdateCache(&cert_handle_); 744 InsertOrUpdateCache(&cert_handle_);
712 for (size_t i = 0; i < intermediates.size(); ++i) { 745 for (size_t i = 0; i < intermediates.size(); ++i) {
713 // Duplicate the incoming certificate, as the caller retains ownership 746 // Duplicate the incoming certificate, as the caller retains ownership
714 // of |intermediates|. 747 // of |intermediates|.
715 OSCertHandle intermediate = DupOSCertHandle(intermediates[i]); 748 OSCertHandle intermediate = DupOSCertHandle(intermediates[i]);
716 // Update the cache, which will assume ownership of the duplicated 749 // Update the cache, which will assume ownership of the duplicated
717 // handle and return a suitable equivalent, potentially from the cache. 750 // handle and return a suitable equivalent, potentially from the cache.
718 InsertOrUpdateCache(&intermediate); 751 InsertOrUpdateCache(&intermediate);
719 intermediate_ca_certs_.push_back(intermediate); 752 intermediate_ca_certs_.push_back(intermediate);
720 } 753 }
721 // Platform-specific initialization. 754 // Platform-specific initialization.
722 Initialize(); 755 Initialize();
723 } 756 }
724 757
725 X509Certificate::~X509Certificate() { 758 X509Certificate::~X509Certificate() {
726 if (cert_handle_) { 759 if (cert_handle_) {
727 RemoveFromCache(cert_handle_); 760 RemoveFromCache(cert_handle_);
728 FreeOSCertHandle(cert_handle_); 761 FreeOSCertHandle(cert_handle_);
729 } 762 }
730 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) { 763 for (size_t i = 0; i < intermediate_ca_certs_.size(); ++i) {
731 RemoveFromCache(intermediate_ca_certs_[i]); 764 RemoveFromCache(intermediate_ca_certs_[i]);
732 FreeOSCertHandle(intermediate_ca_certs_[i]); 765 FreeOSCertHandle(intermediate_ca_certs_[i]);
733 } 766 }
734 } 767 }
735 768
736 } // namespace net 769 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698