Coverage Report

Created: 2026-10-28 07:07

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