This source file includes following definitions.
- VerifyCertificateSignature
- VerifyDomainBoundCert
- TEST
- TEST
#include "base/memory/scoped_ptr.h"
#include "crypto/ec_private_key.h"
#include "crypto/openssl_util.h"
#include "net/cert/x509_util.h"
#include "net/cert/x509_util_openssl.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
void VerifyCertificateSignature(const std::string& der_cert,
const std::vector<uint8>& der_spki) {
const unsigned char* cert_data =
reinterpret_cast<const unsigned char*>(der_cert.data());
int cert_data_len = static_cast<int>(der_cert.size());
crypto::ScopedOpenSSL<X509, X509_free> cert(
d2i_X509(NULL, &cert_data, cert_data_len));
ASSERT_TRUE(cert.get());
const unsigned char* pub_key_data = &der_spki.front();
int pub_key_len = static_cast<int>(der_spki.size());
crypto::ScopedOpenSSL<EVP_PKEY, EVP_PKEY_free> pub_key(
d2i_PUBKEY(NULL, &pub_key_data, pub_key_len));
ASSERT_TRUE(pub_key.get());
EXPECT_EQ(1, X509_verify(cert.get(), pub_key.get()));
}
void VerifyDomainBoundCert(const std::string& domain,
const std::string& der_cert) {
static const char oid_string[] = "1.3.6.1.4.1.11129.2.1.6";
crypto::ScopedOpenSSL<ASN1_OBJECT, ASN1_OBJECT_free> oid_obj(
OBJ_txt2obj(oid_string, 0));
ASSERT_TRUE(oid_obj.get());
const unsigned char* cert_data =
reinterpret_cast<const unsigned char*>(der_cert.data());
int cert_data_len = static_cast<int>(der_cert.size());
crypto::ScopedOpenSSL<X509, X509_free> cert(
d2i_X509(NULL, &cert_data, cert_data_len));
ASSERT_TRUE(cert.get());
int ext_pos = X509_get_ext_by_OBJ(cert.get(), oid_obj.get(), -1);
ASSERT_NE(-1, ext_pos);
X509_EXTENSION* ext = X509_get_ext(cert.get(), ext_pos);
ASSERT_TRUE(ext);
ASN1_STRING* value_asn1 = X509_EXTENSION_get_data(ext);
ASSERT_TRUE(value_asn1);
std::string value_str(reinterpret_cast<const char*>(value_asn1->data),
value_asn1->length);
ASSERT_LE(domain.size(), 127U) << "Domain is too long!";
std::string value_expected;
value_expected.resize(2);
value_expected[0] = 22;
value_expected[1] = static_cast<char>(domain.size());
value_expected += domain;
EXPECT_EQ(value_expected, value_str);
}
}
TEST(X509UtilOpenSSLTest, IsSupportedValidityRange) {
base::Time now = base::Time::Now();
EXPECT_TRUE(x509_util::IsSupportedValidityRange(now, now));
EXPECT_FALSE(x509_util::IsSupportedValidityRange(
now, now - base::TimeDelta::FromSeconds(1)));
const int64 kDaysFromYear0001ToUnixEpoch = 719162;
const int64 kDaysFromUnixEpochToYear10000 = 2932896 + 1;
base::Time too_old = base::Time::UnixEpoch() -
base::TimeDelta::FromDays(kDaysFromYear0001ToUnixEpoch + 1);
base::Time too_late = base::Time::UnixEpoch() +
base::TimeDelta::FromDays(kDaysFromUnixEpochToYear10000 + 1);
EXPECT_FALSE(x509_util::IsSupportedValidityRange(too_old, too_old));
EXPECT_FALSE(x509_util::IsSupportedValidityRange(too_old, now));
EXPECT_FALSE(x509_util::IsSupportedValidityRange(now, too_late));
EXPECT_FALSE(x509_util::IsSupportedValidityRange(too_late, too_late));
}
TEST(X509UtilOpenSSLTest, CreateDomainBoundCertEC) {
std::string domain = "weborigin.com";
base::Time now = base::Time::Now();
scoped_ptr<crypto::ECPrivateKey> private_key(
crypto::ECPrivateKey::Create());
std::string der_cert;
ASSERT_TRUE(
x509_util::CreateDomainBoundCertEC(private_key.get(),
x509_util::DIGEST_SHA1,
domain,
1,
now,
now + base::TimeDelta::FromDays(1),
&der_cert));
VerifyDomainBoundCert(domain, der_cert);
std::vector<uint8> spki;
ASSERT_TRUE(private_key->ExportPublicKey(&spki));
VerifyCertificateSignature(der_cert, spki);
}
}