summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_strnid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/a_strnid.c')
-rw-r--r--src/lib/libcrypto/asn1/a_strnid.c287
1 files changed, 287 insertions, 0 deletions
diff --git a/src/lib/libcrypto/asn1/a_strnid.c b/src/lib/libcrypto/asn1/a_strnid.c
new file mode 100644
index 0000000000..aa49e9d7d0
--- /dev/null
+++ b/src/lib/libcrypto/asn1/a_strnid.c
@@ -0,0 +1,287 @@
1/* a_strnid.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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>
60#include <ctype.h>
61#include "cryptlib.h"
62#include <openssl/asn1.h>
63#include <openssl/objects.h>
64
65
66static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
67static void st_free(ASN1_STRING_TABLE *tbl);
68static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
69 const ASN1_STRING_TABLE * const *b);
70static int table_cmp(const void *a, const void *b);
71
72
73/* This is the global mask for the mbstring functions: this is use to
74 * mask out certain types (such as BMPString and UTF8String) because
75 * certain software (e.g. Netscape) has problems with them.
76 */
77
78static unsigned long global_mask = 0xFFFFFFFFL;
79
80void ASN1_STRING_set_default_mask(unsigned long mask)
81{
82 global_mask = mask;
83}
84
85unsigned long ASN1_STRING_get_default_mask(void)
86{
87 return global_mask;
88}
89
90/* This function sets the default to various "flavours" of configuration.
91 * based on an ASCII string. Currently this is:
92 * MASK:XXXX : a numerical mask value.
93 * nobmp : Don't use BMPStrings (just Printable, T61).
94 * pkix : PKIX recommendation in RFC2459.
95 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
96 * default: the default value, Printable, T61, BMP.
97 */
98
99int ASN1_STRING_set_default_mask_asc(char *p)
100{
101 unsigned long mask;
102 char *end;
103 if(!strncmp(p, "MASK:", 5)) {
104 if(!p[5]) return 0;
105 mask = strtoul(p + 5, &end, 0);
106 if(*end) return 0;
107 } else if(!strcmp(p, "nombstr"))
108 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING));
109 else if(!strcmp(p, "pkix"))
110 mask = ~((unsigned long)B_ASN1_T61STRING);
111 else if(!strcmp(p, "utf8only")) mask = B_ASN1_UTF8STRING;
112 else if(!strcmp(p, "default"))
113 mask = 0xFFFFFFFFL;
114 else return 0;
115 ASN1_STRING_set_default_mask(mask);
116 return 1;
117}
118
119/* The following function generates an ASN1_STRING based on limits in a table.
120 * Frequently the types and length of an ASN1_STRING are restricted by a
121 * corresponding OID. For example certificates and certificate requests.
122 */
123
124ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
125 int inlen, int inform, int nid)
126{
127 ASN1_STRING_TABLE *tbl;
128 ASN1_STRING *str = NULL;
129 unsigned long mask;
130 int ret;
131 if(!out) out = &str;
132 tbl = ASN1_STRING_TABLE_get(nid);
133 if(tbl) {
134 mask = tbl->mask;
135 if(!(tbl->flags & STABLE_NO_MASK)) mask &= global_mask;
136 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
137 tbl->minsize, tbl->maxsize);
138 } else ret = ASN1_mbstring_copy(out, in, inlen, inform, DIRSTRING_TYPE & global_mask);
139 if(ret <= 0) return NULL;
140 return *out;
141}
142
143/* Now the tables and helper functions for the string table:
144 */
145
146/* size limits: this stuff is taken straight from RFC2459 */
147
148#define ub_name 32768
149#define ub_common_name 64
150#define ub_locality_name 128
151#define ub_state_name 128
152#define ub_organization_name 64
153#define ub_organization_unit_name 64
154#define ub_title 64
155#define ub_email_address 128
156
157/* This table must be kept in NID order */
158
159static ASN1_STRING_TABLE tbl_standard[] = {
160{NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
161{NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
162{NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
163{NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
164{NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
165{NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
166{NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
167{NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
168{NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
169{NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
170{NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
171{NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
172{NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
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
180static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
181 const ASN1_STRING_TABLE * const *b)
182{
183 return (*a)->nid - (*b)->nid;
184}
185
186static int table_cmp(const void *a, const void *b)
187{
188 const ASN1_STRING_TABLE *sa = a, *sb = b;
189 return sa->nid - sb->nid;
190}
191
192ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
193{
194 int idx;
195 ASN1_STRING_TABLE *ttmp;
196 ASN1_STRING_TABLE fnd;
197 fnd.nid = nid;
198 ttmp = (ASN1_STRING_TABLE *) OBJ_bsearch((char *)&fnd,
199 (char *)tbl_standard,
200 sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE),
201 sizeof(ASN1_STRING_TABLE), table_cmp);
202 if(ttmp) return ttmp;
203 if(!stable) return NULL;
204 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
205 if(idx < 0) return NULL;
206 return sk_ASN1_STRING_TABLE_value(stable, idx);
207}
208
209int 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 ASN1err(ASN1_F_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 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD,
225 ERR_R_MALLOC_FAILURE);
226 return 0;
227 }
228 tmp->flags = flags | STABLE_FLAGS_MALLOC;
229 tmp->nid = nid;
230 new_nid = 1;
231 } else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
232 if(minsize != -1) tmp->minsize = minsize;
233 if(maxsize != -1) tmp->maxsize = maxsize;
234 tmp->mask = mask;
235 if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp);
236 return 1;
237}
238
239void ASN1_STRING_TABLE_cleanup(void)
240{
241 STACK_OF(ASN1_STRING_TABLE) *tmp;
242 tmp = stable;
243 if(!tmp) return;
244 stable = NULL;
245 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
246}
247
248static void st_free(ASN1_STRING_TABLE *tbl)
249{
250 if(tbl->flags & STABLE_FLAGS_MALLOC) OPENSSL_free(tbl);
251}
252
253
254IMPLEMENT_STACK_OF(ASN1_STRING_TABLE)
255
256#ifdef STRING_TABLE_TEST
257
258main()
259{
260 ASN1_STRING_TABLE *tmp;
261 int i, last_nid = -1;
262
263 for (tmp = tbl_standard, i = 0;
264 i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
265 {
266 if (tmp->nid < last_nid)
267 {
268 last_nid = 0;
269 break;
270 }
271 last_nid = tmp->nid;
272 }
273
274 if (last_nid != 0)
275 {
276 printf("Table order OK\n");
277 exit(0);
278 }
279
280 for (tmp = tbl_standard, i = 0;
281 i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++)
282 printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
283 OBJ_nid2ln(tmp->nid));
284
285}
286
287#endif