be1ff9611299d7926d2c4881f3a20185318618c9
[openssl.git] / crypto / ocsp / ocsp_lib.c
1 /* ocsp_lib.c */
2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3  * project. */
4
5 /* History:
6    This file was transfered to Richard Levitte from CertCo by Kathy
7    Weinhold in mid-spring 2000 to be included in OpenSSL or released
8    as a patch kit. */
9
10 /* ====================================================================
11  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer. 
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in
22  *    the documentation and/or other materials provided with the
23  *    distribution.
24  *
25  * 3. All advertising materials mentioning features or use of this
26  *    software must display the following acknowledgment:
27  *    "This product includes software developed by the OpenSSL Project
28  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
29  *
30  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
31  *    endorse or promote products derived from this software without
32  *    prior written permission. For written permission, please contact
33  *    openssl-core@openssl.org.
34  *
35  * 5. Products derived from this software may not be called "OpenSSL"
36  *    nor may "OpenSSL" appear in their names without prior written
37  *    permission of the OpenSSL Project.
38  *
39  * 6. Redistributions of any form whatsoever must retain the following
40  *    acknowledgment:
41  *    "This product includes software developed by the OpenSSL Project
42  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
45  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55  * OF THE POSSIBILITY OF SUCH DAMAGE.
56  * ====================================================================
57  *
58  * This product includes cryptographic software written by Eric Young
59  * (eay@cryptsoft.com).  This product includes software written by Tim
60  * Hudson (tjh@cryptsoft.com).
61  *
62  */
63
64 #include <stdio.h>
65 #include <cryptlib.h>
66 #include <openssl/objects.h>
67 #include <openssl/x509.h>
68 #include <openssl/pem.h>
69 #include <openssl/x509v3.h>
70 #include <openssl/safestack.h>
71 #include <openssl/ocsp.h>
72
73 static STACK_OF(X509_EXTENSION) *ext_dup(STACK_OF(X509_EXTENSION) *fr)
74         {
75         int i;
76         STACK_OF(X509_EXTENSION) *to = NULL;
77
78         if (!(to = sk_X509_EXTENSION_dup(fr)))
79                 goto err;
80         for (i = 0; i < sk_X509_EXTENSION_num(fr); i++)
81                 {
82                 sk_X509_EXTENSION_set(to, i,
83                       X509_EXTENSION_dup(sk_X509_EXTENSION_value(fr, i)));
84                 if (! sk_X509_EXTENSION_value(to, i))
85                         goto err;
86                 }
87         return to;
88 err:
89         if (to) sk_X509_EXTENSION_pop_free(to, X509_EXTENSION_free);
90         return NULL;
91         }
92
93 OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst, 
94                               X509_NAME *issuerName, 
95                               ASN1_BIT_STRING* issuerKey, 
96                               ASN1_INTEGER *serialNumber)
97         {
98         int nid;
99         unsigned int i;
100         X509_ALGOR *alg;
101         OCSP_CERTID *cid = NULL;
102         unsigned char md[EVP_MAX_MD_SIZE];
103         EVP_MD_CTX ctx;
104
105         if (!(cid = OCSP_CERTID_new())) goto err;
106
107         alg = cid->hashAlgorithm;
108         if (alg->algorithm != NULL) ASN1_OBJECT_free(alg->algorithm);
109         if ((nid = EVP_MD_type(dgst)) == NID_undef)
110                 {
111                 OCSPerr(OCSP_F_CERT_ID_NEW,OCSP_R_UNKNOWN_NID);
112                 goto err;
113                 }
114         if (!(alg->algorithm=OBJ_nid2obj(nid))) goto err;
115         if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
116         alg->parameter->type=V_ASN1_NULL;
117
118         if (!X509_NAME_digest(issuerName, dgst, md, &i)) goto digerr;
119         if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) goto err;
120
121         /* Calculate the issuerKey hash, excluding tag and length */
122         EVP_DigestInit(&ctx,dgst);
123         EVP_DigestUpdate(&ctx,issuerKey->data, issuerKey->length);
124         EVP_DigestFinal(&ctx,md,&i);
125
126         if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
127         
128         if (cid->serialNumber != NULL) ASN1_INTEGER_free(cid->serialNumber);
129         if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err;
130         return cid;
131 digerr:
132         OCSPerr(OCSP_F_CERT_ID_NEW,OCSP_R_DIGEST_ERR);
133 err:
134         if (cid) OCSP_CERTID_free(cid);
135         return NULL;
136         }
137
138 OCSP_CERTSTATUS *OCSP_cert_status_new(int status, int reason, char *tim)
139         {
140         OCSP_REVOKEDINFO *ri;
141         OCSP_CERTSTATUS *cs = NULL;
142
143         if (!(cs = OCSP_CERTSTATUS_new())) goto err;
144         if ((cs->type = status) == V_OCSP_CERTSTATUS_REVOKED)
145                 {
146                 if (!time)
147                         {
148                         OCSPerr(OCSP_F_CERT_STATUS_NEW,OCSP_R_REVOKED_NO_TIME);
149                         goto err;
150                         }
151                 if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new())) goto err;
152                 if (!ASN1_GENERALIZEDTIME_set_string(ri->revocationTime,tim))
153                         goto err;       
154                 if (reason != OCSP_REVOKED_STATUS_NOSTATUS)
155                         {
156                         if (!(ri->revocationReason = ASN1_ENUMERATED_new())) 
157                                 goto err;
158                         if (!(ASN1_ENUMERATED_set(ri->revocationReason, 
159                                                   reason)))
160                                 goto err;       
161                         }
162                 }
163         return cs;
164 err:
165         if (cs) OCSP_CERTSTATUS_free(cs);
166         return NULL;
167         }
168
169 OCSP_REQUEST *OCSP_request_new(X509_NAME* name,
170                                STACK_OF(X509_EXTENSION) *extensions)
171         {
172         OCSP_REQUEST *req = NULL;
173
174         if ((req = OCSP_REQUEST_new()) == NULL) goto err;
175         if (name) /* optional */
176                 {
177                 if (!(req->tbsRequest->requestorName=GENERAL_NAME_new()))
178                         goto err;
179                 req->tbsRequest->requestorName->type = GEN_DIRNAME;
180                 req->tbsRequest->requestorName->d.dirn = X509_NAME_dup(name);
181                 }
182         if (!(req->tbsRequest->requestList = sk_OCSP_ONEREQ_new(NULL))) goto err;
183         if (extensions && 
184             (!(req->tbsRequest->requestExtensions = ext_dup(extensions))))
185                 goto err;
186         return req;
187 err:
188         if (req) OCSP_REQUEST_free(req);
189         return NULL;
190         }
191
192 int OCSP_request_add(OCSP_REQUEST             *req,
193                      OCSP_CERTID              *cid,
194                      STACK_OF(X509_EXTENSION) *extensions)
195         {
196         OCSP_ONEREQ *one = NULL;
197
198         if (!(one = OCSP_ONEREQ_new())) goto err;
199         if (one->reqCert) OCSP_CERTID_free(one->reqCert);
200         if (!(one->reqCert = OCSP_CERTID_dup(cid))) goto err;
201         if (extensions&&(!(one->singleRequestExtensions=ext_dup(extensions))))
202                 goto err;
203         if (!sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one)) goto err;
204         return 1;
205 err:
206         if (one) OCSP_ONEREQ_free(one);
207         return 0;
208         }
209
210 int OCSP_request_sign(OCSP_REQUEST   *req,
211                       EVP_PKEY       *key,
212                       const EVP_MD   *dgst,
213                       STACK_OF(X509) *certs)
214         {
215         int i;
216         OCSP_SIGNATURE *sig;
217
218         if (!(req->optionalSignature = sig = OCSP_SIGNATURE_new())) goto err;
219         if (!OCSP_REQUEST_sign(req, key, dgst)) goto err;
220         if (certs)
221                 {
222                 if (!(sig->certs = sk_X509_dup(certs))) goto err;
223                 for (i = 0; i < sk_X509_num(sig->certs); i++)
224                         {
225                         sk_X509_set(sig->certs, i, 
226                                X509_dup(sk_X509_value(certs,i)));
227                         if (! sk_X509_value(sig->certs, i))
228                               goto err;
229                         }
230                 }
231         return 1;
232 err:
233         if (req->optionalSignature)
234                 {
235                 OCSP_SIGNATURE_free(req->optionalSignature);
236                 req->optionalSignature = NULL;
237                 }
238         return 0;
239         }
240
241 OCSP_BASICRESP *OCSP_basic_response_new(int type,
242                                         X509* cert,
243                                         STACK_OF(X509_EXTENSION) *extensions)
244         {
245         time_t t;
246         OCSP_RESPID *rid;
247         ASN1_BIT_STRING *bs;
248         OCSP_BASICRESP *rsp = NULL;
249         unsigned char md[SHA_DIGEST_LENGTH];
250         
251         if (!(rsp = OCSP_BASICRESP_new())) goto err;
252         rid = rsp->tbsResponseData->responderId;
253         switch (rid->type = type)
254                 {
255                 case V_OCSP_RESPID_NAME:
256                         /* cert is user cert */
257                         if (!(rid->value.byName =
258                                   X509_NAME_dup(X509_get_subject_name(cert))))
259                                 goto err;
260                         break;
261                 case V_OCSP_RESPID_KEY:
262                         /* cert is issuer cert */
263                         /* SHA-1 hash of responder's public key
264                          * (excluding the tag and length fields)
265                          */
266                         bs = cert->cert_info->key->public_key;
267                         SHA1(ASN1_STRING_data((ASN1_STRING*)bs), 
268                              ASN1_STRING_length((ASN1_STRING*)bs), md);
269                         if (!(rid->value.byKey = ASN1_OCTET_STRING_new()))
270                                 goto err;
271                         if (!(ASN1_OCTET_STRING_set(rid->value.byKey,
272                                                     md, sizeof md)))
273                                 goto err;
274                         break;
275                 default:
276                         OCSPerr(OCSP_F_BASIC_RESPONSE_NEW,OCSP_R_BAD_TAG);
277                         goto err;
278                         break;
279                 }
280         time(&t);
281         if (!(ASN1_GENERALIZEDTIME_set(rsp->tbsResponseData->producedAt, t)))
282                 goto err;
283         if (!(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new(NULL))) goto err;
284         if (extensions && (!(rsp->tbsResponseData->responseExtensions = 
285                                               ext_dup(extensions))))
286                 goto err;
287         return rsp;
288 err:
289         if (rsp) OCSP_BASICRESP_free(rsp);
290         return NULL;
291         }
292
293 int OCSP_basic_response_add(OCSP_BASICRESP           *rsp,
294                             OCSP_CERTID              *cid,
295                             OCSP_CERTSTATUS          *cst,
296                             char                     *this,
297                             char                     *next,
298                             STACK_OF(X509_EXTENSION) *extensions)
299         {
300         OCSP_SINGLERESP *single = NULL;
301
302         if (!(single = OCSP_SINGLERESP_new())) goto err;
303         if (single->certId) OCSP_CERTID_free(single->certId);
304         if (!(single->certId = OCSP_CERTID_dup(cid))) goto err;
305         if (single->certStatus) OCSP_CERTSTATUS_free(single->certStatus);
306         if (!(single->certStatus = OCSP_CERTSTATUS_dup(cst))) goto err;
307         if (!ASN1_GENERALIZEDTIME_set_string(single->thisUpdate,this))goto err;
308         if (next)
309                 { 
310                 if (!(single->nextUpdate = ASN1_GENERALIZEDTIME_new()))
311                         goto err;
312                 if (!ASN1_GENERALIZEDTIME_set_string(single->nextUpdate,next))
313                         goto err;
314                 }
315         if (extensions && (!(single->singleExtensions = ext_dup(extensions))))
316                 goto err;
317         if (!sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses,single)) goto err;
318         return 1;
319 err:
320         if (single) OCSP_SINGLERESP_free(single);
321         return 0;
322         }
323
324 int OCSP_basic_response_sign(OCSP_BASICRESP *brsp, 
325                              EVP_PKEY       *key,
326                              const EVP_MD   *dgst,
327                              STACK_OF(X509) *certs)
328         {
329         int i;
330
331         /* Right now, I think that not doing double hashing is the right
332            thing.       -- Richard Levitte */
333         if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0)) goto err;
334         if (certs)
335                 {
336                 if (!(brsp->certs = sk_X509_dup(certs))) goto err;
337                 for (i = 0; i < sk_X509_num(brsp->certs); i++)
338                         {
339                         sk_X509_set(brsp->certs, i,
340                                X509_dup(sk_X509_value(certs, i)));
341                         if (! sk_X509_value(brsp->certs, i))
342                                 goto err;
343                         }
344                 }
345         return 1;
346 err:
347         return 0;
348         }
349
350 OCSP_RESPONSE *OCSP_response_new(int status,
351                                  int nid,
352                                  int (*i2d)(),
353                                  char *data)
354         {
355         OCSP_RESPONSE *rsp = NULL;
356
357         if (!(rsp = OCSP_RESPONSE_new())) goto err;
358         if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) goto err;
359         if (!(rsp->responseBytes = OCSP_RESPBYTES_new())) goto err;
360         if (rsp->responseBytes->responseType) ASN1_OBJECT_free(rsp->responseBytes->responseType);
361         if (!(rsp->responseBytes->responseType = OBJ_nid2obj(nid))) goto err;
362         if (!ASN1_STRING_encode((ASN1_STRING*)rsp->responseBytes->response,
363                                 i2d, data, NULL)) goto err;
364         return rsp;
365 err:
366         if (rsp) OCSP_RESPONSE_free(rsp);
367         return NULL;
368         }
369
370 /* XXX assumes certs in signature are sorted root to leaf XXX */
371 int OCSP_request_verify(OCSP_REQUEST *req, EVP_PKEY *pkey)
372         {
373         STACK_OF(X509) *sk;
374
375         if (!req->optionalSignature) return 0;
376         if (pkey == NULL)
377                 {
378                 if (!(sk = req->optionalSignature->certs)) return 0;
379                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
380                         {
381                         OCSPerr(OCSP_F_REQUEST_VERIFY,OCSP_R_NO_PUBLIC_KEY);
382                         return 0;
383                         }
384                 }
385         return OCSP_REQUEST_verify(req, pkey);
386         }
387
388 int OCSP_response_verify(OCSP_RESPONSE *rsp, EVP_PKEY *pkey)
389         {
390         int i, r;
391         unsigned char *p;
392         OCSP_RESPBYTES *rb;
393         OCSP_BASICRESP *br = NULL;
394
395         if ((rb = rsp->responseBytes) == NULL) 
396                 {
397                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_NO_RESPONSE_DATA);
398                 return 0;
399                 }
400         if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) 
401                 {
402                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_BAD_TAG);
403                 return 0;
404                 }
405         p = ASN1_STRING_data(rb->response);
406         i = ASN1_STRING_length(rb->response);
407         if (!(d2i_OCSP_BASICRESP(&br, &p, i))) return 0;
408         r = OCSP_basic_response_verify(br, pkey);
409         OCSP_BASICRESP_free(br);
410         return r;
411         }
412
413 int OCSP_basic_response_verify(OCSP_BASICRESP *rsp, EVP_PKEY *pkey)
414         {
415         STACK_OF(X509) *sk;
416         int ret;
417
418         if (!rsp->signature) 
419                 {
420                 OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_SIGNATURE);
421                 return 0;
422                 }
423         if (pkey == NULL)
424                 {
425                 if (!(sk = rsp->certs))
426                         {
427                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_CERTIFICATE);
428                         return 0;
429                         }
430                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
431                         {
432                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_PUBLIC_KEY);
433                         return 0;
434                         }
435                 }
436         ret = OCSP_BASICRESP_verify(rsp, pkey, 0);
437         return ret;
438         }