2014-05-15 03:20:00 -07: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 14:25:13 -07:00
|
|
|
#include "mozilla/dom/CryptoKey.h"
|
2014-05-15 03:20:00 -07:00
|
|
|
#include "mozilla/dom/WebCryptoCommon.h"
|
|
|
|
#include "mozilla/dom/SubtleCryptoBinding.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-06-23 14:25:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(CryptoKey, mGlobal, mAlgorithm)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(CryptoKey)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(CryptoKey)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CryptoKey)
|
2014-05-15 03:20:00 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-05-26 03:05:00 -07:00
|
|
|
nsresult
|
2014-06-23 14:25:13 -07:00
|
|
|
StringToUsage(const nsString& aUsage, CryptoKey::KeyUsage& aUsageOut)
|
2014-05-26 03:05:00 -07:00
|
|
|
{
|
|
|
|
if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_ENCRYPT)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::ENCRYPT;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_DECRYPT)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::DECRYPT;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_SIGN)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::SIGN;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_VERIFY)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::VERIFY;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_DERIVEKEY)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::DERIVEKEY;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_DERIVEBITS)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::DERIVEBITS;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_WRAPKEY)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::WRAPKEY;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else if (aUsage.EqualsLiteral(WEBCRYPTO_KEY_USAGE_UNWRAPKEY)) {
|
2014-06-23 14:25:13 -07:00
|
|
|
aUsageOut = CryptoKey::UNWRAPKEY;
|
2014-05-26 03:05:00 -07:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::CryptoKey(nsIGlobalObject* aGlobal)
|
2014-05-15 03:20:00 -07:00
|
|
|
: mGlobal(aGlobal)
|
|
|
|
, mAttributes(0)
|
|
|
|
, mSymKey()
|
|
|
|
, mPrivateKey(nullptr)
|
|
|
|
, mPublicKey(nullptr)
|
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::~CryptoKey()
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
destructorSafeDestroyNSSReference();
|
|
|
|
shutdown(calledFromObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::WrapObject(JSContext* aCx)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
2014-06-23 14:25:13 -07:00
|
|
|
return CryptoKeyBinding::Wrap(aCx, this);
|
2014-05-15 03:20:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::GetType(nsString& aRetVal) const
|
2014-05-15 03:20:00 -07: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 14:25:13 -07:00
|
|
|
CryptoKey::Extractable() const
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
return (mAttributes & EXTRACTABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyAlgorithm*
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::Algorithm() const
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
return mAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::GetUsages(nsTArray<nsString>& aRetVal) const
|
2014-05-15 03:20:00 -07: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-06-23 14:25:13 -07:00
|
|
|
CryptoKey::KeyType
|
|
|
|
CryptoKey::GetKeyType() const
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
2014-06-23 14:25:13 -07:00
|
|
|
return static_cast<CryptoKey::KeyType>(mAttributes & TYPE_MASK);
|
2014-05-15 03:20:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::SetType(const nsString& aType)
|
2014-05-15 03:20:00 -07: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 14:25:13 -07:00
|
|
|
CryptoKey::SetType(CryptoKey::KeyType aType)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
mAttributes &= CLEAR_TYPE;
|
|
|
|
mAttributes |= aType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::SetExtractable(bool aExtractable)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
mAttributes &= CLEAR_EXTRACTABLE;
|
|
|
|
if (aExtractable) {
|
|
|
|
mAttributes |= EXTRACTABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::SetAlgorithm(KeyAlgorithm* aAlgorithm)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
mAlgorithm = aAlgorithm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::ClearUsages()
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
mAttributes &= CLEAR_USAGES;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::AddUsage(const nsString& aUsage)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
2014-05-26 03:05:00 -07:00
|
|
|
return AddUsageIntersecting(aUsage, USAGES_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::AddUsageIntersecting(const nsString& aUsage, uint32_t aUsageMask)
|
2014-05-26 03:05:00 -07:00
|
|
|
{
|
|
|
|
KeyUsage usage;
|
|
|
|
if (NS_FAILED(StringToUsage(aUsage, usage))) {
|
2014-05-15 03:20:00 -07:00
|
|
|
return NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
|
|
|
|
2014-05-26 03:05:00 -07:00
|
|
|
if (usage & aUsageMask) {
|
|
|
|
AddUsage(usage);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-15 03:20:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::AddUsage(CryptoKey::KeyUsage aUsage)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
mAttributes |= aUsage;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::HasUsage(CryptoKey::KeyUsage aUsage)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
return !!(mAttributes & aUsage);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::HasUsageOtherThan(uint32_t aUsages)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
return !!(mAttributes & USAGES_MASK & ~aUsages);
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:25:13 -07:00
|
|
|
void CryptoKey::SetSymKey(const CryptoBuffer& aSymKey)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
mSymKey = aSymKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::SetPrivateKey(SECKEYPrivateKey* aPrivateKey)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!aPrivateKey || isAlreadyShutDown()) {
|
|
|
|
mPrivateKey = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPrivateKey = SECKEY_CopyPrivateKey(aPrivateKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::SetPublicKey(SECKEYPublicKey* aPublicKey)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!aPublicKey || isAlreadyShutDown()) {
|
|
|
|
mPublicKey = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPublicKey = SECKEY_CopyPublicKey(aPublicKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CryptoBuffer&
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::GetSymKey() const
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
return mSymKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPrivateKey*
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::GetPrivateKey() const
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!mPrivateKey || isAlreadyShutDown()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return SECKEY_CopyPrivateKey(mPrivateKey.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPublicKey*
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::GetPublicKey() const
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (!mPublicKey || isAlreadyShutDown()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return SECKEY_CopyPublicKey(mPublicKey.get());
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:25:13 -07:00
|
|
|
void CryptoKey::virtualDestroyNSSReference()
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
destructorSafeDestroyNSSReference();
|
|
|
|
}
|
|
|
|
|
2014-06-23 14:25:13 -07:00
|
|
|
void CryptoKey::destructorSafeDestroyNSSReference()
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
mPrivateKey.dispose();
|
|
|
|
mPublicKey.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Serialization and deserialization convenience methods
|
|
|
|
|
|
|
|
SECKEYPrivateKey*
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::PrivateKeyFromPkcs8(CryptoBuffer& aKeyData,
|
2014-05-15 03:20:00 -07:00
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
SECKEYPrivateKey* privKey;
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
|
|
|
|
ScopedSECItem pkcs8Item(aKeyData.ToSECItem());
|
|
|
|
if (!pkcs8Item) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow everything, we enforce usage ourselves
|
|
|
|
unsigned int usage = KU_ALL;
|
|
|
|
|
|
|
|
nsresult rv = MapSECStatus(PK11_ImportDERPrivateKeyInfoAndReturnKey(
|
|
|
|
slot.get(), pkcs8Item.get(), nullptr, nullptr, false, false,
|
|
|
|
usage, &privKey, nullptr));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return privKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECKEYPublicKey*
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::PublicKeyFromSpki(CryptoBuffer& aKeyData,
|
2014-05-15 03:20:00 -07:00
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
ScopedSECItem spkiItem(aKeyData.ToSECItem());
|
|
|
|
if (!spkiItem) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedCERTSubjectPublicKeyInfo spki(SECKEY_DecodeDERSubjectPublicKeyInfo(spkiItem.get()));
|
|
|
|
if (!spki) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECKEY_ExtractPublicKey(spki.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::PrivateKeyToPkcs8(SECKEYPrivateKey* aPrivKey,
|
2014-05-15 03:20:00 -07: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-06-23 14:25:13 -07:00
|
|
|
CryptoKey::PublicKeyToSpki(SECKEYPublicKey* aPubKey,
|
2014-05-15 03:20:00 -07:00
|
|
|
CryptoBuffer& aRetVal,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
ScopedSECItem spkiItem(PK11_DEREncodePublicKey(aPubKey));
|
|
|
|
if (!spkiItem.get()) {
|
|
|
|
return NS_ERROR_DOM_INVALID_ACCESS_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRetVal.Assign(spkiItem.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const
|
2014-05-15 03:20:00 -07: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 14:25:13 -07:00
|
|
|
CryptoKey::PrivateKeyToPkcs8(mPrivateKey, priv, locker);
|
2014-05-15 03:20:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPublicKey) {
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::PublicKeyToSpki(mPublicKey, pub, locker);
|
2014-05-15 03:20:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return JS_WriteUint32Pair(aWriter, mAttributes, 0) &&
|
|
|
|
WriteBuffer(aWriter, mSymKey) &&
|
|
|
|
WriteBuffer(aWriter, priv) &&
|
|
|
|
WriteBuffer(aWriter, pub) &&
|
|
|
|
mAlgorithm->WriteStructuredClone(aWriter);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-23 14:25:13 -07:00
|
|
|
CryptoKey::ReadStructuredClone(JSStructuredCloneReader* aReader)
|
2014-05-15 03:20:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t zero;
|
|
|
|
CryptoBuffer sym, priv, pub;
|
|
|
|
nsRefPtr<KeyAlgorithm> algorithm;
|
|
|
|
|
|
|
|
bool read = JS_ReadUint32Pair(aReader, &mAttributes, &zero) &&
|
|
|
|
ReadBuffer(aReader, sym) &&
|
|
|
|
ReadBuffer(aReader, priv) &&
|
|
|
|
ReadBuffer(aReader, pub) &&
|
|
|
|
(algorithm = KeyAlgorithm::Create(mGlobal, aReader));
|
|
|
|
if (!read) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym.Length() > 0) {
|
|
|
|
mSymKey = sym;
|
|
|
|
}
|
|
|
|
if (priv.Length() > 0) {
|
2014-06-23 14:25:13 -07:00
|
|
|
mPrivateKey = CryptoKey::PrivateKeyFromPkcs8(priv, locker);
|
2014-05-15 03:20:00 -07:00
|
|
|
}
|
|
|
|
if (pub.Length() > 0) {
|
2014-06-23 14:25:13 -07:00
|
|
|
mPublicKey = CryptoKey::PublicKeyFromSpki(pub, locker);
|
2014-05-15 03:20:00 -07:00
|
|
|
}
|
|
|
|
mAlgorithm = algorithm;
|
|
|
|
|
|
|
|
// 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
|