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