2 * Copyright 2001-2020 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 /* Verify a basic response message */
30 int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
31 X509_STORE *st, unsigned long flags)
34 STACK_OF(X509) *chain = NULL;
35 STACK_OF(X509) *untrusted = NULL;
36 X509_STORE_CTX *ctx = NULL;
37 X509_VERIFY_PARAM *vp;
38 int i, ret = ocsp_find_signer(&signer, bs, certs, flags);
41 OCSPerr(OCSP_F_OCSP_BASIC_VERIFY,
42 OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
45 ctx = X509_STORE_CTX_new();
47 OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, ERR_R_MALLOC_FAILURE);
50 if ((ret == 2) && (flags & OCSP_TRUSTOTHER))
51 flags |= OCSP_NOVERIFY;
52 if (!(flags & OCSP_NOSIGS)) {
55 skey = X509_get0_pubkey(signer);
57 OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, OCSP_R_NO_SIGNER_KEY);
60 ret = OCSP_BASICRESP_verify(bs, skey, 0);
62 OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, OCSP_R_SIGNATURE_FAILURE);
66 if (!(flags & OCSP_NOVERIFY)) {
69 if (flags & OCSP_NOCHAIN) {
71 } else if (bs->certs && certs) {
72 untrusted = sk_X509_dup(bs->certs);
73 if (!X509_add_certs(untrusted, certs, X509_ADD_FLAG_DEFAULT))
75 } else if (certs != NULL) {
78 untrusted = bs->certs;
80 init_res = X509_STORE_CTX_init(ctx, st, signer, untrusted);
82 OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, ERR_R_X509_LIB);
85 if ((flags & OCSP_PARTIAL_CHAIN) != 0
86 && (vp = X509_STORE_CTX_get0_param(ctx)) != NULL)
87 X509_VERIFY_PARAM_set_flags(vp, X509_V_FLAG_PARTIAL_CHAIN);
89 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_OCSP_HELPER);
90 ret = X509_verify_cert(ctx);
91 chain = X509_STORE_CTX_get1_chain(ctx);
93 i = X509_STORE_CTX_get_error(ctx);
94 OCSPerr(OCSP_F_OCSP_BASIC_VERIFY,
95 OCSP_R_CERTIFICATE_VERIFY_ERROR);
96 ERR_add_error_data(2, "Verify error:",
97 X509_verify_cert_error_string(i));
100 if (flags & OCSP_NOCHECKS) {
105 * At this point we have a valid certificate chain need to verify it
106 * against the OCSP issuer criteria.
108 ret = ocsp_check_issuer(bs, chain);
110 /* If fatal error or valid match then finish */
115 * Easy case: explicitly trusted. Get root CA and check for explicit
118 if (flags & OCSP_NOEXPLICIT)
121 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
122 if (X509_check_trust(x, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) {
123 OCSPerr(OCSP_F_OCSP_BASIC_VERIFY, OCSP_R_ROOT_CA_NOT_TRUSTED);
129 X509_STORE_CTX_free(ctx);
130 sk_X509_pop_free(chain, X509_free);
131 if (bs->certs && certs)
132 sk_X509_free(untrusted);
143 int OCSP_resp_get0_signer(OCSP_BASICRESP *bs, X509 **signer,
144 STACK_OF(X509) *extra_certs)
148 ret = ocsp_find_signer(signer, bs, extra_certs, 0);
149 return (ret > 0) ? 1 : 0;
152 static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
153 STACK_OF(X509) *certs, unsigned long flags)
156 OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
158 if ((signer = ocsp_find_signer_sk(certs, rid))) {
162 if (!(flags & OCSP_NOINTERN) &&
163 (signer = ocsp_find_signer_sk(bs->certs, rid))) {
167 /* Maybe lookup from store if by subject name */
173 static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
176 unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
179 /* Easy if lookup by name */
180 if (id->type == V_OCSP_RESPID_NAME)
181 return X509_find_by_subject(certs, id->value.byName);
183 /* Lookup by key hash */
185 /* If key hash isn't SHA1 length then forget it */
186 if (id->value.byKey->length != SHA_DIGEST_LENGTH)
188 keyhash = id->value.byKey->data;
189 /* Calculate hash of each key and compare */
190 for (i = 0; i < sk_X509_num(certs); i++) {
191 x = sk_X509_value(certs, i);
192 if (!X509_pubkey_digest(x, EVP_sha1(), tmphash, NULL))
194 if (memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH) == 0)
200 static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain)
202 STACK_OF(OCSP_SINGLERESP) *sresp;
204 OCSP_CERTID *caid = NULL;
207 sresp = bs->tbsResponseData.responses;
208 if (sk_X509_num(chain) <= 0) {
209 OCSPerr(OCSP_F_OCSP_CHECK_ISSUER, OCSP_R_NO_CERTIFICATES_IN_CHAIN);
213 /* See if the issuer IDs match. */
214 i = ocsp_check_ids(sresp, &caid);
216 /* If ID mismatch or other error then return */
220 signer = sk_X509_value(chain, 0);
221 /* Check to see if OCSP responder CA matches request CA */
222 if (sk_X509_num(chain) > 1) {
223 sca = sk_X509_value(chain, 1);
224 i = ocsp_match_issuerid(sca, caid, sresp);
228 /* We have a match, if extensions OK then success */
229 if (ocsp_check_delegated(signer))
235 /* Otherwise check if OCSP request signed directly by request CA */
236 return ocsp_match_issuerid(signer, caid, sresp);
240 * Check the issuer certificate IDs for equality. If there is a mismatch with
241 * the same algorithm then there's no point trying to match any certificates
242 * against the issuer. If the issuer IDs all match then we just need to check
243 * equality against one of them.
246 static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret)
248 OCSP_CERTID *tmpid, *cid;
251 idcount = sk_OCSP_SINGLERESP_num(sresp);
253 OCSPerr(OCSP_F_OCSP_CHECK_IDS,
254 OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA);
258 cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId;
262 for (i = 1; i < idcount; i++) {
263 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
264 /* Check to see if IDs match */
265 if (OCSP_id_issuer_cmp(cid, tmpid)) {
266 /* If algorithm mismatch let caller deal with it */
267 if (OBJ_cmp(tmpid->hashAlgorithm.algorithm,
268 cid->hashAlgorithm.algorithm))
275 /* All IDs match: only need to check one ID */
281 * Match the certificate issuer ID.
282 * Returns -1 on error, 0 if there is no match and 1 if there is a match.
284 static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
285 STACK_OF(OCSP_SINGLERESP) *sresp)
287 /* If only one ID to match then do it */
290 const X509_NAME *iname;
292 unsigned char md[EVP_MAX_MD_SIZE];
294 dgst = EVP_get_digestbyobj(cid->hashAlgorithm.algorithm);
296 OCSPerr(0, OCSP_R_UNKNOWN_MESSAGE_DIGEST);
300 mdlen = EVP_MD_size(dgst);
302 OCSPerr(0, OCSP_R_DIGEST_SIZE_ERR);
305 if (cid->issuerNameHash.length != mdlen ||
306 cid->issuerKeyHash.length != mdlen)
308 iname = X509_get_subject_name(cert);
309 if (!X509_NAME_digest(iname, dgst, md, NULL)) {
310 OCSPerr(0, OCSP_R_DIGEST_NAME_ERR);
313 if (memcmp(md, cid->issuerNameHash.data, mdlen) != 0)
315 if (!X509_pubkey_digest(cert, dgst, md, NULL)) {
316 OCSPerr(0, OCSP_R_DIGEST_ERR);
319 if (memcmp(md, cid->issuerKeyHash.data, mdlen) != 0)
322 /* We have to match the whole lot */
326 for (i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
327 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
328 ret = ocsp_match_issuerid(cert, tmpid, NULL);
336 static int ocsp_check_delegated(X509 *x)
338 if ((X509_get_extension_flags(x) & EXFLAG_XKUSAGE)
339 && (X509_get_extended_key_usage(x) & XKU_OCSP_SIGN))
341 OCSPerr(OCSP_F_OCSP_CHECK_DELEGATED, OCSP_R_MISSING_OCSPSIGNING_USAGE);
346 * Verify an OCSP request. This is fortunately much easier than OCSP response
347 * verify. Just find the signers certificate and verify it against a given
351 int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
352 X509_STORE *store, unsigned long flags)
358 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
361 OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, ERR_R_MALLOC_FAILURE);
365 if (!req->optionalSignature) {
366 OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, OCSP_R_REQUEST_NOT_SIGNED);
369 gen = req->tbsRequest.requestorName;
370 if (!gen || gen->type != GEN_DIRNAME) {
371 OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
372 OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE);
375 nm = gen->d.directoryName;
376 ret = ocsp_req_find_signer(&signer, req, nm, certs, flags);
378 OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
379 OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
382 if ((ret == 2) && (flags & OCSP_TRUSTOTHER))
383 flags |= OCSP_NOVERIFY;
384 if (!(flags & OCSP_NOSIGS)) {
386 skey = X509_get0_pubkey(signer);
387 ret = OCSP_REQUEST_verify(req, skey);
389 OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, OCSP_R_SIGNATURE_FAILURE);
393 if (!(flags & OCSP_NOVERIFY)) {
396 if (flags & OCSP_NOCHAIN)
397 init_res = X509_STORE_CTX_init(ctx, store, signer, NULL);
399 init_res = X509_STORE_CTX_init(ctx, store, signer,
400 req->optionalSignature->certs);
402 OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY, ERR_R_X509_LIB);
406 X509_STORE_CTX_set_purpose(ctx, X509_PURPOSE_OCSP_HELPER);
407 X509_STORE_CTX_set_trust(ctx, X509_TRUST_OCSP_REQUEST);
408 ret = X509_verify_cert(ctx);
410 ret = X509_STORE_CTX_get_error(ctx);
411 OCSPerr(OCSP_F_OCSP_REQUEST_VERIFY,
412 OCSP_R_CERTIFICATE_VERIFY_ERROR);
413 ERR_add_error_data(2, "Verify error:",
414 X509_verify_cert_error_string(ret));
424 X509_STORE_CTX_free(ctx);
429 static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
430 const X509_NAME *nm, STACK_OF(X509) *certs,
435 if (!(flags & OCSP_NOINTERN)) {
436 signer = X509_find_by_subject(req->optionalSignature->certs, nm);
443 signer = X509_find_by_subject(certs, nm);