2 * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1.h>
13 #include <openssl/objects.h>
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);
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.
26 static unsigned long global_mask = B_ASN1_UTF8STRING;
28 void ASN1_STRING_set_default_mask(unsigned long mask)
33 unsigned long ASN1_STRING_get_default_mask(void)
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 * nobmp : Don't use BMPStrings (just Printable, T61).
43 * pkix : PKIX recommendation in RFC2459.
44 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
45 * default: the default value, Printable, T61, BMP.
48 int ASN1_STRING_set_default_mask_asc(const char *p)
53 if (strncmp(p, "MASK:", 5) == 0) {
56 mask = strtoul(p + 5, &end, 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)
69 ASN1_STRING_set_default_mask(mask);
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.
79 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
80 const unsigned char *in, int inlen,
83 ASN1_STRING_TABLE *tbl;
84 ASN1_STRING *str = NULL;
90 tbl = ASN1_STRING_TABLE_get(nid);
93 if (!(tbl->flags & STABLE_NO_MASK))
95 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
96 tbl->minsize, tbl->maxsize);
98 ret = ASN1_mbstring_copy(out, in, inlen, inform,
99 DIRSTRING_TYPE & global_mask);
107 * Now the tables and helper functions for the string table:
110 #include "tbl_standard.h"
112 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
113 const ASN1_STRING_TABLE *const *b)
115 return (*a)->nid - (*b)->nid;
118 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
120 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
122 return a->nid - b->nid;
125 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
127 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
130 ASN1_STRING_TABLE fnd;
134 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
136 return sk_ASN1_STRING_TABLE_value(stable, idx);
138 return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
142 * Return a string table pointer which can be modified: either directly from
143 * table or a copy of an internal value added to the table.
146 static ASN1_STRING_TABLE *stable_get(int nid)
148 ASN1_STRING_TABLE *tmp, *rv;
150 /* Always need a string table so allocate one if NULL */
151 if (stable == NULL) {
152 stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
156 tmp = ASN1_STRING_TABLE_get(nid);
157 if (tmp != NULL && tmp->flags & STABLE_FLAGS_MALLOC)
159 rv = OPENSSL_zalloc(sizeof(*rv));
162 if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
168 rv->minsize = tmp->minsize;
169 rv->maxsize = tmp->maxsize;
170 rv->mask = tmp->mask;
171 rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
176 rv->flags = STABLE_FLAGS_MALLOC;
181 int ASN1_STRING_TABLE_add(int nid,
182 long minsize, long maxsize, unsigned long mask,
185 ASN1_STRING_TABLE *tmp;
187 tmp = stable_get(nid);
189 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
193 tmp->minsize = minsize;
195 tmp->maxsize = maxsize;
199 tmp->flags = STABLE_FLAGS_MALLOC | flags;
203 void ASN1_STRING_TABLE_cleanup(void)
205 STACK_OF(ASN1_STRING_TABLE) *tmp;
211 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
214 static void st_free(ASN1_STRING_TABLE *tbl)
216 if (tbl->flags & STABLE_FLAGS_MALLOC)