This source file includes following definitions.
- CreateKeyAndDomainBoundCertEC
- CreateKeyAndSelfSignedCert
#include "net/cert/x509_util.h"
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "crypto/ec_private_key.h"
#include "crypto/rsa_private_key.h"
#include "net/cert/x509_certificate.h"
namespace net {
namespace x509_util {
static const uint16 kRSAKeyLength = 1024;
static const DigestAlgorithm kSignatureDigestAlgorithm = DIGEST_SHA256;
ClientCertSorter::ClientCertSorter() : now_(base::Time::Now()) {}
bool ClientCertSorter::operator()(
const scoped_refptr<X509Certificate>& a,
const scoped_refptr<X509Certificate>& b) const {
if (!a.get() || !b.get())
return a.get() && !b.get();
bool a_is_valid = now_ >= a->valid_start() && now_ <= a->valid_expiry();
bool b_is_valid = now_ >= b->valid_start() && now_ <= b->valid_expiry();
if (a_is_valid != b_is_valid)
return a_is_valid && !b_is_valid;
if (a->valid_expiry() != b->valid_expiry())
return a->valid_expiry() > b->valid_expiry();
if (a->valid_start() != b->valid_start())
return a->valid_start() > b->valid_start();
const X509Certificate::OSCertHandles& a_intermediates =
a->GetIntermediateCertificates();
const X509Certificate::OSCertHandles& b_intermediates =
b->GetIntermediateCertificates();
return a_intermediates.size() < b_intermediates.size();
}
bool CreateKeyAndDomainBoundCertEC(const std::string& domain,
uint32 serial_number,
base::Time not_valid_before,
base::Time not_valid_after,
scoped_ptr<crypto::ECPrivateKey>* key,
std::string* der_cert) {
scoped_ptr<crypto::ECPrivateKey> new_key(crypto::ECPrivateKey::Create());
if (!new_key.get())
return false;
bool success = CreateDomainBoundCertEC(new_key.get(),
kSignatureDigestAlgorithm,
domain,
serial_number,
not_valid_before,
not_valid_after,
der_cert);
if (success)
key->reset(new_key.release());
return success;
}
bool CreateKeyAndSelfSignedCert(const std::string& subject,
uint32 serial_number,
base::Time not_valid_before,
base::Time not_valid_after,
scoped_ptr<crypto::RSAPrivateKey>* key,
std::string* der_cert) {
scoped_ptr<crypto::RSAPrivateKey> new_key(
crypto::RSAPrivateKey::Create(kRSAKeyLength));
if (!new_key.get())
return false;
bool success = CreateSelfSignedCert(new_key.get(),
kSignatureDigestAlgorithm,
subject,
serial_number,
not_valid_before,
not_valid_after,
der_cert);
if (success)
key->reset(new_key.release());
return success;
}
}
}