3cfe3649ccb12fc4cf3679b79b9fbb9afa2c92eb
[openssl.git] / crypto / ocsp / ocsp_srv.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 "internal/cryptlib.h"
12 #include <openssl/objects.h>
13 #include <openssl/x509.h>
14 #include <openssl/pem.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/ocsp.h>
17 #include "ocsp_local.h"
18
19 DEFINE_STACK_OF(OCSP_ONEREQ)
20 DEFINE_STACK_OF(X509)
21 DEFINE_STACK_OF(OCSP_SINGLERESP)
22
23 /*
24  * Utility functions related to sending OCSP responses and extracting
25  * relevant information from the request.
26  */
27
28 int OCSP_request_onereq_count(OCSP_REQUEST *req)
29 {
30     return sk_OCSP_ONEREQ_num(req->tbsRequest.requestList);
31 }
32
33 OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
34 {
35     return sk_OCSP_ONEREQ_value(req->tbsRequest.requestList, i);
36 }
37
38 OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
39 {
40     return one->reqCert;
41 }
42
43 int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
44                       ASN1_OCTET_STRING **pikeyHash,
45                       ASN1_INTEGER **pserial, OCSP_CERTID *cid)
46 {
47     if (!cid)
48         return 0;
49     if (pmd)
50         *pmd = cid->hashAlgorithm.algorithm;
51     if (piNameHash)
52         *piNameHash = &cid->issuerNameHash;
53     if (pikeyHash)
54         *pikeyHash = &cid->issuerKeyHash;
55     if (pserial)
56         *pserial = &cid->serialNumber;
57     return 1;
58 }
59
60 int OCSP_request_is_signed(OCSP_REQUEST *req)
61 {
62     if (req->optionalSignature)
63         return 1;
64     return 0;
65 }
66
67 /* Create an OCSP response and encode an optional basic response */
68 OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
69 {
70     OCSP_RESPONSE *rsp = NULL;
71
72     if ((rsp = OCSP_RESPONSE_new()) == NULL)
73         goto err;
74     if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
75         goto err;
76     if (!bs)
77         return rsp;
78     if ((rsp->responseBytes = OCSP_RESPBYTES_new()) == NULL)
79         goto err;
80     rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
81     if (!ASN1_item_pack
82         (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
83          goto err;
84     return rsp;
85  err:
86     OCSP_RESPONSE_free(rsp);
87     return NULL;
88 }
89
90 OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
91                                         OCSP_CERTID *cid,
92                                         int status, int reason,
93                                         ASN1_TIME *revtime,
94                                         ASN1_TIME *thisupd,
95                                         ASN1_TIME *nextupd)
96 {
97     OCSP_SINGLERESP *single = NULL;
98     OCSP_CERTSTATUS *cs;
99     OCSP_REVOKEDINFO *ri;
100
101     if (rsp->tbsResponseData.responses == NULL
102         && (rsp->tbsResponseData.responses
103                 = sk_OCSP_SINGLERESP_new_null()) == NULL)
104         goto err;
105
106     if ((single = OCSP_SINGLERESP_new()) == NULL)
107         goto err;
108
109     if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
110         goto err;
111     if (nextupd &&
112         !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
113         goto err;
114
115     OCSP_CERTID_free(single->certId);
116
117     if ((single->certId = OCSP_CERTID_dup(cid)) == NULL)
118         goto err;
119
120     cs = single->certStatus;
121     switch (cs->type = status) {
122     case V_OCSP_CERTSTATUS_REVOKED:
123         if (!revtime) {
124             OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
125             goto err;
126         }
127         if ((cs->value.revoked = ri = OCSP_REVOKEDINFO_new()) == NULL)
128             goto err;
129         if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
130             goto err;
131         if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
132             if ((ri->revocationReason = ASN1_ENUMERATED_new()) == NULL)
133                 goto err;
134             if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
135                 goto err;
136         }
137         break;
138
139     case V_OCSP_CERTSTATUS_GOOD:
140         if ((cs->value.good = ASN1_NULL_new()) == NULL)
141             goto err;
142         break;
143
144     case V_OCSP_CERTSTATUS_UNKNOWN:
145         if ((cs->value.unknown = ASN1_NULL_new()) == NULL)
146             goto err;
147         break;
148
149     default:
150         goto err;
151
152     }
153     if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData.responses, single)))
154         goto err;
155     return single;
156  err:
157     OCSP_SINGLERESP_free(single);
158     return NULL;
159 }
160
161 /* Add a certificate to an OCSP request */
162
163 int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
164 {
165     if (resp->certs == NULL
166         && (resp->certs = sk_X509_new_null()) == NULL)
167         return 0;
168
169     if (!sk_X509_push(resp->certs, cert))
170         return 0;
171     X509_up_ref(cert);
172     return 1;
173 }
174
175 /*
176  * Sign an OCSP response using the parameters contained in the digest context,
177  * set the responderID to the subject name in the signer's certificate, and
178  * include one or more optional certificates in the response.
179  */
180
181 int OCSP_basic_sign_ctx(OCSP_BASICRESP *brsp,
182                     X509 *signer, EVP_MD_CTX *ctx,
183                     STACK_OF(X509) *certs, unsigned long flags)
184 {
185     int i;
186     OCSP_RESPID *rid;
187     EVP_PKEY *pkey;
188
189     if (ctx == NULL || EVP_MD_CTX_pkey_ctx(ctx) == NULL) {
190         OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX, OCSP_R_NO_SIGNER_KEY);
191         goto err;
192     }
193
194     pkey = EVP_PKEY_CTX_get0_pkey(EVP_MD_CTX_pkey_ctx(ctx));
195     if (pkey == NULL || !X509_check_private_key(signer, pkey)) {
196         OCSPerr(OCSP_F_OCSP_BASIC_SIGN_CTX,
197                 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
198         goto err;
199     }
200
201     if (!(flags & OCSP_NOCERTS)) {
202         if (!OCSP_basic_add1_cert(brsp, signer))
203             goto err;
204         for (i = 0; i < sk_X509_num(certs); i++) {
205             X509 *tmpcert = sk_X509_value(certs, i);
206             if (!OCSP_basic_add1_cert(brsp, tmpcert))
207                 goto err;
208         }
209     }
210
211     rid = &brsp->tbsResponseData.responderId;
212     if (flags & OCSP_RESPID_KEY) {
213         if (!OCSP_RESPID_set_by_key(rid, signer))
214             goto err;
215     } else if (!OCSP_RESPID_set_by_name(rid, signer)) {
216         goto err;
217     }
218
219     if (!(flags & OCSP_NOTIME) &&
220         !X509_gmtime_adj(brsp->tbsResponseData.producedAt, 0))
221         goto err;
222
223     /*
224      * Right now, I think that not doing double hashing is the right thing.
225      * -- Richard Levitte
226      */
227
228     if (!OCSP_BASICRESP_sign_ctx(brsp, ctx, 0))
229         goto err;
230
231     return 1;
232  err:
233     return 0;
234 }
235
236 int OCSP_basic_sign(OCSP_BASICRESP *brsp,
237                     X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
238                     STACK_OF(X509) *certs, unsigned long flags)
239 {
240     EVP_MD_CTX *ctx = EVP_MD_CTX_new();
241     EVP_PKEY_CTX *pkctx = NULL;
242     int i;
243
244     if (ctx == NULL)
245         return 0;
246
247     if (!EVP_DigestSignInit(ctx, &pkctx, dgst, NULL, key)) {
248         EVP_MD_CTX_free(ctx);
249         return 0;
250     }
251     i = OCSP_basic_sign_ctx(brsp, signer, ctx, certs, flags);
252     EVP_MD_CTX_free(ctx);
253     return i;
254 }
255
256 int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert)
257 {
258     if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert)))
259         return 0;
260
261     respid->type = V_OCSP_RESPID_NAME;
262
263     return 1;
264 }
265
266 int OCSP_RESPID_set_by_key_ex(OCSP_RESPID *respid, X509 *cert,
267                               OPENSSL_CTX *libctx, const char *propq)
268 {
269     ASN1_OCTET_STRING *byKey = NULL;
270     unsigned char md[SHA_DIGEST_LENGTH];
271     EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
272     int ret = 0;
273
274     if (sha1 == NULL)
275         return 0;
276
277     /* RFC2560 requires SHA1 */
278     if (!X509_pubkey_digest(cert, sha1, md, NULL))
279         goto err;
280
281     byKey = ASN1_OCTET_STRING_new();
282     if (byKey == NULL)
283         goto err;
284
285     if (!(ASN1_OCTET_STRING_set(byKey, md, SHA_DIGEST_LENGTH))) {
286         ASN1_OCTET_STRING_free(byKey);
287         goto err;
288     }
289
290     respid->type = V_OCSP_RESPID_KEY;
291     respid->value.byKey = byKey;
292
293     ret = 1;
294  err:
295     EVP_MD_free(sha1);
296     return ret;
297 }
298
299 int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert)
300 {
301     return OCSP_RESPID_set_by_key_ex(respid, cert, NULL, NULL);
302 }
303
304 int OCSP_RESPID_match_ex(OCSP_RESPID *respid, X509 *cert, OPENSSL_CTX *libctx,
305                          const char *propq)
306 {
307     EVP_MD *sha1 = NULL;
308     int ret = 0;
309
310     if (respid->type == V_OCSP_RESPID_KEY) {
311         unsigned char md[SHA_DIGEST_LENGTH];
312
313         sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
314         if (sha1 == NULL)
315             goto err;
316
317         if (respid->value.byKey == NULL)
318             goto err;
319
320         /* RFC2560 requires SHA1 */
321         if (!X509_pubkey_digest(cert, sha1, md, NULL))
322             goto err;
323
324         ret = (ASN1_STRING_length(respid->value.byKey) == SHA_DIGEST_LENGTH)
325               && (memcmp(ASN1_STRING_get0_data(respid->value.byKey), md,
326                          SHA_DIGEST_LENGTH) == 0);
327     } else if (respid->type == V_OCSP_RESPID_NAME) {
328         if (respid->value.byName == NULL)
329             return 0;
330
331         return X509_NAME_cmp(respid->value.byName,
332                              X509_get_subject_name(cert)) == 0;
333     }
334
335  err:
336     EVP_MD_free(sha1);
337     return ret;
338 }
339
340 int OCSP_RESPID_match(OCSP_RESPID *respid, X509 *cert)
341 {
342     return OCSP_RESPID_match_ex(respid, cert, NULL, NULL);
343 }