xref: /openbsd/lib/libcrypto/x509/x509_ia5.c (revision 8b5faa71)
1 /* $OpenBSD: x509_ia5.c,v 1.2 2024/07/13 15:08:58 tb 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 #include <string.h>
61 
62 #include <openssl/asn1.h>
63 #include <openssl/conf.h>
64 #include <openssl/err.h>
65 #include <openssl/x509v3.h>
66 
67 static char *i2s_ASN1_IA5STRING(X509V3_EXT_METHOD *method, ASN1_IA5STRING *ia5);
68 static ASN1_IA5STRING *s2i_ASN1_IA5STRING(X509V3_EXT_METHOD *method,
69     X509V3_CTX *ctx, char *str);
70 
71 static const X509V3_EXT_METHOD x509v3_ext_netscape_base_url = {
72 	.ext_nid = NID_netscape_base_url,
73 	.ext_flags = 0,
74 	.it = &ASN1_IA5STRING_it,
75 	.ext_new = NULL,
76 	.ext_free = NULL,
77 	.d2i = NULL,
78 	.i2d = NULL,
79 	.i2s = (X509V3_EXT_I2S)i2s_ASN1_IA5STRING,
80 	.s2i = (X509V3_EXT_S2I)s2i_ASN1_IA5STRING,
81 	.i2v = NULL,
82 	.v2i = NULL,
83 	.i2r = NULL,
84 	.r2i = NULL,
85 	.usr_data = NULL,
86 };
87 
88 const X509V3_EXT_METHOD *
x509v3_ext_method_netscape_base_url(void)89 x509v3_ext_method_netscape_base_url(void)
90 {
91 	return &x509v3_ext_netscape_base_url;
92 }
93 
94 static const X509V3_EXT_METHOD x509v3_ext_netscape_revocation_url = {
95 	.ext_nid = NID_netscape_revocation_url,
96 	.ext_flags = 0,
97 	.it = &ASN1_IA5STRING_it,
98 	.ext_new = NULL,
99 	.ext_free = NULL,
100 	.d2i = NULL,
101 	.i2d = NULL,
102 	.i2s = (X509V3_EXT_I2S)i2s_ASN1_IA5STRING,
103 	.s2i = (X509V3_EXT_S2I)s2i_ASN1_IA5STRING,
104 	.i2v = NULL,
105 	.v2i = NULL,
106 	.i2r = NULL,
107 	.r2i = NULL,
108 	.usr_data = NULL,
109 };
110 
111 const X509V3_EXT_METHOD *
x509v3_ext_method_netscape_revocation_url(void)112 x509v3_ext_method_netscape_revocation_url(void)
113 {
114 	return &x509v3_ext_netscape_revocation_url;
115 }
116 
117 static const X509V3_EXT_METHOD x509v3_ext_netscape_ca_revocation_url = {
118 	.ext_nid = NID_netscape_ca_revocation_url,
119 	.ext_flags = 0,
120 	.it = &ASN1_IA5STRING_it,
121 	.ext_new = NULL,
122 	.ext_free = NULL,
123 	.d2i = NULL,
124 	.i2d = NULL,
125 	.i2s = (X509V3_EXT_I2S)i2s_ASN1_IA5STRING,
126 	.s2i = (X509V3_EXT_S2I)s2i_ASN1_IA5STRING,
127 	.i2v = NULL,
128 	.v2i = NULL,
129 	.i2r = NULL,
130 	.r2i = NULL,
131 	.usr_data = NULL,
132 };
133 
134 const X509V3_EXT_METHOD *
x509v3_ext_method_netscape_ca_revocation_url(void)135 x509v3_ext_method_netscape_ca_revocation_url(void)
136 {
137 	return &x509v3_ext_netscape_ca_revocation_url;
138 }
139 
140 static const X509V3_EXT_METHOD x509v3_ext_netscape_renewal_url = {
141 	.ext_nid = NID_netscape_renewal_url,
142 	.ext_flags = 0,
143 	.it = &ASN1_IA5STRING_it,
144 	.ext_new = NULL,
145 	.ext_free = NULL,
146 	.d2i = NULL,
147 	.i2d = NULL,
148 	.i2s = (X509V3_EXT_I2S)i2s_ASN1_IA5STRING,
149 	.s2i = (X509V3_EXT_S2I)s2i_ASN1_IA5STRING,
150 	.i2v = NULL,
151 	.v2i = NULL,
152 	.i2r = NULL,
153 	.r2i = NULL,
154 	.usr_data = NULL,
155 };
156 
157 const X509V3_EXT_METHOD *
x509v3_ext_method_netscape_renewal_url(void)158 x509v3_ext_method_netscape_renewal_url(void)
159 {
160 	return &x509v3_ext_netscape_renewal_url;
161 }
162 
163 static const X509V3_EXT_METHOD x509v3_ext_netscape_ca_policy_url = {
164 	.ext_nid = NID_netscape_ca_policy_url,
165 	.ext_flags = 0,
166 	.it = &ASN1_IA5STRING_it,
167 	.ext_new = NULL,
168 	.ext_free = NULL,
169 	.d2i = NULL,
170 	.i2d = NULL,
171 	.i2s = (X509V3_EXT_I2S)i2s_ASN1_IA5STRING,
172 	.s2i = (X509V3_EXT_S2I)s2i_ASN1_IA5STRING,
173 	.i2v = NULL,
174 	.v2i = NULL,
175 	.i2r = NULL,
176 	.r2i = NULL,
177 	.usr_data = NULL,
178 };
179 
180 const X509V3_EXT_METHOD *
x509v3_ext_method_netscape_ca_policy_url(void)181 x509v3_ext_method_netscape_ca_policy_url(void)
182 {
183 	return &x509v3_ext_netscape_ca_policy_url;
184 }
185 
186 static const X509V3_EXT_METHOD x509v3_ext_netscape_ssl_server_name = {
187 	.ext_nid = NID_netscape_ssl_server_name,
188 	.ext_flags = 0,
189 	.it = &ASN1_IA5STRING_it,
190 	.ext_new = NULL,
191 	.ext_free = NULL,
192 	.d2i = NULL,
193 	.i2d = NULL,
194 	.i2s = (X509V3_EXT_I2S)i2s_ASN1_IA5STRING,
195 	.s2i = (X509V3_EXT_S2I)s2i_ASN1_IA5STRING,
196 	.i2v = NULL,
197 	.v2i = NULL,
198 	.i2r = NULL,
199 	.r2i = NULL,
200 	.usr_data = NULL,
201 };
202 
203 const X509V3_EXT_METHOD *
x509v3_ext_method_netscape_ssl_server_name(void)204 x509v3_ext_method_netscape_ssl_server_name(void)
205 {
206 	return &x509v3_ext_netscape_ssl_server_name;
207 }
208 
209 static const X509V3_EXT_METHOD x509v3_ext_netscape_comment = {
210 	.ext_nid = NID_netscape_comment,
211 	.ext_flags = 0,
212 	.it = &ASN1_IA5STRING_it,
213 	.ext_new = NULL,
214 	.ext_free = NULL,
215 	.d2i = NULL,
216 	.i2d = NULL,
217 	.i2s = (X509V3_EXT_I2S)i2s_ASN1_IA5STRING,
218 	.s2i = (X509V3_EXT_S2I)s2i_ASN1_IA5STRING,
219 	.i2v = NULL,
220 	.v2i = NULL,
221 	.i2r = NULL,
222 	.r2i = NULL,
223 	.usr_data = NULL,
224 };
225 
226 const X509V3_EXT_METHOD *
x509v3_ext_method_netscape_comment(void)227 x509v3_ext_method_netscape_comment(void)
228 {
229 	return &x509v3_ext_netscape_comment;
230 }
231 
232 static char *
i2s_ASN1_IA5STRING(X509V3_EXT_METHOD * method,ASN1_IA5STRING * ia5)233 i2s_ASN1_IA5STRING(X509V3_EXT_METHOD *method, ASN1_IA5STRING *ia5)
234 {
235 	char *tmp;
236 
237 	if (!ia5 || !ia5->length)
238 		return NULL;
239 	if (!(tmp = malloc(ia5->length + 1))) {
240 		X509V3error(ERR_R_MALLOC_FAILURE);
241 		return NULL;
242 	}
243 	memcpy(tmp, ia5->data, ia5->length);
244 	tmp[ia5->length] = 0;
245 	return tmp;
246 }
247 
248 static ASN1_IA5STRING *
s2i_ASN1_IA5STRING(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,char * str)249 s2i_ASN1_IA5STRING(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *str)
250 {
251 	ASN1_IA5STRING *ia5;
252 	if (!str) {
253 		X509V3error(X509V3_R_INVALID_NULL_ARGUMENT);
254 		return NULL;
255 	}
256 	if (!(ia5 = ASN1_IA5STRING_new()))
257 		goto err;
258 	if (!ASN1_STRING_set((ASN1_STRING *)ia5, (unsigned char*)str,
259 	    strlen(str))) {
260 		ASN1_IA5STRING_free(ia5);
261 		goto err;
262 	}
263 	return ia5;
264 
265 err:
266 	X509V3error(ERR_R_MALLOC_FAILURE);
267 	return NULL;
268 }
269