2014-02-14 22:37:07 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "AppTrustDomain.h"
|
|
|
|
#include "certdb.h"
|
2014-07-18 18:48:49 +00:00
|
|
|
#include "pkix/pkixnss.h"
|
2014-02-14 22:37:07 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
|
|
|
#include "nsIX509CertDB.h"
|
2014-07-06 22:55:38 +00:00
|
|
|
#include "nsNSSCertificate.h"
|
2014-02-14 22:37:07 +00:00
|
|
|
#include "prerror.h"
|
|
|
|
#include "secerr.h"
|
|
|
|
|
|
|
|
// Generated in Makefile.in
|
|
|
|
#include "marketplace-prod-public.inc"
|
|
|
|
#include "marketplace-prod-reviewers.inc"
|
|
|
|
#include "marketplace-dev-public.inc"
|
|
|
|
#include "marketplace-dev-reviewers.inc"
|
2014-07-23 11:20:25 +00:00
|
|
|
#include "marketplace-stage.inc"
|
2014-02-14 22:37:07 +00:00
|
|
|
#include "xpcshell.inc"
|
2014-09-22 14:58:59 +00:00
|
|
|
// Trusted Hosted Apps Certificates
|
|
|
|
#include "manifest-signing-root.inc"
|
|
|
|
#include "manifest-signing-test-root.inc"
|
2014-02-14 22:37:07 +00:00
|
|
|
|
2014-03-20 21:29:21 +00:00
|
|
|
using namespace mozilla::pkix;
|
2014-02-14 22:37:07 +00:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* gPIPNSSLog;
|
|
|
|
#endif
|
|
|
|
|
2014-10-18 13:18:00 +00:00
|
|
|
static const unsigned int DEFAULT_MINIMUM_NON_ECC_BITS = 2048;
|
|
|
|
|
2014-02-14 22:37:07 +00:00
|
|
|
namespace mozilla { namespace psm {
|
|
|
|
|
2014-07-06 22:55:38 +00:00
|
|
|
AppTrustDomain::AppTrustDomain(ScopedCERTCertList& certChain, void* pinArg)
|
|
|
|
: mCertChain(certChain)
|
|
|
|
, mPinArg(pinArg)
|
2014-10-18 13:18:00 +00:00
|
|
|
, mMinimumNonECCBits(DEFAULT_MINIMUM_NON_ECC_BITS)
|
2014-02-14 22:37:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SECStatus
|
|
|
|
AppTrustDomain::SetTrustedRoot(AppTrustedRoot trustedRoot)
|
|
|
|
{
|
|
|
|
SECItem trustedDER;
|
|
|
|
|
|
|
|
// Load the trusted certificate into the in-memory NSS database so that
|
|
|
|
// CERT_CreateSubjectCertList can find it.
|
|
|
|
|
|
|
|
switch (trustedRoot)
|
|
|
|
{
|
|
|
|
case nsIX509CertDB::AppMarketplaceProdPublicRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(marketplaceProdPublicRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(marketplaceProdPublicRoot);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIX509CertDB::AppMarketplaceProdReviewersRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(marketplaceProdReviewersRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(marketplaceProdReviewersRoot);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIX509CertDB::AppMarketplaceDevPublicRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(marketplaceDevPublicRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(marketplaceDevPublicRoot);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIX509CertDB::AppMarketplaceDevReviewersRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(marketplaceDevReviewersRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(marketplaceDevReviewersRoot);
|
|
|
|
break;
|
|
|
|
|
2014-07-23 11:20:25 +00:00
|
|
|
case nsIX509CertDB::AppMarketplaceStageRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(marketplaceStageRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(marketplaceStageRoot);
|
2014-10-18 13:18:00 +00:00
|
|
|
// The staging root was generated with a 1024-bit key.
|
|
|
|
mMinimumNonECCBits = 1024u;
|
2014-07-23 11:20:25 +00:00
|
|
|
break;
|
|
|
|
|
2014-02-14 22:37:07 +00:00
|
|
|
case nsIX509CertDB::AppXPCShellRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(xpcshellRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(xpcshellRoot);
|
|
|
|
break;
|
|
|
|
|
2014-09-22 14:58:59 +00:00
|
|
|
case nsIX509CertDB::TrustedHostedAppPublicRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(trustedAppPublicRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(trustedAppPublicRoot);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIX509CertDB::TrustedHostedAppTestRoot:
|
|
|
|
trustedDER.data = const_cast<uint8_t*>(trustedAppTestRoot);
|
|
|
|
trustedDER.len = mozilla::ArrayLength(trustedAppTestRoot);
|
|
|
|
break;
|
|
|
|
|
2014-02-14 22:37:07 +00:00
|
|
|
default:
|
|
|
|
PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTrustedRoot = CERT_NewTempCertificate(CERT_GetDefaultCertDB(),
|
|
|
|
&trustedDER, nullptr, false, true);
|
|
|
|
if (!mTrustedRoot) {
|
|
|
|
return SECFailure;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SECSuccess;
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:48:49 +00:00
|
|
|
Result
|
2014-07-31 19:17:31 +00:00
|
|
|
AppTrustDomain::FindIssuer(Input encodedIssuerName, IssuerChecker& checker,
|
2014-08-02 15:49:12 +00:00
|
|
|
Time)
|
2014-07-02 23:15:16 +00:00
|
|
|
|
2014-02-14 22:37:07 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTrustedRoot);
|
|
|
|
if (!mTrustedRoot) {
|
2014-07-18 18:48:49 +00:00
|
|
|
return Result::FATAL_ERROR_INVALID_STATE;
|
2014-02-14 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 23:15:16 +00:00
|
|
|
// TODO(bug 1035418): If/when mozilla::pkix relaxes the restriction that
|
|
|
|
// FindIssuer must only pass certificates with a matching subject name to
|
|
|
|
// checker.Check, we can stop using CERT_CreateSubjectCertList and instead
|
|
|
|
// use logic like this:
|
|
|
|
//
|
|
|
|
// 1. First, try the trusted trust anchor.
|
|
|
|
// 2. Secondly, iterate through the certificates that were stored in the CMS
|
|
|
|
// message, passing each one to checker.Check.
|
2014-07-19 05:30:51 +00:00
|
|
|
SECItem encodedIssuerNameSECItem =
|
2014-07-31 19:17:31 +00:00
|
|
|
UnsafeMapInputToSECItem(encodedIssuerName);
|
2014-07-06 22:55:38 +00:00
|
|
|
ScopedCERTCertList
|
2014-07-02 23:15:16 +00:00
|
|
|
candidates(CERT_CreateSubjectCertList(nullptr, CERT_GetDefaultCertDB(),
|
2014-08-02 06:16:21 +00:00
|
|
|
&encodedIssuerNameSECItem, 0,
|
|
|
|
false));
|
2014-07-02 23:15:16 +00:00
|
|
|
if (candidates) {
|
|
|
|
for (CERTCertListNode* n = CERT_LIST_HEAD(candidates);
|
|
|
|
!CERT_LIST_END(n, candidates); n = CERT_LIST_NEXT(n)) {
|
2014-07-31 19:17:31 +00:00
|
|
|
Input certDER;
|
2014-07-19 05:30:51 +00:00
|
|
|
Result rv = certDER.Init(n->cert->derCert.data, n->cert->derCert.len);
|
|
|
|
if (rv != Success) {
|
|
|
|
continue; // probably too big
|
|
|
|
}
|
|
|
|
|
2014-07-02 23:15:16 +00:00
|
|
|
bool keepGoing;
|
2014-07-19 05:30:51 +00:00
|
|
|
rv = checker.Check(certDER, nullptr/*additionalNameConstraints*/,
|
|
|
|
keepGoing);
|
2014-07-18 18:48:49 +00:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
2014-07-02 23:15:16 +00:00
|
|
|
}
|
|
|
|
if (!keepGoing) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:48:49 +00:00
|
|
|
return Success;
|
2014-02-14 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 18:48:49 +00:00
|
|
|
Result
|
2014-02-14 22:37:07 +00:00
|
|
|
AppTrustDomain::GetCertTrust(EndEntityOrCA endEntityOrCA,
|
2014-05-16 01:59:52 +00:00
|
|
|
const CertPolicyId& policy,
|
2014-07-31 19:17:31 +00:00
|
|
|
Input candidateCertDER,
|
2014-07-19 05:30:51 +00:00
|
|
|
/*out*/ TrustLevel& trustLevel)
|
2014-02-14 22:37:07 +00:00
|
|
|
{
|
2014-05-16 01:59:52 +00:00
|
|
|
MOZ_ASSERT(policy.IsAnyPolicy());
|
2014-02-14 22:37:07 +00:00
|
|
|
MOZ_ASSERT(mTrustedRoot);
|
2014-07-20 18:06:26 +00:00
|
|
|
if (!policy.IsAnyPolicy()) {
|
2014-07-18 18:48:49 +00:00
|
|
|
return Result::FATAL_ERROR_INVALID_ARGS;
|
2014-02-14 22:37:07 +00:00
|
|
|
}
|
|
|
|
if (!mTrustedRoot) {
|
2014-07-18 18:48:49 +00:00
|
|
|
return Result::FATAL_ERROR_INVALID_STATE;
|
2014-02-14 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle active distrust of the certificate.
|
2014-06-03 17:47:25 +00:00
|
|
|
|
|
|
|
// XXX: This would be cleaner and more efficient if we could get the trust
|
|
|
|
// information without constructing a CERTCertificate here, but NSS doesn't
|
|
|
|
// expose it in any other easy-to-use fashion.
|
2014-07-19 05:30:51 +00:00
|
|
|
SECItem candidateCertDERSECItem =
|
2014-07-31 19:17:31 +00:00
|
|
|
UnsafeMapInputToSECItem(candidateCertDER);
|
2014-06-03 17:47:25 +00:00
|
|
|
ScopedCERTCertificate candidateCert(
|
2014-07-19 05:30:51 +00:00
|
|
|
CERT_NewTempCertificate(CERT_GetDefaultCertDB(), &candidateCertDERSECItem,
|
|
|
|
nullptr, false, true));
|
2014-06-03 17:47:25 +00:00
|
|
|
if (!candidateCert) {
|
2014-07-18 18:48:49 +00:00
|
|
|
return MapPRErrorCodeToResult(PR_GetError());
|
2014-06-03 17:47:25 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 22:37:07 +00:00
|
|
|
CERTCertTrust trust;
|
2014-06-03 17:47:25 +00:00
|
|
|
if (CERT_GetCertTrust(candidateCert.get(), &trust) == SECSuccess) {
|
2014-08-08 12:39:07 +00:00
|
|
|
uint32_t flags = SEC_GET_TRUST_FLAGS(&trust, trustObjectSigning);
|
2014-02-14 22:37:07 +00:00
|
|
|
|
|
|
|
// For DISTRUST, we use the CERTDB_TRUSTED or CERTDB_TRUSTED_CA bit,
|
|
|
|
// because we can have active distrust for either type of cert. Note that
|
|
|
|
// CERTDB_TERMINAL_RECORD means "stop trying to inherit trust" so if the
|
|
|
|
// relevant trust bit isn't set then that means the cert must be considered
|
|
|
|
// distrusted.
|
2014-08-08 12:39:07 +00:00
|
|
|
uint32_t relevantTrustBit = endEntityOrCA == EndEntityOrCA::MustBeCA
|
2014-02-14 22:37:07 +00:00
|
|
|
? CERTDB_TRUSTED_CA
|
|
|
|
: CERTDB_TRUSTED;
|
|
|
|
if (((flags & (relevantTrustBit | CERTDB_TERMINAL_RECORD)))
|
|
|
|
== CERTDB_TERMINAL_RECORD) {
|
2014-07-20 18:06:26 +00:00
|
|
|
trustLevel = TrustLevel::ActivelyDistrusted;
|
2014-07-18 18:48:49 +00:00
|
|
|
return Success;
|
2014-02-14 22:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mTrustedRoot is the only trust anchor for this validation.
|
2014-06-03 17:47:25 +00:00
|
|
|
if (CERT_CompareCerts(mTrustedRoot.get(), candidateCert.get())) {
|
2014-07-20 18:06:26 +00:00
|
|
|
trustLevel = TrustLevel::TrustAnchor;
|
2014-07-18 18:48:49 +00:00
|
|
|
return Success;
|
2014-02-14 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-20 18:06:26 +00:00
|
|
|
trustLevel = TrustLevel::InheritsTrust;
|
2014-07-18 18:48:49 +00:00
|
|
|
return Success;
|
2014-02-14 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 18:48:49 +00:00
|
|
|
Result
|
2015-02-07 20:14:31 +00:00
|
|
|
AppTrustDomain::DigestBuf(Input item,
|
|
|
|
DigestAlgorithm digestAlg,
|
|
|
|
/*out*/ uint8_t* digestBuf,
|
2014-07-07 02:36:05 +00:00
|
|
|
size_t digestBufLen)
|
|
|
|
{
|
2015-02-07 20:14:31 +00:00
|
|
|
return DigestBufNSS(item, digestAlg, digestBuf, digestBufLen);
|
2014-07-07 02:36:05 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 18:48:49 +00:00
|
|
|
Result
|
2014-08-02 15:49:12 +00:00
|
|
|
AppTrustDomain::CheckRevocation(EndEntityOrCA, const CertID&, Time,
|
2014-07-31 19:17:31 +00:00
|
|
|
/*optional*/ const Input*,
|
|
|
|
/*optional*/ const Input*)
|
2014-02-17 01:35:40 +00:00
|
|
|
{
|
|
|
|
// We don't currently do revocation checking. If we need to distrust an Apps
|
|
|
|
// certificate, we will use the active distrust mechanism.
|
2014-07-18 18:48:49 +00:00
|
|
|
return Success;
|
2014-02-17 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 18:48:49 +00:00
|
|
|
Result
|
2014-09-25 18:18:56 +00:00
|
|
|
AppTrustDomain::IsChainValid(const DERArray& certChain, Time time)
|
2014-07-06 22:55:38 +00:00
|
|
|
{
|
2014-07-18 18:48:49 +00:00
|
|
|
SECStatus srv = ConstructCERTCertListFromReversedDERArray(certChain,
|
|
|
|
mCertChain);
|
|
|
|
if (srv != SECSuccess) {
|
|
|
|
return MapPRErrorCodeToResult(PR_GetError());
|
|
|
|
}
|
|
|
|
return Success;
|
2014-07-06 22:55:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-15 00:59:02 +00:00
|
|
|
Result
|
|
|
|
AppTrustDomain::CheckSignatureDigestAlgorithm(DigestAlgorithm)
|
|
|
|
{
|
|
|
|
// TODO: We should restrict signatures to SHA-256 or better.
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:48:49 +00:00
|
|
|
Result
|
2015-02-03 00:17:08 +00:00
|
|
|
AppTrustDomain::CheckRSAPublicKeyModulusSizeInBits(
|
|
|
|
EndEntityOrCA /*endEntityOrCA*/, unsigned int modulusSizeInBits)
|
2014-07-15 23:49:00 +00:00
|
|
|
{
|
2015-02-03 00:17:08 +00:00
|
|
|
if (modulusSizeInBits < mMinimumNonECCBits) {
|
|
|
|
return Result::ERROR_INADEQUATE_KEY_SIZE;
|
|
|
|
}
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2015-02-07 20:14:31 +00:00
|
|
|
Result
|
|
|
|
AppTrustDomain::VerifyRSAPKCS1SignedDigest(const SignedDigest& signedDigest,
|
|
|
|
Input subjectPublicKeyInfo)
|
|
|
|
{
|
2015-02-15 00:59:02 +00:00
|
|
|
// TODO: We should restrict signatures to SHA-256 or better.
|
2015-02-07 20:14:31 +00:00
|
|
|
return VerifyRSAPKCS1SignedDigestNSS(signedDigest, subjectPublicKeyInfo,
|
|
|
|
mPinArg);
|
|
|
|
}
|
|
|
|
|
2015-02-03 00:17:08 +00:00
|
|
|
Result
|
|
|
|
AppTrustDomain::CheckECDSACurveIsAcceptable(EndEntityOrCA /*endEntityOrCA*/,
|
|
|
|
NamedCurve curve)
|
|
|
|
{
|
|
|
|
switch (curve) {
|
|
|
|
case NamedCurve::secp256r1: // fall through
|
|
|
|
case NamedCurve::secp384r1: // fall through
|
|
|
|
case NamedCurve::secp521r1:
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result::ERROR_UNSUPPORTED_ELLIPTIC_CURVE;
|
2014-07-15 23:49:00 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 20:14:31 +00:00
|
|
|
Result
|
|
|
|
AppTrustDomain::VerifyECDSASignedDigest(const SignedDigest& signedDigest,
|
|
|
|
Input subjectPublicKeyInfo)
|
|
|
|
{
|
|
|
|
return VerifyECDSASignedDigestNSS(signedDigest, subjectPublicKeyInfo,
|
|
|
|
mPinArg);
|
|
|
|
}
|
|
|
|
|
2014-07-06 22:55:38 +00:00
|
|
|
} } // namespace mozilla::psm
|