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