Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_strnid.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <openssl/err.h>
11 #include <openssl/mem.h>
12 #include <openssl/obj.h>
13
14 #include "../internal.h"
15 #include "../lhash/internal.h"
16 #include "internal.h"
17
18
19 DEFINE_LHASH_OF(ASN1_STRING_TABLE)
20
21 static LHASH_OF(ASN1_STRING_TABLE) *string_tables = NULL;
22 static struct CRYPTO_STATIC_MUTEX string_tables_lock = CRYPTO_STATIC_MUTEX_INIT;
23
24 void ASN1_STRING_set_default_mask(unsigned long mask) {}
25
26 unsigned long ASN1_STRING_get_default_mask(void) { return B_ASN1_UTF8STRING; }
27
28 int ASN1_STRING_set_default_mask_asc(const char *p) { return 1; }
29
30 static const ASN1_STRING_TABLE *asn1_string_table_get(int nid);
31
32 // The following function generates an ASN1_STRING based on limits in a
33 // table. Frequently the types and length of an ASN1_STRING are restricted by
34 // a corresponding OID. For example certificates and certificate requests.
35
36 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
37 ossl_ssize_t len, int inform, int nid) {
38 ASN1_STRING *str = NULL;
39 int ret;
40 if (!out) {
41 out = &str;
42 }
43 const ASN1_STRING_TABLE *tbl = asn1_string_table_get(nid);
44 if (tbl != NULL) {
45 unsigned long mask = tbl->mask;
46 if (!(tbl->flags & STABLE_NO_MASK)) {
47 mask &= B_ASN1_UTF8STRING;
48 }
49 ret = ASN1_mbstring_ncopy(out, in, len, inform, mask, tbl->minsize,
50 tbl->maxsize);
51 } else {
52 ret = ASN1_mbstring_copy(out, in, len, inform, B_ASN1_UTF8STRING);
53 }
54 if (ret <= 0) {
55 return NULL;
56 }
57 return *out;
58 }
59
60 // Now the tables and helper functions for the string table:
61
62 // See RFC 5280.
63 #define ub_name 32768
64 #define ub_common_name 64
65 #define ub_locality_name 128
66 #define ub_state_name 128
67 #define ub_organization_name 64
68 #define ub_organization_unit_name 64
69 #define ub_email_address 128
70 #define ub_serial_number 64
71
72 // This table must be kept in NID order
73
74 static const ASN1_STRING_TABLE tbl_standard[] = {
75 {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
76 {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
77 {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
78 {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
79 {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
80 {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE,
81 0},
82 {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING,
83 STABLE_NO_MASK},
84 {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
85 {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
86 {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
87 {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
88 {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
89 {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
90 {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING,
91 STABLE_NO_MASK},
92 {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
93 {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
94 {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
95 {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
96 {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}};
97
98 static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b) {
99 if (a->nid < b->nid) {
100 return -1;
101 }
102 if (a->nid > b->nid) {
103 return 1;
104 }
105 return 0;
106 }
107
108 static int table_cmp_void(const void *a, const void *b) {
109 return table_cmp(a, b);
110 }
111
112 static uint32_t table_hash(const ASN1_STRING_TABLE *tbl) {
113 return OPENSSL_hash32(&tbl->nid, sizeof(tbl->nid));
114 }
115
116 static const ASN1_STRING_TABLE *asn1_string_table_get(int nid) {
117 ASN1_STRING_TABLE key;
118 key.nid = nid;
119 const ASN1_STRING_TABLE *tbl =
120 bsearch(&key, tbl_standard, OPENSSL_ARRAY_SIZE(tbl_standard),
121 sizeof(ASN1_STRING_TABLE), table_cmp_void);
122 if (tbl != NULL) {
123 return tbl;
124 }
125
126 CRYPTO_STATIC_MUTEX_lock_read(&string_tables_lock);
127 if (string_tables != NULL) {
128 tbl = lh_ASN1_STRING_TABLE_retrieve(string_tables, &key);
129 }
130 CRYPTO_STATIC_MUTEX_unlock_read(&string_tables_lock);
131 // Note returning |tbl| without the lock is only safe because
132 // |ASN1_STRING_TABLE_add| cannot modify or delete existing entries. If we
133 // wish to support that, this function must copy the result under a lock.
134 return tbl;
135 }
136
137 int ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize,
138 unsigned long mask, unsigned long flags) {
139 // Existing entries cannot be overwritten.
140 if (asn1_string_table_get(nid) != NULL) {
141 OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
142 return 0;
143 }
144
145 int ret = 0;
146 CRYPTO_STATIC_MUTEX_lock_write(&string_tables_lock);
147
148 if (string_tables == NULL) {
149 string_tables = lh_ASN1_STRING_TABLE_new(table_hash, table_cmp);
150 if (string_tables == NULL) {
151 goto err;
152 }
153 } else {
154 // Check again for an existing entry. One may have been added while
155 // unlocked.
156 ASN1_STRING_TABLE key;
157 key.nid = nid;
158 if (lh_ASN1_STRING_TABLE_retrieve(string_tables, &key) != NULL) {
159 OPENSSL_PUT_ERROR(ASN1, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
160 goto err;
161 }
162 }
163
164 ASN1_STRING_TABLE *tbl = OPENSSL_malloc(sizeof(ASN1_STRING_TABLE));
165 if (tbl == NULL) {
166 goto err;
167 }
168 tbl->nid = nid;
169 tbl->flags = flags;
170 tbl->minsize = minsize;
171 tbl->maxsize = maxsize;
172 tbl->mask = mask;
173 ASN1_STRING_TABLE *old_tbl;
174 if (!lh_ASN1_STRING_TABLE_insert(string_tables, &old_tbl, tbl)) {
175 OPENSSL_free(tbl);
176 goto err;
177 }
178 assert(old_tbl == NULL);
179 ret = 1;
180
181 err:
182 CRYPTO_STATIC_MUTEX_unlock_write(&string_tables_lock);
183 return ret;
184 }
185
186 void ASN1_STRING_TABLE_cleanup(void) {}
187
188 void asn1_get_string_table_for_testing(const ASN1_STRING_TABLE **out_ptr,
189 size_t *out_len) {
190 *out_ptr = tbl_standard;
191 *out_len = OPENSSL_ARRAY_SIZE(tbl_standard);
192 }