1 /* $OpenBSD: v3_sxnet.c,v 1.22 2019/03/13 20:34:00 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
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>
68 /* Support for Thawte strong extranet extension */
72 static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
75 static SXNET * sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
76 STACK_OF(CONF_VALUE) *nval);
79 const X509V3_EXT_METHOD v3_sxnet = {
81 .ext_flags = X509V3_EXT_MULTILINE,
91 .v2i = (X509V3_EXT_V2I)sxnet_v2i,
95 .i2r = (X509V3_EXT_I2R)sxnet_i2r,
100 static const ASN1_TEMPLATE SXNETID_seq_tt[] = {
104 .offset = offsetof(SXNETID, zone),
105 .field_name = "zone",
106 .item = &ASN1_INTEGER_it,
111 .offset = offsetof(SXNETID, user),
112 .field_name = "user",
113 .item = &ASN1_OCTET_STRING_it,
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),
123 .size = sizeof(SXNETID),
129 d2i_SXNETID(SXNETID **a, const unsigned char **in, long len)
131 return (SXNETID *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
136 i2d_SXNETID(SXNETID *a, unsigned char **out)
138 return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNETID_it);
144 return (SXNETID *)ASN1_item_new(&SXNETID_it);
148 SXNETID_free(SXNETID *a)
150 ASN1_item_free((ASN1_VALUE *)a, &SXNETID_it);
153 static const ASN1_TEMPLATE SXNET_seq_tt[] = {
157 .offset = offsetof(SXNET, version),
158 .field_name = "version",
159 .item = &ASN1_INTEGER_it,
162 .flags = ASN1_TFLG_SEQUENCE_OF,
164 .offset = offsetof(SXNET, ids),
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),
176 .size = sizeof(SXNET),
182 d2i_SXNET(SXNET **a, const unsigned char **in, long len)
184 return (SXNET *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
189 i2d_SXNET(SXNET *a, unsigned char **out)
191 return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNET_it);
197 return (SXNET *)ASN1_item_new(&SXNET_it);
203 ASN1_item_free((ASN1_VALUE *)a, &SXNET_it);
207 sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out, int indent)
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);
221 ASN1_STRING_print(out, id->user);
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.
234 sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
235 STACK_OF(CONF_VALUE) *nval)
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))
251 /* Strong Extranet utility functions */
253 /* Add an id given the zone as an ASCII number */
256 SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen)
258 ASN1_INTEGER *izone = NULL;
260 if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
261 X509V3error(X509V3_R_ERROR_CONVERTING_ZONE);
264 return SXNET_add_id_INTEGER(psx, izone, user, userlen);
267 /* Add an id given the zone as an unsigned long */
270 SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user,
273 ASN1_INTEGER *izone = NULL;
275 if (!(izone = ASN1_INTEGER_new()) ||
276 !ASN1_INTEGER_set(izone, lzone)) {
277 X509V3error(ERR_R_MALLOC_FAILURE);
278 ASN1_INTEGER_free(izone);
281 return SXNET_add_id_INTEGER(psx, izone, user, userlen);
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.
290 SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user,
296 if (!psx || !zone || !user) {
297 X509V3error(X509V3_R_INVALID_NULL_ARGUMENT);
301 userlen = strlen(user);
303 X509V3error(X509V3_R_USER_TOO_LONG);
307 if (!(sx = SXNET_new()))
309 if (!ASN1_INTEGER_set(sx->version, 0))
314 if (SXNET_get_id_INTEGER(sx, zone)) {
315 X509V3error(X509V3_R_DUPLICATE_ZONE_ID);
319 if (!(id = SXNETID_new()))
322 userlen = strlen(user);
324 if (!ASN1_STRING_set(id->user, user, userlen))
326 if (!sk_SXNETID_push(sx->ids, id))
332 X509V3error(ERR_R_MALLOC_FAILURE);
340 SXNET_get_id_asc(SXNET *sx, const char *zone)
342 ASN1_INTEGER *izone = NULL;
343 ASN1_OCTET_STRING *oct;
345 if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
346 X509V3error(X509V3_R_ERROR_CONVERTING_ZONE);
349 oct = SXNET_get_id_INTEGER(sx, izone);
350 ASN1_INTEGER_free(izone);
355 SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
357 ASN1_INTEGER *izone = NULL;
358 ASN1_OCTET_STRING *oct;
360 if (!(izone = ASN1_INTEGER_new()) ||
361 !ASN1_INTEGER_set(izone, lzone)) {
362 X509V3error(ERR_R_MALLOC_FAILURE);
363 ASN1_INTEGER_free(izone);
366 oct = SXNET_get_id_INTEGER(sx, izone);
367 ASN1_INTEGER_free(izone);
372 SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
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))