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