2 * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/ocsp.h>
11 #include "ocsp_local.h"
12 #include <openssl/err.h>
15 static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
16 STACK_OF(X509) *certs, unsigned long flags);
17 static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id);
18 static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain);
19 static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp,
21 static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
22 STACK_OF(OCSP_SINGLERESP) *sresp);
23 static int ocsp_check_delegated(X509 *x);
24 static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
25 const X509_NAME *nm, STACK_OF(X509) *certs,
28 /* Returns 1 on success, 0 on failure, or -1 on fatal error */
29 static int ocsp_verify_signer(X509 *signer, int response,
30 X509_STORE *st, unsigned long flags,
31 STACK_OF(X509) *untrusted, STACK_OF(X509) **chain)
33 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
34 X509_VERIFY_PARAM *vp;
38 ERR_raise(ERR_LIB_OCSP, ERR_R_MALLOC_FAILURE);
41 if (!X509_STORE_CTX_init(ctx, st, signer, untrusted)) {
42 ERR_raise(ERR_LIB_OCSP, ERR_R_X509_LIB);
45 if ((vp = X509_STORE_CTX_get0_param(ctx)) == NULL)
47 if ((flags & OCSP_PARTIAL_CHAIN) != 0)
48 X509_VERIFY_PARAM_set_flags(vp, X509_V_FLAG_PARTIAL_CHAIN);
50 && X509_get_ext_by_NID(signer, NID_id_pkix_OCSP_noCheck, -1) >= 0)
52 * Locally disable revocation status checking for OCSP responder cert.
53 * Done here for CRLs; TODO should be done also for OCSP-based checks.
55 X509_VERIFY_PARAM_clear_flags(vp, X509_V_FLAG_CRL_CHECK);
56 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_OCSP_HELPER);
57 X509_STORE_CTX_set_trust(ctx, X509_TRUST_OCSP_REQUEST);
58 /* TODO: why is X509_TRUST_OCSP_REQUEST set? Seems to get ignored. */
60 ret = X509_verify_cert(ctx);
62 ret = X509_STORE_CTX_get_error(ctx);
63 ERR_raise_data(ERR_LIB_OCSP, OCSP_R_CERTIFICATE_VERIFY_ERROR,
64 "Verify error: %s", X509_verify_cert_error_string(ret));
68 *chain = X509_STORE_CTX_get1_chain(ctx);
71 X509_STORE_CTX_free(ctx);
75 static int ocsp_verify(OCSP_REQUEST *req, OCSP_BASICRESP *bs,
76 X509 *signer, unsigned long flags)
81 if ((flags & OCSP_NOSIGS) == 0) {
82 if ((skey = X509_get0_pubkey(signer)) == NULL) {
83 ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_SIGNER_KEY);
87 ret = OCSP_REQUEST_verify(req, skey, signer->libctx, signer->propq);
89 ret = OCSP_BASICRESP_verify(bs, skey, signer->libctx, signer->propq);
91 ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNATURE_FAILURE);
96 /* Verify a basic response message */
97 int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
98 X509_STORE *st, unsigned long flags)
101 STACK_OF(X509) *chain = NULL;
102 STACK_OF(X509) *untrusted = NULL;
103 int ret = ocsp_find_signer(&signer, bs, certs, flags);
106 ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
109 if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0)
110 flags |= OCSP_NOVERIFY;
112 if ((ret = ocsp_verify(NULL, bs, signer, flags)) <= 0)
114 if ((flags & OCSP_NOVERIFY) == 0) {
116 if ((flags & OCSP_NOCHAIN) == 0) {
117 if ((untrusted = sk_X509_dup(bs->certs)) == NULL)
119 if (!X509_add_certs(untrusted, certs, X509_ADD_FLAG_DEFAULT))
122 ret = ocsp_verify_signer(signer, 1, st, flags, untrusted, &chain);
125 if ((flags & OCSP_NOCHECKS) != 0) {
130 * At this point we have a valid certificate chain need to verify it
131 * against the OCSP issuer criteria.
133 ret = ocsp_check_issuer(bs, chain);
135 /* If fatal error or valid match then finish */
140 * Easy case: explicitly trusted. Get root CA and check for explicit
143 if ((flags & OCSP_NOEXPLICIT) != 0)
146 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
147 if (X509_check_trust(x, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) {
148 ERR_raise(ERR_LIB_OCSP, OCSP_R_ROOT_CA_NOT_TRUSTED);
156 sk_X509_pop_free(chain, X509_free);
157 sk_X509_free(untrusted);
161 int OCSP_resp_get0_signer(OCSP_BASICRESP *bs, X509 **signer,
162 STACK_OF(X509) *extra_certs)
164 return ocsp_find_signer(signer, bs, extra_certs, 0) > 0;
167 static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
168 STACK_OF(X509) *certs, unsigned long flags)
171 OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
173 if ((signer = ocsp_find_signer_sk(certs, rid)) != NULL) {
177 if ((flags & OCSP_NOINTERN) == 0 &&
178 (signer = ocsp_find_signer_sk(bs->certs, rid))) {
182 /* Maybe lookup from store if by subject name */
188 static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
191 unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
194 /* Easy if lookup by name */
195 if (id->type == V_OCSP_RESPID_NAME)
196 return X509_find_by_subject(certs, id->value.byName);
198 /* Lookup by key hash */
200 /* If key hash isn't SHA1 length then forget it */
201 if (id->value.byKey->length != SHA_DIGEST_LENGTH)
203 keyhash = id->value.byKey->data;
204 /* Calculate hash of each key and compare */
205 for (i = 0; i < sk_X509_num(certs); i++) {
206 x = sk_X509_value(certs, i);
207 if (!X509_pubkey_digest(x, EVP_sha1(), tmphash, NULL))
209 if (memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH) == 0)
215 static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain)
217 STACK_OF(OCSP_SINGLERESP) *sresp = bs->tbsResponseData.responses;
219 OCSP_CERTID *caid = NULL;
222 if (sk_X509_num(chain) <= 0) {
223 ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_CERTIFICATES_IN_CHAIN);
227 /* See if the issuer IDs match. */
228 ret = ocsp_check_ids(sresp, &caid);
230 /* If ID mismatch or other error then return */
234 signer = sk_X509_value(chain, 0);
235 /* Check to see if OCSP responder CA matches request CA */
236 if (sk_X509_num(chain) > 1) {
237 sca = sk_X509_value(chain, 1);
238 ret = ocsp_match_issuerid(sca, caid, sresp);
242 /* We have a match, if extensions OK then success */
243 if (ocsp_check_delegated(signer))
249 /* Otherwise check if OCSP request signed directly by request CA */
250 return ocsp_match_issuerid(signer, caid, sresp);
254 * Check the issuer certificate IDs for equality. If there is a mismatch with
255 * the same algorithm then there's no point trying to match any certificates
256 * against the issuer. If the issuer IDs all match then we just need to check
257 * equality against one of them.
260 static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret)
262 OCSP_CERTID *tmpid, *cid;
265 idcount = sk_OCSP_SINGLERESP_num(sresp);
267 ERR_raise(ERR_LIB_OCSP, OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA);
271 cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId;
274 for (i = 1; i < idcount; i++) {
275 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
276 /* Check to see if IDs match */
277 if (OCSP_id_issuer_cmp(cid, tmpid)) {
278 /* If algorithm mismatch let caller deal with it */
279 if (OBJ_cmp(tmpid->hashAlgorithm.algorithm,
280 cid->hashAlgorithm.algorithm))
287 /* All IDs match: only need to check one ID */
293 * Match the certificate issuer ID.
294 * Returns -1 on fatal error, 0 if there is no match and 1 if there is a match.
296 static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
297 STACK_OF(OCSP_SINGLERESP) *sresp)
299 /* If only one ID to match then do it */
301 const EVP_MD *dgst = EVP_get_digestbyobj(cid->hashAlgorithm.algorithm);
302 const X509_NAME *iname;
304 unsigned char md[EVP_MAX_MD_SIZE];
307 ERR_raise(ERR_LIB_OCSP, OCSP_R_UNKNOWN_MESSAGE_DIGEST);
311 mdlen = EVP_MD_size(dgst);
313 ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_SIZE_ERR);
316 if (cid->issuerNameHash.length != mdlen ||
317 cid->issuerKeyHash.length != mdlen)
319 iname = X509_get_subject_name(cert);
320 if (!X509_NAME_digest(iname, dgst, md, NULL)) {
321 ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_NAME_ERR);
324 if (memcmp(md, cid->issuerNameHash.data, mdlen) != 0)
326 if (!X509_pubkey_digest(cert, dgst, md, NULL)) {
327 ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_ERR);
330 if (memcmp(md, cid->issuerKeyHash.data, mdlen) != 0)
333 /* We have to match the whole lot */
337 for (i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
338 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
339 ret = ocsp_match_issuerid(cert, tmpid, NULL);
347 static int ocsp_check_delegated(X509 *x)
349 if ((X509_get_extension_flags(x) & EXFLAG_XKUSAGE)
350 && (X509_get_extended_key_usage(x) & XKU_OCSP_SIGN))
352 ERR_raise(ERR_LIB_OCSP, OCSP_R_MISSING_OCSPSIGNING_USAGE);
357 * Verify an OCSP request. This is much easier than OCSP response verify.
358 * Just find the signer's certificate and verify it against a given trust value.
359 * Returns 1 on success, 0 on failure and on fatal error.
361 int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
362 X509_STORE *store, unsigned long flags)
369 if (!req->optionalSignature) {
370 ERR_raise(ERR_LIB_OCSP, OCSP_R_REQUEST_NOT_SIGNED);
373 gen = req->tbsRequest.requestorName;
374 if (!gen || gen->type != GEN_DIRNAME) {
375 ERR_raise(ERR_LIB_OCSP, OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE);
376 return 0; /* not returning -1 here for backward compatibility*/
378 nm = gen->d.directoryName;
379 ret = ocsp_req_find_signer(&signer, req, nm, certs, flags);
381 ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
382 return 0; /* not returning -1 here for backward compatibility*/
384 if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0)
385 flags |= OCSP_NOVERIFY;
387 if ((ret = ocsp_verify(req, NULL, signer, flags)) <= 0)
388 return 0; /* not returning 'ret' here for backward compatibility*/
389 if ((flags & OCSP_NOVERIFY) != 0)
391 return ocsp_verify_signer(signer, 0, store, flags,
392 (flags & OCSP_NOCHAIN) != 0 ?
393 NULL : req->optionalSignature->certs, NULL) > 0;
394 /* using '> 0' here to avoid breaking backward compatibility returning -1 */
397 static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
398 const X509_NAME *nm, STACK_OF(X509) *certs,
403 if ((flags & OCSP_NOINTERN) == 0) {
404 signer = X509_find_by_subject(req->optionalSignature->certs, nm);
405 if (signer != NULL) {
411 if ((signer = X509_find_by_subject(certs, nm)) != NULL) {