libressl: Fix validation errors in certificate chains with expired certificates
[dragonfly.git] / crypto / libressl / crypto / x509 / x509_extku.c
1 /* $OpenBSD: x509_extku.c,v 1.1 2020/06/04 15:19:31 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60
61 #include <openssl/asn1t.h>
62 #include <openssl/conf.h>
63 #include <openssl/err.h>
64 #include <openssl/x509v3.h>
65
66 static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method,
67     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
68 static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(
69     const X509V3_EXT_METHOD *method, void *eku, STACK_OF(CONF_VALUE) *extlist);
70
71 const X509V3_EXT_METHOD v3_ext_ku = {
72         .ext_nid = NID_ext_key_usage,
73         .ext_flags = 0,
74         .it = &EXTENDED_KEY_USAGE_it,
75         .ext_new = NULL,
76         .ext_free = NULL,
77         .d2i = NULL,
78         .i2d = NULL,
79         .i2s = NULL,
80         .s2i = NULL,
81         .i2v = i2v_EXTENDED_KEY_USAGE,
82         .v2i = v2i_EXTENDED_KEY_USAGE,
83         .i2r = NULL,
84         .r2i = NULL,
85         .usr_data = NULL,
86 };
87
88 /* NB OCSP acceptable responses also is a SEQUENCE OF OBJECT */
89 const X509V3_EXT_METHOD v3_ocsp_accresp = {
90         .ext_nid = NID_id_pkix_OCSP_acceptableResponses,
91         .ext_flags = 0,
92         .it = &EXTENDED_KEY_USAGE_it,
93         .ext_new = NULL,
94         .ext_free = NULL,
95         .d2i = NULL,
96         .i2d = NULL,
97         .i2s = NULL,
98         .s2i = NULL,
99         .i2v = i2v_EXTENDED_KEY_USAGE,
100         .v2i = v2i_EXTENDED_KEY_USAGE,
101         .i2r = NULL,
102         .r2i = NULL,
103         .usr_data = NULL,
104 };
105
106 static const ASN1_TEMPLATE EXTENDED_KEY_USAGE_item_tt = {
107         .flags = ASN1_TFLG_SEQUENCE_OF,
108         .tag = 0,
109         .offset = 0,
110         .field_name = "EXTENDED_KEY_USAGE",
111         .item = &ASN1_OBJECT_it,
112 };
113
114 const ASN1_ITEM EXTENDED_KEY_USAGE_it = {
115         .itype = ASN1_ITYPE_PRIMITIVE,
116         .utype = -1,
117         .templates = &EXTENDED_KEY_USAGE_item_tt,
118         .tcount = 0,
119         .funcs = NULL,
120         .size = 0,
121         .sname = "EXTENDED_KEY_USAGE",
122 };
123
124
125 EXTENDED_KEY_USAGE *
126 d2i_EXTENDED_KEY_USAGE(EXTENDED_KEY_USAGE **a, const unsigned char **in, long len)
127 {
128         return (EXTENDED_KEY_USAGE *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
129             &EXTENDED_KEY_USAGE_it);
130 }
131
132 int
133 i2d_EXTENDED_KEY_USAGE(EXTENDED_KEY_USAGE *a, unsigned char **out)
134 {
135         return ASN1_item_i2d((ASN1_VALUE *)a, out, &EXTENDED_KEY_USAGE_it);
136 }
137
138 EXTENDED_KEY_USAGE *
139 EXTENDED_KEY_USAGE_new(void)
140 {
141         return (EXTENDED_KEY_USAGE *)ASN1_item_new(&EXTENDED_KEY_USAGE_it);
142 }
143
144 void
145 EXTENDED_KEY_USAGE_free(EXTENDED_KEY_USAGE *a)
146 {
147         ASN1_item_free((ASN1_VALUE *)a, &EXTENDED_KEY_USAGE_it);
148 }
149
150 static STACK_OF(CONF_VALUE) *
151 i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, void *a,
152     STACK_OF(CONF_VALUE) *extlist)
153 {
154         ASN1_OBJECT *obj;
155         EXTENDED_KEY_USAGE *eku = a;
156         STACK_OF(CONF_VALUE) *free_extlist = NULL;
157         char obj_tmp[80];
158         int i;
159
160         if (extlist == NULL) {
161                 if ((free_extlist = extlist = sk_CONF_VALUE_new_null()) == NULL)
162                         return NULL;
163         }
164
165         for (i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
166                 if ((obj = sk_ASN1_OBJECT_value(eku, i)) == NULL)
167                         goto err;
168                 if (!i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, obj))
169                         goto err;
170                 if (!X509V3_add_value(NULL, obj_tmp, &extlist))
171                         goto err;
172         }
173
174         return extlist;
175
176  err:
177         sk_CONF_VALUE_pop_free(free_extlist, X509V3_conf_free);
178
179         return NULL;
180 }
181
182 static void *
183 v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
184     STACK_OF(CONF_VALUE) *nval)
185 {
186         EXTENDED_KEY_USAGE *extku;
187         char *extval;
188         ASN1_OBJECT *objtmp;
189         CONF_VALUE *val;
190         int i;
191
192         if (!(extku = sk_ASN1_OBJECT_new_null())) {
193                 X509V3error(ERR_R_MALLOC_FAILURE);
194                 return NULL;
195         }
196
197         for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
198                 val = sk_CONF_VALUE_value(nval, i);
199                 if (val->value)
200                         extval = val->value;
201                 else
202                         extval = val->name;
203                 if (!(objtmp = OBJ_txt2obj(extval, 0))) {
204                         sk_ASN1_OBJECT_pop_free(extku, ASN1_OBJECT_free);
205                         X509V3error(X509V3_R_INVALID_OBJECT_IDENTIFIER);
206                         X509V3_conf_err(val);
207                         return NULL;
208                 }
209                 if (sk_ASN1_OBJECT_push(extku, objtmp) == 0) {
210                         ASN1_OBJECT_free(objtmp);
211                         sk_ASN1_OBJECT_pop_free(extku, ASN1_OBJECT_free);
212                         X509V3error(ERR_R_MALLOC_FAILURE);
213                         return NULL;
214                 }
215         }
216         return extku;
217 }