2014-11-27 01:28:28 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2001-05-03 05:00:56 +04:00
|
|
|
*
|
2012-05-31 13:33:35 +04:00
|
|
|
* 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/. */
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2017-03-17 18:31:40 +03:00
|
|
|
#include "nsKeygenHandler.h"
|
|
|
|
|
2016-08-13 16:45:00 +03:00
|
|
|
#include "cryptohi.h"
|
|
|
|
#include "keyhi.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
2017-03-17 18:31:40 +03:00
|
|
|
#include "mozilla/Base64.h"
|
|
|
|
#include "mozilla/Casting.h"
|
2018-01-30 07:28:00 +03:00
|
|
|
|
|
|
|
/* Disable the "base class should be explicitly initialized in the
|
|
|
|
copy constructor" warning that some bindings structs trigger while
|
|
|
|
including Element.h. Looks like it's an inherent part of -Wextra,
|
|
|
|
so we can't just disable it in a targeted way in moz.build. */
|
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wextra"
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wextra"
|
|
|
|
#endif // __clang__ || __GNUC__
|
|
|
|
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
|
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif // __clang__ || __GNUC__
|
|
|
|
|
2017-03-17 18:31:40 +03:00
|
|
|
#include "nsDependentString.h"
|
2016-08-13 16:45:00 +03:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIGenKeypairInfoDlg.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsITokenDialogs.h"
|
2014-11-27 01:28:28 +03:00
|
|
|
#include "nsKeygenHandlerContent.h"
|
2001-08-16 03:27:42 +04:00
|
|
|
#include "nsKeygenThread.h"
|
2017-12-07 06:36:57 +03:00
|
|
|
#include "nsMemory.h"
|
2016-08-13 16:45:00 +03:00
|
|
|
#include "nsNSSComponent.h" // for PIPNSS string bundle calls.
|
2015-10-27 02:02:19 +03:00
|
|
|
#include "nsNSSHelper.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2001-10-30 04:11:04 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
2014-11-27 01:28:28 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2016-08-13 16:45:00 +03:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "secasn1.h"
|
|
|
|
#include "secder.h"
|
|
|
|
#include "secdert.h"
|
2015-09-22 19:52:58 +03:00
|
|
|
|
2018-01-30 07:28:00 +03:00
|
|
|
using mozilla::dom::Element;
|
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
//These defines are taken from the PKCS#11 spec
|
|
|
|
#define CKM_RSA_PKCS_KEY_PAIR_GEN 0x00000000
|
|
|
|
#define CKM_DH_PKCS_KEY_PAIR_GEN 0x00000020
|
|
|
|
|
2007-11-19 20:02:43 +03:00
|
|
|
DERTemplate SECAlgorithmIDTemplate[] = {
|
|
|
|
{ DER_SEQUENCE,
|
2012-10-18 00:48:36 +04:00
|
|
|
0, nullptr, sizeof(SECAlgorithmID) },
|
2007-11-19 20:02:43 +03:00
|
|
|
{ DER_OBJECT_ID,
|
|
|
|
offsetof(SECAlgorithmID,algorithm), },
|
|
|
|
{ DER_OPTIONAL | DER_ANY,
|
|
|
|
offsetof(SECAlgorithmID,parameters), },
|
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
DERTemplate CERTSubjectPublicKeyInfoTemplate[] = {
|
|
|
|
{ DER_SEQUENCE,
|
2012-07-30 18:20:58 +04:00
|
|
|
0, nullptr, sizeof(CERTSubjectPublicKeyInfo) },
|
2001-05-03 05:00:56 +04:00
|
|
|
{ DER_INLINE,
|
|
|
|
offsetof(CERTSubjectPublicKeyInfo,algorithm),
|
|
|
|
SECAlgorithmIDTemplate, },
|
|
|
|
{ DER_BIT_STRING,
|
|
|
|
offsetof(CERTSubjectPublicKeyInfo,subjectPublicKey), },
|
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
|
|
|
|
DERTemplate CERTPublicKeyAndChallengeTemplate[] =
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
{ DER_SEQUENCE, 0, nullptr, sizeof(CERTPublicKeyAndChallenge) },
|
2001-05-03 05:00:56 +04:00
|
|
|
{ DER_ANY, offsetof(CERTPublicKeyAndChallenge,spki), },
|
|
|
|
{ DER_IA5_STRING, offsetof(CERTPublicKeyAndChallenge,challenge), },
|
|
|
|
{ 0, }
|
|
|
|
};
|
|
|
|
|
2006-09-29 04:50:46 +04:00
|
|
|
typedef struct curveNameTagPairStr {
|
2009-03-15 16:58:38 +03:00
|
|
|
const char *curveName;
|
2006-09-29 04:50:46 +04:00
|
|
|
SECOidTag curveOidTag;
|
|
|
|
} CurveNameTagPair;
|
|
|
|
|
|
|
|
static CurveNameTagPair nameTagPair[] =
|
2017-03-14 13:54:57 +03:00
|
|
|
{
|
2006-09-29 04:50:46 +04:00
|
|
|
{ "prime192v1", SEC_OID_ANSIX962_EC_PRIME192V1 },
|
|
|
|
{ "prime192v2", SEC_OID_ANSIX962_EC_PRIME192V2 },
|
|
|
|
{ "prime192v3", SEC_OID_ANSIX962_EC_PRIME192V3 },
|
|
|
|
{ "prime239v1", SEC_OID_ANSIX962_EC_PRIME239V1 },
|
|
|
|
{ "prime239v2", SEC_OID_ANSIX962_EC_PRIME239V2 },
|
|
|
|
{ "prime239v3", SEC_OID_ANSIX962_EC_PRIME239V3 },
|
|
|
|
{ "prime256v1", SEC_OID_ANSIX962_EC_PRIME256V1 },
|
|
|
|
|
|
|
|
{ "secp112r1", SEC_OID_SECG_EC_SECP112R1},
|
|
|
|
{ "secp112r2", SEC_OID_SECG_EC_SECP112R2},
|
|
|
|
{ "secp128r1", SEC_OID_SECG_EC_SECP128R1},
|
|
|
|
{ "secp128r2", SEC_OID_SECG_EC_SECP128R2},
|
|
|
|
{ "secp160k1", SEC_OID_SECG_EC_SECP160K1},
|
|
|
|
{ "secp160r1", SEC_OID_SECG_EC_SECP160R1},
|
|
|
|
{ "secp160r2", SEC_OID_SECG_EC_SECP160R2},
|
|
|
|
{ "secp192k1", SEC_OID_SECG_EC_SECP192K1},
|
|
|
|
{ "secp192r1", SEC_OID_ANSIX962_EC_PRIME192V1 },
|
|
|
|
{ "nistp192", SEC_OID_ANSIX962_EC_PRIME192V1 },
|
|
|
|
{ "secp224k1", SEC_OID_SECG_EC_SECP224K1},
|
|
|
|
{ "secp224r1", SEC_OID_SECG_EC_SECP224R1},
|
|
|
|
{ "nistp224", SEC_OID_SECG_EC_SECP224R1},
|
|
|
|
{ "secp256k1", SEC_OID_SECG_EC_SECP256K1},
|
|
|
|
{ "secp256r1", SEC_OID_ANSIX962_EC_PRIME256V1 },
|
|
|
|
{ "nistp256", SEC_OID_ANSIX962_EC_PRIME256V1 },
|
|
|
|
{ "secp384r1", SEC_OID_SECG_EC_SECP384R1},
|
|
|
|
{ "nistp384", SEC_OID_SECG_EC_SECP384R1},
|
|
|
|
{ "secp521r1", SEC_OID_SECG_EC_SECP521R1},
|
|
|
|
{ "nistp521", SEC_OID_SECG_EC_SECP521R1},
|
|
|
|
|
|
|
|
{ "c2pnb163v1", SEC_OID_ANSIX962_EC_C2PNB163V1 },
|
|
|
|
{ "c2pnb163v2", SEC_OID_ANSIX962_EC_C2PNB163V2 },
|
|
|
|
{ "c2pnb163v3", SEC_OID_ANSIX962_EC_C2PNB163V3 },
|
|
|
|
{ "c2pnb176v1", SEC_OID_ANSIX962_EC_C2PNB176V1 },
|
|
|
|
{ "c2tnb191v1", SEC_OID_ANSIX962_EC_C2TNB191V1 },
|
|
|
|
{ "c2tnb191v2", SEC_OID_ANSIX962_EC_C2TNB191V2 },
|
|
|
|
{ "c2tnb191v3", SEC_OID_ANSIX962_EC_C2TNB191V3 },
|
|
|
|
{ "c2onb191v4", SEC_OID_ANSIX962_EC_C2ONB191V4 },
|
|
|
|
{ "c2onb191v5", SEC_OID_ANSIX962_EC_C2ONB191V5 },
|
|
|
|
{ "c2pnb208w1", SEC_OID_ANSIX962_EC_C2PNB208W1 },
|
|
|
|
{ "c2tnb239v1", SEC_OID_ANSIX962_EC_C2TNB239V1 },
|
|
|
|
{ "c2tnb239v2", SEC_OID_ANSIX962_EC_C2TNB239V2 },
|
|
|
|
{ "c2tnb239v3", SEC_OID_ANSIX962_EC_C2TNB239V3 },
|
|
|
|
{ "c2onb239v4", SEC_OID_ANSIX962_EC_C2ONB239V4 },
|
|
|
|
{ "c2onb239v5", SEC_OID_ANSIX962_EC_C2ONB239V5 },
|
|
|
|
{ "c2pnb272w1", SEC_OID_ANSIX962_EC_C2PNB272W1 },
|
|
|
|
{ "c2pnb304w1", SEC_OID_ANSIX962_EC_C2PNB304W1 },
|
|
|
|
{ "c2tnb359v1", SEC_OID_ANSIX962_EC_C2TNB359V1 },
|
|
|
|
{ "c2pnb368w1", SEC_OID_ANSIX962_EC_C2PNB368W1 },
|
|
|
|
{ "c2tnb431r1", SEC_OID_ANSIX962_EC_C2TNB431R1 },
|
|
|
|
|
|
|
|
{ "sect113r1", SEC_OID_SECG_EC_SECT113R1},
|
|
|
|
{ "sect113r2", SEC_OID_SECG_EC_SECT113R2},
|
|
|
|
{ "sect131r1", SEC_OID_SECG_EC_SECT131R1},
|
|
|
|
{ "sect131r2", SEC_OID_SECG_EC_SECT131R2},
|
|
|
|
{ "sect163k1", SEC_OID_SECG_EC_SECT163K1},
|
|
|
|
{ "nistk163", SEC_OID_SECG_EC_SECT163K1},
|
|
|
|
{ "sect163r1", SEC_OID_SECG_EC_SECT163R1},
|
|
|
|
{ "sect163r2", SEC_OID_SECG_EC_SECT163R2},
|
|
|
|
{ "nistb163", SEC_OID_SECG_EC_SECT163R2},
|
|
|
|
{ "sect193r1", SEC_OID_SECG_EC_SECT193R1},
|
|
|
|
{ "sect193r2", SEC_OID_SECG_EC_SECT193R2},
|
|
|
|
{ "sect233k1", SEC_OID_SECG_EC_SECT233K1},
|
|
|
|
{ "nistk233", SEC_OID_SECG_EC_SECT233K1},
|
|
|
|
{ "sect233r1", SEC_OID_SECG_EC_SECT233R1},
|
|
|
|
{ "nistb233", SEC_OID_SECG_EC_SECT233R1},
|
|
|
|
{ "sect239k1", SEC_OID_SECG_EC_SECT239K1},
|
|
|
|
{ "sect283k1", SEC_OID_SECG_EC_SECT283K1},
|
|
|
|
{ "nistk283", SEC_OID_SECG_EC_SECT283K1},
|
|
|
|
{ "sect283r1", SEC_OID_SECG_EC_SECT283R1},
|
|
|
|
{ "nistb283", SEC_OID_SECG_EC_SECT283R1},
|
|
|
|
{ "sect409k1", SEC_OID_SECG_EC_SECT409K1},
|
|
|
|
{ "nistk409", SEC_OID_SECG_EC_SECT409K1},
|
|
|
|
{ "sect409r1", SEC_OID_SECG_EC_SECT409R1},
|
|
|
|
{ "nistb409", SEC_OID_SECG_EC_SECT409R1},
|
|
|
|
{ "sect571k1", SEC_OID_SECG_EC_SECT571K1},
|
|
|
|
{ "nistk571", SEC_OID_SECG_EC_SECT571K1},
|
|
|
|
{ "sect571r1", SEC_OID_SECG_EC_SECT571R1},
|
|
|
|
{ "nistb571", SEC_OID_SECG_EC_SECT571R1},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2016-08-13 16:45:00 +03:00
|
|
|
mozilla::UniqueSECItem
|
|
|
|
DecodeECParams(const char* curve)
|
2006-09-29 04:50:46 +04:00
|
|
|
{
|
2012-10-18 00:48:36 +04:00
|
|
|
SECOidData *oidData = nullptr;
|
2006-09-29 04:50:46 +04:00
|
|
|
SECOidTag curveOidTag = SEC_OID_UNKNOWN; /* default */
|
|
|
|
int i, numCurves;
|
|
|
|
|
|
|
|
if (curve && *curve) {
|
|
|
|
numCurves = sizeof(nameTagPair)/sizeof(CurveNameTagPair);
|
2017-03-14 13:54:57 +03:00
|
|
|
for (i = 0; ((i < numCurves) && (curveOidTag == SEC_OID_UNKNOWN));
|
2006-09-29 04:50:46 +04:00
|
|
|
i++) {
|
|
|
|
if (PL_strcmp(curve, nameTagPair[i].curveName) == 0)
|
|
|
|
curveOidTag = nameTagPair[i].curveOidTag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 00:48:36 +04:00
|
|
|
/* Return nullptr if curve name is not recognized */
|
2017-03-14 13:54:57 +03:00
|
|
|
if ((curveOidTag == SEC_OID_UNKNOWN) ||
|
2012-10-18 00:48:36 +04:00
|
|
|
(oidData = SECOID_FindOIDByTag(curveOidTag)) == nullptr) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-09-29 04:50:46 +04:00
|
|
|
}
|
|
|
|
|
2016-08-13 16:45:00 +03:00
|
|
|
mozilla::UniqueSECItem ecparams(SECITEM_AllocItem(nullptr, nullptr,
|
|
|
|
2 + oidData->oid.len));
|
|
|
|
if (!ecparams) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2006-09-29 04:50:46 +04:00
|
|
|
|
2017-03-14 13:54:57 +03:00
|
|
|
/*
|
2006-09-29 04:50:46 +04:00
|
|
|
* ecparams->data needs to contain the ASN encoding of an object ID (OID)
|
2017-03-14 13:54:57 +03:00
|
|
|
* representing the named curve. The actual OID is in
|
2006-09-29 04:50:46 +04:00
|
|
|
* oidData->oid.data so we simply prepend 0x06 and OID length
|
|
|
|
*/
|
|
|
|
ecparams->data[0] = SEC_ASN1_OBJECT_ID;
|
|
|
|
ecparams->data[1] = oidData->oid.len;
|
|
|
|
memcpy(ecparams->data + 2, oidData->oid.data, oidData->oid.len);
|
|
|
|
|
|
|
|
return ecparams;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsKeygenFormProcessor, nsIFormProcessor)
|
2001-05-03 05:00:56 +04:00
|
|
|
|
|
|
|
nsKeygenFormProcessor::nsKeygenFormProcessor()
|
2017-03-14 13:54:57 +03:00
|
|
|
{
|
2001-05-03 05:00:56 +04:00
|
|
|
m_ctx = new PipUIContext();
|
2017-03-14 13:54:57 +03:00
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2010-06-10 22:11:40 +04:00
|
|
|
nsresult
|
2001-05-03 05:00:56 +04:00
|
|
|
nsKeygenFormProcessor::Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult)
|
|
|
|
{
|
2014-11-27 01:28:28 +03:00
|
|
|
if (GeckoProcessType_Content == XRE_GetProcessType()) {
|
|
|
|
nsCOMPtr<nsISupports> contentProcessor = new nsKeygenFormProcessorContent();
|
|
|
|
return contentProcessor->QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
nsresult rv;
|
|
|
|
NS_ENSURE_NO_AGGREGATION(aOuter);
|
|
|
|
nsKeygenFormProcessor* formProc = new nsKeygenFormProcessor();
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> stabilize = formProc;
|
|
|
|
rv = formProc->Init();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = formProc->QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsKeygenFormProcessor::Init()
|
|
|
|
{
|
2013-11-26 22:18:21 +04:00
|
|
|
static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);
|
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2001-08-10 23:55:32 +04:00
|
|
|
nsCOMPtr<nsINSSComponent> nssComponent;
|
|
|
|
nssComponent = do_GetService(kNSSComponentCID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2007-11-13 03:31:23 +03:00
|
|
|
// Init possible key size choices.
|
|
|
|
nssComponent->GetPIPNSSBundleString("HighGrade", mSECKeySizeChoiceList[0].name);
|
|
|
|
mSECKeySizeChoiceList[0].size = 2048;
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2007-11-13 03:31:23 +03:00
|
|
|
nssComponent->GetPIPNSSBundleString("MediumGrade", mSECKeySizeChoiceList[1].name);
|
|
|
|
mSECKeySizeChoiceList[1].size = 1024;
|
2001-05-03 05:00:56 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsKeygenFormProcessor::GetSlot(uint32_t aMechanism, PK11SlotInfo** aSlot)
|
2001-05-16 03:15:12 +04:00
|
|
|
{
|
2018-01-23 21:37:47 +03:00
|
|
|
return GetSlotWithMechanism(aMechanism, m_ctx, aSlot);
|
bug 1215690 - remove nsPSMUITracker r=Cykesiopka r=mgoodwin
nsPSMUITracker was problematic. Apparently it was originally intended to prevent
NSS shutdown while NSS-related UI operations were going on (such as choosing a
client certificate). However, when nsNSSComponent would receive the event that
told it to shutdown NSS, it would attempt to call
mShutdownObjectList->evaporateAllNSSResources(), which would call
mActivityState.restrictActivityToCurrentThread(), which failed if such a UI
operation was in progress. This actually prevented the important part of
evaporateAllNSSResources, which is the releasing of all NSS objects in use by
PSM objects. Importantly, nsNSSComponent didn't check for or handle this failure
and proceeded to call NSS_Shutdown(), leaving PSM in an inconsistent state where
it thought it was okay to keep using the NSS objects it had when in fact it
wasn't.
In any case, nsPSMUITracker isn't really necessary as long as we have the
nsNSSShutDownPreventionLock mechanism, which mostly works and is what we should
use instead (or not at all, if no such lock is needed for the operation being
performed (for example, if no NSS functions are being called)).
2015-10-17 00:31:57 +03:00
|
|
|
}
|
2001-08-21 05:12:38 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t MapGenMechToAlgoMech(uint32_t mechanism)
|
2001-08-21 05:12:38 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t searchMech;
|
2001-08-21 05:12:38 +04:00
|
|
|
|
|
|
|
/* We are interested in slots based on the ability to perform
|
|
|
|
a given algorithm, not on their ability to generate keys usable
|
|
|
|
by that algorithm. Therefore, map keygen-specific mechanism tags
|
2015-10-19 23:54:00 +03:00
|
|
|
to tags for the corresponding crypto algorithm. */
|
2001-08-21 05:12:38 +04:00
|
|
|
switch(mechanism)
|
|
|
|
{
|
|
|
|
case CKM_RSA_PKCS_KEY_PAIR_GEN:
|
|
|
|
searchMech = CKM_RSA_PKCS;
|
|
|
|
break;
|
|
|
|
case CKM_RC4_KEY_GEN:
|
|
|
|
searchMech = CKM_RC4;
|
|
|
|
break;
|
|
|
|
case CKM_DH_PKCS_KEY_PAIR_GEN:
|
|
|
|
searchMech = CKM_DH_PKCS_DERIVE; /* ### mwelch is this right? */
|
|
|
|
break;
|
|
|
|
case CKM_DES_KEY_GEN:
|
|
|
|
/* What do we do about DES keygen? Right now, we're just using
|
|
|
|
DES_KEY_GEN to look for tokens, because otherwise we'll have
|
|
|
|
to search the token list three times. */
|
2006-09-29 04:50:46 +04:00
|
|
|
case CKM_EC_KEY_PAIR_GEN:
|
|
|
|
/* The default should also work for EC key pair generation. */
|
2001-08-21 05:12:38 +04:00
|
|
|
default:
|
|
|
|
searchMech = mechanism;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return searchMech;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-16 03:15:12 +04:00
|
|
|
nsresult
|
bug 1215690 - remove nsPSMUITracker r=Cykesiopka r=mgoodwin
nsPSMUITracker was problematic. Apparently it was originally intended to prevent
NSS shutdown while NSS-related UI operations were going on (such as choosing a
client certificate). However, when nsNSSComponent would receive the event that
told it to shutdown NSS, it would attempt to call
mShutdownObjectList->evaporateAllNSSResources(), which would call
mActivityState.restrictActivityToCurrentThread(), which failed if such a UI
operation was in progress. This actually prevented the important part of
evaporateAllNSSResources, which is the releasing of all NSS objects in use by
PSM objects. Importantly, nsNSSComponent didn't check for or handle this failure
and proceeded to call NSS_Shutdown(), leaving PSM in an inconsistent state where
it thought it was okay to keep using the NSS objects it had when in fact it
wasn't.
In any case, nsPSMUITracker isn't really necessary as long as we have the
nsNSSShutDownPreventionLock mechanism, which mostly works and is what we should
use instead (or not at all, if no such lock is needed for the operation being
performed (for example, if no NSS functions are being called)).
2015-10-17 00:31:57 +03:00
|
|
|
GetSlotWithMechanism(uint32_t aMechanism, nsIInterfaceRequestor* m_ctx,
|
2018-01-23 21:37:47 +03:00
|
|
|
PK11SlotInfo** aSlot)
|
2001-05-03 05:00:56 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
PK11SlotList * slotList = nullptr;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t** tokenNameList = nullptr;
|
2016-04-28 04:16:48 +03:00
|
|
|
nsCOMPtr<nsITokenDialogs> dialogs;
|
2017-02-26 15:36:40 +03:00
|
|
|
nsAutoString tokenStr;
|
2001-05-03 05:00:56 +04:00
|
|
|
PK11SlotListElement *slotElement, *tmpSlot;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numSlots = 0, i = 0;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool canceled;
|
2001-05-03 05:00:56 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aSlot = nullptr;
|
2001-05-03 05:00:56 +04:00
|
|
|
|
|
|
|
// Get the slot
|
2017-03-14 13:54:57 +03:00
|
|
|
slotList = PK11_GetAllTokens(MapGenMechToAlgoMech(aMechanism),
|
2011-10-17 18:59:28 +04:00
|
|
|
true, true, m_ctx);
|
2001-05-03 05:00:56 +04:00
|
|
|
if (!slotList || !slotList->head) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!slotList->head->next) {
|
|
|
|
/* only one slot available, just return it */
|
|
|
|
*aSlot = slotList->head->slot;
|
|
|
|
} else {
|
|
|
|
// Gerenate a list of slots and ask the user to choose //
|
|
|
|
tmpSlot = slotList->head;
|
|
|
|
while (tmpSlot) {
|
|
|
|
numSlots++;
|
|
|
|
tmpSlot = tmpSlot->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the slot name buffer //
|
2015-03-27 03:01:12 +03:00
|
|
|
tokenNameList = static_cast<char16_t**>(moz_xmalloc(sizeof(char16_t *) * numSlots));
|
2005-07-13 23:31:14 +04:00
|
|
|
if (!tokenNameList) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
i = 0;
|
|
|
|
slotElement = PK11_GetFirstSafe(slotList);
|
|
|
|
while (slotElement) {
|
2005-07-13 23:31:14 +04:00
|
|
|
tokenNameList[i] = UTF8ToNewUnicode(nsDependentCString(PK11_GetTokenName(slotElement->slot)));
|
2011-10-17 18:59:28 +04:00
|
|
|
slotElement = PK11_GetNextSafe(slotList, slotElement, false);
|
2005-07-13 23:31:14 +04:00
|
|
|
if (tokenNameList[i])
|
|
|
|
i++;
|
|
|
|
else {
|
2017-03-14 13:54:57 +03:00
|
|
|
// OOM. adjust numSlots so we don't free unallocated memory.
|
2005-07-13 23:31:14 +04:00
|
|
|
numSlots = i;
|
2010-03-12 09:50:11 +03:00
|
|
|
PK11_FreeSlotListElement(slotList, slotElement);
|
2005-07-13 23:31:14 +04:00
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto loser;
|
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
}
|
|
|
|
|
2016-04-28 04:16:48 +03:00
|
|
|
// Throw up the token list dialog and get back the token.
|
|
|
|
rv = getNSSDialogs(getter_AddRefs(dialogs), NS_GET_IID(nsITokenDialogs),
|
|
|
|
NS_TOKENDIALOGS_CONTRACTID);
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2016-04-28 04:16:48 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
|
bug 1215690 - remove nsPSMUITracker r=Cykesiopka r=mgoodwin
nsPSMUITracker was problematic. Apparently it was originally intended to prevent
NSS shutdown while NSS-related UI operations were going on (such as choosing a
client certificate). However, when nsNSSComponent would receive the event that
told it to shutdown NSS, it would attempt to call
mShutdownObjectList->evaporateAllNSSResources(), which would call
mActivityState.restrictActivityToCurrentThread(), which failed if such a UI
operation was in progress. This actually prevented the important part of
evaporateAllNSSResources, which is the releasing of all NSS objects in use by
PSM objects. Importantly, nsNSSComponent didn't check for or handle this failure
and proceeded to call NSS_Shutdown(), leaving PSM in an inconsistent state where
it thought it was okay to keep using the NSS objects it had when in fact it
wasn't.
In any case, nsPSMUITracker isn't really necessary as long as we have the
nsNSSShutDownPreventionLock mechanism, which mostly works and is what we should
use instead (or not at all, if no such lock is needed for the operation being
performed (for example, if no NSS functions are being called)).
2015-10-17 00:31:57 +03:00
|
|
|
if (!tokenNameList || !*tokenNameList) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
} else {
|
|
|
|
rv = dialogs->ChooseToken(m_ctx, (const char16_t**)tokenNameList,
|
2017-02-26 15:36:40 +03:00
|
|
|
numSlots, tokenStr, &canceled);
|
2003-01-07 01:23:49 +03:00
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
if (NS_FAILED(rv)) goto loser;
|
|
|
|
|
|
|
|
if (canceled) { rv = NS_ERROR_NOT_AVAILABLE; goto loser; }
|
|
|
|
|
|
|
|
// Get the slot //
|
|
|
|
slotElement = PK11_GetFirstSafe(slotList);
|
|
|
|
while (slotElement) {
|
2006-02-03 17:18:39 +03:00
|
|
|
if (tokenStr.Equals(NS_ConvertUTF8toUTF16(PK11_GetTokenName(slotElement->slot)))) {
|
2001-05-03 05:00:56 +04:00
|
|
|
*aSlot = slotElement->slot;
|
2010-03-12 09:50:11 +03:00
|
|
|
PK11_FreeSlotListElement(slotList, slotElement);
|
2001-05-03 05:00:56 +04:00
|
|
|
break;
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
slotElement = PK11_GetNextSafe(slotList, slotElement, false);
|
2001-05-03 05:00:56 +04:00
|
|
|
}
|
|
|
|
if(!(*aSlot)) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a reference to the slot //
|
|
|
|
PK11_ReferenceSlot(*aSlot);
|
|
|
|
loser:
|
|
|
|
if (slotList) {
|
|
|
|
PK11_FreeSlotList(slotList);
|
|
|
|
}
|
|
|
|
if (tokenNameList) {
|
2005-07-13 23:31:14 +04:00
|
|
|
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(numSlots, tokenNameList);
|
2001-05-03 05:00:56 +04:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-11-27 01:28:28 +03:00
|
|
|
nsKeygenFormProcessor::GetPublicKey(const nsAString& aValue,
|
|
|
|
const nsAString& aChallenge,
|
2017-06-20 12:19:05 +03:00
|
|
|
const nsString& aKeyType,
|
2014-11-27 01:28:28 +03:00
|
|
|
nsAString& aOutPublicKey,
|
|
|
|
const nsAString& aKeyParams)
|
2001-05-03 05:00:56 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2017-03-17 18:31:40 +03:00
|
|
|
nsAutoCString keystring;
|
2015-10-19 23:54:00 +03:00
|
|
|
char *keyparamsString = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t keyGenMechanism;
|
2012-07-30 18:20:58 +04:00
|
|
|
PK11SlotInfo *slot = nullptr;
|
2001-05-03 05:00:56 +04:00
|
|
|
PK11RSAGenParams rsaParams;
|
2016-08-13 16:45:00 +03:00
|
|
|
mozilla::UniqueSECItem ecParams;
|
2001-05-03 05:00:56 +04:00
|
|
|
SECOidTag algTag;
|
|
|
|
int keysize = 0;
|
2016-08-13 16:45:00 +03:00
|
|
|
void *params = nullptr; // Non-owning.
|
2012-07-30 18:20:58 +04:00
|
|
|
SECKEYPrivateKey *privateKey = nullptr;
|
|
|
|
SECKEYPublicKey *publicKey = nullptr;
|
|
|
|
CERTSubjectPublicKeyInfo *spkInfo = nullptr;
|
2016-04-05 03:35:24 +03:00
|
|
|
SECStatus srv = SECFailure;
|
2001-05-03 05:00:56 +04:00
|
|
|
SECItem spkiItem;
|
|
|
|
SECItem pkacItem;
|
|
|
|
SECItem signedItem;
|
2017-03-17 18:31:40 +03:00
|
|
|
nsDependentCSubstring signedItemStr;
|
2001-05-03 05:00:56 +04:00
|
|
|
CERTPublicKeyAndChallenge pkac;
|
2012-07-30 18:20:58 +04:00
|
|
|
pkac.challenge.data = nullptr;
|
2016-04-28 04:16:48 +03:00
|
|
|
nsCOMPtr<nsIGeneratingKeypairInfoDialogs> dialogs;
|
2001-10-23 10:11:57 +04:00
|
|
|
nsKeygenThread *KeygenRunnable = 0;
|
2001-08-16 03:27:42 +04:00
|
|
|
nsCOMPtr<nsIKeygenThread> runnable;
|
2015-10-19 23:54:00 +03:00
|
|
|
|
2012-03-22 02:22:15 +04:00
|
|
|
// permanent and sensitive flags for keygen
|
|
|
|
PK11AttrFlags attrFlags = PK11_ATTR_TOKEN | PK11_ATTR_SENSITIVE | PK11_ATTR_PRIVATE;
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2016-04-05 03:35:24 +03:00
|
|
|
UniquePLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
// Get the key size //
|
2007-11-13 03:31:23 +03:00
|
|
|
for (size_t i = 0; i < number_of_key_size_choices; ++i) {
|
|
|
|
if (aValue.Equals(mSECKeySizeChoiceList[i].name)) {
|
|
|
|
keysize = mSECKeySizeChoiceList[i].size;
|
2001-05-03 05:00:56 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-07-20 23:31:22 +04:00
|
|
|
if (!keysize) {
|
2001-05-03 05:00:56 +04:00
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the keygen mechanism
|
2004-06-17 04:13:25 +04:00
|
|
|
if (aKeyType.IsEmpty() || aKeyType.LowerCaseEqualsLiteral("rsa")) {
|
2001-05-03 05:00:56 +04:00
|
|
|
keyGenMechanism = CKM_RSA_PKCS_KEY_PAIR_GEN;
|
2006-09-29 04:50:46 +04:00
|
|
|
} else if (aKeyType.LowerCaseEqualsLiteral("ec")) {
|
|
|
|
keyparamsString = ToNewCString(aKeyParams);
|
|
|
|
if (!keyparamsString) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyGenMechanism = CKM_EC_KEY_PAIR_GEN;
|
|
|
|
/* ecParams are initialized later */
|
2001-05-03 05:00:56 +04:00
|
|
|
} else {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the slot
|
|
|
|
rv = GetSlot(keyGenMechanism, &slot);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2011-03-28 08:03:11 +04:00
|
|
|
switch (keyGenMechanism) {
|
2001-05-03 05:00:56 +04:00
|
|
|
case CKM_RSA_PKCS_KEY_PAIR_GEN:
|
|
|
|
rsaParams.keySizeInBits = keysize;
|
2001-05-16 03:15:12 +04:00
|
|
|
rsaParams.pe = DEFAULT_RSA_KEYGEN_PE;
|
|
|
|
algTag = DEFAULT_RSA_KEYGEN_ALG;
|
2001-05-03 05:00:56 +04:00
|
|
|
params = &rsaParams;
|
|
|
|
break;
|
2006-09-29 04:50:46 +04:00
|
|
|
case CKM_EC_KEY_PAIR_GEN:
|
2017-03-14 13:54:57 +03:00
|
|
|
/* XXX We ought to rethink how the KEYGEN tag is
|
2006-09-29 04:50:46 +04:00
|
|
|
* displayed. The pulldown selections presented
|
|
|
|
* to the user must depend on the keytype.
|
|
|
|
* The displayed selection could be picked
|
|
|
|
* from the keyparams attribute (this is currently called
|
|
|
|
* the pqg attribute).
|
|
|
|
* For now, we pick ecparams from the keyparams field
|
2017-03-14 13:54:57 +03:00
|
|
|
* if it specifies a valid supported curve, or else
|
2006-09-29 04:50:46 +04:00
|
|
|
* we pick one of secp384r1, secp256r1 or secp192r1
|
|
|
|
* respectively depending on the user's selection
|
2017-03-14 13:54:57 +03:00
|
|
|
* (High, Medium, Low).
|
2006-09-29 04:50:46 +04:00
|
|
|
* (RSA uses RSA-2048, RSA-1024 and RSA-512 for historical
|
|
|
|
* reasons, while ECC choices represent a stronger mapping)
|
|
|
|
* NOTE: The user's selection
|
|
|
|
* is silently ignored when a valid curve is presented
|
|
|
|
* in keyparams.
|
|
|
|
*/
|
2016-08-13 16:45:00 +03:00
|
|
|
ecParams = DecodeECParams(keyparamsString);
|
|
|
|
if (!ecParams) {
|
2006-09-29 04:50:46 +04:00
|
|
|
/* The keyparams attribute did not specify a valid
|
|
|
|
* curve name so use a curve based on the keysize.
|
|
|
|
* NOTE: Here keysize is used only as an indication of
|
|
|
|
* High/Medium/Low strength; elliptic curve
|
|
|
|
* cryptography uses smaller keys than RSA to provide
|
|
|
|
* equivalent security.
|
|
|
|
*/
|
|
|
|
switch (keysize) {
|
|
|
|
case 2048:
|
2016-08-13 16:45:00 +03:00
|
|
|
ecParams = DecodeECParams("secp384r1");
|
2006-09-29 04:50:46 +04:00
|
|
|
break;
|
|
|
|
case 1024:
|
|
|
|
case 512:
|
2016-08-13 16:45:00 +03:00
|
|
|
ecParams = DecodeECParams("secp256r1");
|
2006-09-29 04:50:46 +04:00
|
|
|
break;
|
2017-03-14 13:54:57 +03:00
|
|
|
}
|
2006-09-29 04:50:46 +04:00
|
|
|
}
|
2016-08-13 16:45:00 +03:00
|
|
|
MOZ_ASSERT(ecParams);
|
|
|
|
params = ecParams.get();
|
2006-09-29 04:50:46 +04:00
|
|
|
/* XXX The signature algorithm ought to choose the hashing
|
|
|
|
* algorithm based on key size once ECDSA variations based
|
|
|
|
* on SHA256 SHA384 and SHA512 are standardized.
|
|
|
|
*/
|
|
|
|
algTag = SEC_OID_ANSIX962_ECDSA_SIGNATURE_WITH_SHA1_DIGEST;
|
|
|
|
break;
|
2001-05-03 05:00:56 +04:00
|
|
|
default:
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
2001-05-03 05:22:24 +04:00
|
|
|
/* Make sure token is initialized. */
|
2018-01-23 21:37:47 +03:00
|
|
|
rv = setPassword(slot, m_ctx);
|
2001-05-03 05:22:24 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-03-28 08:03:11 +04:00
|
|
|
goto loser;
|
2001-05-03 05:22:24 +04:00
|
|
|
|
2016-04-05 03:35:24 +03:00
|
|
|
srv = PK11_Authenticate(slot, true, m_ctx);
|
|
|
|
if (srv != SECSuccess) {
|
2001-05-03 05:00:56 +04:00
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:16:48 +03:00
|
|
|
rv = getNSSDialogs(getter_AddRefs(dialogs),
|
2002-09-17 22:51:22 +04:00
|
|
|
NS_GET_IID(nsIGeneratingKeypairInfoDialogs),
|
|
|
|
NS_GENERATINGKEYPAIRINFODIALOGS_CONTRACTID);
|
2001-08-16 03:27:42 +04:00
|
|
|
|
2001-10-23 10:11:57 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
KeygenRunnable = new nsKeygenThread();
|
2011-03-28 08:03:11 +04:00
|
|
|
NS_IF_ADDREF(KeygenRunnable);
|
2001-10-23 10:11:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || !KeygenRunnable) {
|
|
|
|
rv = NS_OK;
|
2012-03-22 02:22:15 +04:00
|
|
|
privateKey = PK11_GenerateKeyPairWithFlags(slot, keyGenMechanism, params,
|
|
|
|
&publicKey, attrFlags, m_ctx);
|
2001-08-16 03:27:42 +04:00
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
KeygenRunnable->SetParams( slot, attrFlags, nullptr, 0,
|
2012-03-22 02:22:15 +04:00
|
|
|
keyGenMechanism, params, m_ctx );
|
2001-10-23 10:11:57 +04:00
|
|
|
|
|
|
|
runnable = do_QueryInterface(KeygenRunnable);
|
2001-08-16 03:27:42 +04:00
|
|
|
if (runnable) {
|
bug 1215690 - remove nsPSMUITracker r=Cykesiopka r=mgoodwin
nsPSMUITracker was problematic. Apparently it was originally intended to prevent
NSS shutdown while NSS-related UI operations were going on (such as choosing a
client certificate). However, when nsNSSComponent would receive the event that
told it to shutdown NSS, it would attempt to call
mShutdownObjectList->evaporateAllNSSResources(), which would call
mActivityState.restrictActivityToCurrentThread(), which failed if such a UI
operation was in progress. This actually prevented the important part of
evaporateAllNSSResources, which is the releasing of all NSS objects in use by
PSM objects. Importantly, nsNSSComponent didn't check for or handle this failure
and proceeded to call NSS_Shutdown(), leaving PSM in an inconsistent state where
it thought it was okay to keep using the NSS objects it had when in fact it
wasn't.
In any case, nsPSMUITracker isn't really necessary as long as we have the
nsNSSShutDownPreventionLock mechanism, which mostly works and is what we should
use instead (or not at all, if no such lock is needed for the operation being
performed (for example, if no NSS functions are being called)).
2015-10-17 00:31:57 +03:00
|
|
|
rv = dialogs->DisplayGeneratingKeypairInfo(m_ctx, runnable);
|
|
|
|
// We call join on the thread so we can be sure that no
|
|
|
|
// simultaneous access to the passed parameters will happen.
|
|
|
|
KeygenRunnable->Join();
|
2001-08-16 03:27:42 +04:00
|
|
|
|
2001-12-13 04:17:49 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
PK11SlotInfo *used_slot = nullptr;
|
2012-03-22 02:22:15 +04:00
|
|
|
rv = KeygenRunnable->ConsumeResult(&used_slot, &privateKey, &publicKey);
|
|
|
|
if (NS_SUCCEEDED(rv) && used_slot) {
|
|
|
|
PK11_FreeSlot(used_slot);
|
|
|
|
}
|
2001-08-16 03:27:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 13:54:57 +03:00
|
|
|
|
2001-10-23 10:11:57 +04:00
|
|
|
if (NS_FAILED(rv) || !privateKey) {
|
2001-05-03 05:00:56 +04:00
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
// just in case we'll need to authenticate to the db -jp //
|
|
|
|
privateKey->wincx = m_ctx;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a subject public key info from the public key.
|
|
|
|
*/
|
|
|
|
spkInfo = SECKEY_CreateSubjectPublicKeyInfo(publicKey);
|
|
|
|
if ( !spkInfo ) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2016-04-05 03:35:24 +03:00
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
/*
|
|
|
|
* Now DER encode the whole subjectPublicKeyInfo.
|
|
|
|
*/
|
2016-04-05 03:35:24 +03:00
|
|
|
srv = DER_Encode(arena.get(), &spkiItem, CERTSubjectPublicKeyInfoTemplate,
|
|
|
|
spkInfo);
|
|
|
|
if (srv != SECSuccess) {
|
2001-05-03 05:00:56 +04:00
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up the PublicKeyAndChallenge data structure, then DER encode it
|
|
|
|
*/
|
|
|
|
pkac.spki = spkiItem;
|
2005-07-13 23:31:14 +04:00
|
|
|
pkac.challenge.len = aChallenge.Length();
|
2001-09-29 12:28:41 +04:00
|
|
|
pkac.challenge.data = (unsigned char *)ToNewCString(aChallenge);
|
2005-07-13 23:31:14 +04:00
|
|
|
if (!pkac.challenge.data) {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto loser;
|
|
|
|
}
|
2016-04-05 03:35:24 +03:00
|
|
|
|
|
|
|
srv = DER_Encode(arena.get(), &pkacItem, CERTPublicKeyAndChallengeTemplate,
|
|
|
|
&pkac);
|
|
|
|
if (srv != SECSuccess) {
|
2001-05-03 05:00:56 +04:00
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now sign the DER encoded PublicKeyAndChallenge
|
|
|
|
*/
|
2016-04-05 03:35:24 +03:00
|
|
|
srv = SEC_DerSignData(arena.get(), &signedItem, pkacItem.data, pkacItem.len,
|
|
|
|
privateKey, algTag);
|
|
|
|
if (srv != SECSuccess) {
|
2001-05-03 05:00:56 +04:00
|
|
|
goto loser;
|
|
|
|
}
|
2016-04-05 03:35:24 +03:00
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
/*
|
|
|
|
* Convert the signed public key and challenge into base64/ascii.
|
|
|
|
*/
|
2017-03-17 18:31:40 +03:00
|
|
|
signedItemStr.Assign(
|
|
|
|
mozilla::BitwiseCast<char*, unsigned char*>(signedItem.data),
|
|
|
|
signedItem.len);
|
|
|
|
rv = mozilla::Base64Encode(signedItemStr, keystring);
|
|
|
|
if (NS_FAILED(rv)) {
|
2005-07-13 23:31:14 +04:00
|
|
|
goto loser;
|
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2017-03-17 18:31:40 +03:00
|
|
|
CopyASCIItoUTF16(keystring, aOutPublicKey);
|
2001-05-03 05:00:56 +04:00
|
|
|
|
|
|
|
rv = NS_OK;
|
2015-09-22 19:52:58 +03:00
|
|
|
|
2001-05-03 05:00:56 +04:00
|
|
|
loser:
|
2016-04-05 03:35:24 +03:00
|
|
|
if (srv != SECSuccess) {
|
2001-05-03 05:00:56 +04:00
|
|
|
if ( privateKey ) {
|
|
|
|
PK11_DestroyTokenObject(privateKey->pkcs11Slot,privateKey->pkcs11ID);
|
|
|
|
}
|
|
|
|
if ( publicKey ) {
|
|
|
|
PK11_DestroyTokenObject(publicKey->pkcs11Slot,publicKey->pkcs11ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( spkInfo ) {
|
2011-03-28 08:03:11 +04:00
|
|
|
SECKEY_DestroySubjectPublicKeyInfo(spkInfo);
|
2001-05-03 05:00:56 +04:00
|
|
|
}
|
|
|
|
if ( publicKey ) {
|
|
|
|
SECKEY_DestroyPublicKey(publicKey);
|
|
|
|
}
|
2003-01-07 01:23:49 +03:00
|
|
|
if ( privateKey ) {
|
|
|
|
SECKEY_DestroyPrivateKey(privateKey);
|
|
|
|
}
|
2012-10-18 00:48:36 +04:00
|
|
|
if (slot) {
|
2001-05-03 05:00:56 +04:00
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
}
|
2001-10-23 10:11:57 +04:00
|
|
|
if (KeygenRunnable) {
|
2011-03-28 08:03:11 +04:00
|
|
|
NS_RELEASE(KeygenRunnable);
|
2001-10-23 10:11:57 +04:00
|
|
|
}
|
2006-09-29 04:50:46 +04:00
|
|
|
if (keyparamsString) {
|
2015-03-27 03:01:12 +03:00
|
|
|
free(keyparamsString);
|
2005-07-13 23:31:14 +04:00
|
|
|
}
|
|
|
|
if (pkac.challenge.data) {
|
2015-03-27 03:01:12 +03:00
|
|
|
free(pkac.challenge.data);
|
2005-07-13 23:31:14 +04:00
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-27 01:28:28 +03:00
|
|
|
// static
|
|
|
|
void
|
2018-01-30 07:29:11 +03:00
|
|
|
nsKeygenFormProcessor::ExtractParams(Element* aElement,
|
2014-11-27 01:28:28 +03:00
|
|
|
nsAString& challengeValue,
|
|
|
|
nsAString& keyTypeValue,
|
|
|
|
nsAString& keyParamsValue)
|
|
|
|
{
|
2018-01-30 07:29:11 +03:00
|
|
|
aElement->GetAttribute(NS_LITERAL_STRING("keytype"), keyTypeValue);
|
2010-02-25 08:58:16 +03:00
|
|
|
if (keyTypeValue.IsEmpty()) {
|
2001-05-03 05:00:56 +04:00
|
|
|
// If this field is not present, we default to rsa.
|
2010-02-25 08:58:16 +03:00
|
|
|
keyTypeValue.AssignLiteral("rsa");
|
2001-05-03 05:00:56 +04:00
|
|
|
}
|
2014-11-27 01:28:28 +03:00
|
|
|
|
2018-01-30 07:29:11 +03:00
|
|
|
aElement->GetAttribute(NS_LITERAL_STRING("pqg"),
|
|
|
|
keyParamsValue);
|
2017-03-14 13:54:57 +03:00
|
|
|
/* XXX We can still support the pqg attribute in the keygen
|
|
|
|
* tag for backward compatibility while introducing a more
|
2010-02-25 08:58:16 +03:00
|
|
|
* general attribute named keyparams.
|
|
|
|
*/
|
|
|
|
if (keyParamsValue.IsEmpty()) {
|
2018-01-30 07:29:11 +03:00
|
|
|
aElement->GetAttribute(NS_LITERAL_STRING("keyparams"),
|
|
|
|
keyParamsValue);
|
2010-02-25 08:58:16 +03:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:29:11 +03:00
|
|
|
aElement->GetAttribute(NS_LITERAL_STRING("challenge"), challengeValue);
|
2014-11-27 01:28:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2018-01-30 07:29:11 +03:00
|
|
|
nsKeygenFormProcessor::ProcessValue(Element* aElement,
|
2014-11-27 01:28:28 +03:00
|
|
|
const nsAString& aName,
|
|
|
|
nsAString& aValue)
|
|
|
|
{
|
|
|
|
nsAutoString challengeValue;
|
|
|
|
nsAutoString keyTypeValue;
|
|
|
|
nsAutoString keyParamsValue;
|
|
|
|
ExtractParams(aElement, challengeValue, keyTypeValue, keyParamsValue);
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2017-03-14 13:54:57 +03:00
|
|
|
return GetPublicKey(aValue, challengeValue, keyTypeValue,
|
2010-02-25 08:58:16 +03:00
|
|
|
aValue, keyParamsValue);
|
2014-11-27 01:28:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsKeygenFormProcessor::ProcessValueIPC(const nsAString& aOldValue,
|
|
|
|
const nsAString& aChallenge,
|
|
|
|
const nsAString& aKeyType,
|
|
|
|
const nsAString& aKeyParams,
|
|
|
|
nsAString& newValue)
|
|
|
|
{
|
|
|
|
return GetPublicKey(aOldValue, aChallenge, PromiseFlatString(aKeyType),
|
|
|
|
newValue, aKeyParams);
|
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
|
2014-11-27 01:28:28 +03:00
|
|
|
nsresult
|
|
|
|
nsKeygenFormProcessor::ProvideContent(const nsAString& aFormType,
|
|
|
|
nsTArray<nsString>& aContent,
|
|
|
|
nsAString& aAttribute)
|
2017-03-14 13:54:57 +03:00
|
|
|
{
|
2014-11-27 01:28:28 +03:00
|
|
|
if (Compare(aFormType, NS_LITERAL_STRING("SELECT"),
|
|
|
|
nsCaseInsensitiveStringComparator()) == 0) {
|
2007-11-13 03:31:23 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < number_of_key_size_choices; ++i) {
|
2009-01-18 23:14:14 +03:00
|
|
|
aContent.AppendElement(mSECKeySizeChoiceList[i].name);
|
2001-05-03 05:00:56 +04:00
|
|
|
}
|
2004-06-17 04:13:25 +04:00
|
|
|
aAttribute.AssignLiteral("-mozilla-keygen");
|
2001-05-03 05:00:56 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2017-03-14 13:54:57 +03:00
|
|
|
}
|
2001-05-03 05:00:56 +04:00
|
|
|
|