Various function for commmon operations.
[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/rand.h>
68 #include <openssl/x509.h>
69 #include <openssl/pem.h>
70 #include <openssl/x509v3.h>
71 #include <openssl/ocsp.h>
72
73 /* Convert a certificate and its issuer to an OCSP_CERTID */
74
75 OCSP_CERTID *OCSP_cert_to_id(const EVP_MD *dgst, X509 *subject, X509 *issuer)
76 {
77         X509_NAME *iname;
78         ASN1_INTEGER *serial;
79         ASN1_BIT_STRING *ikey;
80 #ifndef NO_SHA1
81         if(!dgst) dgst = EVP_sha1();
82 #endif
83         iname = X509_get_issuer_name(subject);
84         serial = X509_get_serialNumber(subject);
85         ikey = X509_get0_pubkey_bitstr(issuer);
86         return OCSP_cert_id_new(dgst, iname, ikey, serial);
87 }
88
89
90 OCSP_CERTID *OCSP_cert_id_new(const EVP_MD *dgst, 
91                               X509_NAME *issuerName, 
92                               ASN1_BIT_STRING* issuerKey, 
93                               ASN1_INTEGER *serialNumber)
94         {
95         int nid;
96         unsigned int i;
97         X509_ALGOR *alg;
98         OCSP_CERTID *cid = NULL;
99         unsigned char md[EVP_MAX_MD_SIZE];
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_Digest(issuerKey->data, issuerKey->length, md, &i, dgst);
119
120         if (!(ASN1_OCTET_STRING_set(cid->issuerKeyHash, md, i))) goto err;
121         
122         if (cid->serialNumber != NULL) ASN1_INTEGER_free(cid->serialNumber);
123         if (!(cid->serialNumber = ASN1_INTEGER_dup(serialNumber))) goto err;
124         return cid;
125 digerr:
126         OCSPerr(OCSP_F_CERT_ID_NEW,OCSP_R_DIGEST_ERR);
127 err:
128         if (cid) OCSP_CERTID_free(cid);
129         return NULL;
130         }
131
132 OCSP_CERTSTATUS *OCSP_cert_status_new(int status, int reason, char *tim)
133         {
134         OCSP_REVOKEDINFO *ri;
135         OCSP_CERTSTATUS *cs = NULL;
136
137         if (!(cs = OCSP_CERTSTATUS_new())) goto err;
138         if ((cs->type = status) == V_OCSP_CERTSTATUS_REVOKED)
139                 {
140                 if (!time)
141                         {
142                         OCSPerr(OCSP_F_CERT_STATUS_NEW,OCSP_R_REVOKED_NO_TIME);
143                         goto err;
144                         }
145                 if (!(cs->value.revoked = ri = OCSP_REVOKEDINFO_new())) goto err;
146                 if (!ASN1_GENERALIZEDTIME_set_string(ri->revocationTime,tim))
147                         goto err;       
148                 if (reason != OCSP_REVOKED_STATUS_NOSTATUS)
149                         {
150                         if (!(ri->revocationReason = ASN1_ENUMERATED_new())) 
151                                 goto err;
152                         if (!(ASN1_ENUMERATED_set(ri->revocationReason, 
153                                                   reason)))
154                                 goto err;       
155                         }
156                 }
157         return cs;
158 err:
159         if (cs) OCSP_CERTSTATUS_free(cs);
160         return NULL;
161         }
162
163
164 int OCSP_id_issuer_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
165         {
166         int ret;
167         ret = OBJ_cmp(a->hashAlgorithm->algorithm, b->hashAlgorithm->algorithm);
168         if (ret) return ret;
169         ret = ASN1_OCTET_STRING_cmp(a->issuerNameHash, b->issuerNameHash);
170         if (ret) return ret;
171         return ASN1_OCTET_STRING_cmp(a->issuerKeyHash, b->issuerKeyHash);
172         }
173
174 int OCSP_id_cmp(OCSP_CERTID *a, OCSP_CERTID *b)
175         {
176         int ret;
177         ret = OCSP_id_issuer_cmp(a, b);
178         if (ret) return ret;
179         return ASN1_INTEGER_cmp(a->serialNumber, b->serialNumber);
180         }
181
182 OCSP_BASICRESP *OCSP_basic_response_new(int type, X509* cert)
183         {
184         time_t t;
185         OCSP_RESPID *rid;
186         OCSP_BASICRESP *rsp = NULL;
187         unsigned char md[SHA_DIGEST_LENGTH];
188         
189         if (!(rsp = OCSP_BASICRESP_new())) goto err;
190         rid = rsp->tbsResponseData->responderId;
191         switch (rid->type = type)
192                 {
193                 case V_OCSP_RESPID_NAME:
194                         /* cert is user cert */
195                         if (!(rid->value.byName =
196                                   X509_NAME_dup(X509_get_subject_name(cert))))
197                                 goto err;
198                         break;
199                 case V_OCSP_RESPID_KEY:
200                         /* cert is issuer cert */
201                         /* SHA-1 hash of responder's public key
202                          * (excluding the tag and length fields)
203                          */
204                         X509_pubkey_digest(cert, EVP_sha1(), md, NULL);
205                         if (!(rid->value.byKey = ASN1_OCTET_STRING_new()))
206                                 goto err;
207                         if (!(ASN1_OCTET_STRING_set(rid->value.byKey,
208                                                     md, sizeof md)))
209                                 goto err;
210                         break;
211                 default:
212                         OCSPerr(OCSP_F_BASIC_RESPONSE_NEW,OCSP_R_BAD_TAG);
213                         goto err;
214                         break;
215                 }
216         time(&t);
217         if (!(ASN1_GENERALIZEDTIME_set(rsp->tbsResponseData->producedAt, t)))
218                 goto err;
219         if (!(rsp->tbsResponseData->responses = sk_OCSP_SINGLERESP_new(NULL))) goto err;
220         return rsp;
221 err:
222         if (rsp) OCSP_BASICRESP_free(rsp);
223         return NULL;
224         }
225
226 int OCSP_basic_response_add(OCSP_BASICRESP           *rsp,
227                             OCSP_CERTID              *cid,
228                             OCSP_CERTSTATUS          *cst,
229                             char                     *this,
230                             char                     *next)
231         {
232         OCSP_SINGLERESP *single = NULL;
233
234         if (!(single = OCSP_SINGLERESP_new())) goto err;
235         if (single->certId) OCSP_CERTID_free(single->certId);
236         if (!(single->certId = OCSP_CERTID_dup(cid))) goto err;
237         if (single->certStatus) OCSP_CERTSTATUS_free(single->certStatus);
238         if (!(single->certStatus = OCSP_CERTSTATUS_dup(cst))) goto err;
239         if (!ASN1_GENERALIZEDTIME_set_string(single->thisUpdate,this))goto err;
240         if (next)
241                 { 
242                 if (!(single->nextUpdate = ASN1_GENERALIZEDTIME_new()))
243                         goto err;
244                 if (!ASN1_GENERALIZEDTIME_set_string(single->nextUpdate,next))
245                         goto err;
246                 }
247         if (!sk_OCSP_SINGLERESP_push(rsp->tbsResponseData->responses,single)) goto err;
248         return 1;
249 err:
250         if (single) OCSP_SINGLERESP_free(single);
251         return 0;
252         }
253
254 int OCSP_basic_response_sign(OCSP_BASICRESP *brsp, 
255                              EVP_PKEY       *key,
256                              const EVP_MD   *dgst,
257                              STACK_OF(X509) *certs)
258         {
259         int i;
260
261         /* Right now, I think that not doing double hashing is the right
262            thing.       -- Richard Levitte */
263         if (!OCSP_BASICRESP_sign(brsp, key, dgst, 0)) goto err;
264         if (certs)
265                 {
266                 if (!(brsp->certs = sk_X509_dup(certs))) goto err;
267                 for (i = 0; i < sk_X509_num(brsp->certs); i++)
268                         {
269                         sk_X509_set(brsp->certs, i,
270                                X509_dup(sk_X509_value(certs, i)));
271                         if (! sk_X509_value(brsp->certs, i))
272                                 goto err;
273                         }
274                 }
275         return 1;
276 err:
277         return 0;
278         }
279
280 OCSP_RESPONSE *OCSP_response_new(int status,
281                                  int nid,
282                                  int (*i2d)(),
283                                  char *data)
284         {
285         OCSP_RESPONSE *rsp = NULL;
286
287         if (!(rsp = OCSP_RESPONSE_new())) goto err;
288         if (!(ASN1_ENUMERATED_set(rsp->responseStatus, status))) goto err;
289         if (!(rsp->responseBytes = OCSP_RESPBYTES_new())) goto err;
290         if (rsp->responseBytes->responseType) ASN1_OBJECT_free(rsp->responseBytes->responseType);
291         if (!(rsp->responseBytes->responseType = OBJ_nid2obj(nid))) goto err;
292         if (!ASN1_STRING_encode((ASN1_STRING*)rsp->responseBytes->response,
293                                 i2d, data, NULL)) goto err;
294         return rsp;
295 err:
296         if (rsp) OCSP_RESPONSE_free(rsp);
297         return NULL;
298         }
299
300 /* XXX assumes certs in signature are sorted root to leaf XXX */
301 int OCSP_request_verify(OCSP_REQUEST *req, EVP_PKEY *pkey)
302         {
303         STACK_OF(X509) *sk;
304
305         if (!req->optionalSignature) return 0;
306         if (pkey == NULL)
307                 {
308                 if (!(sk = req->optionalSignature->certs)) return 0;
309                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
310                         {
311                         OCSPerr(OCSP_F_REQUEST_VERIFY,OCSP_R_NO_PUBLIC_KEY);
312                         return 0;
313                         }
314                 }
315         return OCSP_REQUEST_verify(req, pkey);
316         }
317
318 int OCSP_response_verify(OCSP_RESPONSE *rsp, EVP_PKEY *pkey)
319         {
320         int i, r;
321         unsigned char *p;
322         OCSP_RESPBYTES *rb;
323         OCSP_BASICRESP *br = NULL;
324
325         if ((rb = rsp->responseBytes) == NULL) 
326                 {
327                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_NO_RESPONSE_DATA);
328                 return 0;
329                 }
330         if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) 
331                 {
332                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_BAD_TAG);
333                 return 0;
334                 }
335         p = ASN1_STRING_data(rb->response);
336         i = ASN1_STRING_length(rb->response);
337         if (!(d2i_OCSP_BASICRESP(&br, &p, i))) return 0;
338         r = OCSP_basic_response_verify(br, pkey);
339         OCSP_BASICRESP_free(br);
340         return r;
341         }
342
343 int OCSP_basic_response_verify(OCSP_BASICRESP *rsp, EVP_PKEY *pkey)
344         {
345         STACK_OF(X509) *sk;
346         int ret;
347
348         if (!rsp->signature) 
349                 {
350                 OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_SIGNATURE);
351                 return 0;
352                 }
353         if (pkey == NULL)
354                 {
355                 if (!(sk = rsp->certs))
356                         {
357                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_CERTIFICATE);
358                         return 0;
359                         }
360                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
361                         {
362                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_PUBLIC_KEY);
363                         return 0;
364                         }
365                 }
366         ret = OCSP_BASICRESP_verify(rsp, pkey, 0);
367         return ret;
368         }