1 /* $OpenBSD: x509_sxnet.c,v 1.1 2020/06/04 15:19:32 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 #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/x509v3.h>
67 
68 /* Support for Thawte strong extranet extension */
69 
70 #define SXNET_TEST
71 
72 static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
73     int indent);
74 #ifdef SXNET_TEST
75 static SXNET * sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
76     STACK_OF(CONF_VALUE) *nval);
77 #endif
78 
79 const X509V3_EXT_METHOD v3_sxnet = {
80 	.ext_nid = NID_sxnet,
81 	.ext_flags = X509V3_EXT_MULTILINE,
82 	.it = &SXNET_it,
83 	.ext_new = NULL,
84 	.ext_free = NULL,
85 	.d2i = NULL,
86 	.i2d = NULL,
87 	.i2s = NULL,
88 	.s2i = NULL,
89 	.i2v = NULL,
90 #ifdef SXNET_TEST
91 	.v2i = (X509V3_EXT_V2I)sxnet_v2i,
92 #else
93 	.v2i = NULL,
94 #endif
95 	.i2r = (X509V3_EXT_I2R)sxnet_i2r,
96 	.r2i = NULL,
97 	.usr_data = NULL,
98 };
99 
100 static const ASN1_TEMPLATE SXNETID_seq_tt[] = {
101 	{
102 		.flags = 0,
103 		.tag = 0,
104 		.offset = offsetof(SXNETID, zone),
105 		.field_name = "zone",
106 		.item = &ASN1_INTEGER_it,
107 	},
108 	{
109 		.flags = 0,
110 		.tag = 0,
111 		.offset = offsetof(SXNETID, user),
112 		.field_name = "user",
113 		.item = &ASN1_OCTET_STRING_it,
114 	},
115 };
116 
117 const ASN1_ITEM SXNETID_it = {
118 	.itype = ASN1_ITYPE_SEQUENCE,
119 	.utype = V_ASN1_SEQUENCE,
120 	.templates = SXNETID_seq_tt,
121 	.tcount = sizeof(SXNETID_seq_tt) / sizeof(ASN1_TEMPLATE),
122 	.funcs = NULL,
123 	.size = sizeof(SXNETID),
124 	.sname = "SXNETID",
125 };
126 
127 
128 SXNETID *
d2i_SXNETID(SXNETID ** a,const unsigned char ** in,long len)129 d2i_SXNETID(SXNETID **a, const unsigned char **in, long len)
130 {
131 	return (SXNETID *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
132 	    &SXNETID_it);
133 }
134 
135 int
i2d_SXNETID(SXNETID * a,unsigned char ** out)136 i2d_SXNETID(SXNETID *a, unsigned char **out)
137 {
138 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNETID_it);
139 }
140 
141 SXNETID *
SXNETID_new(void)142 SXNETID_new(void)
143 {
144 	return (SXNETID *)ASN1_item_new(&SXNETID_it);
145 }
146 
147 void
SXNETID_free(SXNETID * a)148 SXNETID_free(SXNETID *a)
149 {
150 	ASN1_item_free((ASN1_VALUE *)a, &SXNETID_it);
151 }
152 
153 static const ASN1_TEMPLATE SXNET_seq_tt[] = {
154 	{
155 		.flags = 0,
156 		.tag = 0,
157 		.offset = offsetof(SXNET, version),
158 		.field_name = "version",
159 		.item = &ASN1_INTEGER_it,
160 	},
161 	{
162 		.flags = ASN1_TFLG_SEQUENCE_OF,
163 		.tag = 0,
164 		.offset = offsetof(SXNET, ids),
165 		.field_name = "ids",
166 		.item = &SXNETID_it,
167 	},
168 };
169 
170 const ASN1_ITEM SXNET_it = {
171 	.itype = ASN1_ITYPE_SEQUENCE,
172 	.utype = V_ASN1_SEQUENCE,
173 	.templates = SXNET_seq_tt,
174 	.tcount = sizeof(SXNET_seq_tt) / sizeof(ASN1_TEMPLATE),
175 	.funcs = NULL,
176 	.size = sizeof(SXNET),
177 	.sname = "SXNET",
178 };
179 
180 
181 SXNET *
d2i_SXNET(SXNET ** a,const unsigned char ** in,long len)182 d2i_SXNET(SXNET **a, const unsigned char **in, long len)
183 {
184 	return (SXNET *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
185 	    &SXNET_it);
186 }
187 
188 int
i2d_SXNET(SXNET * a,unsigned char ** out)189 i2d_SXNET(SXNET *a, unsigned char **out)
190 {
191 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNET_it);
192 }
193 
194 SXNET *
SXNET_new(void)195 SXNET_new(void)
196 {
197 	return (SXNET *)ASN1_item_new(&SXNET_it);
198 }
199 
200 void
SXNET_free(SXNET * a)201 SXNET_free(SXNET *a)
202 {
203 	ASN1_item_free((ASN1_VALUE *)a, &SXNET_it);
204 }
205 
206 static int
sxnet_i2r(X509V3_EXT_METHOD * method,SXNET * sx,BIO * out,int indent)207 sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out, int indent)
208 {
209 	long v;
210 	char *tmp;
211 	SXNETID *id;
212 	int i;
213 
214 	v = ASN1_INTEGER_get(sx->version);
215 	BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", v + 1, v);
216 	for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
217 		id = sk_SXNETID_value(sx->ids, i);
218 		tmp = i2s_ASN1_INTEGER(NULL, id->zone);
219 		BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
220 		free(tmp);
221 		ASN1_STRING_print(out, id->user);
222 	}
223 	return 1;
224 }
225 
226 #ifdef SXNET_TEST
227 
228 /* NBB: this is used for testing only. It should *not* be used for anything
229  * else because it will just take static IDs from the configuration file and
230  * they should really be separate values for each user.
231  */
232 
233 static SXNET *
sxnet_v2i(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)234 sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
235     STACK_OF(CONF_VALUE) *nval)
236 {
237 	CONF_VALUE *cnf;
238 	SXNET *sx = NULL;
239 	int i;
240 
241 	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
242 		cnf = sk_CONF_VALUE_value(nval, i);
243 		if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
244 			return NULL;
245 	}
246 	return sx;
247 }
248 
249 #endif
250 
251 /* Strong Extranet utility functions */
252 
253 /* Add an id given the zone as an ASCII number */
254 
255 int
SXNET_add_id_asc(SXNET ** psx,const char * zone,const char * user,int userlen)256 SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen)
257 {
258 	ASN1_INTEGER *izone = NULL;
259 
260 	if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
261 		X509V3error(X509V3_R_ERROR_CONVERTING_ZONE);
262 		return 0;
263 	}
264 	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
265 }
266 
267 /* Add an id given the zone as an unsigned long */
268 
269 int
SXNET_add_id_ulong(SXNET ** psx,unsigned long lzone,const char * user,int userlen)270 SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user,
271     int userlen)
272 {
273 	ASN1_INTEGER *izone = NULL;
274 
275 	if (!(izone = ASN1_INTEGER_new()) ||
276 	    !ASN1_INTEGER_set(izone, lzone)) {
277 		X509V3error(ERR_R_MALLOC_FAILURE);
278 		ASN1_INTEGER_free(izone);
279 		return 0;
280 	}
281 	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
282 }
283 
284 /* Add an id given the zone as an ASN1_INTEGER.
285  * Note this version uses the passed integer and doesn't make a copy so don't
286  * free it up afterwards.
287  */
288 
289 int
SXNET_add_id_INTEGER(SXNET ** psx,ASN1_INTEGER * zone,const char * user,int userlen)290 SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user,
291     int userlen)
292 {
293 	SXNET *sx = NULL;
294 	SXNETID *id = NULL;
295 
296 	if (!psx || !zone || !user) {
297 		X509V3error(X509V3_R_INVALID_NULL_ARGUMENT);
298 		return 0;
299 	}
300 	if (userlen == -1)
301 		userlen = strlen(user);
302 	if (userlen > 64) {
303 		X509V3error(X509V3_R_USER_TOO_LONG);
304 		return 0;
305 	}
306 	if (!*psx) {
307 		if (!(sx = SXNET_new()))
308 			goto err;
309 		if (!ASN1_INTEGER_set(sx->version, 0))
310 			goto err;
311 		*psx = sx;
312 	} else
313 		sx = *psx;
314 	if (SXNET_get_id_INTEGER(sx, zone)) {
315 		X509V3error(X509V3_R_DUPLICATE_ZONE_ID);
316 		return 0;
317 	}
318 
319 	if (!(id = SXNETID_new()))
320 		goto err;
321 	if (userlen == -1)
322 		userlen = strlen(user);
323 
324 	if (!ASN1_STRING_set(id->user, user, userlen))
325 		goto err;
326 	if (!sk_SXNETID_push(sx->ids, id))
327 		goto err;
328 	id->zone = zone;
329 	return 1;
330 
331 err:
332 	X509V3error(ERR_R_MALLOC_FAILURE);
333 	SXNETID_free(id);
334 	SXNET_free(sx);
335 	*psx = NULL;
336 	return 0;
337 }
338 
339 ASN1_OCTET_STRING *
SXNET_get_id_asc(SXNET * sx,const char * zone)340 SXNET_get_id_asc(SXNET *sx, const char *zone)
341 {
342 	ASN1_INTEGER *izone = NULL;
343 	ASN1_OCTET_STRING *oct;
344 
345 	if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
346 		X509V3error(X509V3_R_ERROR_CONVERTING_ZONE);
347 		return NULL;
348 	}
349 	oct = SXNET_get_id_INTEGER(sx, izone);
350 	ASN1_INTEGER_free(izone);
351 	return oct;
352 }
353 
354 ASN1_OCTET_STRING *
SXNET_get_id_ulong(SXNET * sx,unsigned long lzone)355 SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
356 {
357 	ASN1_INTEGER *izone = NULL;
358 	ASN1_OCTET_STRING *oct;
359 
360 	if (!(izone = ASN1_INTEGER_new()) ||
361 	    !ASN1_INTEGER_set(izone, lzone)) {
362 		X509V3error(ERR_R_MALLOC_FAILURE);
363 		ASN1_INTEGER_free(izone);
364 		return NULL;
365 	}
366 	oct = SXNET_get_id_INTEGER(sx, izone);
367 	ASN1_INTEGER_free(izone);
368 	return oct;
369 }
370 
371 ASN1_OCTET_STRING *
SXNET_get_id_INTEGER(SXNET * sx,ASN1_INTEGER * zone)372 SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
373 {
374 	SXNETID *id;
375 	int i;
376 
377 	for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
378 		id = sk_SXNETID_value(sx->ids, i);
379 		if (!ASN1_INTEGER_cmp(id->zone, zone))
380 			return id->user;
381 	}
382 	return NULL;
383 }
384