| 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 <cert.h> | 7 #include <cert.h> |
| 8 #include <cryptohi.h> | 8 #include <cryptohi.h> |
| 9 #include <keyhi.h> | 9 #include <keyhi.h> |
| 10 #include <nss.h> | 10 #include <nss.h> |
| (...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 | 643 |
| 644 serial_number_ = std::string( | 644 serial_number_ = std::string( |
| 645 reinterpret_cast<char*>(cert_handle_->serialNumber.data), | 645 reinterpret_cast<char*>(cert_handle_->serialNumber.data), |
| 646 cert_handle_->serialNumber.len); | 646 cert_handle_->serialNumber.len); |
| 647 // Remove leading zeros. | 647 // Remove leading zeros. |
| 648 while (serial_number_.size() > 1 && serial_number_[0] == 0) | 648 while (serial_number_.size() > 1 && serial_number_[0] == 0) |
| 649 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1); | 649 serial_number_ = serial_number_.substr(1, serial_number_.size() - 1); |
| 650 } | 650 } |
| 651 | 651 |
| 652 // static | 652 // static |
| 653 X509Certificate* X509Certificate::CreateFromPickle(const Pickle& pickle, | |
| 654 void** pickle_iter) { | |
| 655 const char* data; | |
| 656 int length; | |
| 657 if (!pickle.ReadData(pickle_iter, &data, &length)) | |
| 658 return NULL; | |
| 659 | |
| 660 return CreateFromBytes(data, length); | |
| 661 } | |
| 662 | |
| 663 // static | |
| 664 X509Certificate* X509Certificate::CreateSelfSigned( | 653 X509Certificate* X509Certificate::CreateSelfSigned( |
| 665 crypto::RSAPrivateKey* key, | 654 crypto::RSAPrivateKey* key, |
| 666 const std::string& subject, | 655 const std::string& subject, |
| 667 uint32 serial_number, | 656 uint32 serial_number, |
| 668 base::TimeDelta valid_duration) { | 657 base::TimeDelta valid_duration) { |
| 669 DCHECK(key); | 658 DCHECK(key); |
| 670 | 659 |
| 671 // Create info about public key. | 660 // Create info about public key. |
| 672 CERTSubjectPublicKeyInfo* spki = | 661 CERTSubjectPublicKeyInfo* spki = |
| 673 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); | 662 SECKEY_CreateSubjectPublicKeyInfo(key->public_key()); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 | 744 |
| 756 // Save the signed result to the cert. | 745 // Save the signed result to the cert. |
| 757 cert->derCert = *result; | 746 cert->derCert = *result; |
| 758 | 747 |
| 759 X509Certificate* x509_cert = | 748 X509Certificate* x509_cert = |
| 760 CreateFromHandle(cert, SOURCE_LONE_CERT_IMPORT, OSCertHandles()); | 749 CreateFromHandle(cert, SOURCE_LONE_CERT_IMPORT, OSCertHandles()); |
| 761 CERT_DestroyCertificate(cert); | 750 CERT_DestroyCertificate(cert); |
| 762 return x509_cert; | 751 return x509_cert; |
| 763 } | 752 } |
| 764 | 753 |
| 765 void X509Certificate::Persist(Pickle* pickle) { | |
| 766 pickle->WriteData(reinterpret_cast<const char*>(cert_handle_->derCert.data), | |
| 767 cert_handle_->derCert.len); | |
| 768 } | |
| 769 | |
| 770 void X509Certificate::GetDNSNames(std::vector<std::string>* dns_names) const { | 754 void X509Certificate::GetDNSNames(std::vector<std::string>* dns_names) const { |
| 771 dns_names->clear(); | 755 dns_names->clear(); |
| 772 | 756 |
| 773 // Compare with CERT_VerifyCertName(). | 757 // Compare with CERT_VerifyCertName(). |
| 774 GetCertSubjectAltNamesOfType(cert_handle_, certDNSName, dns_names); | 758 GetCertSubjectAltNamesOfType(cert_handle_, certDNSName, dns_names); |
| 775 | 759 |
| 776 if (dns_names->empty()) | 760 if (dns_names->empty()) |
| 777 dns_names->push_back(subject_.common_name); | 761 dns_names->push_back(subject_.common_name); |
| 778 } | 762 } |
| 779 | 763 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 DCHECK(NULL != cert->derCert.data); | 982 DCHECK(NULL != cert->derCert.data); |
| 999 DCHECK(0 != cert->derCert.len); | 983 DCHECK(0 != cert->derCert.len); |
| 1000 | 984 |
| 1001 SECStatus rv = HASH_HashBuf(HASH_AlgSHA1, sha1.data, | 985 SECStatus rv = HASH_HashBuf(HASH_AlgSHA1, sha1.data, |
| 1002 cert->derCert.data, cert->derCert.len); | 986 cert->derCert.data, cert->derCert.len); |
| 1003 DCHECK(rv == SECSuccess); | 987 DCHECK(rv == SECSuccess); |
| 1004 | 988 |
| 1005 return sha1; | 989 return sha1; |
| 1006 } | 990 } |
| 1007 | 991 |
| 992 // static |
| 993 X509Certificate::OSCertHandle |
| 994 X509Certificate::ReadCertHandleFromPickle(const Pickle& pickle, |
| 995 void** pickle_iter) { |
| 996 const char* data; |
| 997 int length; |
| 998 if (!pickle.ReadData(pickle_iter, &data, &length)) |
| 999 return NULL; |
| 1000 |
| 1001 return CreateOSCertHandleFromBytes(data, length); |
| 1002 } |
| 1003 |
| 1004 // static |
| 1005 bool X509Certificate::WriteCertHandleToPickle(OSCertHandle cert_handle, |
| 1006 Pickle* pickle) { |
| 1007 return pickle->WriteData( |
| 1008 reinterpret_cast<const char*>(cert_handle->derCert.data), |
| 1009 cert_handle->derCert.len); |
| 1010 } |
| 1011 |
| 1008 } // namespace net | 1012 } // namespace net |
| OLD | NEW |