1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
/* 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 "mozpkix/pkixc.h"
#include "mozpkix/pkix.h"
#include "mozpkix/pkixnss.h"
#include "mozpkix/pkixtypes.h"
#include "secerr.h"
using namespace mozilla::pkix;
const size_t SHA256_DIGEST_LENGTH = 256 / 8;
class CodeSigningTrustDomain final : public TrustDomain {
public:
explicit CodeSigningTrustDomain(const uint8_t** certificates,
const uint16_t* certificateLengths,
size_t numCertificates,
const uint8_t* rootSHA256Digest)
: mCertificates(certificates),
mCertificateLengths(certificateLengths),
mNumCertificates(numCertificates),
mRootSHA256Digest(rootSHA256Digest) {}
virtual Result GetCertTrust(EndEntityOrCA endEntityOrCA,
const CertPolicyId& policy,
Input candidateCertDER,
/*out*/ TrustLevel& trustLevel) override {
uint8_t digestBuf[SHA256_DIGEST_LENGTH] = {0};
Result rv = DigestBufNSS(candidateCertDER, DigestAlgorithm::sha256,
digestBuf, SHA256_DIGEST_LENGTH);
if (rv != Success) {
return rv;
}
Input candidateDigestInput;
rv = candidateDigestInput.Init(digestBuf, SHA256_DIGEST_LENGTH);
if (rv != Success) {
return rv;
}
Input rootDigestInput;
rv = rootDigestInput.Init(mRootSHA256Digest, SHA256_DIGEST_LENGTH);
if (rv != Success) {
return rv;
}
if (InputsAreEqual(candidateDigestInput, rootDigestInput)) {
trustLevel = TrustLevel::TrustAnchor;
} else {
trustLevel = TrustLevel::InheritsTrust;
}
return Success;
}
virtual Result FindIssuer(Input encodedIssuerName, IssuerChecker& checker,
Time time) override {
for (size_t i = 0; i < mNumCertificates; i++) {
Input certInput;
Result rv = certInput.Init(mCertificates[i], mCertificateLengths[i]);
if (rv != Success) {
return rv;
}
bool keepGoing;
rv = checker.Check(certInput, nullptr /*additionalNameConstraints*/,
keepGoing);
if (rv != Success) {
return rv;
}
if (!keepGoing) {
break;
}
}
return Success;
}
virtual Result CheckRevocation(
EndEntityOrCA endEntityOrCA, const CertID& certID, Time time,
Duration validityDuration,
/*optional*/ const Input* stapledOCSPresponse,
/*optional*/ const Input* aiaExtension,
/*optional*/ const Input* sctExtension) override {
return Success;
}
virtual Result IsChainValid(const DERArray& certChain, Time time,
const CertPolicyId& requiredPolicy) override {
return Success;
}
virtual Result CheckSignatureDigestAlgorithm(DigestAlgorithm digestAlg,
EndEntityOrCA endEntityOrCA,
Time notBefore) override {
switch (digestAlg) {
case DigestAlgorithm::sha256: // fall through
case DigestAlgorithm::sha384: // fall through
case DigestAlgorithm::sha512:
return Success;
default:
return Result::ERROR_CERT_SIGNATURE_ALGORITHM_DISABLED;
}
}
virtual Result CheckRSAPublicKeyModulusSizeInBits(
EndEntityOrCA endEntityOrCA, unsigned int modulusSizeInBits) override {
if (modulusSizeInBits < 2048) {
return Result::ERROR_INADEQUATE_KEY_SIZE;
}
return Success;
}
virtual Result VerifyRSAPKCS1SignedData(
Input data, DigestAlgorithm digestAlgorithm, Input signature,
Input subjectPublicKeyInfo) override {
return VerifyRSAPKCS1SignedDataNSS(data, digestAlgorithm, signature,
subjectPublicKeyInfo, nullptr);
}
virtual Result VerifyRSAPSSSignedData(
Input data, DigestAlgorithm digestAlgorithm, Input signature,
Input subjectPublicKeyInfo) override {
return VerifyRSAPSSSignedDataNSS(data, digestAlgorithm, signature,
subjectPublicKeyInfo, nullptr);
}
virtual Result CheckECDSACurveIsAcceptable(EndEntityOrCA endEntityOrCA,
NamedCurve curve) override {
switch (curve) {
case NamedCurve::secp256r1: // fall through
case NamedCurve::secp384r1: // fall through
case NamedCurve::secp521r1:
return Success;
}
return Result::ERROR_UNSUPPORTED_ELLIPTIC_CURVE;
}
virtual Result VerifyECDSASignedData(
Input data, DigestAlgorithm digestAlgorithm, Input signature,
Input subjectPublicKeyInfo) override {
return VerifyECDSASignedDataNSS(data, digestAlgorithm, signature,
subjectPublicKeyInfo, nullptr);
}
virtual Result CheckValidityIsAcceptable(Time notBefore, Time notAfter,
EndEntityOrCA endEntityOrCA,
KeyPurposeId keyPurpose) override {
return Success;
}
virtual Result NetscapeStepUpMatchesServerAuth(
Time notBefore, /*out*/ bool& matches) override {
matches = false;
return Success;
}
virtual void NoteAuxiliaryExtension(AuxiliaryExtension extension,
Input extensionData) override {}
virtual Result DigestBuf(Input item, DigestAlgorithm digestAlg,
/*out*/ uint8_t* digestBuf,
size_t digestBufLen) override {
return DigestBufNSS(item, digestAlg, digestBuf, digestBufLen);
}
private:
const uint8_t** mCertificates;
const uint16_t* mCertificateLengths;
size_t mNumCertificates;
const uint8_t* mRootSHA256Digest;
};
class CodeSigningNameMatchingPolicy : public NameMatchingPolicy {
public:
virtual Result FallBackToCommonName(
Time notBefore,
/*out*/ FallBackToSearchWithinSubject& fallBackToCommonName) override {
fallBackToCommonName = FallBackToSearchWithinSubject::No;
return Success;
}
};
bool VerifyCodeSigningCertificateChain(
const uint8_t** certificates, const uint16_t* certificateLengths,
size_t numCertificates, uint64_t secondsSinceEpoch,
const uint8_t* rootSHA256Digest, const uint8_t* hostname,
size_t hostnameLength, PRErrorCode* error) {
if (!error) {
return false;
}
if (!certificates || !certificateLengths || !rootSHA256Digest) {
*error = SEC_ERROR_INVALID_ARGS;
return false;
}
CodeSigningTrustDomain trustDomain(certificates, certificateLengths,
numCertificates, rootSHA256Digest);
Input certificate;
Result rv = certificate.Init(certificates[0], certificateLengths[0]);
if (rv != Success) {
*error = MapResultToPRErrorCode(rv);
return false;
}
Time time = TimeFromEpochInSeconds(secondsSinceEpoch);
rv = BuildCertChain(
trustDomain, certificate, time, EndEntityOrCA::MustBeEndEntity,
KeyUsage::noParticularKeyUsageRequired, KeyPurposeId::id_kp_codeSigning,
CertPolicyId::anyPolicy, nullptr);
if (rv != Success) {
*error = MapResultToPRErrorCode(rv);
return false;
}
Input hostnameInput;
rv = hostnameInput.Init(hostname, hostnameLength);
if (rv != Success) {
*error = MapResultToPRErrorCode(rv);
return false;
}
CodeSigningNameMatchingPolicy nameMatchingPolicy;
rv = CheckCertHostname(certificate, hostnameInput, nameMatchingPolicy);
if (rv != Success) {
*error = MapResultToPRErrorCode(rv);
return false;
}
return true;
}
|