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