Fix double free in d2i_PrivateKey().
[openssl.git] / crypto / asn1 / a_strnid.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 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 "internal/cryptlib.h"
62 #include <openssl/asn1.h>
63 #include <openssl/objects.h>
64
65 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
66 static void st_free(ASN1_STRING_TABLE *tbl);
67 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
68                         const ASN1_STRING_TABLE *const *b);
69
70 /*
71  * This is the global mask for the mbstring functions: this is use to mask
72  * out certain types (such as BMPString and UTF8String) because certain
73  * software (e.g. Netscape) has problems with them.
74  */
75
76 static unsigned long global_mask = B_ASN1_UTF8STRING;
77
78 void ASN1_STRING_set_default_mask(unsigned long mask)
79 {
80     global_mask = mask;
81 }
82
83 unsigned long ASN1_STRING_get_default_mask(void)
84 {
85     return global_mask;
86 }
87
88 /*-
89  * This function sets the default to various "flavours" of configuration.
90  * based on an ASCII string. Currently this is:
91  * MASK:XXXX : a numerical mask value.
92  * nobmp : Don't use BMPStrings (just Printable, T61).
93  * pkix : PKIX recommendation in RFC2459.
94  * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
95  * default:   the default value, Printable, T61, BMP.
96  */
97
98 int ASN1_STRING_set_default_mask_asc(const char *p)
99 {
100     unsigned long mask;
101     char *end;
102     if (strncmp(p, "MASK:", 5) == 0) {
103         if (!p[5])
104             return 0;
105         mask = strtoul(p + 5, &end, 0);
106         if (*end)
107             return 0;
108     } else if (strcmp(p, "nombstr") == 0)
109         mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
110     else if (strcmp(p, "pkix") == 0)
111         mask = ~((unsigned long)B_ASN1_T61STRING);
112     else if (strcmp(p, "utf8only") == 0)
113         mask = B_ASN1_UTF8STRING;
114     else if (strcmp(p, "default") == 0)
115         mask = 0xFFFFFFFFL;
116     else
117         return 0;
118     ASN1_STRING_set_default_mask(mask);
119     return 1;
120 }
121
122 /*
123  * The following function generates an ASN1_STRING based on limits in a
124  * table. Frequently the types and length of an ASN1_STRING are restricted by
125  * a corresponding OID. For example certificates and certificate requests.
126  */
127
128 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
129                                     const unsigned char *in, int inlen,
130                                     int inform, int nid)
131 {
132     ASN1_STRING_TABLE *tbl;
133     ASN1_STRING *str = NULL;
134     unsigned long mask;
135     int ret;
136     if (!out)
137         out = &str;
138     tbl = ASN1_STRING_TABLE_get(nid);
139     if (tbl) {
140         mask = tbl->mask;
141         if (!(tbl->flags & STABLE_NO_MASK))
142             mask &= global_mask;
143         ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
144                                   tbl->minsize, tbl->maxsize);
145     } else
146         ret =
147             ASN1_mbstring_copy(out, in, inlen, inform,
148                                DIRSTRING_TYPE & global_mask);
149     if (ret <= 0)
150         return NULL;
151     return *out;
152 }
153
154 /*
155  * Now the tables and helper functions for the string table:
156  */
157
158 /* size limits: this stuff is taken straight from RFC3280 */
159
160 #define ub_name                         32768
161 #define ub_common_name                  64
162 #define ub_locality_name                128
163 #define ub_state_name                   128
164 #define ub_organization_name            64
165 #define ub_organization_unit_name       64
166 #define ub_title                        64
167 #define ub_email_address                128
168 #define ub_serial_number                64
169
170 /* This table must be kept in NID order */
171
172 static const ASN1_STRING_TABLE tbl_standard[] = {
173     {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
174     {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
175     {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
176     {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
177     {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
178     {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
179      0},
180     {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
181      STABLE_NO_MASK},
182     {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
183     {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
184     {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
185     {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
186     {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
187     {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
188     {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
189      STABLE_NO_MASK},
190     {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
191     {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
192     {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
193     {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
194     {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
195     {NID_INN, 1, 12, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
196     {NID_OGRN, 1, 13, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
197     {NID_SNILS, 1, 11, B_ASN1_NUMERICSTRING, STABLE_NO_MASK}
198 };
199
200 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
201                         const ASN1_STRING_TABLE *const *b)
202 {
203     return (*a)->nid - (*b)->nid;
204 }
205
206 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
207
208 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
209 {
210     return a->nid - b->nid;
211 }
212
213 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
214
215 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
216 {
217     int idx;
218     ASN1_STRING_TABLE fnd;
219     fnd.nid = nid;
220     if (stable) {
221         idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
222         if (idx >= 0)
223             return sk_ASN1_STRING_TABLE_value(stable, idx);
224     }
225     return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
226 }
227
228 /*
229  * Return a string table pointer which can be modified: either directly from
230  * table or a copy of an internal value added to the table.
231  */
232
233 static ASN1_STRING_TABLE *stable_get(int nid)
234 {
235     ASN1_STRING_TABLE *tmp, *rv;
236     /* Always need a string table so allocate one if NULL */
237     if (stable == NULL) {
238         stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
239         if (stable == NULL)
240             return NULL;
241     }
242     tmp = ASN1_STRING_TABLE_get(nid);
243     if (tmp && tmp->flags & STABLE_FLAGS_MALLOC)
244         return tmp;
245     rv = OPENSSL_malloc(sizeof(*rv));
246     if (rv == NULL)
247         return NULL;
248     if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
249         OPENSSL_free(rv);
250         return NULL;
251     }
252     if (tmp) {
253         rv->nid = tmp->nid;
254         rv->minsize = tmp->minsize;
255         rv->maxsize = tmp->maxsize;
256         rv->mask = tmp->mask;
257         rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
258     } else {
259         rv->minsize = -1;
260         rv->maxsize = -1;
261         rv->mask = 0;
262         rv->flags = STABLE_FLAGS_MALLOC;
263     }
264     return rv;
265 }
266
267 int ASN1_STRING_TABLE_add(int nid,
268                           long minsize, long maxsize, unsigned long mask,
269                           unsigned long flags)
270 {
271     ASN1_STRING_TABLE *tmp;
272     tmp = stable_get(nid);
273     if (!tmp) {
274         ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
275         return 0;
276     }
277     if (minsize >= 0)
278         tmp->minsize = minsize;
279     if (maxsize >= 0)
280         tmp->maxsize = maxsize;
281     if (mask)
282         tmp->mask = mask;
283     if (flags)
284         tmp->flags = STABLE_FLAGS_MALLOC | flags;
285     return 1;
286 }
287
288 void ASN1_STRING_TABLE_cleanup(void)
289 {
290     STACK_OF(ASN1_STRING_TABLE) *tmp;
291     tmp = stable;
292     if (!tmp)
293         return;
294     stable = NULL;
295     sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
296 }
297
298 static void st_free(ASN1_STRING_TABLE *tbl)
299 {
300     if (tbl->flags & STABLE_FLAGS_MALLOC)
301         OPENSSL_free(tbl);
302 }
303
304
305 #ifdef STRING_TABLE_TEST
306
307 main()
308 {
309     ASN1_STRING_TABLE *tmp;
310     int i, last_nid = -1;
311
312     for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
313         if (tmp->nid < last_nid) {
314             last_nid = 0;
315             break;
316         }
317         last_nid = tmp->nid;
318     }
319
320     if (last_nid != 0) {
321         printf("Table order OK\n");
322         exit(0);
323     }
324
325     for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
326         printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
327                OBJ_nid2ln(tmp->nid));
328
329 }
330
331 #endif