Commit 62b23c2f authored by rsleevi@chromium.org's avatar rsleevi@chromium.org

Move X509Certificate::Verify into CertVerifyProc

With this split, CertVerifyProc is responsible for
interacting with the underlying PKIX path building and
verification library, while X509Certificate is responsible
for parsing certificates with the underlying crypto library
and exposing a common interface for higher-level code such
as UI.

BUG=114343
TEST=net_unittests 
Review URL: https://chromiumcodereview.appspot.com/9691054

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128172 0039d316-1c4b-4281-b951-d872f2087c98
parent 305a8b34
......@@ -12,6 +12,7 @@
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "net/base/x509_certificate.h"
#include "net/base/x509_util_mac.h"
void ShowCertificateViewer(gfx::NativeWindow parent,
net::X509Certificate* cert) {
......@@ -42,7 +43,7 @@ void ShowCertificateViewer(gfx::NativeWindow parent,
// Add a basic X.509 policy, in order to match the behaviour of
// SFCertificatePanel when no policies are specified.
SecPolicyRef basic_policy = NULL;
OSStatus status = net::X509Certificate::CreateBasicX509Policy(&basic_policy);
OSStatus status = net::x509_util::CreateBasicX509Policy(&basic_policy);
if (status != noErr) {
NOTREACHED();
return;
......@@ -50,7 +51,7 @@ void ShowCertificateViewer(gfx::NativeWindow parent,
CFArrayAppendValue(policies, basic_policy);
CFRelease(basic_policy);
status = net::X509Certificate::CreateRevocationPolicies(false, policies);
status = net::x509_util::CreateRevocationPolicies(false, policies);
if (status != noErr) {
NOTREACHED();
return;
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 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.
......@@ -21,6 +21,7 @@
#include "grit/generated_resources.h"
#include "net/base/ssl_cert_request_info.h"
#include "net/base/x509_certificate.h"
#include "net/base/x509_util_mac.h"
#include "ui/base/l10n/l10n_util_mac.h"
using content::BrowserThread;
......@@ -218,7 +219,7 @@ void ShowSSLClientCertificateSelector(
[panel setDefaultButtonTitle:l10n_util::GetNSString(IDS_OK)];
[panel setAlternateButtonTitle:l10n_util::GetNSString(IDS_CANCEL)];
SecPolicyRef sslPolicy;
if (net::X509Certificate::CreateSSLClientPolicy(&sslPolicy) == noErr) {
if (net::x509_util::CreateSSLClientPolicy(&sslPolicy) == noErr) {
[panel setPolicies:(id)sslPolicy];
CFRelease(sslPolicy);
}
......
This diff is collapsed.
......@@ -7,9 +7,12 @@
#pragma once
#include <string>
#include <vector>
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "net/base/net_export.h"
#include "net/base/x509_cert_types.h"
namespace net {
......@@ -55,6 +58,7 @@ class NET_EXPORT CertVerifyProc
protected:
friend class base::RefCountedThreadSafe<CertVerifyProc>;
FRIEND_TEST_ALL_PREFIXES(CertVerifyProcTest, DigiNotarCerts);
CertVerifyProc();
virtual ~CertVerifyProc();
......@@ -67,6 +71,14 @@ class NET_EXPORT CertVerifyProc
int flags,
CRLSet* crl_set,
CertVerifyResult* verify_result) = 0;
// Returns true if |cert| is explicitly blacklisted.
static bool IsBlacklisted(X509Certificate* cert);
// IsPublicKeyBlacklisted returns true iff one of |public_key_hashes| (which
// are SHA1 hashes of SubjectPublicKeyInfo structures) is explicitly blocked.
static bool IsPublicKeyBlacklisted(
const std::vector<SHA1Fingerprint>& public_key_hashes);
};
} // namespace net
......
This diff is collapsed.
// Copyright (c) 2012 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.
#ifndef NET_BASE_CERT_VERIFY_PROC_MAC_H_
#define NET_BASE_CERT_VERIFY_PROC_MAC_H_
#pragma once
#include "net/base/cert_verify_proc.h"
#include "base/synchronization/lock.h"
namespace net {
// Performs certificate path construction and validation using OS X's
// Security.framework.
class CertVerifyProcMac : public CertVerifyProc {
public:
CertVerifyProcMac();
protected:
virtual ~CertVerifyProcMac();
private:
virtual int VerifyInternal(X509Certificate* cert,
const std::string& hostname,
int flags,
CRLSet* crl_set,
CertVerifyResult* verify_result) OVERRIDE;
// Blocks multiple threads from verifying the cert simultaneously.
// (Marked mutable because it's used in a const method.)
mutable base::Lock verification_lock_;
};
} // namespace net
#endif // NET_BASE_CERT_VERIFY_PROC_MAC_H_
This diff is collapsed.
// Copyright (c) 2012 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.
#ifndef NET_BASE_CERT_VERIFY_PROC_NSS_H_
#define NET_BASE_CERT_VERIFY_PROC_NSS_H_
#pragma once
#include "net/base/cert_verify_proc.h"
namespace net {
// Performs certificate path construction and validation using NSS's libpkix.
class CertVerifyProcNSS : public CertVerifyProc {
public:
CertVerifyProcNSS();
protected:
virtual ~CertVerifyProcNSS();
private:
virtual int VerifyInternal(X509Certificate* cert,
const std::string& hostname,
int flags,
CRLSet* crl_set,
CertVerifyResult* verify_result) OVERRIDE;
};
} // namespace net
#endif // NET_BASE_CERT_VERIFY_PROC_NSS_H_
This diff is collapsed.
// Copyright (c) 2012 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.
#ifndef NET_BASE_CERT_VERIFY_PROC_OPENSSL_H_
#define NET_BASE_CERT_VERIFY_PROC_OPENSSL_H_
#pragma once
#include "net/base/cert_verify_proc.h"
namespace net {
// Performs certificate path construction and validation using OpenSSL.
class CertVerifyProcOpenSSL : public CertVerifyProc {
public:
CertVerifyProcOpenSSL();
protected:
virtual ~CertVerifyProcOpenSSL();
private:
virtual int VerifyInternal(X509Certificate* cert,
const std::string& hostname,
int flags,
CRLSet* crl_set,
CertVerifyResult* verify_result) OVERRIDE;
};
} // namespace net
#endif // NET_BASE_CERT_VERIFY_PROC_OPENSSL_H_
......@@ -9,6 +9,7 @@
#include "base/file_path.h"
#include "base/string_number_conversions.h"
#include "base/sha1.h"
#include "net/base/asn1_util.h"
#include "net/base/cert_status_flags.h"
#include "net/base/cert_test_util.h"
#include "net/base/cert_verify_result.h"
......@@ -372,6 +373,41 @@ TEST_F(CertVerifyProcTest, GoogleDigiNotarTest) {
EXPECT_NE(OK, error);
}
TEST_F(CertVerifyProcTest, DigiNotarCerts) {
static const char* const kDigiNotarFilenames[] = {
"diginotar_root_ca.pem",
"diginotar_cyber_ca.pem",
"diginotar_services_1024_ca.pem",
"diginotar_pkioverheid.pem",
"diginotar_pkioverheid_g2.pem",
NULL,
};
FilePath certs_dir = GetTestCertsDirectory();
for (size_t i = 0; kDigiNotarFilenames[i]; i++) {
scoped_refptr<X509Certificate> diginotar_cert =
ImportCertFromFile(certs_dir, kDigiNotarFilenames[i]);
std::string der_bytes;
ASSERT_TRUE(X509Certificate::GetDEREncoded(
diginotar_cert->os_cert_handle(), &der_bytes));
base::StringPiece spki;
ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(der_bytes, &spki));
std::string spki_sha1 = base::SHA1HashString(spki.as_string());
std::vector<SHA1Fingerprint> public_keys;
SHA1Fingerprint fingerprint;
ASSERT_EQ(sizeof(fingerprint.data), spki_sha1.size());
memcpy(fingerprint.data, spki_sha1.data(), spki_sha1.size());
public_keys.push_back(fingerprint);
EXPECT_TRUE(CertVerifyProc::IsPublicKeyBlacklisted(public_keys)) <<
"Public key not blocked for " << kDigiNotarFilenames[i];
}
}
// Bug 111893: This test needs a new certificate.
TEST_F(CertVerifyProcTest, DISABLED_TestKnownRoot) {
FilePath certs_dir = GetTestCertsDirectory();
......
This diff is collapsed.
// Copyright (c) 2012 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.
#ifndef NET_BASE_CERT_VERIFY_PROC_WIN_H_
#define NET_BASE_CERT_VERIFY_PROC_WIN_H_
#pragma once
#include "net/base/cert_verify_proc.h"
namespace net {
// Performs certificate path construction and validation using Windows'
// CryptoAPI.
class CertVerifyProcWin : public CertVerifyProc {
public:
CertVerifyProcWin();
protected:
virtual ~CertVerifyProcWin();
private:
virtual int VerifyInternal(X509Certificate* cert,
const std::string& hostname,
int flags,
CRLSet* crl_set,
CertVerifyResult* verify_result) OVERRIDE;
};
} // namespace net
#endif // NET_BASE_CERT_VERIFY_PROC_WIN_H_
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 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/base/x509_cert_types.h"
#include "net/base/x509_certificate.h"
#include <cstdlib>
#include <cstring>
#include "base/logging.h"
#include "base/sha1.h"
#include "base/string_number_conversions.h"
#include "base/string_piece.h"
#include "base/time.h"
#include "net/base/x509_certificate.h"
namespace net {
......@@ -25,8 +29,24 @@ int ParseIntAndAdvance(const char** field, size_t field_len, bool* ok) {
return result;
}
// CompareSHA1Hashes is a helper function for using bsearch() with an array of
// SHA1 hashes.
int CompareSHA1Hashes(const void* a, const void* b) {
return memcmp(a, b, base::kSHA1Length);
}
} // namespace
// static
bool IsSHA1HashInSortedArray(const SHA1Fingerprint& hash,
const uint8* array,
size_t array_byte_len) {
DCHECK_EQ(0u, array_byte_len % base::kSHA1Length);
const size_t arraylen = array_byte_len / base::kSHA1Length;
return NULL != bsearch(hash.data, array, arraylen, base::kSHA1Length,
CompareSHA1Hashes);
}
CertPrincipal::CertPrincipal() {
}
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2012 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.
......@@ -29,7 +29,7 @@ namespace net {
class X509Certificate;
// SHA-1 fingerprint (160 bits) of a certificate.
struct SHA1Fingerprint {
struct NET_EXPORT SHA1Fingerprint {
bool Equals(const SHA1Fingerprint& other) const {
return memcmp(data, other.data, sizeof(data)) == 0;
}
......@@ -37,7 +37,7 @@ struct SHA1Fingerprint {
unsigned char data[20];
};
class SHA1FingerprintLessThan {
class NET_EXPORT SHA1FingerprintLessThan {
public:
bool operator() (const SHA1Fingerprint& lhs,
const SHA1Fingerprint& rhs) const {
......@@ -45,6 +45,12 @@ class SHA1FingerprintLessThan {
}
};
// IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted
// array of SHA1 hashes.
bool NET_EXPORT IsSHA1HashInSortedArray(const SHA1Fingerprint& hash,
const uint8* array,
size_t array_byte_len);
// CertPrincipal represents the issuer or subject field of an X.509 certificate.
struct NET_EXPORT CertPrincipal {
CertPrincipal();
......
This diff is collapsed.
......@@ -26,7 +26,6 @@
#include <CoreFoundation/CFArray.h>
#include <Security/SecBase.h>
#include "base/synchronization/lock.h"
#elif defined(USE_OPENSSL)
// Forward declaration; real one in <x509.h>
typedef struct x509_st X509;
......@@ -224,6 +223,9 @@ class NET_EXPORT X509Certificate
// Appends a representation of this object to the given pickle.
void Persist(Pickle* pickle);
// The serial number, DER encoded, possibly including a leading 00 byte.
const std::string& serial_number() const { return serial_number_; }
// The subject of the certificate. For HTTPS server certificates, this
// represents the web server. The common name of the subject should match
// the host name of the web server.
......@@ -282,26 +284,6 @@ class NET_EXPORT X509Certificate
// Do any of the given issuer names appear in this cert's chain of trust?
bool IsIssuedBy(const std::vector<CertPrincipal>& valid_issuers);
// Creates a security policy for certificates used as client certificates
// in SSL.
// If a policy is successfully created, it will be stored in
// |*policy| and ownership transferred to the caller.
static OSStatus CreateSSLClientPolicy(SecPolicyRef* policy);
// Creates a security policy for basic X.509 validation. If the policy is
// successfully created, it will be stored in |*policy| and ownership
// transferred to the caller.
static OSStatus CreateBasicX509Policy(SecPolicyRef* policy);
// Creates security policies to control revocation checking (OCSP and CRL).
// If |enable_revocation_checking| is false, the policies returned will be
// explicitly disabled from accessing the network or the cache. This may be
// used to override system settings regarding revocation checking.
// If the policies are successfully created, they will be appended to
// |policies|.
static OSStatus CreateRevocationPolicies(bool enable_revocation_checking,
CFMutableArrayRef policies);
// Adds all available SSL client identity certs to the given vector.
// |server_domain| is a hint for which domain the cert is to be sent to
// (a cert previously specified as the default for that domain will be given
......@@ -363,12 +345,6 @@ class NET_EXPORT X509Certificate
PCCERT_CONTEXT CreateOSCertChainForCert() const;
#endif
#if defined(OS_ANDROID)
// |chain_bytes| will contain the chain (including this certificate) encoded
// using GetChainDEREncodedBytes below.
void GetChainDEREncodedBytes(std::vector<std::string>* chain_bytes) const;
#endif
#if defined(USE_OPENSSL)
// Returns a handle to a global, in-memory certificate store. We
// use it for test code, e.g. importing the test server's certificate.
......@@ -455,11 +431,8 @@ class NET_EXPORT X509Certificate
private:
friend class base::RefCountedThreadSafe<X509Certificate>;
friend class TestRootCerts; // For unit tests
// TODO(rsleevi): Temporary refactoring - http://crbug.com/114343
friend class CertVerifyProcStub;
FRIEND_TEST_ALL_PREFIXES(X509CertificateNameVerifyTest, VerifyHostname);
FRIEND_TEST_ALL_PREFIXES(X509CertificateTest, DigiNotarCerts);
FRIEND_TEST_ALL_PREFIXES(X509CertificateTest, SerialNumbers);
// Construct an X509Certificate from a handle to the certificate object
......@@ -472,44 +445,6 @@ class NET_EXPORT X509Certificate
// Common object initialization code. Called by the constructors only.
void Initialize();
// Verifies the certificate against the given hostname. Returns OK if
// successful or an error code upon failure.
//
// The |*verify_result| structure, including the |verify_result->cert_status|
// bitmask, is always filled out regardless of the return value. If the
// certificate has multiple errors, the corresponding status flags are set in
// |verify_result->cert_status|, and the error code for the most serious
// error is returned.
//
// |flags| is bitwise OR'd of VerifyFlags:
//
// If VERIFY_REV_CHECKING_ENABLED is set in |flags|, online certificate
// revocation checking is performed (i.e. OCSP and downloading CRLs). CRLSet
// based revocation checking is always enabled, regardless of this flag, if
// |crl_set| is given.
//
// If VERIFY_EV_CERT is set in |flags| too, EV certificate verification is
// performed.
//
// |crl_set| points to an optional CRLSet structure which can be used to
// avoid revocation checks over the network.
int Verify(const std::string& hostname,
int flags,
CRLSet* crl_set,
CertVerifyResult* verify_result) const;
#if defined(OS_WIN)
bool CheckEV(PCCERT_CHAIN_CONTEXT chain_context,
bool rev_checking_enabled,
const char* policy_oid) const;
static bool IsIssuedByKnownRoot(PCCERT_CHAIN_CONTEXT chain_context);
#endif
#if defined(OS_MACOSX)
static bool IsIssuedByKnownRoot(CFArrayRef chain);
#endif
#if defined(USE_NSS)
bool VerifyEV(int flags, CRLSet* crl_set) const;
#endif
#if defined(USE_OPENSSL)
// Resets the store returned by cert_store() to default state. Used by
// TestRootCerts to undo modifications.
......@@ -530,32 +465,6 @@ class NET_EXPORT X509Certificate
const std::vector<std::string>& cert_san_dns_names,
const std::vector<std::string>& cert_san_ip_addrs);
// Performs the platform-dependent part of the Verify() method, verifiying
// this certificate against the platform's root CA certificates.
//
// Parameters and return value are as per Verify().
int VerifyInternal(const std::string& hostname,
int flags,
CRLSet* crl_set,
CertVerifyResult* verify_result) const;
// The serial number, DER encoded, possibly including a leading 00 byte.
const std::string& serial_number() const { return serial_number_; }
// IsBlacklisted returns true if this certificate is explicitly blacklisted.
bool IsBlacklisted() const;
// IsPublicKeyBlacklisted returns true iff one of |public_key_hashes| (which
// are SHA1 hashes of SubjectPublicKeyInfo structures) is explicitly blocked.
static bool IsPublicKeyBlacklisted(
const std::vector<SHA1Fingerprint>& public_key_hashes);
// IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted
// array of SHA1 hashes.
static bool IsSHA1HashInSortedArray(const SHA1Fingerprint& hash,
const uint8* array,
size_t array_byte_len);
// Reads a single certificate from |pickle| and returns a platform-specific
// certificate handle. The format of the certificate stored in |pickle| is
// not guaranteed to be the same across different underlying cryptographic
......@@ -603,12 +512,6 @@ class NET_EXPORT X509Certificate
std::string default_nickname_;
#endif
#if defined(OS_MACOSX)
// Blocks multiple threads from verifying the cert simultaneously.
// (Marked mutable because it's used in a const method.)
mutable base::Lock verification_lock_;
#endif
DISALLOW_COPY_AND_ASSIGN(X509Certificate);
};
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -438,41 +438,6 @@ TEST(X509CertificateTest, CAFingerprints) {
cert_chain3_ca_fingerprint, 20) == 0);
}
TEST(X509CertificateTest, DigiNotarCerts) {
static const char* const kDigiNotarFilenames[] = {
"diginotar_root_ca.pem",
"diginotar_cyber_ca.pem",
"diginotar_services_1024_ca.pem",
"diginotar_pkioverheid.pem",
"diginotar_pkioverheid_g2.pem",
NULL,
};
FilePath certs_dir = GetTestCertsDirectory();
for (size_t i = 0; kDigiNotarFilenames[i]; i++) {
scoped_refptr<X509Certificate> diginotar_cert =
ImportCertFromFile(certs_dir, kDigiNotarFilenames[i]);
std::string der_bytes;
ASSERT_TRUE(X509Certificate::GetDEREncoded(
diginotar_cert->os_cert_handle(), &der_bytes));
base::StringPiece spki;
ASSERT_TRUE(asn1::ExtractSPKIFromDERCert(der_bytes, &spki));
std::string spki_sha1 = base::SHA1HashString(spki.as_string());
std::vector<SHA1Fingerprint> public_keys;
SHA1Fingerprint fingerprint;
ASSERT_EQ(sizeof(fingerprint.data), spki_sha1.size());
memcpy(fingerprint.data, spki_sha1.data(), spki_sha1.size());
public_keys.push_back(fingerprint);
EXPECT_TRUE(X509Certificate::IsPublicKeyBlacklisted(public_keys)) <<
"Public key not blocked for " << kDigiNotarFilenames[i];
}
}
TEST(X509CertificateTest, ExtractSPKIFromDERCert) {
FilePath certs_dir = GetTestCertsDirectory();
scoped_refptr<X509Certificate> cert =
......
This diff is collapsed.
// Copyright (c) 2012 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/base/x509_util_mac.h"
#include "base/logging.h"
#include "third_party/apple_apsl/cssmapplePriv.h"
namespace net {
namespace x509_util {
namespace {
// Creates a SecPolicyRef for the given OID, with optional value.
OSStatus CreatePolicy(const CSSM_OID* policy_oid,
void* option_data,
size_t option_length,
SecPolicyRef* policy) {
SecPolicySearchRef search;
OSStatus err = SecPolicySearchCreate(CSSM_CERT_X_509v3, policy_oid, NULL,
&search);
if (err)
return err;
err = SecPolicySearchCopyNext(search, policy);
CFRelease(search);
if (err)
return err;
if (option_data) {
CSSM_DATA options_data = {
option_length,
reinterpret_cast<uint8_t*>(option_data)
};
err = SecPolicySetValue(*policy, &options_data);
if (err) {
CFRelease(*policy);
return err;
}
}
return noErr;
}
} // namespace
OSStatus CreateSSLClientPolicy(SecPolicyRef* policy) {
CSSM_APPLE_TP_SSL_OPTIONS tp_ssl_options;
memset(&tp_ssl_options, 0, sizeof(tp_ssl_options));
tp_ssl_options.Version = CSSM_APPLE_TP_SSL_OPTS_VERSION;
tp_ssl_options.Flags |= CSSM_APPLE_TP_SSL_CLIENT;
return CreatePolicy(&CSSMOID_APPLE_TP_SSL, &tp_ssl_options,
sizeof(tp_ssl_options), policy);
}
OSStatus CreateSSLServerPolicy(const std::string& hostname,
SecPolicyRef* policy) {
CSSM_APPLE_TP_SSL_OPTIONS tp_ssl_options;
memset(&tp_ssl_options, 0, sizeof(tp_ssl_options));
tp_ssl_options.Version = CSSM_APPLE_TP_SSL_OPTS_VERSION;
if (!hostname.empty()) {
tp_ssl_options.ServerName = hostname.data();
tp_ssl_options.ServerNameLen = hostname.size();
}
return CreatePolicy(&CSSMOID_APPLE_TP_SSL, &tp_ssl_options,
sizeof(tp_ssl_options), policy);
}
OSStatus CreateBasicX509Policy(SecPolicyRef* policy) {
return CreatePolicy(&CSSMOID_APPLE_X509_BASIC, NULL, 0, policy);
}
OSStatus CreateRevocationPolicies(bool enable_revocation_checking,
CFMutableArrayRef policies) {
// In order to actually disable revocation checking, the SecTrustRef must
// have at least one revocation policy associated with it. If none are
// present, the Apple TP will add policies according to the system
// preferences, which will enable revocation checking even if the caller
// explicitly disabled it. An OCSP policy is used, rather than a CRL policy,
// because the Apple TP will force an OCSP policy to be present and enabled
// if it believes the certificate may chain to an EV root. By explicitly
// disabling network and OCSP cache access, then even if the Apple TP
// enables OCSP checking, no revocation checking will actually succeed.
CSSM_APPLE_TP_OCSP_OPTIONS tp_ocsp_options;
memset(&tp_ocsp_options, 0, sizeof(tp_ocsp_options));
tp_ocsp_options.Version = CSSM_APPLE_TP_OCSP_OPTS_VERSION;
if (enable_revocation_checking) {
// The default for the OCSP policy is to fetch responses via the network,
// unlike the CRL policy default. The policy is further modified to
// prefer OCSP over CRLs, if both are specified on the certificate. This
// is because an OCSP response is both sufficient and typically
// significantly smaller than the CRL counterpart.
tp_ocsp_options.Flags = CSSM_TP_ACTION_OCSP_SUFFICIENT;
} else {
// Effectively disable OCSP checking by making it impossible to get an
// OCSP response. Even if the Apple TP forces OCSP, no checking will
// be able to succeed. If this happens, the Apple TP will report an error
// that OCSP was unavailable, but this will be handled and suppressed in
// X509Certificate::Verify().
tp_ocsp_options.Flags = CSSM_TP_ACTION_OCSP_DISABLE_NET |
CSSM_TP_ACTION_OCSP_CACHE_READ_DISABLE;
}
SecPolicyRef ocsp_policy;
OSStatus status = CreatePolicy(&CSSMOID_APPLE_TP_REVOCATION_OCSP,
&tp_ocsp_options, sizeof(tp_ocsp_options),
&ocsp_policy);
if (status)
return status;
CFArrayAppendValue(policies, ocsp_policy);
CFRelease(ocsp_policy);
if (enable_revocation_checking) {
CSSM_APPLE_TP_CRL_OPTIONS tp_crl_options;
memset(&tp_crl_options, 0, sizeof(tp_crl_options));
tp_crl_options.Version = CSSM_APPLE_TP_CRL_OPTS_VERSION;
tp_crl_options.CrlFlags = CSSM_TP_ACTION_FETCH_CRL_FROM_NET;
SecPolicyRef crl_policy;
status = CreatePolicy(&CSSMOID_APPLE_TP_REVOCATION_CRL, &tp_crl_options,
sizeof(tp_crl_options), &crl_policy);
if (status)
return status;
CFArrayAppendValue(policies, crl_policy);
CFRelease(crl_policy);
}
return status;
}
CSSMFieldValue::CSSMFieldValue()
: cl_handle_(NULL),
oid_(NULL),
field_(NULL) {
}
CSSMFieldValue::CSSMFieldValue(CSSM_CL_HANDLE cl_handle,
const CSSM_OID* oid,
CSSM_DATA_PTR field)
: cl_handle_(cl_handle),
oid_(const_cast<CSSM_OID_PTR>(oid)),
field_(field) {
}
CSSMFieldValue::~CSSMFieldValue() {
Reset(NULL, NULL, NULL);
}
void CSSMFieldValue::Reset(CSSM_CL_HANDLE cl_handle,
CSSM_OID_PTR oid,
CSSM_DATA_PTR field) {
if (cl_handle_ && oid_ && field_)
CSSM_CL_FreeFieldValue(cl_handle_, oid_, field_);
cl_handle_ = cl_handle;
oid_ = oid;
field_ = field;
}
CSSMCachedCertificate::CSSMCachedCertificate()
: cl_handle_(NULL),
cached_cert_handle_(NULL) {
}
CSSMCachedCertificate::~CSSMCachedCertificate() {
if (cl_handle_ && cached_cert_handle_)
CSSM_CL_CertAbortCache(cl_handle_, cached_cert_handle_);
}
OSStatus CSSMCachedCertificate::Init(SecCertificateRef os_cert_handle) {
DCHECK(!cl_handle_ && !cached_cert_handle_);
DCHECK(os_cert_handle);
CSSM_DATA cert_data;
OSStatus status = SecCertificateGetData(os_cert_handle, &cert_data);
if (status)
return status;
status = SecCertificateGetCLHandle(os_cert_handle, &cl_handle_);
if (status) {
DCHECK(!cl_handle_);
return status;
}
status = CSSM_CL_CertCache(cl_handle_, &cert_data, &cached_cert_handle_);
if (status)
DCHECK(!cached_cert_handle_);
return status;
}
OSStatus CSSMCachedCertificate::GetField(const CSSM_OID* field_oid,
CSSMFieldValue* field) const {
DCHECK(cl_handle_);
DCHECK(cached_cert_handle_);
CSSM_OID_PTR oid = const_cast<CSSM_OID_PTR>(field_oid);
CSSM_DATA_PTR field_ptr = NULL;
CSSM_HANDLE results_handle = NULL;
uint32 field_value_count = 0;
CSSM_RETURN status = CSSM_CL_CertGetFirstCachedFieldValue(
cl_handle_, cached_cert_handle_, oid, &results_handle,
&field_value_count, &field_ptr);
if (status)
return status;
// Note: |field_value_count| may be > 1, indicating that more than one
// value is present. This may happen with extensions, but for current
// usages, only the first value is returned.
CSSM_CL_CertAbortQuery(cl_handle_, results_handle);
field->Reset(cl_handle_, oid, field_ptr);
return CSSM_OK;
}
} // namespace x509_util
} // namespace net
// Copyright (c) 2012 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.
#ifndef NET_BASE_X509_UTIL_MAC_H_
#define NET_BASE_X509_UTIL_MAC_H_
#include <CoreFoundation/CFArray.h>
#include <Security/Security.h>
#include <string>
#include "base/basictypes.h"
#include "net/base/net_export.h"
namespace net {
namespace x509_util {
// Creates a security policy for certificates used as client certificates
// in SSL.
// If a policy is successfully created, it will be stored in
// |*policy| and ownership transferred to the caller.
OSStatus NET_EXPORT CreateSSLClientPolicy(SecPolicyRef* policy);
// Create an SSL server policy. While certificate name validation will be
// performed by SecTrustEvaluate(), it has the following limitations:
// - Doesn't support IP addresses in dotted-quad literals (127.0.0.1)
// - Doesn't support IPv6 addresses
// - Doesn't support the iPAddress subjectAltName
// Providing the hostname is necessary in order to locate certain user or
// system trust preferences, such as those created by Safari. Preferences
// created by Keychain Access do not share this requirement.
// On success, stores the resultant policy in |*policy| and returns noErr.
OSStatus NET_EXPORT CreateSSLServerPolicy(const std::string& hostname,
SecPolicyRef* policy);
// Creates a security policy for basic X.509 validation. If the policy is
// successfully created, it will be stored in |*policy| and ownership
// transferred to the caller.
OSStatus NET_EXPORT CreateBasicX509Policy(SecPolicyRef* policy);
// Creates security policies to control revocation checking (OCSP and CRL).
// If |enable_revocation_checking| is false, the policies returned will be
// explicitly disabled from accessing the network or the cache. This may be
// used to override system settings regarding revocation checking.
// If the policies are successfully created, they will be appended to
// |policies|.
OSStatus NET_EXPORT CreateRevocationPolicies(bool enable_revocation_checking,
CFMutableArrayRef policies);
// Wrapper for a CSSM_DATA_PTR that was obtained via one of the CSSM field
// accessors (such as CSSM_CL_CertGet[First/Next]Value or
// CSSM_CL_CertGet[First/Next]CachedValue).
class CSSMFieldValue {
public:
CSSMFieldValue();
CSSMFieldValue(CSSM_CL_HANDLE cl_handle,
const CSSM_OID* oid,
CSSM_DATA_PTR field);
~CSSMFieldValue();
CSSM_OID_PTR oid() const { return oid_; }
CSSM_DATA_PTR field() const { return field_; }
// Returns the field as if it was an arbitrary type - most commonly, by
// interpreting the field as a specific CSSM/CDSA parsed type, such as
// CSSM_X509_SUBJECT_PUBLIC_KEY_INFO or CSSM_X509_ALGORITHM_IDENTIFIER.
// An added check is applied to ensure that the current field is large
// enough to actually contain the requested type.
template <typename T> const T* GetAs() const {
if (!field_ || field_->Length < sizeof(T))
return NULL;
return reinterpret_cast<const T*>(field_->Data);
}
void Reset(CSSM_CL_HANDLE cl_handle,
CSSM_OID_PTR oid,
CSSM_DATA_PTR field);
private:
CSSM_CL_HANDLE cl_handle_;
CSSM_OID_PTR oid_;
CSSM_DATA_PTR field_;
DISALLOW_COPY_AND_ASSIGN(CSSMFieldValue);
};
// CSSMCachedCertificate is a container class that is used to wrap the
// CSSM_CL_CertCache APIs and provide safe and efficient access to
// certificate fields in their CSSM form.
//
// To provide efficient access to certificate/CRL fields, CSSM provides an
// API/SPI to "cache" a certificate/CRL. The exact meaning of a cached
// certificate is not defined by CSSM, but is documented to generally be some
// intermediate or parsed form of the certificate. In the case of Apple's
// CSSM CL implementation, the intermediate form is the parsed certificate
// stored in an internal format (which happens to be NSS). By caching the
// certificate, callers that wish to access multiple fields (such as subject,
// issuer, and validity dates) do not need to repeatedly parse the entire
// certificate, nor are they forced to convert all fields from their NSS types
// to their CSSM equivalents. This latter point is especially helpful when
// running on OS X 10.5, as it will fail to convert some fields that reference
// unsupported algorithms, such as ECC.
class CSSMCachedCertificate {
public:
CSSMCachedCertificate();
~CSSMCachedCertificate();
// Initializes the CSSMCachedCertificate by caching the specified
// |os_cert_handle|. On success, returns noErr.
// Note: Once initialized, the cached certificate should only be accessed
// from a single thread.
OSStatus Init(SecCertificateRef os_cert_handle);
// Fetches the first value for the field associated with |field_oid|.
// If |field_oid| is a valid OID and is present in the current certificate,
// returns CSSM_OK and stores the first value in |field|. If additional
// values are associated with |field_oid|, they are ignored.
OSStatus GetField(const CSSM_OID* field_oid, CSSMFieldValue* field) const;
private:
CSSM_CL_HANDLE cl_handle_;
CSSM_HANDLE cached_cert_handle_;
};
} // namespace x509_util
} // namespace net
#endif // NET_BASE_X509_UTIL_MAC_H_
......@@ -72,6 +72,14 @@
'base/cert_verifier.h',
'base/cert_verify_proc.cc',
'base/cert_verify_proc.h',
'base/cert_verify_proc_mac.cc',
'base/cert_verify_proc_mac.h',
'base/cert_verify_proc_nss.cc',
'base/cert_verify_proc_nss.h',
'base/cert_verify_proc_openssl.cc',
'base/cert_verify_proc_openssl.h',
'base/cert_verify_proc_win.cc',
'base/cert_verify_proc_win.h',
'base/cert_verify_result.cc',
'base/cert_verify_result.h',
'base/completion_callback.h',
......@@ -269,6 +277,8 @@
'base/x509_certificate_openssl.cc',
'base/x509_certificate_win.cc',
'base/x509_util.h',
'base/x509_util_mac.cc',
'base/x509_util_mac.h',
'base/x509_util_nss.cc',
'base/x509_util_nss.h',
'base/x509_util_openssl.cc',
......@@ -821,6 +831,8 @@
['use_openssl==1', {
'sources!': [
'base/cert_database_nss.cc',
'base/cert_verify_proc_nss.cc',
'base/cert_verify_proc_nss.h',
'base/crypto_module_nss.cc',
'base/dnssec_keyset.cc',
'base/dnssec_keyset.h',
......@@ -852,6 +864,8 @@
{ # else !use_openssl: remove the unneeded files
'sources!': [
'base/cert_database_openssl.cc',
'base/cert_verify_proc_openssl.cc',
'base/cert_verify_proc_openssl.h',
'base/crypto_module_openssl.cc',
'base/keygen_handler_openssl.cc',
'base/openssl_memory_private_key_store.cc',
......@@ -927,6 +941,12 @@
'../build/linux/system.gyp:gdk',
],
}],
[ 'use_nss != 1', {
'sources!': [
'base/cert_verify_proc_nss.cc',
'base/cert_verify_proc_nss.h',
],
}],
[ 'OS == "win"', {
'sources!': [
'http/http_auth_handler_ntlm_portable.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment