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