Update OCSP API.
[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/ocsp.h>
71
72 /* Convert a certificate and its issuer to an OCSP_CERTID */
73
74 OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
75 {
76         X509_NAME *iname;
77         ASN1_INTEGER *serial;
78         ASN1_BIT_STRING *ikey;
79 #ifndef NO_SHA1
80         if(!dgst) dgst = EVP_sha1();
81 #endif
82         iname = X509_get_issuer_name(subject);
83         serial = X509_get_serialNumber(subject);
84         ikey = issuer->cert_info->key->public_key;
85         return OCSP_cert_id_new(dgst, iname, ikey, serial);
86 }
87
88
89 OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst, 
90                               X509_NAME *issuerName, 
91                               ASN1_BIT_STRING* issuerKey, 
92                               ASN1_INTEGER *serialNumber)
93         {
94         int nid;
95         unsigned int i;
96         X509_ALGOR *alg;
97         OCSP_CERTID *cid = NULL;
98         unsigned char md[EVP_MAX_MD_SIZE];
99         EVP_MD_CTX ctx;
100
101         if (!(cid = OCSP_CERTID_new())) goto err;
102
103         alg = cid->hashAlgorithm;
104         if (alg->algorithm != NULL) ASN1_OBJECT_free(alg->algorithm);
105         if ((nid = EVP_MD_type(dgst)) == NID_undef)
106                 {
107                 OCSPerr(OCSP_F_CERT_ID_NEW,OCSP_R_UNKNOWN_NID);
108                 goto err;
109                 }
110         if (!(alg->algorithm=OBJ_nid2obj(nid))) goto err;
111         if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
112         alg->parameter->type=V_ASN1_NULL;
113
114         if (!X509_NAME_digest(issuerName, dgst, md, &i)) goto digerr;
115         if (!(ASN1_OCTET_STRING_set(cid->issuerNameHash, md, i))) goto err;
116
117         /* Calculate the issuerKey hash, excluding tag and length */
118         EVP_DigestInit(&ctx,dgst);
119         EVP_DigestUpdate(&ctx,issuerKey->data, issuerKey->length);
120         EVP_DigestFinal(&ctx,md,&i);
121
122         if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
123         
124         if (cid->serialNumber != NULL) ASN1_INTEGER_free(cid->serialNumber);
125         if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err;
126         return cid;
127 digerr:
128         OCSPerr(OCSP_F_CERT_ID_NEW,OCSP_R_DIGEST_ERR);
129 err:
130         if (cid) OCSP_CERTID_free(cid);
131         return NULL;
132         }
133
134 OCSP_CERTSTATUS *OCSP_cert_status_new(int status, int reason, char *tim)
135         {
136         OCSP_REVOKEDINFO *ri;
137         OCSP_CERTSTATUS *cs = NULL;
138
139         if (!(cs = OCSP_CERTSTATUS_new())) goto err;
140         if ((cs->type = status) == V_OCSP_CERTSTATUS_REVOKED)
141                 {
142                 if (!time)
143                         {
144                         OCSPerr(OCSP_F_CERT_STATUS_NEW,OCSP_R_REVOKED_NO_TIME);
145                         goto err;
146                         }
147                 if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new())) goto err;
148                 if (!ASN1_GENERALIZEDTIME_set_string(ri->revocationTime,tim))
149                         goto err;       
150                 if (reason != OCSP_REVOKED_STATUS_NOSTATUS)
151                         {
152                         if (!(ri->revocationReason = ASN1_ENUMERATED_new())) 
153                                 goto err;
154                         if (!(ASN1_ENUMERATED_set(ri->revocationReason, 
155                                                   reason)))
156                                 goto err;       
157                         }
158                 }
159         return cs;
160 err:
161         if (cs) OCSP_CERTSTATUS_free(cs);
162         return NULL;
163         }
164
165 OCSP_REQUEST *OCSP_request_new(X509_NAME* name)
166         {
167         OCSP_REQUEST *req = NULL;
168
169         if ((req = OCSP_REQUEST_new()) == NULL) goto err;
170         if (name) /* optional */
171                 {
172                 if (!(req->tbsRequest->requestorName=GENERAL_NAME_new()))
173                         goto err;
174                 req->tbsRequest->requestorName->type = GEN_DIRNAME;
175                 req->tbsRequest->requestorName->d.dirn = X509_NAME_dup(name);
176                 }
177         if (!(req->tbsRequest->requestList = sk_OCSP_ONEREQ_new(NULL))) goto err;
178         return req;
179 err:
180         if (req) OCSP_REQUEST_free(req);
181         return NULL;
182         }
183
184 int OCSP_request_add(OCSP_REQUEST *req, OCSP_CERTID *cid)
185         {
186         OCSP_ONEREQ *one = NULL;
187
188         if (!(one = OCSP_ONEREQ_new())) goto err;
189         if (one->reqCert) OCSP_CERTID_free(one->reqCert);
190         if (!(one->reqCert = OCSP_CERTID_dup(cid))) goto err;
191         if (!sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one)) goto err;
192         return 1;
193 err:
194         if (one) OCSP_ONEREQ_free(one);
195         return 0;
196         }
197
198 int OCSP_request_sign(OCSP_REQUEST   *req,
199                       EVP_PKEY       *key,
200                       const EVP_MD   *dgst,
201                       STACK_OF(X509) *certs)
202         {
203         int i;
204         OCSP_SIGNATURE *sig;
205
206         if (!(req->optionalSignature = sig = OCSP_SIGNATURE_new())) goto err;
207         if (!OCSP_REQUEST_sign(req, key, dgst)) goto err;
208         if (certs)
209                 {
210                 if (!(sig->certs = sk_X509_dup(certs))) goto err;
211                 for (i = 0; i < sk_X509_num(sig->certs); i++)
212                         {
213                         sk_X509_set(sig->certs, i, 
214                                X509_dup(sk_X509_value(certs,i)));
215                         if (! sk_X509_value(sig->certs, i))
216                               goto err;
217                         }
218                 }
219         return 1;
220 err:
221         if (req->optionalSignature)
222                 {
223                 OCSP_SIGNATURE_free(req->optionalSignature);
224                 req->optionalSignature = NULL;
225                 }
226         return 0;
227         }
228
229 OCSP_BASICRESP *OCSP_basic_response_new(int type, X509* cert)
230         {
231         time_t t;
232         OCSP_RESPID *rid;
233         ASN1_BIT_STRING *bs;
234         OCSP_BASICRESP *rsp = NULL;
235         unsigned char md[SHA_DIGEST_LENGTH];
236         
237         if (!(rsp = OCSP_BASICRESP_new())) goto err;
238         rid = rsp->tbsResponseData->responderId;
239         switch (rid->type = type)
240                 {
241                 case V_OCSP_RESPID_NAME:
242                         /* cert is user cert */
243                         if (!(rid->value.byName =
244                                   X509_NAME_dup(X509_get_subject_name(cert))))
245                                 goto err;
246                         break;
247                 case V_OCSP_RESPID_KEY:
248                         /* cert is issuer cert */
249                         /* SHA-1 hash of responder's public key
250                          * (excluding the tag and length fields)
251                          */
252                         bs = cert->cert_info->key->public_key;
253                         SHA1(ASN1_STRING_data((ASN1_STRING*)bs), 
254                              ASN1_STRING_length((ASN1_STRING*)bs), md);
255                         if (!(rid->value.byKey = ASN1_OCTET_STRING_new()))
256                                 goto err;
257                         if (!(ASN1_OCTET_STRING_set(rid->value.byKey,
258                                                     md, sizeof md)))
259                                 goto err;
260                         break;
261                 default:
262                         OCSPerr(OCSP_F_BASIC_RESPONSE_NEW,OCSP_R_BAD_TAG);
263                         goto err;
264                         break;
265                 }
266         time(&t);
267         if (!(ASN1_GENERALIZEDTIME_set(rsp->tbsResponseData->producedAt, t)))
268                 goto err;
269         if (!(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new(NULL))) goto err;
270         return rsp;
271 err:
272         if (rsp) OCSP_BASICRESP_free(rsp);
273         return NULL;
274         }
275
276 int OCSP_basic_response_add(OCSP_BASICRESP           *rsp,
277                             OCSP_CERTID              *cid,
278                             OCSP_CERTSTATUS          *cst,
279                             char                     *this,
280                             char                     *next)
281         {
282         OCSP_SINGLERESP *single = NULL;
283
284         if (!(single = OCSP_SINGLERESP_new())) goto err;
285         if (single->certId) OCSP_CERTID_free(single->certId);
286         if (!(single->certId = OCSP_CERTID_dup(cid))) goto err;
287         if (single->certStatus) OCSP_CERTSTATUS_free(single->certStatus);
288         if (!(single->certStatus = OCSP_CERTSTATUS_dup(cst))) goto err;
289         if (!ASN1_GENERALIZEDTIME_set_string(single->thisUpdate,this))goto err;
290         if (next)
291                 { 
292                 if (!(single->nextUpdate = ASN1_GENERALIZEDTIME_new()))
293                         goto err;
294                 if (!ASN1_GENERALIZEDTIME_set_string(single->nextUpdate,next))
295                         goto err;
296                 }
297         if (!sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses,single)) goto err;
298         return 1;
299 err:
300         if (single) OCSP_SINGLERESP_free(single);
301         return 0;
302         }
303
304 int OCSP_basic_response_sign(OCSP_BASICRESP *brsp, 
305                              EVP_PKEY       *key,
306                              const EVP_MD   *dgst,
307                              STACK_OF(X509) *certs)
308         {
309         int i;
310
311         /* Right now, I think that not doing double hashing is the right
312            thing.       -- Richard Levitte */
313         if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0)) goto err;
314         if (certs)
315                 {
316                 if (!(brsp->certs = sk_X509_dup(certs))) goto err;
317                 for (i = 0; i < sk_X509_num(brsp->certs); i++)
318                         {
319                         sk_X509_set(brsp->certs, i,
320                                X509_dup(sk_X509_value(certs, i)));
321                         if (! sk_X509_value(brsp->certs, i))
322                                 goto err;
323                         }
324                 }
325         return 1;
326 err:
327         return 0;
328         }
329
330 OCSP_RESPONSE *OCSP_response_new(int status,
331                                  int nid,
332                                  int (*i2d)(),
333                                  char *data)
334         {
335         OCSP_RESPONSE *rsp = NULL;
336
337         if (!(rsp = OCSP_RESPONSE_new())) goto err;
338         if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) goto err;
339         if (!(rsp->responseBytes = OCSP_RESPBYTES_new())) goto err;
340         if (rsp->responseBytes->responseType) ASN1_OBJECT_free(rsp->responseBytes->responseType);
341         if (!(rsp->responseBytes->responseType = OBJ_nid2obj(nid))) goto err;
342         if (!ASN1_STRING_encode((ASN1_STRING*)rsp->responseBytes->response,
343                                 i2d, data, NULL)) goto err;
344         return rsp;
345 err:
346         if (rsp) OCSP_RESPONSE_free(rsp);
347         return NULL;
348         }
349
350 /* XXX assumes certs in signature are sorted root to leaf XXX */
351 int OCSP_request_verify(OCSP_REQUEST *req, EVP_PKEY *pkey)
352         {
353         STACK_OF(X509) *sk;
354
355         if (!req->optionalSignature) return 0;
356         if (pkey == NULL)
357                 {
358                 if (!(sk = req->optionalSignature->certs)) return 0;
359                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
360                         {
361                         OCSPerr(OCSP_F_REQUEST_VERIFY,OCSP_R_NO_PUBLIC_KEY);
362                         return 0;
363                         }
364                 }
365         return OCSP_REQUEST_verify(req, pkey);
366         }
367
368 int OCSP_response_verify(OCSP_RESPONSE *rsp, EVP_PKEY *pkey)
369         {
370         int i, r;
371         unsigned char *p;
372         OCSP_RESPBYTES *rb;
373         OCSP_BASICRESP *br = NULL;
374
375         if ((rb = rsp->responseBytes) == NULL) 
376                 {
377                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_NO_RESPONSE_DATA);
378                 return 0;
379                 }
380         if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) 
381                 {
382                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_BAD_TAG);
383                 return 0;
384                 }
385         p = ASN1_STRING_data(rb->response);
386         i = ASN1_STRING_length(rb->response);
387         if (!(d2i_OCSP_BASICRESP(&br, &p, i))) return 0;
388         r = OCSP_basic_response_verify(br, pkey);
389         OCSP_BASICRESP_free(br);
390         return r;
391         }
392
393 int OCSP_basic_response_verify(OCSP_BASICRESP *rsp, EVP_PKEY *pkey)
394         {
395         STACK_OF(X509) *sk;
396         int ret;
397
398         if (!rsp->signature) 
399                 {
400                 OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_SIGNATURE);
401                 return 0;
402                 }
403         if (pkey == NULL)
404                 {
405                 if (!(sk = rsp->certs))
406                         {
407                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_CERTIFICATE);
408                         return 0;
409                         }
410                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
411                         {
412                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_PUBLIC_KEY);
413                         return 0;
414                         }
415                 }
416         ret = OCSP_BASICRESP_verify(rsp, pkey, 0);
417         return ret;
418         }