xref: /openssl/crypto/ocsp/ocsp_cl.c (revision e6c2f964)
1 /*
2  * Copyright 2001-2021 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 <stdio.h>
11 #include <time.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15 #include <openssl/x509.h>
16 #include <openssl/pem.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/ocsp.h>
19 #include "ocsp_local.h"
20 
21 /*
22  * Utility functions related to sending OCSP requests and extracting relevant
23  * information from the response.
24  */
25 
26 /*
27  * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
28  * useful if we want to add extensions.
29  */
OCSP_request_add0_id(OCSP_REQUEST * req,OCSP_CERTID * cid)30 OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
31 {
32     OCSP_ONEREQ *one = NULL;
33 
34     if ((one = OCSP_ONEREQ_new()) == NULL)
35         return NULL;
36     OCSP_CERTID_free(one->reqCert);
37     one->reqCert = cid;
38     if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one)) {
39         one->reqCert = NULL; /* do not free on error */
40         OCSP_ONEREQ_free(one);
41         return NULL;
42     }
43     return one;
44 }
45 
46 /* Set requestorName from an X509_NAME structure */
OCSP_request_set1_name(OCSP_REQUEST * req,const X509_NAME * nm)47 int OCSP_request_set1_name(OCSP_REQUEST *req, const X509_NAME *nm)
48 {
49     GENERAL_NAME *gen = GENERAL_NAME_new();
50 
51     if (gen == NULL)
52         return 0;
53     if (!X509_NAME_set(&gen->d.directoryName, nm)) {
54         GENERAL_NAME_free(gen);
55         return 0;
56     }
57     gen->type = GEN_DIRNAME;
58     GENERAL_NAME_free(req->tbsRequest.requestorName);
59     req->tbsRequest.requestorName = gen;
60     return 1;
61 }
62 
63 /* Add a certificate to an OCSP request */
OCSP_request_add1_cert(OCSP_REQUEST * req,X509 * cert)64 int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
65 {
66     if (req->optionalSignature == NULL
67             && (req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
68         return 0;
69     if (cert == NULL)
70         return 1;
71     return ossl_x509_add_cert_new(&req->optionalSignature->certs, cert,
72                                   X509_ADD_FLAG_UP_REF);
73 }
74 
75 /*
76  * Sign an OCSP request set the requestorName to the subject name of an
77  * optional signers certificate and include one or more optional certificates
78  * in the request. Behaves like PKCS7_sign().
79  */
OCSP_request_sign(OCSP_REQUEST * req,X509 * signer,EVP_PKEY * key,const EVP_MD * dgst,STACK_OF (X509)* certs,unsigned long flags)80 int OCSP_request_sign(OCSP_REQUEST *req,
81                       X509 *signer,
82                       EVP_PKEY *key,
83                       const EVP_MD *dgst,
84                       STACK_OF(X509) *certs, unsigned long flags)
85 {
86     if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
87         goto err;
88 
89     if ((req->optionalSignature = OCSP_SIGNATURE_new()) == NULL)
90         goto err;
91     if (key != NULL) {
92         if (!X509_check_private_key(signer, key)) {
93             ERR_raise(ERR_LIB_OCSP,
94                       OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
95             goto err;
96         }
97         if (!OCSP_REQUEST_sign(req, key, dgst, signer->libctx, signer->propq))
98             goto err;
99     }
100 
101     if ((flags & OCSP_NOCERTS) == 0) {
102         if (!OCSP_request_add1_cert(req, signer)
103             || !X509_add_certs(req->optionalSignature->certs, certs,
104                                X509_ADD_FLAG_UP_REF))
105             goto err;
106     }
107 
108     return 1;
109  err:
110     OCSP_SIGNATURE_free(req->optionalSignature);
111     req->optionalSignature = NULL;
112     return 0;
113 }
114 
115 /* Get response status */
OCSP_response_status(OCSP_RESPONSE * resp)116 int OCSP_response_status(OCSP_RESPONSE *resp)
117 {
118     return ASN1_ENUMERATED_get(resp->responseStatus);
119 }
120 
121 /*
122  * Extract basic response from OCSP_RESPONSE or NULL if no basic response
123  * present.
124  */
OCSP_response_get1_basic(OCSP_RESPONSE * resp)125 OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
126 {
127     OCSP_RESPBYTES *rb = resp->responseBytes;
128 
129     if (rb == NULL) {
130         ERR_raise(ERR_LIB_OCSP, OCSP_R_NO_RESPONSE_DATA);
131         return NULL;
132     }
133     if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) {
134         ERR_raise(ERR_LIB_OCSP, OCSP_R_NOT_BASIC_RESPONSE);
135         return NULL;
136     }
137 
138     return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
139 }
140 
OCSP_resp_get0_signature(const OCSP_BASICRESP * bs)141 const ASN1_OCTET_STRING *OCSP_resp_get0_signature(const OCSP_BASICRESP *bs)
142 {
143     return bs->signature;
144 }
145 
OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP * bs)146 const X509_ALGOR *OCSP_resp_get0_tbs_sigalg(const OCSP_BASICRESP *bs)
147 {
148     return &bs->signatureAlgorithm;
149 }
150 
OCSP_resp_get0_respdata(const OCSP_BASICRESP * bs)151 const OCSP_RESPDATA *OCSP_resp_get0_respdata(const OCSP_BASICRESP *bs)
152 {
153     return &bs->tbsResponseData;
154 }
155 
156 /* Return number of OCSP_SINGLERESP responses present in a basic response */
157 
OCSP_resp_count(OCSP_BASICRESP * bs)158 int OCSP_resp_count(OCSP_BASICRESP *bs)
159 {
160     if (bs == NULL)
161         return -1;
162     return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses);
163 }
164 
165 /* Extract an OCSP_SINGLERESP response with a given index */
OCSP_resp_get0(OCSP_BASICRESP * bs,int idx)166 OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
167 {
168     if (bs == NULL)
169         return NULL;
170     return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx);
171 }
172 
OCSP_resp_get0_produced_at(const OCSP_BASICRESP * bs)173 const ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(const OCSP_BASICRESP *bs)
174 {
175     return bs->tbsResponseData.producedAt;
176 }
177 
STACK_OF(X509)178 const STACK_OF(X509) *OCSP_resp_get0_certs(const OCSP_BASICRESP *bs)
179 {
180     return bs->certs;
181 }
182 
OCSP_resp_get0_id(const OCSP_BASICRESP * bs,const ASN1_OCTET_STRING ** pid,const X509_NAME ** pname)183 int OCSP_resp_get0_id(const OCSP_BASICRESP *bs,
184                       const ASN1_OCTET_STRING **pid,
185                       const X509_NAME **pname)
186 {
187     const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
188 
189     if (rid->type == V_OCSP_RESPID_NAME) {
190         *pname = rid->value.byName;
191         *pid = NULL;
192     } else if (rid->type == V_OCSP_RESPID_KEY) {
193         *pid = rid->value.byKey;
194         *pname = NULL;
195     } else {
196         return 0;
197     }
198     return 1;
199 }
200 
OCSP_resp_get1_id(const OCSP_BASICRESP * bs,ASN1_OCTET_STRING ** pid,X509_NAME ** pname)201 int OCSP_resp_get1_id(const OCSP_BASICRESP *bs,
202                       ASN1_OCTET_STRING **pid,
203                       X509_NAME **pname)
204 {
205     const OCSP_RESPID *rid = &bs->tbsResponseData.responderId;
206 
207     if (rid->type == V_OCSP_RESPID_NAME) {
208         *pname = X509_NAME_dup(rid->value.byName);
209         *pid = NULL;
210     } else if (rid->type == V_OCSP_RESPID_KEY) {
211         *pid = ASN1_OCTET_STRING_dup(rid->value.byKey);
212         *pname = NULL;
213     } else {
214         return 0;
215     }
216     if (*pname == NULL && *pid == NULL)
217         return 0;
218     return 1;
219 }
220 
221 /* Look single response matching a given certificate ID */
OCSP_resp_find(OCSP_BASICRESP * bs,OCSP_CERTID * id,int last)222 int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
223 {
224     int i;
225     STACK_OF(OCSP_SINGLERESP) *sresp;
226     OCSP_SINGLERESP *single;
227 
228     if (bs == NULL)
229         return -1;
230     if (last < 0)
231         last = 0;
232     else
233         last++;
234     sresp = bs->tbsResponseData.responses;
235     for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
236         single = sk_OCSP_SINGLERESP_value(sresp, i);
237         if (!OCSP_id_cmp(id, single->certId))
238             return i;
239     }
240     return -1;
241 }
242 
243 /*
244  * Extract status information from an OCSP_SINGLERESP structure. Note: the
245  * revtime and reason values are only set if the certificate status is
246  * revoked. Returns numerical value of status.
247  */
OCSP_single_get0_status(OCSP_SINGLERESP * single,int * reason,ASN1_GENERALIZEDTIME ** revtime,ASN1_GENERALIZEDTIME ** thisupd,ASN1_GENERALIZEDTIME ** nextupd)248 int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
249                             ASN1_GENERALIZEDTIME **revtime,
250                             ASN1_GENERALIZEDTIME **thisupd,
251                             ASN1_GENERALIZEDTIME **nextupd)
252 {
253     int ret;
254     OCSP_CERTSTATUS *cst;
255 
256     if (single == NULL)
257         return -1;
258     cst = single->certStatus;
259     ret = cst->type;
260     if (ret == V_OCSP_CERTSTATUS_REVOKED) {
261         OCSP_REVOKEDINFO *rev = cst->value.revoked;
262 
263         if (revtime)
264             *revtime = rev->revocationTime;
265         if (reason) {
266             if (rev->revocationReason)
267                 *reason = ASN1_ENUMERATED_get(rev->revocationReason);
268             else
269                 *reason = -1;
270         }
271     }
272     if (thisupd != NULL)
273         *thisupd = single->thisUpdate;
274     if (nextupd != NULL)
275         *nextupd = single->nextUpdate;
276     return ret;
277 }
278 
279 /*
280  * This function combines the previous ones: look up a certificate ID and if
281  * found extract status information. Return 0 is successful.
282  */
OCSP_resp_find_status(OCSP_BASICRESP * bs,OCSP_CERTID * id,int * status,int * reason,ASN1_GENERALIZEDTIME ** revtime,ASN1_GENERALIZEDTIME ** thisupd,ASN1_GENERALIZEDTIME ** nextupd)283 int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
284                           int *reason,
285                           ASN1_GENERALIZEDTIME **revtime,
286                           ASN1_GENERALIZEDTIME **thisupd,
287                           ASN1_GENERALIZEDTIME **nextupd)
288 {
289     int i = OCSP_resp_find(bs, id, -1);
290     OCSP_SINGLERESP *single;
291 
292     /* Maybe check for multiple responses and give an error? */
293     if (i < 0)
294         return 0;
295     single = OCSP_resp_get0(bs, i);
296     i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
297     if (status != NULL)
298         *status = i;
299     return 1;
300 }
301 
302 /*
303  * Check validity of thisUpdate and nextUpdate fields. It is possible that
304  * the request will take a few seconds to process and/or the time won't be
305  * totally accurate. Therefore to avoid rejecting otherwise valid time we
306  * allow the times to be within 'nsec' of the current time. Also to avoid
307  * accepting very old responses without a nextUpdate field an optional maxage
308  * parameter specifies the maximum age the thisUpdate field can be.
309  */
OCSP_check_validity(ASN1_GENERALIZEDTIME * thisupd,ASN1_GENERALIZEDTIME * nextupd,long nsec,long maxsec)310 int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
311                         ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
312 {
313     int ret = 1;
314     time_t t_now, t_tmp;
315 
316     time(&t_now);
317     /* Check thisUpdate is valid and not more than nsec in the future */
318     if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
319         ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
320         ret = 0;
321     } else {
322         t_tmp = t_now + nsec;
323         if (X509_cmp_time(thisupd, &t_tmp) > 0) {
324             ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_NOT_YET_VALID);
325             ret = 0;
326         }
327 
328         /*
329          * If maxsec specified check thisUpdate is not more than maxsec in
330          * the past
331          */
332         if (maxsec >= 0) {
333             t_tmp = t_now - maxsec;
334             if (X509_cmp_time(thisupd, &t_tmp) < 0) {
335                 ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_TOO_OLD);
336                 ret = 0;
337             }
338         }
339     }
340 
341     if (nextupd == NULL)
342         return ret;
343 
344     /* Check nextUpdate is valid and not more than nsec in the past */
345     if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
346         ERR_raise(ERR_LIB_OCSP, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
347         ret = 0;
348     } else {
349         t_tmp = t_now - nsec;
350         if (X509_cmp_time(nextupd, &t_tmp) < 0) {
351             ERR_raise(ERR_LIB_OCSP, OCSP_R_STATUS_EXPIRED);
352             ret = 0;
353         }
354     }
355 
356     /* Also don't allow nextUpdate to precede thisUpdate */
357     if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
358         ERR_raise(ERR_LIB_OCSP, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
359         ret = 0;
360     }
361 
362     return ret;
363 }
364 
OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP * single)365 const OCSP_CERTID *OCSP_SINGLERESP_get0_id(const OCSP_SINGLERESP *single)
366 {
367     return single->certId;
368 }
369