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