3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
63 #include <openssl/asn1.h>
64 #include <openssl/objects.h>
66 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
67 static void st_free(ASN1_STRING_TABLE *tbl);
68 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
69 const ASN1_STRING_TABLE *const *b);
70 static int table_cmp(const void *a, const void *b);
73 * This is the global mask for the mbstring functions: this is use to mask
74 * out certain types (such as BMPString and UTF8String) because certain
75 * software (e.g. Netscape) has problems with them.
78 static unsigned long global_mask = B_ASN1_UTF8STRING;
80 void ASN1_STRING_set_default_mask(unsigned long mask)
85 unsigned long ASN1_STRING_get_default_mask(void)
91 * This function sets the default to various "flavours" of configuration.
92 * based on an ASCII string. Currently this is:
93 * MASK:XXXX : a numerical mask value.
94 * nobmp : Don't use BMPStrings (just Printable, T61).
95 * pkix : PKIX recommendation in RFC2459.
96 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
97 * default: the default value, Printable, T61, BMP.
100 int ASN1_STRING_set_default_mask_asc(const char *p)
104 if (!strncmp(p, "MASK:", 5)) {
107 mask = strtoul(p + 5, &end, 0);
110 } else if (!strcmp(p, "nombstr"))
111 mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
112 else if (!strcmp(p, "pkix"))
113 mask = ~((unsigned long)B_ASN1_T61STRING);
114 else if (!strcmp(p, "utf8only"))
115 mask = B_ASN1_UTF8STRING;
116 else if (!strcmp(p, "default"))
120 ASN1_STRING_set_default_mask(mask);
125 * The following function generates an ASN1_STRING based on limits in a
126 * table. Frequently the types and length of an ASN1_STRING are restricted by
127 * a corresponding OID. For example certificates and certificate requests.
130 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
131 const unsigned char *in, int inlen,
134 ASN1_STRING_TABLE *tbl;
135 ASN1_STRING *str = NULL;
140 tbl = ASN1_STRING_TABLE_get(nid);
143 if (!(tbl->flags & STABLE_NO_MASK))
145 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
146 tbl->minsize, tbl->maxsize);
149 ASN1_mbstring_copy(out, in, inlen, inform,
150 DIRSTRING_TYPE & global_mask);
157 * Now the tables and helper functions for the string table:
160 /* size limits: this stuff is taken straight from RFC3280 */
162 #define ub_name 32768
163 #define ub_common_name 64
164 #define ub_locality_name 128
165 #define ub_state_name 128
166 #define ub_organization_name 64
167 #define ub_organization_unit_name 64
169 #define ub_email_address 128
170 #define ub_serial_number 64
172 /* This table must be kept in NID order */
174 static ASN1_STRING_TABLE tbl_standard[] = {
175 {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
176 {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
177 {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
178 {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
179 {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
180 {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
182 {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
184 {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
185 {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
186 {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
187 {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
188 {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
189 {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
190 {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
192 {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
193 {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
194 {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
195 {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
196 {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
199 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
200 const ASN1_STRING_TABLE *const *b)
202 return (*a)->nid - (*b)->nid;
205 static int table_cmp(const void *a, const void *b)
207 const ASN1_STRING_TABLE *sa = a, *sb = b;
208 return sa->nid - sb->nid;
211 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
214 ASN1_STRING_TABLE *ttmp;
215 ASN1_STRING_TABLE fnd;
217 ttmp = (ASN1_STRING_TABLE *)OBJ_bsearch((char *)&fnd,
218 (char *)tbl_standard,
219 sizeof(tbl_standard) /
220 sizeof(ASN1_STRING_TABLE),
221 sizeof(ASN1_STRING_TABLE),
227 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
230 return sk_ASN1_STRING_TABLE_value(stable, idx);
233 int ASN1_STRING_TABLE_add(int nid,
234 long minsize, long maxsize, unsigned long mask,
237 ASN1_STRING_TABLE *tmp;
239 flags &= ~STABLE_FLAGS_MALLOC;
241 stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
243 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
246 if (!(tmp = ASN1_STRING_TABLE_get(nid))) {
247 tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
249 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
252 tmp->flags = flags | STABLE_FLAGS_MALLOC;
256 tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
258 tmp->minsize = minsize;
260 tmp->maxsize = maxsize;
263 sk_ASN1_STRING_TABLE_push(stable, tmp);
267 void ASN1_STRING_TABLE_cleanup(void)
269 STACK_OF(ASN1_STRING_TABLE) *tmp;
274 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
277 static void st_free(ASN1_STRING_TABLE *tbl)
279 if (tbl->flags & STABLE_FLAGS_MALLOC)
284 IMPLEMENT_STACK_OF(ASN1_STRING_TABLE)
286 #ifdef STRING_TABLE_TEST
290 ASN1_STRING_TABLE *tmp;
291 int i, last_nid = -1;
293 for (tmp = tbl_standard, i = 0;
294 i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) {
295 if (tmp->nid < last_nid) {
303 printf("Table order OK\n");
307 for (tmp = tbl_standard, i = 0;
308 i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++)
309 printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
310 OBJ_nid2ln(tmp->nid));