2012-10-02 13:04:58 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-07-06 10:40:04 -07:00
|
|
|
#include "dtlsidentity.h"
|
|
|
|
|
|
|
|
#include "cert.h"
|
2012-10-02 13:04:58 -07:00
|
|
|
#include "cryptohi.h"
|
|
|
|
#include "keyhi.h"
|
2015-07-06 10:40:04 -07:00
|
|
|
#include "nsError.h"
|
2012-10-02 13:04:58 -07:00
|
|
|
#include "pk11pub.h"
|
2015-07-06 10:40:04 -07:00
|
|
|
#include "prprf.h"
|
2012-10-02 13:04:58 -07:00
|
|
|
#include "sechash.h"
|
2015-07-06 10:40:04 -07:00
|
|
|
#include "ssl.h"
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
DtlsIdentity::~DtlsIdentity() {
|
2015-07-06 10:40:04 -07:00
|
|
|
if (cert_) {
|
2012-10-02 13:04:58 -07:00
|
|
|
CERT_DestroyCertificate(cert_);
|
2015-07-06 10:40:04 -07:00
|
|
|
}
|
2012-10-02 13:04:58 -07:00
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DtlsIdentity> DtlsIdentity::Generate() {
|
2012-10-02 13:04:58 -07:00
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
|
|
|
if (!slot) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t random_name[16];
|
|
|
|
|
|
|
|
SECStatus rv = PK11_GenerateRandomOnSlot(slot, random_name,
|
|
|
|
sizeof(random_name));
|
|
|
|
if (rv != SECSuccess)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
char chunk[3];
|
2013-01-31 09:13:29 -08:00
|
|
|
for (size_t i = 0; i < sizeof(random_name); ++i) {
|
2012-10-02 13:04:58 -07:00
|
|
|
PR_snprintf(chunk, sizeof(chunk), "%.2x", random_name[i]);
|
|
|
|
name += chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string subject_name_string = "CN=" + name;
|
|
|
|
ScopedCERTName subject_name(CERT_AsciiToName(subject_name_string.c_str()));
|
|
|
|
if (!subject_name) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-06 10:40:04 -07:00
|
|
|
unsigned char paramBuf[12]; // OIDs are small
|
|
|
|
SECItem ecdsaParams = { siBuffer, paramBuf, sizeof(paramBuf) };
|
|
|
|
SECOidData* oidData = SECOID_FindOIDByTag(SEC_OID_SECG_EC_SECP256R1);
|
|
|
|
if (!oidData || (oidData->oid.len > (sizeof(paramBuf) - 2))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ecdsaParams.data[0] = SEC_ASN1_OBJECT_ID;
|
|
|
|
ecdsaParams.data[1] = oidData->oid.len;
|
|
|
|
memcpy(ecdsaParams.data + 2, oidData->oid.data, oidData->oid.len);
|
|
|
|
ecdsaParams.len = oidData->oid.len + 2;
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
ScopedSECKEYPrivateKey private_key;
|
|
|
|
ScopedSECKEYPublicKey public_key;
|
|
|
|
SECKEYPublicKey *pubkey;
|
|
|
|
|
|
|
|
private_key =
|
|
|
|
PK11_GenerateKeyPair(slot,
|
2015-07-06 10:40:04 -07:00
|
|
|
CKM_EC_KEY_PAIR_GEN, &ecdsaParams, &pubkey,
|
2012-10-25 09:32:24 -07:00
|
|
|
PR_FALSE, PR_TRUE, nullptr);
|
2012-10-02 13:04:58 -07:00
|
|
|
if (private_key == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
public_key = pubkey;
|
|
|
|
|
|
|
|
ScopedCERTSubjectPublicKeyInfo spki(
|
|
|
|
SECKEY_CreateSubjectPublicKeyInfo(pubkey));
|
|
|
|
if (!spki) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedCERTCertificateRequest certreq(
|
|
|
|
CERT_CreateCertificateRequest(subject_name, spki, nullptr));
|
|
|
|
if (!certreq) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From 1 day before todayto 30 days after.
|
|
|
|
// This is a sort of arbitrary range designed to be valid
|
|
|
|
// now with some slack in case the other side expects
|
|
|
|
// some before expiry.
|
|
|
|
//
|
2014-01-02 07:50:14 -08:00
|
|
|
// Note: explicit casts necessary to avoid
|
2012-10-02 13:04:58 -07:00
|
|
|
// warning C4307: '*' : integral constant overflow
|
|
|
|
static const PRTime oneDay = PRTime(PR_USEC_PER_SEC)
|
|
|
|
* PRTime(60) // sec
|
|
|
|
* PRTime(60) // min
|
|
|
|
* PRTime(24); // hours
|
|
|
|
PRTime now = PR_Now();
|
|
|
|
PRTime notBefore = now - oneDay;
|
|
|
|
PRTime notAfter = now + (PRTime(30) * oneDay);
|
|
|
|
|
|
|
|
ScopedCERTValidity validity(CERT_CreateValidity(notBefore, notAfter));
|
|
|
|
if (!validity) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long serial;
|
|
|
|
// Note: This serial in principle could collide, but it's unlikely
|
|
|
|
rv = PK11_GenerateRandomOnSlot(slot,
|
|
|
|
reinterpret_cast<unsigned char *>(&serial),
|
|
|
|
sizeof(serial));
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedCERTCertificate certificate(
|
|
|
|
CERT_CreateCertificate(serial, subject_name, validity, certreq));
|
|
|
|
if (!certificate) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-10 23:39:28 -07:00
|
|
|
PLArenaPool *arena = certificate->arena;
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
rv = SECOID_SetAlgorithmID(arena, &certificate->signature,
|
2015-07-06 10:40:04 -07:00
|
|
|
SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE, 0);
|
2012-10-02 13:04:58 -07:00
|
|
|
if (rv != SECSuccess)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Set version to X509v3.
|
|
|
|
*(certificate->version.data) = SEC_CERTIFICATE_VERSION_3;
|
|
|
|
certificate->version.len = 1;
|
|
|
|
|
|
|
|
SECItem innerDER;
|
|
|
|
innerDER.len = 0;
|
|
|
|
innerDER.data = nullptr;
|
|
|
|
|
|
|
|
if (!SEC_ASN1EncodeItem(arena, &innerDER, certificate,
|
|
|
|
SEC_ASN1_GET(CERT_CertificateTemplate))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECItem *signedCert = PORT_ArenaZNew(arena, SECItem);
|
|
|
|
if (!signedCert) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = SEC_DerSignData(arena, signedCert, innerDER.data, innerDER.len,
|
|
|
|
private_key,
|
2015-07-06 10:40:04 -07:00
|
|
|
SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE);
|
2012-10-02 13:04:58 -07:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
certificate->derCert = *signedCert;
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DtlsIdentity> identity =
|
2015-07-06 10:40:04 -07:00
|
|
|
new DtlsIdentity(private_key.forget(), certificate.forget(), ssl_kea_ecdh);
|
2015-04-30 12:17:08 -07:00
|
|
|
return identity.forget();
|
2012-10-02 13:04:58 -07:00
|
|
|
}
|
|
|
|
|
2015-07-06 10:40:04 -07:00
|
|
|
const std::string DtlsIdentity::DEFAULT_HASH_ALGORITHM = "sha-256";
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
nsresult DtlsIdentity::ComputeFingerprint(const std::string algorithm,
|
2015-07-06 10:40:04 -07:00
|
|
|
uint8_t *digest,
|
|
|
|
size_t size,
|
|
|
|
size_t *digest_length) const {
|
|
|
|
const CERTCertificate* c = cert();
|
|
|
|
MOZ_ASSERT(c);
|
2012-10-02 13:04:58 -07:00
|
|
|
|
2015-07-06 10:40:04 -07:00
|
|
|
return ComputeFingerprint(c, algorithm, digest, size, digest_length);
|
2012-10-02 13:04:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult DtlsIdentity::ComputeFingerprint(const CERTCertificate *cert,
|
|
|
|
const std::string algorithm,
|
2015-07-06 10:40:04 -07:00
|
|
|
uint8_t *digest,
|
|
|
|
size_t size,
|
|
|
|
size_t *digest_length) {
|
2012-10-02 13:04:58 -07:00
|
|
|
MOZ_ASSERT(cert);
|
|
|
|
|
|
|
|
HASH_HashType ht;
|
|
|
|
|
|
|
|
if (algorithm == "sha-1") {
|
|
|
|
ht = HASH_AlgSHA1;
|
|
|
|
} else if (algorithm == "sha-224") {
|
|
|
|
ht = HASH_AlgSHA224;
|
|
|
|
} else if (algorithm == "sha-256") {
|
|
|
|
ht = HASH_AlgSHA256;
|
|
|
|
} else if (algorithm == "sha-384") {
|
|
|
|
ht = HASH_AlgSHA384;
|
|
|
|
} else if (algorithm == "sha-512") {
|
|
|
|
ht = HASH_AlgSHA512;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SECHashObject *ho = HASH_GetHashObject(ht);
|
|
|
|
MOZ_ASSERT(ho);
|
2015-07-06 10:40:04 -07:00
|
|
|
if (!ho) {
|
2012-10-02 13:04:58 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-07-06 10:40:04 -07:00
|
|
|
}
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
MOZ_ASSERT(ho->length >= 20); // Double check
|
|
|
|
|
2015-07-06 10:40:04 -07:00
|
|
|
if (size < ho->length) {
|
2012-10-02 13:04:58 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-07-06 10:40:04 -07:00
|
|
|
}
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
SECStatus rv = HASH_HashBuf(ho->type, digest,
|
|
|
|
cert->derCert.data,
|
|
|
|
cert->derCert.len);
|
2015-07-06 10:40:04 -07:00
|
|
|
if (rv != SECSuccess) {
|
2012-10-02 13:04:58 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-07-06 10:40:04 -07:00
|
|
|
}
|
2012-10-02 13:04:58 -07:00
|
|
|
|
|
|
|
*digest_length = ho->length;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // close namespace
|