Revert "Add some casts for %j"
[openssl.git] / crypto / asn1 / a_strnid.c
1 /*
2  * Copyright 1999-2017 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
54     if (strncmp(p, "MASK:", 5) == 0) {
55         if (!p[5])
56             return 0;
57         mask = strtoul(p + 5, &end, 0);
58         if (*end)
59             return 0;
60     } else if (strcmp(p, "nombstr") == 0)
61         mask = ~((unsigned long)(B_ASN1_BMPSTRING | B_ASN1_UTF8STRING));
62     else if (strcmp(p, "pkix") == 0)
63         mask = ~((unsigned long)B_ASN1_T61STRING);
64     else if (strcmp(p, "utf8only") == 0)
65         mask = B_ASN1_UTF8STRING;
66     else if (strcmp(p, "default") == 0)
67         mask = 0xFFFFFFFFL;
68     else
69         return 0;
70     ASN1_STRING_set_default_mask(mask);
71     return 1;
72 }
73
74 /*
75  * The following function generates an ASN1_STRING based on limits in a
76  * table. Frequently the types and length of an ASN1_STRING are restricted by
77  * a corresponding OID. For example certificates and certificate requests.
78  */
79
80 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out,
81                                     const unsigned char *in, int inlen,
82                                     int inform, int nid)
83 {
84     ASN1_STRING_TABLE *tbl;
85     ASN1_STRING *str = NULL;
86     unsigned long mask;
87     int ret;
88
89     if (out == NULL)
90         out = &str;
91     tbl = ASN1_STRING_TABLE_get(nid);
92     if (tbl != NULL) {
93         mask = tbl->mask;
94         if (!(tbl->flags & STABLE_NO_MASK))
95             mask &= global_mask;
96         ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
97                                   tbl->minsize, tbl->maxsize);
98     } else {
99         ret = ASN1_mbstring_copy(out, in, inlen, inform,
100                                  DIRSTRING_TYPE & global_mask);
101     }
102     if (ret <= 0)
103         return NULL;
104     return *out;
105 }
106
107 /*
108  * Now the tables and helper functions for the string table:
109  */
110
111 #include "tbl_standard.h"
112
113 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
114                         const ASN1_STRING_TABLE *const *b)
115 {
116     return (*a)->nid - (*b)->nid;
117 }
118
119 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
120
121 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
122 {
123     return a->nid - b->nid;
124 }
125
126 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
127
128 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
129 {
130     int idx;
131     ASN1_STRING_TABLE fnd;
132
133     fnd.nid = nid;
134     if (stable) {
135         idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
136         if (idx >= 0)
137             return sk_ASN1_STRING_TABLE_value(stable, idx);
138     }
139     return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
140 }
141
142 /*
143  * Return a string table pointer which can be modified: either directly from
144  * table or a copy of an internal value added to the table.
145  */
146
147 static ASN1_STRING_TABLE *stable_get(int nid)
148 {
149     ASN1_STRING_TABLE *tmp, *rv;
150
151     /* Always need a string table so allocate one if NULL */
152     if (stable == NULL) {
153         stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
154         if (stable == NULL)
155             return NULL;
156     }
157     tmp = ASN1_STRING_TABLE_get(nid);
158     if (tmp != NULL && tmp->flags & STABLE_FLAGS_MALLOC)
159         return tmp;
160     rv = OPENSSL_zalloc(sizeof(*rv));
161     if (rv == NULL)
162         return NULL;
163     if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
164         OPENSSL_free(rv);
165         return NULL;
166     }
167     if (tmp != NULL) {
168         rv->nid = tmp->nid;
169         rv->minsize = tmp->minsize;
170         rv->maxsize = tmp->maxsize;
171         rv->mask = tmp->mask;
172         rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
173     } else {
174         rv->nid = nid;
175         rv->minsize = -1;
176         rv->maxsize = -1;
177         rv->flags = STABLE_FLAGS_MALLOC;
178     }
179     return rv;
180 }
181
182 int ASN1_STRING_TABLE_add(int nid,
183                           long minsize, long maxsize, unsigned long mask,
184                           unsigned long flags)
185 {
186     ASN1_STRING_TABLE *tmp;
187
188     tmp = stable_get(nid);
189     if (tmp == NULL) {
190         ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
191         return 0;
192     }
193     if (minsize >= 0)
194         tmp->minsize = minsize;
195     if (maxsize >= 0)
196         tmp->maxsize = maxsize;
197     if (mask)
198         tmp->mask = mask;
199     if (flags)
200         tmp->flags = STABLE_FLAGS_MALLOC | flags;
201     return 1;
202 }
203
204 void ASN1_STRING_TABLE_cleanup(void)
205 {
206     STACK_OF(ASN1_STRING_TABLE) *tmp;
207
208     tmp = stable;
209     if (tmp == NULL)
210         return;
211     stable = NULL;
212     sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
213 }
214
215 static void st_free(ASN1_STRING_TABLE *tbl)
216 {
217     if (tbl->flags & STABLE_FLAGS_MALLOC)
218         OPENSSL_free(tbl);
219 }