986ab8ebb5c94016da46c9b67f3889bcbe8a64f1
[openssl.git] / crypto / ocsp / ocsp_res.c
1 /* ocsp_req.c */
2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
3  * project. */
4
5 /* History:
6    This file was originally part of ocsp.c and was transfered to Richard
7    Levitte from CertCo by Kathy Weinhold in mid-spring 2000 to be included
8    in OpenSSL or released 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 <openssl/err.h>
65 #include <openssl/objects.h>
66 #include <openssl/asn1.h>
67 #include <openssl/ocsp.h>
68 #include <openssl/x509.h>
69 #include <openssl/x509v3.h>
70
71 int i2a_OCSP_RESPBYTES(BIO *bp,
72                        OCSP_RESPBYTES* a)
73         {
74         i2a_ASN1_OBJECT(bp, a->responseType);
75         i2a_ASN1_STRING(bp, a->response, V_ASN1_OCTET_STRING);
76         return 2;
77         }
78
79 int i2a_OCSP_RESPONSE(BIO *bp, OCSP_RESPONSE* a)
80         {
81         i2a_ASN1_STRING(bp, a->responseStatus, V_ASN1_ENUMERATED);
82         i2a_OCSP_RESPBYTES(bp, a->responseBytes);
83         return a->responseBytes ? 2 : 1;
84         }
85
86 int i2a_OCSP_RESPID(BIO *bp, OCSP_RESPID* a)
87         {
88         switch (a->type)
89                 {
90                 case V_OCSP_RESPID_NAME:
91                         X509_NAME_print(bp, a->value.byName, 16);
92                         break;
93                 case V_OCSP_RESPID_KEY:
94                         i2a_ASN1_STRING(bp, a->value.byKey, V_ASN1_OCTET_STRING);
95                         break;
96                 }
97
98         return 1;
99         }
100
101 int i2a_OCSP_RESPDATA(BIO *bp, OCSP_RESPDATA* a)
102         {
103         int i, j=2;
104         if (a->version == NULL) BIO_puts(bp, "0");
105         else i2a_ASN1_INTEGER(bp, a->version);
106         i2a_OCSP_RESPID(bp, a->responderId);
107         if (!ASN1_GENERALIZEDTIME_print(bp, a->producedAt)) return 0;
108         if (a->responses != NULL)
109                 {
110                 for (i=0; i<sk_OCSP_SINGLERESP_num(a->responses); i++)
111                         if (sk_OCSP_SINGLERESP_value(a->responses,i) != NULL)
112                                 i2a_OCSP_SINGLERESP(bp, 
113                                       sk_OCSP_SINGLERESP_value(a->responses,i));
114                 j+=sk_OCSP_SINGLERESP_num(a->responses);
115                 }
116 #ifdef UNDEF
117         /* XXX need generic extension print method or need to register
118          * ocsp extensions with existing extension handler mechanism,
119          * invoke i2a callbacks.
120          */
121         if (a->responseExtensions != NULL)
122                 {
123                 for (i=0; i<sk_X509_EXTENSION_num(a->responseExtensions); i++)
124                         if (sk_X509_EXTENSION_value(a->responseExtensions,i) != NULL)
125                                 i2a_X509_EXTENSION(bp, 
126                                    sk_X509_EXTENSION_value(a->responseExtensions,i));
127                 j+=sk_X509_EXTENSION_num(a->responseExtensions);
128                 }
129 #endif
130         return j;
131         }
132
133 int i2a_OCSP_BASICRESP(BIO *bp, OCSP_BASICRESP* a)
134         {
135         int i, j=3;
136         i2a_OCSP_RESPDATA(bp, a->tbsResponseData);
137 #ifdef UNDEF
138         /* XXX this guy isn't implemented. */
139         i2a_X509_ALGOR(bp, a->signatureAlgorithm);
140 #else   /* instead, just show OID, not param */
141         i2a_ASN1_OBJECT(bp, a->signatureAlgorithm->algorithm);
142 #endif
143         i2a_ASN1_STRING(bp, a->signature, V_ASN1_BIT_STRING);
144         if (a->certs != NULL)
145                 {
146                 for (i=0; i<sk_X509_num(a->certs); i++)
147                         if (sk_X509_value(a->certs,i) != NULL)
148                                 X509_print(bp, sk_X509_value(a->certs,i));
149                 j+=sk_X509_num(a->certs);
150                 }
151         return j;
152         }
153
154 int i2a_OCSP_REVOKEDINFO(BIO *bp, OCSP_REVOKEDINFO* a)
155         {
156         int i=0; 
157         if (!ASN1_GENERALIZEDTIME_print(bp, a->revocationTime)) return 0;
158         if (a->revocationReason)
159                 {
160                 i2a_ASN1_STRING(bp, a->revocationReason, V_ASN1_ENUMERATED);
161                 i++;
162                 }
163         return i;
164         }
165
166 int i2a_OCSP_CERTSTATUS(BIO *bp, OCSP_CERTSTATUS* a)
167         {
168         switch (a->type)
169                 {
170                 case V_OCSP_CERTSTATUS_GOOD:
171                         BIO_puts(bp, "CertStatus: good");
172                         break;
173                 case V_OCSP_CERTSTATUS_REVOKED:
174                         BIO_puts(bp, "CertStatus: revoked");
175                         i2a_OCSP_REVOKEDINFO(bp, a->value.revoked);
176                         break;
177                 case V_OCSP_CERTSTATUS_UNKNOWN:
178                         BIO_puts(bp, "CertStatus: unknown");
179                         break;
180                 }
181         return 1;
182         }
183
184 int i2a_OCSP_SINGLERESP(BIO *bp, OCSP_SINGLERESP* a)
185         {
186         int /* XXX i, */ j=3;
187         i2a_OCSP_CERTID(bp, a->certId);
188         i2a_OCSP_CERTSTATUS(bp, a->certStatus);
189         if (!ASN1_GENERALIZEDTIME_print(bp, a->thisUpdate)) return 0;
190         if (a->nextUpdate) 
191                 {
192                 if (!ASN1_GENERALIZEDTIME_print(bp, a->nextUpdate)) return 0;
193                 j++;
194                 }
195 #ifdef UNDEF
196         /* XXX need generic extension print method or need to register
197          * ocsp extensions with existing extension handler mechanism,
198          * invoke i2a callbacks.
199          */
200         if (a->singleExtensions != NULL)
201                 {
202                 for (i=0; i<sk_X509_EXTENSION_num(a->singleExtensions); i++)
203                         if (sk_X509_EXTENSION_value(a->singleExtensions,i) != NULL)
204                                 i2a_X509_EXTENSION(bp, 
205                                   sk_X509_EXTENSION_value(a->singleExtensions,i));
206                 j+=sk_X509_EXTENSION_num(a->singleExtensions);
207                 }
208 #endif
209         return j;
210         }
211
212 int i2a_OCSP_CRLID(BIO *bp, OCSP_CRLID* a)
213         {
214         int i = 0;
215         char buf[1024];
216         if (a->crlUrl && ASN1_STRING_print(bp, (ASN1_STRING*)a->crlUrl)) i++;
217         if (a->crlNum && a2i_ASN1_INTEGER(bp, a->crlNum, buf, sizeof buf)) i++;
218         if (a->crlTime && ASN1_GENERALIZEDTIME_print(bp, a->crlTime)) i++;
219         return i;
220         }
221
222 int i2a_OCSP_SERVICELOC(BIO *bp,
223                         OCSP_SERVICELOC* a)
224         {
225         int i;
226         X509_NAME_print(bp, a->issuer, 16);
227         if (!a->locator) return 1;
228         for (i=0; i<sk_ACCESS_DESCRIPTION_num(a->locator); i++)
229                 i2a_ACCESS_DESCRIPTION(bp,
230                              sk_ACCESS_DESCRIPTION_value(a->locator,i));
231         return i+2;
232         }