In OpenSSL builds, declare STACK for datatypes ...
[openssl.git] / crypto / ocsp / v3_ocsp.c
1 /*
2  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 # include <stdio.h>
11 # include "internal/cryptlib.h"
12 # include <openssl/conf.h>
13 # include <openssl/asn1.h>
14 # include <openssl/ocsp.h>
15 # include "ocsp_local.h"
16 # include <openssl/x509v3.h>
17 # include "../x509/ext_dat.h"
18
19 DEFINE_STACK_OF(ACCESS_DESCRIPTION)
20
21 /*
22  * OCSP extensions and a couple of CRL entry extensions
23  */
24
25 static int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *nonce,
26                           BIO *out, int indent);
27 static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *nonce,
28                             BIO *out, int indent);
29 static int i2r_object(const X509V3_EXT_METHOD *method, void *obj, BIO *out,
30                       int indent);
31
32 static void *ocsp_nonce_new(void);
33 static int i2d_ocsp_nonce(const void *a, unsigned char **pp);
34 static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length);
35 static void ocsp_nonce_free(void *a);
36 static int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce,
37                           BIO *out, int indent);
38
39 static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method,
40                             void *nocheck, BIO *out, int indent);
41 static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method,
42                               X509V3_CTX *ctx, const char *str);
43 static int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in,
44                                BIO *bp, int ind);
45
46 const X509V3_EXT_METHOD v3_ocsp_crlid = {
47     NID_id_pkix_OCSP_CrlID, 0, ASN1_ITEM_ref(OCSP_CRLID),
48     0, 0, 0, 0,
49     0, 0,
50     0, 0,
51     i2r_ocsp_crlid, 0,
52     NULL
53 };
54
55 const X509V3_EXT_METHOD v3_ocsp_acutoff = {
56     NID_id_pkix_OCSP_archiveCutoff, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
57     0, 0, 0, 0,
58     0, 0,
59     0, 0,
60     i2r_ocsp_acutoff, 0,
61     NULL
62 };
63
64 const X509V3_EXT_METHOD v3_crl_invdate = {
65     NID_invalidity_date, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
66     0, 0, 0, 0,
67     0, 0,
68     0, 0,
69     i2r_ocsp_acutoff, 0,
70     NULL
71 };
72
73 const X509V3_EXT_METHOD v3_crl_hold = {
74     NID_hold_instruction_code, 0, ASN1_ITEM_ref(ASN1_OBJECT),
75     0, 0, 0, 0,
76     0, 0,
77     0, 0,
78     i2r_object, 0,
79     NULL
80 };
81
82 const X509V3_EXT_METHOD v3_ocsp_nonce = {
83     NID_id_pkix_OCSP_Nonce, 0, NULL,
84     ocsp_nonce_new,
85     ocsp_nonce_free,
86     d2i_ocsp_nonce,
87     i2d_ocsp_nonce,
88     0, 0,
89     0, 0,
90     i2r_ocsp_nonce, 0,
91     NULL
92 };
93
94 const X509V3_EXT_METHOD v3_ocsp_nocheck = {
95     NID_id_pkix_OCSP_noCheck, 0, ASN1_ITEM_ref(ASN1_NULL),
96     0, 0, 0, 0,
97     0, s2i_ocsp_nocheck,
98     0, 0,
99     i2r_ocsp_nocheck, 0,
100     NULL
101 };
102
103 const X509V3_EXT_METHOD v3_ocsp_serviceloc = {
104     NID_id_pkix_OCSP_serviceLocator, 0, ASN1_ITEM_ref(OCSP_SERVICELOC),
105     0, 0, 0, 0,
106     0, 0,
107     0, 0,
108     i2r_ocsp_serviceloc, 0,
109     NULL
110 };
111
112 static int i2r_ocsp_crlid(const X509V3_EXT_METHOD *method, void *in, BIO *bp,
113                           int ind)
114 {
115     OCSP_CRLID *a = in;
116     if (a->crlUrl) {
117         if (BIO_printf(bp, "%*scrlUrl: ", ind, "") <= 0)
118             goto err;
119         if (!ASN1_STRING_print(bp, (ASN1_STRING *)a->crlUrl))
120             goto err;
121         if (BIO_write(bp, "\n", 1) <= 0)
122             goto err;
123     }
124     if (a->crlNum) {
125         if (BIO_printf(bp, "%*scrlNum: ", ind, "") <= 0)
126             goto err;
127         if (i2a_ASN1_INTEGER(bp, a->crlNum) <= 0)
128             goto err;
129         if (BIO_write(bp, "\n", 1) <= 0)
130             goto err;
131     }
132     if (a->crlTime) {
133         if (BIO_printf(bp, "%*scrlTime: ", ind, "") <= 0)
134             goto err;
135         if (!ASN1_GENERALIZEDTIME_print(bp, a->crlTime))
136             goto err;
137         if (BIO_write(bp, "\n", 1) <= 0)
138             goto err;
139     }
140     return 1;
141  err:
142     return 0;
143 }
144
145 static int i2r_ocsp_acutoff(const X509V3_EXT_METHOD *method, void *cutoff,
146                             BIO *bp, int ind)
147 {
148     if (BIO_printf(bp, "%*s", ind, "") <= 0)
149         return 0;
150     if (!ASN1_GENERALIZEDTIME_print(bp, cutoff))
151         return 0;
152     return 1;
153 }
154
155 static int i2r_object(const X509V3_EXT_METHOD *method, void *oid, BIO *bp,
156                       int ind)
157 {
158     if (BIO_printf(bp, "%*s", ind, "") <= 0)
159         return 0;
160     if (i2a_ASN1_OBJECT(bp, oid) <= 0)
161         return 0;
162     return 1;
163 }
164
165 /*
166  * OCSP nonce. This is needs special treatment because it doesn't have an
167  * ASN1 encoding at all: it just contains arbitrary data.
168  */
169
170 static void *ocsp_nonce_new(void)
171 {
172     return ASN1_OCTET_STRING_new();
173 }
174
175 static int i2d_ocsp_nonce(const void *a, unsigned char **pp)
176 {
177     const ASN1_OCTET_STRING *os = a;
178     if (pp) {
179         memcpy(*pp, os->data, os->length);
180         *pp += os->length;
181     }
182     return os->length;
183 }
184
185 static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length)
186 {
187     ASN1_OCTET_STRING *os, **pos;
188     pos = a;
189     if (pos == NULL || *pos == NULL) {
190         os = ASN1_OCTET_STRING_new();
191         if (os == NULL)
192             goto err;
193     } else {
194         os = *pos;
195     }
196     if (!ASN1_OCTET_STRING_set(os, *pp, length))
197         goto err;
198
199     *pp += length;
200
201     if (pos)
202         *pos = os;
203     return os;
204
205  err:
206     if ((pos == NULL) || (*pos != os))
207         ASN1_OCTET_STRING_free(os);
208     OCSPerr(OCSP_F_D2I_OCSP_NONCE, ERR_R_MALLOC_FAILURE);
209     return NULL;
210 }
211
212 static void ocsp_nonce_free(void *a)
213 {
214     ASN1_OCTET_STRING_free(a);
215 }
216
217 static int i2r_ocsp_nonce(const X509V3_EXT_METHOD *method, void *nonce,
218                           BIO *out, int indent)
219 {
220     if (BIO_printf(out, "%*s", indent, "") <= 0)
221         return 0;
222     if (i2a_ASN1_STRING(out, nonce, V_ASN1_OCTET_STRING) <= 0)
223         return 0;
224     return 1;
225 }
226
227 /* Nocheck is just a single NULL. Don't print anything and always set it */
228
229 static int i2r_ocsp_nocheck(const X509V3_EXT_METHOD *method, void *nocheck,
230                             BIO *out, int indent)
231 {
232     return 1;
233 }
234
235 static void *s2i_ocsp_nocheck(const X509V3_EXT_METHOD *method,
236                               X509V3_CTX *ctx, const char *str)
237 {
238     return ASN1_NULL_new();
239 }
240
241 static int i2r_ocsp_serviceloc(const X509V3_EXT_METHOD *method, void *in,
242                                BIO *bp, int ind)
243 {
244     int i;
245     OCSP_SERVICELOC *a = in;
246     ACCESS_DESCRIPTION *ad;
247
248     if (BIO_printf(bp, "%*sIssuer: ", ind, "") <= 0)
249         goto err;
250     if (X509_NAME_print_ex(bp, a->issuer, 0, XN_FLAG_ONELINE) <= 0)
251         goto err;
252     for (i = 0; i < sk_ACCESS_DESCRIPTION_num(a->locator); i++) {
253         ad = sk_ACCESS_DESCRIPTION_value(a->locator, i);
254         if (BIO_printf(bp, "\n%*s", (2 * ind), "") <= 0)
255             goto err;
256         if (i2a_ASN1_OBJECT(bp, ad->method) <= 0)
257             goto err;
258         if (BIO_puts(bp, " - ") <= 0)
259             goto err;
260         if (GENERAL_NAME_print(bp, ad->location) <= 0)
261             goto err;
262     }
263     return 1;
264  err:
265     return 0;
266 }