/src/openssl/crypto/x509/v3_sxnet.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1999-2024 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  |  | /* Support for Thawte strong extranet extension */  | 
19  |  |  | 
20  |  | #define SXNET_TEST  | 
21  |  |  | 
22  |  | static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,  | 
23  |  |                      int indent);  | 
24  |  | #ifdef SXNET_TEST  | 
25  |  | static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,  | 
26  |  |                         STACK_OF(CONF_VALUE) *nval);  | 
27  |  | #endif  | 
28  |  | const X509V3_EXT_METHOD ossl_v3_sxnet = { | 
29  |  |     NID_sxnet, X509V3_EXT_MULTILINE, ASN1_ITEM_ref(SXNET),  | 
30  |  |     0, 0, 0, 0,  | 
31  |  |     0, 0,  | 
32  |  |     0,  | 
33  |  | #ifdef SXNET_TEST  | 
34  |  |     (X509V3_EXT_V2I)sxnet_v2i,  | 
35  |  | #else  | 
36  |  |     0,  | 
37  |  | #endif  | 
38  |  |     (X509V3_EXT_I2R)sxnet_i2r,  | 
39  |  |     0,  | 
40  |  |     NULL  | 
41  |  | };  | 
42  |  |  | 
43  |  | ASN1_SEQUENCE(SXNETID) = { | 
44  |  |         ASN1_SIMPLE(SXNETID, zone, ASN1_INTEGER),  | 
45  |  |         ASN1_SIMPLE(SXNETID, user, ASN1_OCTET_STRING)  | 
46  |  | } ASN1_SEQUENCE_END(SXNETID)  | 
47  |  |  | 
48  |  | IMPLEMENT_ASN1_FUNCTIONS(SXNETID)  | 
49  |  |  | 
50  |  | ASN1_SEQUENCE(SXNET) = { | 
51  |  |         ASN1_SIMPLE(SXNET, version, ASN1_INTEGER),  | 
52  |  |         ASN1_SEQUENCE_OF(SXNET, ids, SXNETID)  | 
53  |  | } ASN1_SEQUENCE_END(SXNET)  | 
54  |  |  | 
55  |  | IMPLEMENT_ASN1_FUNCTIONS(SXNET)  | 
56  |  |  | 
57  |  | static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,  | 
58  |  |                      int indent)  | 
59  | 0  | { | 
60  | 0  |     int64_t v;  | 
61  | 0  |     char *tmp;  | 
62  | 0  |     SXNETID *id;  | 
63  | 0  |     int i;  | 
64  |  |  | 
65  |  |     /*  | 
66  |  |      * Since we add 1 to the version number to display it, we don't support  | 
67  |  |      * LONG_MAX since that would cause on overflow.  | 
68  |  |      */  | 
69  | 0  |     if (!ASN1_INTEGER_get_int64(&v, sx->version)  | 
70  | 0  |             || v >= LONG_MAX  | 
71  | 0  |             || v < LONG_MIN) { | 
72  | 0  |         BIO_printf(out, "%*sVersion: <unsupported>", indent, "");  | 
73  | 0  |     } else { | 
74  | 0  |         long vl = (long)v;  | 
75  |  | 
  | 
76  | 0  |         BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", vl + 1, vl);  | 
77  | 0  |     }  | 
78  | 0  |     for (i = 0; i < sk_SXNETID_num(sx->ids); i++) { | 
79  | 0  |         id = sk_SXNETID_value(sx->ids, i);  | 
80  | 0  |         tmp = i2s_ASN1_INTEGER(NULL, id->zone);  | 
81  | 0  |         if (tmp == NULL)  | 
82  | 0  |             return 0;  | 
83  | 0  |         BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);  | 
84  | 0  |         OPENSSL_free(tmp);  | 
85  | 0  |         ASN1_STRING_print(out, id->user);  | 
86  | 0  |     }  | 
87  | 0  |     return 1;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | #ifdef SXNET_TEST  | 
91  |  |  | 
92  |  | /*  | 
93  |  |  * NBB: this is used for testing only. It should *not* be used for anything  | 
94  |  |  * else because it will just take static IDs from the configuration file and  | 
95  |  |  * they should really be separate values for each user.  | 
96  |  |  */  | 
97  |  |  | 
98  |  | static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,  | 
99  |  |                         STACK_OF(CONF_VALUE) *nval)  | 
100  | 0  | { | 
101  | 0  |     CONF_VALUE *cnf;  | 
102  | 0  |     SXNET *sx = NULL;  | 
103  | 0  |     int i;  | 
104  | 0  |     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { | 
105  | 0  |         cnf = sk_CONF_VALUE_value(nval, i);  | 
106  | 0  |         if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1)) { | 
107  | 0  |             SXNET_free(sx);  | 
108  | 0  |             return NULL;  | 
109  | 0  |   }  | 
110  | 0  |     }  | 
111  | 0  |     return sx;  | 
112  | 0  | }  | 
113  |  |  | 
114  |  | #endif  | 
115  |  |  | 
116  |  | /* Strong Extranet utility functions */  | 
117  |  |  | 
118  |  | /* Add an id given the zone as an ASCII number */  | 
119  |  |  | 
120  |  | int SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen)  | 
121  | 0  | { | 
122  | 0  |     ASN1_INTEGER *izone;  | 
123  |  | 
  | 
124  | 0  |     if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) { | 
125  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CONVERTING_ZONE);  | 
126  | 0  |         return 0;  | 
127  | 0  |     }  | 
128  | 0  |     if (!SXNET_add_id_INTEGER(psx, izone, user, userlen)) { | 
129  | 0  |         ASN1_INTEGER_free(izone);  | 
130  | 0  |         return 0;  | 
131  | 0  |     }  | 
132  | 0  |     return 1;  | 
133  | 0  | }  | 
134  |  |  | 
135  |  | /* Add an id given the zone as an unsigned long */  | 
136  |  |  | 
137  |  | int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user,  | 
138  |  |                        int userlen)  | 
139  | 0  | { | 
140  | 0  |     ASN1_INTEGER *izone;  | 
141  |  | 
  | 
142  | 0  |     if ((izone = ASN1_INTEGER_new()) == NULL  | 
143  | 0  |         || !ASN1_INTEGER_set(izone, lzone)) { | 
144  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
145  | 0  |         ASN1_INTEGER_free(izone);  | 
146  | 0  |         return 0;  | 
147  | 0  |     }  | 
148  | 0  |     if (!SXNET_add_id_INTEGER(psx, izone, user, userlen)) { | 
149  | 0  |         ASN1_INTEGER_free(izone);  | 
150  | 0  |         return 0;  | 
151  | 0  |     }  | 
152  | 0  |     return 1;  | 
153  | 0  | }  | 
154  |  |  | 
155  |  | /*  | 
156  |  |  * Add an id given the zone as an ASN1_INTEGER. Note this version uses the  | 
157  |  |  * passed integer and doesn't make a copy so don't free it up afterwards.  | 
158  |  |  */  | 
159  |  |  | 
160  |  | int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user,  | 
161  |  |                          int userlen)  | 
162  | 0  | { | 
163  | 0  |     SXNET *sx = NULL;  | 
164  | 0  |     SXNETID *id = NULL;  | 
165  |  | 
  | 
166  | 0  |     if (psx == NULL || zone == NULL || user == NULL) { | 
167  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_ARGUMENT);  | 
168  | 0  |         return 0;  | 
169  | 0  |     }  | 
170  | 0  |     if (userlen == -1)  | 
171  | 0  |         userlen = strlen(user);  | 
172  | 0  |     if (userlen > 64) { | 
173  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_USER_TOO_LONG);  | 
174  | 0  |         return 0;  | 
175  | 0  |     }  | 
176  | 0  |     if (*psx == NULL) { | 
177  | 0  |         if ((sx = SXNET_new()) == NULL) { | 
178  | 0  |             ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
179  | 0  |             goto err;  | 
180  | 0  |         }  | 
181  | 0  |         if (!ASN1_INTEGER_set(sx->version, 0)) { | 
182  | 0  |             ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
183  | 0  |             goto err;  | 
184  | 0  |         }  | 
185  | 0  |     } else  | 
186  | 0  |         sx = *psx;  | 
187  | 0  |     if (SXNET_get_id_INTEGER(sx, zone)) { | 
188  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_DUPLICATE_ZONE_ID);  | 
189  | 0  |         if (*psx == NULL)  | 
190  | 0  |             SXNET_free(sx);  | 
191  | 0  |         return 0;  | 
192  | 0  |     }  | 
193  |  |  | 
194  | 0  |     if ((id = SXNETID_new()) == NULL) { | 
195  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
196  | 0  |         goto err;  | 
197  | 0  |     }  | 
198  |  |  | 
199  | 0  |     if (!ASN1_OCTET_STRING_set(id->user, (const unsigned char *)user, userlen)){ | 
200  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
201  | 0  |         goto err;  | 
202  | 0  |     }  | 
203  | 0  |     if (!sk_SXNETID_push(sx->ids, id)) { | 
204  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);  | 
205  | 0  |         goto err;  | 
206  | 0  |     }  | 
207  | 0  |     ASN1_INTEGER_free(id->zone);  | 
208  | 0  |     id->zone = zone;  | 
209  | 0  |     *psx = sx;  | 
210  | 0  |     return 1;  | 
211  |  |  | 
212  | 0  |  err:  | 
213  | 0  |     SXNETID_free(id);  | 
214  | 0  |     if (*psx == NULL)  | 
215  | 0  |         SXNET_free(sx);  | 
216  | 0  |     return 0;  | 
217  | 0  | }  | 
218  |  |  | 
219  |  | ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, const char *zone)  | 
220  | 0  | { | 
221  | 0  |     ASN1_INTEGER *izone;  | 
222  | 0  |     ASN1_OCTET_STRING *oct;  | 
223  |  | 
  | 
224  | 0  |     if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) { | 
225  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CONVERTING_ZONE);  | 
226  | 0  |         return NULL;  | 
227  | 0  |     }  | 
228  | 0  |     oct = SXNET_get_id_INTEGER(sx, izone);  | 
229  | 0  |     ASN1_INTEGER_free(izone);  | 
230  | 0  |     return oct;  | 
231  | 0  | }  | 
232  |  |  | 
233  |  | ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)  | 
234  | 0  | { | 
235  | 0  |     ASN1_INTEGER *izone;  | 
236  | 0  |     ASN1_OCTET_STRING *oct;  | 
237  |  | 
  | 
238  | 0  |     if ((izone = ASN1_INTEGER_new()) == NULL  | 
239  | 0  |         || !ASN1_INTEGER_set(izone, lzone)) { | 
240  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
241  | 0  |         ASN1_INTEGER_free(izone);  | 
242  | 0  |         return NULL;  | 
243  | 0  |     }  | 
244  | 0  |     oct = SXNET_get_id_INTEGER(sx, izone);  | 
245  | 0  |     ASN1_INTEGER_free(izone);  | 
246  | 0  |     return oct;  | 
247  | 0  | }  | 
248  |  |  | 
249  |  | ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)  | 
250  | 0  | { | 
251  | 0  |     SXNETID *id;  | 
252  | 0  |     int i;  | 
253  | 0  |     for (i = 0; i < sk_SXNETID_num(sx->ids); i++) { | 
254  | 0  |         id = sk_SXNETID_value(sx->ids, i);  | 
255  | 0  |         if (!ASN1_INTEGER_cmp(id->zone, zone))  | 
256  | 0  |             return id->user;  | 
257  | 0  |     }  | 
258  | 0  |     return NULL;  | 
259  | 0  | }  |