2012-10-03 00:04:58 +04: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 20:40:04 +03:00
|
|
|
#include "dtlsidentity.h"
|
|
|
|
|
|
|
|
#include "cert.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "cryptohi.h"
|
|
|
|
#include "keyhi.h"
|
2015-07-06 20:40:04 +03:00
|
|
|
#include "nsError.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "pk11pub.h"
|
|
|
|
#include "sechash.h"
|
2015-07-06 20:40:04 +03:00
|
|
|
#include "ssl.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2016-08-17 02:54:00 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DtlsIdentity> DtlsIdentity::Generate() {
|
2016-12-21 17:09:10 +03:00
|
|
|
UniquePK11SlotInfo slot(PK11_GetInternalSlot());
|
2012-10-03 00:04:58 +04:00
|
|
|
if (!slot) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t random_name[16];
|
|
|
|
|
2016-12-21 17:09:10 +03:00
|
|
|
SECStatus rv = PK11_GenerateRandomOnSlot(slot.get(), random_name,
|
2012-10-03 00:04:58 +04:00
|
|
|
sizeof(random_name));
|
|
|
|
if (rv != SECSuccess)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
char chunk[3];
|
2017-02-13 16:42:00 +03:00
|
|
|
for (unsigned char r_name : random_name) {
|
|
|
|
SprintfLiteral(chunk, "%.2x", r_name);
|
2012-10-03 00:04:58 +04:00
|
|
|
name += chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string subject_name_string = "CN=" + name;
|
2016-12-21 17:09:10 +03:00
|
|
|
UniqueCERTName subject_name(CERT_AsciiToName(subject_name_string.c_str()));
|
2012-10-03 00:04:58 +04:00
|
|
|
if (!subject_name) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-06 20:40:04 +03: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-03 00:04:58 +04:00
|
|
|
|
|
|
|
SECKEYPublicKey *pubkey;
|
2016-12-21 17:09:10 +03:00
|
|
|
UniqueSECKEYPrivateKey private_key(
|
|
|
|
PK11_GenerateKeyPair(slot.get(),
|
2015-07-06 20:40:04 +03:00
|
|
|
CKM_EC_KEY_PAIR_GEN, &ecdsaParams, &pubkey,
|
2016-12-21 17:09:10 +03:00
|
|
|
PR_FALSE, PR_TRUE, nullptr));
|
2012-10-03 00:04:58 +04:00
|
|
|
if (private_key == nullptr)
|
|
|
|
return nullptr;
|
2016-12-21 17:09:10 +03:00
|
|
|
UniqueSECKEYPublicKey public_key(pubkey);
|
|
|
|
pubkey = nullptr;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2016-12-21 17:09:10 +03:00
|
|
|
UniqueCERTSubjectPublicKeyInfo spki(
|
|
|
|
SECKEY_CreateSubjectPublicKeyInfo(public_key.get()));
|
2012-10-03 00:04:58 +04:00
|
|
|
if (!spki) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:09:10 +03:00
|
|
|
UniqueCERTCertificateRequest certreq(
|
|
|
|
CERT_CreateCertificateRequest(subject_name.get(), spki.get(), nullptr));
|
2012-10-03 00:04:58 +04:00
|
|
|
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 19:50:14 +04:00
|
|
|
// Note: explicit casts necessary to avoid
|
2012-10-03 00:04:58 +04: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);
|
|
|
|
|
2016-12-21 17:09:10 +03:00
|
|
|
UniqueCERTValidity validity(CERT_CreateValidity(notBefore, notAfter));
|
2012-10-03 00:04:58 +04:00
|
|
|
if (!validity) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long serial;
|
|
|
|
// Note: This serial in principle could collide, but it's unlikely
|
2016-12-21 17:09:10 +03:00
|
|
|
rv = PK11_GenerateRandomOnSlot(slot.get(),
|
2012-10-03 00:04:58 +04:00
|
|
|
reinterpret_cast<unsigned char *>(&serial),
|
|
|
|
sizeof(serial));
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:09:10 +03:00
|
|
|
UniqueCERTCertificate certificate(
|
|
|
|
CERT_CreateCertificate(serial, subject_name.get(), validity.get(),
|
|
|
|
certreq.get()));
|
2012-10-03 00:04:58 +04:00
|
|
|
if (!certificate) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-10-11 10:39:28 +04:00
|
|
|
PLArenaPool *arena = certificate->arena;
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
rv = SECOID_SetAlgorithmID(arena, &certificate->signature,
|
2017-06-28 22:38:00 +03:00
|
|
|
SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE, nullptr);
|
2012-10-03 00:04:58 +04: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;
|
|
|
|
|
2016-12-21 17:09:10 +03:00
|
|
|
if (!SEC_ASN1EncodeItem(arena, &innerDER, certificate.get(),
|
2012-10-03 00:04:58 +04:00
|
|
|
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,
|
2016-12-21 17:09:10 +03:00
|
|
|
private_key.get(),
|
2015-07-06 20:40:04 +03:00
|
|
|
SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE);
|
2012-10-03 00:04:58 +04:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
certificate->derCert = *signedCert;
|
|
|
|
|
2016-12-21 17:09:10 +03:00
|
|
|
RefPtr<DtlsIdentity> identity = new DtlsIdentity(Move(private_key),
|
|
|
|
Move(certificate),
|
|
|
|
ssl_kea_ecdh);
|
2015-04-30 22:17:08 +03:00
|
|
|
return identity.forget();
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
2015-07-06 20:40:04 +03:00
|
|
|
const std::string DtlsIdentity::DEFAULT_HASH_ALGORITHM = "sha-256";
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
nsresult DtlsIdentity::ComputeFingerprint(const std::string algorithm,
|
2015-07-06 20:40:04 +03:00
|
|
|
uint8_t *digest,
|
|
|
|
size_t size,
|
|
|
|
size_t *digest_length) const {
|
2016-10-20 03:29:58 +03:00
|
|
|
const UniqueCERTCertificate& c = cert();
|
2015-07-06 20:40:04 +03:00
|
|
|
MOZ_ASSERT(c);
|
2012-10-03 00:04:58 +04:00
|
|
|
|
2015-07-06 20:40:04 +03:00
|
|
|
return ComputeFingerprint(c, algorithm, digest, size, digest_length);
|
2012-10-03 00:04:58 +04:00
|
|
|
}
|
|
|
|
|
2016-10-20 03:29:58 +03:00
|
|
|
nsresult DtlsIdentity::ComputeFingerprint(const UniqueCERTCertificate& cert,
|
2012-10-03 00:04:58 +04:00
|
|
|
const std::string algorithm,
|
2015-07-06 20:40:04 +03:00
|
|
|
uint8_t *digest,
|
|
|
|
size_t size,
|
|
|
|
size_t *digest_length) {
|
2012-10-03 00:04:58 +04: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 20:40:04 +03:00
|
|
|
if (!ho) {
|
2012-10-03 00:04:58 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-07-06 20:40:04 +03:00
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(ho->length >= 20); // Double check
|
|
|
|
|
2015-07-06 20:40:04 +03:00
|
|
|
if (size < ho->length) {
|
2012-10-03 00:04:58 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-07-06 20:40:04 +03:00
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
SECStatus rv = HASH_HashBuf(ho->type, digest,
|
|
|
|
cert->derCert.data,
|
|
|
|
cert->derCert.len);
|
2015-07-06 20:40:04 +03:00
|
|
|
if (rv != SECSuccess) {
|
2012-10-03 00:04:58 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-07-06 20:40:04 +03:00
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
*digest_length = ho->length;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // close namespace
|