Convert asn1 selftests (a_strnid and ameth_lib) into internal test
[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 #include "tbl_standard.h"
110
111 static int sk_table_cmp(const ASN1_STRING_TABLE *const *a,
112                         const ASN1_STRING_TABLE *const *b)
113 {
114     return (*a)->nid - (*b)->nid;
115 }
116
117 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
118
119 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
120 {
121     return a->nid - b->nid;
122 }
123
124 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
125
126 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
127 {
128     int idx;
129     ASN1_STRING_TABLE fnd;
130     fnd.nid = nid;
131     if (stable) {
132         idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
133         if (idx >= 0)
134             return sk_ASN1_STRING_TABLE_value(stable, idx);
135     }
136     return OBJ_bsearch_table(&fnd, tbl_standard, OSSL_NELEM(tbl_standard));
137 }
138
139 /*
140  * Return a string table pointer which can be modified: either directly from
141  * table or a copy of an internal value added to the table.
142  */
143
144 static ASN1_STRING_TABLE *stable_get(int nid)
145 {
146     ASN1_STRING_TABLE *tmp, *rv;
147     /* Always need a string table so allocate one if NULL */
148     if (stable == NULL) {
149         stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
150         if (stable == NULL)
151             return NULL;
152     }
153     tmp = ASN1_STRING_TABLE_get(nid);
154     if (tmp && tmp->flags & STABLE_FLAGS_MALLOC)
155         return tmp;
156     rv = OPENSSL_zalloc(sizeof(*rv));
157     if (rv == NULL)
158         return NULL;
159     if (!sk_ASN1_STRING_TABLE_push(stable, rv)) {
160         OPENSSL_free(rv);
161         return NULL;
162     }
163     if (tmp) {
164         rv->nid = tmp->nid;
165         rv->minsize = tmp->minsize;
166         rv->maxsize = tmp->maxsize;
167         rv->mask = tmp->mask;
168         rv->flags = tmp->flags | STABLE_FLAGS_MALLOC;
169     } else {
170         rv->minsize = -1;
171         rv->maxsize = -1;
172         rv->flags = STABLE_FLAGS_MALLOC;
173     }
174     return rv;
175 }
176
177 int ASN1_STRING_TABLE_add(int nid,
178                           long minsize, long maxsize, unsigned long mask,
179                           unsigned long flags)
180 {
181     ASN1_STRING_TABLE *tmp;
182     tmp = stable_get(nid);
183     if (!tmp) {
184         ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
185         return 0;
186     }
187     if (minsize >= 0)
188         tmp->minsize = minsize;
189     if (maxsize >= 0)
190         tmp->maxsize = maxsize;
191     if (mask)
192         tmp->mask = mask;
193     if (flags)
194         tmp->flags = STABLE_FLAGS_MALLOC | flags;
195     return 1;
196 }
197
198 void ASN1_STRING_TABLE_cleanup(void)
199 {
200     STACK_OF(ASN1_STRING_TABLE) *tmp;
201     tmp = stable;
202     if (!tmp)
203         return;
204     stable = NULL;
205     sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
206 }
207
208 static void st_free(ASN1_STRING_TABLE *tbl)
209 {
210     if (tbl->flags & STABLE_FLAGS_MALLOC)
211         OPENSSL_free(tbl);
212 }