// 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->cert_buffer()), CRYPTO_BUFFER_len(cert->cert_buffer())); } 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::CreateCertBufferFromBytes( reinterpret_cast(CFDataGetBytePtr(der_data)), CFDataGetLength(der_data))); if (!cert_handle) return nullptr; std::vector> intermediates; 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::CreateCertBufferFromBytes( reinterpret_cast(CFDataGetBytePtr(der_data)), CFDataGetLength(der_data))); if (!intermediate_cert_handle) return nullptr; intermediates.push_back(std::move(intermediate_cert_handle)); } scoped_refptr result(X509Certificate::CreateFromBuffer( std::move(cert_handle), std::move(intermediates))); return result; } } // namespace x509_util } // namespace net