2014-05-15 10:20:00 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "pk11pub.h"
|
|
|
|
#include "cryptohi.h"
|
|
|
|
#include "ScopedNSSTypes.h"
|
2014-06-23 21:25:13 +00:00
|
|
|
#include "mozilla/dom/CryptoKey.h"
|
2014-05-15 10:20:00 +00:00
|
|
|
#include "mozilla/dom/WebCryptoCommon.h"
|
|
|
|
#include "mozilla/dom/SubtleCryptoBinding.h"
|
2014-09-27 18:22:57 +00:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2014-05-15 10:20:00 +00:00
|
|
|
|
2014-08-22 13:32:15 +00:00
|
|
|
// Templates taken from security/nss/lib/cryptohi/seckey.c
|
|
|
|
// These would ideally be exported by NSS and until that
|
|
|
|
// happens we have to keep our own copies.
|
|
|
|
const SEC_ASN1Template SECKEY_DHPublicKeyTemplate[] = {
|
|
|
|
{ SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.publicValue), },
|
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
const SEC_ASN1Template SECKEY_DHParamKeyTemplate[] = {
|
|
|
|
{ SEC_ASN1_SEQUENCE, 0, NULL, sizeof(SECKEYPublicKey) },
|
|
|
|
{ SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.prime), },
|
|
|
|
{ SEC_ASN1_INTEGER, offsetof(SECKEYPublicKey,u.dh.base), },
|
|
|
|
{ SEC_ASN1_SKIP_REST },
|
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
|
2014-05-15 10:20:00 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CryptoKey, mGlobal)
|
2014-06-23 21:25:13 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(CryptoKey)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(CryptoKey)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CryptoKey)
|
2014-05-15 10:20:00 +00:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-05-26 10:05:00 +00:00
|
|
|
nsresult
|
2014-06-23 21:25:13 +00:00
|
|
|
StringToUsage(const nsString& aUsage, CryptoKey::KeyUsage& aUsageOut)
|
2014-05-26 10:05:00 +00:00
|
|
|
{
|
|
|
|
if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_ENCRYPT)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::ENCRYPT;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_DECRYPT)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::DECRYPT;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_SIGN)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::SIGN;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_VERIFY)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::VERIFY;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_DERIVEKEY)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::DERIVEKEY;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_DERIVEBITS)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::DERIVEBITS;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_WRAPKEY)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::WRAPKEY;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_UNWRAPKEY)) {
|
2014-06-23 21:25:13 +00:00
|
|
|
aUsageOut = CryptoKey::UNWRAPKEY;
|
2014-05-26 10:05:00 +00:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::CryptoKey(nsIGlobalObject* aGlobal)
|
2014-05-15 10:20:00 +00:00
|
|
|
: mGlobal(aGlobal)
|
|
|
|
, mAttributes(0)
|
|
|
|
, mSymKey()
|
|
|
|
, mPrivateKey(nullptr)
|
|
|
|
, mPublicKey(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::~CryptoKey()
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
destructorSafeDestroyNSSReference();
|
|
|
|
shutdown(calledFromObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::WrapObject(JSContext* aCx)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
2014-06-23 21:25:13 +00:00
|
|
|
return CryptoKeyBinding::Wrap(aCx, this);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::GetType(nsString& aRetVal) const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
uint32_t type = mAttributes & TYPE_MASK;
|
|
|
|
switch (type) {
|
|
|
|
case PUBLIC: aRetVal.AssignLiteral(WEBCRYPTO_KEY_TYPE_PUBLIC); break;
|
|
|
|
case PRIVATE: aRetVal.AssignLiteral(WEBCRYPTO_KEY_TYPE_PRIVATE); break;
|
|
|
|
case SECRET: aRetVal.AssignLiteral(WEBCRYPTO_KEY_TYPE_SECRET); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::Extractable() const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
return (mAttributes & EXTRACTABLE);
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
void
|
|
|
|
CryptoKey::GetAlgorithm(JSContext* cx, JS::MutableHandle<JSObject*> aRetVal,
|
|
|
|
ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
bool converted = false;
|
|
|
|
JS::RootedValue val(cx);
|
|
|
|
switch (mAlgorithm.mType) {
|
|
|
|
case KeyAlgorithmProxy::AES:
|
|
|
|
converted = ToJSValue(cx, mAlgorithm.mAes, &val);
|
|
|
|
break;
|
|
|
|
case KeyAlgorithmProxy::HMAC:
|
|
|
|
converted = ToJSValue(cx, mAlgorithm.mHmac, &val);
|
|
|
|
break;
|
|
|
|
case KeyAlgorithmProxy::RSA: {
|
|
|
|
RootedDictionary<RsaHashedKeyAlgorithm> rsa(cx);
|
|
|
|
mAlgorithm.mRsa.ToKeyAlgorithm(cx, rsa);
|
|
|
|
converted = ToJSValue(cx, rsa, &val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case KeyAlgorithmProxy::EC:
|
|
|
|
converted = ToJSValue(cx, mAlgorithm.mEc, &val);
|
|
|
|
break;
|
2014-07-20 03:51:10 +00:00
|
|
|
case KeyAlgorithmProxy::DH: {
|
|
|
|
RootedDictionary<DhKeyAlgorithm> dh(cx);
|
|
|
|
mAlgorithm.mDh.ToKeyAlgorithm(cx, dh);
|
|
|
|
converted = ToJSValue(cx, dh, &val);
|
|
|
|
break;
|
|
|
|
}
|
2014-09-27 18:22:57 +00:00
|
|
|
}
|
|
|
|
if (!converted) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_OPERATION_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRetVal.set(&val.toObject());
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::GetUsages(nsTArray<nsString>& aRetVal) const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
if (mAttributes & ENCRYPT) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_ENCRYPT));
|
|
|
|
}
|
|
|
|
if (mAttributes & DECRYPT) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_DECRYPT));
|
|
|
|
}
|
|
|
|
if (mAttributes & SIGN) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_SIGN));
|
|
|
|
}
|
|
|
|
if (mAttributes & VERIFY) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_VERIFY));
|
|
|
|
}
|
|
|
|
if (mAttributes & DERIVEKEY) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_DERIVEKEY));
|
|
|
|
}
|
|
|
|
if (mAttributes & DERIVEBITS) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_DERIVEBITS));
|
|
|
|
}
|
|
|
|
if (mAttributes & WRAPKEY) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_WRAPKEY));
|
|
|
|
}
|
|
|
|
if (mAttributes & UNWRAPKEY) {
|
|
|
|
aRetVal.AppendElement(NS_LITERAL_STRING(WEBCRYPTO_KEY_USAGE_UNWRAPKEY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
KeyAlgorithmProxy&
|
|
|
|
CryptoKey::Algorithm()
|
|
|
|
{
|
|
|
|
return mAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KeyAlgorithmProxy&
|
|
|
|
CryptoKey::Algorithm() const
|
|
|
|
{
|
|
|
|
return mAlgorithm;
|
|
|
|
}
|
|
|
|
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::KeyType
|
|
|
|
CryptoKey::GetKeyType() const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
2014-06-23 21:25:13 +00:00
|
|
|
return static_cast<CryptoKey::KeyType>(mAttributes & TYPE_MASK);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::SetType(const nsString& aType)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
mAttributes &= CLEAR_TYPE;
|
|
|
|
if (aType.EqualsLiteral(WEBCRYPTO_KEY_TYPE_SECRET)) {
|
|
|
|
mAttributes |= SECRET;
|
|
|
|
} else if (aType.EqualsLiteral(WEBCRYPTO_KEY_TYPE_PUBLIC)) {
|
|
|
|
mAttributes |= PUBLIC;
|
|
|
|
} else if (aType.EqualsLiteral(WEBCRYPTO_KEY_TYPE_PRIVATE)) {
|
|
|
|
mAttributes |= PRIVATE;
|
|
|
|
} else {
|
|
|
|
mAttributes |= UNKNOWN;
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::SetType(CryptoKey::KeyType aType)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
mAttributes &= CLEAR_TYPE;
|
|
|
|
mAttributes |= aType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::SetExtractable(bool aExtractable)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
mAttributes &= CLEAR_EXTRACTABLE;
|
|
|
|
if (aExtractable) {
|
|
|
|
mAttributes |= EXTRACTABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::ClearUsages()
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
mAttributes &= CLEAR_USAGES;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::AddUsage(const nsString& aUsage)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
2014-05-26 10:05:00 +00:00
|
|
|
return AddUsageIntersecting(aUsage, USAGES_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::AddUsageIntersecting(const nsString& aUsage, uint32_t aUsageMask)
|
2014-05-26 10:05:00 +00:00
|
|
|
{
|
|
|
|
KeyUsage usage;
|
|
|
|
if (NS_FAILED(StringToUsage(aUsage, usage))) {
|
2014-05-15 10:20:00 +00:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2014-05-26 10:05:00 +00:00
|
|
|
if (usage & aUsageMask) {
|
|
|
|
AddUsage(usage);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:20:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::AddUsage(CryptoKey::KeyUsage aUsage)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
mAttributes |= aUsage;
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
bool
|
|
|
|
CryptoKey::HasAnyUsage()
|
|
|
|
{
|
|
|
|
return !!(mAttributes & USAGES_MASK);
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:20:00 +00:00
|
|
|
bool
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::HasUsage(CryptoKey::KeyUsage aUsage)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
return !!(mAttributes & aUsage);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::HasUsageOtherThan(uint32_t aUsages)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
return !!(mAttributes & USAGES_MASK & ~aUsages);
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
bool
|
|
|
|
CryptoKey::IsRecognizedUsage(const nsString& aUsage)
|
|
|
|
{
|
|
|
|
KeyUsage dummy;
|
|
|
|
nsresult rv = StringToUsage(aUsage, dummy);
|
|
|
|
return NS_SUCCEEDED(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CryptoKey::AllUsagesRecognized(const Sequence<nsString>& aUsages)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aUsages.Length(); ++i) {
|
|
|
|
if (!IsRecognizedUsage(aUsages[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-23 21:25:13 +00:00
|
|
|
void CryptoKey::SetSymKey(const CryptoBuffer& aSymKey)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
mSymKey = aSymKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::SetPrivateKey(SECKEYPrivateKey* aPrivateKey)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!aPrivateKey || isAlreadyShutDown()) {
|
|
|
|
mPrivateKey = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPrivateKey = SECKEY_CopyPrivateKey(aPrivateKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::SetPublicKey(SECKEYPublicKey* aPublicKey)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!aPublicKey || isAlreadyShutDown()) {
|
|
|
|
mPublicKey = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPublicKey = SECKEY_CopyPublicKey(aPublicKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CryptoBuffer&
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::GetSymKey() const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
return mSymKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPrivateKey*
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::GetPrivateKey() const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!mPrivateKey || isAlreadyShutDown()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return SECKEY_CopyPrivateKey(mPrivateKey.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPublicKey*
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::GetPublicKey() const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!mPublicKey || isAlreadyShutDown()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return SECKEY_CopyPublicKey(mPublicKey.get());
|
|
|
|
}
|
|
|
|
|
2014-06-23 21:25:13 +00:00
|
|
|
void CryptoKey::virtualDestroyNSSReference()
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
destructorSafeDestroyNSSReference();
|
|
|
|
}
|
|
|
|
|
2014-06-23 21:25:13 +00:00
|
|
|
void CryptoKey::destructorSafeDestroyNSSReference()
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
mPrivateKey.dispose();
|
|
|
|
mPublicKey.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Serialization and deserialization convenience methods
|
|
|
|
|
|
|
|
SECKEYPrivateKey*
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::PrivateKeyFromPkcs8(CryptoBuffer& aKeyData,
|
2014-05-15 10:20:00 +00:00
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
SECKEYPrivateKey* privKey;
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
2014-09-03 13:40:21 +00:00
|
|
|
|
|
|
|
ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECItem pkcs8Item = { siBuffer, nullptr, 0 };
|
|
|
|
if (!aKeyData.ToSECItem(arena, &pkcs8Item)) {
|
2014-05-15 10:20:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow everything, we enforce usage ourselves
|
|
|
|
unsigned int usage = KU_ALL;
|
|
|
|
|
2014-07-25 11:53:28 +00:00
|
|
|
SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey(
|
2014-09-03 13:40:21 +00:00
|
|
|
slot.get(), &pkcs8Item, nullptr, nullptr, false, false,
|
2014-07-25 11:53:28 +00:00
|
|
|
usage, &privKey, nullptr);
|
2014-05-15 10:20:00 +00:00
|
|
|
|
2014-07-25 11:53:28 +00:00
|
|
|
if (rv == SECFailure) {
|
2014-05-15 10:20:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return privKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPublicKey*
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::PublicKeyFromSpki(CryptoBuffer& aKeyData,
|
2014-05-15 10:20:00 +00:00
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
2014-09-03 13:40:21 +00:00
|
|
|
ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECItem spkiItem = { siBuffer, nullptr, 0 };
|
|
|
|
if (!aKeyData.ToSECItem(arena, &spkiItem)) {
|
2014-05-15 10:20:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-03 13:40:21 +00:00
|
|
|
ScopedCERTSubjectPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(&spkiItem));
|
2014-05-15 10:20:00 +00:00
|
|
|
if (!spki) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:32:15 +00:00
|
|
|
bool isECDHAlgorithm = SECITEM_ItemsAreEqual(&SEC_OID_DATA_EC_DH,
|
|
|
|
&spki->algorithm.algorithm);
|
|
|
|
bool isDHAlgorithm = SECITEM_ItemsAreEqual(&SEC_OID_DATA_DH_KEY_AGREEMENT,
|
|
|
|
&spki->algorithm.algorithm);
|
|
|
|
|
|
|
|
// Check for |id-ecDH| and |dhKeyAgreement|. Per the WebCrypto spec we must
|
|
|
|
// support these OIDs but NSS does unfortunately not know about them. Let's
|
|
|
|
// change the algorithm to |id-ecPublicKey| or |dhPublicKey| to make NSS happy.
|
|
|
|
if (isECDHAlgorithm || isDHAlgorithm) {
|
|
|
|
SECOidTag oid = SEC_OID_UNKNOWN;
|
|
|
|
if (isECDHAlgorithm) {
|
|
|
|
oid = SEC_OID_ANSIX962_EC_PUBLIC_KEY;
|
|
|
|
} else if (isDHAlgorithm) {
|
|
|
|
oid = SEC_OID_X942_DIFFIE_HELMAN_KEY;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECOidData* oidData = SECOID_FindOIDByTag(oid);
|
2014-08-04 07:39:12 +00:00
|
|
|
if (!oidData) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus rv = SECITEM_CopyItem(spki->arena, &spki->algorithm.algorithm,
|
|
|
|
&oidData->oid);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 21:19:00 +00:00
|
|
|
ScopedSECKEYPublicKey tmp(SECKEY_ExtractPublicKey(spki.get()));
|
|
|
|
if (!tmp.get() || !PublicKeyValid(tmp.get())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECKEY_CopyPublicKey(tmp);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::PrivateKeyToPkcs8(SECKEYPrivateKey* aPrivKey,
|
2014-05-15 10:20:00 +00:00
|
|
|
CryptoBuffer& aRetVal,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
ScopedSECItem pkcs8Item(PK11_ExportDERPrivateKeyInfo(aPrivKey, nullptr));
|
|
|
|
if (!pkcs8Item.get()) {
|
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
aRetVal.Assign(pkcs8Item.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-08-22 13:32:15 +00:00
|
|
|
PublicDhKeyToSpki(SECKEYPublicKey* aPubKey,
|
2014-10-17 13:14:18 +00:00
|
|
|
CERTSubjectPublicKeyInfo* aSpki)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
2014-10-17 13:14:18 +00:00
|
|
|
SECItem* params = ::SECITEM_AllocItem(aSpki->arena, nullptr, 0);
|
2014-08-22 13:32:15 +00:00
|
|
|
if (!params) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:14:18 +00:00
|
|
|
SECItem* rvItem = SEC_ASN1EncodeItem(aSpki->arena, params, aPubKey,
|
2014-08-22 13:32:15 +00:00
|
|
|
SECKEY_DHParamKeyTemplate);
|
|
|
|
if (!rvItem) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:14:18 +00:00
|
|
|
SECStatus rv = SECOID_SetAlgorithmID(aSpki->arena, &aSpki->algorithm,
|
2014-08-22 13:32:15 +00:00
|
|
|
SEC_OID_X942_DIFFIE_HELMAN_KEY, params);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:14:18 +00:00
|
|
|
rvItem = SEC_ASN1EncodeItem(aSpki->arena, &aSpki->subjectPublicKey, aPubKey,
|
2014-08-22 13:32:15 +00:00
|
|
|
SECKEY_DHPublicKeyTemplate);
|
|
|
|
if (!rvItem) {
|
2014-08-04 07:39:12 +00:00
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
2014-08-04 07:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 13:32:15 +00:00
|
|
|
// The public value is a BIT_STRING encoded as an INTEGER. After encoding
|
|
|
|
// an INT we need to adjust the length to reflect the number of bits.
|
|
|
|
aSpki->subjectPublicKey.len <<= 3;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CryptoKey::PublicKeyToSpki(SECKEYPublicKey* aPubKey,
|
|
|
|
CryptoBuffer& aRetVal,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
ScopedCERTSubjectPublicKeyInfo spki;
|
|
|
|
|
|
|
|
// NSS doesn't support exporting DH public keys.
|
|
|
|
if (aPubKey->keyType == dhKey) {
|
2014-10-17 13:05:59 +00:00
|
|
|
// Mimic the behavior of SECKEY_CreateSubjectPublicKeyInfo() and create
|
|
|
|
// a new arena for the SPKI object.
|
|
|
|
ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
2014-10-17 12:58:33 +00:00
|
|
|
if (!arena) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
2014-08-22 13:32:15 +00:00
|
|
|
spki = PORT_ArenaZNew(arena, CERTSubjectPublicKeyInfo);
|
|
|
|
if (!spki) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
2014-10-17 13:05:59 +00:00
|
|
|
// Assign |arena| to |spki| and null the variable afterwards so that the
|
|
|
|
// arena created above that holds the SPKI object is free'd when |spki|
|
|
|
|
// goes out of scope, not when |arena| does.
|
|
|
|
spki->arena = arena.forget();
|
|
|
|
|
2014-10-17 13:14:18 +00:00
|
|
|
nsresult rv = PublicDhKeyToSpki(aPubKey, spki);
|
2014-08-22 13:32:15 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
spki = SECKEY_CreateSubjectPublicKeyInfo(aPubKey);
|
|
|
|
if (!spki) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 07:39:12 +00:00
|
|
|
// Per WebCrypto spec we must export ECDH SPKIs with the algorithm OID
|
2014-08-22 13:32:15 +00:00
|
|
|
// id-ecDH (1.3.132.112) and DH SPKIs with OID dhKeyAgreement
|
|
|
|
// (1.2.840.113549.1.3.1). NSS doesn't know about these OIDs and there is
|
2014-08-04 07:39:12 +00:00
|
|
|
// no way to specify the algorithm to use when exporting a public key.
|
2014-08-22 13:32:15 +00:00
|
|
|
if (aPubKey->keyType == ecKey || aPubKey->keyType == dhKey) {
|
|
|
|
const SECItem* oidData = nullptr;
|
|
|
|
if (aPubKey->keyType == ecKey) {
|
|
|
|
oidData = &SEC_OID_DATA_EC_DH;
|
|
|
|
} else if (aPubKey->keyType == dhKey) {
|
|
|
|
oidData = &SEC_OID_DATA_DH_KEY_AGREEMENT;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
|
|
|
|
2014-10-17 12:58:33 +00:00
|
|
|
SECStatus rv = SECITEM_CopyItem(spki->arena, &spki->algorithm.algorithm,
|
|
|
|
oidData);
|
2014-08-04 07:39:12 +00:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SEC_ASN1Template* tpl = SEC_ASN1_GET(CERT_SubjectPublicKeyInfoTemplate);
|
2014-10-17 12:58:33 +00:00
|
|
|
ScopedSECItem spkiItem(SEC_ASN1EncodeItem(nullptr, nullptr, spki, tpl));
|
2014-08-04 07:39:12 +00:00
|
|
|
|
2014-10-17 12:58:33 +00:00
|
|
|
aRetVal.Assign(spkiItem.get());
|
2014-05-15 10:20:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
SECItem*
|
|
|
|
CreateECPointForCoordinates(const CryptoBuffer& aX,
|
|
|
|
const CryptoBuffer& aY,
|
|
|
|
PLArenaPool* aArena)
|
2014-07-19 13:25:00 +00:00
|
|
|
{
|
2014-07-29 09:10:07 +00:00
|
|
|
// Check that both points have the same length.
|
|
|
|
if (aX.Length() != aY.Length()) {
|
2014-08-05 18:04:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
// Create point.
|
|
|
|
SECItem* point = ::SECITEM_AllocItem(aArena, nullptr, aX.Length() + aY.Length() + 1);
|
|
|
|
if (!point) {
|
2014-07-19 13:25:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
// Set point data.
|
|
|
|
point->data[0] = EC_POINT_FORM_UNCOMPRESSED;
|
|
|
|
memcpy(point->data + 1, aX.Elements(), aX.Length());
|
|
|
|
memcpy(point->data + 1 + aX.Length(), aY.Elements(), aY.Length());
|
2014-07-19 13:25:00 +00:00
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
return point;
|
|
|
|
}
|
2014-07-19 13:25:00 +00:00
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
SECKEYPrivateKey*
|
|
|
|
PrivateKeyFromPrivateKeyTemplate(SECItem* aObjID,
|
|
|
|
CK_ATTRIBUTE* aTemplate,
|
|
|
|
CK_ULONG aTemplateSize)
|
|
|
|
{
|
2014-07-19 13:25:00 +00:00
|
|
|
// Create a generic object with the contents of the key
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
|
|
|
if (!slot.get()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedPK11GenericObject obj(PK11_CreateGenericObject(slot.get(),
|
2014-07-29 09:10:07 +00:00
|
|
|
aTemplate,
|
|
|
|
aTemplateSize,
|
2014-07-19 13:25:00 +00:00
|
|
|
PR_FALSE));
|
|
|
|
if (!obj.get()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have NSS translate the object to a private key by inspection
|
|
|
|
// and make a copy we can own
|
2014-07-29 09:10:07 +00:00
|
|
|
ScopedSECKEYPrivateKey privKey(PK11_FindKeyByKeyID(slot.get(), aObjID,
|
2014-07-19 13:25:00 +00:00
|
|
|
nullptr));
|
|
|
|
if (!privKey.get()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-29 09:10:07 +00:00
|
|
|
|
2014-07-19 13:25:00 +00:00
|
|
|
return SECKEY_CopyPrivateKey(privKey.get());
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
SECKEYPrivateKey*
|
|
|
|
CryptoKey::PrivateKeyFromJwk(const JsonWebKey& aJwk,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
CK_OBJECT_CLASS privateKeyValue = CKO_PRIVATE_KEY;
|
|
|
|
CK_BBOOL falseValue = CK_FALSE;
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
if (aJwk.mKty.EqualsLiteral(JWK_TYPE_EC)) {
|
2014-07-29 09:10:07 +00:00
|
|
|
// Verify that all of the required parameters are present
|
|
|
|
CryptoBuffer x, y, d;
|
|
|
|
if (!aJwk.mCrv.WasPassed() ||
|
|
|
|
!aJwk.mX.WasPassed() || NS_FAILED(x.FromJwkBase64(aJwk.mX.Value())) ||
|
|
|
|
!aJwk.mY.WasPassed() || NS_FAILED(y.FromJwkBase64(aJwk.mY.Value())) ||
|
|
|
|
!aJwk.mD.WasPassed() || NS_FAILED(d.FromJwkBase64(aJwk.mD.Value()))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString namedCurve;
|
2014-09-27 18:22:57 +00:00
|
|
|
if (!NormalizeToken(aJwk.mCrv.Value(), namedCurve)) {
|
2014-07-29 09:10:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create parameters.
|
|
|
|
SECItem* params = CreateECParamsForCurve(namedCurve, arena.get());
|
|
|
|
if (!params) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECItem* ecPoint = CreateECPointForCoordinates(x, y, arena.get());
|
|
|
|
if (!ecPoint) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the ID for this key
|
|
|
|
// This is generated with a SHA-1 hash, so unlikely to collide
|
|
|
|
ScopedSECItem objID(PK11_MakeIDFromPubKey(ecPoint));
|
|
|
|
if (!objID.get()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate template from parameters
|
|
|
|
CK_KEY_TYPE ecValue = CKK_EC;
|
|
|
|
CK_ATTRIBUTE keyTemplate[9] = {
|
|
|
|
{ CKA_CLASS, &privateKeyValue, sizeof(privateKeyValue) },
|
|
|
|
{ CKA_KEY_TYPE, &ecValue, sizeof(ecValue) },
|
|
|
|
{ CKA_TOKEN, &falseValue, sizeof(falseValue) },
|
|
|
|
{ CKA_SENSITIVE, &falseValue, sizeof(falseValue) },
|
|
|
|
{ CKA_PRIVATE, &falseValue, sizeof(falseValue) },
|
|
|
|
{ CKA_ID, objID->data, objID->len },
|
|
|
|
{ CKA_EC_PARAMS, params->data, params->len },
|
|
|
|
{ CKA_EC_POINT, ecPoint->data, ecPoint->len },
|
|
|
|
{ CKA_VALUE, (void*) d.Elements(), d.Length() },
|
|
|
|
};
|
|
|
|
|
|
|
|
return PrivateKeyFromPrivateKeyTemplate(objID, keyTemplate,
|
|
|
|
PR_ARRAY_SIZE(keyTemplate));
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
if (aJwk.mKty.EqualsLiteral(JWK_TYPE_RSA)) {
|
2014-07-29 09:10:07 +00:00
|
|
|
// Verify that all of the required parameters are present
|
|
|
|
CryptoBuffer n, e, d, p, q, dp, dq, qi;
|
|
|
|
if (!aJwk.mN.WasPassed() || NS_FAILED(n.FromJwkBase64(aJwk.mN.Value())) ||
|
|
|
|
!aJwk.mE.WasPassed() || NS_FAILED(e.FromJwkBase64(aJwk.mE.Value())) ||
|
|
|
|
!aJwk.mD.WasPassed() || NS_FAILED(d.FromJwkBase64(aJwk.mD.Value())) ||
|
|
|
|
!aJwk.mP.WasPassed() || NS_FAILED(p.FromJwkBase64(aJwk.mP.Value())) ||
|
|
|
|
!aJwk.mQ.WasPassed() || NS_FAILED(q.FromJwkBase64(aJwk.mQ.Value())) ||
|
|
|
|
!aJwk.mDp.WasPassed() || NS_FAILED(dp.FromJwkBase64(aJwk.mDp.Value())) ||
|
|
|
|
!aJwk.mDq.WasPassed() || NS_FAILED(dq.FromJwkBase64(aJwk.mDq.Value())) ||
|
|
|
|
!aJwk.mQi.WasPassed() || NS_FAILED(qi.FromJwkBase64(aJwk.mQi.Value()))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-03 13:40:21 +00:00
|
|
|
ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
// Compute the ID for this key
|
|
|
|
// This is generated with a SHA-1 hash, so unlikely to collide
|
2014-09-03 13:40:21 +00:00
|
|
|
SECItem nItem = { siBuffer, nullptr, 0 };
|
|
|
|
if (!n.ToSECItem(arena, &nItem)) {
|
2014-07-29 09:10:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-09-03 13:40:21 +00:00
|
|
|
ScopedSECItem objID(PK11_MakeIDFromPubKey(&nItem));
|
2014-07-29 09:10:07 +00:00
|
|
|
if (!objID.get()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate template from parameters
|
|
|
|
CK_KEY_TYPE rsaValue = CKK_RSA;
|
|
|
|
CK_ATTRIBUTE keyTemplate[14] = {
|
|
|
|
{ CKA_CLASS, &privateKeyValue, sizeof(privateKeyValue) },
|
|
|
|
{ CKA_KEY_TYPE, &rsaValue, sizeof(rsaValue) },
|
|
|
|
{ CKA_TOKEN, &falseValue, sizeof(falseValue) },
|
|
|
|
{ CKA_SENSITIVE, &falseValue, sizeof(falseValue) },
|
|
|
|
{ CKA_PRIVATE, &falseValue, sizeof(falseValue) },
|
|
|
|
{ CKA_ID, objID->data, objID->len },
|
|
|
|
{ CKA_MODULUS, (void*) n.Elements(), n.Length() },
|
|
|
|
{ CKA_PUBLIC_EXPONENT, (void*) e.Elements(), e.Length() },
|
|
|
|
{ CKA_PRIVATE_EXPONENT, (void*) d.Elements(), d.Length() },
|
|
|
|
{ CKA_PRIME_1, (void*) p.Elements(), p.Length() },
|
|
|
|
{ CKA_PRIME_2, (void*) q.Elements(), q.Length() },
|
|
|
|
{ CKA_EXPONENT_1, (void*) dp.Elements(), dp.Length() },
|
|
|
|
{ CKA_EXPONENT_2, (void*) dq.Elements(), dq.Length() },
|
|
|
|
{ CKA_COEFFICIENT, (void*) qi.Elements(), qi.Length() },
|
|
|
|
};
|
|
|
|
|
|
|
|
return PrivateKeyFromPrivateKeyTemplate(objID, keyTemplate,
|
|
|
|
PR_ARRAY_SIZE(keyTemplate));
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-19 13:25:00 +00:00
|
|
|
bool ReadAndEncodeAttribute(SECKEYPrivateKey* aKey,
|
|
|
|
CK_ATTRIBUTE_TYPE aAttribute,
|
|
|
|
Optional<nsString>& aDst)
|
|
|
|
{
|
2014-08-06 10:15:35 +00:00
|
|
|
ScopedSECItem item(::SECITEM_AllocItem(nullptr, nullptr, 0));
|
|
|
|
if (!item) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-19 13:25:00 +00:00
|
|
|
if (PK11_ReadRawAttribute(PK11_TypePrivKey, aKey, aAttribute, item)
|
|
|
|
!= SECSuccess) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CryptoBuffer buffer;
|
|
|
|
if (!buffer.Assign(item)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(buffer.ToJwkBase64(aDst.Value()))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
bool
|
|
|
|
ECKeyToJwk(const PK11ObjectType aKeyType, void* aKey, const SECItem* aEcParams,
|
|
|
|
const SECItem* aPublicValue, JsonWebKey& aRetVal)
|
|
|
|
{
|
|
|
|
aRetVal.mX.Construct();
|
|
|
|
aRetVal.mY.Construct();
|
|
|
|
|
|
|
|
// Check that the given EC parameters are valid.
|
|
|
|
if (!CheckEncodedECParameters(aEcParams)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the OID tag.
|
|
|
|
SECItem oid = { siBuffer, nullptr, 0 };
|
|
|
|
oid.len = aEcParams->data[1];
|
|
|
|
oid.data = aEcParams->data + 2;
|
|
|
|
|
|
|
|
uint32_t flen;
|
|
|
|
switch (SECOID_FindOIDTag(&oid)) {
|
|
|
|
case SEC_OID_SECG_EC_SECP256R1:
|
|
|
|
flen = 32; // bytes
|
|
|
|
aRetVal.mCrv.Construct(NS_LITERAL_STRING(WEBCRYPTO_NAMED_CURVE_P256));
|
|
|
|
break;
|
|
|
|
case SEC_OID_SECG_EC_SECP384R1:
|
|
|
|
flen = 48; // bytes
|
|
|
|
aRetVal.mCrv.Construct(NS_LITERAL_STRING(WEBCRYPTO_NAMED_CURVE_P384));
|
|
|
|
break;
|
|
|
|
case SEC_OID_SECG_EC_SECP521R1:
|
|
|
|
flen = 66; // bytes
|
|
|
|
aRetVal.mCrv.Construct(NS_LITERAL_STRING(WEBCRYPTO_NAMED_CURVE_P521));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No support for compressed points.
|
|
|
|
if (aPublicValue->data[0] != EC_POINT_FORM_UNCOMPRESSED) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check length of uncompressed point coordinates.
|
|
|
|
if (aPublicValue->len != (2 * flen + 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedSECItem ecPointX(::SECITEM_AllocItem(nullptr, nullptr, flen));
|
|
|
|
ScopedSECItem ecPointY(::SECITEM_AllocItem(nullptr, nullptr, flen));
|
|
|
|
if (!ecPointX || !ecPointY) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract point data.
|
|
|
|
memcpy(ecPointX->data, aPublicValue->data + 1, flen);
|
|
|
|
memcpy(ecPointY->data, aPublicValue->data + 1 + flen, flen);
|
|
|
|
|
|
|
|
CryptoBuffer x, y;
|
|
|
|
if (!x.Assign(ecPointX) || NS_FAILED(x.ToJwkBase64(aRetVal.mX.Value())) ||
|
|
|
|
!y.Assign(ecPointY) || NS_FAILED(y.ToJwkBase64(aRetVal.mY.Value()))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
aRetVal.mKty = NS_LITERAL_STRING(JWK_TYPE_EC);
|
2014-07-29 09:10:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-19 13:25:00 +00:00
|
|
|
nsresult
|
|
|
|
CryptoKey::PrivateKeyToJwk(SECKEYPrivateKey* aPrivKey,
|
|
|
|
JsonWebKey& aRetVal,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
switch (aPrivKey->keyType) {
|
|
|
|
case rsaKey: {
|
|
|
|
aRetVal.mN.Construct();
|
|
|
|
aRetVal.mE.Construct();
|
|
|
|
aRetVal.mD.Construct();
|
|
|
|
aRetVal.mP.Construct();
|
|
|
|
aRetVal.mQ.Construct();
|
|
|
|
aRetVal.mDp.Construct();
|
|
|
|
aRetVal.mDq.Construct();
|
|
|
|
aRetVal.mQi.Construct();
|
|
|
|
|
|
|
|
if (!ReadAndEncodeAttribute(aPrivKey, CKA_MODULUS, aRetVal.mN) ||
|
|
|
|
!ReadAndEncodeAttribute(aPrivKey, CKA_PUBLIC_EXPONENT, aRetVal.mE) ||
|
|
|
|
!ReadAndEncodeAttribute(aPrivKey, CKA_PRIVATE_EXPONENT, aRetVal.mD) ||
|
|
|
|
!ReadAndEncodeAttribute(aPrivKey, CKA_PRIME_1, aRetVal.mP) ||
|
|
|
|
!ReadAndEncodeAttribute(aPrivKey, CKA_PRIME_2, aRetVal.mQ) ||
|
|
|
|
!ReadAndEncodeAttribute(aPrivKey, CKA_EXPONENT_1, aRetVal.mDp) ||
|
|
|
|
!ReadAndEncodeAttribute(aPrivKey, CKA_EXPONENT_2, aRetVal.mDq) ||
|
|
|
|
!ReadAndEncodeAttribute(aPrivKey, CKA_COEFFICIENT, aRetVal.mQi)) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
aRetVal.mKty = NS_LITERAL_STRING(JWK_TYPE_RSA);
|
2014-07-19 13:25:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-07-29 09:10:07 +00:00
|
|
|
case ecKey: {
|
|
|
|
// Read EC params.
|
|
|
|
ScopedSECItem params(::SECITEM_AllocItem(nullptr, nullptr, 0));
|
|
|
|
SECStatus rv = PK11_ReadRawAttribute(PK11_TypePrivKey, aPrivKey,
|
|
|
|
CKA_EC_PARAMS, params);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read public point Q.
|
|
|
|
ScopedSECItem ecPoint(::SECITEM_AllocItem(nullptr, nullptr, 0));
|
|
|
|
rv = PK11_ReadRawAttribute(PK11_TypePrivKey, aPrivKey, CKA_EC_POINT,
|
|
|
|
ecPoint);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ECKeyToJwk(PK11_TypePrivKey, aPrivKey, params, ecPoint, aRetVal)) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRetVal.mD.Construct();
|
|
|
|
|
|
|
|
// Read private value.
|
|
|
|
if (!ReadAndEncodeAttribute(aPrivKey, CKA_VALUE, aRetVal.mD)) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-07-19 13:25:00 +00:00
|
|
|
default:
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPublicKey*
|
|
|
|
CryptoKey::PublicKeyFromJwk(const JsonWebKey& aJwk,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
2014-09-27 18:22:57 +00:00
|
|
|
if (aJwk.mKty.EqualsLiteral(JWK_TYPE_RSA)) {
|
2014-07-29 09:10:07 +00:00
|
|
|
// Verify that all of the required parameters are present
|
|
|
|
CryptoBuffer n, e;
|
|
|
|
if (!aJwk.mN.WasPassed() || NS_FAILED(n.FromJwkBase64(aJwk.mN.Value())) ||
|
|
|
|
!aJwk.mE.WasPassed() || NS_FAILED(e.FromJwkBase64(aJwk.mE.Value()))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transcode to a DER RSAPublicKey structure
|
|
|
|
struct RSAPublicKeyData {
|
|
|
|
SECItem n;
|
|
|
|
SECItem e;
|
|
|
|
};
|
|
|
|
const RSAPublicKeyData input = {
|
|
|
|
{ siUnsignedInteger, n.Elements(), (unsigned int) n.Length() },
|
|
|
|
{ siUnsignedInteger, e.Elements(), (unsigned int) e.Length() }
|
|
|
|
};
|
|
|
|
const SEC_ASN1Template rsaPublicKeyTemplate[] = {
|
|
|
|
{SEC_ASN1_SEQUENCE, 0, nullptr, sizeof(RSAPublicKeyData)},
|
|
|
|
{SEC_ASN1_INTEGER, offsetof(RSAPublicKeyData, n),},
|
|
|
|
{SEC_ASN1_INTEGER, offsetof(RSAPublicKeyData, e),},
|
|
|
|
{0,}
|
|
|
|
};
|
|
|
|
|
|
|
|
ScopedSECItem pkDer(SEC_ASN1EncodeItem(nullptr, nullptr, &input,
|
|
|
|
rsaPublicKeyTemplate));
|
|
|
|
if (!pkDer.get()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECKEY_ImportDERPublicKey(pkDer.get(), CKK_RSA);
|
2014-07-19 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
if (aJwk.mKty.EqualsLiteral(JWK_TYPE_EC)) {
|
2014-07-29 09:10:07 +00:00
|
|
|
// Verify that all of the required parameters are present
|
|
|
|
CryptoBuffer x, y;
|
|
|
|
if (!aJwk.mCrv.WasPassed() ||
|
|
|
|
!aJwk.mX.WasPassed() || NS_FAILED(x.FromJwkBase64(aJwk.mX.Value())) ||
|
|
|
|
!aJwk.mY.WasPassed() || NS_FAILED(y.FromJwkBase64(aJwk.mY.Value()))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPublicKey* key = PORT_ArenaZNew(arena, SECKEYPublicKey);
|
|
|
|
if (!key) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
key->keyType = ecKey;
|
|
|
|
key->pkcs11Slot = nullptr;
|
|
|
|
key->pkcs11ID = CK_INVALID_HANDLE;
|
|
|
|
|
|
|
|
nsString namedCurve;
|
2014-09-27 18:22:57 +00:00
|
|
|
if (!NormalizeToken(aJwk.mCrv.Value(), namedCurve)) {
|
2014-07-29 09:10:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create parameters.
|
|
|
|
SECItem* params = CreateECParamsForCurve(namedCurve, arena.get());
|
|
|
|
if (!params) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
key->u.ec.DEREncodedParams = *params;
|
|
|
|
|
|
|
|
// Create point.
|
|
|
|
SECItem* point = CreateECPointForCoordinates(x, y, arena.get());
|
|
|
|
if (!point) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
key->u.ec.publicValue = *point;
|
|
|
|
|
2014-10-13 21:19:00 +00:00
|
|
|
if (!PublicKeyValid(key)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
return SECKEY_CopyPublicKey(key);
|
2014-07-19 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 09:10:07 +00:00
|
|
|
return nullptr;
|
2014-07-19 13:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CryptoKey::PublicKeyToJwk(SECKEYPublicKey* aPubKey,
|
|
|
|
JsonWebKey& aRetVal,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
switch (aPubKey->keyType) {
|
|
|
|
case rsaKey: {
|
|
|
|
CryptoBuffer n, e;
|
|
|
|
aRetVal.mN.Construct();
|
|
|
|
aRetVal.mE.Construct();
|
|
|
|
|
|
|
|
if (!n.Assign(&aPubKey->u.rsa.modulus) ||
|
|
|
|
!e.Assign(&aPubKey->u.rsa.publicExponent) ||
|
|
|
|
NS_FAILED(n.ToJwkBase64(aRetVal.mN.Value())) ||
|
|
|
|
NS_FAILED(e.ToJwkBase64(aRetVal.mE.Value()))) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
aRetVal.mKty = NS_LITERAL_STRING(JWK_TYPE_RSA);
|
2014-07-19 13:25:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-07-29 09:10:07 +00:00
|
|
|
case ecKey:
|
|
|
|
if (!ECKeyToJwk(PK11_TypePubKey, aPubKey, &aPubKey->u.ec.DEREncodedParams,
|
|
|
|
&aPubKey->u.ec.publicValue, aRetVal)) {
|
|
|
|
return NS_ERROR_DOM_OPERATION_ERR;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2014-07-19 13:25:00 +00:00
|
|
|
default:
|
|
|
|
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 10:02:14 +00:00
|
|
|
SECKEYPublicKey*
|
|
|
|
CryptoKey::PublicDhKeyFromRaw(CryptoBuffer& aKeyData,
|
|
|
|
const CryptoBuffer& aPrime,
|
|
|
|
const CryptoBuffer& aGenerator,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
ScopedPLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPublicKey* key = PORT_ArenaZNew(arena, SECKEYPublicKey);
|
|
|
|
if (!key) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
key->keyType = dhKey;
|
|
|
|
key->pkcs11Slot = nullptr;
|
|
|
|
key->pkcs11ID = CK_INVALID_HANDLE;
|
|
|
|
|
|
|
|
// Set DH public key params.
|
|
|
|
if (!aPrime.ToSECItem(arena, &key->u.dh.prime) ||
|
|
|
|
!aGenerator.ToSECItem(arena, &key->u.dh.base) ||
|
|
|
|
!aKeyData.ToSECItem(arena, &key->u.dh.publicValue)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
key->u.dh.prime.type = siUnsignedInteger;
|
|
|
|
key->u.dh.base.type = siUnsignedInteger;
|
|
|
|
key->u.dh.publicValue.type = siUnsignedInteger;
|
|
|
|
|
|
|
|
return SECKEY_CopyPublicKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CryptoKey::PublicDhKeyToRaw(SECKEYPublicKey* aPubKey,
|
|
|
|
CryptoBuffer& aRetVal,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
aRetVal.Assign(&aPubKey->u.dh.publicValue);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-13 21:19:00 +00:00
|
|
|
bool
|
|
|
|
CryptoKey::PublicKeyValid(SECKEYPublicKey* aPubKey)
|
|
|
|
{
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
|
|
|
if (!slot.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This assumes that NSS checks the validity of a public key when
|
|
|
|
// it is imported into a PKCS#11 module, and returns CK_INVALID_HANDLE
|
|
|
|
// if it is invalid.
|
|
|
|
CK_OBJECT_HANDLE id = PK11_ImportPublicKey(slot, aPubKey, PR_FALSE);
|
|
|
|
if (id == CK_INVALID_HANDLE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus rv = PK11_DestroyObject(slot, id);
|
|
|
|
return (rv == SECSuccess);
|
|
|
|
}
|
|
|
|
|
2014-05-15 10:20:00 +00:00
|
|
|
bool
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write in five pieces
|
|
|
|
// 1. Attributes
|
|
|
|
// 2. Symmetric key as raw (if present)
|
|
|
|
// 3. Private key as pkcs8 (if present)
|
|
|
|
// 4. Public key as spki (if present)
|
|
|
|
// 5. Algorithm in whatever form it chooses
|
|
|
|
CryptoBuffer priv, pub;
|
|
|
|
|
|
|
|
if (mPrivateKey) {
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::PrivateKeyToPkcs8(mPrivateKey, priv, locker);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPublicKey) {
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::PublicKeyToSpki(mPublicKey, pub, locker);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
return JS_WriteUint32Pair(aWriter, mAttributes, CRYPTOKEY_SC_VERSION) &&
|
2014-05-15 10:20:00 +00:00
|
|
|
WriteBuffer(aWriter, mSymKey) &&
|
|
|
|
WriteBuffer(aWriter, priv) &&
|
|
|
|
WriteBuffer(aWriter, pub) &&
|
2014-09-27 18:22:57 +00:00
|
|
|
mAlgorithm.WriteStructuredClone(aWriter);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-23 21:25:13 +00:00
|
|
|
CryptoKey::ReadStructuredClone(JSStructuredCloneReader* aReader)
|
2014-05-15 10:20:00 +00:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
uint32_t version;
|
2014-05-15 10:20:00 +00:00
|
|
|
CryptoBuffer sym, priv, pub;
|
|
|
|
|
2014-09-27 18:22:57 +00:00
|
|
|
bool read = JS_ReadUint32Pair(aReader, &mAttributes, &version) &&
|
|
|
|
(version == CRYPTOKEY_SC_VERSION) &&
|
2014-05-15 10:20:00 +00:00
|
|
|
ReadBuffer(aReader, sym) &&
|
|
|
|
ReadBuffer(aReader, priv) &&
|
|
|
|
ReadBuffer(aReader, pub) &&
|
2014-09-27 18:22:57 +00:00
|
|
|
mAlgorithm.ReadStructuredClone(aReader);
|
2014-05-15 10:20:00 +00:00
|
|
|
if (!read) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym.Length() > 0) {
|
|
|
|
mSymKey = sym;
|
|
|
|
}
|
|
|
|
if (priv.Length() > 0) {
|
2014-06-23 21:25:13 +00:00
|
|
|
mPrivateKey = CryptoKey::PrivateKeyFromPkcs8(priv, locker);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
if (pub.Length() > 0) {
|
2014-06-23 21:25:13 +00:00
|
|
|
mPublicKey = CryptoKey::PublicKeyFromSpki(pub, locker);
|
2014-05-15 10:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that what we've read is consistent
|
|
|
|
// If the attributes indicate a key type, should have a key of that type
|
|
|
|
if (!((GetKeyType() == SECRET && mSymKey.Length() > 0) ||
|
|
|
|
(GetKeyType() == PRIVATE && mPrivateKey) ||
|
|
|
|
(GetKeyType() == PUBLIC && mPublicKey))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|