]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ocsp/ocsp_vfy.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / ocsp / ocsp_vfy.c
CommitLineData
0f113f3e 1/*
33388b44 2 * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
9b4dc830 3 *
0c496700 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
9b4dc830
DSH
8 */
9
10#include <openssl/ocsp.h>
706457b7 11#include "ocsp_local.h"
9b4dc830 12#include <openssl/err.h>
3ebac273 13#include <string.h>
9b4dc830 14
0f113f3e 15static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
a773b52a 16 STACK_OF(X509) *certs, unsigned long flags);
9b4dc830 17static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id);
a773b52a 18static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain);
0f113f3e
MC
19static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp,
20 OCSP_CERTID **ret);
21static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
22 STACK_OF(OCSP_SINGLERESP) *sresp);
a773b52a 23static int ocsp_check_delegated(X509 *x);
0f113f3e 24static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
8cc86b81 25 const X509_NAME *nm, STACK_OF(X509) *certs,
a773b52a 26 unsigned long flags);
9b4dc830 27
4a71bee6 28/* Returns 1 on success, 0 on failure, or -1 on fatal error */
4ff993d7
DDO
29static int ocsp_verify_signer(X509 *signer, int response,
30 X509_STORE *st, unsigned long flags,
4a71bee6
DDO
31 STACK_OF(X509) *untrusted, STACK_OF(X509) **chain)
32{
33 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
34 X509_VERIFY_PARAM *vp;
35 int ret = -1;
9b4dc830 36
4a71bee6 37 if (ctx == NULL) {
9311d0c4 38 ERR_raise(ERR_LIB_OCSP, ERR_R_MALLOC_FAILURE);
4a71bee6
DDO
39 goto end;
40 }
41 if (!X509_STORE_CTX_init(ctx, st, signer, untrusted)) {
9311d0c4 42 ERR_raise(ERR_LIB_OCSP, ERR_R_X509_LIB);
4a71bee6
DDO
43 goto end;
44 }
4ff993d7
DDO
45 if ((vp = X509_STORE_CTX_get0_param(ctx)) == NULL)
46 goto end;
47 if ((flags & OCSP_PARTIAL_CHAIN) != 0)
4a71bee6 48 X509_VERIFY_PARAM_set_flags(vp, X509_V_FLAG_PARTIAL_CHAIN);
4ff993d7
DDO
49 if (response
50 && X509_get_ext_by_NID(signer, NID_id_pkix_OCSP_noCheck, -1) >= 0)
51 /*
52 * Locally disable revocation status checking for OCSP responder cert.
53 * Done here for CRLs; TODO should be done also for OCSP-based checks.
54 */
55 X509_VERIFY_PARAM_clear_flags(vp, X509_V_FLAG_CRL_CHECK);
4a71bee6
DDO
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. */
59
60 ret = X509_verify_cert(ctx);
61 if (ret <= 0) {
62 ret = X509_STORE_CTX_get_error(ctx);
9311d0c4 63 ERR_raise(ERR_LIB_OCSP, OCSP_R_CERTIFICATE_VERIFY_ERROR);
4a71bee6
DDO
64 ERR_add_error_data(2, "Verify error:",
65 X509_verify_cert_error_string(ret));
66 goto end;
67 }
68 if (chain != NULL)
69 *chain = X509_STORE_CTX_get1_chain(ctx);
70
71 end:
72 X509_STORE_CTX_free(ctx);
73 return ret;
74}
75
76static int ocsp_verify(OCSP_REQUEST *req, OCSP_BASICRESP *bs,
77 X509 *signer, unsigned long flags)
78{
79 EVP_PKEY *skey;
80 int ret = 1;
81
82 if ((flags & OCSP_NOSIGS) == 0) {
83 if ((skey = X509_get0_pubkey(signer)) == NULL) {
9311d0c4 84 ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_SIGNER_KEY);
4a71bee6
DDO
85 return -1;
86 }
87 if (req != NULL)
88 ret = OCSP_REQUEST_verify(req, skey);
89 else
90 ret = OCSP_BASICRESP_verify(bs, skey);
91 if (ret <= 0)
9311d0c4 92 ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNATURE_FAILURE);
4a71bee6
DDO
93 }
94 return ret;
95}
96
97/* Verify a basic response message */
9b4dc830 98int OCSP_basic_verify(OCSP_BASICRESP *bs, STACK_OF(X509) *certs,
0f113f3e
MC
99 X509_STORE *st, unsigned long flags)
100{
101 X509 *signer, *x;
102 STACK_OF(X509) *chain = NULL;
4ca5efc2 103 STACK_OF(X509) *untrusted = NULL;
4a71bee6 104 int ret = ocsp_find_signer(&signer, bs, certs, flags);
a773b52a 105
4a71bee6 106 if (ret == 0) {
9311d0c4 107 ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
d32f5d87 108 goto end;
f0e0fd51 109 }
4a71bee6 110 if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0)
0f113f3e 111 flags |= OCSP_NOVERIFY;
eeccc237 112
4a71bee6
DDO
113 if ((ret = ocsp_verify(NULL, bs, signer, flags)) <= 0)
114 goto end;
115 if ((flags & OCSP_NOVERIFY) == 0) {
116 ret = -1;
117 if ((flags & OCSP_NOCHAIN) != 0) {
4ca5efc2 118 untrusted = NULL;
4a71bee6 119 } else if (bs->certs != NULL && certs != NULL) {
4ca5efc2 120 untrusted = sk_X509_dup(bs->certs);
eeccc237 121 if (!X509_add_certs(untrusted, certs, X509_ADD_FLAG_DEFAULT))
4a71bee6 122 goto end;
121738d1
DO
123 } else if (certs != NULL) {
124 untrusted = certs;
4ca5efc2
DSH
125 } else {
126 untrusted = bs->certs;
127 }
4ff993d7 128 ret = ocsp_verify_signer(signer, 1, st, flags, untrusted, &chain);
4a71bee6 129 if (ret <= 0)
d32f5d87 130 goto end;
4a71bee6 131 if ((flags & OCSP_NOCHECKS) != 0) {
0f113f3e
MC
132 ret = 1;
133 goto end;
134 }
135 /*
136 * At this point we have a valid certificate chain need to verify it
137 * against the OCSP issuer criteria.
138 */
a773b52a 139 ret = ocsp_check_issuer(bs, chain);
0f113f3e
MC
140
141 /* If fatal error or valid match then finish */
142 if (ret != 0)
64a1385a 143 goto end;
0f113f3e
MC
144
145 /*
146 * Easy case: explicitly trusted. Get root CA and check for explicit
147 * trust
148 */
4a71bee6 149 if ((flags & OCSP_NOEXPLICIT) != 0)
0f113f3e
MC
150 goto end;
151
152 x = sk_X509_value(chain, sk_X509_num(chain) - 1);
153 if (X509_check_trust(x, NID_OCSP_sign, 0) != X509_TRUST_TRUSTED) {
9311d0c4 154 ERR_raise(ERR_LIB_OCSP, OCSP_R_ROOT_CA_NOT_TRUSTED);
4a71bee6
DDO
155 ret = 0;
156 goto end;
0f113f3e
MC
157 }
158 ret = 1;
159 }
4a71bee6 160
0f113f3e 161 end:
222561fe 162 sk_X509_pop_free(chain, X509_free);
4ca5efc2
DSH
163 if (bs->certs && certs)
164 sk_X509_free(untrusted);
0f113f3e
MC
165 return ret;
166}
167
ce5886dd 168int OCSP_resp_get0_signer(OCSP_BASICRESP *bs, X509 **signer,
eb48052e 169 STACK_OF(X509) *extra_certs)
ce5886dd 170{
4a71bee6 171 return ocsp_find_signer(signer, bs, extra_certs, 0) > 0;
ce5886dd
BK
172}
173
0f113f3e 174static int ocsp_find_signer(X509 **psigner, OCSP_BASICRESP *bs,
a773b52a 175 STACK_OF(X509) *certs, unsigned long flags)
0f113f3e
MC
176{
177 X509 *signer;
a332635e 178 OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
c51a8af8 179
4a71bee6 180 if ((signer = ocsp_find_signer_sk(certs, rid)) != NULL) {
0f113f3e
MC
181 *psigner = signer;
182 return 2;
183 }
4a71bee6 184 if ((flags & OCSP_NOINTERN) == 0 &&
0f113f3e
MC
185 (signer = ocsp_find_signer_sk(bs->certs, rid))) {
186 *psigner = signer;
187 return 1;
188 }
189 /* Maybe lookup from store if by subject name */
190
191 *psigner = NULL;
192 return 0;
193}
9b4dc830
DSH
194
195static X509 *ocsp_find_signer_sk(STACK_OF(X509) *certs, OCSP_RESPID *id)
0f113f3e
MC
196{
197 int i;
198 unsigned char tmphash[SHA_DIGEST_LENGTH], *keyhash;
199 X509 *x;
200
201 /* Easy if lookup by name */
202 if (id->type == V_OCSP_RESPID_NAME)
203 return X509_find_by_subject(certs, id->value.byName);
204
205 /* Lookup by key hash */
206
207 /* If key hash isn't SHA1 length then forget it */
208 if (id->value.byKey->length != SHA_DIGEST_LENGTH)
209 return NULL;
210 keyhash = id->value.byKey->data;
211 /* Calculate hash of each key and compare */
212 for (i = 0; i < sk_X509_num(certs); i++) {
213 x = sk_X509_value(certs, i);
c51a8af8
P
214 if (!X509_pubkey_digest(x, EVP_sha1(), tmphash, NULL))
215 break;
216 if (memcmp(keyhash, tmphash, SHA_DIGEST_LENGTH) == 0)
0f113f3e
MC
217 return x;
218 }
219 return NULL;
220}
221
a773b52a 222static int ocsp_check_issuer(OCSP_BASICRESP *bs, STACK_OF(X509) *chain)
0f113f3e 223{
4a71bee6 224 STACK_OF(OCSP_SINGLERESP) *sresp = bs->tbsResponseData.responses;
0f113f3e
MC
225 X509 *signer, *sca;
226 OCSP_CERTID *caid = NULL;
4a71bee6 227 int ret;
0f113f3e
MC
228
229 if (sk_X509_num(chain) <= 0) {
9311d0c4 230 ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_CERTIFICATES_IN_CHAIN);
0f113f3e
MC
231 return -1;
232 }
233
234 /* See if the issuer IDs match. */
4a71bee6 235 ret = ocsp_check_ids(sresp, &caid);
0f113f3e
MC
236
237 /* If ID mismatch or other error then return */
4a71bee6
DDO
238 if (ret <= 0)
239 return ret;
0f113f3e
MC
240
241 signer = sk_X509_value(chain, 0);
242 /* Check to see if OCSP responder CA matches request CA */
243 if (sk_X509_num(chain) > 1) {
244 sca = sk_X509_value(chain, 1);
4a71bee6
DDO
245 ret = ocsp_match_issuerid(sca, caid, sresp);
246 if (ret < 0)
247 return ret;
248 if (ret != 0) {
0f113f3e 249 /* We have a match, if extensions OK then success */
a773b52a 250 if (ocsp_check_delegated(signer))
0f113f3e
MC
251 return 1;
252 return 0;
253 }
254 }
255
256 /* Otherwise check if OCSP request signed directly by request CA */
257 return ocsp_match_issuerid(signer, caid, sresp);
258}
259
260/*
261 * Check the issuer certificate IDs for equality. If there is a mismatch with
262 * the same algorithm then there's no point trying to match any certificates
263 * against the issuer. If the issuer IDs all match then we just need to check
264 * equality against one of them.
e8af92fc 265 */
0f113f3e 266
e8af92fc 267static int ocsp_check_ids(STACK_OF(OCSP_SINGLERESP) *sresp, OCSP_CERTID **ret)
0f113f3e
MC
268{
269 OCSP_CERTID *tmpid, *cid;
270 int i, idcount;
271
272 idcount = sk_OCSP_SINGLERESP_num(sresp);
273 if (idcount <= 0) {
9311d0c4 274 ERR_raise(ERR_LIB_OCSP, OCSP_R_RESPONSE_CONTAINS_NO_REVOCATION_DATA);
0f113f3e
MC
275 return -1;
276 }
277
278 cid = sk_OCSP_SINGLERESP_value(sresp, 0)->certId;
279
280 *ret = NULL;
0f113f3e
MC
281 for (i = 1; i < idcount; i++) {
282 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
283 /* Check to see if IDs match */
284 if (OCSP_id_issuer_cmp(cid, tmpid)) {
0d4fb843 285 /* If algorithm mismatch let caller deal with it */
a332635e
DSH
286 if (OBJ_cmp(tmpid->hashAlgorithm.algorithm,
287 cid->hashAlgorithm.algorithm))
0f113f3e
MC
288 return 2;
289 /* Else mismatch */
290 return 0;
291 }
292 }
e8af92fc 293
0f113f3e
MC
294 /* All IDs match: only need to check one ID */
295 *ret = cid;
296 return 1;
297}
e8af92fc 298
c51a8af8
P
299/*
300 * Match the certificate issuer ID.
4a71bee6 301 * Returns -1 on fatal error, 0 if there is no match and 1 if there is a match.
c51a8af8 302 */
e8af92fc 303static int ocsp_match_issuerid(X509 *cert, OCSP_CERTID *cid,
0f113f3e
MC
304 STACK_OF(OCSP_SINGLERESP) *sresp)
305{
306 /* If only one ID to match then do it */
c51a8af8 307 if (cid != NULL) {
4a71bee6 308 const EVP_MD *dgst = EVP_get_digestbyobj(cid->hashAlgorithm.algorithm);
8cc86b81 309 const X509_NAME *iname;
0f113f3e
MC
310 int mdlen;
311 unsigned char md[EVP_MAX_MD_SIZE];
c51a8af8 312
c51a8af8 313 if (dgst == NULL) {
9311d0c4 314 ERR_raise(ERR_LIB_OCSP, OCSP_R_UNKNOWN_MESSAGE_DIGEST);
0f113f3e
MC
315 return -1;
316 }
317
318 mdlen = EVP_MD_size(dgst);
c51a8af8 319 if (mdlen < 0) {
9311d0c4 320 ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_SIZE_ERR);
0f113f3e 321 return -1;
c51a8af8
P
322 }
323 if (cid->issuerNameHash.length != mdlen ||
324 cid->issuerKeyHash.length != mdlen)
0f113f3e
MC
325 return 0;
326 iname = X509_get_subject_name(cert);
c51a8af8 327 if (!X509_NAME_digest(iname, dgst, md, NULL)) {
9311d0c4 328 ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_NAME_ERR);
0f113f3e 329 return -1;
c51a8af8
P
330 }
331 if (memcmp(md, cid->issuerNameHash.data, mdlen) != 0)
0f113f3e 332 return 0;
c51a8af8 333 if (!X509_pubkey_digest(cert, dgst, md, NULL)) {
9311d0c4 334 ERR_raise(ERR_LIB_OCSP, OCSP_R_DIGEST_ERR);
c51a8af8
P
335 return -1;
336 }
337 if (memcmp(md, cid->issuerKeyHash.data, mdlen) != 0)
0f113f3e 338 return 0;
0f113f3e
MC
339 } else {
340 /* We have to match the whole lot */
341 int i, ret;
342 OCSP_CERTID *tmpid;
c51a8af8 343
0f113f3e
MC
344 for (i = 0; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
345 tmpid = sk_OCSP_SINGLERESP_value(sresp, i)->certId;
346 ret = ocsp_match_issuerid(cert, tmpid, NULL);
347 if (ret <= 0)
348 return ret;
349 }
0f113f3e 350 }
c51a8af8 351 return 1;
0f113f3e 352}
e8af92fc 353
a773b52a 354static int ocsp_check_delegated(X509 *x)
0f113f3e 355{
a8d8e06b
DSH
356 if ((X509_get_extension_flags(x) & EXFLAG_XKUSAGE)
357 && (X509_get_extended_key_usage(x) & XKU_OCSP_SIGN))
0f113f3e 358 return 1;
9311d0c4 359 ERR_raise(ERR_LIB_OCSP, OCSP_R_MISSING_OCSPSIGNING_USAGE);
0f113f3e
MC
360 return 0;
361}
362
363/*
4a71bee6
DDO
364 * Verify an OCSP request. This is much easier than OCSP response verify.
365 * Just find the signer's certificate and verify it against a given trust value.
366 * Returns 1 on success, 0 on failure and on fatal error.
fafc7f98 367 */
0f113f3e
MC
368int OCSP_request_verify(OCSP_REQUEST *req, STACK_OF(X509) *certs,
369 X509_STORE *store, unsigned long flags)
370{
371 X509 *signer;
8cc86b81 372 const X509_NAME *nm;
0f113f3e 373 GENERAL_NAME *gen;
4a71bee6 374 int ret;
f0e0fd51 375
0f113f3e 376 if (!req->optionalSignature) {
9311d0c4 377 ERR_raise(ERR_LIB_OCSP, OCSP_R_REQUEST_NOT_SIGNED);
4a71bee6 378 return 0;
0f113f3e 379 }
a332635e 380 gen = req->tbsRequest.requestorName;
0f113f3e 381 if (!gen || gen->type != GEN_DIRNAME) {
9311d0c4 382 ERR_raise(ERR_LIB_OCSP, OCSP_R_UNSUPPORTED_REQUESTORNAME_TYPE);
4a71bee6 383 return 0; /* not returning -1 here for backward compatibility*/
0f113f3e
MC
384 }
385 nm = gen->d.directoryName;
a773b52a 386 ret = ocsp_req_find_signer(&signer, req, nm, certs, flags);
0f113f3e 387 if (ret <= 0) {
9311d0c4 388 ERR_raise(ERR_LIB_OCSP, OCSP_R_SIGNER_CERTIFICATE_NOT_FOUND);
4a71bee6 389 return 0; /* not returning -1 here for backward compatibility*/
0f113f3e 390 }
4a71bee6 391 if ((ret == 2) && (flags & OCSP_TRUSTOTHER) != 0)
0f113f3e 392 flags |= OCSP_NOVERIFY;
f0e0fd51 393
4a71bee6
DDO
394 if ((ret = ocsp_verify(req, NULL, signer, flags)) <= 0)
395 return 0; /* not returning 'ret' here for backward compatibility*/
396 if ((flags & OCSP_NOVERIFY) != 0)
397 return 1;
4ff993d7 398 return ocsp_verify_signer(signer, 0, store, flags,
4a71bee6
DDO
399 (flags & OCSP_NOCHAIN) != 0 ?
400 NULL : req->optionalSignature->certs, NULL) > 0;
401 /* using '> 0' here to avoid breaking backward compatibility returning -1 */
0f113f3e
MC
402}
403
404static int ocsp_req_find_signer(X509 **psigner, OCSP_REQUEST *req,
8cc86b81 405 const X509_NAME *nm, STACK_OF(X509) *certs,
a773b52a 406 unsigned long flags)
0f113f3e
MC
407{
408 X509 *signer;
c51a8af8 409
4a71bee6 410 if ((flags & OCSP_NOINTERN) == 0) {
0f113f3e 411 signer = X509_find_by_subject(req->optionalSignature->certs, nm);
4a71bee6 412 if (signer != NULL) {
0f113f3e
MC
413 *psigner = signer;
414 return 1;
415 }
416 }
417
4a71bee6 418 if ((signer = X509_find_by_subject(certs, nm)) != NULL) {
0f113f3e
MC
419 *psigner = signer;
420 return 2;
421 }
422 return 0;
423}