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