]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_strnid.c
Use safer sizeof variant in malloc
[thirdparty/openssl.git] / crypto / asn1 / a_strnid.c
CommitLineData
f769ce3e 1/* a_strnid.c */
0f113f3e
MC
2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4 * 1999.
f769ce3e
DSH
5 */
6/* ====================================================================
7 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
0f113f3e 14 * notice, this list of conditions and the following disclaimer.
f769ce3e
DSH
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59
60#include <stdio.h>
61#include <ctype.h>
62#include "cryptlib.h"
63#include <openssl/asn1.h>
64#include <openssl/objects.h>
65
f769ce3e
DSH
66static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
67static void st_free(ASN1_STRING_TABLE *tbl);
0f113f3e
MC
68static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
69 const ASN1_STRING_TABLE *const *b);
b38f9f66 70
0f113f3e
MC
71/*
72 * This is the global mask for the mbstring functions: this is use to mask
73 * out certain types (such as BMPString and UTF8String) because certain
74 * software (e.g. Netscape) has problems with them.
b38f9f66
DSH
75 */
76
3009244d 77static unsigned long global_mask = B_ASN1_UTF8STRING;
b38f9f66
DSH
78
79void ASN1_STRING_set_default_mask(unsigned long mask)
80{
0f113f3e 81 global_mask = mask;
b38f9f66
DSH
82}
83
84unsigned long ASN1_STRING_get_default_mask(void)
85{
0f113f3e 86 return global_mask;
b38f9f66
DSH
87}
88
c80fd6b2
MC
89/*-
90 * This function sets the default to various "flavours" of configuration.
b38f9f66
DSH
91 * based on an ASCII string. Currently this is:
92 * MASK:XXXX : a numerical mask value.
93 * nobmp : Don't use BMPStrings (just Printable, T61).
94 * pkix : PKIX recommendation in RFC2459.
95 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
96 * default: the default value, Printable, T61, BMP.
97 */
98
7d05edd1 99int ASN1_STRING_set_default_mask_asc(const char *p)
b38f9f66 100{
0f113f3e
MC
101 unsigned long mask;
102 char *end;
103 if (!strncmp(p, "MASK:", 5)) {
104 if (!p[5])
105 return 0;
106 mask = strtoul(p + 5, &end, 0);
107 if (*end)
108 return 0;
109 } else if (!strcmp(p, "nombstr"))
110 mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
111 else if (!strcmp(p, "pkix"))
112 mask = ~((unsigned long)B_ASN1_T61STRING);
113 else if (!strcmp(p, "utf8only"))
114 mask = B_ASN1_UTF8STRING;
115 else if (!strcmp(p, "default"))
116 mask = 0xFFFFFFFFL;
117 else
118 return 0;
119 ASN1_STRING_set_default_mask(mask);
120 return 1;
b38f9f66
DSH
121}
122
0f113f3e
MC
123/*
124 * The following function generates an ASN1_STRING based on limits in a
125 * table. Frequently the types and length of an ASN1_STRING are restricted by
126 * a corresponding OID. For example certificates and certificate requests.
f769ce3e
DSH
127 */
128
0f113f3e
MC
129ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
130 const unsigned char *in, int inlen,
131 int inform, int nid)
f769ce3e 132{
0f113f3e
MC
133 ASN1_STRING_TABLE *tbl;
134 ASN1_STRING *str = NULL;
135 unsigned long mask;
136 int ret;
137 if (!out)
138 out = &str;
139 tbl = ASN1_STRING_TABLE_get(nid);
140 if (tbl) {
141 mask = tbl->mask;
142 if (!(tbl->flags & STABLE_NO_MASK))
143 mask &= global_mask;
144 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
145 tbl->minsize, tbl->maxsize);
146 } else
147 ret =
148 ASN1_mbstring_copy(out, in, inlen, inform,
149 DIRSTRING_TYPE & global_mask);
150 if (ret <= 0)
151 return NULL;
152 return *out;
f769ce3e
DSH
153}
154
0f113f3e
MC
155/*
156 * Now the tables and helper functions for the string table:
f769ce3e
DSH
157 */
158
727ef76e 159/* size limits: this stuff is taken straight from RFC3280 */
f769ce3e 160
0f113f3e
MC
161#define ub_name 32768
162#define ub_common_name 64
163#define ub_locality_name 128
164#define ub_state_name 128
165#define ub_organization_name 64
166#define ub_organization_unit_name 64
167#define ub_title 64
168#define ub_email_address 128
169#define ub_serial_number 64
f769ce3e 170
dd413410
DSH
171/* This table must be kept in NID order */
172
ebc06fba 173static const ASN1_STRING_TABLE tbl_standard[] = {
0f113f3e
MC
174 {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
175 {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
176 {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
177 {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
178 {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
179 {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
180 0},
181 {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
182 STABLE_NO_MASK},
183 {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
184 {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
185 {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
186 {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
187 {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
188 {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
189 {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
190 STABLE_NO_MASK},
191 {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
192 {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
193 {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
194 {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
195 {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
f769ce3e
DSH
196};
197
0f113f3e
MC
198static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
199 const ASN1_STRING_TABLE *const *b)
f769ce3e 200{
0f113f3e 201 return (*a)->nid - (*b)->nid;
f769ce3e
DSH
202}
203
e19106f5 204DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
babb3798
BL
205
206static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
f769ce3e 207{
0f113f3e 208 return a->nid - b->nid;
f769ce3e
DSH
209}
210
e19106f5 211IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
babb3798 212
f769ce3e 213ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
0f113f3e
MC
214{
215 int idx;
216 ASN1_STRING_TABLE fnd;
217 fnd.nid = nid;
218 if (stable) {
219 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
220 if (idx >= 0)
221 return sk_ASN1_STRING_TABLE_value(stable, idx);
222 }
b6eb9827 223 return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
0f113f3e
MC
224}
225
226/*
227 * Return a string table pointer which can be modified: either directly from
228 * table or a copy of an internal value added to the table.
d35c0ff3
DSH
229 */
230
231static ASN1_STRING_TABLE *stable_get(int nid)
0f113f3e
MC
232{
233 ASN1_STRING_TABLE *tmp, *rv;
234 /* Always need a string table so allocate one if NULL */
235 if (!stable) {
236 stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
237 if (!stable)
238 return NULL;
239 }
240 tmp = ASN1_STRING_TABLE_get(nid);
241 if (tmp && tmp->flags & STABLE_FLAGS_MALLOC)
242 return tmp;
b4faea50 243 rv = OPENSSL_malloc(sizeof(*rv));
0f113f3e
MC
244 if (!rv)
245 return NULL;
246 if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
247 OPENSSL_free(rv);
248 return NULL;
249 }
250 if (tmp) {
251 rv->nid = tmp->nid;
252 rv->minsize = tmp->minsize;
253 rv->maxsize = tmp->maxsize;
254 rv->mask = tmp->mask;
255 rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
256 } else {
257 rv->minsize = -1;
258 rv->maxsize = -1;
259 rv->mask = 0;
260 rv->flags = STABLE_FLAGS_MALLOC;
261 }
262 return rv;
263}
d35c0ff3 264
f769ce3e 265int ASN1_STRING_TABLE_add(int nid,
0f113f3e
MC
266 long minsize, long maxsize, unsigned long mask,
267 unsigned long flags)
268{
269 ASN1_STRING_TABLE *tmp;
270 tmp = stable_get(nid);
271 if (!tmp) {
272 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
273 return 0;
274 }
275 if (minsize >= 0)
276 tmp->minsize = minsize;
277 if (maxsize >= 0)
278 tmp->maxsize = maxsize;
279 if (mask)
280 tmp->mask = mask;
281 if (flags)
282 tmp->flags = STABLE_FLAGS_MALLOC | flags;
283 return 1;
284}
f769ce3e
DSH
285
286void ASN1_STRING_TABLE_cleanup(void)
287{
0f113f3e
MC
288 STACK_OF(ASN1_STRING_TABLE) *tmp;
289 tmp = stable;
290 if (!tmp)
291 return;
292 stable = NULL;
293 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
f769ce3e
DSH
294}
295
296static void st_free(ASN1_STRING_TABLE *tbl)
297{
0f113f3e
MC
298 if (tbl->flags & STABLE_FLAGS_MALLOC)
299 OPENSSL_free(tbl);
f769ce3e
DSH
300}
301
e6539fe2 302
e6539fe2
DSH
303#ifdef STRING_TABLE_TEST
304
305main()
306{
0f113f3e
MC
307 ASN1_STRING_TABLE *tmp;
308 int i, last_nid = -1;
309
b6eb9827 310 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
0f113f3e
MC
311 if (tmp->nid < last_nid) {
312 last_nid = 0;
313 break;
314 }
315 last_nid = tmp->nid;
316 }
317
318 if (last_nid != 0) {
319 printf("Table order OK\n");
320 exit(0);
321 }
322
b6eb9827 323 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
0f113f3e
MC
324 printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
325 OBJ_nid2ln(tmp->nid));
e6539fe2
DSH
326
327}
328
329#endif