Fix ASN1_STRING_to_UTF8 could not convert NumericString
[openssl.git] / crypto / asn1 / a_strnid.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <ctype.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/asn1.h>
14 #include <openssl/objects.h>
15
16 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
17 static void st_free(ASN1_STRING_TABLE *tbl);
18 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
19                         const ASN1_STRING_TABLE *const *b);
20
21 /*
22  * This is the global mask for the mbstring functions: this is use to mask
23  * out certain types (such as BMPString and UTF8String) because certain
24  * software (e.g. Netscape) has problems with them.
25  */
26
27 static unsigned long global_mask = B_ASN1_UTF8STRING;
28
29 void ASN1_STRING_set_default_mask(unsigned long mask)
30 {
31     global_mask = mask;
32 }
33
34 unsigned long ASN1_STRING_get_default_mask(void)
35 {
36     return global_mask;
37 }
38
39 /*-
40  * This function sets the default to various "flavours" of configuration.
41  * based on an ASCII string. Currently this is:
42  * MASK:XXXX : a numerical mask value.
43  * nobmp : Don't use BMPStrings (just Printable, T61).
44  * pkix : PKIX recommendation in RFC2459.
45  * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
46  * default:   the default value, Printable, T61, BMP.
47  */
48
49 int ASN1_STRING_set_default_mask_asc(const char *p)
50 {
51     unsigned long mask;
52     char *end;
53     if (strncmp(p, "MASK:", 5) == 0) {
54         if (!p[5])
55             return 0;
56         mask = strtoul(p + 5, &end, 0);
57         if (*end)
58             return 0;
59     } else if (strcmp(p, "nombstr") == 0)
60         mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
61     else if (strcmp(p, "pkix") == 0)
62         mask = ~((unsigned long)B_ASN1_T61STRING);
63     else if (strcmp(p, "utf8only") == 0)
64         mask = B_ASN1_UTF8STRING;
65     else if (strcmp(p, "default") == 0)
66         mask = 0xFFFFFFFFL;
67     else
68         return 0;
69     ASN1_STRING_set_default_mask(mask);
70     return 1;
71 }
72
73 /*
74  * The following function generates an ASN1_STRING based on limits in a
75  * table. Frequently the types and length of an ASN1_STRING are restricted by
76  * a corresponding OID. For example certificates and certificate requests.
77  */
78
79 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
80                                     const unsigned char *in, int inlen,
81                                     int inform, int nid)
82 {
83     ASN1_STRING_TABLE *tbl;
84     ASN1_STRING *str = NULL;
85     unsigned long mask;
86     int ret;
87     if (!out)
88         out = &str;
89     tbl = ASN1_STRING_TABLE_get(nid);
90     if (tbl) {
91         mask = tbl->mask;
92         if (!(tbl->flags & STABLE_NO_MASK))
93             mask &= global_mask;
94         ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
95                                   tbl->minsize, tbl->maxsize);
96     } else
97         ret =
98             ASN1_mbstring_copy(out, in, inlen, inform,
99                                DIRSTRING_TYPE & global_mask);
100     if (ret <= 0)
101         return NULL;
102     return *out;
103 }
104
105 /*
106  * Now the tables and helper functions for the string table:
107  */
108
109 /* size limits: this stuff is taken straight from RFC3280 */
110
111 #define ub_name                         32768
112 #define ub_common_name                  64
113 #define ub_locality_name                128
114 #define ub_state_name                   128
115 #define ub_organization_name            64
116 #define ub_organization_unit_name       64
117 #define ub_title                        64
118 #define ub_email_address                128
119 #define ub_serial_number                64
120
121 /* From RFC4524 */
122
123 #define ub_rfc822_mailbox               256
124
125 /* This table must be kept in NID order */
126
127 static const ASN1_STRING_TABLE tbl_standard[] = {
128     {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
129     {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
130     {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
131     {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
132     {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
133     {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
134      0},
135     {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
136      STABLE_NO_MASK},
137     {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
138     {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
139     {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
140     {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
141     {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
142     {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
143     {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
144      STABLE_NO_MASK},
145     {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
146     {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
147     {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
148     {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
149     {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
150     {NID_rfc822Mailbox, 1, ub_rfc822_mailbox, B_ASN1_IA5STRING,
151      STABLE_NO_MASK},
152     {NID_INN, 1, 12, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
153     {NID_OGRN, 1, 13, B_ASN1_NUMERICSTRING, STABLE_NO_MASK},
154     {NID_SNILS, 1, 11, B_ASN1_NUMERICSTRING, STABLE_NO_MASK}
155 };
156
157 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
158                         const ASN1_STRING_TABLE *const *b)
159 {
160     return (*a)->nid - (*b)->nid;
161 }
162
163 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
164
165 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
166 {
167     return a->nid - b->nid;
168 }
169
170 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
171
172 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
173 {
174     int idx;
175     ASN1_STRING_TABLE fnd;
176     fnd.nid = nid;
177     if (stable) {
178         idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
179         if (idx >= 0)
180             return sk_ASN1_STRING_TABLE_value(stable, idx);
181     }
182     return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
183 }
184
185 /*
186  * Return a string table pointer which can be modified: either directly from
187  * table or a copy of an internal value added to the table.
188  */
189
190 static ASN1_STRING_TABLE *stable_get(int nid)
191 {
192     ASN1_STRING_TABLE *tmp, *rv;
193     /* Always need a string table so allocate one if NULL */
194     if (stable == NULL) {
195         stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
196         if (stable == NULL)
197             return NULL;
198     }
199     tmp = ASN1_STRING_TABLE_get(nid);
200     if (tmp && tmp->flags & STABLE_FLAGS_MALLOC)
201         return tmp;
202     rv = OPENSSL_zalloc(sizeof(*rv));
203     if (rv == NULL)
204         return NULL;
205     if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
206         OPENSSL_free(rv);
207         return NULL;
208     }
209     if (tmp) {
210         rv->nid = tmp->nid;
211         rv->minsize = tmp->minsize;
212         rv->maxsize = tmp->maxsize;
213         rv->mask = tmp->mask;
214         rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
215     } else {
216         rv->minsize = -1;
217         rv->maxsize = -1;
218         rv->flags = STABLE_FLAGS_MALLOC;
219     }
220     return rv;
221 }
222
223 int ASN1_STRING_TABLE_add(int nid,
224                           long minsize, long maxsize, unsigned long mask,
225                           unsigned long flags)
226 {
227     ASN1_STRING_TABLE *tmp;
228     tmp = stable_get(nid);
229     if (!tmp) {
230         ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
231         return 0;
232     }
233     if (minsize >= 0)
234         tmp->minsize = minsize;
235     if (maxsize >= 0)
236         tmp->maxsize = maxsize;
237     if (mask)
238         tmp->mask = mask;
239     if (flags)
240         tmp->flags = STABLE_FLAGS_MALLOC | flags;
241     return 1;
242 }
243
244 void ASN1_STRING_TABLE_cleanup(void)
245 {
246     STACK_OF(ASN1_STRING_TABLE) *tmp;
247     tmp = stable;
248     if (!tmp)
249         return;
250     stable = NULL;
251     sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
252 }
253
254 static void st_free(ASN1_STRING_TABLE *tbl)
255 {
256     if (tbl->flags & STABLE_FLAGS_MALLOC)
257         OPENSSL_free(tbl);
258 }
259
260
261 #ifdef STRING_TABLE_TEST
262
263 main()
264 {
265     ASN1_STRING_TABLE *tmp;
266     int i, last_nid = -1;
267
268     for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) {
269         if (tmp->nid < last_nid) {
270             last_nid = 0;
271             break;
272         }
273         last_nid = tmp->nid;
274     }
275
276     if (last_nid != 0) {
277         printf("Table order OK\n");
278         exit(0);
279     }
280
281     for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++)
282         printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
283                OBJ_nid2ln(tmp->nid));
284
285 }
286
287 #endif