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