Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/asn1/a_strnid.c

raw
1 /*
2 * Copyright 1999-2025 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/asn1.h>
13 #include <openssl/objects.h>
14
15 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
16 static void st_free(ASN1_STRING_TABLE *tbl);
17 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
18 const ASN1_STRING_TABLE *const *b);
19
20 /*
21 * This is the global mask for the mbstring functions: this is use to mask
22 * out certain types (such as BMPString and UTF8String) because certain
23 * software (e.g. Netscape) has problems with them.
24 */
25
26 static unsigned long global_mask = B_ASN1_UTF8STRING;
27
28 void ASN1_STRING_set_default_mask(unsigned long mask)
29 {
30 global_mask = mask;
31 }
32
33 unsigned long ASN1_STRING_get_default_mask(void)
34 {
35 return global_mask;
36 }
37
38 /*-
39 * This function sets the default to various "flavours" of configuration.
40 * based on an ASCII string. Currently this is:
41 * MASK:XXXX : a numerical mask value.
42 * default : use Printable, IA5, T61, BMP, and UTF8 string types
43 * nombstr : any string type except variable-sized BMPStrings or UTF8Strings
44 * pkix : PKIX recommendation in RFC 5280
45 * utf8only : this is the default, use UTF8Strings
46 */
47
48 int ASN1_STRING_set_default_mask_asc(const char *p)
49 {
50 unsigned long mask;
51 char *end;
52
53 if (CHECK_AND_SKIP_PREFIX(p, "MASK:")) {
54 if (*p == '\0')
55 return 0;
56 mask = strtoul(p, &end, 0);
57 if (*end)
58 return 0;
59 } else if (strcmp(p, "nombstr") == 0)
60 mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
61 else if (strcmp(p, "pkix") == 0)
62 mask = ~((unsigned long)B_ASN1_T61STRING);
63 else if (strcmp(p, "utf8only") == 0)
64 mask = B_ASN1_UTF8STRING;
65 else if (strcmp(p, "default") == 0)
66 mask = 0xFFFFFFFFL;
67 else
68 return 0;
69 ASN1_STRING_set_default_mask(mask);
70 return 1;
71 }
72
73 /*
74 * The following function generates an ASN1_STRING based on limits in a
75 * table. Frequently the types and length of an ASN1_STRING are restricted by
76 * a corresponding OID. For example certificates and certificate requests.
77 */
78
79 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
80 const unsigned char *in, int inlen,
81 int inform, int nid)
82 {
83 ASN1_STRING_TABLE *tbl;
84 ASN1_STRING *str = NULL;
85 unsigned long mask;
86 int ret;
87
88 if (out == NULL)
89 out = &str;
90 tbl = ASN1_STRING_TABLE_get(nid);
91 if (tbl != NULL) {
92 mask = tbl->mask;
93 if (!(tbl->flags & STABLE_NO_MASK))
94 mask &= global_mask;
95 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
96 tbl->minsize, tbl->maxsize);
97 } else {
98 ret = ASN1_mbstring_copy(out, in, inlen, inform,
99 DIRSTRING_TYPE & global_mask);
100 }
101 if (ret <= 0)
102 return NULL;
103 return *out;
104 }
105
106 /*
107 * Now the tables and helper functions for the string table:
108 */
109
110 #include "tbl_standard.h"
111
112 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
113 const ASN1_STRING_TABLE *const *b)
114 {
115 return (*a)->nid - (*b)->nid;
116 }
117
118 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
119
120 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
121 {
122 return a->nid - b->nid;
123 }
124
125 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
126
127 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
128 {
129 int idx;
130 ASN1_STRING_TABLE fnd;
131
132 if (nid <= 0) {
133 ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
134 return NULL;
135 }
136
137 #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
138 /* "stable" can be impacted by config, so load the config file first */
139 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
140 #endif
141
142 fnd.nid = nid;
143 if (stable != NULL) {
144 /* Ideally, this would be done under lock */
145 sk_ASN1_STRING_TABLE_sort(stable);
146 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
147 if (idx >= 0)
148 return sk_ASN1_STRING_TABLE_value(stable, idx);
149 }
150 return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
151 }
152
153 /*
154 * Return a string table pointer which can be modified: either directly from
155 * table or a copy of an internal value added to the table.
156 */
157
158 static ASN1_STRING_TABLE *stable_get(int nid)
159 {
160 ASN1_STRING_TABLE *tmp, *rv;
161
162 /* Always need a string table so allocate one if NULL */
163 if (stable == NULL) {
164 stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
165 if (stable == NULL)
166 return NULL;
167 }
168 tmp = ASN1_STRING_TABLE_get(nid);
169 if (tmp != NULL && tmp->flags & STABLE_FLAGS_MALLOC)
170 return tmp;
171 if ((rv = OPENSSL_zalloc(sizeof(*rv))) == NULL)
172 return NULL;
173 if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
174 OPENSSL_free(rv);
175 return NULL;
176 }
177 if (tmp != NULL) {
178 rv->nid = tmp->nid;
179 rv->minsize = tmp->minsize;
180 rv->maxsize = tmp->maxsize;
181 rv->mask = tmp->mask;
182 rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
183 } else {
184 rv->nid = nid;
185 rv->minsize = -1;
186 rv->maxsize = -1;
187 rv->flags = STABLE_FLAGS_MALLOC;
188 }
189 return rv;
190 }
191
192 int ASN1_STRING_TABLE_add(int nid,
193 long minsize, long maxsize, unsigned long mask,
194 unsigned long flags)
195 {
196 ASN1_STRING_TABLE *tmp;
197
198 if (nid <= 0 || (minsize >= 0 && maxsize >= 0 && minsize > maxsize)) {
199 ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_INVALID_ARGUMENT);
200 return 0;
201 }
202
203 tmp = stable_get(nid);
204 if (tmp == NULL) {
205 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
206 return 0;
207 }
208 if (minsize >= 0)
209 tmp->minsize = minsize;
210 if (maxsize >= 0)
211 tmp->maxsize = maxsize;
212 if (mask)
213 tmp->mask = mask;
214 if (flags)
215 tmp->flags = STABLE_FLAGS_MALLOC | flags;
216 return 1;
217 }
218
219 void ASN1_STRING_TABLE_cleanup(void)
220 {
221 STACK_OF(ASN1_STRING_TABLE) *tmp;
222
223 tmp = stable;
224 if (tmp == NULL)
225 return;
226 stable = NULL;
227 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
228 }
229
230 static void st_free(ASN1_STRING_TABLE *tbl)
231 {
232 if (tbl->flags & STABLE_FLAGS_MALLOC)
233 OPENSSL_free(tbl);
234 }