195d87c8bedf7472d152f550dc60a02a6ec03255
[openssl.git] / crypto / ocsp / ocsp_cl.c
1 /*
2  * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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/objects.h>
14 #include <openssl/x509.h>
15 #include <openssl/pem.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/ocsp.h>
18 #include "ocsp_lcl.h"
19
20 /*
21  * Utility functions related to sending OCSP requests and extracting relevant
22  * information from the response.
23  */
24
25 /*
26  * Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ pointer:
27  * useful if we want to add extensions.
28  */
29
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         goto err;
36     OCSP_CERTID_free(one->reqCert);
37     one->reqCert = cid;
38     if (req && !sk_OCSP_ONEREQ_push(req->tbsRequest.requestList, one))
39         goto err;
40     return one;
41  err:
42     OCSP_ONEREQ_free(one);
43     return NULL;
44 }
45
46 /* Set requestorName from an X509_NAME structure */
47
48 int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)
49 {
50     GENERAL_NAME *gen;
51
52     gen = GENERAL_NAME_new();
53     if (gen == NULL)
54         return 0;
55     if (!X509_NAME_set(&gen->d.directoryName, nm)) {
56         GENERAL_NAME_free(gen);
57         return 0;
58     }
59     gen->type = GEN_DIRNAME;
60     GENERAL_NAME_free(req->tbsRequest.requestorName);
61     req->tbsRequest.requestorName = gen;
62     return 1;
63 }
64
65 /* Add a certificate to an OCSP request */
66
67 int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
68 {
69     OCSP_SIGNATURE *sig;
70     if (req->optionalSignature == NULL)
71         req->optionalSignature = OCSP_SIGNATURE_new();
72     sig = req->optionalSignature;
73     if (sig == NULL)
74         return 0;
75     if (cert == NULL)
76         return 1;
77     if (sig->certs == NULL
78         && (sig->certs = sk_X509_new_null()) == NULL)
79         return 0;
80
81     if (!sk_X509_push(sig->certs, cert))
82         return 0;
83     X509_up_ref(cert);
84     return 1;
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 ASN1_OCTET_STRING *OCSP_resp_get0_signature(OCSP_BASICRESP *bs)
163 {
164     return bs->signature;
165 }
166
167 /*
168  * Return number of OCSP_SINGLERESP responses present in a basic response.
169  */
170
171 int OCSP_resp_count(OCSP_BASICRESP *bs)
172 {
173     if (!bs)
174         return -1;
175     return sk_OCSP_SINGLERESP_num(bs->tbsResponseData.responses);
176 }
177
178 /* Extract an OCSP_SINGLERESP response with a given index */
179
180 OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
181 {
182     if (!bs)
183         return NULL;
184     return sk_OCSP_SINGLERESP_value(bs->tbsResponseData.responses, idx);
185 }
186
187 ASN1_GENERALIZEDTIME *OCSP_resp_get0_produced_at(OCSP_BASICRESP* bs)
188 {
189     if (!bs)
190         return NULL;
191     return bs->tbsResponseData.producedAt;
192 }
193
194 /* Look single response matching a given certificate ID */
195
196 int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
197 {
198     int i;
199     STACK_OF(OCSP_SINGLERESP) *sresp;
200     OCSP_SINGLERESP *single;
201     if (!bs)
202         return -1;
203     if (last < 0)
204         last = 0;
205     else
206         last++;
207     sresp = bs->tbsResponseData.responses;
208     for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++) {
209         single = sk_OCSP_SINGLERESP_value(sresp, i);
210         if (!OCSP_id_cmp(id, single->certId))
211             return i;
212     }
213     return -1;
214 }
215
216 /*
217  * Extract status information from an OCSP_SINGLERESP structure. Note: the
218  * revtime and reason values are only set if the certificate status is
219  * revoked. Returns numerical value of status.
220  */
221
222 int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
223                             ASN1_GENERALIZEDTIME **revtime,
224                             ASN1_GENERALIZEDTIME **thisupd,
225                             ASN1_GENERALIZEDTIME **nextupd)
226 {
227     int ret;
228     OCSP_CERTSTATUS *cst;
229     if (!single)
230         return -1;
231     cst = single->certStatus;
232     ret = cst->type;
233     if (ret == V_OCSP_CERTSTATUS_REVOKED) {
234         OCSP_REVOKEDINFO *rev = cst->value.revoked;
235         if (revtime)
236             *revtime = rev->revocationTime;
237         if (reason) {
238             if (rev->revocationReason)
239                 *reason = ASN1_ENUMERATED_get(rev->revocationReason);
240             else
241                 *reason = -1;
242         }
243     }
244     if (thisupd)
245         *thisupd = single->thisUpdate;
246     if (nextupd)
247         *nextupd = single->nextUpdate;
248     return ret;
249 }
250
251 /*
252  * This function combines the previous ones: look up a certificate ID and if
253  * found extract status information. Return 0 is successful.
254  */
255
256 int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
257                           int *reason,
258                           ASN1_GENERALIZEDTIME **revtime,
259                           ASN1_GENERALIZEDTIME **thisupd,
260                           ASN1_GENERALIZEDTIME **nextupd)
261 {
262     int i;
263     OCSP_SINGLERESP *single;
264     i = OCSP_resp_find(bs, id, -1);
265     /* Maybe check for multiple responses and give an error? */
266     if (i < 0)
267         return 0;
268     single = OCSP_resp_get0(bs, i);
269     i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
270     if (status)
271         *status = i;
272     return 1;
273 }
274
275 /*
276  * Check validity of thisUpdate and nextUpdate fields. It is possible that
277  * the request will take a few seconds to process and/or the time wont be
278  * totally accurate. Therefore to avoid rejecting otherwise valid time we
279  * allow the times to be within 'nsec' of the current time. Also to avoid
280  * accepting very old responses without a nextUpdate field an optional maxage
281  * parameter specifies the maximum age the thisUpdate field can be.
282  */
283
284 int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd,
285                         ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
286 {
287     int ret = 1;
288     time_t t_now, t_tmp;
289     time(&t_now);
290     /* Check thisUpdate is valid and not more than nsec in the future */
291     if (!ASN1_GENERALIZEDTIME_check(thisupd)) {
292         OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
293         ret = 0;
294     } else {
295         t_tmp = t_now + nsec;
296         if (X509_cmp_time(thisupd, &t_tmp) > 0) {
297             OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
298             ret = 0;
299         }
300
301         /*
302          * If maxsec specified check thisUpdate is not more than maxsec in
303          * the past
304          */
305         if (maxsec >= 0) {
306             t_tmp = t_now - maxsec;
307             if (X509_cmp_time(thisupd, &t_tmp) < 0) {
308                 OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
309                 ret = 0;
310             }
311         }
312     }
313
314     if (!nextupd)
315         return ret;
316
317     /* Check nextUpdate is valid and not more than nsec in the past */
318     if (!ASN1_GENERALIZEDTIME_check(nextupd)) {
319         OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
320         ret = 0;
321     } else {
322         t_tmp = t_now - nsec;
323         if (X509_cmp_time(nextupd, &t_tmp) < 0) {
324             OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
325             ret = 0;
326         }
327     }
328
329     /* Also don't allow nextUpdate to precede thisUpdate */
330     if (ASN1_STRING_cmp(nextupd, thisupd) < 0) {
331         OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY,
332                 OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
333         ret = 0;
334     }
335
336     return ret;
337 }
338
339 OCSP_CERTID *OCSP_SINGLERESP_get0_id(OCSP_SINGLERESP *single)
340 {
341     return single->certId;
342 }