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