xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/x509/v3_info.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /*
2  * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
3  * 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  *    [email protected].
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  * ([email protected]).  This product includes software written by Tim
55  * Hudson ([email protected]).
56  *
57  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 
62 #include <openssl/asn1.h>
63 #include <openssl/asn1t.h>
64 #include <openssl/conf.h>
65 #include <openssl/err.h>
66 #include <openssl/mem.h>
67 #include <openssl/obj.h>
68 #include <openssl/x509.h>
69 
70 #include "internal.h"
71 
72 
73 static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(
74     const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret);
75 static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method,
76                                        const X509V3_CTX *ctx,
77                                        const STACK_OF(CONF_VALUE) *nval);
78 
79 const X509V3_EXT_METHOD v3_info = {
80     NID_info_access,
81     X509V3_EXT_MULTILINE,
82     ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
83     0,
84     0,
85     0,
86     0,
87     0,
88     0,
89     i2v_AUTHORITY_INFO_ACCESS,
90     v2i_AUTHORITY_INFO_ACCESS,
91     0,
92     0,
93     NULL,
94 };
95 
96 const X509V3_EXT_METHOD v3_sinfo = {
97     NID_sinfo_access,
98     X509V3_EXT_MULTILINE,
99     ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
100     0,
101     0,
102     0,
103     0,
104     0,
105     0,
106     i2v_AUTHORITY_INFO_ACCESS,
107     v2i_AUTHORITY_INFO_ACCESS,
108     0,
109     0,
110     NULL,
111 };
112 
113 ASN1_SEQUENCE(ACCESS_DESCRIPTION) = {
114     ASN1_SIMPLE(ACCESS_DESCRIPTION, method, ASN1_OBJECT),
115     ASN1_SIMPLE(ACCESS_DESCRIPTION, location, GENERAL_NAME),
116 } ASN1_SEQUENCE_END(ACCESS_DESCRIPTION)
117 
118 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ACCESS_DESCRIPTION)
119 
120 ASN1_ITEM_TEMPLATE(AUTHORITY_INFO_ACCESS) = ASN1_EX_TEMPLATE_TYPE(
121     ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, ACCESS_DESCRIPTION)
122 ASN1_ITEM_TEMPLATE_END(AUTHORITY_INFO_ACCESS)
123 
124 IMPLEMENT_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS)
125 
126 static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(
127     const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) {
128   const AUTHORITY_INFO_ACCESS *ainfo = ext;
129   ACCESS_DESCRIPTION *desc;
130   int nlen;
131   char objtmp[80], *ntmp;
132   CONF_VALUE *vtmp;
133   STACK_OF(CONF_VALUE) *tret = ret;
134 
135   for (size_t i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) {
136     STACK_OF(CONF_VALUE) *tmp;
137 
138     desc = sk_ACCESS_DESCRIPTION_value(ainfo, i);
139     tmp = i2v_GENERAL_NAME(method, desc->location, tret);
140     if (tmp == NULL) {
141       goto err;
142     }
143     tret = tmp;
144     vtmp = sk_CONF_VALUE_value(tret, i);
145     i2t_ASN1_OBJECT(objtmp, sizeof objtmp, desc->method);
146     nlen = strlen(objtmp) + strlen(vtmp->name) + 5;
147     ntmp = OPENSSL_malloc(nlen);
148     if (ntmp == NULL) {
149       goto err;
150     }
151     OPENSSL_strlcpy(ntmp, objtmp, nlen);
152     OPENSSL_strlcat(ntmp, " - ", nlen);
153     OPENSSL_strlcat(ntmp, vtmp->name, nlen);
154     OPENSSL_free(vtmp->name);
155     vtmp->name = ntmp;
156   }
157   if (ret == NULL && tret == NULL) {
158     return sk_CONF_VALUE_new_null();
159   }
160 
161   return tret;
162 err:
163   if (ret == NULL && tret != NULL) {
164     sk_CONF_VALUE_pop_free(tret, X509V3_conf_free);
165   }
166   return NULL;
167 }
168 
v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD * method,const X509V3_CTX * ctx,const STACK_OF (CONF_VALUE)* nval)169 static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method,
170                                        const X509V3_CTX *ctx,
171                                        const STACK_OF(CONF_VALUE) *nval) {
172   AUTHORITY_INFO_ACCESS *ainfo = NULL;
173   ACCESS_DESCRIPTION *acc;
174   if (!(ainfo = sk_ACCESS_DESCRIPTION_new_null())) {
175     return NULL;
176   }
177   for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
178     const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i);
179     if (!(acc = ACCESS_DESCRIPTION_new()) ||
180         !sk_ACCESS_DESCRIPTION_push(ainfo, acc)) {
181       goto err;
182     }
183     char *ptmp = strchr(cnf->name, ';');
184     if (!ptmp) {
185       OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SYNTAX);
186       goto err;
187     }
188     CONF_VALUE ctmp;
189     ctmp.name = ptmp + 1;
190     ctmp.value = cnf->value;
191     if (!v2i_GENERAL_NAME_ex(acc->location, method, ctx, &ctmp, 0)) {
192       goto err;
193     }
194     char *objtmp = OPENSSL_strndup(cnf->name, ptmp - cnf->name);
195     if (objtmp == NULL) {
196       goto err;
197     }
198     acc->method = OBJ_txt2obj(objtmp, 0);
199     if (!acc->method) {
200       OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_OBJECT);
201       ERR_add_error_data(2, "value=", objtmp);
202       OPENSSL_free(objtmp);
203       goto err;
204     }
205     OPENSSL_free(objtmp);
206   }
207   return ainfo;
208 err:
209   sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free);
210   return NULL;
211 }
212