2014-02-03 09:21:00 +04: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: */
|
2014-05-14 17:37:25 +04:00
|
|
|
/* This code is made available to you under your choice of the following sets
|
|
|
|
* of licensing terms:
|
|
|
|
*/
|
|
|
|
/* 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/.
|
|
|
|
*/
|
|
|
|
/* Copyright 2013 Mozilla Contributors
|
2014-02-03 09:21:00 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-07-19 09:30:51 +04:00
|
|
|
#include "pkixcheck.h"
|
|
|
|
|
2014-02-07 06:13:20 +04:00
|
|
|
#include "pkixder.h"
|
2014-02-03 09:21:00 +04:00
|
|
|
#include "pkixutil.h"
|
|
|
|
|
2014-03-21 01:29:21 +04:00
|
|
|
namespace mozilla { namespace pkix {
|
2014-02-03 09:21:00 +04:00
|
|
|
|
2015-02-23 03:59:03 +03:00
|
|
|
// 4.1.1.2 signatureAlgorithm
|
|
|
|
// 4.1.2.3 signature
|
|
|
|
|
|
|
|
Result
|
2015-02-15 03:59:02 +03:00
|
|
|
CheckSignatureAlgorithm(TrustDomain& trustDomain,
|
|
|
|
EndEntityOrCA endEntityOrCA,
|
2015-09-11 21:52:30 +03:00
|
|
|
Time notBefore,
|
2015-02-15 03:59:02 +03:00
|
|
|
const der::SignedDataWithSignature& signedData,
|
|
|
|
Input signatureValue)
|
2015-02-23 03:59:03 +03:00
|
|
|
{
|
|
|
|
// 4.1.1.2. signatureAlgorithm
|
|
|
|
der::PublicKeyAlgorithm publicKeyAlg;
|
|
|
|
DigestAlgorithm digestAlg;
|
2015-02-15 03:59:02 +03:00
|
|
|
Reader signatureAlgorithmReader(signedData.algorithm);
|
2015-02-23 03:59:03 +03:00
|
|
|
Result rv = der::SignatureAlgorithmIdentifierValue(signatureAlgorithmReader,
|
|
|
|
publicKeyAlg, digestAlg);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = der::End(signatureAlgorithmReader);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4.1.2.3. Signature
|
|
|
|
der::PublicKeyAlgorithm signedPublicKeyAlg;
|
|
|
|
DigestAlgorithm signedDigestAlg;
|
|
|
|
Reader signedSignatureAlgorithmReader(signatureValue);
|
|
|
|
rv = der::SignatureAlgorithmIdentifierValue(signedSignatureAlgorithmReader,
|
|
|
|
signedPublicKeyAlg,
|
|
|
|
signedDigestAlg);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = der::End(signedSignatureAlgorithmReader);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "This field MUST contain the same algorithm identifier as the
|
|
|
|
// signatureAlgorithm field in the sequence Certificate." However, it may
|
|
|
|
// be encoded differently. In particular, one of the fields may have a NULL
|
|
|
|
// parameter while the other one may omit the parameter field altogether, and
|
|
|
|
// these are considered equivalent. Some certificates generation software
|
|
|
|
// actually generates certificates like that, so we compare the parsed values
|
|
|
|
// instead of comparing the encoded values byte-for-byte.
|
|
|
|
//
|
|
|
|
// Along the same lines, we accept two different OIDs for RSA-with-SHA1, and
|
|
|
|
// we consider those OIDs to be equivalent here.
|
|
|
|
if (publicKeyAlg != signedPublicKeyAlg || digestAlg != signedDigestAlg) {
|
|
|
|
return Result::ERROR_SIGNATURE_ALGORITHM_MISMATCH;
|
|
|
|
}
|
|
|
|
|
2015-02-15 03:59:02 +03:00
|
|
|
// During the time of the deprecation of SHA-1 and the deprecation of RSA
|
|
|
|
// keys of less than 2048 bits, we will encounter many certs signed using
|
|
|
|
// SHA-1 and/or too-small RSA keys. With this in mind, we ask the trust
|
|
|
|
// domain early on if it knows it will reject the signature purely based on
|
|
|
|
// the digest algorithm and/or the RSA key size (if an RSA signature). This
|
|
|
|
// is a good optimization because it completely avoids calling
|
|
|
|
// trustDomain.FindIssuers (which may be slow) for such rejected certs, and
|
|
|
|
// more generally it short-circuits any path building with them (which, of
|
|
|
|
// course, is even slower).
|
|
|
|
|
2015-09-11 21:52:30 +03:00
|
|
|
rv = trustDomain.CheckSignatureDigestAlgorithm(digestAlg, endEntityOrCA,
|
|
|
|
notBefore);
|
2015-02-15 03:59:02 +03:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (publicKeyAlg) {
|
|
|
|
case der::PublicKeyAlgorithm::RSA_PKCS1:
|
|
|
|
{
|
|
|
|
// The RSA computation may give a result that requires fewer bytes to
|
|
|
|
// encode than the public key (since it is modular arithmetic). However,
|
|
|
|
// the last step of generating a PKCS#1.5 signature is the I2OSP
|
|
|
|
// procedure, which pads any such shorter result with zeros so that it
|
|
|
|
// is exactly the same length as the public key.
|
|
|
|
unsigned int signatureSizeInBits = signedData.signature.GetLength() * 8u;
|
|
|
|
return trustDomain.CheckRSAPublicKeyModulusSizeInBits(
|
|
|
|
endEntityOrCA, signatureSizeInBits);
|
|
|
|
}
|
|
|
|
|
|
|
|
case der::PublicKeyAlgorithm::ECDSA:
|
|
|
|
// In theory, we could implement a similar early-pruning optimization for
|
|
|
|
// ECDSA curves. However, since there has been no similar deprecation for
|
|
|
|
// for any curve that we support, the chances of us encountering a curve
|
|
|
|
// during path building is too low to be worth bothering with.
|
|
|
|
break;
|
|
|
|
|
|
|
|
MOZILLA_PKIX_UNREACHABLE_DEFAULT_ENUM
|
|
|
|
}
|
|
|
|
|
2015-02-23 03:59:03 +03:00
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2015-02-03 03:17:08 +03:00
|
|
|
// 4.1.2.5 Validity
|
|
|
|
|
2014-02-03 09:21:00 +04:00
|
|
|
Result
|
2015-09-11 21:52:30 +03:00
|
|
|
ParseValidity(Input encodedValidity,
|
2015-04-07 02:10:28 +03:00
|
|
|
/*optional out*/ Time* notBeforeOut,
|
|
|
|
/*optional out*/ Time* notAfterOut)
|
2014-02-03 09:21:00 +04:00
|
|
|
{
|
2014-07-31 23:17:31 +04:00
|
|
|
Reader validity(encodedValidity);
|
2014-08-02 19:49:12 +04:00
|
|
|
Time notBefore(Time::uninitialized);
|
2014-07-15 21:33:49 +04:00
|
|
|
if (der::TimeChoice(validity, notBefore) != Success) {
|
2015-02-18 23:56:00 +03:00
|
|
|
return Result::ERROR_INVALID_DER_TIME;
|
2014-06-25 08:48:12 +04:00
|
|
|
}
|
2014-02-03 09:21:00 +04:00
|
|
|
|
2014-08-02 19:49:12 +04:00
|
|
|
Time notAfter(Time::uninitialized);
|
2014-07-15 21:33:49 +04:00
|
|
|
if (der::TimeChoice(validity, notAfter) != Success) {
|
2015-02-18 23:56:00 +03:00
|
|
|
return Result::ERROR_INVALID_DER_TIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (der::End(validity) != Success) {
|
|
|
|
return Result::ERROR_INVALID_DER_TIME;
|
2014-06-25 08:48:12 +04:00
|
|
|
}
|
2015-02-18 23:56:00 +03:00
|
|
|
|
|
|
|
if (notBefore > notAfter) {
|
|
|
|
return Result::ERROR_INVALID_DER_TIME;
|
|
|
|
}
|
|
|
|
|
2015-09-11 21:52:30 +03:00
|
|
|
if (notBeforeOut) {
|
|
|
|
*notBeforeOut = notBefore;
|
|
|
|
}
|
|
|
|
if (notAfterOut) {
|
|
|
|
*notAfterOut = notAfter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result
|
|
|
|
CheckValidity(Time time, Time notBefore, Time notAfter)
|
|
|
|
{
|
2015-02-18 23:56:00 +03:00
|
|
|
if (time < notBefore) {
|
|
|
|
return Result::ERROR_NOT_YET_VALID_CERTIFICATE;
|
|
|
|
}
|
|
|
|
|
2014-06-25 08:48:12 +04:00
|
|
|
if (time > notAfter) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_EXPIRED_CERTIFICATE;
|
2014-02-03 09:21:00 +04:00
|
|
|
}
|
|
|
|
|
2015-02-18 23:56:00 +03:00
|
|
|
return Success;
|
2014-02-03 09:21:00 +04:00
|
|
|
}
|
|
|
|
|
2015-02-03 03:17:08 +03:00
|
|
|
// 4.1.2.7 Subject Public Key Info
|
|
|
|
|
|
|
|
Result
|
2016-07-05 08:35:06 +03:00
|
|
|
CheckSubjectPublicKeyInfoContents(Reader& input, TrustDomain& trustDomain,
|
|
|
|
EndEntityOrCA endEntityOrCA)
|
2015-02-03 03:17:08 +03:00
|
|
|
{
|
|
|
|
// Here, we validate the syntax and do very basic semantic validation of the
|
|
|
|
// public key of the certificate. The intention here is to filter out the
|
|
|
|
// types of bad inputs that are most likely to trigger non-mathematical
|
|
|
|
// security vulnerabilities in the TrustDomain, like buffer overflows or the
|
|
|
|
// use of unsafe elliptic curves.
|
|
|
|
//
|
|
|
|
// We don't check (all of) the mathematical properties of the public key here
|
|
|
|
// because it is more efficient for the TrustDomain to do it during signature
|
|
|
|
// verification and/or other use of the public key. In particular, we
|
|
|
|
// delegate the arithmetic validation of the public key, as specified in
|
|
|
|
// NIST SP800-56A section 5.6.2, to the TrustDomain, at least for now.
|
|
|
|
|
|
|
|
Reader algorithm;
|
|
|
|
Input subjectPublicKey;
|
|
|
|
Result rv = der::ExpectTagAndGetValue(input, der::SEQUENCE, algorithm);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = der::BitStringWithNoUnusedBits(input, subjectPublicKey);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = der::End(input);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader subjectPublicKeyReader(subjectPublicKey);
|
|
|
|
|
|
|
|
Reader algorithmOID;
|
|
|
|
rv = der::ExpectTagAndGetValue(algorithm, der::OIDTag, algorithmOID);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 3279 Section 2.3.1
|
|
|
|
// python DottedOIDToCode.py rsaEncryption 1.2.840.113549.1.1.1
|
|
|
|
static const uint8_t rsaEncryption[] = {
|
|
|
|
0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01
|
|
|
|
};
|
|
|
|
|
|
|
|
// RFC 3279 Section 2.3.5 and RFC 5480 Section 2.1.1
|
|
|
|
// python DottedOIDToCode.py id-ecPublicKey 1.2.840.10045.2.1
|
|
|
|
static const uint8_t id_ecPublicKey[] = {
|
|
|
|
0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
|
|
|
|
};
|
|
|
|
|
|
|
|
if (algorithmOID.MatchRest(id_ecPublicKey)) {
|
|
|
|
// An id-ecPublicKey AlgorithmIdentifier has a parameter that identifes
|
|
|
|
// the curve being used. Although RFC 5480 specifies multiple forms, we
|
|
|
|
// only supported the NamedCurve form, where the curve is identified by an
|
|
|
|
// OID.
|
|
|
|
|
|
|
|
Reader namedCurveOIDValue;
|
|
|
|
rv = der::ExpectTagAndGetValue(algorithm, der::OIDTag,
|
|
|
|
namedCurveOIDValue);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 5480
|
|
|
|
// python DottedOIDToCode.py secp256r1 1.2.840.10045.3.1.7
|
|
|
|
static const uint8_t secp256r1[] = {
|
|
|
|
0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07
|
|
|
|
};
|
|
|
|
|
|
|
|
// RFC 5480
|
|
|
|
// python DottedOIDToCode.py secp384r1 1.3.132.0.34
|
|
|
|
static const uint8_t secp384r1[] = {
|
|
|
|
0x2b, 0x81, 0x04, 0x00, 0x22
|
|
|
|
};
|
|
|
|
|
|
|
|
// RFC 5480
|
|
|
|
// python DottedOIDToCode.py secp521r1 1.3.132.0.35
|
|
|
|
static const uint8_t secp521r1[] = {
|
|
|
|
0x2b, 0x81, 0x04, 0x00, 0x23
|
|
|
|
};
|
|
|
|
|
|
|
|
// Matching is attempted based on a rough estimate of the commonality of the
|
|
|
|
// elliptic curve, to minimize the number of MatchRest calls.
|
|
|
|
NamedCurve curve;
|
|
|
|
unsigned int bits;
|
|
|
|
if (namedCurveOIDValue.MatchRest(secp256r1)) {
|
|
|
|
curve = NamedCurve::secp256r1;
|
|
|
|
bits = 256;
|
|
|
|
} else if (namedCurveOIDValue.MatchRest(secp384r1)) {
|
|
|
|
curve = NamedCurve::secp384r1;
|
|
|
|
bits = 384;
|
|
|
|
} else if (namedCurveOIDValue.MatchRest(secp521r1)) {
|
|
|
|
curve = NamedCurve::secp521r1;
|
|
|
|
bits = 521;
|
|
|
|
} else {
|
|
|
|
return Result::ERROR_UNSUPPORTED_ELLIPTIC_CURVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = trustDomain.CheckECDSACurveIsAcceptable(endEntityOrCA, curve);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 5480 Section 2.2 says that the first octet will be 0x04 to indicate
|
|
|
|
// an uncompressed point, which is the only encoding we support.
|
|
|
|
uint8_t compressedOrUncompressed;
|
|
|
|
rv = subjectPublicKeyReader.Read(compressedOrUncompressed);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (compressedOrUncompressed != 0x04) {
|
|
|
|
return Result::ERROR_UNSUPPORTED_EC_POINT_FORM;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The point is encoded as two raw (not DER-encoded) integers, each padded
|
|
|
|
// to the bit length (rounded up to the nearest byte).
|
|
|
|
Input point;
|
|
|
|
rv = subjectPublicKeyReader.SkipToEnd(point);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (point.GetLength() != ((bits + 7) / 8u) * 2u) {
|
|
|
|
return Result::ERROR_BAD_DER;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: We defer the mathematical verification of the validity of the point
|
|
|
|
// until signature verification. This means that if we never verify a
|
|
|
|
// signature, we'll never fully check whether the public key is valid.
|
|
|
|
} else if (algorithmOID.MatchRest(rsaEncryption)) {
|
|
|
|
// RFC 3279 Section 2.3.1 says "The parameters field MUST have ASN.1 type
|
|
|
|
// NULL for this algorithm identifier."
|
|
|
|
rv = der::ExpectTagAndEmptyValue(algorithm, der::NULLTag);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RSAPublicKey :: = SEQUENCE{
|
|
|
|
// modulus INTEGER, --n
|
|
|
|
// publicExponent INTEGER } --e
|
|
|
|
rv = der::Nested(subjectPublicKeyReader, der::SEQUENCE,
|
|
|
|
[&trustDomain, endEntityOrCA](Reader& r) {
|
|
|
|
Input modulus;
|
|
|
|
Input::size_type modulusSignificantBytes;
|
|
|
|
Result rv = der::PositiveInteger(r, modulus, &modulusSignificantBytes);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// XXX: Should we do additional checks of the modulus?
|
|
|
|
rv = trustDomain.CheckRSAPublicKeyModulusSizeInBits(
|
|
|
|
endEntityOrCA, modulusSignificantBytes * 8u);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: We don't allow the TrustDomain to validate the exponent.
|
|
|
|
// XXX: We don't do our own sanity checking of the exponent.
|
|
|
|
Input exponent;
|
|
|
|
return der::PositiveInteger(r, exponent);
|
|
|
|
});
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Result::ERROR_UNSUPPORTED_KEYALG;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = der::End(algorithm);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = der::End(subjectPublicKeyReader);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2016-07-05 08:35:06 +03:00
|
|
|
Result
|
|
|
|
CheckSubjectPublicKeyInfo(Input subjectPublicKeyInfo, TrustDomain& trustDomain,
|
|
|
|
EndEntityOrCA endEntityOrCA)
|
|
|
|
{
|
|
|
|
Reader spkiReader(subjectPublicKeyInfo);
|
|
|
|
Result rv = der::Nested(spkiReader, der::SEQUENCE, [&](Reader& r) {
|
|
|
|
return CheckSubjectPublicKeyInfoContents(r, trustDomain, endEntityOrCA);
|
|
|
|
});
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return der::End(spkiReader);
|
|
|
|
}
|
|
|
|
|
2014-01-29 11:20:11 +04:00
|
|
|
// 4.2.1.3. Key Usage (id-ce-keyUsage)
|
2014-06-19 11:13:20 +04:00
|
|
|
|
|
|
|
// As explained in the comment in CheckKeyUsage, bit 0 is the most significant
|
|
|
|
// bit and bit 7 is the least significant bit.
|
|
|
|
inline uint8_t KeyUsageToBitMask(KeyUsage keyUsage)
|
|
|
|
{
|
2014-08-02 20:17:59 +04:00
|
|
|
assert(keyUsage != KeyUsage::noParticularKeyUsageRequired);
|
2014-06-19 11:13:20 +04:00
|
|
|
return 0x80u >> static_cast<uint8_t>(keyUsage);
|
|
|
|
}
|
|
|
|
|
2014-01-29 11:20:11 +04:00
|
|
|
Result
|
2014-07-31 23:17:31 +04:00
|
|
|
CheckKeyUsage(EndEntityOrCA endEntityOrCA, const Input* encodedKeyUsage,
|
2014-06-19 11:13:20 +04:00
|
|
|
KeyUsage requiredKeyUsageIfPresent)
|
2014-01-29 11:20:11 +04:00
|
|
|
{
|
|
|
|
if (!encodedKeyUsage) {
|
2014-04-24 00:38:19 +04:00
|
|
|
// TODO(bug 970196): Reject certificates that are being used to verify
|
|
|
|
// certificate signatures unless the certificate is a trust anchor, to
|
|
|
|
// reduce the chances of an end-entity certificate being abused as a CA
|
|
|
|
// certificate.
|
2014-04-26 03:29:26 +04:00
|
|
|
// if (endEntityOrCA == EndEntityOrCA::MustBeCA && !isTrustAnchor) {
|
2014-07-18 22:48:49 +04:00
|
|
|
// return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-01-29 11:20:11 +04:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// TODO: Users may configure arbitrary certificates as trust anchors, not
|
|
|
|
// just roots. We should only allow a certificate without a key usage to be
|
|
|
|
// used as a CA when it is self-issued and self-signed.
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2014-07-31 23:17:31 +04:00
|
|
|
Reader input(*encodedKeyUsage);
|
|
|
|
Reader value;
|
2014-07-15 21:33:49 +04:00
|
|
|
if (der::ExpectTagAndGetValue(input, der::BIT_STRING, value) != Success) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-01-29 11:20:11 +04:00
|
|
|
}
|
|
|
|
|
2014-06-19 11:13:20 +04:00
|
|
|
uint8_t numberOfPaddingBits;
|
2014-07-15 21:33:49 +04:00
|
|
|
if (value.Read(numberOfPaddingBits) != Success) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-06-19 11:13:20 +04:00
|
|
|
}
|
|
|
|
if (numberOfPaddingBits > 7) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-06-19 11:13:20 +04:00
|
|
|
}
|
2014-01-29 11:20:11 +04:00
|
|
|
|
2014-06-19 11:13:20 +04:00
|
|
|
uint8_t bits;
|
2014-07-15 21:33:49 +04:00
|
|
|
if (value.Read(bits) != Success) {
|
2014-06-19 11:13:20 +04:00
|
|
|
// Reject empty bit masks.
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-01-29 11:20:11 +04:00
|
|
|
}
|
|
|
|
|
2014-06-19 11:13:20 +04:00
|
|
|
// The most significant bit is numbered 0 (digitalSignature) and the least
|
|
|
|
// significant bit is numbered 7 (encipherOnly), and the padding is in the
|
|
|
|
// least significant bits of the last byte. The numbering of bits in a byte
|
|
|
|
// is backwards from how we usually interpret them.
|
|
|
|
//
|
|
|
|
// For example, let's say bits is encoded in one byte with of value 0xB0 and
|
|
|
|
// numberOfPaddingBits == 4. Then, bits is 10110000 in binary:
|
|
|
|
//
|
|
|
|
// bit 0 bit 3
|
|
|
|
// | |
|
|
|
|
// v v
|
|
|
|
// 10110000
|
|
|
|
// ^^^^
|
|
|
|
// |
|
|
|
|
// 4 padding bits
|
|
|
|
//
|
|
|
|
// Since bits is the last byte, we have to consider the padding by ensuring
|
|
|
|
// that the least significant 4 bits are all zero, since DER rules require
|
|
|
|
// all padding bits to be zero. Then we have to look at the bit N bits to the
|
|
|
|
// right of the most significant bit, where N is a value from the KeyUsage
|
|
|
|
// enumeration.
|
|
|
|
//
|
|
|
|
// Let's say we're interested in the keyCertSign (5) bit. We'd need to look
|
|
|
|
// at bit 5, which is zero, so keyCertSign is not asserted. (Since we check
|
|
|
|
// that the padding is all zeros, it is OK to read from the padding bits.)
|
|
|
|
//
|
|
|
|
// Let's say we're interested in the digitalSignature (0) bit. We'd need to
|
|
|
|
// look at the bit 0 (the most significant bit), which is set, so that means
|
|
|
|
// digitalSignature is asserted. Similarly, keyEncipherment (2) and
|
|
|
|
// dataEncipherment (3) are asserted.
|
|
|
|
//
|
|
|
|
// Note that since the KeyUsage enumeration is limited to values 0-7, we
|
|
|
|
// only ever need to examine the first byte test for
|
|
|
|
// requiredKeyUsageIfPresent.
|
|
|
|
|
|
|
|
if (requiredKeyUsageIfPresent != KeyUsage::noParticularKeyUsageRequired) {
|
|
|
|
// Check that the required key usage bit is set.
|
|
|
|
if ((bits & KeyUsageToBitMask(requiredKeyUsageIfPresent)) == 0) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-01-29 11:20:11 +04:00
|
|
|
}
|
2014-06-19 11:13:20 +04:00
|
|
|
}
|
|
|
|
|
2014-09-03 21:12:55 +04:00
|
|
|
// RFC 5280 says "The keyCertSign bit is asserted when the subject public
|
|
|
|
// key is used for verifying signatures on public key certificates. If the
|
|
|
|
// keyCertSign bit is asserted, then the cA bit in the basic constraints
|
|
|
|
// extension (Section 4.2.1.9) MUST also be asserted."
|
|
|
|
// However, we allow end-entity certificates (i.e. certificates without
|
|
|
|
// basicConstraints.cA set to TRUE) to claim keyCertSign for compatibility
|
|
|
|
// reasons. This does not compromise security because we only allow
|
|
|
|
// certificates with basicConstraints.cA set to TRUE to act as CAs.
|
|
|
|
if (requiredKeyUsageIfPresent == KeyUsage::keyCertSign &&
|
|
|
|
endEntityOrCA != EndEntityOrCA::MustBeCA) {
|
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-06-19 11:13:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The padding applies to the last byte, so skip to the last byte.
|
|
|
|
while (!value.AtEnd()) {
|
2014-07-15 21:33:49 +04:00
|
|
|
if (value.Read(bits) != Success) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-06-19 11:13:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All of the padding bits must be zero, according to DER rules.
|
|
|
|
uint8_t paddingMask = static_cast<uint8_t>((1 << numberOfPaddingBits) - 1);
|
|
|
|
if ((bits & paddingMask) != 0) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_KEY_USAGE;
|
2014-01-29 11:20:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:37:45 +04:00
|
|
|
// RFC5820 4.2.1.4. Certificate Policies
|
2014-05-16 05:59:52 +04:00
|
|
|
|
2014-02-25 00:37:45 +04:00
|
|
|
// "The user-initial-policy-set contains the special value any-policy if the
|
|
|
|
// user is not concerned about certificate policy."
|
2014-05-16 05:59:52 +04:00
|
|
|
//
|
2014-07-20 05:51:10 +04:00
|
|
|
// python DottedOIDToCode.py anyPolicy 2.5.29.32.0
|
2014-05-16 05:59:52 +04:00
|
|
|
|
2014-07-20 05:51:10 +04:00
|
|
|
static const uint8_t anyPolicy[] = {
|
|
|
|
0x55, 0x1d, 0x20, 0x00
|
2014-05-16 05:59:52 +04:00
|
|
|
};
|
|
|
|
|
2014-07-20 05:51:10 +04:00
|
|
|
/*static*/ const CertPolicyId CertPolicyId::anyPolicy = {
|
|
|
|
4, { 0x55, 0x1d, 0x20, 0x00 }
|
|
|
|
};
|
2014-05-16 05:59:52 +04:00
|
|
|
|
2014-07-20 05:51:10 +04:00
|
|
|
bool
|
|
|
|
CertPolicyId::IsAnyPolicy() const {
|
|
|
|
if (this == &CertPolicyId::anyPolicy) {
|
|
|
|
return true;
|
2014-05-16 05:59:52 +04:00
|
|
|
}
|
2014-08-04 05:24:35 +04:00
|
|
|
return numBytes == sizeof(::mozilla::pkix::anyPolicy) &&
|
2015-09-09 15:16:59 +03:00
|
|
|
std::equal(bytes, bytes + numBytes, ::mozilla::pkix::anyPolicy);
|
2014-05-16 05:59:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
|
2014-02-25 00:37:45 +04:00
|
|
|
Result
|
2014-05-16 05:59:52 +04:00
|
|
|
CheckCertificatePolicies(EndEntityOrCA endEntityOrCA,
|
2014-07-31 23:17:31 +04:00
|
|
|
const Input* encodedCertificatePolicies,
|
|
|
|
const Input* encodedInhibitAnyPolicy,
|
2014-05-16 05:59:52 +04:00
|
|
|
TrustLevel trustLevel,
|
|
|
|
const CertPolicyId& requiredPolicy)
|
2014-02-25 00:37:45 +04:00
|
|
|
{
|
2014-05-16 05:59:52 +04:00
|
|
|
if (requiredPolicy.numBytes == 0 ||
|
|
|
|
requiredPolicy.numBytes > sizeof requiredPolicy.bytes) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::FATAL_ERROR_INVALID_ARGS;
|
2014-02-25 00:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-07-20 05:51:10 +04:00
|
|
|
bool requiredPolicyFound = requiredPolicy.IsAnyPolicy();
|
|
|
|
if (requiredPolicyFound) {
|
2014-05-16 05:59:52 +04:00
|
|
|
return Success;
|
2014-02-25 00:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-03-28 21:00:29 +04:00
|
|
|
// Bug 989051. Until we handle inhibitAnyPolicy we will fail close when
|
2014-07-20 05:51:10 +04:00
|
|
|
// inhibitAnyPolicy extension is present and we are validating for a policy.
|
|
|
|
if (!requiredPolicyFound && encodedInhibitAnyPolicy) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_POLICY_VALIDATION_FAILED;
|
2014-03-28 21:00:29 +04:00
|
|
|
}
|
|
|
|
|
2014-02-25 00:37:45 +04:00
|
|
|
// The root CA certificate may omit the policies that it has been
|
|
|
|
// trusted for, so we cannot require the policies to be present in those
|
|
|
|
// certificates. Instead, the determination of which roots are trusted for
|
|
|
|
// which policies is made by the TrustDomain's GetCertTrust method.
|
2014-05-16 05:59:52 +04:00
|
|
|
if (trustLevel == TrustLevel::TrustAnchor &&
|
|
|
|
endEntityOrCA == EndEntityOrCA::MustBeCA) {
|
2014-07-20 05:51:10 +04:00
|
|
|
requiredPolicyFound = true;
|
2014-02-25 00:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-07-20 05:51:10 +04:00
|
|
|
Input requiredPolicyDER;
|
|
|
|
if (requiredPolicyDER.Init(requiredPolicy.bytes, requiredPolicy.numBytes)
|
|
|
|
!= Success) {
|
|
|
|
return Result::FATAL_ERROR_INVALID_ARGS;
|
2014-02-25 00:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-07-20 05:51:10 +04:00
|
|
|
if (encodedCertificatePolicies) {
|
|
|
|
Reader extension(*encodedCertificatePolicies);
|
|
|
|
Reader certificatePolicies;
|
|
|
|
Result rv = der::ExpectTagAndGetValue(extension, der::SEQUENCE,
|
|
|
|
certificatePolicies);
|
|
|
|
if (rv != Success) {
|
|
|
|
return Result::ERROR_POLICY_VALIDATION_FAILED;
|
|
|
|
}
|
|
|
|
if (!extension.AtEnd()) {
|
|
|
|
return Result::ERROR_POLICY_VALIDATION_FAILED;
|
|
|
|
}
|
2014-02-25 00:37:45 +04:00
|
|
|
|
2014-07-20 05:51:10 +04:00
|
|
|
do {
|
|
|
|
// PolicyInformation ::= SEQUENCE {
|
|
|
|
// policyIdentifier CertPolicyId,
|
|
|
|
// policyQualifiers SEQUENCE SIZE (1..MAX) OF
|
|
|
|
// PolicyQualifierInfo OPTIONAL }
|
|
|
|
Reader policyInformation;
|
|
|
|
rv = der::ExpectTagAndGetValue(certificatePolicies, der::SEQUENCE,
|
|
|
|
policyInformation);
|
|
|
|
if (rv != Success) {
|
|
|
|
return Result::ERROR_POLICY_VALIDATION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader policyIdentifier;
|
|
|
|
rv = der::ExpectTagAndGetValue(policyInformation, der::OIDTag,
|
|
|
|
policyIdentifier);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (policyIdentifier.MatchRest(requiredPolicyDER)) {
|
|
|
|
requiredPolicyFound = true;
|
|
|
|
} else if (endEntityOrCA == EndEntityOrCA::MustBeCA &&
|
|
|
|
policyIdentifier.MatchRest(anyPolicy)) {
|
|
|
|
requiredPolicyFound = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 5280 Section 4.2.1.4 says "Optional qualifiers, which MAY be
|
|
|
|
// present, are not expected to change the definition of the policy." Also,
|
|
|
|
// it seems that Section 6, which defines validation, does not require any
|
|
|
|
// matching of qualifiers. Thus, doing anything with the policy qualifiers
|
|
|
|
// would be a waste of time and a source of potential incompatibilities, so
|
|
|
|
// we just ignore them.
|
|
|
|
} while (!requiredPolicyFound && !certificatePolicies.AtEnd());
|
2014-05-16 05:59:52 +04:00
|
|
|
}
|
2014-07-20 05:51:10 +04:00
|
|
|
|
|
|
|
if (!requiredPolicyFound) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_POLICY_VALIDATION_FAILED;
|
2014-02-25 00:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-05-16 05:59:52 +04:00
|
|
|
return Success;
|
2014-02-25 00:37:45 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 20:55:57 +04:00
|
|
|
static const long UNLIMITED_PATH_LEN = -1; // must be less than zero
|
|
|
|
|
2014-03-21 22:52:01 +04:00
|
|
|
// BasicConstraints ::= SEQUENCE {
|
|
|
|
// cA BOOLEAN DEFAULT FALSE,
|
|
|
|
// pathLenConstraint INTEGER (0..MAX) OPTIONAL }
|
|
|
|
|
2014-02-07 06:13:20 +04:00
|
|
|
// RFC5280 4.2.1.9. Basic Constraints (id-ce-basicConstraints)
|
|
|
|
Result
|
2014-04-26 07:27:27 +04:00
|
|
|
CheckBasicConstraints(EndEntityOrCA endEntityOrCA,
|
2014-07-31 23:17:31 +04:00
|
|
|
const Input* encodedBasicConstraints,
|
2014-07-09 03:16:26 +04:00
|
|
|
const der::Version version, TrustLevel trustLevel,
|
2014-02-07 06:13:20 +04:00
|
|
|
unsigned int subCACount)
|
|
|
|
{
|
2014-05-05 20:55:57 +04:00
|
|
|
bool isCA = false;
|
|
|
|
long pathLenConstraint = UNLIMITED_PATH_LEN;
|
|
|
|
|
2014-04-26 07:27:27 +04:00
|
|
|
if (encodedBasicConstraints) {
|
2014-07-31 23:17:31 +04:00
|
|
|
Reader input(*encodedBasicConstraints);
|
2015-01-21 15:00:40 +03:00
|
|
|
Result rv = der::Nested(input, der::SEQUENCE,
|
|
|
|
[&isCA, &pathLenConstraint](Reader& r) {
|
|
|
|
Result rv = der::OptionalBoolean(r, isCA);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// TODO(bug 985025): If isCA is false, pathLenConstraint
|
|
|
|
// MUST NOT be included (as per RFC 5280 section
|
|
|
|
// 4.2.1.9), but for compatibility reasons, we don't
|
|
|
|
// check this.
|
|
|
|
return der::OptionalInteger(r, UNLIMITED_PATH_LEN, pathLenConstraint);
|
|
|
|
});
|
|
|
|
if (rv != Success) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_EXTENSION_VALUE_INVALID;
|
2014-05-05 20:55:57 +04:00
|
|
|
}
|
2014-07-15 21:33:49 +04:00
|
|
|
if (der::End(input) != Success) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_EXTENSION_VALUE_INVALID;
|
2014-02-07 06:13:20 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// "If the basic constraints extension is not present in a version 3
|
|
|
|
// certificate, or the extension is present but the cA boolean is not
|
|
|
|
// asserted, then the certified public key MUST NOT be used to verify
|
|
|
|
// certificate signatures."
|
|
|
|
//
|
|
|
|
// For compatibility, we must accept v1 trust anchors without basic
|
|
|
|
// constraints as CAs.
|
|
|
|
//
|
2014-10-15 21:38:51 +04:00
|
|
|
// There are devices with v1 certificates that are unlikely to be trust
|
|
|
|
// anchors. In order to allow applications to treat this case differently
|
|
|
|
// from other basic constraints violations (e.g. allowing certificate error
|
|
|
|
// overrides for only this case), we return a different error code.
|
|
|
|
//
|
2014-02-07 06:13:20 +04:00
|
|
|
// TODO: add check for self-signedness?
|
2014-10-15 21:38:51 +04:00
|
|
|
if (endEntityOrCA == EndEntityOrCA::MustBeCA && version == der::Version::v1) {
|
|
|
|
if (trustLevel == TrustLevel::TrustAnchor) {
|
|
|
|
isCA = true;
|
|
|
|
} else {
|
|
|
|
return Result::ERROR_V1_CERT_USED_AS_CA;
|
|
|
|
}
|
2014-02-07 06:13:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-26 03:29:26 +04:00
|
|
|
if (endEntityOrCA == EndEntityOrCA::MustBeEndEntity) {
|
2014-02-07 06:13:20 +04:00
|
|
|
// CA certificates are not trusted as EE certs.
|
|
|
|
|
2014-05-05 20:55:57 +04:00
|
|
|
if (isCA) {
|
2014-08-11 23:35:45 +04:00
|
|
|
// Note that this check prevents a delegated OCSP response signing
|
|
|
|
// certificate with the CA bit from successfully validating when we check
|
|
|
|
// it from pkixocsp.cpp, which is a good thing.
|
|
|
|
return Result::ERROR_CA_CERT_USED_AS_END_ENTITY;
|
2014-02-07 06:13:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2014-08-18 03:50:45 +04:00
|
|
|
assert(endEntityOrCA == EndEntityOrCA::MustBeCA);
|
2014-02-07 06:13:20 +04:00
|
|
|
|
|
|
|
// End-entity certificates are not allowed to act as CA certs.
|
2014-05-05 20:55:57 +04:00
|
|
|
if (!isCA) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_CA_CERT_INVALID;
|
2014-02-07 06:13:20 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 20:55:57 +04:00
|
|
|
if (pathLenConstraint >= 0 &&
|
|
|
|
static_cast<long>(subCACount) > pathLenConstraint) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_PATH_LEN_CONSTRAINT_INVALID;
|
2014-02-07 06:13:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2014-02-09 03:00:32 +04:00
|
|
|
// 4.2.1.12. Extended Key Usage (id-ce-extKeyUsage)
|
2014-05-14 12:02:34 +04:00
|
|
|
|
2014-07-15 21:33:49 +04:00
|
|
|
static Result
|
2014-07-31 23:17:31 +04:00
|
|
|
MatchEKU(Reader& value, KeyPurposeId requiredEKU,
|
2016-05-06 02:11:11 +03:00
|
|
|
EndEntityOrCA endEntityOrCA, TrustDomain& trustDomain,
|
|
|
|
Time notBefore, /*in/out*/ bool& found,
|
2014-05-14 12:02:34 +04:00
|
|
|
/*in/out*/ bool& foundOCSPSigning)
|
2014-02-09 03:00:32 +04:00
|
|
|
{
|
2014-05-14 12:02:34 +04:00
|
|
|
// See Section 5.9 of "A Layman's Guide to a Subset of ASN.1, BER, and DER"
|
|
|
|
// for a description of ASN.1 DER encoding of OIDs.
|
|
|
|
|
|
|
|
// id-pkix OBJECT IDENTIFIER ::=
|
|
|
|
// { iso(1) identified-organization(3) dod(6) internet(1)
|
|
|
|
// security(5) mechanisms(5) pkix(7) }
|
|
|
|
// id-kp OBJECT IDENTIFIER ::= { id-pkix 3 }
|
|
|
|
// id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 }
|
|
|
|
// id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 }
|
|
|
|
// id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 }
|
|
|
|
// id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 }
|
|
|
|
// id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 }
|
|
|
|
static const uint8_t server[] = { (40*1)+3, 6, 1, 5, 5, 7, 3, 1 };
|
|
|
|
static const uint8_t client[] = { (40*1)+3, 6, 1, 5, 5, 7, 3, 2 };
|
|
|
|
static const uint8_t code [] = { (40*1)+3, 6, 1, 5, 5, 7, 3, 3 };
|
|
|
|
static const uint8_t email [] = { (40*1)+3, 6, 1, 5, 5, 7, 3, 4 };
|
|
|
|
static const uint8_t ocsp [] = { (40*1)+3, 6, 1, 5, 5, 7, 3, 9 };
|
|
|
|
|
|
|
|
// id-Netscape OBJECT IDENTIFIER ::= { 2 16 840 1 113730 }
|
|
|
|
// id-Netscape-policy OBJECT IDENTIFIER ::= { id-Netscape 4 }
|
|
|
|
// id-Netscape-stepUp OBJECT IDENTIFIER ::= { id-Netscape-policy 1 }
|
|
|
|
static const uint8_t serverStepUp[] =
|
|
|
|
{ (40*2)+16, 128+6,72, 1, 128+6,128+120,66, 4, 1 };
|
|
|
|
|
|
|
|
bool match = false;
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
switch (requiredEKU) {
|
2016-05-06 02:11:11 +03:00
|
|
|
case KeyPurposeId::id_kp_serverAuth: {
|
|
|
|
if (value.MatchRest(server)) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Potentially treat CA certs with step-up OID as also having SSL server
|
|
|
|
// type. Comodo has issued certificates that require this behavior that
|
|
|
|
// don't expire until June 2020!
|
|
|
|
if (endEntityOrCA == EndEntityOrCA::MustBeCA &&
|
|
|
|
value.MatchRest(serverStepUp)) {
|
|
|
|
Result rv = trustDomain.NetscapeStepUpMatchesServerAuth(notBefore,
|
|
|
|
match);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 12:02:34 +04:00
|
|
|
break;
|
2016-05-06 02:11:11 +03:00
|
|
|
}
|
2014-05-14 12:02:34 +04:00
|
|
|
|
|
|
|
case KeyPurposeId::id_kp_clientAuth:
|
2014-05-30 09:09:45 +04:00
|
|
|
match = value.MatchRest(client);
|
2014-05-14 12:02:34 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyPurposeId::id_kp_codeSigning:
|
2014-05-30 09:09:45 +04:00
|
|
|
match = value.MatchRest(code);
|
2014-05-14 12:02:34 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyPurposeId::id_kp_emailProtection:
|
2014-05-30 09:09:45 +04:00
|
|
|
match = value.MatchRest(email);
|
2014-05-14 12:02:34 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyPurposeId::id_kp_OCSPSigning:
|
2014-05-30 09:09:45 +04:00
|
|
|
match = value.MatchRest(ocsp);
|
2014-05-14 12:02:34 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyPurposeId::anyExtendedKeyUsage:
|
2014-08-04 05:24:35 +04:00
|
|
|
return NotReached("anyExtendedKeyUsage should start with found==true",
|
|
|
|
Result::FATAL_ERROR_LIBRARY_FAILURE);
|
2014-05-14 12:02:34 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 03:00:32 +04:00
|
|
|
|
2014-05-14 12:02:34 +04:00
|
|
|
if (match) {
|
2014-05-30 09:09:45 +04:00
|
|
|
found = true;
|
|
|
|
if (requiredEKU == KeyPurposeId::id_kp_OCSPSigning) {
|
|
|
|
foundOCSPSigning = true;
|
2014-05-14 12:02:34 +04:00
|
|
|
}
|
2014-05-30 09:09:45 +04:00
|
|
|
} else if (value.MatchRest(ocsp)) {
|
2014-05-14 12:02:34 +04:00
|
|
|
foundOCSPSigning = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
value.SkipToEnd(); // ignore unmatched OIDs.
|
|
|
|
|
2014-07-15 21:33:49 +04:00
|
|
|
return Success;
|
2014-05-14 12:02:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Result
|
|
|
|
CheckExtendedKeyUsage(EndEntityOrCA endEntityOrCA,
|
2014-07-31 23:17:31 +04:00
|
|
|
const Input* encodedExtendedKeyUsage,
|
2016-05-06 02:11:11 +03:00
|
|
|
KeyPurposeId requiredEKU, TrustDomain& trustDomain,
|
|
|
|
Time notBefore)
|
2014-05-14 12:02:34 +04:00
|
|
|
{
|
2014-07-18 22:48:49 +04:00
|
|
|
// XXX: We're using Result::ERROR_INADEQUATE_CERT_TYPE here so that callers
|
|
|
|
// can distinguish EKU mismatch from KU mismatch from basic constraints
|
|
|
|
// mismatch. We should probably add a new error code that is more clear for
|
|
|
|
// this type of problem.
|
2014-02-09 03:00:32 +04:00
|
|
|
|
|
|
|
bool foundOCSPSigning = false;
|
|
|
|
|
2014-05-14 12:02:34 +04:00
|
|
|
if (encodedExtendedKeyUsage) {
|
|
|
|
bool found = requiredEKU == KeyPurposeId::anyExtendedKeyUsage;
|
2014-02-09 03:00:32 +04:00
|
|
|
|
2014-07-31 23:17:31 +04:00
|
|
|
Reader input(*encodedExtendedKeyUsage);
|
2015-01-21 15:00:40 +03:00
|
|
|
Result rv = der::NestedOf(input, der::SEQUENCE, der::OIDTag,
|
|
|
|
der::EmptyAllowed::No, [&](Reader& r) {
|
2016-05-06 02:11:11 +03:00
|
|
|
return MatchEKU(r, requiredEKU, endEntityOrCA, trustDomain, notBefore,
|
|
|
|
found, foundOCSPSigning);
|
2015-01-21 15:00:40 +03:00
|
|
|
});
|
|
|
|
if (rv != Success) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_CERT_TYPE;
|
2014-05-14 12:02:34 +04:00
|
|
|
}
|
2014-07-15 21:33:49 +04:00
|
|
|
if (der::End(input) != Success) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_CERT_TYPE;
|
2014-02-09 03:00:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the EKU extension was included, then the required EKU must be in the
|
|
|
|
// list.
|
|
|
|
if (!found) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_CERT_TYPE;
|
2014-02-09 03:00:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pkixocsp.cpp depends on the following additional checks.
|
|
|
|
|
2014-04-26 03:29:26 +04:00
|
|
|
if (endEntityOrCA == EndEntityOrCA::MustBeEndEntity) {
|
2014-02-09 03:00:32 +04:00
|
|
|
// When validating anything other than an delegated OCSP signing cert,
|
|
|
|
// reject any cert that also claims to be an OCSP responder, because such
|
|
|
|
// a cert does not make sense. For example, if an SSL certificate were to
|
|
|
|
// assert id-kp-OCSPSigning then it could sign OCSP responses for itself,
|
|
|
|
// if not for this check.
|
2014-04-10 21:15:02 +04:00
|
|
|
// That said, we accept CA certificates with id-kp-OCSPSigning because
|
|
|
|
// some CAs in Mozilla's CA program have issued such intermediate
|
|
|
|
// certificates, and because some CAs have reported some Microsoft server
|
|
|
|
// software wrongly requires CA certificates to have id-kp-OCSPSigning.
|
|
|
|
// Allowing this exception does not cause any security issues because we
|
|
|
|
// require delegated OCSP response signing certificates to be end-entity
|
|
|
|
// certificates.
|
2014-05-14 12:02:34 +04:00
|
|
|
if (foundOCSPSigning && requiredEKU != KeyPurposeId::id_kp_OCSPSigning) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_CERT_TYPE;
|
2014-02-09 03:00:32 +04:00
|
|
|
}
|
|
|
|
// http://tools.ietf.org/html/rfc6960#section-4.2.2.2:
|
|
|
|
// "OCSP signing delegation SHALL be designated by the inclusion of
|
|
|
|
// id-kp-OCSPSigning in an extended key usage certificate extension
|
|
|
|
// included in the OCSP response signer's certificate."
|
|
|
|
//
|
|
|
|
// id-kp-OCSPSigning is the only EKU that isn't implicitly assumed when the
|
|
|
|
// EKU extension is missing from an end-entity certificate. However, any CA
|
|
|
|
// certificate can issue a delegated OCSP response signing certificate, so
|
|
|
|
// we can't require the EKU be explicitly included for CA certificates.
|
2014-05-14 12:02:34 +04:00
|
|
|
if (!foundOCSPSigning && requiredEKU == KeyPurposeId::id_kp_OCSPSigning) {
|
2014-07-18 22:48:49 +04:00
|
|
|
return Result::ERROR_INADEQUATE_CERT_TYPE;
|
2014-04-10 21:15:02 +04:00
|
|
|
}
|
2014-02-09 03:00:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2015-11-13 19:49:08 +03:00
|
|
|
Result
|
|
|
|
CheckTLSFeatures(const BackCert& subject, BackCert& potentialIssuer)
|
|
|
|
{
|
|
|
|
const Input* issuerTLSFeatures = potentialIssuer.GetRequiredTLSFeatures();
|
|
|
|
if (!issuerTLSFeatures) {
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Input* subjectTLSFeatures = subject.GetRequiredTLSFeatures();
|
|
|
|
if (issuerTLSFeatures->GetLength() == 0 ||
|
|
|
|
!subjectTLSFeatures ||
|
|
|
|
!InputsAreEqual(*issuerTLSFeatures, *subjectTLSFeatures)) {
|
|
|
|
return Result::ERROR_REQUIRED_TLS_FEATURE_MISSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result
|
|
|
|
TLSFeaturesSatisfiedInternal(const Input* requiredTLSFeatures,
|
|
|
|
const Input* stapledOCSPResponse)
|
|
|
|
{
|
|
|
|
if (!requiredTLSFeatures) {
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFC 6066 10.2: ExtensionType status_request
|
|
|
|
const static uint8_t status_request = 5;
|
|
|
|
const static uint8_t status_request_bytes[] = { status_request };
|
|
|
|
|
|
|
|
Reader input(*requiredTLSFeatures);
|
|
|
|
return der::NestedOf(input, der::SEQUENCE, der::INTEGER,
|
|
|
|
der::EmptyAllowed::No, [&](Reader& r) {
|
|
|
|
if (!r.MatchRest(status_request_bytes)) {
|
|
|
|
return Result::ERROR_REQUIRED_TLS_FEATURE_MISSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stapledOCSPResponse) {
|
|
|
|
return Result::ERROR_REQUIRED_TLS_FEATURE_MISSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result::Success;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Result
|
|
|
|
CheckTLSFeaturesAreSatisfied(Input& cert,
|
|
|
|
const Input* stapledOCSPResponse)
|
|
|
|
{
|
|
|
|
BackCert backCert(cert, EndEntityOrCA::MustBeEndEntity, nullptr);
|
|
|
|
Result rv = backCert.Init();
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TLSFeaturesSatisfiedInternal(backCert.GetRequiredTLSFeatures(),
|
|
|
|
stapledOCSPResponse);
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:13:20 +04:00
|
|
|
Result
|
2014-02-17 06:09:06 +04:00
|
|
|
CheckIssuerIndependentProperties(TrustDomain& trustDomain,
|
2014-07-04 03:59:42 +04:00
|
|
|
const BackCert& cert,
|
2014-08-02 19:49:12 +04:00
|
|
|
Time time,
|
2014-06-19 11:13:20 +04:00
|
|
|
KeyUsage requiredKeyUsageIfPresent,
|
2014-05-14 12:02:34 +04:00
|
|
|
KeyPurposeId requiredEKUIfPresent,
|
2014-05-16 05:59:52 +04:00
|
|
|
const CertPolicyId& requiredPolicy,
|
2014-02-17 06:09:06 +04:00
|
|
|
unsigned int subCACount,
|
2014-07-20 22:06:26 +04:00
|
|
|
/*out*/ TrustLevel& trustLevel)
|
2014-02-07 06:13:20 +04:00
|
|
|
{
|
2014-02-17 06:09:06 +04:00
|
|
|
Result rv;
|
|
|
|
|
2014-07-09 00:04:17 +04:00
|
|
|
const EndEntityOrCA endEntityOrCA = cert.endEntityOrCA;
|
|
|
|
|
2015-02-23 03:59:03 +03:00
|
|
|
// Check the cert's trust first, because we want to minimize the amount of
|
|
|
|
// processing we do on a distrusted cert, in case it is trying to exploit
|
|
|
|
// some bug in our processing.
|
2014-07-18 22:48:49 +04:00
|
|
|
rv = trustDomain.GetCertTrust(endEntityOrCA, requiredPolicy, cert.GetDER(),
|
2014-07-20 22:06:26 +04:00
|
|
|
trustLevel);
|
2014-02-17 06:09:06 +04:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-02-23 03:59:03 +03:00
|
|
|
|
2015-09-11 21:52:30 +03:00
|
|
|
// IMPORTANT: We parse the validity interval here, so that we can use the
|
|
|
|
// notBefore and notAfter values in checks for things that might be deprecated
|
|
|
|
// over time. However, we must not fail for semantic errors until the end of
|
|
|
|
// this method, in order to preserve error ranking.
|
|
|
|
Time notBefore(Time::uninitialized);
|
|
|
|
Time notAfter(Time::uninitialized);
|
|
|
|
rv = ParseValidity(cert.GetValidity(), ¬Before, ¬After);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-02-15 02:59:38 +03:00
|
|
|
if (trustLevel == TrustLevel::TrustAnchor &&
|
|
|
|
endEntityOrCA == EndEntityOrCA::MustBeEndEntity &&
|
|
|
|
requiredEKUIfPresent == KeyPurposeId::id_kp_OCSPSigning) {
|
|
|
|
// OCSP signer certificates can never be trust anchors, especially
|
|
|
|
// since we don't support designated OCSP responders. All of the checks
|
|
|
|
// below that are dependent on trustLevel rely on this overriding of the
|
|
|
|
// trust level for OCSP signers.
|
|
|
|
trustLevel = TrustLevel::InheritsTrust;
|
|
|
|
}
|
|
|
|
|
2015-02-23 03:59:03 +03:00
|
|
|
switch (trustLevel) {
|
|
|
|
case TrustLevel::InheritsTrust:
|
2015-09-11 21:52:30 +03:00
|
|
|
rv = CheckSignatureAlgorithm(trustDomain, endEntityOrCA, notBefore,
|
2015-02-15 03:59:02 +03:00
|
|
|
cert.GetSignedData(), cert.GetSignature());
|
2015-02-23 03:59:03 +03:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TrustLevel::TrustAnchor:
|
|
|
|
// We don't even bother checking signatureAlgorithm or signature for
|
|
|
|
// syntactic validity for trust anchors, because we don't use those
|
|
|
|
// fields for anything, and because the trust anchor might be signed
|
|
|
|
// with a signature algorithm we don't actually support.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TrustLevel::ActivelyDistrusted:
|
|
|
|
return Result::ERROR_UNTRUSTED_CERT;
|
2014-02-17 06:09:06 +04:00
|
|
|
}
|
|
|
|
|
2015-02-23 03:59:03 +03:00
|
|
|
// Check the SPKI early, because it is one of the most selective properties
|
2015-02-03 03:17:08 +03:00
|
|
|
// of the certificate due to SHA-1 deprecation and the deprecation of
|
|
|
|
// certificates with keys weaker than RSA 2048.
|
2016-07-05 08:35:06 +03:00
|
|
|
rv = CheckSubjectPublicKeyInfo(cert.GetSubjectPublicKeyInfo(), trustDomain,
|
|
|
|
endEntityOrCA);
|
2015-02-03 03:17:08 +03:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:37:45 +04:00
|
|
|
// 4.2.1.1. Authority Key Identifier is ignored (see bug 965136).
|
|
|
|
|
|
|
|
// 4.2.1.2. Subject Key Identifier is ignored (see bug 965136).
|
2014-01-29 11:20:11 +04:00
|
|
|
|
2014-02-25 00:37:45 +04:00
|
|
|
// 4.2.1.3. Key Usage
|
2014-07-04 03:59:42 +04:00
|
|
|
rv = CheckKeyUsage(endEntityOrCA, cert.GetKeyUsage(),
|
2014-06-19 11:13:20 +04:00
|
|
|
requiredKeyUsageIfPresent);
|
2014-01-29 11:20:11 +04:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:13:20 +04:00
|
|
|
// 4.2.1.4. Certificate Policies
|
2014-07-04 03:59:42 +04:00
|
|
|
rv = CheckCertificatePolicies(endEntityOrCA, cert.GetCertificatePolicies(),
|
|
|
|
cert.GetInhibitAnyPolicy(), trustLevel,
|
2014-02-25 00:37:45 +04:00
|
|
|
requiredPolicy);
|
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4.2.1.5. Policy Mappings are not supported; see the documentation about
|
|
|
|
// policy enforcement in pkix.h.
|
|
|
|
|
|
|
|
// 4.2.1.6. Subject Alternative Name dealt with during name constraint
|
|
|
|
// checking and during name verification (CERT_VerifyCertName).
|
|
|
|
|
|
|
|
// 4.2.1.7. Issuer Alternative Name is not something that needs checking.
|
|
|
|
|
|
|
|
// 4.2.1.8. Subject Directory Attributes is not something that needs
|
|
|
|
// checking.
|
|
|
|
|
|
|
|
// 4.2.1.9. Basic Constraints.
|
2014-07-04 03:59:42 +04:00
|
|
|
rv = CheckBasicConstraints(endEntityOrCA, cert.GetBasicConstraints(),
|
|
|
|
cert.GetVersion(), trustLevel, subCACount);
|
2014-02-07 06:13:20 +04:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:37:45 +04:00
|
|
|
// 4.2.1.10. Name Constraints is dealt with in during path building.
|
|
|
|
|
|
|
|
// 4.2.1.11. Policy Constraints are implicitly supported; see the
|
|
|
|
// documentation about policy enforcement in pkix.h.
|
2013-09-13 11:09:08 +04:00
|
|
|
|
2014-02-07 06:13:20 +04:00
|
|
|
// 4.2.1.12. Extended Key Usage
|
2014-07-04 03:59:42 +04:00
|
|
|
rv = CheckExtendedKeyUsage(endEntityOrCA, cert.GetExtKeyUsage(),
|
2016-05-06 02:11:11 +03:00
|
|
|
requiredEKUIfPresent, trustDomain, notBefore);
|
2014-02-09 03:00:32 +04:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:37:45 +04:00
|
|
|
// 4.2.1.13. CRL Distribution Points is not supported, though the
|
|
|
|
// TrustDomain's CheckRevocation method may parse it and process it
|
|
|
|
// on its own.
|
|
|
|
|
|
|
|
// 4.2.1.14. Inhibit anyPolicy is implicitly supported; see the documentation
|
|
|
|
// about policy enforcement in pkix.h.
|
2014-02-07 06:13:20 +04:00
|
|
|
|
2015-09-11 21:52:30 +03:00
|
|
|
// IMPORTANT: Even though we parse validity above, we wait until this point to
|
|
|
|
// check it, so that error ranking works correctly.
|
|
|
|
rv = CheckValidity(time, notBefore, notAfter);
|
2015-06-29 23:19:00 +03:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = trustDomain.CheckValidityIsAcceptable(notBefore, notAfter, endEntityOrCA,
|
|
|
|
requiredEKUIfPresent);
|
2014-02-11 12:46:05 +04:00
|
|
|
if (rv != Success) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-07 06:13:20 +04:00
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2014-03-21 01:29:21 +04:00
|
|
|
} } // namespace mozilla::pkix
|