2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1.h>
13 #include <openssl/objects.h>
15 DEFINE_STACK_OF(ASN1_STRING_TABLE
)
17 static STACK_OF(ASN1_STRING_TABLE
) *stable
= NULL
;
18 static void st_free(ASN1_STRING_TABLE
*tbl
);
19 static int sk_table_cmp(const ASN1_STRING_TABLE
*const *a
,
20 const ASN1_STRING_TABLE
*const *b
);
23 * This is the global mask for the mbstring functions: this is use to mask
24 * out certain types (such as BMPString and UTF8String) because certain
25 * software (e.g. Netscape) has problems with them.
28 static unsigned long global_mask
= B_ASN1_UTF8STRING
;
30 void ASN1_STRING_set_default_mask(unsigned long mask
)
35 unsigned long ASN1_STRING_get_default_mask(void)
41 * This function sets the default to various "flavours" of configuration.
42 * based on an ASCII string. Currently this is:
43 * MASK:XXXX : a numerical mask value.
44 * nobmp : Don't use BMPStrings (just Printable, T61).
45 * pkix : PKIX recommendation in RFC2459.
46 * utf8only : only use UTF8Strings (RFC2459 recommendation for 2004).
47 * default: the default value, Printable, T61, BMP.
50 int ASN1_STRING_set_default_mask_asc(const char *p
)
55 if (strncmp(p
, "MASK:", 5) == 0) {
58 mask
= strtoul(p
+ 5, &end
, 0);
61 } else if (strcmp(p
, "nombstr") == 0)
62 mask
= ~((unsigned long)(B_ASN1_BMPSTRING
| B_ASN1_UTF8STRING
));
63 else if (strcmp(p
, "pkix") == 0)
64 mask
= ~((unsigned long)B_ASN1_T61STRING
);
65 else if (strcmp(p
, "utf8only") == 0)
66 mask
= B_ASN1_UTF8STRING
;
67 else if (strcmp(p
, "default") == 0)
71 ASN1_STRING_set_default_mask(mask
);
76 * The following function generates an ASN1_STRING based on limits in a
77 * table. Frequently the types and length of an ASN1_STRING are restricted by
78 * a corresponding OID. For example certificates and certificate requests.
81 ASN1_STRING
*ASN1_STRING_set_by_NID(ASN1_STRING
**out
,
82 const unsigned char *in
, int inlen
,
85 ASN1_STRING_TABLE
*tbl
;
86 ASN1_STRING
*str
= NULL
;
92 tbl
= ASN1_STRING_TABLE_get(nid
);
95 if (!(tbl
->flags
& STABLE_NO_MASK
))
97 ret
= ASN1_mbstring_ncopy(out
, in
, inlen
, inform
, mask
,
98 tbl
->minsize
, tbl
->maxsize
);
100 ret
= ASN1_mbstring_copy(out
, in
, inlen
, inform
,
101 DIRSTRING_TYPE
& global_mask
);
109 * Now the tables and helper functions for the string table:
112 #include "tbl_standard.h"
114 static int sk_table_cmp(const ASN1_STRING_TABLE
*const *a
,
115 const ASN1_STRING_TABLE
*const *b
)
117 return (*a
)->nid
- (*b
)->nid
;
120 DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE
, ASN1_STRING_TABLE
, table
);
122 static int table_cmp(const ASN1_STRING_TABLE
*a
, const ASN1_STRING_TABLE
*b
)
124 return a
->nid
- b
->nid
;
127 IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE
, ASN1_STRING_TABLE
, table
);
129 ASN1_STRING_TABLE
*ASN1_STRING_TABLE_get(int nid
)
132 ASN1_STRING_TABLE fnd
;
134 /* "stable" can be impacted by config, so load the config file first */
135 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG
, NULL
);
139 idx
= sk_ASN1_STRING_TABLE_find(stable
, &fnd
);
141 return sk_ASN1_STRING_TABLE_value(stable
, idx
);
143 return OBJ_bsearch_table(&fnd
, tbl_standard
, OSSL_NELEM(tbl_standard
));
147 * Return a string table pointer which can be modified: either directly from
148 * table or a copy of an internal value added to the table.
151 static ASN1_STRING_TABLE
*stable_get(int nid
)
153 ASN1_STRING_TABLE
*tmp
, *rv
;
155 /* Always need a string table so allocate one if NULL */
156 if (stable
== NULL
) {
157 stable
= sk_ASN1_STRING_TABLE_new(sk_table_cmp
);
161 tmp
= ASN1_STRING_TABLE_get(nid
);
162 if (tmp
!= NULL
&& tmp
->flags
& STABLE_FLAGS_MALLOC
)
164 if ((rv
= OPENSSL_zalloc(sizeof(*rv
))) == NULL
) {
165 ASN1err(ASN1_F_STABLE_GET
, ERR_R_MALLOC_FAILURE
);
168 if (!sk_ASN1_STRING_TABLE_push(stable
, rv
)) {
174 rv
->minsize
= tmp
->minsize
;
175 rv
->maxsize
= tmp
->maxsize
;
176 rv
->mask
= tmp
->mask
;
177 rv
->flags
= tmp
->flags
| STABLE_FLAGS_MALLOC
;
182 rv
->flags
= STABLE_FLAGS_MALLOC
;
187 int ASN1_STRING_TABLE_add(int nid
,
188 long minsize
, long maxsize
, unsigned long mask
,
191 ASN1_STRING_TABLE
*tmp
;
193 tmp
= stable_get(nid
);
195 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD
, ERR_R_MALLOC_FAILURE
);
199 tmp
->minsize
= minsize
;
201 tmp
->maxsize
= maxsize
;
205 tmp
->flags
= STABLE_FLAGS_MALLOC
| flags
;
209 void ASN1_STRING_TABLE_cleanup(void)
211 STACK_OF(ASN1_STRING_TABLE
) *tmp
;
217 sk_ASN1_STRING_TABLE_pop_free(tmp
, st_free
);
220 static void st_free(ASN1_STRING_TABLE
*tbl
)
222 if (tbl
->flags
& STABLE_FLAGS_MALLOC
)