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