2 * Copyright 1999-2016 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
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
16 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
17 static void st_free(ASN1_STRING_TABLE *tbl);
18 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
19 const ASN1_STRING_TABLE *const *b);
22 * This is the global mask for the mbstring functions: this is use to mask
23 * out certain types (such as BMPString and UTF8String) because certain
24 * software (e.g. Netscape) has problems with them.
27 static unsigned long global_mask = B_ASN1_UTF8STRING;
29 void ASN1_STRING_set_default_mask(unsigned long mask)
34 unsigned long ASN1_STRING_get_default_mask(void)
40 * This function sets the default to various "flavours" of configuration.
41 * based on an ASCII string. Currently this is:
42 * MASK:XXXX : a numerical mask value.
43 * nobmp : Don't use BMPStrings (just Printable, T61).
44 * pkix : PKIX recommendation in RFC2459.
45 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
46 * default: the default value, Printable, T61, BMP.
49 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;
89 tbl = ASN1_STRING_TABLE_get(nid);
92 if (!(tbl->flags & STABLE_NO_MASK))
94 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
95 tbl->minsize, tbl->maxsize);
98 ASN1_mbstring_copy(out, in, inlen, inform,
99 DIRSTRING_TYPE & global_mask);
106 * Now the tables and helper functions for the string table:
109 /* size limits: this stuff is taken straight from RFC3280 */
111 #define ub_name 32768
112 #define ub_common_name 64
113 #define ub_locality_name 128
114 #define ub_state_name 128
115 #define ub_organization_name 64
116 #define ub_organization_unit_name 64
118 #define ub_email_address 128
119 #define ub_serial_number 64
123 #define ub_rfc822_mailbox 256
125 /* This table must be kept in NID order */
127 static const ASN1_STRING_TABLE tbl_standard[] = {
128 {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
129 {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
130 {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
131 {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
132 {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
133 {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
135 {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
137 {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
138 {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
139 {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
140 {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
141 {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
142 {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
143 {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
145 {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
146 {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
147 {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
148 {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
149 {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
150 {NID_rfc822Mailbox, 1, ub_rfc822_mailbox, B_ASN1_IA5STRING,
152 {NID_INN, 1, 12, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
153 {NID_OGRN, 1, 13, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
154 {NID_SNILS, 1, 11, B_ASN1_NUMERICSTRING, STABLE_NO_MASK}
157 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
158 const ASN1_STRING_TABLE *const *b)
160 return (*a)->nid - (*b)->nid;
163 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
165 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
167 return a->nid - b->nid;
170 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
172 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
175 ASN1_STRING_TABLE fnd;
178 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
180 return sk_ASN1_STRING_TABLE_value(stable, idx);
182 return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
186 * Return a string table pointer which can be modified: either directly from
187 * table or a copy of an internal value added to the table.
190 static ASN1_STRING_TABLE *stable_get(int nid)
192 ASN1_STRING_TABLE *tmp, *rv;
193 /* Always need a string table so allocate one if NULL */
194 if (stable == NULL) {
195 stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
199 tmp = ASN1_STRING_TABLE_get(nid);
200 if (tmp && tmp->flags & STABLE_FLAGS_MALLOC)
202 rv = OPENSSL_zalloc(sizeof(*rv));
205 if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
211 rv->minsize = tmp->minsize;
212 rv->maxsize = tmp->maxsize;
213 rv->mask = tmp->mask;
214 rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
218 rv->flags = STABLE_FLAGS_MALLOC;
223 int ASN1_STRING_TABLE_add(int nid,
224 long minsize, long maxsize, unsigned long mask,
227 ASN1_STRING_TABLE *tmp;
228 tmp = stable_get(nid);
230 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
234 tmp->minsize = minsize;
236 tmp->maxsize = maxsize;
240 tmp->flags = STABLE_FLAGS_MALLOC | flags;
244 void ASN1_STRING_TABLE_cleanup(void)
246 STACK_OF(ASN1_STRING_TABLE) *tmp;
251 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
254 static void st_free(ASN1_STRING_TABLE *tbl)
256 if (tbl->flags & STABLE_FLAGS_MALLOC)
261 #ifdef STRING_TABLE_TEST
265 ASN1_STRING_TABLE *tmp;
266 int i, last_nid = -1;
268 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
269 if (tmp->nid < last_nid) {
277 printf("Table order OK\n");
281 for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
282 printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
283 OBJ_nid2ln(tmp->nid));