Files
palemoon27/security/manager/ssl/nsKeyModule.cpp
T
roytam1 13934d9866 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1184996 (Part 1) - Create decoders with a DecoderFactory. r=tn (dacf21ed2)
- Bug 1184996 (Part 2) - Clean up RasterImage's decoding API. r=tn (c127af0b3)
- Bug 1184996 (Part 3) - Replace all remaining references to 'size decodes' with 'metadata decodes'. r=tn (3744e5df4)
- Bug 1184996 (Part 4) - Forbid instantiation of decoders except via DecoderFactory. r=tn (588d56d84)
- No bug - Remove extra printf left over from bug 1127618. r=smaug (b02f7bfe1)
- Bug 1187386 (Part 1) - Make most decoder state private. r=tn (328dbc605)
- Bug 1187386 (Part 2) - Rework decoder code to avoid calling Decode::GetImage(). r=tn (9a94096f9)
- Bug 1187386 (Part 3) - Don't destroy Decoder::mImage if Decoder::mImage is null. r=tn (cbb6738cd)
- Bug 1187386 (Part 4) - Make imgFrame::SetOptimizable() callable from off-main-thread. r=tn (f03478b28)
- Bug 1187386 (Part 5) - Merge Decoder::SetSizeOnImage() into ImageMetadata::SetOnImage(). r=tn (8afb5d4a3)
- Bug 1033090 - Truncate a large URI in the user message about it. r=seth (7bd4b447b)
- Bug 1187386 (Part 6) - Merge Decoder::Finish() and RasterImage::OnDecodingComplete() into RasterImage::FinalizeDecoder(). r=tn (f342dd5db)
- Bug 1181863 (Part 1) - Add support for reading from nsIInputStreams directly to SourceBuffer. r=tn (74748dad9)
- Bug 1181863 (Part 2) - Add ImageOps::DecodeToSurface() to allow image decoding without involving any main-thread-only objects. r=tn (25b86eb50)
- Bug 1181863 (Part 3) - Add tests for DecodeToSurface(). r=tn (9506eb2f6)
-  Bug 1181863 - Part 4: Fix the build bustage (aee05bdc9)
- Bug 1187546 - Make it possible to ask image decoders to only decode the first frame. r=tn (1beeeefb6)
- Bug 1191100 - Remove XPIDL signature comments in .cpp files. r=ehsan (ee75fe3b5)
2021-09-09 09:51:21 +08:00

195 lines
4.3 KiB
C++

/* 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 "nsComponentManagerUtils.h"
#include "nsCOMPtr.h"
#include "nsKeyModule.h"
#include "nsString.h"
#include "ScopedNSSTypes.h"
using namespace mozilla;
using namespace mozilla::psm;
NS_IMPL_ISUPPORTS(nsKeyObject, nsIKeyObject)
nsKeyObject::nsKeyObject()
: mKeyType(0), mSymKey(nullptr), mPrivateKey(nullptr),
mPublicKey(nullptr)
{
}
nsKeyObject::~nsKeyObject()
{
CleanUp();
}
void
nsKeyObject::CleanUp()
{
switch (mKeyType) {
case nsIKeyObject::SYM_KEY:
PK11_FreeSymKey(mSymKey);
break;
case nsIKeyObject::PRIVATE_KEY:
PK11_DeleteTokenPrivateKey(mPrivateKey, true /* force */);
break;
case nsIKeyObject::PUBLIC_KEY:
PK11_DeleteTokenPublicKey(mPublicKey);
break;
default:
// probably not initialized, do nothing
break;
}
mKeyType = 0;
}
//////////////////////////////////////////////////////////////////////////////
// nsIKeyObject
NS_IMETHODIMP
nsKeyObject::InitKey(int16_t aAlgorithm, void * aKey)
{
// Clear previous key data if it exists
CleanUp();
switch (aAlgorithm) {
case nsIKeyObject::RC4:
case nsIKeyObject::HMAC:
mSymKey = reinterpret_cast<PK11SymKey*>(aKey);
if (!mSymKey) {
NS_ERROR("no symkey");
break;
}
mKeyType = nsIKeyObject::SYM_KEY;
break;
case nsIKeyObject::AES_CBC:
return NS_ERROR_NOT_IMPLEMENTED;
default:
return NS_ERROR_INVALID_ARG;
}
// One of these should have been created
if (!mSymKey && !mPrivateKey && !mPublicKey)
return NS_ERROR_FAILURE;
return NS_OK;
}
NS_IMETHODIMP
nsKeyObject::GetKeyObj(void * *_retval)
{
if (mKeyType == 0)
return NS_ERROR_NOT_INITIALIZED;
switch (mKeyType) {
case nsIKeyObject::SYM_KEY:
*_retval = (void*)mSymKey;
break;
case nsIKeyObject::PRIVATE_KEY:
*_retval = (void*)mPublicKey;
break;
case nsIKeyObject::PUBLIC_KEY:
*_retval = (void*)mPrivateKey;
break;
default:
// unknown key type? How did that happen?
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
nsKeyObject::GetType(int16_t *_retval)
{
if (mKeyType == 0)
return NS_ERROR_NOT_INITIALIZED;
*_retval = mKeyType;
return NS_OK;
}
//////////////////////////////////////////////////////////////////////////////
// nsIKeyObjectFactory
NS_IMPL_ISUPPORTS(nsKeyObjectFactory, nsIKeyObjectFactory)
nsKeyObjectFactory::nsKeyObjectFactory()
{
}
NS_IMETHODIMP
nsKeyObjectFactory::LookupKeyByName(const nsACString & aName,
nsIKeyObject **_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsKeyObjectFactory::UnwrapKey(int16_t aAlgorithm, const uint8_t *aWrappedKey,
uint32_t aWrappedKeyLen, nsIKeyObject **_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsKeyObjectFactory::KeyFromString(int16_t aAlgorithm, const nsACString & aKey,
nsIKeyObject **_retval)
{
CK_MECHANISM_TYPE cipherMech;
CK_ATTRIBUTE_TYPE cipherOperation;
switch (aAlgorithm)
{
case nsIKeyObject::HMAC:
cipherMech = CKM_GENERIC_SECRET_KEY_GEN;
cipherOperation = CKA_SIGN;
break;
case nsIKeyObject::RC4:
cipherMech = CKM_RC4;
cipherOperation = CKA_ENCRYPT;
break;
default:
return NS_ERROR_INVALID_ARG;
}
nsresult rv;
nsCOMPtr<nsIKeyObject> key =
do_CreateInstance(NS_KEYMODULEOBJECT_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Convert the raw string into a SECItem
const nsCString& flatKey = PromiseFlatCString(aKey);
SECItem keyItem;
keyItem.data = (unsigned char*)flatKey.get();
keyItem.len = flatKey.Length();
ScopedPK11SlotInfo slot(PK11_GetBestSlot(cipherMech, nullptr));
if (!slot) {
NS_ERROR("no slot");
return NS_ERROR_FAILURE;
}
PK11SymKey* symKey = PK11_ImportSymKey(slot, cipherMech, PK11_OriginUnwrap,
cipherOperation, &keyItem, nullptr);
if (!symKey) {
return NS_ERROR_FAILURE;
}
rv = key->InitKey(aAlgorithm, (void*)symKey);
NS_ENSURE_SUCCESS(rv, rv);
key.swap(*_retval);
return NS_OK;
}