Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/ocsp/ocsp_verify.c

raw
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3
4 #include <string.h>
5 #include "../internal.h"
6 #include "internal.h"
7
8 #define SIGNER_IN_PROVIDED_CERTS 2
9 #define SIGNER_IN_OCSP_CERTS 1
10 #define SIGNER_NOT_FOUND 0
11
12 // Set up |X509_STORE_CTX| to verify signer and returns cert chain if verify is
13 // OK. A |OCSP_RESPID| can be identified either by name or its keyhash.
14 // https://datatracker.ietf.org/doc/html/rfc6960#section-4.2.2.3
15 static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id) {
16 if (certs == NULL || id == NULL) {
17 return NULL;
18 }
19
20 // Easy if lookup by name.
21 if (id->type == V_OCSP_RESPID_NAME) {
22 return X509_find_by_subject(certs, id->value.byName);
23 }
24
25 // Lookup by key hash.
26 unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
27
28 // If key hash isn't SHA1 length then forget it.
29 if (id->value.byKey == NULL || id->value.byKey->length != SHA_DIGEST_LENGTH) {
30 return NULL;
31 }
32 keyhash = id->value.byKey->data;
33 // Calculate hash of each key and compare.
34 X509 *cert;
35 for (size_t i = 0; i < sk_X509_num(certs); i++) {
36 cert = sk_X509_value(certs, i);
37 if (X509_pubkey_digest(cert, EVP_sha1(), tmphash, NULL)) {
38 if (OPENSSL_memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH) == 0) {
39 return cert;
40 }
41 }
42 }
43
44 return NULL;
45 }
46
47 // Find signer in cert stack or |OCSP_BASICRESP|'s cert stack.
48 static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
49 STACK_OF(X509) *certs, unsigned long flags) {
50 if (psigner == NULL) {
51 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
52 return -1;
53 }
54
55 X509 *signer;
56 OCSP_RESPID *rid = bs->tbsResponseData->responderId;
57 // look for signer in certs stack.
58 signer = ocsp_find_signer_sk(certs, rid);
59 if (signer != NULL) {
60 *psigner = signer;
61 return SIGNER_IN_PROVIDED_CERTS;
62 }
63
64 // look in certs stack the responder may have included in |OCSP_BASICRESP|,
65 // unless the flags contain |OCSP_NOINTERN|.
66 signer = ocsp_find_signer_sk(bs->certs, rid);
67 if (signer != NULL && !IS_OCSP_FLAG_SET(flags, OCSP_NOINTERN)) {
68 *psigner = signer;
69 return SIGNER_IN_OCSP_CERTS;
70 }
71 // Maybe lookup from store if by subject name.
72
73 *psigner = NULL;
74 return SIGNER_NOT_FOUND;
75 }
76
77 // check if public key in signer matches key in |OCSP_BASICRESP|.
78 static int ocsp_verify_key(OCSP_BASICRESP *bs, X509 *signer) {
79 if (signer == NULL) {
80 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
81 return -1;
82 }
83
84 EVP_PKEY *skey = X509_get_pubkey(signer);
85 if (skey == NULL) {
86 OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_SIGNER_KEY);
87 return -1;
88 }
89 int ret =
90 ASN1_item_verify(ASN1_ITEM_rptr(OCSP_RESPDATA), bs->signatureAlgorithm,
91 bs->signature, bs->tbsResponseData, skey);
92 EVP_PKEY_free(skey);
93 if (ret <= 0) {
94 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SIGNATURE_FAILURE);
95 }
96
97 return ret;
98 }
99
100 // Set untrusted certificate stack from |OCSP_BASICRESP|.
101 static int ocsp_setup_untrusted(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
102 STACK_OF(X509) **untrusted,
103 unsigned long flags) {
104 if (untrusted == NULL) {
105 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
106 return -1;
107 }
108 if (IS_OCSP_FLAG_SET(flags, OCSP_NOCHAIN)) {
109 *untrusted = NULL;
110 } else if (bs->certs && certs) {
111 *untrusted = sk_X509_dup(bs->certs);
112 for (size_t i = 0; i < sk_X509_num(certs); i++) {
113 if (!sk_X509_push(*untrusted, sk_X509_value(certs, i))) {
114 return -1;
115 }
116 }
117 } else if (certs != NULL) {
118 *untrusted = sk_X509_dup(certs);
119 } else {
120 *untrusted = sk_X509_dup(bs->certs);
121 }
122 return 1;
123 }
124
125
126 static int ocsp_verify_signer(X509 *signer, X509_STORE *st,
127 STACK_OF(X509) *untrusted,
128 STACK_OF(X509) **chain) {
129 if (signer == NULL) {
130 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
131 return -1;
132 }
133
134 // Set up |X509_STORE_CTX| with |*signer|, |*st|, and |*untrusted|.
135 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
136 int ret = -1;
137
138 if (ctx == NULL) {
139 goto end;
140 }
141 if (!X509_STORE_CTX_init(ctx, st, signer, untrusted)) {
142 OPENSSL_PUT_ERROR(OCSP, ERR_R_X509_LIB);
143 goto end;
144 }
145 // RFC 6960 section 4.2.2.2.1: if the responder certificate has the
146 // id-pkix-ocsp-nocheck extension, the CA has indicated that the client
147 // should trust the responder for its lifetime without revocation checking.
148 // Locally disable CRL-based revocation checking in this case.
149 if (X509_get_ext_by_NID(signer, NID_id_pkix_OCSP_noCheck, -1) >= 0) {
150 X509_VERIFY_PARAM *vp = X509_STORE_CTX_get0_param(ctx);
151 if (vp == NULL) {
152 OPENSSL_PUT_ERROR(OCSP, ERR_R_X509_LIB);
153 goto end;
154 }
155 X509_VERIFY_PARAM_clear_flags(vp, X509_V_FLAG_CRL_CHECK);
156 }
157 if (!X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_OCSP_HELPER)) {
158 OPENSSL_PUT_ERROR(OCSP, ERR_R_X509_LIB);
159 goto end;
160 }
161
162 // Verify |X509_STORE_CTX| and return certificate chain.
163 ret = X509_verify_cert(ctx);
164 if (ret <= 0) {
165 int err = X509_STORE_CTX_get_error(ctx);
166 OPENSSL_PUT_ERROR(OCSP, OCSP_R_CERTIFICATE_VERIFY_ERROR);
167 ERR_add_error_data(2, "Verify error: ", X509_verify_cert_error_string(err));
168 goto end;
169 }
170 if (chain != NULL) {
171 *chain = X509_STORE_CTX_get1_chain(ctx);
172 }
173
174 end:
175 X509_STORE_CTX_free(ctx);
176 return ret;
177 }
178
179 // Check the issuer certificate IDs for equality. If the issuer IDs all match
180 // then we just need to check equality against one of them. If there is a
181 // mismatch with the same algorithm then there's no point trying to match any
182 // certificates against the issuer, and |*ret| will be set to NULL.
183 static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret) {
184 if (sresp == NULL || ret == NULL) {
185 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
186 return -1;
187 }
188
189 OCSP_CERTID *tmpid, *cid;
190 size_t idcount = sk_OCSP_SINGLERESP_num(sresp);
191 if (idcount == 0) {
192 OPENSSL_PUT_ERROR(OCSP, OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA);
193 return -1;
194 }
195 cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId;
196
197 *ret = NULL;
198 for (size_t i = 1; i < idcount; i++) {
199 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
200 // Check to see if IDs match.
201 if (OCSP_id_issuer_cmp(cid, tmpid) != 0) {
202 // If algorithm mismatch, let caller deal with it instead.
203 if (OBJ_cmp(tmpid->hashAlgorithm->algorithm,
204 cid->hashAlgorithm->algorithm) != 0) {
205 return 1;
206 }
207 return 0;
208 }
209 }
210
211 // All IDs match: only need to check one ID.
212 *ret = cid;
213 return 1;
214 }
215
216 // Returns -1 on fatal error, 0 if there is no match and 1 if there is a
217 // match.
218 static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
219 STACK_OF(OCSP_SINGLERESP) *sresp) {
220 if (cert == NULL) {
221 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
222 return -1;
223 }
224
225 // If only one ID to match then do it.
226 if (cid) {
227 const EVP_MD *dgst;
228 X509_NAME *iname;
229 unsigned char md[EVP_MAX_MD_SIZE];
230 // Set up message digest for comparison.
231 dgst = EVP_get_digestbyobj(cid->hashAlgorithm->algorithm);
232 if (dgst == NULL) {
233 OPENSSL_PUT_ERROR(OCSP, OCSP_R_UNKNOWN_MESSAGE_DIGEST);
234 return -1;
235 }
236 size_t mdlen = EVP_MD_size(dgst);
237 iname = X509_get_subject_name(cert);
238 if (!X509_NAME_digest(iname, dgst, md, NULL)) {
239 return -1;
240 }
241
242 // Compare message digest with |OCSP_CERTID|
243 if (cid->issuerNameHash->length >= 0 && cid->issuerKeyHash->length >= 0) {
244 if (((size_t)cid->issuerNameHash->length != mdlen) ||
245 (size_t)cid->issuerKeyHash->length != mdlen) {
246 return 0;
247 }
248 }
249 if (0 != OPENSSL_memcmp(md, cid->issuerNameHash->data, mdlen)) {
250 return 0;
251 }
252 if (1 != X509_pubkey_digest(cert, dgst, md, NULL)) {
253 return -1;
254 }
255 if (0 != OPENSSL_memcmp(md, cid->issuerKeyHash->data, mdlen)) {
256 return 0;
257 }
258 return 1;
259
260 }
261 // We have to match the whole stack recursively, if |cid| is NULL. This
262 // means that the issuer certificate's hash algorithm did not match with
263 // the rest of the |certId|s in the |OCSP_SINGLERESP| stack. (Issuer
264 // certificate is taken from the root of the |OCSP_SINGLERESP| stack).
265 // We'll have to find a match from the signer or responder CA certificate
266 // (both are certificates in the certificate chain) in the
267 // |OCSP_SINGLERESP| stack instead.
268 else {
269 int ret;
270 OCSP_CERTID *tmpid;
271 for (size_t i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
272 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
273 ret = ocsp_match_issuerid(cert, tmpid, NULL);
274 if (ret <= 0) {
275 return ret;
276 }
277 }
278 return 1;
279 }
280 }
281
282 static int ocsp_check_delegated(X509 *x) {
283 if (x == NULL) {
284 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
285 return -1;
286 }
287
288 if ((X509_get_extension_flags(x) & EXFLAG_XKUSAGE) &&
289 (X509_get_extended_key_usage(x) & XKU_OCSP_SIGN)) {
290 return 1;
291 }
292 OPENSSL_PUT_ERROR(OCSP, OCSP_R_MISSING_OCSPSIGNING_USAGE);
293 return 0;
294 }
295
296 // check OCSP responder issuer and ids
297 static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain) {
298 if (chain == NULL) {
299 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
300 return -1;
301 }
302
303 STACK_OF(OCSP_SINGLERESP) *sresp = bs->tbsResponseData->responses;
304 OCSP_CERTID *caid = NULL;
305 int ret;
306
307 if (sk_X509_num(chain) <= 0) {
308 OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_CERTIFICATES_IN_CHAIN);
309 return -1;
310 }
311
312 // See if the issuer IDs match.
313 ret = ocsp_check_ids(sresp, &caid);
314
315 // If ID mismatch or other error then return.
316 if (ret <= 0) {
317 return ret;
318 }
319
320 X509 *signer, *sca;
321 signer = sk_X509_value(chain, 0);
322 // Check to see if OCSP responder CA matches request CA.
323 if (sk_X509_num(chain) > 1) {
324 sca = sk_X509_value(chain, 1);
325 ret = ocsp_match_issuerid(sca, caid, sresp);
326 if (ret < 0) {
327 return ret;
328 }
329 if (ret != 0) {
330 // If matches, then check extension flags.
331 if (ocsp_check_delegated(signer)) {
332 return 1;
333 }
334 return 0;
335 }
336 }
337
338 // Otherwise check if OCSP request signed directly by request CA.
339 return ocsp_match_issuerid(signer, caid, sresp);
340 }
341
342 int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs, X509_STORE *st,
343 unsigned long flags) {
344 if (bs == NULL || st == NULL) {
345 OPENSSL_PUT_ERROR(OCSP, ERR_R_PASSED_NULL_PARAMETER);
346 return -1;
347 }
348
349 X509 *signer;
350 STACK_OF(X509) *chain = NULL;
351 STACK_OF(X509) *untrusted = NULL;
352
353 // Look for signer certificate.
354 int ret = ocsp_find_signer(&signer, bs, certs, flags);
355 if (ret <= SIGNER_NOT_FOUND) {
356 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
357 goto end;
358 }
359 if ((ret == SIGNER_IN_PROVIDED_CERTS) &&
360 IS_OCSP_FLAG_SET(flags, OCSP_TRUSTOTHER)) {
361 // We skip verification if the flag to trust |certs| is set and the signer
362 // is found within that stack.
363 flags |= OCSP_NOVERIFY;
364 }
365
366 // Check if public key in signer matches key in |OCSP_BASICRESP|.
367 ret = ocsp_verify_key(bs, signer);
368 if (ret <= 0) {
369 goto end;
370 }
371 if (!IS_OCSP_FLAG_SET(flags, OCSP_NOVERIFY)) {
372 // Verify signer and if valid, check the certificate chain.
373 ret = ocsp_setup_untrusted(bs, certs, &untrusted, flags);
374 if (ret <= 0) {
375 goto end;
376 }
377 ret = ocsp_verify_signer(signer, st, untrusted, &chain);
378 if (ret <= 0) {
379 goto end;
380 }
381
382 // At this point we have a valid certificate chain, need to verify it
383 // against the OCSP issuer criteria.
384 ret = ocsp_check_issuer(bs, chain);
385
386 // If a certificate chain is not verifiable against the OCSP issuer
387 // criteria, we try to check for explicit trust.
388 if (ret == 0) {
389 // Easy case: explicitly trusted. Get root CA and check for explicit
390 // trust.
391 if (IS_OCSP_FLAG_SET(flags, OCSP_NOEXPLICIT)) {
392 goto end;
393 }
394 X509 *root_cert = sk_X509_value(chain, sk_X509_num(chain) - 1);
395 if (X509_check_trust(root_cert, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) {
396 OPENSSL_PUT_ERROR(OCSP, OCSP_R_ROOT_CA_NOT_TRUSTED);
397 ret = 0;
398 goto end;
399 }
400 ret = 1;
401 }
402 }
403
404 end:
405 sk_X509_pop_free(chain, X509_free);
406 sk_X509_free(untrusted);
407 return ret;
408 }
409
410 // ocsp_req_find_signer assigns |*psigner| to the signing certificate and
411 // returns |SIGNER_IN_OCSP_CERTS| if it was found within |req| or returns
412 // |SIGNER_IN_TRUSTED_CERTS| if it was found within |certs|. It returns
413 // |SIGNER_NOT_FOUND| if the signing certificate is not found.
414 static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
415 X509_NAME *nm, STACK_OF(X509) *certs,
416 unsigned long flags) {
417 X509 *signer = NULL;
418 if (!IS_OCSP_FLAG_SET(flags, OCSP_NOINTERN)) {
419 signer = X509_find_by_subject(req->optionalSignature->certs, nm);
420 if (signer != NULL) {
421 *psigner = signer;
422 return SIGNER_IN_OCSP_CERTS;
423 }
424 }
425
426 signer = X509_find_by_subject(certs, nm);
427 if (signer != NULL) {
428 *psigner = signer;
429 return SIGNER_IN_PROVIDED_CERTS;
430 }
431 return SIGNER_NOT_FOUND;
432 }
433
434 int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
435 X509_STORE *store, unsigned long flags) {
436 GUARD_PTR(req);
437 GUARD_PTR(req->tbsRequest);
438 GUARD_PTR(store);
439
440 // Check if |req| has signature to check against.
441 if (req->optionalSignature == NULL) {
442 OPENSSL_PUT_ERROR(OCSP, OCSP_R_REQUEST_NOT_SIGNED);
443 return 0;
444 }
445
446 GENERAL_NAME *gen = req->tbsRequest->requestorName;
447 if (gen == NULL || gen->type != GEN_DIRNAME) {
448 OPENSSL_PUT_ERROR(OCSP, OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE);
449 return 0;
450 }
451
452 // Find |signer| from |certs| or |req->optionalSignature->certs| against criteria.
453 X509 *signer = NULL;
454 int signer_status =
455 ocsp_req_find_signer(&signer, req, gen->d.directoryName, certs, flags);
456 if (signer_status <= SIGNER_NOT_FOUND || signer == NULL) {
457 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
458 return 0;
459 }
460 if (signer_status == SIGNER_IN_PROVIDED_CERTS &&
461 IS_OCSP_FLAG_SET(flags, OCSP_TRUSTOTHER)) {
462 // We skip certificate verification if the flag to trust |certs| is set and
463 // the signer is found within that stack.
464 flags |= OCSP_NOVERIFY;
465 }
466
467 // Validate |req|'s signature.
468 EVP_PKEY *skey = X509_get0_pubkey(signer);
469 if (skey == NULL) {
470 OPENSSL_PUT_ERROR(OCSP, OCSP_R_NO_SIGNER_KEY);
471 return 0;
472 }
473 if (ASN1_item_verify(ASN1_ITEM_rptr(OCSP_REQINFO),
474 req->optionalSignature->signatureAlgorithm,
475 req->optionalSignature->signature, req->tbsRequest,
476 skey) <= 0) {
477 OPENSSL_PUT_ERROR(OCSP, OCSP_R_SIGNATURE_FAILURE);
478 return 0;
479 }
480
481 // Set up |ctx| and start doing the actual verification.
482 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
483 if (ctx == NULL) {
484 return 0;
485 }
486
487 // Validate the signing certificate.
488 int ret = 0;
489 if (!IS_OCSP_FLAG_SET(flags, OCSP_NOVERIFY)) {
490 // Initialize and set purpose of |ctx| for verification.
491 if (1 != X509_STORE_CTX_init(ctx, store, signer, NULL) ||
492 1 != X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_OCSP_HELPER)) {
493 OPENSSL_PUT_ERROR(OCSP, ERR_R_X509_LIB);
494 goto end;
495 }
496 if (!IS_OCSP_FLAG_SET(flags, OCSP_NOCHAIN)) {
497 X509_STORE_CTX_set_chain(ctx, req->optionalSignature->certs);
498 }
499
500 // Do the verification.
501 if (X509_verify_cert(ctx) <= 0) {
502 int err = X509_STORE_CTX_get_error(ctx);
503 OPENSSL_PUT_ERROR(OCSP, OCSP_R_CERTIFICATE_VERIFY_ERROR);
504 ERR_add_error_data(2,
505 "Verify error:", X509_verify_cert_error_string(err));
506 goto end;
507 }
508 }
509 ret = 1;
510
511 end:
512 X509_STORE_CTX_free(ctx);
513 return ret;
514 }