xref: /netbsd-src/crypto/external/bsd/openssl.old/dist/crypto/x509v3/v3_sxnet.c (revision 4724848cf0da353df257f730694b7882798e5daf)
1c9496f6bSchristos /*
2*4724848cSchristos  * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
3c9496f6bSchristos  *
4*4724848cSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*4724848cSchristos  * this file except in compliance with the License.  You can obtain a copy
6*4724848cSchristos  * in the file LICENSE in the source distribution or at
7*4724848cSchristos  * https://www.openssl.org/source/license.html
8c9496f6bSchristos  */
9c9496f6bSchristos 
10c9496f6bSchristos #include <stdio.h>
11*4724848cSchristos #include "internal/cryptlib.h"
12c9496f6bSchristos #include <openssl/conf.h>
13c9496f6bSchristos #include <openssl/asn1.h>
14c9496f6bSchristos #include <openssl/asn1t.h>
15c9496f6bSchristos #include <openssl/x509v3.h>
16*4724848cSchristos #include "ext_dat.h"
17c9496f6bSchristos 
18c9496f6bSchristos /* Support for Thawte strong extranet extension */
19c9496f6bSchristos 
20c9496f6bSchristos #define SXNET_TEST
21c9496f6bSchristos 
22c9496f6bSchristos static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
23c9496f6bSchristos                      int indent);
24c9496f6bSchristos #ifdef SXNET_TEST
25c9496f6bSchristos static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
26c9496f6bSchristos                         STACK_OF(CONF_VALUE) *nval);
27c9496f6bSchristos #endif
28c9496f6bSchristos const X509V3_EXT_METHOD v3_sxnet = {
29c9496f6bSchristos     NID_sxnet, X509V3_EXT_MULTILINE, ASN1_ITEM_ref(SXNET),
30c9496f6bSchristos     0, 0, 0, 0,
31c9496f6bSchristos     0, 0,
32c9496f6bSchristos     0,
33c9496f6bSchristos #ifdef SXNET_TEST
34c9496f6bSchristos     (X509V3_EXT_V2I)sxnet_v2i,
35c9496f6bSchristos #else
36c9496f6bSchristos     0,
37c9496f6bSchristos #endif
38c9496f6bSchristos     (X509V3_EXT_I2R)sxnet_i2r,
39c9496f6bSchristos     0,
40c9496f6bSchristos     NULL
41c9496f6bSchristos };
42c9496f6bSchristos 
43c9496f6bSchristos ASN1_SEQUENCE(SXNETID) = {
44c9496f6bSchristos         ASN1_SIMPLE(SXNETID, zone, ASN1_INTEGER),
45c9496f6bSchristos         ASN1_SIMPLE(SXNETID, user, ASN1_OCTET_STRING)
46c9496f6bSchristos } ASN1_SEQUENCE_END(SXNETID)
47c9496f6bSchristos 
48c9496f6bSchristos IMPLEMENT_ASN1_FUNCTIONS(SXNETID)
49c9496f6bSchristos 
50c9496f6bSchristos ASN1_SEQUENCE(SXNET) = {
51c9496f6bSchristos         ASN1_SIMPLE(SXNET, version, ASN1_INTEGER),
52c9496f6bSchristos         ASN1_SEQUENCE_OF(SXNET, ids, SXNETID)
53c9496f6bSchristos } ASN1_SEQUENCE_END(SXNET)
54c9496f6bSchristos 
55c9496f6bSchristos IMPLEMENT_ASN1_FUNCTIONS(SXNET)
56c9496f6bSchristos 
57c9496f6bSchristos static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
58c9496f6bSchristos                      int indent)
59c9496f6bSchristos {
60*4724848cSchristos     int64_t v;
61c9496f6bSchristos     char *tmp;
62c9496f6bSchristos     SXNETID *id;
63c9496f6bSchristos     int i;
64*4724848cSchristos 
65*4724848cSchristos     /*
66*4724848cSchristos      * Since we add 1 to the version number to display it, we don't support
67*4724848cSchristos      * LONG_MAX since that would cause on overflow.
68*4724848cSchristos      */
69*4724848cSchristos     if (!ASN1_INTEGER_get_int64(&v, sx->version)
70*4724848cSchristos             || v >= LONG_MAX
71*4724848cSchristos             || v < LONG_MIN) {
72*4724848cSchristos         BIO_printf(out, "%*sVersion: <unsupported>", indent, "");
73*4724848cSchristos     } else {
74*4724848cSchristos         long vl = (long)v;
75*4724848cSchristos 
76*4724848cSchristos         BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", vl + 1, vl);
77*4724848cSchristos     }
78c9496f6bSchristos     for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
79c9496f6bSchristos         id = sk_SXNETID_value(sx->ids, i);
80c9496f6bSchristos         tmp = i2s_ASN1_INTEGER(NULL, id->zone);
81*4724848cSchristos         if (tmp == NULL)
82*4724848cSchristos             return 0;
83c9496f6bSchristos         BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
84c9496f6bSchristos         OPENSSL_free(tmp);
85*4724848cSchristos         ASN1_STRING_print(out, id->user);
86c9496f6bSchristos     }
87c9496f6bSchristos     return 1;
88c9496f6bSchristos }
89c9496f6bSchristos 
90c9496f6bSchristos #ifdef SXNET_TEST
91c9496f6bSchristos 
92c9496f6bSchristos /*
93c9496f6bSchristos  * NBB: this is used for testing only. It should *not* be used for anything
94c9496f6bSchristos  * else because it will just take static IDs from the configuration file and
95c9496f6bSchristos  * they should really be separate values for each user.
96c9496f6bSchristos  */
97c9496f6bSchristos 
sxnet_v2i(X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)98c9496f6bSchristos static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
99c9496f6bSchristos                         STACK_OF(CONF_VALUE) *nval)
100c9496f6bSchristos {
101c9496f6bSchristos     CONF_VALUE *cnf;
102c9496f6bSchristos     SXNET *sx = NULL;
103c9496f6bSchristos     int i;
104c9496f6bSchristos     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
105c9496f6bSchristos         cnf = sk_CONF_VALUE_value(nval, i);
106c9496f6bSchristos         if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
107c9496f6bSchristos             return NULL;
108c9496f6bSchristos     }
109c9496f6bSchristos     return sx;
110c9496f6bSchristos }
111c9496f6bSchristos 
112c9496f6bSchristos #endif
113c9496f6bSchristos 
114c9496f6bSchristos /* Strong Extranet utility functions */
115c9496f6bSchristos 
116c9496f6bSchristos /* Add an id given the zone as an ASCII number */
117c9496f6bSchristos 
SXNET_add_id_asc(SXNET ** psx,const char * zone,const char * user,int userlen)118*4724848cSchristos int SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen)
119c9496f6bSchristos {
120*4724848cSchristos     ASN1_INTEGER *izone;
121*4724848cSchristos 
122*4724848cSchristos     if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
123c9496f6bSchristos         X509V3err(X509V3_F_SXNET_ADD_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
124c9496f6bSchristos         return 0;
125c9496f6bSchristos     }
126c9496f6bSchristos     return SXNET_add_id_INTEGER(psx, izone, user, userlen);
127c9496f6bSchristos }
128c9496f6bSchristos 
129c9496f6bSchristos /* Add an id given the zone as an unsigned long */
130c9496f6bSchristos 
SXNET_add_id_ulong(SXNET ** psx,unsigned long lzone,const char * user,int userlen)131*4724848cSchristos int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user,
132c9496f6bSchristos                        int userlen)
133c9496f6bSchristos {
134*4724848cSchristos     ASN1_INTEGER *izone;
135*4724848cSchristos 
136*4724848cSchristos     if ((izone = ASN1_INTEGER_new()) == NULL
137*4724848cSchristos         || !ASN1_INTEGER_set(izone, lzone)) {
138c9496f6bSchristos         X509V3err(X509V3_F_SXNET_ADD_ID_ULONG, ERR_R_MALLOC_FAILURE);
139*4724848cSchristos         ASN1_INTEGER_free(izone);
140c9496f6bSchristos         return 0;
141c9496f6bSchristos     }
142c9496f6bSchristos     return SXNET_add_id_INTEGER(psx, izone, user, userlen);
143c9496f6bSchristos 
144c9496f6bSchristos }
145c9496f6bSchristos 
146c9496f6bSchristos /*
147c9496f6bSchristos  * Add an id given the zone as an ASN1_INTEGER. Note this version uses the
148c9496f6bSchristos  * passed integer and doesn't make a copy so don't free it up afterwards.
149c9496f6bSchristos  */
150c9496f6bSchristos 
SXNET_add_id_INTEGER(SXNET ** psx,ASN1_INTEGER * zone,const char * user,int userlen)151*4724848cSchristos int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user,
152c9496f6bSchristos                          int userlen)
153c9496f6bSchristos {
154c9496f6bSchristos     SXNET *sx = NULL;
155c9496f6bSchristos     SXNETID *id = NULL;
156c9496f6bSchristos     if (!psx || !zone || !user) {
157c9496f6bSchristos         X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,
158c9496f6bSchristos                   X509V3_R_INVALID_NULL_ARGUMENT);
159c9496f6bSchristos         return 0;
160c9496f6bSchristos     }
161c9496f6bSchristos     if (userlen == -1)
162c9496f6bSchristos         userlen = strlen(user);
163c9496f6bSchristos     if (userlen > 64) {
164c9496f6bSchristos         X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_USER_TOO_LONG);
165c9496f6bSchristos         return 0;
166c9496f6bSchristos     }
167*4724848cSchristos     if (*psx == NULL) {
168*4724848cSchristos         if ((sx = SXNET_new()) == NULL)
169c9496f6bSchristos             goto err;
170c9496f6bSchristos         if (!ASN1_INTEGER_set(sx->version, 0))
171c9496f6bSchristos             goto err;
172c9496f6bSchristos         *psx = sx;
173c9496f6bSchristos     } else
174c9496f6bSchristos         sx = *psx;
175c9496f6bSchristos     if (SXNET_get_id_INTEGER(sx, zone)) {
176c9496f6bSchristos         X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_DUPLICATE_ZONE_ID);
177c9496f6bSchristos         return 0;
178c9496f6bSchristos     }
179c9496f6bSchristos 
180*4724848cSchristos     if ((id = SXNETID_new()) == NULL)
181c9496f6bSchristos         goto err;
182c9496f6bSchristos     if (userlen == -1)
183c9496f6bSchristos         userlen = strlen(user);
184c9496f6bSchristos 
185*4724848cSchristos     if (!ASN1_OCTET_STRING_set(id->user, (const unsigned char *)user, userlen))
186c9496f6bSchristos         goto err;
187c9496f6bSchristos     if (!sk_SXNETID_push(sx->ids, id))
188c9496f6bSchristos         goto err;
189c9496f6bSchristos     id->zone = zone;
190c9496f6bSchristos     return 1;
191c9496f6bSchristos 
192c9496f6bSchristos  err:
193c9496f6bSchristos     X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, ERR_R_MALLOC_FAILURE);
194c9496f6bSchristos     SXNETID_free(id);
195c9496f6bSchristos     SXNET_free(sx);
196c9496f6bSchristos     *psx = NULL;
197c9496f6bSchristos     return 0;
198c9496f6bSchristos }
199c9496f6bSchristos 
SXNET_get_id_asc(SXNET * sx,const char * zone)200*4724848cSchristos ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, const char *zone)
201c9496f6bSchristos {
202*4724848cSchristos     ASN1_INTEGER *izone;
203c9496f6bSchristos     ASN1_OCTET_STRING *oct;
204*4724848cSchristos 
205*4724848cSchristos     if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
206c9496f6bSchristos         X509V3err(X509V3_F_SXNET_GET_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
207c9496f6bSchristos         return NULL;
208c9496f6bSchristos     }
209c9496f6bSchristos     oct = SXNET_get_id_INTEGER(sx, izone);
210*4724848cSchristos     ASN1_INTEGER_free(izone);
211c9496f6bSchristos     return oct;
212c9496f6bSchristos }
213c9496f6bSchristos 
SXNET_get_id_ulong(SXNET * sx,unsigned long lzone)214c9496f6bSchristos ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
215c9496f6bSchristos {
216*4724848cSchristos     ASN1_INTEGER *izone;
217c9496f6bSchristos     ASN1_OCTET_STRING *oct;
218*4724848cSchristos 
219*4724848cSchristos     if ((izone = ASN1_INTEGER_new()) == NULL
220*4724848cSchristos         || !ASN1_INTEGER_set(izone, lzone)) {
221c9496f6bSchristos         X509V3err(X509V3_F_SXNET_GET_ID_ULONG, ERR_R_MALLOC_FAILURE);
222*4724848cSchristos         ASN1_INTEGER_free(izone);
223c9496f6bSchristos         return NULL;
224c9496f6bSchristos     }
225c9496f6bSchristos     oct = SXNET_get_id_INTEGER(sx, izone);
226*4724848cSchristos     ASN1_INTEGER_free(izone);
227c9496f6bSchristos     return oct;
228c9496f6bSchristos }
229c9496f6bSchristos 
SXNET_get_id_INTEGER(SXNET * sx,ASN1_INTEGER * zone)230c9496f6bSchristos ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
231c9496f6bSchristos {
232c9496f6bSchristos     SXNETID *id;
233c9496f6bSchristos     int i;
234c9496f6bSchristos     for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
235c9496f6bSchristos         id = sk_SXNETID_value(sx->ids, i);
236*4724848cSchristos         if (!ASN1_INTEGER_cmp(id->zone, zone))
237c9496f6bSchristos             return id->user;
238c9496f6bSchristos     }
239c9496f6bSchristos     return NULL;
240c9496f6bSchristos }
241