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