1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <openssl/asn1.h>
58 
59 #include <stdlib.h>  /* For bsearch */
60 #include <string.h>
61 
62 #include <openssl/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 
66 
67 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
68 static void st_free(ASN1_STRING_TABLE *tbl);
69 
70 /* This is the global mask for the mbstring functions: this is use to
71  * mask out certain types (such as BMPString and UTF8String) because
72  * certain software (e.g. Netscape) has problems with them.
73  */
74 
75 static unsigned long global_mask = B_ASN1_UTF8STRING;
76 
ASN1_STRING_set_default_mask(unsigned long mask)77 void ASN1_STRING_set_default_mask(unsigned long mask)
78 {
79 	global_mask = mask;
80 }
81 
ASN1_STRING_get_default_mask(void)82 unsigned long ASN1_STRING_get_default_mask(void)
83 {
84 	return global_mask;
85 }
86 
87 /* This function sets the default to various "flavours" of configuration.
88  * based on an ASCII string. Currently this is:
89  * MASK:XXXX : a numerical mask value.
90  * nobmp : Don't use BMPStrings (just Printable, T61).
91  * pkix : PKIX recommendation in RFC2459.
92  * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
93  * default:   the default value, Printable, T61, BMP.
94  */
95 
ASN1_STRING_set_default_mask_asc(const char * p)96 int ASN1_STRING_set_default_mask_asc(const char *p)
97 {
98 	unsigned long mask;
99 	char *end;
100 	if(!strncmp(p, "MASK:", 5)) {
101 		if(!p[5]) return 0;
102 		mask = strtoul(p + 5, &end, 0);
103 		if(*end) return 0;
104 	} else if(!strcmp(p, "nombstr"))
105 			 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING));
106 	else if(!strcmp(p, "pkix"))
107 			mask = ~((unsigned long)B_ASN1_T61STRING);
108 	else if(!strcmp(p, "utf8only")) mask = B_ASN1_UTF8STRING;
109 	else if(!strcmp(p, "default"))
110 	    mask = 0xFFFFFFFFL;
111 	else return 0;
112 	ASN1_STRING_set_default_mask(mask);
113 	return 1;
114 }
115 
116 /* The following function generates an ASN1_STRING based on limits in a table.
117  * Frequently the types and length of an ASN1_STRING are restricted by a
118  * corresponding OID. For example certificates and certificate requests.
119  */
120 
ASN1_STRING_set_by_NID(ASN1_STRING ** out,const unsigned char * in,int inlen,int inform,int nid)121 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
122 					int inlen, int inform, int nid)
123 {
124 	ASN1_STRING_TABLE *tbl;
125 	ASN1_STRING *str = NULL;
126 	unsigned long mask;
127 	int ret;
128 	if(!out) out = &str;
129 	tbl = ASN1_STRING_TABLE_get(nid);
130 	if(tbl) {
131 		mask = tbl->mask;
132 		if(!(tbl->flags & STABLE_NO_MASK)) mask &= global_mask;
133 		ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
134 					tbl->minsize, tbl->maxsize);
135 	} else ret = ASN1_mbstring_copy(out, in, inlen, inform, DIRSTRING_TYPE & global_mask);
136 	if(ret <= 0) return NULL;
137 	return *out;
138 }
139 
140 /* Now the tables and helper functions for the string table:
141  */
142 
143 /* size limits: this stuff is taken straight from RFC3280 */
144 
145 #define ub_name				32768
146 #define ub_common_name			64
147 #define ub_locality_name		128
148 #define ub_state_name			128
149 #define ub_organization_name		64
150 #define ub_organization_unit_name	64
151 #define ub_title			64
152 #define ub_email_address		128
153 #define ub_serial_number		64
154 
155 
156 /* This table must be kept in NID order */
157 
158 static const ASN1_STRING_TABLE tbl_standard[] = {
159 {NID_commonName,		1, ub_common_name, DIRSTRING_TYPE, 0},
160 {NID_countryName,		2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
161 {NID_localityName,		1, ub_locality_name, DIRSTRING_TYPE, 0},
162 {NID_stateOrProvinceName,	1, ub_state_name, DIRSTRING_TYPE, 0},
163 {NID_organizationName,		1, ub_organization_name, DIRSTRING_TYPE, 0},
164 {NID_organizationalUnitName,	1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
165 {NID_pkcs9_emailAddress,	1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
166 {NID_pkcs9_unstructuredName,	1, -1, PKCS9STRING_TYPE, 0},
167 {NID_pkcs9_challengePassword,	1, -1, PKCS9STRING_TYPE, 0},
168 {NID_pkcs9_unstructuredAddress,	1, -1, DIRSTRING_TYPE, 0},
169 {NID_givenName,			1, ub_name, DIRSTRING_TYPE, 0},
170 {NID_surname,			1, ub_name, DIRSTRING_TYPE, 0},
171 {NID_initials,			1, ub_name, DIRSTRING_TYPE, 0},
172 {NID_serialNumber,		1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
173 {NID_friendlyName,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
174 {NID_name,			1, ub_name, DIRSTRING_TYPE, 0},
175 {NID_dnQualifier,		-1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
176 {NID_domainComponent,		1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
177 {NID_ms_csp_name,		-1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
178 };
179 
sk_table_cmp(const ASN1_STRING_TABLE ** a,const ASN1_STRING_TABLE ** b)180 static int sk_table_cmp(const ASN1_STRING_TABLE **a,
181 			const ASN1_STRING_TABLE **b)
182 {
183 	return (*a)->nid - (*b)->nid;
184 }
185 
table_cmp(const void * in_a,const void * in_b)186 static int table_cmp(const void *in_a, const void *in_b)
187 {
188 	const ASN1_STRING_TABLE *a = in_a;
189 	const ASN1_STRING_TABLE *b = in_b;
190 	return a->nid - b->nid;
191 }
192 
ASN1_STRING_TABLE_get(int nid)193 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
194 {
195 	int found;
196 	size_t idx;
197 	ASN1_STRING_TABLE *ttmp;
198 	ASN1_STRING_TABLE fnd;
199 	fnd.nid = nid;
200 
201 	ttmp = bsearch(&fnd, tbl_standard, sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE), sizeof(ASN1_STRING_TABLE), table_cmp);
202 	if(ttmp) return ttmp;
203 	if(!stable) return NULL;
204 	found = sk_ASN1_STRING_TABLE_find(stable, &idx, &fnd);
205 	if (!found) return NULL;
206 	return sk_ASN1_STRING_TABLE_value(stable, idx);
207 }
208 
ASN1_STRING_TABLE_add(int nid,long minsize,long maxsize,unsigned long mask,unsigned long flags)209 int ASN1_STRING_TABLE_add(int nid,
210 		 long minsize, long maxsize, unsigned long mask,
211 				unsigned long flags)
212 {
213 	ASN1_STRING_TABLE *tmp;
214 	char new_nid = 0;
215 	flags &= ~STABLE_FLAGS_MALLOC;
216 	if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
217 	if(!stable) {
218 		OPENSSL_PUT_ERROR(ASN1, ASN1_STRING_TABLE_add,  ERR_R_MALLOC_FAILURE);
219 		return 0;
220 	}
221 	if(!(tmp = ASN1_STRING_TABLE_get(nid))) {
222 		tmp = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
223 		if(!tmp) {
224 			OPENSSL_PUT_ERROR(ASN1, ASN1_STRING_TABLE_add,  ERR_R_MALLOC_FAILURE);
225 			return 0;
226 		}
227 		tmp->flags = flags | STABLE_FLAGS_MALLOC;
228 		tmp->nid = nid;
229 		new_nid = 1;
230 	} else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
231 	if(minsize != -1) tmp->minsize = minsize;
232 	if(maxsize != -1) tmp->maxsize = maxsize;
233 	tmp->mask = mask;
234 	if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp);
235 	return 1;
236 }
237 
ASN1_STRING_TABLE_cleanup(void)238 void ASN1_STRING_TABLE_cleanup(void)
239 {
240 	STACK_OF(ASN1_STRING_TABLE) *tmp;
241 	tmp = stable;
242 	if(!tmp) return;
243 	stable = NULL;
244 	sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
245 }
246 
st_free(ASN1_STRING_TABLE * tbl)247 static void st_free(ASN1_STRING_TABLE *tbl)
248 {
249 	if(tbl->flags & STABLE_FLAGS_MALLOC) OPENSSL_free(tbl);
250 }
251 
252 
253 #ifdef STRING_TABLE_TEST
254 
255 int
main(void)256 main(void)
257 {
258 	ASN1_STRING_TABLE *tmp;
259 	int i, last_nid = -1;
260 
261 	for (tmp = tbl_standard, i = 0;
262 		i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
263 		{
264 			if (tmp->nid < last_nid)
265 				{
266 				last_nid = 0;
267 				break;
268 				}
269 			last_nid = tmp->nid;
270 		}
271 
272 	if (last_nid != 0)
273 		{
274 		printf("Table order OK\n");
275 		exit(0);
276 		}
277 
278 	for (tmp = tbl_standard, i = 0;
279 		i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
280 			printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
281 							OBJ_nid2ln(tmp->nid));
282 
283 	return 0;
284 }
285 
286 #endif
287