xref: /minix3/crypto/external/bsd/openssl/dist/crypto/ocsp/ocsp_srv.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* ocsp_srv.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc  * 2001.
5ebfedea0SLionel Sambuc  */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc  *    distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
23ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25ebfedea0SLionel Sambuc  *
26ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc  *
35ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc  *    acknowledgment:
37ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc  * ====================================================================
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
56ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc  *
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc #include <stdio.h>
61ebfedea0SLionel Sambuc #include <cryptlib.h>
62ebfedea0SLionel Sambuc #include <openssl/objects.h>
63ebfedea0SLionel Sambuc #include <openssl/rand.h>
64ebfedea0SLionel Sambuc #include <openssl/x509.h>
65ebfedea0SLionel Sambuc #include <openssl/pem.h>
66ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
67ebfedea0SLionel Sambuc #include <openssl/ocsp.h>
68ebfedea0SLionel Sambuc 
69*0a6a1f1dSLionel Sambuc /*
70*0a6a1f1dSLionel Sambuc  * Utility functions related to sending OCSP responses and extracting
71ebfedea0SLionel Sambuc  * relevant information from the request.
72ebfedea0SLionel Sambuc  */
73ebfedea0SLionel Sambuc 
OCSP_request_onereq_count(OCSP_REQUEST * req)74ebfedea0SLionel Sambuc int OCSP_request_onereq_count(OCSP_REQUEST *req)
75ebfedea0SLionel Sambuc {
76ebfedea0SLionel Sambuc     return sk_OCSP_ONEREQ_num(req->tbsRequest->requestList);
77ebfedea0SLionel Sambuc }
78ebfedea0SLionel Sambuc 
OCSP_request_onereq_get0(OCSP_REQUEST * req,int i)79ebfedea0SLionel Sambuc OCSP_ONEREQ *OCSP_request_onereq_get0(OCSP_REQUEST *req, int i)
80ebfedea0SLionel Sambuc {
81ebfedea0SLionel Sambuc     return sk_OCSP_ONEREQ_value(req->tbsRequest->requestList, i);
82ebfedea0SLionel Sambuc }
83ebfedea0SLionel Sambuc 
OCSP_onereq_get0_id(OCSP_ONEREQ * one)84ebfedea0SLionel Sambuc OCSP_CERTID *OCSP_onereq_get0_id(OCSP_ONEREQ *one)
85ebfedea0SLionel Sambuc {
86ebfedea0SLionel Sambuc     return one->reqCert;
87ebfedea0SLionel Sambuc }
88ebfedea0SLionel Sambuc 
OCSP_id_get0_info(ASN1_OCTET_STRING ** piNameHash,ASN1_OBJECT ** pmd,ASN1_OCTET_STRING ** pikeyHash,ASN1_INTEGER ** pserial,OCSP_CERTID * cid)89ebfedea0SLionel Sambuc int OCSP_id_get0_info(ASN1_OCTET_STRING **piNameHash, ASN1_OBJECT **pmd,
90ebfedea0SLionel Sambuc                       ASN1_OCTET_STRING **pikeyHash,
91ebfedea0SLionel Sambuc                       ASN1_INTEGER **pserial, OCSP_CERTID *cid)
92ebfedea0SLionel Sambuc {
93*0a6a1f1dSLionel Sambuc     if (!cid)
94*0a6a1f1dSLionel Sambuc         return 0;
95*0a6a1f1dSLionel Sambuc     if (pmd)
96*0a6a1f1dSLionel Sambuc         *pmd = cid->hashAlgorithm->algorithm;
97*0a6a1f1dSLionel Sambuc     if (piNameHash)
98*0a6a1f1dSLionel Sambuc         *piNameHash = cid->issuerNameHash;
99*0a6a1f1dSLionel Sambuc     if (pikeyHash)
100*0a6a1f1dSLionel Sambuc         *pikeyHash = cid->issuerKeyHash;
101*0a6a1f1dSLionel Sambuc     if (pserial)
102*0a6a1f1dSLionel Sambuc         *pserial = cid->serialNumber;
103ebfedea0SLionel Sambuc     return 1;
104ebfedea0SLionel Sambuc }
105ebfedea0SLionel Sambuc 
OCSP_request_is_signed(OCSP_REQUEST * req)106ebfedea0SLionel Sambuc int OCSP_request_is_signed(OCSP_REQUEST *req)
107ebfedea0SLionel Sambuc {
108*0a6a1f1dSLionel Sambuc     if (req->optionalSignature)
109*0a6a1f1dSLionel Sambuc         return 1;
110ebfedea0SLionel Sambuc     return 0;
111ebfedea0SLionel Sambuc }
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc /* Create an OCSP response and encode an optional basic response */
OCSP_response_create(int status,OCSP_BASICRESP * bs)114ebfedea0SLionel Sambuc OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs)
115ebfedea0SLionel Sambuc {
116ebfedea0SLionel Sambuc     OCSP_RESPONSE *rsp = NULL;
117ebfedea0SLionel Sambuc 
118*0a6a1f1dSLionel Sambuc     if (!(rsp = OCSP_RESPONSE_new()))
119*0a6a1f1dSLionel Sambuc         goto err;
120*0a6a1f1dSLionel Sambuc     if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status)))
121*0a6a1f1dSLionel Sambuc         goto err;
122*0a6a1f1dSLionel Sambuc     if (!bs)
123*0a6a1f1dSLionel Sambuc         return rsp;
124*0a6a1f1dSLionel Sambuc     if (!(rsp->responseBytes = OCSP_RESPBYTES_new()))
125*0a6a1f1dSLionel Sambuc         goto err;
126ebfedea0SLionel Sambuc     rsp->responseBytes->responseType = OBJ_nid2obj(NID_id_pkix_OCSP_basic);
127*0a6a1f1dSLionel Sambuc     if (!ASN1_item_pack
128*0a6a1f1dSLionel Sambuc         (bs, ASN1_ITEM_rptr(OCSP_BASICRESP), &rsp->responseBytes->response))
129ebfedea0SLionel Sambuc          goto err;
130ebfedea0SLionel Sambuc     return rsp;
131ebfedea0SLionel Sambuc  err:
132*0a6a1f1dSLionel Sambuc     if (rsp)
133*0a6a1f1dSLionel Sambuc         OCSP_RESPONSE_free(rsp);
134ebfedea0SLionel Sambuc     return NULL;
135ebfedea0SLionel Sambuc }
136ebfedea0SLionel Sambuc 
OCSP_basic_add1_status(OCSP_BASICRESP * rsp,OCSP_CERTID * cid,int status,int reason,ASN1_TIME * revtime,ASN1_TIME * thisupd,ASN1_TIME * nextupd)137ebfedea0SLionel Sambuc OCSP_SINGLERESP *OCSP_basic_add1_status(OCSP_BASICRESP *rsp,
138ebfedea0SLionel Sambuc                                         OCSP_CERTID *cid,
139ebfedea0SLionel Sambuc                                         int status, int reason,
140ebfedea0SLionel Sambuc                                         ASN1_TIME *revtime,
141*0a6a1f1dSLionel Sambuc                                         ASN1_TIME *thisupd,
142*0a6a1f1dSLionel Sambuc                                         ASN1_TIME *nextupd)
143ebfedea0SLionel Sambuc {
144ebfedea0SLionel Sambuc     OCSP_SINGLERESP *single = NULL;
145ebfedea0SLionel Sambuc     OCSP_CERTSTATUS *cs;
146ebfedea0SLionel Sambuc     OCSP_REVOKEDINFO *ri;
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc     if (!rsp->tbsResponseData->responses &&
149ebfedea0SLionel Sambuc         !(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new_null()))
150ebfedea0SLionel Sambuc         goto err;
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc     if (!(single = OCSP_SINGLERESP_new()))
153ebfedea0SLionel Sambuc         goto err;
154ebfedea0SLionel Sambuc 
155ebfedea0SLionel Sambuc     if (!ASN1_TIME_to_generalizedtime(thisupd, &single->thisUpdate))
156ebfedea0SLionel Sambuc         goto err;
157ebfedea0SLionel Sambuc     if (nextupd &&
158ebfedea0SLionel Sambuc         !ASN1_TIME_to_generalizedtime(nextupd, &single->nextUpdate))
159ebfedea0SLionel Sambuc         goto err;
160ebfedea0SLionel Sambuc 
161ebfedea0SLionel Sambuc     OCSP_CERTID_free(single->certId);
162ebfedea0SLionel Sambuc 
163ebfedea0SLionel Sambuc     if (!(single->certId = OCSP_CERTID_dup(cid)))
164ebfedea0SLionel Sambuc         goto err;
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc     cs = single->certStatus;
167*0a6a1f1dSLionel Sambuc     switch (cs->type = status) {
168ebfedea0SLionel Sambuc     case V_OCSP_CERTSTATUS_REVOKED:
169*0a6a1f1dSLionel Sambuc         if (!revtime) {
170ebfedea0SLionel Sambuc             OCSPerr(OCSP_F_OCSP_BASIC_ADD1_STATUS, OCSP_R_NO_REVOKED_TIME);
171ebfedea0SLionel Sambuc             goto err;
172ebfedea0SLionel Sambuc         }
173*0a6a1f1dSLionel Sambuc         if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new()))
174*0a6a1f1dSLionel Sambuc             goto err;
175ebfedea0SLionel Sambuc         if (!ASN1_TIME_to_generalizedtime(revtime, &ri->revocationTime))
176ebfedea0SLionel Sambuc             goto err;
177*0a6a1f1dSLionel Sambuc         if (reason != OCSP_REVOKED_STATUS_NOSTATUS) {
178ebfedea0SLionel Sambuc             if (!(ri->revocationReason = ASN1_ENUMERATED_new()))
179ebfedea0SLionel Sambuc                 goto err;
180*0a6a1f1dSLionel Sambuc             if (!(ASN1_ENUMERATED_set(ri->revocationReason, reason)))
181ebfedea0SLionel Sambuc                 goto err;
182ebfedea0SLionel Sambuc         }
183ebfedea0SLionel Sambuc         break;
184ebfedea0SLionel Sambuc 
185ebfedea0SLionel Sambuc     case V_OCSP_CERTSTATUS_GOOD:
186ebfedea0SLionel Sambuc         cs->value.good = ASN1_NULL_new();
187ebfedea0SLionel Sambuc         break;
188ebfedea0SLionel Sambuc 
189ebfedea0SLionel Sambuc     case V_OCSP_CERTSTATUS_UNKNOWN:
190ebfedea0SLionel Sambuc         cs->value.unknown = ASN1_NULL_new();
191ebfedea0SLionel Sambuc         break;
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc     default:
194ebfedea0SLionel Sambuc         goto err;
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc     }
197ebfedea0SLionel Sambuc     if (!(sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses, single)))
198ebfedea0SLionel Sambuc         goto err;
199ebfedea0SLionel Sambuc     return single;
200ebfedea0SLionel Sambuc  err:
201ebfedea0SLionel Sambuc     OCSP_SINGLERESP_free(single);
202ebfedea0SLionel Sambuc     return NULL;
203ebfedea0SLionel Sambuc }
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc /* Add a certificate to an OCSP request */
206ebfedea0SLionel Sambuc 
OCSP_basic_add1_cert(OCSP_BASICRESP * resp,X509 * cert)207ebfedea0SLionel Sambuc int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert)
208ebfedea0SLionel Sambuc {
209ebfedea0SLionel Sambuc     if (!resp->certs && !(resp->certs = sk_X509_new_null()))
210ebfedea0SLionel Sambuc         return 0;
211ebfedea0SLionel Sambuc 
212*0a6a1f1dSLionel Sambuc     if (!sk_X509_push(resp->certs, cert))
213*0a6a1f1dSLionel Sambuc         return 0;
214ebfedea0SLionel Sambuc     CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
215ebfedea0SLionel Sambuc     return 1;
216ebfedea0SLionel Sambuc }
217ebfedea0SLionel Sambuc 
OCSP_basic_sign(OCSP_BASICRESP * brsp,X509 * signer,EVP_PKEY * key,const EVP_MD * dgst,STACK_OF (X509)* certs,unsigned long flags)218ebfedea0SLionel Sambuc int OCSP_basic_sign(OCSP_BASICRESP *brsp,
219ebfedea0SLionel Sambuc                     X509 *signer, EVP_PKEY *key, const EVP_MD *dgst,
220ebfedea0SLionel Sambuc                     STACK_OF(X509) *certs, unsigned long flags)
221ebfedea0SLionel Sambuc {
222ebfedea0SLionel Sambuc     int i;
223ebfedea0SLionel Sambuc     OCSP_RESPID *rid;
224ebfedea0SLionel Sambuc 
225*0a6a1f1dSLionel Sambuc     if (!X509_check_private_key(signer, key)) {
226*0a6a1f1dSLionel Sambuc         OCSPerr(OCSP_F_OCSP_BASIC_SIGN,
227*0a6a1f1dSLionel Sambuc                 OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
228ebfedea0SLionel Sambuc         goto err;
229ebfedea0SLionel Sambuc     }
230ebfedea0SLionel Sambuc 
231*0a6a1f1dSLionel Sambuc     if (!(flags & OCSP_NOCERTS)) {
232ebfedea0SLionel Sambuc         if (!OCSP_basic_add1_cert(brsp, signer))
233ebfedea0SLionel Sambuc             goto err;
234*0a6a1f1dSLionel Sambuc         for (i = 0; i < sk_X509_num(certs); i++) {
235ebfedea0SLionel Sambuc             X509 *tmpcert = sk_X509_value(certs, i);
236ebfedea0SLionel Sambuc             if (!OCSP_basic_add1_cert(brsp, tmpcert))
237ebfedea0SLionel Sambuc                 goto err;
238ebfedea0SLionel Sambuc         }
239ebfedea0SLionel Sambuc     }
240ebfedea0SLionel Sambuc 
241ebfedea0SLionel Sambuc     rid = brsp->tbsResponseData->responderId;
242*0a6a1f1dSLionel Sambuc     if (flags & OCSP_RESPID_KEY) {
243ebfedea0SLionel Sambuc         unsigned char md[SHA_DIGEST_LENGTH];
244ebfedea0SLionel Sambuc         X509_pubkey_digest(signer, EVP_sha1(), md, NULL);
245ebfedea0SLionel Sambuc         if (!(rid->value.byKey = ASN1_OCTET_STRING_new()))
246ebfedea0SLionel Sambuc             goto err;
247ebfedea0SLionel Sambuc         if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH)))
248ebfedea0SLionel Sambuc             goto err;
249ebfedea0SLionel Sambuc         rid->type = V_OCSP_RESPID_KEY;
250*0a6a1f1dSLionel Sambuc     } else {
251*0a6a1f1dSLionel Sambuc         if (!X509_NAME_set(&rid->value.byName, X509_get_subject_name(signer)))
252ebfedea0SLionel Sambuc             goto err;
253ebfedea0SLionel Sambuc         rid->type = V_OCSP_RESPID_NAME;
254ebfedea0SLionel Sambuc     }
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc     if (!(flags & OCSP_NOTIME) &&
257ebfedea0SLionel Sambuc         !X509_gmtime_adj(brsp->tbsResponseData->producedAt, 0))
258ebfedea0SLionel Sambuc         goto err;
259ebfedea0SLionel Sambuc 
260*0a6a1f1dSLionel Sambuc     /*
261*0a6a1f1dSLionel Sambuc      * Right now, I think that not doing double hashing is the right thing.
262*0a6a1f1dSLionel Sambuc      * -- Richard Levitte
263*0a6a1f1dSLionel Sambuc      */
264ebfedea0SLionel Sambuc 
265*0a6a1f1dSLionel Sambuc     if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0))
266*0a6a1f1dSLionel Sambuc         goto err;
267ebfedea0SLionel Sambuc 
268ebfedea0SLionel Sambuc     return 1;
269ebfedea0SLionel Sambuc  err:
270ebfedea0SLionel Sambuc     return 0;
271ebfedea0SLionel Sambuc }
272