2012-05-31 02:33:35 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-02 12:34:46 -07:00
|
|
|
// XXX: This must be done prior to including cert.h (directly or indirectly).
|
|
|
|
// CERT_AddTempCertToPerm is exposed as __CERT_AddTempCertToPerm, but it is
|
|
|
|
// only exported so PSM can use it for this specific purpose.
|
|
|
|
#define CERT_AddTempCertToPerm __CERT_AddTempCertToPerm
|
|
|
|
|
2012-10-27 00:11:35 -07:00
|
|
|
#include "nsNSSComponent.h"
|
2013-04-15 16:45:11 -07:00
|
|
|
#include "nsNSSCertificateDB.h"
|
2012-10-27 00:11:35 -07:00
|
|
|
|
|
|
|
#include "CertVerifier.h"
|
2014-01-26 19:36:28 -08:00
|
|
|
#include "ExtendedValidation.h"
|
2013-07-08 16:30:59 -07:00
|
|
|
#include "NSSCertDBTrustDomain.h"
|
2014-03-20 14:29:21 -07:00
|
|
|
#include "pkix/pkixtypes.h"
|
2012-10-27 00:11:35 -07:00
|
|
|
#include "nsNSSComponent.h"
|
2012-11-14 15:31:39 -08:00
|
|
|
#include "mozilla/Base64.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsNSSCertificate.h"
|
|
|
|
#include "nsNSSHelper.h"
|
|
|
|
#include "nsNSSCertHelper.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsICertificateDialogs.h"
|
|
|
|
#include "nsNSSCertTrust.h"
|
2012-06-05 19:08:30 -07:00
|
|
|
#include "nsIFile.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPKCS12Blob.h"
|
|
|
|
#include "nsPK11TokenDB.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsIMutableArray.h"
|
|
|
|
#include "nsArrayUtils.h"
|
|
|
|
#include "nsNSSShutDown.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIPrompt.h"
|
2011-11-02 21:02:48 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2012-12-06 19:05:27 -08:00
|
|
|
#include "nsIObserverService.h"
|
2012-12-07 14:50:43 -08:00
|
|
|
#include "SharedSSLState.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "certdb.h"
|
|
|
|
#include "secerr.h"
|
|
|
|
#include "nssb64.h"
|
|
|
|
#include "secasn1.h"
|
|
|
|
#include "secder.h"
|
|
|
|
#include "ssl.h"
|
|
|
|
#include "plbase64.h"
|
|
|
|
|
2012-10-17 13:48:36 -07:00
|
|
|
using namespace mozilla;
|
2012-10-27 00:11:35 -07:00
|
|
|
using namespace mozilla::psm;
|
2012-12-07 14:50:43 -08:00
|
|
|
using mozilla::psm::SharedSSLState;
|
2012-10-17 13:48:36 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
extern PRLogModuleInfo* gPIPNSSLog;
|
|
|
|
|
2014-03-01 19:01:23 -08:00
|
|
|
static nsresult
|
|
|
|
attemptToLogInWithDefaultPassword()
|
|
|
|
{
|
|
|
|
#ifdef NSS_DISABLE_DBM
|
|
|
|
// The SQL NSS DB requires the user to be authenticated to set certificate
|
|
|
|
// trust settings, even if the user's password is empty. To maintain
|
|
|
|
// compatibility with the DBM-based database, try to log in with the
|
|
|
|
// default empty password. This will allow, at least, tests that need to
|
|
|
|
// change certificate trust to pass on all platforms. TODO(bug 978120): Do
|
|
|
|
// proper testing and/or implement a better solution so that we are confident
|
|
|
|
// that this does the correct thing outside of xpcshell tests too.
|
|
|
|
ScopedPK11SlotInfo slot(PK11_GetInternalKeySlot());
|
|
|
|
if (!slot) {
|
|
|
|
return MapSECStatus(SECFailure);
|
|
|
|
}
|
|
|
|
if (PK11_NeedUserInit(slot)) {
|
|
|
|
// Ignore the return value. Presumably PK11_InitPin will fail if the user
|
|
|
|
// has a non-default password.
|
|
|
|
(void) PK11_InitPin(slot, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-03 22:09:24 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsNSSCertificateDB, nsIX509CertDB)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsNSSCertificateDB::~nsNSSCertificateDB()
|
|
|
|
{
|
2013-12-19 11:45:50 -08:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
shutdown(calledFromObject);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::FindCertByNickname(nsISupports *aToken,
|
|
|
|
const nsAString &nickname,
|
|
|
|
nsIX509Cert **_rvCert)
|
|
|
|
{
|
2012-11-12 09:42:28 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(_rvCert);
|
|
|
|
*_rvCert = nullptr;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert;
|
2012-10-17 13:48:36 -07:00
|
|
|
char *asciiname = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ConvertUTF16toUTF8 aUtf8Nickname(nickname);
|
2007-07-08 00:08:04 -07:00
|
|
|
asciiname = const_cast<char*>(aUtf8Nickname.get());
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("Getting \"%s\"\n", asciiname));
|
2012-10-17 13:48:36 -07:00
|
|
|
cert = PK11_FindCertFromNickname(asciiname, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!cert) {
|
|
|
|
cert = CERT_FindCertByNickname(CERT_GetDefaultCertDB(), asciiname);
|
|
|
|
}
|
|
|
|
if (cert) {
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("got it\n"));
|
2014-01-22 17:13:19 -08:00
|
|
|
nsCOMPtr<nsIX509Cert> pCert = nsNSSCertificate::Create(cert.get());
|
2010-10-26 04:57:18 -07:00
|
|
|
if (pCert) {
|
2012-11-12 09:42:28 -08:00
|
|
|
pCert.forget(_rvCert);
|
2010-10-26 04:57:18 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::FindCertByDBKey(const char *aDBkey, nsISupports *aToken,
|
|
|
|
nsIX509Cert **_cert)
|
|
|
|
{
|
2012-11-12 09:42:28 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(aDBkey);
|
|
|
|
NS_ENSURE_ARG(aDBkey[0]);
|
|
|
|
NS_ENSURE_ARG_POINTER(_cert);
|
|
|
|
*_cert = nullptr;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
SECItem keyItem = {siBuffer, nullptr, 0};
|
2007-03-22 10:30:00 -07:00
|
|
|
SECItem *dummy;
|
|
|
|
CERTIssuerAndSN issuerSN;
|
2012-02-21 09:39:58 -08:00
|
|
|
//unsigned long moduleID,slotID;
|
2007-05-09 16:43:01 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
dummy = NSSBase64_DecodeBuffer(nullptr, &keyItem, aDBkey,
|
2013-05-07 20:40:12 -07:00
|
|
|
(uint32_t)strlen(aDBkey));
|
2007-09-17 13:46:27 -07:00
|
|
|
if (!dummy || keyItem.len < NS_NSS_LONG*4) {
|
|
|
|
PR_FREEIF(keyItem.data);
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert;
|
2007-03-22 10:30:00 -07:00
|
|
|
// someday maybe we can speed up the search using the moduleID and slotID
|
2012-02-21 09:39:58 -08:00
|
|
|
// moduleID = NS_NSS_GET_LONG(keyItem.data);
|
|
|
|
// slotID = NS_NSS_GET_LONG(&keyItem.data[NS_NSS_LONG]);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// build the issuer/SN structure
|
|
|
|
issuerSN.serialNumber.len = NS_NSS_GET_LONG(&keyItem.data[NS_NSS_LONG*2]);
|
|
|
|
issuerSN.derIssuer.len = NS_NSS_GET_LONG(&keyItem.data[NS_NSS_LONG*3]);
|
2007-09-17 13:46:27 -07:00
|
|
|
if (issuerSN.serialNumber.len == 0 || issuerSN.derIssuer.len == 0
|
|
|
|
|| issuerSN.serialNumber.len + issuerSN.derIssuer.len
|
|
|
|
!= keyItem.len - NS_NSS_LONG*4) {
|
|
|
|
PR_FREEIF(keyItem.data);
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
issuerSN.serialNumber.data= &keyItem.data[NS_NSS_LONG*4];
|
|
|
|
issuerSN.derIssuer.data= &keyItem.data[NS_NSS_LONG*4+
|
|
|
|
issuerSN.serialNumber.len];
|
|
|
|
|
|
|
|
cert = CERT_FindCertByIssuerAndSN(CERT_GetDefaultCertDB(), &issuerSN);
|
|
|
|
PR_FREEIF(keyItem.data);
|
|
|
|
if (cert) {
|
2014-01-22 17:13:19 -08:00
|
|
|
nsCOMPtr<nsIX509Cert> nssCert = nsNSSCertificate::Create(cert.get());
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!nssCert)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-11-12 09:42:28 -08:00
|
|
|
nssCert.forget(_cert);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::FindCertNicknames(nsISupports *aToken,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aType,
|
|
|
|
uint32_t *_count,
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t ***_certNames)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
/*
|
|
|
|
* obtain the cert list from NSS
|
|
|
|
*/
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertList certList(PK11_ListCerts(PK11CertListUnique, nullptr));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!certList)
|
|
|
|
goto cleanup;
|
|
|
|
/*
|
|
|
|
* get list of cert names from list of certs
|
|
|
|
* XXX also cull the list (NSS only distinguishes based on user/non-user
|
|
|
|
*/
|
2014-01-22 17:13:19 -08:00
|
|
|
getCertNames(certList.get(), aType, _count, _certNames, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_OK;
|
|
|
|
/*
|
|
|
|
* finish up
|
|
|
|
*/
|
|
|
|
cleanup:
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-09-25 09:18:38 -07:00
|
|
|
SECStatus
|
2007-03-22 10:30:00 -07:00
|
|
|
collect_certs(void *arg, SECItem **certs, int numcerts)
|
|
|
|
{
|
|
|
|
CERTDERCerts *collectArgs;
|
|
|
|
SECItem *cert;
|
|
|
|
SECStatus rv;
|
|
|
|
|
|
|
|
collectArgs = (CERTDERCerts *)arg;
|
|
|
|
|
|
|
|
collectArgs->numcerts = numcerts;
|
|
|
|
collectArgs->rawCerts = (SECItem *) PORT_ArenaZAlloc(collectArgs->arena,
|
|
|
|
sizeof(SECItem) * numcerts);
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!collectArgs->rawCerts)
|
2007-03-22 10:30:00 -07:00
|
|
|
return(SECFailure);
|
|
|
|
|
|
|
|
cert = collectArgs->rawCerts;
|
|
|
|
|
|
|
|
while ( numcerts-- ) {
|
|
|
|
rv = SECITEM_CopyItem(collectArgs->arena, cert, *certs);
|
|
|
|
if ( rv == SECFailure )
|
|
|
|
return(SECFailure);
|
|
|
|
cert++;
|
|
|
|
certs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (SECSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
CERTDERCerts*
|
2012-10-10 23:39:28 -07:00
|
|
|
nsNSSCertificateDB::getCertsFromPackage(PLArenaPool *arena, uint8_t *data,
|
2013-08-29 16:15:35 -07:00
|
|
|
uint32_t length,
|
|
|
|
const nsNSSShutDownPreventionLock &/*proofOfLock*/)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
CERTDERCerts *collectArgs =
|
|
|
|
(CERTDERCerts *)PORT_ArenaZAlloc(arena, sizeof(CERTDERCerts));
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!collectArgs)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
collectArgs->arena = arena;
|
2007-07-08 00:08:04 -07:00
|
|
|
SECStatus sec_rv = CERT_DecodeCertPackage(reinterpret_cast<char *>(data),
|
2007-03-22 10:30:00 -07:00
|
|
|
length, collect_certs,
|
|
|
|
(void *)collectArgs);
|
|
|
|
if (sec_rv != SECSuccess)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return collectArgs;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsNSSCertificateDB::handleCACertDownload(nsIArray *x509Certs,
|
2013-08-29 16:15:35 -07:00
|
|
|
nsIInterfaceRequestor *ctx,
|
|
|
|
const nsNSSShutDownPreventionLock &proofOfLock)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// First thing we have to do is figure out which certificate we're
|
|
|
|
// gonna present to the user. The CA may have sent down a list of
|
|
|
|
// certs which may or may not be a chained list of certs. Until
|
|
|
|
// the day we can design some solid UI for the general case, we'll
|
|
|
|
// code to the > 90% case. That case is where a CA sends down a
|
|
|
|
// list that is a hierarchy whose root is either the first or
|
|
|
|
// the last cert. What we're gonna do is compare the first
|
|
|
|
// 2 entries, if the second was signed by the first, we assume
|
|
|
|
// the root cert is the first cert and display it. Otherwise,
|
|
|
|
// we compare the last 2 entries, if the second to last cert was
|
|
|
|
// signed by the last cert, then we assume the last cert is the
|
|
|
|
// root and display it.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numCerts;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
x509Certs->GetLength(&numCerts);
|
|
|
|
NS_ASSERTION(numCerts > 0, "Didn't get any certs to import.");
|
|
|
|
if (numCerts == 0)
|
|
|
|
return NS_OK; // Nothing to import, so nothing to do.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIX509Cert> certToShow;
|
|
|
|
nsCOMPtr<nsISupports> isupports;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t selCertIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (numCerts == 1) {
|
|
|
|
// There's only one cert, so let's show it.
|
|
|
|
selCertIndex = 0;
|
|
|
|
certToShow = do_QueryElementAt(x509Certs, selCertIndex);
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIX509Cert> cert0; // first cert
|
|
|
|
nsCOMPtr<nsIX509Cert> cert1; // second cert
|
|
|
|
nsCOMPtr<nsIX509Cert> certn_2; // second to last cert
|
|
|
|
nsCOMPtr<nsIX509Cert> certn_1; // last cert
|
|
|
|
|
|
|
|
cert0 = do_QueryElementAt(x509Certs, 0);
|
|
|
|
cert1 = do_QueryElementAt(x509Certs, 1);
|
|
|
|
certn_2 = do_QueryElementAt(x509Certs, numCerts-2);
|
|
|
|
certn_1 = do_QueryElementAt(x509Certs, numCerts-1);
|
|
|
|
|
|
|
|
nsXPIDLString cert0SubjectName;
|
|
|
|
nsXPIDLString cert1IssuerName;
|
|
|
|
nsXPIDLString certn_2IssuerName;
|
|
|
|
nsXPIDLString certn_1SubjectName;
|
|
|
|
|
|
|
|
cert0->GetSubjectName(cert0SubjectName);
|
|
|
|
cert1->GetIssuerName(cert1IssuerName);
|
|
|
|
certn_2->GetIssuerName(certn_2IssuerName);
|
|
|
|
certn_1->GetSubjectName(certn_1SubjectName);
|
|
|
|
|
|
|
|
if (cert1IssuerName.Equals(cert0SubjectName)) {
|
|
|
|
// In this case, the first cert in the list signed the second,
|
|
|
|
// so the first cert is the root. Let's display it.
|
|
|
|
selCertIndex = 0;
|
|
|
|
certToShow = cert0;
|
|
|
|
} else
|
|
|
|
if (certn_2IssuerName.Equals(certn_1SubjectName)) {
|
|
|
|
// In this case the last cert has signed the second to last cert.
|
|
|
|
// The last cert is the root, so let's display it.
|
|
|
|
selCertIndex = numCerts-1;
|
|
|
|
certToShow = certn_1;
|
|
|
|
} else {
|
|
|
|
// It's not a chain, so let's just show the first one in the
|
|
|
|
// downloaded list.
|
|
|
|
selCertIndex = 0;
|
|
|
|
certToShow = cert0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!certToShow)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICertificateDialogs> dialogs;
|
|
|
|
nsresult rv = ::getNSSDialogs(getter_AddRefs(dialogs),
|
|
|
|
NS_GET_IID(nsICertificateDialogs),
|
|
|
|
NS_CERTIFICATEDIALOGS_CONTRACTID);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
SECItem der;
|
2012-08-22 08:56:38 -07:00
|
|
|
rv=certToShow->GetRawDER(&der.len, (uint8_t **)&der.data);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("Creating temp cert\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
CERTCertDBHandle *certdb = CERT_GetDefaultCertDB();
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate tmpCert(CERT_FindCertByDERCert(certdb, &der));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!tmpCert) {
|
|
|
|
tmpCert = CERT_NewTempCertificate(certdb, &der,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2015-03-26 17:01:12 -07:00
|
|
|
free(der.data);
|
2012-07-30 07:20:58 -07:00
|
|
|
der.data = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
der.len = 0;
|
|
|
|
|
|
|
|
if (!tmpCert) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("Couldn't create cert from DER blob");
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
if (!CERT_IsCACert(tmpCert.get(), nullptr)) {
|
2013-08-29 16:15:35 -07:00
|
|
|
DisplayCertificateAlert(ctx, "NotACACert", certToShow, proofOfLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmpCert->isperm) {
|
2013-08-29 16:15:35 -07:00
|
|
|
DisplayCertificateAlert(ctx, "CaCertExists", certToShow, proofOfLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t trustBits;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool allows;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = dialogs->ConfirmDownloadCACert(ctx, certToShow, &trustBits, &allows);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!allows)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("trust is %d\n", trustBits));
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXPIDLCString nickname;
|
2014-01-22 17:13:19 -08:00
|
|
|
nickname.Adopt(CERT_MakeCANickname(tmpCert.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("Created nick \"%s\"\n", nickname.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsNSSCertTrust trust;
|
|
|
|
trust.SetValidCA();
|
2008-09-25 15:34:22 -07:00
|
|
|
trust.AddCATrust(!!(trustBits & nsIX509CertDB::TRUSTED_SSL),
|
|
|
|
!!(trustBits & nsIX509CertDB::TRUSTED_EMAIL),
|
|
|
|
!!(trustBits & nsIX509CertDB::TRUSTED_OBJSIGN));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
SECStatus srv = __CERT_AddTempCertToPerm(tmpCert.get(),
|
2012-11-02 12:34:46 -07:00
|
|
|
const_cast<char*>(nickname.get()),
|
|
|
|
trust.GetTrust());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (srv != SECSuccess)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Import additional delivered certificates that can be verified.
|
|
|
|
|
|
|
|
// build a CertList for filtering
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertList certList(CERT_NewCertList());
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!certList) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all remaining certs into temp store
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i=0; i<numCerts; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (i == selCertIndex) {
|
|
|
|
// we already processed that one
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
certToShow = do_QueryElementAt(x509Certs, i);
|
2012-08-22 08:56:38 -07:00
|
|
|
certToShow->GetRawDER(&der.len, (uint8_t **)&der.data);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
CERTCertificate *tmpCert2 =
|
2012-07-30 07:20:58 -07:00
|
|
|
CERT_NewTempCertificate(certdb, &der, nullptr, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-03-26 17:01:12 -07:00
|
|
|
free(der.data);
|
2012-07-30 07:20:58 -07:00
|
|
|
der.data = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
der.len = 0;
|
|
|
|
|
|
|
|
if (!tmpCert2) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("Couldn't create temp cert from DER blob");
|
2007-03-22 10:30:00 -07:00
|
|
|
continue; // Let's try to import the rest of 'em
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
CERT_AddCertToListTail(certList.get(), tmpCert2);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
return ImportValidCACertsInList(certList.get(), ctx, proofOfLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* [noscript] void importCertificates(in charPtr data, in unsigned long length,
|
|
|
|
* in unsigned long type,
|
|
|
|
* in nsIInterfaceRequestor ctx);
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsNSSCertificateDB::ImportCertificates(uint8_t * data, uint32_t length,
|
|
|
|
uint32_t type,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIInterfaceRequestor *ctx)
|
|
|
|
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult nsrv;
|
|
|
|
|
2012-10-10 23:39:28 -07:00
|
|
|
PLArenaPool *arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!arena)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2013-08-29 16:15:35 -07:00
|
|
|
CERTDERCerts *certCollection = getCertsFromPackage(arena, data, length, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!certCollection) {
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIMutableArray> array =
|
|
|
|
do_CreateInstance(NS_ARRAY_CONTRACTID, &nsrv);
|
|
|
|
if (NS_FAILED(nsrv)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsrv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now let's create some certs to work with
|
|
|
|
nsCOMPtr<nsIX509Cert> x509Cert;
|
|
|
|
nsNSSCertificate *nssCert;
|
|
|
|
SECItem *currItem;
|
|
|
|
for (int i=0; i<certCollection->numcerts; i++) {
|
|
|
|
currItem = &certCollection->rawCerts[i];
|
|
|
|
nssCert = nsNSSCertificate::ConstructFromDER((char*)currItem->data, currItem->len);
|
|
|
|
if (!nssCert)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
x509Cert = do_QueryInterface((nsIX509Cert*)nssCert);
|
2011-10-17 07:59:28 -07:00
|
|
|
array->AppendElement(x509Cert, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case nsIX509Cert::CA_CERT:
|
2013-08-29 16:15:35 -07:00
|
|
|
nsrv = handleCACertDownload(array, ctx, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// We only deal with import CA certs in this method currently.
|
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsrv;
|
|
|
|
}
|
|
|
|
|
2012-10-27 00:11:35 -07:00
|
|
|
static
|
|
|
|
SECStatus
|
2014-01-22 17:13:19 -08:00
|
|
|
ImportCertsIntoPermanentStorage(
|
2014-07-06 15:55:38 -07:00
|
|
|
const ScopedCERTCertList& certChain,
|
2014-01-22 17:13:19 -08:00
|
|
|
const SECCertUsage usage, const PRBool caOnly)
|
2012-10-27 00:11:35 -07:00
|
|
|
{
|
|
|
|
CERTCertDBHandle *certdb = CERT_GetDefaultCertDB();
|
|
|
|
|
|
|
|
int chainLen = 0;
|
|
|
|
for (CERTCertListNode *chainNode = CERT_LIST_HEAD(certChain);
|
|
|
|
!CERT_LIST_END(chainNode, certChain);
|
|
|
|
chainNode = CERT_LIST_NEXT(chainNode)) {
|
|
|
|
chainLen++;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECItem **rawArray;
|
|
|
|
rawArray = (SECItem **) PORT_Alloc(chainLen * sizeof(SECItem *));
|
|
|
|
if (!rawArray) {
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (CERTCertListNode *chainNode = CERT_LIST_HEAD(certChain);
|
|
|
|
!CERT_LIST_END(chainNode, certChain);
|
|
|
|
chainNode = CERT_LIST_NEXT(chainNode), i++) {
|
|
|
|
rawArray[i] = &chainNode->cert->derCert;
|
|
|
|
}
|
2013-11-06 09:30:45 -08:00
|
|
|
SECStatus srv = CERT_ImportCerts(certdb, usage, chainLen, rawArray,
|
|
|
|
nullptr, true, caOnly, nullptr);
|
2012-10-27 00:11:35 -07:00
|
|
|
|
2013-11-06 09:30:45 -08:00
|
|
|
PORT_Free(rawArray);
|
|
|
|
return srv;
|
2012-10-27 00:11:35 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* [noscript] void importEmailCertificates(in charPtr data, in unsigned long length,
|
|
|
|
* in nsIInterfaceRequestor ctx);
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsNSSCertificateDB::ImportEmailCertificate(uint8_t * data, uint32_t length,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIInterfaceRequestor *ctx)
|
|
|
|
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
SECStatus srv = SECFailure;
|
|
|
|
nsresult nsrv = NS_OK;
|
|
|
|
CERTCertDBHandle *certdb;
|
2012-10-17 13:48:36 -07:00
|
|
|
CERTCertificate **certArray = nullptr;
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertList certList;
|
2007-03-22 10:30:00 -07:00
|
|
|
CERTCertListNode *node;
|
|
|
|
SECItem **rawArray;
|
|
|
|
int numcerts;
|
|
|
|
int i;
|
2013-04-15 16:45:11 -07:00
|
|
|
|
2012-10-10 23:39:28 -07:00
|
|
|
PLArenaPool *arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!arena)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2013-08-29 16:15:35 -07:00
|
|
|
CERTDERCerts *certCollection = getCertsFromPackage(arena, data, length, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!certCollection) {
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-01-19 14:05:40 -08:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
2012-10-27 00:11:35 -07:00
|
|
|
NS_ENSURE_TRUE(certVerifier, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
certdb = CERT_GetDefaultCertDB();
|
|
|
|
|
|
|
|
numcerts = certCollection->numcerts;
|
|
|
|
|
|
|
|
rawArray = (SECItem **) PORT_Alloc(sizeof(SECItem *) * numcerts);
|
|
|
|
if ( !rawArray ) {
|
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i < numcerts; i++) {
|
|
|
|
rawArray[i] = &certCollection->rawCerts[i];
|
|
|
|
}
|
|
|
|
|
2012-10-27 00:11:35 -07:00
|
|
|
srv = CERT_ImportCerts(certdb, certUsageEmailRecipient, numcerts, rawArray,
|
2012-10-17 13:48:36 -07:00
|
|
|
&certArray, false, false, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PORT_Free(rawArray);
|
2012-10-17 13:48:36 -07:00
|
|
|
rawArray = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
// build a CertList for filtering
|
|
|
|
certList = CERT_NewCertList();
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!certList) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
for (i=0; i < numcerts; i++) {
|
|
|
|
CERTCertificate *cert = certArray[i];
|
|
|
|
if (cert)
|
|
|
|
cert = CERT_DupCertificate(cert);
|
|
|
|
if (cert)
|
2014-01-22 17:13:19 -08:00
|
|
|
CERT_AddCertToListTail(certList.get(), cert);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* go down the remaining list of certs and verify that they have
|
|
|
|
* valid chains, then import them.
|
|
|
|
*/
|
2011-05-05 13:41:40 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (node = CERT_LIST_HEAD(certList);
|
|
|
|
!CERT_LIST_END(node,certList);
|
|
|
|
node = CERT_LIST_NEXT(node)) {
|
|
|
|
|
|
|
|
if (!node->cert) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:55:38 -07:00
|
|
|
ScopedCERTCertList certChain;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-05 14:49:10 -08:00
|
|
|
SECStatus rv = certVerifier->VerifyCert(node->cert,
|
2012-10-27 00:11:35 -07:00
|
|
|
certificateUsageEmailRecipient,
|
2014-08-02 08:49:12 -07:00
|
|
|
mozilla::pkix::Now(), ctx,
|
|
|
|
nullptr, 0, nullptr, &certChain);
|
2013-04-15 16:44:38 -07:00
|
|
|
|
2012-10-27 00:11:35 -07:00
|
|
|
if (rv != SECSuccess) {
|
2010-10-26 04:57:18 -07:00
|
|
|
nsCOMPtr<nsIX509Cert> certToShow = nsNSSCertificate::Create(node->cert);
|
2013-08-29 16:15:35 -07:00
|
|
|
DisplayCertificateAlert(ctx, "NotImportingUnverifiedCert", certToShow, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-22 17:13:19 -08:00
|
|
|
rv = ImportCertsIntoPermanentStorage(certChain, certUsageEmailRecipient,
|
|
|
|
false);
|
2012-10-27 00:11:35 -07:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2012-10-17 13:48:36 -07:00
|
|
|
CERT_SaveSMimeProfile(node->cert, nullptr, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
loser:
|
|
|
|
if (certArray) {
|
|
|
|
CERT_DestroyCertArray(certArray, numcerts);
|
|
|
|
}
|
|
|
|
if (arena)
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsrv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsNSSCertificateDB::ImportServerCertificate(uint8_t * data, uint32_t length,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIInterfaceRequestor *ctx)
|
|
|
|
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
SECStatus srv = SECFailure;
|
|
|
|
nsresult nsrv = NS_OK;
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert;
|
2012-07-30 07:20:58 -07:00
|
|
|
SECItem **rawCerts = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
int numcerts;
|
|
|
|
int i;
|
|
|
|
nsNSSCertTrust trust;
|
2012-07-30 07:20:58 -07:00
|
|
|
char *serverNickname = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-10 23:39:28 -07:00
|
|
|
PLArenaPool *arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!arena)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2013-08-29 16:15:35 -07:00
|
|
|
CERTDERCerts *certCollection = getCertsFromPackage(arena, data, length, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!certCollection) {
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(), certCollection->rawCerts,
|
2012-10-17 13:48:36 -07:00
|
|
|
nullptr, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!cert) {
|
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
numcerts = certCollection->numcerts;
|
|
|
|
rawCerts = (SECItem **) PORT_Alloc(sizeof(SECItem *) * numcerts);
|
|
|
|
if ( !rawCerts ) {
|
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 0; i < numcerts; i++ ) {
|
|
|
|
rawCerts[i] = &certCollection->rawCerts[i];
|
|
|
|
}
|
|
|
|
|
2013-07-08 16:30:59 -07:00
|
|
|
serverNickname = DefaultServerNicknameForCert(cert.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
srv = CERT_ImportCerts(CERT_GetDefaultCertDB(), certUsageSSLServer,
|
2012-10-17 13:48:36 -07:00
|
|
|
numcerts, rawCerts, nullptr, true, false,
|
2007-03-22 10:30:00 -07:00
|
|
|
serverNickname);
|
|
|
|
PR_FREEIF(serverNickname);
|
|
|
|
if ( srv != SECSuccess ) {
|
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
trust.SetValidServerPeer();
|
2014-01-22 17:13:19 -08:00
|
|
|
srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), cert.get(),
|
|
|
|
trust.GetTrust());
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( srv != SECSuccess ) {
|
|
|
|
nsrv = NS_ERROR_FAILURE;
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
loser:
|
|
|
|
PORT_Free(rawCerts);
|
|
|
|
if (arena)
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsrv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-29 16:15:35 -07:00
|
|
|
nsNSSCertificateDB::ImportValidCACerts(int numCACerts, SECItem *CACerts, nsIInterfaceRequestor *ctx, const nsNSSShutDownPreventionLock &proofOfLock)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-12 09:42:28 -08:00
|
|
|
ScopedCERTCertList certList;
|
2007-03-22 10:30:00 -07:00
|
|
|
SECItem **rawArray;
|
|
|
|
|
|
|
|
// build a CertList for filtering
|
|
|
|
certList = CERT_NewCertList();
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!certList) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all certs into temp store
|
|
|
|
SECStatus srv = SECFailure;
|
2012-10-17 13:48:36 -07:00
|
|
|
CERTCertificate **certArray = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rawArray = (SECItem **) PORT_Alloc(sizeof(SECItem *) * numCACerts);
|
|
|
|
if ( !rawArray ) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i < numCACerts; i++) {
|
|
|
|
rawArray[i] = &CACerts[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
srv = CERT_ImportCerts(CERT_GetDefaultCertDB(), certUsageAnyCA, numCACerts, rawArray,
|
2012-10-17 13:48:36 -07:00
|
|
|
&certArray, false, true, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PORT_Free(rawArray);
|
2012-10-17 13:48:36 -07:00
|
|
|
rawArray = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i2=0; i2 < numCACerts; i2++) {
|
|
|
|
CERTCertificate *cacert = certArray[i2];
|
|
|
|
if (cacert)
|
|
|
|
cacert = CERT_DupCertificate(cacert);
|
|
|
|
if (cacert)
|
|
|
|
CERT_AddCertToListTail(certList, cacert);
|
|
|
|
}
|
|
|
|
|
|
|
|
CERT_DestroyCertArray(certArray, numCACerts);
|
|
|
|
|
2013-08-29 16:15:35 -07:00
|
|
|
return ImportValidCACertsInList(certList, ctx, proofOfLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2013-08-29 16:15:35 -07:00
|
|
|
nsNSSCertificateDB::ImportValidCACertsInList(CERTCertList *certList, nsIInterfaceRequestor *ctx,
|
|
|
|
const nsNSSShutDownPreventionLock &proofOfLock)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-01-19 14:05:40 -08:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
2012-10-27 00:11:35 -07:00
|
|
|
if (!certVerifier)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* filter out the certs we don't want */
|
2011-10-17 07:59:28 -07:00
|
|
|
SECStatus srv = CERT_FilterCertListByUsage(certList, certUsageAnyCA, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* go down the remaining list of certs and verify that they have
|
|
|
|
* valid chains, if yes, then import.
|
|
|
|
*/
|
|
|
|
CERTCertListNode *node;
|
2011-05-05 13:41:40 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
for (node = CERT_LIST_HEAD(certList);
|
|
|
|
!CERT_LIST_END(node,certList);
|
|
|
|
node = CERT_LIST_NEXT(node)) {
|
2014-07-06 15:55:38 -07:00
|
|
|
ScopedCERTCertList certChain;
|
2014-02-05 14:49:10 -08:00
|
|
|
SECStatus rv = certVerifier->VerifyCert(node->cert,
|
2013-09-27 19:53:36 -07:00
|
|
|
certificateUsageVerifyCA,
|
2014-08-02 08:49:12 -07:00
|
|
|
mozilla::pkix::Now(), ctx,
|
|
|
|
nullptr, 0, nullptr, &certChain);
|
2012-10-27 00:11:35 -07:00
|
|
|
if (rv != SECSuccess) {
|
2010-10-26 04:57:18 -07:00
|
|
|
nsCOMPtr<nsIX509Cert> certToShow = nsNSSCertificate::Create(node->cert);
|
2013-08-29 16:15:35 -07:00
|
|
|
DisplayCertificateAlert(ctx, "NotImportingUnverifiedCert", certToShow, proofOfLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-11-06 09:30:45 -08:00
|
|
|
rv = ImportCertsIntoPermanentStorage(certChain, certUsageAnyCA, true);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsNSSCertificateDB::DisplayCertificateAlert(nsIInterfaceRequestor *ctx,
|
|
|
|
const char *stringID,
|
2013-08-29 16:15:35 -07:00
|
|
|
nsIX509Cert *certToShow,
|
|
|
|
const nsNSSShutDownPreventionLock &/*proofOfLock*/)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-26 10:18:21 -08:00
|
|
|
static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);
|
|
|
|
|
2011-11-02 21:02:48 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsNSSCertificateDB::DisplayCertificateAlert called off the main thread");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPSMUITracker tracker;
|
|
|
|
if (!tracker.isUIForbidden()) {
|
|
|
|
|
2011-11-02 21:02:48 -07:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> my_ctx = ctx;
|
|
|
|
if (!my_ctx)
|
|
|
|
my_ctx = new PipUIContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// This shall be replaced by embedding ovverridable prompts
|
|
|
|
// as discussed in bug 310446, and should make use of certToShow.
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsINSSComponent> nssComponent(do_GetService(kNSSComponentCID, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsAutoString tmpMessage;
|
|
|
|
nssComponent->GetPIPNSSBundleString(stringID, tmpMessage);
|
|
|
|
|
2011-11-02 21:02:48 -07:00
|
|
|
nsCOMPtr<nsIPrompt> prompt (do_GetInterface(my_ctx));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!prompt)
|
|
|
|
return;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
prompt->Alert(nullptr, tmpMessage.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsNSSCertificateDB::ImportUserCertificate(uint8_t *data, uint32_t length, nsIInterfaceRequestor *ctx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-02 21:02:48 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsNSSCertificateDB::ImportUserCertificate called off the main thread");
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2012-11-12 09:42:28 -08:00
|
|
|
ScopedPK11SlotInfo slot;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString nickname;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
int numCACerts;
|
|
|
|
SECItem *CACerts;
|
|
|
|
CERTDERCerts * collectArgs;
|
2012-10-10 23:39:28 -07:00
|
|
|
PLArenaPool *arena;
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!arena) {
|
2007-03-22 10:30:00 -07:00
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
2013-08-29 16:15:35 -07:00
|
|
|
collectArgs = getCertsFromPackage(arena, data, length, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!collectArgs) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = CERT_NewTempCertificate(CERT_GetDefaultCertDB(), collectArgs->rawCerts,
|
2012-10-17 13:48:36 -07:00
|
|
|
nullptr, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!cert) {
|
|
|
|
goto loser;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
slot = PK11_KeyForCertExists(cert.get(), nullptr, ctx);
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!slot) {
|
2014-01-22 17:13:19 -08:00
|
|
|
nsCOMPtr<nsIX509Cert> certToShow = nsNSSCertificate::Create(cert.get());
|
2013-08-29 16:15:35 -07:00
|
|
|
DisplayCertificateAlert(ctx, "UserCertIgnoredNoPrivateKey", certToShow, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto loser;
|
|
|
|
}
|
2012-11-12 09:42:28 -08:00
|
|
|
slot = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* pick a nickname for the cert */
|
|
|
|
if (cert->nickname) {
|
|
|
|
/* sigh, we need a call to look up other certs with this subject and
|
|
|
|
* identify nicknames from them. We can no longer walk down internal
|
|
|
|
* database structures rjr */
|
|
|
|
nickname = cert->nickname;
|
|
|
|
}
|
|
|
|
else {
|
2014-01-22 17:13:19 -08:00
|
|
|
get_default_nickname(cert.get(), ctx, nickname, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* user wants to import the cert */
|
2008-06-11 11:20:24 -07:00
|
|
|
{
|
|
|
|
char *cast_const_away = const_cast<char*>(nickname.get());
|
2014-01-22 17:13:19 -08:00
|
|
|
slot = PK11_ImportCertForKey(cert.get(), cast_const_away, ctx);
|
2008-06-11 11:20:24 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!slot) {
|
|
|
|
goto loser;
|
|
|
|
}
|
2012-11-12 09:42:28 -08:00
|
|
|
slot = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
{
|
2014-01-22 17:13:19 -08:00
|
|
|
nsCOMPtr<nsIX509Cert> certToShow = nsNSSCertificate::Create(cert.get());
|
2013-08-29 16:15:35 -07:00
|
|
|
DisplayCertificateAlert(ctx, "UserCertImported", certToShow, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
numCACerts = collectArgs->numcerts - 1;
|
|
|
|
if (numCACerts) {
|
|
|
|
CACerts = collectArgs->rawCerts+1;
|
2013-08-29 16:15:35 -07:00
|
|
|
rv = ImportValidCACerts(numCACerts, CACerts, ctx, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
loser:
|
|
|
|
if (arena) {
|
2011-10-17 07:59:28 -07:00
|
|
|
PORT_FreeArena(arena, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* void deleteCertificate(in nsIX509Cert aCert);
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::DeleteCertificate(nsIX509Cert *aCert)
|
|
|
|
{
|
2015-05-27 14:12:26 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aCert);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert(aCert->GetCert());
|
2014-07-03 22:09:24 -07:00
|
|
|
if (!cert) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
SECStatus srv = SECSuccess;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t certType;
|
2014-07-03 22:09:24 -07:00
|
|
|
aCert->GetCertType(&certType);
|
|
|
|
if (NS_FAILED(aCert->MarkForPermDeletion()))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cert->slot && certType != nsIX509Cert::USER_CERT) {
|
|
|
|
// To delete a cert of a slot (builtin, most likely), mark it as
|
|
|
|
// completely untrusted. This way we keep a copy cached in the
|
|
|
|
// local database, and next time we try to load it off of the
|
|
|
|
// external token/slot, we'll know not to trust it. We don't
|
|
|
|
// want to do that with user certs, because a user may re-store
|
|
|
|
// the cert onto the card again at which point we *will* want to
|
|
|
|
// trust that cert if it chains up properly.
|
|
|
|
nsNSSCertTrust trust(0, 0, 0);
|
|
|
|
srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(),
|
2014-01-22 17:13:19 -08:00
|
|
|
cert.get(), trust.GetTrust());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("cert deleted: %d", srv));
|
2007-03-22 10:30:00 -07:00
|
|
|
return (srv) ? NS_ERROR_FAILURE : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* void setCertTrust(in nsIX509Cert cert,
|
|
|
|
* in unsigned long type,
|
|
|
|
* in unsigned long trust);
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::SetCertTrust(nsIX509Cert *cert,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t type,
|
|
|
|
uint32_t trusted)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2015-05-27 14:12:26 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(cert);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNSSCertTrust trust;
|
2014-02-28 11:19:38 -08:00
|
|
|
nsresult rv;
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate nsscert(cert->GetCert());
|
2012-11-12 09:42:28 -08:00
|
|
|
|
2014-03-01 19:01:23 -08:00
|
|
|
rv = attemptToLogInWithDefaultPassword();
|
|
|
|
if (NS_WARN_IF(rv != NS_OK)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-28 11:19:38 -08:00
|
|
|
SECStatus srv;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (type == nsIX509Cert::CA_CERT) {
|
|
|
|
// always start with untrusted and move up
|
|
|
|
trust.SetValidCA();
|
2008-09-25 15:34:22 -07:00
|
|
|
trust.AddCATrust(!!(trusted & nsIX509CertDB::TRUSTED_SSL),
|
|
|
|
!!(trusted & nsIX509CertDB::TRUSTED_EMAIL),
|
|
|
|
!!(trusted & nsIX509CertDB::TRUSTED_OBJSIGN));
|
2007-03-22 10:30:00 -07:00
|
|
|
srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(),
|
2014-01-22 17:13:19 -08:00
|
|
|
nsscert.get(),
|
2007-03-22 10:30:00 -07:00
|
|
|
trust.GetTrust());
|
|
|
|
} else if (type == nsIX509Cert::SERVER_CERT) {
|
|
|
|
// always start with untrusted and move up
|
|
|
|
trust.SetValidPeer();
|
|
|
|
trust.AddPeerTrust(trusted & nsIX509CertDB::TRUSTED_SSL, 0, 0);
|
|
|
|
srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(),
|
2014-01-22 17:13:19 -08:00
|
|
|
nsscert.get(),
|
2007-03-22 10:30:00 -07:00
|
|
|
trust.GetTrust());
|
|
|
|
} else if (type == nsIX509Cert::EMAIL_CERT) {
|
|
|
|
// always start with untrusted and move up
|
|
|
|
trust.SetValidPeer();
|
2008-09-25 15:34:22 -07:00
|
|
|
trust.AddPeerTrust(0, !!(trusted & nsIX509CertDB::TRUSTED_EMAIL), 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(),
|
2014-01-22 17:13:19 -08:00
|
|
|
nsscert.get(),
|
2007-03-22 10:30:00 -07:00
|
|
|
trust.GetTrust());
|
|
|
|
} else {
|
|
|
|
// ignore user certs
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-02-28 11:19:38 -08:00
|
|
|
return MapSECStatus(srv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::IsCertTrusted(nsIX509Cert *cert,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t certType,
|
|
|
|
uint32_t trustType,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *_isTrusted)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(_isTrusted);
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
SECStatus srv;
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate nsscert(cert->GetCert());
|
2007-03-22 10:30:00 -07:00
|
|
|
CERTCertTrust nsstrust;
|
2014-01-22 17:13:19 -08:00
|
|
|
srv = CERT_GetCertTrust(nsscert.get(), &nsstrust);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (srv != SECSuccess)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsNSSCertTrust trust(&nsstrust);
|
|
|
|
if (certType == nsIX509Cert::CA_CERT) {
|
|
|
|
if (trustType & nsIX509CertDB::TRUSTED_SSL) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedCA(true, false, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_EMAIL) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedCA(false, true, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_OBJSIGN) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedCA(false, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else if (certType == nsIX509Cert::SERVER_CERT) {
|
|
|
|
if (trustType & nsIX509CertDB::TRUSTED_SSL) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(true, false, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_EMAIL) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, true, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_OBJSIGN) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else if (certType == nsIX509Cert::EMAIL_CERT) {
|
|
|
|
if (trustType & nsIX509CertDB::TRUSTED_SSL) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(true, false, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_EMAIL) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, true, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_OBJSIGN) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, false, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} /* user: ignore */
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ImportCertsFromFile(nsISupports *aToken,
|
2012-06-05 19:08:30 -07:00
|
|
|
nsIFile *aFile,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-29 16:15:35 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-07-23 03:48:51 -07:00
|
|
|
NS_ENSURE_ARG(aFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (aType) {
|
|
|
|
case nsIX509Cert::CA_CERT:
|
|
|
|
case nsIX509Cert::EMAIL_CERT:
|
|
|
|
case nsIX509Cert::SERVER_CERT:
|
|
|
|
// good
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// not supported (yet)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
PRFileDesc *fd = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rv = aFile->OpenNSPRFileDesc(PR_RDONLY, 0, &fd);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!fd)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRFileInfo file_info;
|
|
|
|
if (PR_SUCCESS != PR_GetOpenFileInfo(fd, &file_info))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
unsigned char *buf = new unsigned char[file_info.size];
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t bytes_obtained = PR_Read(fd, buf, file_info.size);
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_Close(fd);
|
|
|
|
|
|
|
|
if (bytes_obtained != file_info.size)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cxt = new PipUIContext();
|
|
|
|
|
|
|
|
switch (aType) {
|
|
|
|
case nsIX509Cert::CA_CERT:
|
|
|
|
rv = ImportCertificates(buf, bytes_obtained, aType, cxt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIX509Cert::SERVER_CERT:
|
|
|
|
rv = ImportServerCertificate(buf, bytes_obtained, cxt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIX509Cert::EMAIL_CERT:
|
|
|
|
rv = ImportEmailCertificate(buf, bytes_obtained, cxt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] buf;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ImportPKCS12File(nsISupports *aToken,
|
2012-06-05 19:08:30 -07:00
|
|
|
nsIFile *aFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-29 16:15:35 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG(aFile);
|
|
|
|
nsPKCS12Blob blob;
|
|
|
|
nsCOMPtr<nsIPK11Token> token = do_QueryInterface(aToken);
|
|
|
|
if (token) {
|
|
|
|
blob.SetToken(token);
|
|
|
|
}
|
|
|
|
return blob.ImportFromFile(aFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ExportPKCS12File(nsISupports *aToken,
|
2012-06-05 19:08:30 -07:00
|
|
|
nsIFile *aFile,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIX509Cert **certs)
|
2014-01-04 07:02:17 -08:00
|
|
|
//const char16_t **aCertNames)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_ARG(aFile);
|
|
|
|
nsPKCS12Blob blob;
|
|
|
|
if (count == 0) return NS_OK;
|
|
|
|
nsCOMPtr<nsIPK11Token> localRef;
|
|
|
|
if (!aToken) {
|
2012-11-12 09:42:28 -08:00
|
|
|
ScopedPK11SlotInfo keySlot(PK11_GetInternalKeySlot());
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(keySlot,"Failed to get the internal key slot");
|
|
|
|
localRef = new nsPK11Token(keySlot);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
localRef = do_QueryInterface(aToken);
|
|
|
|
}
|
|
|
|
blob.SetToken(localRef);
|
|
|
|
//blob.LoadCerts(aCertNames, count);
|
|
|
|
//return blob.ExportToFile(aFile);
|
|
|
|
return blob.ExportToFile(aFile, certs, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NSS Helper Routines (private to nsNSSCertificateDB)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DELIM '\001'
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GetSortedNameList
|
|
|
|
*
|
|
|
|
* Converts a CERTCertList to a list of certificate names
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
nsNSSCertificateDB::getCertNames(CERTCertList *certList,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t type,
|
|
|
|
uint32_t *_count,
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t ***_certNames,
|
2013-08-29 16:15:35 -07:00
|
|
|
const nsNSSShutDownPreventionLock &/*proofOfLock*/)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
CERTCertListNode *node;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t numcerts = 0, i=0;
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t **tmpArray = nullptr;
|
2013-08-29 16:15:35 -07:00
|
|
|
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("List of certs %d:\n", type));
|
2007-03-22 10:30:00 -07:00
|
|
|
for (node = CERT_LIST_HEAD(certList);
|
|
|
|
!CERT_LIST_END(node, certList);
|
|
|
|
node = CERT_LIST_NEXT(node)) {
|
|
|
|
if (getCertType(node->cert) == type) {
|
|
|
|
numcerts++;
|
|
|
|
}
|
|
|
|
}
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("num certs: %d\n", numcerts));
|
2007-03-22 10:30:00 -07:00
|
|
|
int nc = (numcerts == 0) ? 1 : numcerts;
|
2015-03-26 17:01:12 -07:00
|
|
|
tmpArray = (char16_t **)moz_xmalloc(sizeof(char16_t *) * nc);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (numcerts == 0) goto finish;
|
|
|
|
for (node = CERT_LIST_HEAD(certList);
|
|
|
|
!CERT_LIST_END(node, certList);
|
|
|
|
node = CERT_LIST_NEXT(node)) {
|
|
|
|
if (getCertType(node->cert) == type) {
|
2014-07-30 13:00:29 -07:00
|
|
|
RefPtr<nsNSSCertificate> pipCert(new nsNSSCertificate(node->cert));
|
2012-10-17 13:48:36 -07:00
|
|
|
char *dbkey = nullptr;
|
|
|
|
char *namestr = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString certstr;
|
2014-07-30 13:00:29 -07:00
|
|
|
pipCert->GetDbKey(&dbkey);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString keystr = NS_ConvertASCIItoUTF16(dbkey);
|
|
|
|
PR_FREEIF(dbkey);
|
|
|
|
if (type == nsIX509Cert::EMAIL_CERT) {
|
|
|
|
namestr = node->cert->emailAddr;
|
|
|
|
} else {
|
|
|
|
namestr = node->cert->nickname;
|
|
|
|
if (namestr) {
|
|
|
|
char *sc = strchr(namestr, ':');
|
|
|
|
if (sc) *sc = DELIM;
|
|
|
|
}
|
|
|
|
}
|
2009-08-18 08:48:32 -07:00
|
|
|
nsAutoString certname = NS_ConvertASCIItoUTF16(namestr ? namestr : "");
|
2014-01-04 07:02:17 -08:00
|
|
|
certstr.Append(char16_t(DELIM));
|
2007-03-22 10:30:00 -07:00
|
|
|
certstr += certname;
|
2014-01-04 07:02:17 -08:00
|
|
|
certstr.Append(char16_t(DELIM));
|
2007-03-22 10:30:00 -07:00
|
|
|
certstr += keystr;
|
|
|
|
tmpArray[i++] = ToNewUnicode(certstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finish:
|
|
|
|
*_count = numcerts;
|
|
|
|
*_certNames = tmpArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-03 22:09:24 -07:00
|
|
|
nsNSSCertificateDB::FindEmailEncryptionCert(const nsAString& aNickname,
|
|
|
|
nsIX509Cert** _retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-12 09:42:28 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aNickname.IsEmpty())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> ctx = new PipUIContext();
|
2012-10-17 13:48:36 -07:00
|
|
|
char *asciiname = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ConvertUTF16toUTF8 aUtf8Nickname(aNickname);
|
2007-07-08 00:08:04 -07:00
|
|
|
asciiname = const_cast<char*>(aUtf8Nickname.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Find a good cert in the user's database */
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert(CERT_FindUserCertByUsage(CERT_GetDefaultCertDB(),
|
|
|
|
asciiname,
|
|
|
|
certUsageEmailRecipient,
|
|
|
|
true, ctx));
|
2012-11-12 09:42:28 -08:00
|
|
|
if (!cert) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
nsCOMPtr<nsIX509Cert> nssCert = nsNSSCertificate::Create(cert.get());
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!nssCert) {
|
2012-11-12 09:42:28 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-11-12 09:42:28 -08:00
|
|
|
nssCert.forget(_retval);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-07-03 22:09:24 -07:00
|
|
|
nsNSSCertificateDB::FindEmailSigningCert(const nsAString& aNickname,
|
|
|
|
nsIX509Cert** _retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-12 09:42:28 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
*_retval = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aNickname.IsEmpty())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> ctx = new PipUIContext();
|
2012-10-17 13:48:36 -07:00
|
|
|
char *asciiname = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ConvertUTF16toUTF8 aUtf8Nickname(aNickname);
|
2007-07-08 00:08:04 -07:00
|
|
|
asciiname = const_cast<char*>(aUtf8Nickname.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Find a good cert in the user's database */
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert(CERT_FindUserCertByUsage(CERT_GetDefaultCertDB(),
|
|
|
|
asciiname,
|
|
|
|
certUsageEmailSigner,
|
|
|
|
true, ctx));
|
2012-11-12 09:42:28 -08:00
|
|
|
if (!cert) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
nsCOMPtr<nsIX509Cert> nssCert = nsNSSCertificate::Create(cert.get());
|
2012-10-17 13:48:36 -07:00
|
|
|
if (!nssCert) {
|
2012-11-12 09:42:28 -08:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-11-12 09:42:28 -08:00
|
|
|
nssCert.forget(_retval);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::FindCertByEmailAddress(nsISupports *aToken, const char *aEmailAddress, nsIX509Cert **_retval)
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-29 16:15:35 -07:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2011-06-28 05:26:41 -07:00
|
|
|
|
2014-01-19 14:05:40 -08:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
2012-10-27 00:11:35 -07:00
|
|
|
NS_ENSURE_TRUE(certVerifier, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2012-11-12 09:42:28 -08:00
|
|
|
ScopedCERTCertList certlist(
|
|
|
|
PK11_FindCertsFromEmailAddress(aEmailAddress, nullptr));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!certlist)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-06-28 05:26:41 -07:00
|
|
|
// certlist now contains certificates with the right email address,
|
|
|
|
// but they might not have the correct usage or might even be invalid
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (CERT_LIST_END(CERT_LIST_HEAD(certlist), certlist))
|
2011-06-28 05:26:41 -07:00
|
|
|
return NS_ERROR_FAILURE; // no certs found
|
|
|
|
|
|
|
|
CERTCertListNode *node;
|
|
|
|
// search for a valid certificate
|
|
|
|
for (node = CERT_LIST_HEAD(certlist);
|
|
|
|
!CERT_LIST_END(node, certlist);
|
|
|
|
node = CERT_LIST_NEXT(node)) {
|
|
|
|
|
2014-02-05 14:49:10 -08:00
|
|
|
SECStatus srv = certVerifier->VerifyCert(node->cert,
|
2012-10-27 00:11:35 -07:00
|
|
|
certificateUsageEmailRecipient,
|
2014-08-02 08:49:12 -07:00
|
|
|
mozilla::pkix::Now(),
|
|
|
|
nullptr /*XXX pinarg*/,
|
2014-02-05 14:49:10 -08:00
|
|
|
nullptr /*hostname*/);
|
2012-10-27 00:11:35 -07:00
|
|
|
if (srv == SECSuccess) {
|
|
|
|
break;
|
2011-06-28 05:26:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CERT_LIST_END(node, certlist)) {
|
|
|
|
// no valid cert found
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-06-28 05:26:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// node now contains the first valid certificate with correct usage
|
2015-07-01 10:10:53 -07:00
|
|
|
nsRefPtr<nsNSSCertificate> nssCert = nsNSSCertificate::Create(node->cert);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!nssCert)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2015-07-01 10:10:53 -07:00
|
|
|
nssCert.forget(_retval);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-03-27 11:21:08 -07:00
|
|
|
nsNSSCertificateDB::ConstructX509FromBase64(const char *base64,
|
|
|
|
nsIX509Cert **_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-11-04 11:22:37 -08:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2014-01-31 17:33:28 -08:00
|
|
|
if (NS_WARN_IF(!_retval)) {
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-03-27 11:21:08 -07:00
|
|
|
// sure would be nice to have a smart pointer class for PL_ allocations
|
|
|
|
// unfortunately, we cannot distinguish out-of-memory from bad-input here
|
2013-05-07 20:47:33 -07:00
|
|
|
uint32_t len = base64 ? strlen(base64) : 0;
|
2012-10-17 13:48:36 -07:00
|
|
|
char *certDER = PL_Base64Decode(base64, len, nullptr);
|
2011-03-27 11:21:08 -07:00
|
|
|
if (!certDER)
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
if (!*certDER) {
|
|
|
|
PL_strfree(certDER);
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get to this point, we know we had well-formed base64 input;
|
|
|
|
// therefore the input string cannot have been less than two
|
|
|
|
// characters long. Compute the unpadded length of the decoded data.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t lengthDER = (len * 3) / 4;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (base64[len-1] == '=') {
|
2011-03-27 11:21:08 -07:00
|
|
|
lengthDER--;
|
|
|
|
if (base64[len-2] == '=')
|
|
|
|
lengthDER--;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-31 17:33:28 -08:00
|
|
|
nsresult rv = ConstructX509(certDER, lengthDER, _retval);
|
|
|
|
PL_strfree(certDER);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ConstructX509(const char* certDER,
|
|
|
|
uint32_t lengthDER,
|
|
|
|
nsIX509Cert** _retval)
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!_retval)) {
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
2013-08-29 16:15:35 -07:00
|
|
|
|
2011-03-27 11:21:08 -07:00
|
|
|
SECItem secitem_cert;
|
|
|
|
secitem_cert.type = siDERCertBuffer;
|
|
|
|
secitem_cert.data = (unsigned char*)certDER;
|
|
|
|
secitem_cert.len = lengthDER;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate cert(CERT_NewTempCertificate(CERT_GetDefaultCertDB(),
|
|
|
|
&secitem_cert, nullptr,
|
|
|
|
false, true));
|
2011-03-27 11:21:08 -07:00
|
|
|
if (!cert)
|
|
|
|
return (PORT_GetError() == SEC_ERROR_NO_MEMORY)
|
|
|
|
? NS_ERROR_OUT_OF_MEMORY : NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
nsCOMPtr<nsIX509Cert> nssCert = nsNSSCertificate::Create(cert.get());
|
2012-11-12 09:42:28 -08:00
|
|
|
if (!nssCert) {
|
2011-03-27 11:21:08 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-11-12 09:42:28 -08:00
|
|
|
}
|
|
|
|
nssCert.forget(_retval);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-06-11 11:20:24 -07:00
|
|
|
void
|
|
|
|
nsNSSCertificateDB::get_default_nickname(CERTCertificate *cert,
|
|
|
|
nsIInterfaceRequestor* ctx,
|
2013-08-29 16:15:35 -07:00
|
|
|
nsCString &nickname,
|
|
|
|
const nsNSSShutDownPreventionLock &/*proofOfLock*/)
|
2008-06-11 11:20:24 -07:00
|
|
|
{
|
2013-11-26 10:18:21 -08:00
|
|
|
static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);
|
|
|
|
|
2008-06-11 11:20:24 -07:00
|
|
|
nickname.Truncate();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
CK_OBJECT_HANDLE keyHandle;
|
|
|
|
|
|
|
|
CERTCertDBHandle *defaultcertdb = CERT_GetDefaultCertDB();
|
|
|
|
nsCOMPtr<nsINSSComponent> nssComponent(do_GetService(kNSSComponentCID, &rv));
|
2008-06-11 11:20:24 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString username;
|
2008-06-11 11:20:24 -07:00
|
|
|
char *temp_un = CERT_GetCommonName(&cert->subject);
|
|
|
|
if (temp_un) {
|
|
|
|
username = temp_un;
|
|
|
|
PORT_Free(temp_un);
|
2012-07-30 07:20:58 -07:00
|
|
|
temp_un = nullptr;
|
2008-06-11 11:20:24 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString caname;
|
2008-06-11 11:20:24 -07:00
|
|
|
char *temp_ca = CERT_GetOrgName(&cert->issuer);
|
|
|
|
if (temp_ca) {
|
|
|
|
caname = temp_ca;
|
|
|
|
PORT_Free(temp_ca);
|
2012-07-30 07:20:58 -07:00
|
|
|
temp_ca = nullptr;
|
2008-06-11 11:20:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString tmpNickFmt;
|
2007-03-22 10:30:00 -07:00
|
|
|
nssComponent->GetPIPNSSBundleString("nick_template", tmpNickFmt);
|
2008-06-11 11:20:24 -07:00
|
|
|
NS_ConvertUTF16toUTF8 nickFmt(tmpNickFmt);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString baseName;
|
2008-06-11 11:20:24 -07:00
|
|
|
char *temp_nn = PR_smprintf(nickFmt.get(), username.get(), caname.get());
|
|
|
|
if (!temp_nn) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
baseName = temp_nn;
|
|
|
|
PR_smprintf_free(temp_nn);
|
2012-07-30 07:20:58 -07:00
|
|
|
temp_nn = nullptr;
|
2008-06-11 11:20:24 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-11 11:20:24 -07:00
|
|
|
nickname = baseName;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to see if the private key exists on a token, if it does
|
|
|
|
* then we need to check for nicknames that already exist on the smart
|
|
|
|
* card.
|
|
|
|
*/
|
2012-11-12 09:42:28 -08:00
|
|
|
ScopedPK11SlotInfo slot(PK11_KeyForCertExists(cert, &keyHandle, ctx));
|
2008-06-11 11:20:24 -07:00
|
|
|
if (!slot)
|
|
|
|
return;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!PK11_IsInternal(slot)) {
|
2008-06-11 11:20:24 -07:00
|
|
|
char *tmp = PR_smprintf("%s:%s", PK11_GetTokenName(slot), baseName.get());
|
|
|
|
if (!tmp) {
|
|
|
|
nickname.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
baseName = tmp;
|
|
|
|
PR_smprintf_free(tmp);
|
|
|
|
|
|
|
|
nickname = baseName;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-06-11 11:20:24 -07:00
|
|
|
|
|
|
|
int count = 1;
|
|
|
|
while (true) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if ( count > 1 ) {
|
2008-06-11 11:20:24 -07:00
|
|
|
char *tmp = PR_smprintf("%s #%d", baseName.get(), count);
|
|
|
|
if (!tmp) {
|
|
|
|
nickname.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nickname = tmp;
|
|
|
|
PR_smprintf_free(tmp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-06-11 11:20:24 -07:00
|
|
|
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate dummycert;
|
2008-06-11 11:20:24 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (PK11_IsInternal(slot)) {
|
|
|
|
/* look up the nickname to make sure it isn't in use already */
|
2008-06-11 11:20:24 -07:00
|
|
|
dummycert = CERT_FindCertByNickname(defaultcertdb, nickname.get());
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Check the cert against others that already live on the smart
|
|
|
|
* card.
|
|
|
|
*/
|
2008-06-11 11:20:24 -07:00
|
|
|
dummycert = PK11_FindCertFromNickname(nickname.get(), ctx);
|
2012-10-17 13:48:36 -07:00
|
|
|
if (dummycert) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Make sure the subject names are different.
|
|
|
|
*/
|
|
|
|
if (CERT_CompareName(&cert->subject, &dummycert->subject) == SECEqual)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* There is another certificate with the same nickname and
|
|
|
|
* the same subject name on the smart card, so let's use this
|
|
|
|
* nickname.
|
|
|
|
*/
|
2012-10-17 13:48:36 -07:00
|
|
|
dummycert = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 22:09:24 -07:00
|
|
|
if (!dummycert) {
|
2008-06-11 11:20:24 -07:00
|
|
|
break;
|
2014-07-03 22:09:24 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
count++;
|
2008-06-11 11:20:24 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-03 22:09:24 -07:00
|
|
|
NS_IMETHODIMP nsNSSCertificateDB::AddCertFromBase64(const char* aBase64,
|
|
|
|
const char* aTrust,
|
|
|
|
const char* aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aBase64);
|
|
|
|
nsCOMPtr <nsIX509Cert> newCert;
|
|
|
|
|
2013-08-29 16:15:35 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNSSCertTrust trust;
|
|
|
|
|
|
|
|
// need to calculate the trust bits from the aTrust string.
|
2012-07-27 07:03:25 -07:00
|
|
|
SECStatus stat = CERT_DecodeTrustString(trust.GetTrust(),
|
|
|
|
/* this is const, but not declared that way */(char *) aTrust);
|
|
|
|
NS_ENSURE_STATE(stat == SECSuccess); // if bad trust passed in, return error.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
2012-07-27 07:03:25 -07:00
|
|
|
nsresult rv = ConstructX509FromBase64(aBase64, getter_AddRefs(newCert));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
SECItem der;
|
2012-08-22 08:56:38 -07:00
|
|
|
rv = newCert->GetRawDER(&der.len, (uint8_t **)&der.data);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("Creating temp cert\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
CERTCertDBHandle *certdb = CERT_GetDefaultCertDB();
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate tmpCert(CERT_FindCertByDERCert(certdb, &der));
|
2014-02-28 11:19:38 -08:00
|
|
|
if (!tmpCert)
|
2007-03-22 10:30:00 -07:00
|
|
|
tmpCert = CERT_NewTempCertificate(certdb, &der,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, false, true);
|
2015-03-26 17:01:12 -07:00
|
|
|
free(der.data);
|
2012-07-30 07:20:58 -07:00
|
|
|
der.data = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
der.len = 0;
|
|
|
|
|
|
|
|
if (!tmpCert) {
|
2010-06-17 13:28:38 -07:00
|
|
|
NS_ERROR("Couldn't create cert from DER blob");
|
2014-02-28 11:19:38 -08:00
|
|
|
return MapSECStatus(SECFailure);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-10 02:38:00 -07:00
|
|
|
// If there's already a certificate that matches this one in the database,
|
|
|
|
// we still want to set its trust to the given value.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (tmpCert->isperm) {
|
2014-07-10 02:38:00 -07:00
|
|
|
return SetCertTrustFromString(newCert, aTrust);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsXPIDLCString nickname;
|
2014-01-22 17:13:19 -08:00
|
|
|
nickname.Adopt(CERT_MakeCANickname(tmpCert.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-06-03 15:25:57 -07:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("Created nick \"%s\"\n", nickname.get()));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-01 19:01:23 -08:00
|
|
|
rv = attemptToLogInWithDefaultPassword();
|
|
|
|
if (NS_WARN_IF(rv != NS_OK)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:13:19 -08:00
|
|
|
SECStatus srv = __CERT_AddTempCertToPerm(tmpCert.get(),
|
2012-11-02 12:34:46 -07:00
|
|
|
const_cast<char*>(nickname.get()),
|
|
|
|
trust.GetTrust());
|
2014-02-28 11:19:38 -08:00
|
|
|
return MapSECStatus(srv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 15:31:39 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::AddCert(const nsACString & aCertDER, const char *aTrust,
|
|
|
|
const char *aName)
|
|
|
|
{
|
|
|
|
nsCString base64;
|
|
|
|
nsresult rv = Base64Encode(aCertDER, base64);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return AddCertFromBase64(base64.get(), aTrust, aName);
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:08:06 -08:00
|
|
|
NS_IMETHODIMP
|
2014-07-03 22:09:24 -07:00
|
|
|
nsNSSCertificateDB::SetCertTrustFromString(nsIX509Cert* cert,
|
2014-02-22 19:08:06 -08:00
|
|
|
const char* trustString)
|
|
|
|
{
|
|
|
|
CERTCertTrust trust;
|
|
|
|
|
|
|
|
// need to calculate the trust bits from the aTrust string.
|
|
|
|
SECStatus srv = CERT_DecodeTrustString(&trust,
|
|
|
|
const_cast<char *>(trustString));
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(SECFailure);
|
|
|
|
}
|
2014-07-03 21:49:56 -07:00
|
|
|
ScopedCERTCertificate nssCert(cert->GetCert());
|
2014-03-01 19:01:23 -08:00
|
|
|
|
|
|
|
nsresult rv = attemptToLogInWithDefaultPassword();
|
|
|
|
if (NS_WARN_IF(rv != NS_OK)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-22 19:08:06 -08:00
|
|
|
srv = CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), nssCert.get(), &trust);
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::GetCerts(nsIX509CertList **_retval)
|
|
|
|
{
|
2013-08-29 16:15:35 -07:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-02-22 19:08:06 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> ctx = new PipUIContext();
|
|
|
|
nsCOMPtr<nsIX509CertList> nssCertList;
|
2014-07-06 15:55:38 -07:00
|
|
|
ScopedCERTCertList certList(PK11_ListCerts(PK11CertListUnique, ctx));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-17 13:48:36 -07:00
|
|
|
// nsNSSCertList 1) adopts certList, and 2) handles the nullptr case fine.
|
2007-03-22 10:30:00 -07:00
|
|
|
// (returns an empty list)
|
2013-11-11 15:16:45 -08:00
|
|
|
nssCertList = new nsNSSCertList(certList, locker);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-07-01 10:10:53 -07:00
|
|
|
nssCertList.forget(_retval);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-12-06 19:05:27 -08:00
|
|
|
|
2013-11-11 16:37:06 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::VerifyCertNow(nsIX509Cert* aCert,
|
|
|
|
int64_t /*SECCertificateUsage*/ aUsage,
|
2014-01-17 11:04:09 -08:00
|
|
|
uint32_t aFlags,
|
2015-05-07 11:06:07 -07:00
|
|
|
const char* aHostname,
|
|
|
|
nsIX509CertList** aVerifiedChain,
|
2013-11-11 16:37:06 -08:00
|
|
|
bool* aHasEVPolicy,
|
|
|
|
int32_t* /*PRErrorCode*/ _retval )
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCert);
|
|
|
|
NS_ENSURE_ARG_POINTER(aHasEVPolicy);
|
2015-05-07 11:06:07 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(aVerifiedChain);
|
2013-11-11 16:37:06 -08:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
2015-05-07 11:06:07 -07:00
|
|
|
*aVerifiedChain = nullptr;
|
2013-11-11 16:37:06 -08:00
|
|
|
*aHasEVPolicy = false;
|
|
|
|
*_retval = PR_UNKNOWN_ERROR;
|
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:15:53 -08:00
|
|
|
#ifndef MOZ_NO_EV_CERTS
|
2014-01-26 19:36:28 -08:00
|
|
|
EnsureIdentityInfoLoaded();
|
2013-11-11 16:37:06 -08:00
|
|
|
#endif
|
|
|
|
|
2014-07-03 22:09:24 -07:00
|
|
|
ScopedCERTCertificate nssCert(aCert->GetCert());
|
|
|
|
if (!nssCert) {
|
2013-11-11 16:37:06 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2014-01-19 14:05:40 -08:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
2013-11-11 16:37:06 -08:00
|
|
|
NS_ENSURE_TRUE(certVerifier, NS_ERROR_FAILURE);
|
|
|
|
|
2014-07-06 15:55:38 -07:00
|
|
|
ScopedCERTCertList resultChain;
|
2013-11-11 16:37:06 -08:00
|
|
|
SECOidTag evOidPolicy;
|
|
|
|
SECStatus srv;
|
|
|
|
|
2015-05-07 11:06:07 -07:00
|
|
|
if (aHostname && aUsage == certificateUsageSSLServer) {
|
|
|
|
srv = certVerifier->VerifySSLServerCert(nssCert,
|
|
|
|
nullptr, // stapledOCSPResponse
|
|
|
|
mozilla::pkix::Now(),
|
|
|
|
nullptr, // Assume no context
|
|
|
|
aHostname,
|
|
|
|
false, // don't save intermediates
|
|
|
|
aFlags,
|
|
|
|
&resultChain,
|
|
|
|
&evOidPolicy);
|
|
|
|
} else {
|
|
|
|
srv = certVerifier->VerifyCert(nssCert, aUsage, mozilla::pkix::Now(),
|
|
|
|
nullptr, // Assume no context
|
|
|
|
aHostname,
|
|
|
|
aFlags,
|
|
|
|
nullptr, // stapledOCSPResponse
|
|
|
|
&resultChain,
|
|
|
|
&evOidPolicy);
|
|
|
|
}
|
2013-11-11 16:37:06 -08:00
|
|
|
|
|
|
|
PRErrorCode error = PR_GetError();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIX509CertList> nssCertList;
|
|
|
|
// This adopts the list
|
|
|
|
nssCertList = new nsNSSCertList(resultChain, locker);
|
|
|
|
NS_ENSURE_TRUE(nssCertList, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (srv == SECSuccess) {
|
|
|
|
if (evOidPolicy != SEC_OID_UNKNOWN) {
|
|
|
|
*aHasEVPolicy = true;
|
|
|
|
}
|
|
|
|
*_retval = 0;
|
|
|
|
} else {
|
|
|
|
NS_ENSURE_TRUE(evOidPolicy == SEC_OID_UNKNOWN, NS_ERROR_FAILURE);
|
|
|
|
NS_ENSURE_TRUE(error != 0, NS_ERROR_FAILURE);
|
|
|
|
*_retval = error;
|
|
|
|
}
|
2015-05-07 11:06:07 -07:00
|
|
|
nssCertList.forget(aVerifiedChain);
|
2013-11-11 16:37:06 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-03-12 13:08:48 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ClearOCSPCache()
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
|
|
|
NS_ENSURE_TRUE(certVerifier, NS_ERROR_FAILURE);
|
2014-06-16 23:13:29 -07:00
|
|
|
certVerifier->ClearOCSPCache();
|
2014-03-12 13:08:48 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|