// Copyright 2017 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "net/cert/x509_util_ios.h" #include "net/cert/x509_certificate.h" #include "third_party/boringssl/src/include/openssl/pool.h" namespace net { namespace x509_util { base::ScopedCFTypeRef CreateSecCertificateFromBytes( const uint8_t* data, size_t length) { base::ScopedCFTypeRef cert_data(CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, reinterpret_cast(data), base::checked_cast(length), kCFAllocatorNull)); if (!cert_data) return base::ScopedCFTypeRef(); return base::ScopedCFTypeRef( SecCertificateCreateWithData(nullptr, cert_data)); } base::ScopedCFTypeRef CreateSecCertificateFromX509Certificate(const X509Certificate* cert) { return CreateSecCertificateFromBytes( CRYPTO_BUFFER_data(cert->os_cert_handle()), CRYPTO_BUFFER_len(cert->os_cert_handle())); } scoped_refptr CreateX509CertificateFromSecCertificate( SecCertificateRef sec_cert, const std::vector& sec_chain) { if (!sec_cert) return nullptr; base::ScopedCFTypeRef der_data(SecCertificateCopyData(sec_cert)); if (!der_data) return nullptr; bssl::UniquePtr cert_handle( X509Certificate::CreateOSCertHandleFromBytes( reinterpret_cast(CFDataGetBytePtr(der_data)), CFDataGetLength(der_data))); if (!cert_handle) return nullptr; std::vector> intermediates; X509Certificate::OSCertHandles intermediates_raw; for (const SecCertificateRef& sec_intermediate : sec_chain) { if (!sec_intermediate) return nullptr; der_data.reset(SecCertificateCopyData(sec_intermediate)); if (!der_data) return nullptr; bssl::UniquePtr intermediate_cert_handle( X509Certificate::CreateOSCertHandleFromBytes( reinterpret_cast(CFDataGetBytePtr(der_data)), CFDataGetLength(der_data))); if (!intermediate_cert_handle) return nullptr; intermediates_raw.push_back(intermediate_cert_handle.get()); intermediates.push_back(std::move(intermediate_cert_handle)); } scoped_refptr result( X509Certificate::CreateFromHandle(cert_handle.get(), intermediates_raw)); return result; } } // namespace x509_util } // namespace net