Replace the old style OCSP ASN1 module.
[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 char* ocspResponseStatus2string(long s)
371         {
372         static struct { long t; char *m; } ts[6]= { 
373                 { OCSP_RESPONSE_STATUS_SUCCESSFULL, "successful" },
374                 { OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" },
375                 { OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" },
376                 { OCSP_RESPONSE_STATUS_TRYLATER, "trylater" },
377                 { OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired" },
378                 { OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized" } }, *p;
379         for (p=ts; p < &ts[sizeof ts/sizeof ts[0]]; p++)
380                 if (p->t == s)
381                          return p->m;
382         return "(UNKNOWN)";
383         } 
384
385 char* ocspCertStatus2string(long s)
386         {
387         static struct { long t; char *m; } ts[3]= { 
388                 { V_OCSP_CERTSTATUS_GOOD, "good" },
389                 { V_OCSP_CERTSTATUS_REVOKED, "revoked" },
390                 { V_OCSP_CERTSTATUS_UNKNOWN, "unknown" } }, *p;
391         for (p=ts; p < &ts[sizeof ts/sizeof ts[0]]; p++)
392                 if (p->t == s)
393                          return p->m;
394         return "(UNKNOWN)";
395         } 
396
397 char * cRLReason2string(long s)
398         {
399         static struct { long t; char *m; } ts[8]= { 
400           { OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified" },
401           { OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise" },
402           { OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise" },
403           { OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged" },
404           { OCSP_REVOKED_STATUS_SUPERSEDED, "superseded" },
405           { OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation" },
406           { OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold" },
407           { OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL" } }, *p;
408         for (p=ts; p < &ts[sizeof ts/sizeof ts[0]]; p++)
409                 if (p->t == s)
410                          return p->m;
411         return "(UNKNOWN)";
412         } 
413
414 static int i2a_GENERAL_NAME(bp,n)
415 BIO *bp; 
416 GENERAL_NAME *n;
417         {
418         int j;
419         char *p;
420
421         if (n == NULL) return(0);
422
423         switch (n->type)
424                 {
425
426         case GEN_DIRNAME:
427                 X509_NAME_print(bp,n->d.dirn,16);
428                 break;
429
430         case GEN_EMAIL:
431         case GEN_DNS:
432         case GEN_URI:
433         case GEN_IPADD:
434                 p=(char *)n->d.ip->data;
435                 for (j=n->d.ip->length;j>0;j--)
436                         {
437                         if ((*p >= ' ') && (*p <= '~'))
438                                 BIO_printf(bp,"%c",*p);
439                         else if (*p & 0x80)
440                                 BIO_printf(bp,"\\0x%02X",*p);
441                         else if ((unsigned char)*p == 0xf7)
442                                 BIO_printf(bp,"^?");
443                         else    BIO_printf(bp,"^%c",*p+'@');
444                         p++;
445                         }
446                 break;
447
448         case GEN_RID:
449                 i2a_ASN1_OBJECT(bp, n->d.rid);
450                 break;
451
452         /* XXX these are legit, need to support at some time... */
453         case GEN_OTHERNAME:
454         case GEN_X400:
455         case GEN_EDIPARTY:
456         default:
457                 return 0;
458                 }
459
460         return 1;
461         }
462
463
464 int OCSP_REQUEST_print(bp, o)
465 BIO *bp; 
466 OCSP_REQUEST* o;
467         {
468         int i,j,n;
469         long l;
470         char *s;
471         OCSP_CERTID* cid = NULL;
472         OCSP_ONEREQ *one = NULL;
473         OCSP_REQINFO *inf = o->tbsRequest;
474         OCSP_SIGNATURE *sig = o->optionalSignature;
475
476         if (BIO_write(bp,"OCSP Request Data:\n",19) <= 0) goto err;
477         l=ASN1_INTEGER_get(inf->version);
478         if (BIO_printf(bp,"%4sVersion: %lu (0x%lx)","",l+1,l) <= 0) goto err;
479         if (inf->requestorName != NULL)
480                 {
481                 if (BIO_write(bp,"\n    Requestor Name: ",21) <= 0) 
482                         goto err;
483                 i2a_GENERAL_NAME(bp, inf->requestorName);
484                 }
485         if (BIO_write(bp,"\n    Requestor List:\n",21) <= 0) goto err;
486         for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++)
487                 {
488                 if (! sk_OCSP_ONEREQ_value(inf->requestList, i)) continue;
489                 one = sk_OCSP_ONEREQ_value(inf->requestList, i);
490                 cid = one->reqCert;
491                 j=OBJ_obj2nid(cid->hashAlgorithm->algorithm);
492                 if (BIO_printf(bp,"%8sHash Algorithm: %s","",
493                                (j == NID_undef)?"UNKNOWN":OBJ_nid2ln(j)) <= 0)
494                         goto err;
495                 if (BIO_write(bp,"\n        Issuer Name Hash: ",27) <= 0)
496                         goto err;
497                 i2a_ASN1_STRING(bp, cid->issuerNameHash, V_ASN1_OCTET_STRING);
498                 if (BIO_write(bp,"\n        Issuer Key Hash: ",26) <= 0) 
499                         goto err;
500                 i2a_ASN1_STRING(bp, cid->issuerKeyHash, V_ASN1_OCTET_STRING);
501                 if (BIO_write(bp,"\n        Serial Number: ",24) <= 0) 
502                         goto err;
503                 if (!i2a_ASN1_INTEGER(bp, cid->serialNumber)) 
504                         goto err;
505                 if (!BIO_write(bp,"\n",1)) goto err;
506                 if (!OCSP_extensions_print(bp, one->singleRequestExtensions,
507                                            "Request Single Extensions"))
508                         goto err;
509                 }
510         if (!OCSP_extensions_print(bp, inf->requestExtensions, 
511                                    "Request Extensions"))
512                 goto err;
513         if (sig)
514                 {
515                 i=OBJ_obj2nid(sig->signatureAlgorithm->algorithm);
516                 if (BIO_printf(bp,"OCSP Request Signature Algorithm: %s",
517                                (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0)
518                         goto err;
519                 n=sig->signature->length;
520                 s=(char *)sig->signature->data;
521                 for (i=0; i<n; i++)
522                         {
523                         if ((i%18) == 0)
524                         if (BIO_write(bp,"\n        ",9) <= 0) goto err;
525                         if (BIO_printf(bp,"%02x%s",(unsigned char)s[i],
526                                 ((i+1) == n)?"":":") <= 0) goto err;
527                         }
528                 if (BIO_write(bp,"\n",1) != 1) goto err;
529                 if (sig->certs)
530                         {
531                         for (i=0; i<sk_X509_num(sig->certs); i++)
532                                 if (sk_X509_value(sig->certs,i) != NULL)
533                                         X509_print(bp,
534                                             sk_X509_value(sig->certs,i));
535                         }
536                 }
537         return 1;
538 err:
539         return 0;
540         }
541
542 int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o)
543         {
544         int i, j, n;
545         long l;
546         char *s;
547         unsigned char *p;
548         OCSP_CERTID *cid = NULL;
549         OCSP_BASICRESP *br = NULL;
550         OCSP_RESPDATA  *rd = NULL;
551         OCSP_CERTSTATUS *cst = NULL;
552         OCSP_REVOKEDINFO *rev = NULL;
553         OCSP_SINGLERESP *single = NULL;
554         OCSP_RESPBYTES *rb = o->responseBytes;
555
556         l=ASN1_ENUMERATED_get(o->responseStatus);
557         if (BIO_printf(bp,"OCSP Response Status: %s (0x%x)\n", 
558                        ocspResponseStatus2string(l), l) <= 0) goto err;
559         if (rb == NULL) return 1;
560         i=OBJ_obj2nid(rb->responseType);
561         if (BIO_printf(bp,"OCSP Response Bytes Response Type: %s",
562                        (i == NID_undef)?"UNKNOWN":OBJ_nid2sn(i)) <= 0)
563                 goto err;
564         if (i != NID_id_pkix_OCSP_basic) 
565                 {
566                 BIO_printf(bp," (unknown response type)\n");
567                 return 1;
568                 }
569         p = ASN1_STRING_data(rb->response);
570         i = ASN1_STRING_length(rb->response);
571         if (!(d2i_OCSP_BASICRESP(&br, &p, i))) goto err;
572         rd = br->tbsResponseData;
573         l=ASN1_INTEGER_get(rd->version);
574         if (BIO_printf(bp,"\nBasic Response Data Version: %lu (0x%lx)\n",
575                        l+1,l) <= 0) goto err;
576         if (BIO_printf(bp,"Basic Response Data Responder Id: ") <= 0) goto err;
577         i2a_OCSP_RESPID(bp, rd->responderId);
578         if (BIO_printf(bp,"\nBasic Response Data Produced At: ")<=0) goto err;
579         if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt)) goto err;
580         if (BIO_printf(bp,"\nBasic Response Data Responses:\n") <= 0) goto err;
581         for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++)
582                 {
583                 if (! sk_OCSP_SINGLERESP_value(rd->responses, i)) continue;
584                 single = sk_OCSP_SINGLERESP_value(rd->responses, i);
585                 cid = single->certId;
586                 j=OBJ_obj2nid(cid->hashAlgorithm->algorithm);
587                 if (BIO_printf(bp,"    Cert Id:") <= 0) goto err;
588                 if (BIO_printf(bp,"\n%8sHash Algorithm: %s","",
589                                (j == NID_undef)?"UNKNOWN":OBJ_nid2ln(j)) <= 0)
590                         goto err;
591                 if (BIO_write(bp,"\n        Issuer Name Hash: ",27) <= 0)
592                         goto err;
593                 i2a_ASN1_STRING(bp, cid->issuerNameHash, V_ASN1_OCTET_STRING);
594                 if (BIO_write(bp,"\n        Issuer Key Hash: ",26) <= 0) 
595                         goto err;
596                 i2a_ASN1_STRING(bp, cid->issuerKeyHash, V_ASN1_OCTET_STRING);
597                 if (BIO_write(bp,"\n        Serial Number: ",24) <= 0) 
598                         goto err;
599                 if (!i2a_ASN1_INTEGER(bp, cid->serialNumber)) 
600                         goto err;
601                 cst = single->certStatus;
602                 if (BIO_printf(bp,"\n    Cert Status: %s (0x%x)",
603                                ocspCertStatus2string(cst->type), cst->type) <= 0)
604                         goto err;
605                 if (cst->type == V_OCSP_CERTSTATUS_REVOKED)
606                         {
607                         rev = cst->value.revoked;
608                         if (BIO_printf(bp, "\n    Revocation Time: ") <= 0) 
609                                 goto err;
610                         if (!ASN1_GENERALIZEDTIME_print(bp, 
611                                                         rev->revocationTime)) 
612                                 goto err;
613                         if (rev->revocationReason) 
614                                 {
615                                 l=ASN1_ENUMERATED_get(rev->revocationReason);
616                                 if (BIO_printf(bp, 
617                                          "\n    Revocation Reason: %s (0x%x)",
618                                                cRLReason2string(l), l) <= 0)
619                                         goto err;
620                                 }
621                         }
622                 if (BIO_printf(bp,"\n    This Update: ") <= 0) goto err;
623                 if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate)) 
624                         goto err;
625                 if (single->nextUpdate)
626                         {
627                         if (BIO_printf(bp,"\n    Next Update: ") <= 0)goto err;
628                         if (!ASN1_GENERALIZEDTIME_print(bp,single->nextUpdate))
629                                 goto err;
630                         }
631                 if (!BIO_write(bp,"\n",1)) goto err;
632                 if (!OCSP_extensions_print(bp, single->singleExtensions,
633                                            "Basic Response Single Extensions"))
634                         goto err;
635                 }
636         if (!OCSP_extensions_print(bp, rd->responseExtensions,
637                                    "Basic Response Extensions")) goto err;
638         i=OBJ_obj2nid(br->signatureAlgorithm->algorithm);
639         if (BIO_printf(bp,"Basic Response Signature Algorithm: %s",
640                        (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0)
641                 goto err;
642         n=br->signature->length;
643         s=(char *)br->signature->data;
644         for (i=0; i<n; i++)
645                 {
646                 if ((i%18) == 0)
647                 if (BIO_write(bp,"\n        ",9) <= 0) goto err;
648                 if (BIO_printf(bp,"%02x%s",(unsigned char)s[i],
649                         ((i+1) == n)?"":":") <= 0) goto err;
650                 }
651         if (BIO_write(bp,"\n",1) != 1) goto err;
652         if (br->certs)
653                 {
654                 for (i=0; i<sk_X509_num(br->certs); i++)
655                         if (sk_X509_value(br->certs,i) != NULL) {
656                                 X509_print(bp, sk_X509_value(br->certs,i));
657                                 PEM_write_bio_X509(bp,sk_X509_value(br->certs,i));
658                         }
659                 }
660         return 1;
661 err:
662         return 0;
663         }
664
665 int OCSP_CRLID_print(BIO *bp, OCSP_CRLID *a, int ind)
666         {
667         if (a->crlUrl)
668                 {
669                 if (!BIO_printf(bp, "%*scrlUrl: ", ind, "")) goto err;
670                 if (!ASN1_STRING_print(bp, (ASN1_STRING*)a->crlUrl)) goto err;
671                 if (!BIO_write(bp, "\n", 1)) goto err;
672                 }
673         if (a->crlNum)
674                 {
675                 if (!BIO_printf(bp, "%*scrlNum: ", ind, "")) goto err;
676                 if (!i2a_ASN1_INTEGER(bp, a->crlNum)) goto err;
677                 if (!BIO_write(bp, "\n", 1)) goto err;
678                 }
679         if (a->crlTime)
680                 {
681                 if (!BIO_printf(bp, "%*scrlTime: ", ind, "")) goto err;
682                 if (!ASN1_GENERALIZEDTIME_print(bp, a->crlTime)) goto err;
683                 if (!BIO_write(bp, "\n", 1)) goto err;
684                 }
685         return 1;
686 err:
687         return 0;
688         }
689
690 int OCSP_SERVICELOC_print(BIO *bp, OCSP_SERVICELOC* a, int ind)
691         {
692         int i, j;
693         ACCESS_DESCRIPTION *ad;
694
695         if (BIO_printf(bp, "%*sissuer: ", ind, "") <= 0) goto err;
696         if (X509_NAME_print(bp, a->issuer, 16) <= 0) goto err;
697         if (BIO_printf(bp, "\n", 1) <= 0) goto err;
698
699                 /* Service locator is optional */
700                 if (a->locator != NULL) {
701                         if (BIO_printf(bp, "%*slocator:\n", ind, "") <= 0) goto err;
702                         for (i = 0; i < sk_ACCESS_DESCRIPTION_num(a->locator); i++)
703                 {
704                                 ad = sk_ACCESS_DESCRIPTION_value(a->locator,i);
705                                 if (BIO_printf(bp, "%*smethod: ", (2*ind), "") <= 0) 
706                                         goto err;
707                                 j=OBJ_obj2nid(ad->method);
708                                 if (BIO_printf(bp,"%s", (j == NID_undef)?"UNKNOWN":
709                                                            OBJ_nid2ln(j)) <= 0)
710                                         goto err;
711                                 if (BIO_printf(bp, "\n%*sname: ", (2*ind), "") <= 0) 
712                                         goto err;
713                                 if (i2a_GENERAL_NAME(bp, ad->location) <= 0) goto err;
714                                 if (BIO_write(bp, "\n", 1) <= 0) goto err;
715                         }
716                 }
717         return 1;
718 err:
719         return 0;
720         }
721
722 /* XXX assumes certs in signature are sorted root to leaf XXX */
723 int OCSP_request_verify(OCSP_REQUEST *req, EVP_PKEY *pkey)
724         {
725         STACK_OF(X509) *sk;
726
727         if (!req->optionalSignature) return 0;
728         if (pkey == NULL)
729                 {
730                 if (!(sk = req->optionalSignature->certs)) return 0;
731                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
732                         {
733                         OCSPerr(OCSP_F_REQUEST_VERIFY,OCSP_R_NO_PUBLIC_KEY);
734                         return 0;
735                         }
736                 }
737         return OCSP_REQUEST_verify(req, pkey);
738         }
739
740 int OCSP_response_verify(OCSP_RESPONSE *rsp, EVP_PKEY *pkey)
741         {
742         int i, r;
743         unsigned char *p;
744         OCSP_RESPBYTES *rb;
745         OCSP_BASICRESP *br = NULL;
746
747         if ((rb = rsp->responseBytes) == NULL) 
748                 {
749                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_NO_RESPONSE_DATA);
750                 return 0;
751                 }
752         if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) 
753                 {
754                 OCSPerr(OCSP_F_RESPONSE_VERIFY,OCSP_R_BAD_TAG);
755                 return 0;
756                 }
757         p = ASN1_STRING_data(rb->response);
758         i = ASN1_STRING_length(rb->response);
759         if (!(d2i_OCSP_BASICRESP(&br, &p, i))) return 0;
760         r = OCSP_basic_response_verify(br, pkey);
761         OCSP_BASICRESP_free(br);
762         return r;
763         }
764
765 int OCSP_basic_response_verify(OCSP_BASICRESP *rsp, EVP_PKEY *pkey)
766         {
767         STACK_OF(X509) *sk;
768         int ret;
769
770         if (!rsp->signature) 
771                 {
772                 OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_SIGNATURE);
773                 return 0;
774                 }
775         if (pkey == NULL)
776                 {
777                 if (!(sk = rsp->certs))
778                         {
779                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_CERTIFICATE);
780                         return 0;
781                         }
782                 if (!(pkey=X509_get_pubkey(sk_X509_value(sk, sk_X509_num(sk)-1))))
783                         {
784                         OCSPerr(OCSP_F_BASIC_RESPONSE_VERIFY,OCSP_R_NO_PUBLIC_KEY);
785                         return 0;
786                         }
787                 }
788         ret = OCSP_BASICRESP_verify(rsp, pkey, 0);
789         return ret;
790         }