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