OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/x509_util_ios.h" |
| 6 |
| 7 #include <cert.h> |
| 8 #include <nss.h> |
| 9 #include <prtypes.h> |
| 10 |
| 11 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "crypto/nss_util.h" |
| 13 #include "net/base/x509_certificate.h" |
| 14 |
| 15 using base::mac::ScopedCFTypeRef; |
| 16 |
| 17 namespace net { |
| 18 |
| 19 namespace x509_util { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Creates an NSS certificate handle from |data|, which is |length| bytes in |
| 24 // size. |
| 25 CERTCertificate* CreateNSSCertHandleFromBytes(const char* data, |
| 26 int length) { |
| 27 if (length < 0) |
| 28 return NULL; |
| 29 |
| 30 crypto::EnsureNSSInit(); |
| 31 |
| 32 if (!NSS_IsInitialized()) |
| 33 return NULL; |
| 34 |
| 35 SECItem der_cert; |
| 36 der_cert.data = reinterpret_cast<unsigned char*>(const_cast<char*>(data)); |
| 37 der_cert.len = length; |
| 38 der_cert.type = siDERCertBuffer; |
| 39 |
| 40 // Parse into a certificate structure. |
| 41 return CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &der_cert, NULL, |
| 42 PR_FALSE, PR_TRUE); |
| 43 } |
| 44 |
| 45 } // namespace |
| 46 |
| 47 CERTCertificate* CreateNSSCertHandleFromOSHandle( |
| 48 SecCertificateRef cert_handle) { |
| 49 ScopedCFTypeRef<CFDataRef> cert_data(SecCertificateCopyData(cert_handle)); |
| 50 return CreateNSSCertHandleFromBytes( |
| 51 reinterpret_cast<const char*>(CFDataGetBytePtr(cert_data)), |
| 52 CFDataGetLength(cert_data)); |
| 53 } |
| 54 |
| 55 SecCertificateRef CreateOSCertHandleFromNSSHandle( |
| 56 CERTCertificate* nss_cert_handle) { |
| 57 return X509Certificate::CreateOSCertHandleFromBytes( |
| 58 reinterpret_cast<const char*>(nss_cert_handle->derCert.data), |
| 59 nss_cert_handle->derCert.len); |
| 60 } |
| 61 |
| 62 NSSCertificate::NSSCertificate(SecCertificateRef cert_handle) { |
| 63 nss_cert_handle_ = x509_util::CreateNSSCertHandleFromOSHandle(cert_handle); |
| 64 } |
| 65 |
| 66 NSSCertificate::~NSSCertificate() { |
| 67 CERT_DestroyCertificate(nss_cert_handle_); |
| 68 } |
| 69 |
| 70 CERTCertificate* NSSCertificate::cert_handle() { |
| 71 return nss_cert_handle_; |
| 72 } |
| 73 |
| 74 } // namespace x509_util |
| 75 |
| 76 } // namespace net |
| 77 |
OLD | NEW |