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/. */
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2013-04-16 03:45:11 +04:00
|
|
|
#include "nsNSSCertificateDB.h"
|
2012-10-27 11:11:35 +04:00
|
|
|
|
|
|
|
#include "CertVerifier.h"
|
2016-07-01 00:09:18 +03:00
|
|
|
#include "CryptoTask.h"
|
2014-01-27 07:36:28 +04:00
|
|
|
#include "ExtendedValidation.h"
|
2013-07-09 03:30:59 +04:00
|
|
|
#include "NSSCertDBTrustDomain.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "SharedSSLState.h"
|
2016-08-23 08:29:15 +03:00
|
|
|
#include "certdb.h"
|
2016-11-19 02:32:58 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2017-01-02 09:11:39 +03:00
|
|
|
#include "mozilla/Base64.h"
|
2016-05-19 07:20:56 +03:00
|
|
|
#include "mozilla/Casting.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-03-25 16:36:25 +03:00
|
|
|
#include "nsArray.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "nsArrayUtils.h"
|
2002-09-17 22:51:22 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2002-09-17 22:51:22 +04:00
|
|
|
#include "nsICertificateDialogs.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2006-04-12 19:43:32 +04:00
|
|
|
#include "nsIMutableArray.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "nsIObserverService.h"
|
2004-09-07 22:54:02 +04:00
|
|
|
#include "nsIPrefBranch.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "nsIPrefService.h"
|
2006-06-26 21:13:13 +04:00
|
|
|
#include "nsIPrompt.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "nsNSSCertHelper.h"
|
|
|
|
#include "nsNSSCertTrust.h"
|
|
|
|
#include "nsNSSCertificate.h"
|
|
|
|
#include "nsNSSComponent.h"
|
|
|
|
#include "nsNSSHelper.h"
|
|
|
|
#include "nsNSSShutDown.h"
|
|
|
|
#include "nsPKCS12Blob.h"
|
2016-08-23 08:29:15 +03:00
|
|
|
#include "nsPromiseFlatString.h"
|
2016-07-01 00:09:18 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2011-11-03 08:02:48 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2016-08-23 08:29:15 +03:00
|
|
|
#include "nspr.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "pkix/Time.h"
|
2016-10-10 10:44:41 +03:00
|
|
|
#include "pkix/pkixnss.h"
|
2016-03-16 13:54:26 +03:00
|
|
|
#include "pkix/pkixtypes.h"
|
2002-09-17 22:51:22 +04:00
|
|
|
#include "secasn1.h"
|
|
|
|
#include "secder.h"
|
2016-08-23 08:29:15 +03:00
|
|
|
#include "secerr.h"
|
2002-09-17 22:51:22 +04:00
|
|
|
#include "ssl.h"
|
|
|
|
|
2016-01-16 01:33:56 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <winsock.h> // for ntohl
|
|
|
|
#endif
|
|
|
|
|
2012-10-18 00:48:36 +04:00
|
|
|
using namespace mozilla;
|
2012-10-27 11:11:35 +04:00
|
|
|
using namespace mozilla::psm;
|
2012-12-08 02:50:43 +04:00
|
|
|
using mozilla::psm::SharedSSLState;
|
2012-10-18 00:48:36 +04:00
|
|
|
|
2016-01-28 21:36:00 +03:00
|
|
|
extern LazyLogModule gPIPNSSLog;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2014-07-04 09:09:24 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsNSSCertificateDB, nsIX509CertDB)
|
2002-09-17 22:51:22 +04:00
|
|
|
|
|
|
|
nsNSSCertificateDB::~nsNSSCertificateDB()
|
|
|
|
{
|
2013-12-19 23:45:50 +04:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-20 18:00:19 +03:00
|
|
|
shutdown(ShutdownCalledFrom::Object);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-01-30 11:20:58 +03:00
|
|
|
NS_IMETHODIMP
|
2017-02-09 18:11:29 +03:00
|
|
|
nsNSSCertificateDB::FindCertByDBKey(const nsACString& aDBKey,
|
|
|
|
/*out*/ nsIX509Cert** _cert)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2012-11-12 21:42:28 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(_cert);
|
|
|
|
*_cert = nullptr;
|
|
|
|
|
2017-02-09 18:11:29 +03:00
|
|
|
if (aDBKey.IsEmpty()) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2017-06-09 02:10:00 +03:00
|
|
|
nsresult rv = BlockUntilLoadableRootsLoaded();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-01-13 02:39:43 +03:00
|
|
|
UniqueCERTCertificate cert;
|
2017-06-09 02:10:00 +03:00
|
|
|
rv = FindCertByDBKey(aDBKey, cert);
|
2016-01-13 02:39:43 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// If we can't find the certificate, that's not an error. Just return null.
|
|
|
|
if (!cert) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIX509Cert> nssCert = nsNSSCertificate::Create(cert.get());
|
|
|
|
if (!nssCert) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
nssCert.forget(_cert);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-02-09 18:11:29 +03:00
|
|
|
nsNSSCertificateDB::FindCertByDBKey(const nsACString& aDBKey,
|
2016-01-13 02:39:43 +03:00
|
|
|
UniqueCERTCertificate& cert)
|
|
|
|
{
|
2016-01-16 01:33:56 +03:00
|
|
|
static_assert(sizeof(uint64_t) == 8, "type size sanity check");
|
|
|
|
static_assert(sizeof(uint32_t) == 4, "type size sanity check");
|
|
|
|
// (From nsNSSCertificate::GetDbKey)
|
|
|
|
// The format of the key is the base64 encoding of the following:
|
|
|
|
// 4 bytes: {0, 0, 0, 0} (this was intended to be the module ID, but it was
|
|
|
|
// never implemented)
|
|
|
|
// 4 bytes: {0, 0, 0, 0} (this was intended to be the slot ID, but it was
|
|
|
|
// never implemented)
|
|
|
|
// 4 bytes: <serial number length in big-endian order>
|
|
|
|
// 4 bytes: <DER-encoded issuer distinguished name length in big-endian order>
|
|
|
|
// n bytes: <bytes of serial number>
|
|
|
|
// m bytes: <DER-encoded issuer distinguished name>
|
|
|
|
nsAutoCString decoded;
|
2016-01-13 02:39:43 +03:00
|
|
|
nsAutoCString tmpDBKey(aDBKey);
|
2016-01-16 01:33:56 +03:00
|
|
|
// Filter out any whitespace for backwards compatibility.
|
|
|
|
tmpDBKey.StripWhitespace();
|
|
|
|
nsresult rv = Base64Decode(tmpDBKey, decoded);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2007-09-18 00:46:27 +04:00
|
|
|
}
|
2016-01-16 01:33:56 +03:00
|
|
|
if (decoded.Length() < 16) {
|
|
|
|
return NS_ERROR_ILLEGAL_INPUT;
|
|
|
|
}
|
|
|
|
const char* reader = decoded.BeginReading();
|
2016-05-19 07:20:56 +03:00
|
|
|
uint64_t zeroes = *BitwiseCast<const uint64_t*, const char*>(reader);
|
2016-01-16 01:33:56 +03:00
|
|
|
if (zeroes != 0) {
|
|
|
|
return NS_ERROR_ILLEGAL_INPUT;
|
|
|
|
}
|
|
|
|
reader += sizeof(uint64_t);
|
2016-05-19 07:20:56 +03:00
|
|
|
// Note: We surround the ntohl() argument with parentheses to stop the macro
|
|
|
|
// from thinking two arguments were passed.
|
|
|
|
uint32_t serialNumberLen = ntohl(
|
|
|
|
(*BitwiseCast<const uint32_t*, const char*>(reader)));
|
2016-01-16 01:33:56 +03:00
|
|
|
reader += sizeof(uint32_t);
|
2016-05-19 07:20:56 +03:00
|
|
|
uint32_t issuerLen = ntohl(
|
|
|
|
(*BitwiseCast<const uint32_t*, const char*>(reader)));
|
2016-01-16 01:33:56 +03:00
|
|
|
reader += sizeof(uint32_t);
|
|
|
|
if (decoded.Length() != 16ULL + serialNumberLen + issuerLen) {
|
|
|
|
return NS_ERROR_ILLEGAL_INPUT;
|
|
|
|
}
|
|
|
|
CERTIssuerAndSN issuerSN;
|
|
|
|
issuerSN.serialNumber.len = serialNumberLen;
|
2016-05-19 07:20:56 +03:00
|
|
|
issuerSN.serialNumber.data = BitwiseCast<unsigned char*, const char*>(reader);
|
2016-01-16 01:33:56 +03:00
|
|
|
reader += serialNumberLen;
|
|
|
|
issuerSN.derIssuer.len = issuerLen;
|
2016-05-19 07:20:56 +03:00
|
|
|
issuerSN.derIssuer.data = BitwiseCast<unsigned char*, const char*>(reader);
|
2016-01-16 01:33:56 +03:00
|
|
|
reader += issuerLen;
|
|
|
|
MOZ_ASSERT(reader == decoded.EndReading());
|
|
|
|
|
2016-01-13 02:39:43 +03:00
|
|
|
cert.reset(CERT_FindCertByIssuerAndSN(CERT_GetDefaultCertDB(), &issuerSN));
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-25 20:18:38 +04:00
|
|
|
SECStatus
|
2002-09-17 22:51:22 +04: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-18 00:48:36 +04:00
|
|
|
if (!collectArgs->rawCerts)
|
2002-09-17 22:51:22 +04: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*
|
2016-03-16 13:54:26 +03:00
|
|
|
nsNSSCertificateDB::getCertsFromPackage(const UniquePLArenaPool& arena,
|
|
|
|
uint8_t* data, uint32_t length,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2016-05-19 04:58:40 +03:00
|
|
|
CERTDERCerts* collectArgs = PORT_ArenaZNew(arena.get(), CERTDERCerts);
|
2016-03-16 13:54:26 +03:00
|
|
|
if (!collectArgs) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2016-03-16 13:54:26 +03:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
collectArgs->arena = arena.get();
|
2016-09-08 15:46:26 +03:00
|
|
|
if (CERT_DecodeCertPackage(BitwiseCast<char*, uint8_t*>(data), length,
|
|
|
|
collect_certs, collectArgs) != SECSuccess) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2016-03-16 13:54:26 +03:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
|
|
|
return collectArgs;
|
|
|
|
}
|
|
|
|
|
2017-07-22 02:58:42 +03:00
|
|
|
// When using the sql-backed softoken, trust settings are authenticated using a
|
|
|
|
// key in the secret database. Thus, if the user has a password, we need to
|
|
|
|
// authenticate to the token in order to be able to change trust settings.
|
|
|
|
SECStatus
|
|
|
|
ChangeCertTrustWithPossibleAuthentication(const UniqueCERTCertificate& cert,
|
|
|
|
CERTCertTrust& trust, void* ctx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(cert, "cert must be non-null");
|
|
|
|
if (!cert) {
|
|
|
|
PR_SetError(SEC_ERROR_LIBRARY_FAILURE, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
// NSS ignores the first argument to CERT_ChangeCertTrust
|
|
|
|
SECStatus srv = CERT_ChangeCertTrust(nullptr, cert.get(), &trust);
|
|
|
|
if (srv == SECSuccess || PR_GetError() != SEC_ERROR_TOKEN_NOT_LOGGED_IN) {
|
|
|
|
return srv;
|
|
|
|
}
|
|
|
|
if (cert->slot) {
|
|
|
|
// If this certificate is on an external PKCS#11 token, we have to
|
|
|
|
// authenticate to that token.
|
|
|
|
srv = PK11_Authenticate(cert->slot, PR_TRUE, ctx);
|
|
|
|
} else {
|
|
|
|
// Otherwise, the certificate is on the internal module.
|
|
|
|
UniquePK11SlotInfo internalSlot(PK11_GetInternalKeySlot());
|
|
|
|
srv = PK11_Authenticate(internalSlot.get(), PR_TRUE, ctx);
|
|
|
|
}
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return srv;
|
|
|
|
}
|
|
|
|
return CERT_ChangeCertTrust(nullptr, cert.get(), &trust);
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:42:20 +03:00
|
|
|
static nsresult
|
|
|
|
ImportCertsIntoPermanentStorage(const UniqueCERTCertList& certChain,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/)
|
|
|
|
{
|
|
|
|
bool encounteredFailure = false;
|
|
|
|
PRErrorCode savedErrorCode = 0;
|
|
|
|
UniquePK11SlotInfo slot(PK11_GetInternalKeySlot());
|
|
|
|
for (CERTCertListNode* chainNode = CERT_LIST_HEAD(certChain);
|
|
|
|
!CERT_LIST_END(chainNode, certChain);
|
|
|
|
chainNode = CERT_LIST_NEXT(chainNode)) {
|
|
|
|
UniquePORTString nickname(CERT_MakeCANickname(chainNode->cert));
|
|
|
|
SECStatus srv = PK11_ImportCert(slot.get(), chainNode->cert,
|
|
|
|
CK_INVALID_HANDLE, nickname.get(),
|
|
|
|
false); // this parameter is ignored by NSS
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
encounteredFailure = true;
|
|
|
|
savedErrorCode = PR_GetError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encounteredFailure) {
|
|
|
|
return GetXPCOMFromNSSError(savedErrorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
nsresult
|
2016-07-05 12:59:18 +03:00
|
|
|
nsNSSCertificateDB::handleCACertDownload(NotNull<nsIArray*> x509Certs,
|
2013-08-30 03:15:35 +04:00
|
|
|
nsIInterfaceRequestor *ctx,
|
|
|
|
const nsNSSShutDownPreventionLock &proofOfLock)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2017-07-06 15:00:35 +03: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
|
2002-09-17 22:51:22 +04:00
|
|
|
// 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
|
2017-07-06 15:00:35 +03:00
|
|
|
// list that is a hierarchy whose root is either the first or
|
|
|
|
// the last cert. What we're gonna do is compare the first
|
2005-09-27 02:43:22 +04:00
|
|
|
// 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.
|
2003-01-18 17:03:00 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numCerts;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2002-10-14 18:49:00 +04:00
|
|
|
x509Certs->GetLength(&numCerts);
|
2017-01-02 09:11:39 +03:00
|
|
|
MOZ_ASSERT(numCerts > 0, "Didn't get any certs to import.");
|
2002-09-17 22:51:22 +04:00
|
|
|
if (numCerts == 0)
|
|
|
|
return NS_OK; // Nothing to import, so nothing to do.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIX509Cert> certToShow;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t selCertIndex;
|
2002-09-17 22:51:22 +04:00
|
|
|
if (numCerts == 1) {
|
|
|
|
// There's only one cert, so let's show it.
|
|
|
|
selCertIndex = 0;
|
2002-10-14 18:49:00 +04:00
|
|
|
certToShow = do_QueryElementAt(x509Certs, selCertIndex);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else {
|
2005-09-27 02:43:22 +04:00
|
|
|
nsCOMPtr<nsIX509Cert> cert0; // first cert
|
|
|
|
nsCOMPtr<nsIX509Cert> cert1; // second cert
|
|
|
|
nsCOMPtr<nsIX509Cert> certn_2; // second to last cert
|
|
|
|
nsCOMPtr<nsIX509Cert> certn_1; // last cert
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2002-10-14 18:49:00 +04:00
|
|
|
cert0 = do_QueryElementAt(x509Certs, 0);
|
|
|
|
cert1 = do_QueryElementAt(x509Certs, 1);
|
2005-09-27 02:43:22 +04:00
|
|
|
certn_2 = do_QueryElementAt(x509Certs, numCerts-2);
|
|
|
|
certn_1 = do_QueryElementAt(x509Certs, numCerts-1);
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2017-08-08 09:07:55 +03:00
|
|
|
nsAutoString cert0SubjectName;
|
|
|
|
nsAutoString cert1IssuerName;
|
|
|
|
nsAutoString certn_2IssuerName;
|
|
|
|
nsAutoString certn_1SubjectName;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2002-09-24 00:17:16 +04:00
|
|
|
cert0->GetSubjectName(cert0SubjectName);
|
|
|
|
cert1->GetIssuerName(cert1IssuerName);
|
2005-09-27 02:43:22 +04:00
|
|
|
certn_2->GetIssuerName(certn_2IssuerName);
|
|
|
|
certn_1->GetSubjectName(certn_1SubjectName);
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2004-01-31 10:09:21 +03:00
|
|
|
if (cert1IssuerName.Equals(cert0SubjectName)) {
|
2002-09-17 22:51:22 +04:00
|
|
|
// In this case, the first cert in the list signed the second,
|
2017-07-06 15:00:35 +03:00
|
|
|
// so the first cert is the root. Let's display it.
|
2002-09-17 22:51:22 +04:00
|
|
|
selCertIndex = 0;
|
|
|
|
certToShow = cert0;
|
2017-07-06 15:00:35 +03:00
|
|
|
} else
|
|
|
|
if (certn_2IssuerName.Equals(certn_1SubjectName)) {
|
2005-09-27 02:43:22 +04:00
|
|
|
// 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;
|
2002-09-17 22:51:22 +04:00
|
|
|
} else {
|
2017-07-06 15:00:35 +03:00
|
|
|
// It's not a chain, so let's just show the first one in the
|
2002-09-17 22:51:22 +04:00
|
|
|
// downloaded list.
|
|
|
|
selCertIndex = 0;
|
|
|
|
certToShow = cert0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!certToShow)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICertificateDialogs> dialogs;
|
2017-07-06 15:00:35 +03:00
|
|
|
nsresult rv = ::getNSSDialogs(getter_AddRefs(dialogs),
|
2002-09-17 22:51:22 +04:00
|
|
|
NS_GET_IID(nsICertificateDialogs),
|
|
|
|
NS_CERTIFICATEDIALOGS_CONTRACTID);
|
2016-07-05 12:59:18 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2002-09-17 22:51:22 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2016-04-20 11:14:22 +03:00
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
UniqueCERTCertificate tmpCert(certToShow->GetCert());
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!tmpCert) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-01-23 05:13:19 +04:00
|
|
|
if (!CERT_IsCACert(tmpCert.get(), nullptr)) {
|
2013-08-30 03:15:35 +04:00
|
|
|
DisplayCertificateAlert(ctx, "NotACACert", certToShow, proofOfLock);
|
2005-09-27 02:43:22 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
if (tmpCert->isperm) {
|
2013-08-30 03:15:35 +04:00
|
|
|
DisplayCertificateAlert(ctx, "CaCertExists", certToShow, proofOfLock);
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t trustBits;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool allows;
|
2002-11-14 03:50:02 +03:00
|
|
|
rv = dialogs->ConfirmDownloadCACert(ctx, certToShow, &trustBits, &allows);
|
2002-09-17 22:51:22 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2002-11-14 03:50:02 +03:00
|
|
|
if (!allows)
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("trust is %d\n", trustBits));
|
2016-06-30 04:42:37 +03:00
|
|
|
UniquePORTString nickname(CERT_MakeCANickname(tmpCert.get()));
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("Created nick \"%s\"\n", nickname.get()));
|
2002-09-17 22:51:22 +04:00
|
|
|
|
|
|
|
nsNSSCertTrust trust;
|
|
|
|
trust.SetValidCA();
|
2008-09-26 02:34:22 +04:00
|
|
|
trust.AddCATrust(!!(trustBits & nsIX509CertDB::TRUSTED_SSL),
|
|
|
|
!!(trustBits & nsIX509CertDB::TRUSTED_EMAIL),
|
|
|
|
!!(trustBits & nsIX509CertDB::TRUSTED_OBJSIGN));
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2017-01-07 03:29:12 +03:00
|
|
|
UniquePK11SlotInfo slot(PK11_GetInternalKeySlot());
|
|
|
|
SECStatus srv = PK11_ImportCert(slot.get(), tmpCert.get(), CK_INVALID_HANDLE,
|
|
|
|
nickname.get(),
|
|
|
|
false); // this parameter is ignored by NSS
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
2017-07-22 02:58:42 +03:00
|
|
|
srv = ChangeCertTrustWithPossibleAuthentication(tmpCert, trust.GetTrust(),
|
|
|
|
ctx);
|
2017-01-07 03:29:12 +03:00
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
2016-07-05 12:59:18 +03:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2004-07-28 04:31:37 +04:00
|
|
|
// Import additional delivered certificates that can be verified.
|
|
|
|
|
|
|
|
// build a CertList for filtering
|
2016-03-16 13:54:26 +03:00
|
|
|
UniqueCERTCertList certList(CERT_NewCertList());
|
2012-10-18 00:48:36 +04:00
|
|
|
if (!certList) {
|
2004-07-28 04:31:37 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all remaining certs into temp store
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i=0; i<numCerts; i++) {
|
2004-07-28 04:31:37 +04:00
|
|
|
if (i == selCertIndex) {
|
|
|
|
// we already processed that one
|
2002-09-17 22:51:22 +04:00
|
|
|
continue;
|
2004-07-28 04:31:37 +04:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
nsCOMPtr<nsIX509Cert> remainingCert = do_QueryElementAt(x509Certs, i);
|
|
|
|
if (!remainingCert) {
|
|
|
|
continue;
|
|
|
|
}
|
2006-02-22 23:36:53 +03:00
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
UniqueCERTCertificate tmpCert2(remainingCert->GetCert());
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!tmpCert2) {
|
|
|
|
continue; // Let's try to import the rest of 'em
|
|
|
|
}
|
2016-03-16 13:54:26 +03:00
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
if (CERT_AddCertToListTail(certList.get(), tmpCert2.get()) != SECSuccess) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Unused << tmpCert2.release();
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2004-07-28 04:31:37 +04:00
|
|
|
|
2017-09-23 01:42:20 +03:00
|
|
|
return ImportCertsIntoPermanentStorage(certList, proofOfLock);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ImportCertificates(uint8_t* data, uint32_t length,
|
|
|
|
uint32_t type,
|
|
|
|
nsIInterfaceRequestor* ctx)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
// We currently only handle CA certificates.
|
|
|
|
if (type != nsIX509Cert::CA_CERT) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
UniquePLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2016-03-16 13:54:26 +03:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
CERTDERCerts* certCollection = getCertsFromPackage(arena, data, length,
|
|
|
|
locker);
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!certCollection) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-03-16 13:54:26 +03:00
|
|
|
|
2016-03-25 16:36:25 +03:00
|
|
|
nsCOMPtr<nsIMutableArray> array = nsArrayBase::Create();
|
|
|
|
if (!array) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now let's create some certs to work with
|
2016-03-16 13:54:26 +03:00
|
|
|
for (int i = 0; i < certCollection->numcerts; i++) {
|
|
|
|
SECItem* currItem = &certCollection->rawCerts[i];
|
2016-05-19 07:20:56 +03:00
|
|
|
nsCOMPtr<nsIX509Cert> cert = nsNSSCertificate::ConstructFromDER(
|
|
|
|
BitwiseCast<char*, unsigned char*>(currItem->data), currItem->len);
|
2016-03-16 13:54:26 +03:00
|
|
|
if (!cert) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-03-25 16:36:25 +03:00
|
|
|
nsresult rv = array->AppendElement(cert, false);
|
2016-03-16 13:54:26 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
return handleCACertDownload(WrapNotNull(array), ctx, locker);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
/**
|
2017-09-23 01:42:20 +03:00
|
|
|
* Decodes a given array of DER-encoded certificates into temporary storage.
|
2016-03-16 13:54:26 +03:00
|
|
|
*
|
|
|
|
* @param numcerts
|
|
|
|
* Size of the |certs| array.
|
|
|
|
* @param certs
|
2017-09-23 01:42:20 +03:00
|
|
|
* Pointer to array of certs to decode.
|
|
|
|
* @param temporaryCerts
|
|
|
|
* List of decoded certificates.
|
2016-03-16 13:54:26 +03:00
|
|
|
*/
|
|
|
|
static nsresult
|
|
|
|
ImportCertsIntoTempStorage(int numcerts, SECItem* certs,
|
|
|
|
const nsNSSShutDownPreventionLock& /*proofOfLock*/,
|
2017-09-23 01:42:20 +03:00
|
|
|
/*out*/ const UniqueCERTCertList& temporaryCerts)
|
2012-10-27 11:11:35 +04:00
|
|
|
{
|
2016-03-16 13:54:26 +03:00
|
|
|
NS_ENSURE_ARG_MIN(numcerts, 1);
|
|
|
|
NS_ENSURE_ARG_POINTER(certs);
|
2017-09-23 01:42:20 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(temporaryCerts);
|
2016-03-16 13:54:26 +03:00
|
|
|
|
|
|
|
// CERT_ImportCerts() expects an array of *pointers* to SECItems, so we have
|
|
|
|
// to convert |certs| to such a format first.
|
|
|
|
SECItem** ptrArray =
|
|
|
|
static_cast<SECItem**>(PORT_Alloc(sizeof(SECItem*) * numcerts));
|
|
|
|
if (!ptrArray) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < numcerts; i++) {
|
|
|
|
ptrArray[i] = &certs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
CERTCertificate** importedCerts = nullptr;
|
2017-09-23 01:42:20 +03:00
|
|
|
SECStatus srv = CERT_ImportCerts(CERT_GetDefaultCertDB(),
|
|
|
|
certUsageAnyCA, // this argument is ignored
|
2016-03-16 13:54:26 +03:00
|
|
|
numcerts, ptrArray, &importedCerts, false,
|
2017-09-23 01:42:20 +03:00
|
|
|
false, // this argument is ignored
|
|
|
|
nullptr);
|
2016-03-16 13:54:26 +03:00
|
|
|
PORT_Free(ptrArray);
|
|
|
|
ptrArray = nullptr;
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < numcerts; i++) {
|
|
|
|
if (!importedCerts[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniqueCERTCertificate cert(CERT_DupCertificate(importedCerts[i]));
|
|
|
|
if (!cert) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:42:20 +03:00
|
|
|
if (CERT_AddCertToListTail(temporaryCerts.get(), cert.get()) == SECSuccess) {
|
2016-03-16 13:54:26 +03:00
|
|
|
Unused << cert.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CERT_DestroyCertArray(importedCerts, numcerts);
|
2012-10-27 11:11:35 +04:00
|
|
|
|
2017-01-07 03:29:12 +03:00
|
|
|
return NS_OK;
|
2016-03-16 13:54:26 +03:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-03-16 13:54:26 +03:00
|
|
|
nsNSSCertificateDB::ImportEmailCertificate(uint8_t* data, uint32_t length,
|
|
|
|
nsIInterfaceRequestor* ctx)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
UniquePLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
|
|
|
if (!arena) {
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2016-03-16 13:54:26 +03:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2013-08-30 03:15:35 +04:00
|
|
|
CERTDERCerts *certCollection = getCertsFromPackage(arena, data, length, locker);
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!certCollection) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2004-07-28 04:31:37 +04:00
|
|
|
|
2017-09-23 01:42:20 +03:00
|
|
|
UniqueCERTCertList temporaryCerts(CERT_NewCertList());
|
|
|
|
if (!temporaryCerts) {
|
2016-03-16 13:54:26 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2004-07-28 04:31:37 +04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
nsresult rv = ImportCertsIntoTempStorage(certCollection->numcerts,
|
|
|
|
certCollection->rawCerts,
|
2017-09-23 01:42:20 +03:00
|
|
|
locker, temporaryCerts);
|
2016-03-16 13:54:26 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2017-09-23 01:42:20 +03:00
|
|
|
return ImportCertsIntoPermanentStorage(temporaryCerts, locker);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2004-07-28 04:31:37 +04:00
|
|
|
nsresult
|
2017-09-23 01:42:20 +03:00
|
|
|
nsNSSCertificateDB::ImportCACerts(int numCACerts, SECItem* caCerts,
|
|
|
|
nsIInterfaceRequestor* ctx,
|
|
|
|
const nsNSSShutDownPreventionLock& proofOfLock)
|
2004-07-28 04:31:37 +04:00
|
|
|
{
|
2017-09-23 01:42:20 +03:00
|
|
|
UniqueCERTCertList temporaryCerts(CERT_NewCertList());
|
|
|
|
if (!temporaryCerts) {
|
2004-07-28 04:31:37 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-09-23 01:42:20 +03:00
|
|
|
nsresult rv = ImportCertsIntoTempStorage(numCACerts, caCerts, proofOfLock,
|
|
|
|
temporaryCerts);
|
2016-03-16 13:54:26 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2004-07-28 04:31:37 +04:00
|
|
|
}
|
|
|
|
|
2017-09-23 01:42:20 +03:00
|
|
|
return ImportCertsIntoPermanentStorage(temporaryCerts, proofOfLock);
|
2004-07-28 04:31:37 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
void nsNSSCertificateDB::DisplayCertificateAlert(nsIInterfaceRequestor *ctx,
|
|
|
|
const char *stringID,
|
2013-08-30 03:15:35 +04:00
|
|
|
nsIX509Cert *certToShow,
|
|
|
|
const nsNSSShutDownPreventionLock &/*proofOfLock*/)
|
2006-06-26 21:13:13 +04:00
|
|
|
{
|
2013-11-26 22:18:21 +04:00
|
|
|
static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);
|
|
|
|
|
2011-11-03 08:02:48 +04:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsNSSCertificateDB::DisplayCertificateAlert called off the main thread");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
nsCOMPtr<nsIInterfaceRequestor> my_ctx = ctx;
|
|
|
|
if (!my_ctx) {
|
|
|
|
my_ctx = new PipUIContext();
|
|
|
|
}
|
2006-12-06 19:31:32 +03: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
|
|
|
// This shall be replaced by embedding ovverridable prompts
|
|
|
|
// as discussed in bug 310446, and should make use of certToShow.
|
2006-06-26 21:13:13 +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
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsINSSComponent> nssComponent(do_GetService(kNSSComponentCID, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsAutoString tmpMessage;
|
|
|
|
nssComponent->GetPIPNSSBundleString(stringID, tmpMessage);
|
2006-06-26 21:13:13 +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
|
|
|
nsCOMPtr<nsIPrompt> prompt (do_GetInterface(my_ctx));
|
|
|
|
if (!prompt) {
|
|
|
|
return;
|
2006-06-26 21:13:13 +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
|
|
|
|
|
|
|
prompt->Alert(nullptr, tmpMessage.get());
|
2006-06-26 21:13:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ImportUserCertificate(uint8_t* data, uint32_t length,
|
|
|
|
nsIInterfaceRequestor* ctx)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2011-11-03 08:02:48 +04:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_ERROR("nsNSSCertificateDB::ImportUserCertificate called off the main thread");
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
2016-03-16 13:54:26 +03:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
UniquePLArenaPool arena(PORT_NewArena(DER_DEFAULT_CHUNKSIZE));
|
2012-10-18 00:48:36 +04:00
|
|
|
if (!arena) {
|
2016-03-16 13:54:26 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
CERTDERCerts* collectArgs = getCertsFromPackage(arena, data, length, locker);
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!collectArgs) {
|
2016-03-16 13:54:26 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
UniqueCERTCertificate cert(
|
|
|
|
CERT_NewTempCertificate(CERT_GetDefaultCertDB(), collectArgs->rawCerts,
|
|
|
|
nullptr, false, true));
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!cert) {
|
2016-03-16 13:54:26 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-03-16 13:54:26 +03:00
|
|
|
UniquePK11SlotInfo slot(PK11_KeyForCertExists(cert.get(), nullptr, ctx));
|
2012-10-18 00:48:36 +04:00
|
|
|
if (!slot) {
|
2014-01-23 05:13:19 +04:00
|
|
|
nsCOMPtr<nsIX509Cert> certToShow = nsNSSCertificate::Create(cert.get());
|
2013-08-30 03:15:35 +04:00
|
|
|
DisplayCertificateAlert(ctx, "UserCertIgnoredNoPrivateKey", certToShow, locker);
|
2016-03-16 13:54:26 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2012-11-12 21:42:28 +04:00
|
|
|
slot = nullptr;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
|
|
|
/* pick a nickname for the cert */
|
2016-03-16 13:54:26 +03:00
|
|
|
nsAutoCString nickname;
|
2002-09-17 22:51:22 +04:00
|
|
|
if (cert->nickname) {
|
2016-03-16 13:54:26 +03:00
|
|
|
nickname = cert->nickname;
|
|
|
|
} else {
|
2014-01-23 05:13:19 +04:00
|
|
|
get_default_nickname(cert.get(), ctx, nickname, locker);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* user wants to import the cert */
|
2016-03-16 13:54:26 +03:00
|
|
|
slot.reset(PK11_ImportCertForKey(cert.get(), nickname.get(), ctx));
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!slot) {
|
2016-03-16 13:54:26 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2012-11-12 21:42:28 +04:00
|
|
|
slot = nullptr;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2006-06-26 21:13:13 +04:00
|
|
|
{
|
2014-01-23 05:13:19 +04:00
|
|
|
nsCOMPtr<nsIX509Cert> certToShow = nsNSSCertificate::Create(cert.get());
|
2013-08-30 03:15:35 +04:00
|
|
|
DisplayCertificateAlert(ctx, "UserCertImported", certToShow, locker);
|
2006-06-26 21:13:13 +04:00
|
|
|
}
|
2004-07-28 04:31:37 +04:00
|
|
|
|
2017-09-19 02:51:41 +03:00
|
|
|
nsresult rv = NS_OK;
|
2016-03-16 13:54:26 +03:00
|
|
|
int numCACerts = collectArgs->numcerts - 1;
|
2002-09-17 22:51:22 +04:00
|
|
|
if (numCACerts) {
|
2016-03-16 13:54:26 +03:00
|
|
|
SECItem* caCerts = collectArgs->rawCerts + 1;
|
2017-09-23 01:42:20 +03:00
|
|
|
rv = ImportCACerts(numCACerts, caCerts, ctx, locker);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2016-03-16 13:54:26 +03:00
|
|
|
|
2017-09-19 02:51:41 +03:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->NotifyObservers(nullptr, "psm:user-certificate-added", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2002-09-17 22:51:22 +04:00
|
|
|
nsNSSCertificateDB::DeleteCertificate(nsIX509Cert *aCert)
|
|
|
|
{
|
2015-05-28 00:12:26 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aCert);
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-04-20 11:14:22 +03:00
|
|
|
UniqueCERTCertificate cert(aCert->GetCert());
|
2014-07-04 09:09:24 +04:00
|
|
|
if (!cert) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
SECStatus srv = SECSuccess;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t certType;
|
2014-07-04 09:09:24 +04:00
|
|
|
aCert->GetCertType(&certType);
|
|
|
|
if (NS_FAILED(aCert->MarkForPermDeletion()))
|
2002-09-17 22:51:22 +04: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
|
2017-07-06 15:00:35 +03:00
|
|
|
// 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
|
2002-09-17 22:51:22 +04:00
|
|
|
// want to do that with user certs, because a user may re-store
|
2017-07-06 15:00:35 +03:00
|
|
|
// the cert onto the card again at which point we *will* want to
|
2002-09-17 22:51:22 +04:00
|
|
|
// trust that cert if it chains up properly.
|
|
|
|
nsNSSCertTrust trust(0, 0, 0);
|
2017-07-22 02:58:42 +03:00
|
|
|
srv = ChangeCertTrustWithPossibleAuthentication(cert, trust.GetTrust(),
|
|
|
|
nullptr);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("cert deleted: %d", srv));
|
2017-09-19 02:51:41 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->NotifyObservers(nullptr, "psm:user-certificate-deleted", nullptr);
|
|
|
|
}
|
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
return (srv) ? NS_ERROR_FAILURE : NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::SetCertTrust(nsIX509Cert *cert,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t type,
|
|
|
|
uint32_t trusted)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2015-05-28 00:12:26 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(cert);
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2012-11-12 21:42:28 +04:00
|
|
|
|
2017-07-22 02:58:42 +03:00
|
|
|
nsNSSCertTrust trust;
|
|
|
|
switch (type) {
|
|
|
|
case nsIX509Cert::CA_CERT:
|
|
|
|
trust.SetValidCA();
|
|
|
|
trust.AddCATrust(!!(trusted & nsIX509CertDB::TRUSTED_SSL),
|
|
|
|
!!(trusted & nsIX509CertDB::TRUSTED_EMAIL),
|
|
|
|
!!(trusted & nsIX509CertDB::TRUSTED_OBJSIGN));
|
|
|
|
break;
|
|
|
|
case nsIX509Cert::SERVER_CERT:
|
|
|
|
trust.SetValidPeer();
|
|
|
|
trust.AddPeerTrust(trusted & nsIX509CertDB::TRUSTED_SSL, false, false);
|
|
|
|
break;
|
|
|
|
case nsIX509Cert::EMAIL_CERT:
|
|
|
|
trust.SetValidPeer();
|
|
|
|
trust.AddPeerTrust(false, !!(trusted & nsIX509CertDB::TRUSTED_EMAIL),
|
|
|
|
false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Ignore any other type of certificate (including invalid types).
|
|
|
|
return NS_OK;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2017-07-22 02:58:42 +03:00
|
|
|
|
|
|
|
UniqueCERTCertificate nsscert(cert->GetCert());
|
|
|
|
SECStatus srv = ChangeCertTrustWithPossibleAuthentication(nsscert,
|
|
|
|
trust.GetTrust(),
|
|
|
|
nullptr);
|
2014-02-28 23:19:38 +04:00
|
|
|
return MapSECStatus(srv);
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::IsCertTrusted(nsIX509Cert *cert,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t certType,
|
|
|
|
uint32_t trustType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *_isTrusted)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2005-09-04 23:05:01 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(_isTrusted);
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = false;
|
2005-09-04 23:05:01 +04:00
|
|
|
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2017-06-09 02:10:00 +03:00
|
|
|
|
|
|
|
nsresult rv = BlockUntilLoadableRootsLoaded();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
SECStatus srv;
|
2016-04-20 11:14:22 +03:00
|
|
|
UniqueCERTCertificate nsscert(cert->GetCert());
|
2002-09-17 22:51:22 +04:00
|
|
|
CERTCertTrust nsstrust;
|
2014-01-23 05:13:19 +04:00
|
|
|
srv = CERT_GetCertTrust(nsscert.get(), &nsstrust);
|
2005-09-04 23:05:01 +04:00
|
|
|
if (srv != SECSuccess)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
nsNSSCertTrust trust(&nsstrust);
|
|
|
|
if (certType == nsIX509Cert::CA_CERT) {
|
|
|
|
if (trustType & nsIX509CertDB::TRUSTED_SSL) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedCA(true, false, false);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_EMAIL) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedCA(false, true, false);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_OBJSIGN) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedCA(false, false, true);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else if (certType == nsIX509Cert::SERVER_CERT) {
|
|
|
|
if (trustType & nsIX509CertDB::TRUSTED_SSL) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(true, false, false);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_EMAIL) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, true, false);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_OBJSIGN) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, false, true);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} else if (certType == nsIX509Cert::EMAIL_CERT) {
|
|
|
|
if (trustType & nsIX509CertDB::TRUSTED_SSL) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(true, false, false);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_EMAIL) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, true, false);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else if (trustType & nsIX509CertDB::TRUSTED_OBJSIGN) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*_isTrusted = trust.HasTrustedPeer(false, false, true);
|
2002-09-17 22:51:22 +04:00
|
|
|
} else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
} /* user: ignore */
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-30 11:20:58 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ImportCertsFromFile(nsIFile* aFile, uint32_t aType)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2013-08-30 03:15:35 +04:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-07-23 14:48:51 +04:00
|
|
|
NS_ENSURE_ARG(aFile);
|
2002-09-17 22:51:22 +04:00
|
|
|
switch (aType) {
|
|
|
|
case nsIX509Cert::CA_CERT:
|
|
|
|
case nsIX509Cert::EMAIL_CERT:
|
|
|
|
// good
|
|
|
|
break;
|
2016-01-30 11:20:58 +03:00
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
default:
|
|
|
|
// not supported (yet)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-02-07 07:41:11 +03:00
|
|
|
PRFileDesc* fd = nullptr;
|
|
|
|
nsresult rv = aFile->OpenNSPRFileDesc(PR_RDONLY, 0, &fd);
|
|
|
|
if (NS_FAILED(rv)) {
|
2002-09-17 22:51:22 +04:00
|
|
|
return rv;
|
2016-02-07 07:41:11 +03:00
|
|
|
}
|
|
|
|
if (!fd) {
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2016-02-07 07:41:11 +03:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2016-02-07 07:41:11 +03:00
|
|
|
PRFileInfo fileInfo;
|
|
|
|
if (PR_GetOpenFileInfo(fd, &fileInfo) != PR_SUCCESS) {
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2016-02-07 07:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto buf = MakeUnique<unsigned char[]>(fileInfo.size);
|
|
|
|
int32_t bytesObtained = PR_Read(fd, buf.get(), fileInfo.size);
|
2002-09-17 22:51:22 +04:00
|
|
|
PR_Close(fd);
|
2016-02-07 07:41:11 +03:00
|
|
|
|
|
|
|
if (bytesObtained != fileInfo.size) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-02-07 07:41:11 +03:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cxt = new PipUIContext();
|
|
|
|
|
|
|
|
switch (aType) {
|
|
|
|
case nsIX509Cert::CA_CERT:
|
|
|
|
return ImportCertificates(buf.get(), bytesObtained, aType, cxt);
|
|
|
|
case nsIX509Cert::EMAIL_CERT:
|
|
|
|
return ImportEmailCertificate(buf.get(), bytesObtained, cxt);
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Unsupported type should have been filtered out");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-05-10 04:02:40 +03:00
|
|
|
NS_IMETHODIMP
|
2017-01-28 02:26:20 +03:00
|
|
|
nsNSSCertificateDB::ImportPKCS12File(nsIFile* aFile)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2017-03-03 22:12:54 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
2013-08-30 03:15:35 +04:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2017-06-09 02:10:00 +03:00
|
|
|
nsresult rv = BlockUntilLoadableRootsLoaded();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-08-30 03:15:35 +04:00
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
NS_ENSURE_ARG(aFile);
|
|
|
|
nsPKCS12Blob blob;
|
2017-09-19 02:51:41 +03:00
|
|
|
rv = blob.ImportFromFile(aFile);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (NS_SUCCEEDED(rv) && observerService) {
|
|
|
|
observerService->NotifyObservers(nullptr, "psm:user-certificate-added", nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2016-05-10 04:02:40 +03:00
|
|
|
NS_IMETHODIMP
|
2017-01-28 02:26:20 +03:00
|
|
|
nsNSSCertificateDB::ExportPKCS12File(nsIFile* aFile, uint32_t count,
|
2016-01-30 11:20:58 +03:00
|
|
|
nsIX509Cert** certs)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2017-03-03 22:12:54 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2017-06-09 02:10:00 +03:00
|
|
|
nsresult rv = BlockUntilLoadableRootsLoaded();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-08-30 03:15:35 +04:00
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
NS_ENSURE_ARG(aFile);
|
2017-01-28 02:26:20 +03:00
|
|
|
if (count == 0) {
|
|
|
|
return NS_OK;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2017-01-28 02:26:20 +03:00
|
|
|
nsPKCS12Blob blob;
|
2002-09-17 22:51:22 +04:00
|
|
|
return blob.ExportToFile(aFile, certs, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-02-09 18:11:29 +03:00
|
|
|
nsNSSCertificateDB::FindCertByEmailAddress(const nsACString& aEmailAddress,
|
2016-01-30 11:20:58 +03:00
|
|
|
nsIX509Cert** _retval)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2003-01-18 17:03:00 +03:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
2013-08-30 03:15:35 +04:00
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-01-30 11:20:58 +03:00
|
|
|
|
2017-06-09 02:10:00 +03:00
|
|
|
nsresult rv = BlockUntilLoadableRootsLoaded();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
2012-10-27 11:11:35 +04:00
|
|
|
NS_ENSURE_TRUE(certVerifier, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2017-02-09 18:11:29 +03:00
|
|
|
const nsCString& flatEmailAddress = PromiseFlatCString(aEmailAddress);
|
2016-05-06 00:56:36 +03:00
|
|
|
UniqueCERTCertList certlist(
|
2017-02-09 18:11:29 +03:00
|
|
|
PK11_FindCertsFromEmailAddress(flatEmailAddress.get(), nullptr));
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!certlist)
|
2016-01-30 11:20:58 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2011-06-28 16:26:41 +04:00
|
|
|
// certlist now contains certificates with the right email address,
|
|
|
|
// but they might not have the correct usage or might even be invalid
|
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
if (CERT_LIST_END(CERT_LIST_HEAD(certlist), certlist))
|
2011-06-28 16:26:41 +04: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)) {
|
|
|
|
|
2016-05-06 00:56:36 +03:00
|
|
|
UniqueCERTCertList unusedCertChain;
|
2016-10-10 10:44:41 +03:00
|
|
|
mozilla::pkix::Result result =
|
|
|
|
certVerifier->VerifyCert(node->cert, certificateUsageEmailRecipient,
|
|
|
|
mozilla::pkix::Now(),
|
|
|
|
nullptr /*XXX pinarg*/,
|
|
|
|
nullptr /*hostname*/,
|
|
|
|
unusedCertChain);
|
|
|
|
if (result == mozilla::pkix::Success) {
|
2012-10-27 11:11:35 +04:00
|
|
|
break;
|
2011-06-28 16:26:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CERT_LIST_END(node, certlist)) {
|
|
|
|
// no valid cert found
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-06-28 16:26:41 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
// node now contains the first valid certificate with correct usage
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsNSSCertificate> nssCert = nsNSSCertificate::Create(node->cert);
|
2002-09-17 22:51:22 +04:00
|
|
|
if (!nssCert)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2015-07-01 20:10:53 +03:00
|
|
|
nssCert.forget(_retval);
|
2002-09-17 22:51:22 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-08-23 08:29:15 +03:00
|
|
|
nsNSSCertificateDB::ConstructX509FromBase64(const nsACString& base64,
|
|
|
|
/*out*/ nsIX509Cert** _retval)
|
2002-09-17 22:51:22 +04:00
|
|
|
{
|
2013-11-04 23:22:37 +04:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2016-08-23 08:29:15 +03:00
|
|
|
if (!_retval) {
|
2014-02-01 05:33:28 +04:00
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2016-08-23 08:29:15 +03:00
|
|
|
// Base64Decode() doesn't consider a zero length input as an error, and just
|
|
|
|
// returns the empty string. We don't want this behavior, so the below check
|
|
|
|
// catches this case.
|
|
|
|
if (base64.Length() < 1) {
|
2011-03-27 22:21:08 +04:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
2016-08-23 08:29:15 +03:00
|
|
|
nsAutoCString certDER;
|
|
|
|
nsresult rv = Base64Decode(base64, certDER);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2017-02-09 18:11:29 +03:00
|
|
|
return ConstructX509(certDER, _retval);
|
2014-02-01 05:33:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-02-09 18:11:29 +03:00
|
|
|
nsNSSCertificateDB::ConstructX509(const nsACString& certDER,
|
2014-02-01 05:33:28 +04:00
|
|
|
nsIX509Cert** _retval)
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!_retval)) {
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
}
|
2013-08-30 03:15:35 +04:00
|
|
|
|
2017-02-09 18:11:29 +03:00
|
|
|
SECItem certData;
|
|
|
|
certData.type = siDERCertBuffer;
|
|
|
|
certData.data = BitwiseCast<unsigned char*, const char*>(certDER.BeginReading());
|
|
|
|
certData.len = certDER.Length();
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2016-04-20 11:14:22 +03:00
|
|
|
UniqueCERTCertificate cert(CERT_NewTempCertificate(CERT_GetDefaultCertDB(),
|
2017-02-09 18:11:29 +03:00
|
|
|
&certData, nullptr,
|
2014-07-04 08:49:56 +04:00
|
|
|
false, true));
|
2011-03-27 22:21:08 +04:00
|
|
|
if (!cert)
|
|
|
|
return (PORT_GetError() == SEC_ERROR_NO_MEMORY)
|
|
|
|
? NS_ERROR_OUT_OF_MEMORY : NS_ERROR_FAILURE;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2014-01-23 05:13:19 +04:00
|
|
|
nsCOMPtr<nsIX509Cert> nssCert = nsNSSCertificate::Create(cert.get());
|
2012-11-12 21:42:28 +04:00
|
|
|
if (!nssCert) {
|
2011-03-27 22:21:08 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2012-11-12 21:42:28 +04:00
|
|
|
}
|
|
|
|
nssCert.forget(_retval);
|
|
|
|
return NS_OK;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
|
2008-06-11 22:20:24 +04:00
|
|
|
void
|
2017-07-06 15:00:35 +03:00
|
|
|
nsNSSCertificateDB::get_default_nickname(CERTCertificate *cert,
|
2008-06-11 22:20:24 +04:00
|
|
|
nsIInterfaceRequestor* ctx,
|
2013-08-30 03:15:35 +04:00
|
|
|
nsCString &nickname,
|
|
|
|
const nsNSSShutDownPreventionLock &/*proofOfLock*/)
|
2008-06-11 22:20:24 +04:00
|
|
|
{
|
2013-11-26 22:18:21 +04:00
|
|
|
static NS_DEFINE_CID(kNSSComponentCID, NS_NSSCOMPONENT_CID);
|
|
|
|
|
2008-06-11 22:20:24 +04:00
|
|
|
nickname.Truncate();
|
2002-09-17 22:51:22 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
CK_OBJECT_HANDLE keyHandle;
|
|
|
|
|
2017-06-09 02:10:00 +03:00
|
|
|
if (NS_FAILED(BlockUntilLoadableRootsLoaded())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-09-17 22:51:22 +04:00
|
|
|
CERTCertDBHandle *defaultcertdb = CERT_GetDefaultCertDB();
|
|
|
|
nsCOMPtr<nsINSSComponent> nssComponent(do_GetService(kNSSComponentCID, &rv));
|
2008-06-11 22:20:24 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString username;
|
2016-04-09 11:03:59 +03:00
|
|
|
UniquePORTString tempCN(CERT_GetCommonName(&cert->subject));
|
|
|
|
if (tempCN) {
|
|
|
|
username = tempCN.get();
|
2008-06-11 22:20:24 +04:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString caname;
|
2016-04-09 11:03:59 +03:00
|
|
|
UniquePORTString tempIssuerOrg(CERT_GetOrgName(&cert->issuer));
|
|
|
|
if (tempIssuerOrg) {
|
|
|
|
caname = tempIssuerOrg.get();
|
2008-06-11 22:20:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString tmpNickFmt;
|
2004-02-26 07:07:23 +03:00
|
|
|
nssComponent->GetPIPNSSBundleString("nick_template", tmpNickFmt);
|
2008-06-11 22:20:24 +04:00
|
|
|
NS_ConvertUTF16toUTF8 nickFmt(tmpNickFmt);
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString baseName;
|
2016-05-11 09:38:55 +03:00
|
|
|
baseName.AppendPrintf(nickFmt.get(), username.get(), caname.get());
|
|
|
|
if (baseName.IsEmpty()) {
|
2008-06-11 22:20:24 +04:00
|
|
|
return;
|
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
|
2008-06-11 22:20:24 +04:00
|
|
|
nickname = baseName;
|
2002-09-17 22:51:22 +04: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.
|
|
|
|
*/
|
2016-05-10 04:02:40 +03:00
|
|
|
UniquePK11SlotInfo slot(PK11_KeyForCertExists(cert, &keyHandle, ctx));
|
2008-06-11 22:20:24 +04:00
|
|
|
if (!slot)
|
|
|
|
return;
|
|
|
|
|
2016-05-10 04:02:40 +03:00
|
|
|
if (!PK11_IsInternal(slot.get())) {
|
2016-05-11 09:38:55 +03:00
|
|
|
nsAutoCString tmp;
|
|
|
|
tmp.AppendPrintf("%s:%s", PK11_GetTokenName(slot.get()), baseName.get());
|
|
|
|
if (tmp.IsEmpty()) {
|
2008-06-11 22:20:24 +04:00
|
|
|
nickname.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
baseName = tmp;
|
|
|
|
nickname = baseName;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2008-06-11 22:20:24 +04:00
|
|
|
|
|
|
|
int count = 1;
|
|
|
|
while (true) {
|
2002-09-17 22:51:22 +04:00
|
|
|
if ( count > 1 ) {
|
2016-05-11 09:38:55 +03:00
|
|
|
nsAutoCString tmp;
|
|
|
|
tmp.AppendPrintf("%s #%d", baseName.get(), count);
|
|
|
|
if (tmp.IsEmpty()) {
|
2008-06-11 22:20:24 +04:00
|
|
|
nickname.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nickname = tmp;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2008-06-11 22:20:24 +04:00
|
|
|
|
2016-04-20 11:14:22 +03:00
|
|
|
UniqueCERTCertificate dummycert;
|
2008-06-11 22:20:24 +04:00
|
|
|
|
2016-05-10 04:02:40 +03:00
|
|
|
if (PK11_IsInternal(slot.get())) {
|
2002-09-17 22:51:22 +04:00
|
|
|
/* look up the nickname to make sure it isn't in use already */
|
2016-04-20 11:14:22 +03:00
|
|
|
dummycert.reset(CERT_FindCertByNickname(defaultcertdb, nickname.get()));
|
2002-09-17 22:51:22 +04:00
|
|
|
} else {
|
2016-05-10 04:02:40 +03:00
|
|
|
// Check the cert against others that already live on the smart card.
|
2016-04-20 11:14:22 +03:00
|
|
|
dummycert.reset(PK11_FindCertFromNickname(nickname.get(), ctx));
|
2012-10-18 00:48:36 +04:00
|
|
|
if (dummycert) {
|
2016-05-10 04:02:40 +03:00
|
|
|
// Make sure the subject names are different.
|
2002-09-17 22:51:22 +04:00
|
|
|
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-18 00:48:36 +04:00
|
|
|
dummycert = nullptr;
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 09:09:24 +04:00
|
|
|
if (!dummycert) {
|
2008-06-11 22:20:24 +04:00
|
|
|
break;
|
2014-07-04 09:09:24 +04:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
count++;
|
2008-06-11 22:20:24 +04:00
|
|
|
}
|
2002-09-17 22:51:22 +04:00
|
|
|
}
|
2003-10-20 19:00:17 +04:00
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
NS_IMETHODIMP
|
2016-08-23 08:29:15 +03:00
|
|
|
nsNSSCertificateDB::AddCertFromBase64(const nsACString& aBase64,
|
|
|
|
const nsACString& aTrust,
|
2016-11-19 02:32:58 +03:00
|
|
|
nsIX509Cert** addedCertificate)
|
2003-10-20 19:00:17 +04:00
|
|
|
{
|
2016-11-19 02:32:58 +03:00
|
|
|
MOZ_ASSERT(addedCertificate);
|
|
|
|
if (!addedCertificate) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
*addedCertificate = nullptr;
|
|
|
|
|
2013-08-30 03:15:35 +04:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2003-10-20 19:00:17 +04:00
|
|
|
nsNSSCertTrust trust;
|
2017-07-22 02:58:42 +03:00
|
|
|
if (CERT_DecodeTrustString(&trust.GetTrust(), PromiseFlatCString(aTrust).get())
|
2016-08-23 08:29:15 +03:00
|
|
|
!= SECSuccess) {
|
2016-07-05 12:59:18 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-10-20 19:00:17 +04:00
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
nsCOMPtr<nsIX509Cert> newCert;
|
2012-07-27 18:03:25 +04:00
|
|
|
nsresult rv = ConstructX509FromBase64(aBase64, getter_AddRefs(newCert));
|
2016-08-23 08:29:15 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2003-10-20 19:00:17 +04:00
|
|
|
|
2016-07-05 12:59:18 +03:00
|
|
|
UniqueCERTCertificate tmpCert(newCert->GetCert());
|
2016-04-20 11:14:22 +03:00
|
|
|
if (!tmpCert) {
|
2016-07-05 12:59:18 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2003-10-20 19:00:17 +04:00
|
|
|
}
|
|
|
|
|
2016-07-05 12:59:18 +03: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.
|
2003-10-20 19:00:17 +04:00
|
|
|
if (tmpCert->isperm) {
|
2016-11-19 02:32:58 +03:00
|
|
|
rv = SetCertTrustFromString(newCert, aTrust);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
newCert.forget(addedCertificate);
|
|
|
|
return NS_OK;
|
2003-10-20 19:00:17 +04:00
|
|
|
}
|
|
|
|
|
2016-06-30 04:42:37 +03:00
|
|
|
UniquePORTString nickname(CERT_MakeCANickname(tmpCert.get()));
|
2003-10-20 19:00:17 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gPIPNSSLog, LogLevel::Debug, ("Created nick \"%s\"\n", nickname.get()));
|
2003-10-20 19:00:17 +04:00
|
|
|
|
2017-01-07 03:29:12 +03:00
|
|
|
UniquePK11SlotInfo slot(PK11_GetInternalKeySlot());
|
|
|
|
SECStatus srv = PK11_ImportCert(slot.get(), tmpCert.get(), CK_INVALID_HANDLE,
|
|
|
|
nickname.get(),
|
|
|
|
false); // this parameter is ignored by NSS
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
2017-07-22 02:58:42 +03:00
|
|
|
srv = ChangeCertTrustWithPossibleAuthentication(tmpCert, trust.GetTrust(),
|
|
|
|
nullptr);
|
2016-11-19 02:32:58 +03:00
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
newCert.forget(addedCertificate);
|
|
|
|
return NS_OK;
|
2003-10-20 19:00:17 +04:00
|
|
|
}
|
2006-01-10 05:07:47 +03:00
|
|
|
|
2012-11-15 03:31:39 +04:00
|
|
|
NS_IMETHODIMP
|
2016-11-19 02:32:58 +03:00
|
|
|
nsNSSCertificateDB::AddCert(const nsACString& aCertDER,
|
|
|
|
const nsACString& aTrust,
|
|
|
|
nsIX509Cert** addedCertificate)
|
2012-11-15 03:31:39 +04:00
|
|
|
{
|
|
|
|
nsCString base64;
|
|
|
|
nsresult rv = Base64Encode(aCertDER, base64);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-11-19 02:32:58 +03:00
|
|
|
return AddCertFromBase64(base64, aTrust, addedCertificate);
|
2012-11-15 03:31:39 +04:00
|
|
|
}
|
|
|
|
|
2014-02-23 07:08:06 +04:00
|
|
|
NS_IMETHODIMP
|
2014-07-04 09:09:24 +04:00
|
|
|
nsNSSCertificateDB::SetCertTrustFromString(nsIX509Cert* cert,
|
2016-08-23 08:29:15 +03:00
|
|
|
const nsACString& trustString)
|
2014-02-23 07:08:06 +04:00
|
|
|
{
|
2016-08-23 08:29:15 +03:00
|
|
|
NS_ENSURE_ARG(cert);
|
2014-02-23 07:08:06 +04:00
|
|
|
|
2016-08-23 08:29:15 +03:00
|
|
|
CERTCertTrust trust;
|
2014-02-23 07:08:06 +04:00
|
|
|
SECStatus srv = CERT_DecodeTrustString(&trust,
|
2016-08-23 08:29:15 +03:00
|
|
|
PromiseFlatCString(trustString).get());
|
2014-02-23 07:08:06 +04:00
|
|
|
if (srv != SECSuccess) {
|
2016-08-23 08:29:15 +03:00
|
|
|
return MapSECStatus(srv);
|
2014-02-23 07:08:06 +04:00
|
|
|
}
|
2016-04-20 11:14:22 +03:00
|
|
|
UniqueCERTCertificate nssCert(cert->GetCert());
|
2014-03-02 07:01:23 +04:00
|
|
|
|
2017-07-22 02:58:42 +03:00
|
|
|
srv = ChangeCertTrustWithPossibleAuthentication(nssCert, trust, nullptr);
|
2014-02-23 07:08:06 +04:00
|
|
|
return MapSECStatus(srv);
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2006-01-10 05:07:47 +03:00
|
|
|
nsNSSCertificateDB::GetCerts(nsIX509CertList **_retval)
|
|
|
|
{
|
2013-08-30 03:15:35 +04:00
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2016-05-06 00:56:36 +03:00
|
|
|
}
|
2006-01-10 05:07:47 +03:00
|
|
|
|
2017-09-07 00:29:17 +03:00
|
|
|
nsresult rv = BlockUntilLoadableRootsLoaded();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2006-01-10 05:07:47 +03:00
|
|
|
nsCOMPtr<nsIInterfaceRequestor> ctx = new PipUIContext();
|
|
|
|
nsCOMPtr<nsIX509CertList> nssCertList;
|
2016-05-06 00:56:36 +03:00
|
|
|
UniqueCERTCertList certList(PK11_ListCerts(PK11CertListUnique, ctx));
|
2006-01-10 05:07:47 +03:00
|
|
|
|
2012-10-18 00:48:36 +04:00
|
|
|
// nsNSSCertList 1) adopts certList, and 2) handles the nullptr case fine.
|
2017-07-06 15:00:35 +03:00
|
|
|
// (returns an empty list)
|
2016-05-06 00:56:36 +03:00
|
|
|
nssCertList = new nsNSSCertList(Move(certList), locker);
|
2006-01-10 05:07:47 +03:00
|
|
|
|
2015-07-01 20:10:53 +03:00
|
|
|
nssCertList.forget(_retval);
|
2006-01-10 05:07:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-12-07 07:05:27 +04:00
|
|
|
|
2016-04-14 01:36:22 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::GetEnterpriseRoots(nsIX509CertList** enterpriseRoots)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
return NS_ERROR_NOT_SAME_THREAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_ARG_POINTER(enterpriseRoots);
|
|
|
|
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
nsCOMPtr<nsINSSComponent> psm(do_GetService(PSM_COMPONENT_CONTRACTID));
|
|
|
|
if (!psm) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return psm->GetEnterpriseRoots(enterpriseRoots);
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-09-11 21:52:30 +03:00
|
|
|
nsresult
|
|
|
|
VerifyCertAtTime(nsIX509Cert* aCert,
|
|
|
|
int64_t /*SECCertificateUsage*/ aUsage,
|
|
|
|
uint32_t aFlags,
|
2017-02-09 18:11:29 +03:00
|
|
|
const nsACString& aHostname,
|
2015-09-11 21:52:30 +03:00
|
|
|
mozilla::pkix::Time aTime,
|
|
|
|
nsIX509CertList** aVerifiedChain,
|
|
|
|
bool* aHasEVPolicy,
|
|
|
|
int32_t* /*PRErrorCode*/ _retval,
|
|
|
|
const nsNSSShutDownPreventionLock& locker)
|
2013-11-12 04:37:06 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCert);
|
|
|
|
NS_ENSURE_ARG_POINTER(aHasEVPolicy);
|
2015-05-07 21:06:07 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aVerifiedChain);
|
2013-11-12 04:37:06 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(_retval);
|
|
|
|
|
2015-05-07 21:06:07 +03:00
|
|
|
*aVerifiedChain = nullptr;
|
2013-11-12 04:37:06 +04:00
|
|
|
*aHasEVPolicy = false;
|
|
|
|
*_retval = PR_UNKNOWN_ERROR;
|
|
|
|
|
2016-04-20 11:14:22 +03:00
|
|
|
UniqueCERTCertificate nssCert(aCert->GetCert());
|
2014-07-04 09:09:24 +04:00
|
|
|
if (!nssCert) {
|
2013-11-12 04:37:06 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
2013-11-12 04:37:06 +04:00
|
|
|
NS_ENSURE_TRUE(certVerifier, NS_ERROR_FAILURE);
|
|
|
|
|
2016-05-06 00:56:36 +03:00
|
|
|
UniqueCERTCertList resultChain;
|
2013-11-12 04:37:06 +04:00
|
|
|
SECOidTag evOidPolicy;
|
2016-10-10 10:44:41 +03:00
|
|
|
mozilla::pkix::Result result;
|
2013-11-12 04:37:06 +04:00
|
|
|
|
2017-02-09 18:11:29 +03:00
|
|
|
if (!aHostname.IsVoid() && aUsage == certificateUsageSSLServer) {
|
2016-10-10 10:44:41 +03:00
|
|
|
result = certVerifier->VerifySSLServerCert(nssCert,
|
|
|
|
nullptr, // stapledOCSPResponse
|
|
|
|
nullptr, // sctsFromTLSExtension
|
|
|
|
aTime,
|
|
|
|
nullptr, // Assume no context
|
2017-06-03 08:35:51 +03:00
|
|
|
aHostname,
|
2016-10-10 10:44:41 +03:00
|
|
|
resultChain,
|
|
|
|
false, // don't save intermediates
|
|
|
|
aFlags,
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes(),
|
2016-10-10 10:44:41 +03:00
|
|
|
&evOidPolicy);
|
2015-05-07 21:06:07 +03:00
|
|
|
} else {
|
2017-06-03 08:35:51 +03:00
|
|
|
const nsCString& flatHostname = PromiseFlatCString(aHostname);
|
2016-10-10 10:44:41 +03:00
|
|
|
result = certVerifier->VerifyCert(nssCert.get(), aUsage, aTime,
|
|
|
|
nullptr, // Assume no context
|
2017-02-09 18:11:29 +03:00
|
|
|
aHostname.IsVoid() ? nullptr
|
|
|
|
: flatHostname.get(),
|
2016-10-10 10:44:41 +03:00
|
|
|
resultChain,
|
|
|
|
aFlags,
|
|
|
|
nullptr, // stapledOCSPResponse
|
|
|
|
nullptr, // sctsFromTLSExtension
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes(),
|
2016-10-10 10:44:41 +03:00
|
|
|
&evOidPolicy);
|
2015-05-07 21:06:07 +03:00
|
|
|
}
|
2013-11-12 04:37:06 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIX509CertList> nssCertList;
|
|
|
|
// This adopts the list
|
2016-05-06 00:56:36 +03:00
|
|
|
nssCertList = new nsNSSCertList(Move(resultChain), locker);
|
2013-11-12 04:37:06 +04:00
|
|
|
NS_ENSURE_TRUE(nssCertList, NS_ERROR_FAILURE);
|
|
|
|
|
2016-10-10 10:44:41 +03:00
|
|
|
*_retval = mozilla::pkix::MapResultToPRErrorCode(result);
|
|
|
|
if (result == mozilla::pkix::Success && evOidPolicy != SEC_OID_UNKNOWN) {
|
|
|
|
*aHasEVPolicy = true;
|
2013-11-12 04:37:06 +04:00
|
|
|
}
|
2015-05-07 21:06:07 +03:00
|
|
|
nssCertList.forget(aVerifiedChain);
|
2013-11-12 04:37:06 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-03-13 00:08:48 +04:00
|
|
|
|
2015-09-11 21:52:30 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::VerifyCertNow(nsIX509Cert* aCert,
|
|
|
|
int64_t /*SECCertificateUsage*/ aUsage,
|
|
|
|
uint32_t aFlags,
|
2017-02-09 18:11:29 +03:00
|
|
|
const nsACString& aHostname,
|
2015-09-11 21:52:30 +03:00
|
|
|
nsIX509CertList** aVerifiedChain,
|
|
|
|
bool* aHasEVPolicy,
|
|
|
|
int32_t* /*PRErrorCode*/ _retval)
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::VerifyCertAtTime(aCert, aUsage, aFlags, aHostname,
|
|
|
|
mozilla::pkix::Now(),
|
|
|
|
aVerifiedChain, aHasEVPolicy, _retval, locker);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::VerifyCertAtTime(nsIX509Cert* aCert,
|
|
|
|
int64_t /*SECCertificateUsage*/ aUsage,
|
|
|
|
uint32_t aFlags,
|
2017-02-09 18:11:29 +03:00
|
|
|
const nsACString& aHostname,
|
2015-09-11 21:52:30 +03:00
|
|
|
uint64_t aTime,
|
|
|
|
nsIX509CertList** aVerifiedChain,
|
|
|
|
bool* aHasEVPolicy,
|
|
|
|
int32_t* /*PRErrorCode*/ _retval)
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::VerifyCertAtTime(aCert, aUsage, aFlags, aHostname,
|
|
|
|
mozilla::pkix::TimeFromEpochInSeconds(aTime),
|
|
|
|
aVerifiedChain, aHasEVPolicy, _retval, locker);
|
|
|
|
}
|
|
|
|
|
2016-07-01 00:09:18 +03:00
|
|
|
class VerifyCertAtTimeTask final : public CryptoTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
VerifyCertAtTimeTask(nsIX509Cert* aCert, int64_t aUsage, uint32_t aFlags,
|
2017-02-09 18:11:29 +03:00
|
|
|
const nsACString& aHostname, uint64_t aTime,
|
2016-07-01 00:09:18 +03:00
|
|
|
nsICertVerificationCallback* aCallback)
|
|
|
|
: mCert(aCert)
|
|
|
|
, mUsage(aUsage)
|
|
|
|
, mFlags(aFlags)
|
|
|
|
, mHostname(aHostname)
|
|
|
|
, mTime(aTime)
|
2017-06-14 04:27:17 +03:00
|
|
|
, mCallback(new nsMainThreadPtrHolder<nsICertVerificationCallback>(
|
|
|
|
"nsICertVerificationCallback", aCallback))
|
2016-07-01 00:09:18 +03:00
|
|
|
, mPRErrorCode(SEC_ERROR_LIBRARY_FAILURE)
|
|
|
|
, mVerifiedCertList(nullptr)
|
|
|
|
, mHasEVPolicy(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual nsresult CalculateResult() override
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIX509CertDB> certDB = do_GetService(NS_X509CERTDB_CONTRACTID);
|
|
|
|
if (!certDB) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-02-09 18:11:29 +03:00
|
|
|
return certDB->VerifyCertAtTime(mCert, mUsage, mFlags, mHostname, mTime,
|
2016-07-01 00:09:18 +03:00
|
|
|
getter_AddRefs(mVerifiedCertList),
|
|
|
|
&mHasEVPolicy, &mPRErrorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// No NSS resources are directly held, so there is nothing to release.
|
|
|
|
virtual void ReleaseNSSResources() override { }
|
|
|
|
|
|
|
|
virtual void CallCallback(nsresult rv) override
|
|
|
|
{
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Unused << mCallback->VerifyCertFinished(SEC_ERROR_LIBRARY_FAILURE,
|
|
|
|
nullptr, false);
|
|
|
|
} else {
|
|
|
|
Unused << mCallback->VerifyCertFinished(mPRErrorCode, mVerifiedCertList,
|
|
|
|
mHasEVPolicy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIX509Cert> mCert;
|
|
|
|
int64_t mUsage;
|
|
|
|
uint32_t mFlags;
|
|
|
|
nsCString mHostname;
|
|
|
|
uint64_t mTime;
|
|
|
|
nsMainThreadPtrHandle<nsICertVerificationCallback> mCallback;
|
|
|
|
int32_t mPRErrorCode;
|
|
|
|
nsCOMPtr<nsIX509CertList> mVerifiedCertList;
|
|
|
|
bool mHasEVPolicy;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::AsyncVerifyCertAtTime(nsIX509Cert* aCert,
|
|
|
|
int64_t /*SECCertificateUsage*/ aUsage,
|
|
|
|
uint32_t aFlags,
|
2017-02-09 18:11:29 +03:00
|
|
|
const nsACString& aHostname,
|
2016-07-01 00:09:18 +03:00
|
|
|
uint64_t aTime,
|
|
|
|
nsICertVerificationCallback* aCallback)
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
RefPtr<VerifyCertAtTimeTask> task(new VerifyCertAtTimeTask(aCert, aUsage,
|
|
|
|
aFlags, aHostname,
|
|
|
|
aTime, aCallback));
|
|
|
|
return task->Dispatch("VerifyCert");
|
|
|
|
}
|
|
|
|
|
2014-03-13 00:08:48 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNSSCertificateDB::ClearOCSPCache()
|
|
|
|
{
|
|
|
|
nsNSSShutDownPreventionLock locker;
|
|
|
|
if (isAlreadyShutDown()) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedCertVerifier> certVerifier(GetDefaultCertVerifier());
|
2014-03-13 00:08:48 +04:00
|
|
|
NS_ENSURE_TRUE(certVerifier, NS_ERROR_FAILURE);
|
2014-06-17 10:13:29 +04:00
|
|
|
certVerifier->ClearOCSPCache();
|
2014-03-13 00:08:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|