/src/libressl/crypto/x509/x509_sxnet.c
Line | Count | Source (jump to first uncovered line) |
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 * |
129 | | d2i_SXNETID(SXNETID **a, const unsigned char **in, long len) |
130 | 0 | { |
131 | 0 | return (SXNETID *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
132 | 0 | &SXNETID_it); |
133 | 0 | } |
134 | | |
135 | | int |
136 | | i2d_SXNETID(SXNETID *a, unsigned char **out) |
137 | 0 | { |
138 | 0 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNETID_it); |
139 | 0 | } |
140 | | |
141 | | SXNETID * |
142 | | SXNETID_new(void) |
143 | 0 | { |
144 | 0 | return (SXNETID *)ASN1_item_new(&SXNETID_it); |
145 | 0 | } |
146 | | |
147 | | void |
148 | | SXNETID_free(SXNETID *a) |
149 | 0 | { |
150 | 0 | ASN1_item_free((ASN1_VALUE *)a, &SXNETID_it); |
151 | 0 | } |
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 * |
182 | | d2i_SXNET(SXNET **a, const unsigned char **in, long len) |
183 | 0 | { |
184 | 0 | return (SXNET *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
185 | 0 | &SXNET_it); |
186 | 0 | } |
187 | | |
188 | | int |
189 | | i2d_SXNET(SXNET *a, unsigned char **out) |
190 | 0 | { |
191 | 0 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNET_it); |
192 | 0 | } |
193 | | |
194 | | SXNET * |
195 | | SXNET_new(void) |
196 | 0 | { |
197 | 0 | return (SXNET *)ASN1_item_new(&SXNET_it); |
198 | 0 | } |
199 | | |
200 | | void |
201 | | SXNET_free(SXNET *a) |
202 | 0 | { |
203 | 0 | ASN1_item_free((ASN1_VALUE *)a, &SXNET_it); |
204 | 0 | } |
205 | | |
206 | | static int |
207 | | sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out, int indent) |
208 | 0 | { |
209 | 0 | long v; |
210 | 0 | char *tmp; |
211 | 0 | SXNETID *id; |
212 | 0 | int i; |
213 | |
|
214 | 0 | v = ASN1_INTEGER_get(sx->version); |
215 | 0 | BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", v + 1, v); |
216 | 0 | for (i = 0; i < sk_SXNETID_num(sx->ids); i++) { |
217 | 0 | id = sk_SXNETID_value(sx->ids, i); |
218 | 0 | tmp = i2s_ASN1_INTEGER(NULL, id->zone); |
219 | 0 | BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp); |
220 | 0 | free(tmp); |
221 | 0 | ASN1_STRING_print(out, id->user); |
222 | 0 | } |
223 | 0 | return 1; |
224 | 0 | } |
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 * |
234 | | sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, |
235 | | STACK_OF(CONF_VALUE) *nval) |
236 | 0 | { |
237 | 0 | CONF_VALUE *cnf; |
238 | 0 | SXNET *sx = NULL; |
239 | 0 | int i; |
240 | |
|
241 | 0 | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
242 | 0 | cnf = sk_CONF_VALUE_value(nval, i); |
243 | 0 | if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1)) |
244 | 0 | return NULL; |
245 | 0 | } |
246 | 0 | return sx; |
247 | 0 | } |
248 | | |
249 | | #endif |
250 | | |
251 | | /* Strong Extranet utility functions */ |
252 | | |
253 | | /* Add an id given the zone as an ASCII number */ |
254 | | |
255 | | int |
256 | | SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen) |
257 | 0 | { |
258 | 0 | ASN1_INTEGER *izone = NULL; |
259 | |
|
260 | 0 | if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) { |
261 | 0 | X509V3error(X509V3_R_ERROR_CONVERTING_ZONE); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 0 | return SXNET_add_id_INTEGER(psx, izone, user, userlen); |
265 | 0 | } |
266 | | |
267 | | /* Add an id given the zone as an unsigned long */ |
268 | | |
269 | | int |
270 | | SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user, |
271 | | int userlen) |
272 | 0 | { |
273 | 0 | ASN1_INTEGER *izone = NULL; |
274 | |
|
275 | 0 | if (!(izone = ASN1_INTEGER_new()) || |
276 | 0 | !ASN1_INTEGER_set(izone, lzone)) { |
277 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
278 | 0 | ASN1_INTEGER_free(izone); |
279 | 0 | return 0; |
280 | 0 | } |
281 | 0 | return SXNET_add_id_INTEGER(psx, izone, user, userlen); |
282 | 0 | } |
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 |
290 | | SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user, |
291 | | int userlen) |
292 | 0 | { |
293 | 0 | SXNET *sx = NULL; |
294 | 0 | SXNETID *id = NULL; |
295 | |
|
296 | 0 | if (!psx || !zone || !user) { |
297 | 0 | X509V3error(X509V3_R_INVALID_NULL_ARGUMENT); |
298 | 0 | return 0; |
299 | 0 | } |
300 | 0 | if (userlen == -1) |
301 | 0 | userlen = strlen(user); |
302 | 0 | if (userlen > 64) { |
303 | 0 | X509V3error(X509V3_R_USER_TOO_LONG); |
304 | 0 | return 0; |
305 | 0 | } |
306 | 0 | if (!*psx) { |
307 | 0 | if (!(sx = SXNET_new())) |
308 | 0 | goto err; |
309 | 0 | if (!ASN1_INTEGER_set(sx->version, 0)) |
310 | 0 | goto err; |
311 | 0 | *psx = sx; |
312 | 0 | } else |
313 | 0 | sx = *psx; |
314 | 0 | if (SXNET_get_id_INTEGER(sx, zone)) { |
315 | 0 | X509V3error(X509V3_R_DUPLICATE_ZONE_ID); |
316 | 0 | return 0; |
317 | 0 | } |
318 | | |
319 | 0 | if (!(id = SXNETID_new())) |
320 | 0 | goto err; |
321 | 0 | if (userlen == -1) |
322 | 0 | userlen = strlen(user); |
323 | |
|
324 | 0 | if (!ASN1_STRING_set(id->user, user, userlen)) |
325 | 0 | goto err; |
326 | 0 | if (!sk_SXNETID_push(sx->ids, id)) |
327 | 0 | goto err; |
328 | 0 | id->zone = zone; |
329 | 0 | return 1; |
330 | | |
331 | 0 | err: |
332 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
333 | 0 | SXNETID_free(id); |
334 | 0 | SXNET_free(sx); |
335 | 0 | *psx = NULL; |
336 | 0 | return 0; |
337 | 0 | } |
338 | | |
339 | | ASN1_OCTET_STRING * |
340 | | SXNET_get_id_asc(SXNET *sx, const char *zone) |
341 | 0 | { |
342 | 0 | ASN1_INTEGER *izone = NULL; |
343 | 0 | ASN1_OCTET_STRING *oct; |
344 | |
|
345 | 0 | if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) { |
346 | 0 | X509V3error(X509V3_R_ERROR_CONVERTING_ZONE); |
347 | 0 | return NULL; |
348 | 0 | } |
349 | 0 | oct = SXNET_get_id_INTEGER(sx, izone); |
350 | 0 | ASN1_INTEGER_free(izone); |
351 | 0 | return oct; |
352 | 0 | } |
353 | | |
354 | | ASN1_OCTET_STRING * |
355 | | SXNET_get_id_ulong(SXNET *sx, unsigned long lzone) |
356 | 0 | { |
357 | 0 | ASN1_INTEGER *izone = NULL; |
358 | 0 | ASN1_OCTET_STRING *oct; |
359 | |
|
360 | 0 | if (!(izone = ASN1_INTEGER_new()) || |
361 | 0 | !ASN1_INTEGER_set(izone, lzone)) { |
362 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
363 | 0 | ASN1_INTEGER_free(izone); |
364 | 0 | return NULL; |
365 | 0 | } |
366 | 0 | oct = SXNET_get_id_INTEGER(sx, izone); |
367 | 0 | ASN1_INTEGER_free(izone); |
368 | 0 | return oct; |
369 | 0 | } |
370 | | |
371 | | ASN1_OCTET_STRING * |
372 | | SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone) |
373 | 0 | { |
374 | 0 | SXNETID *id; |
375 | 0 | int i; |
376 | |
|
377 | 0 | for (i = 0; i < sk_SXNETID_num(sx->ids); i++) { |
378 | 0 | id = sk_SXNETID_value(sx->ids, i); |
379 | 0 | if (!ASN1_INTEGER_cmp(id->zone, zone)) |
380 | 0 | return id->user; |
381 | 0 | } |
382 | 0 | return NULL; |
383 | 0 | } |