Make OBJ_NAME case insensitive.
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/7089)
Pauli
5 years ago
0 | /* | |
1 | * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. | |
2 | * | |
3 | * Licensed under the OpenSSL license (the "License"). You may not use | |
4 | * this file except in compliance with the License. You can obtain a copy | |
5 | * in the file LICENSE in the source distribution or at | |
6 | * https://www.openssl.org/source/license.html | |
7 | */ | |
8 | ||
9 | #ifndef INTERNAL_LHASH_H | |
10 | # define INTERNAL_LHASH_H | |
11 | ||
12 | unsigned long openssl_lh_strcasehash(const char *); | |
13 | ||
14 | #endif |
12 | 12 | #include <openssl/crypto.h> |
13 | 13 | #include <openssl/lhash.h> |
14 | 14 | #include <openssl/err.h> |
15 | #include "internal/ctype.h" | |
16 | #include "internal/lhash.h" | |
15 | 17 | #include "lhash_lcl.h" |
16 | 18 | |
17 | 19 | /* |
348 | 350 | return (ret >> 16) ^ ret; |
349 | 351 | } |
350 | 352 | |
353 | unsigned long openssl_lh_strcasehash(const char *c) | |
354 | { | |
355 | unsigned long ret = 0; | |
356 | long n; | |
357 | unsigned long v; | |
358 | int r; | |
359 | ||
360 | if (c == NULL || *c == '\0') | |
361 | return ret; | |
362 | ||
363 | for (n = 0x100; *c != '\0'; n += 0x100) { | |
364 | v = n | ossl_tolower(*c); | |
365 | r = (int)((v >> 2) ^ v) & 0x0f; | |
366 | ret = (ret << r) | (ret >> (32 - r)); | |
367 | ret &= 0xFFFFFFFFL; | |
368 | ret ^= v * v; | |
369 | c++; | |
370 | } | |
371 | return (ret >> 16) ^ ret; | |
372 | } | |
373 | ||
351 | 374 | unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh) |
352 | 375 | { |
353 | 376 | return lh ? lh->num_items : 0; |
16 | 16 | #include <openssl/safestack.h> |
17 | 17 | #include <openssl/e_os2.h> |
18 | 18 | #include "internal/thread_once.h" |
19 | #include "internal/lhash.h" | |
19 | 20 | #include "obj_lcl.h" |
21 | #include "e_os.h" | |
20 | 22 | |
21 | 23 | /* |
22 | 24 | * We define this wrapper for two reasons. Firstly, later versions of |
23 | 25 | * DEC C add linkage information to certain functions, which makes it |
24 | 26 | * tricky to use them as values to regular function pointers. |
25 | * Secondly, in the EDK2 build environment, the strcmp function is | |
26 | * actually an external function (AsciiStrCmp) with the Microsoft ABI, | |
27 | * so we can't transparently assign function pointers to it. | |
28 | * Arguably the latter is a stupidity of the UEFI environment, but | |
29 | * since the wrapper solves the DEC C issue too, let's just use the | |
30 | * same solution. | |
27 | * Secondly, in the EDK2 build environment, the strcasecmp function is | |
28 | * actually an external function with the Microsoft ABI, so we can't | |
29 | * transparently assign function pointers to it. | |
31 | 30 | */ |
32 | 31 | #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI) |
33 | static int obj_strcmp(const char *a, const char *b) | |
34 | { | |
35 | return strcmp(a, b); | |
32 | static int obj_strcasecmp(const char *a, const char *b) | |
33 | { | |
34 | return strcasecmp(a, b); | |
36 | 35 | } |
37 | 36 | #else |
38 | #define obj_strcmp strcmp | |
37 | #define obj_strcasecmp strcasecmp | |
39 | 38 | #endif |
40 | 39 | |
41 | 40 | /* |
110 | 109 | ret = 0; |
111 | 110 | goto out; |
112 | 111 | } |
113 | name_funcs->hash_func = OPENSSL_LH_strhash; | |
114 | name_funcs->cmp_func = obj_strcmp; | |
112 | name_funcs->hash_func = openssl_lh_strcasehash; | |
113 | name_funcs->cmp_func = obj_strcasecmp; | |
115 | 114 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE); |
116 | 115 | |
117 | 116 | push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs); |
148 | 147 | ret = sk_NAME_FUNCS_value(name_funcs_stack, |
149 | 148 | a->type)->cmp_func(a->name, b->name); |
150 | 149 | } else |
151 | ret = strcmp(a->name, b->name); | |
150 | ret = strcasecmp(a->name, b->name); | |
152 | 151 | } |
153 | 152 | return ret; |
154 | 153 | } |
163 | 162 | sk_NAME_FUNCS_value(name_funcs_stack, |
164 | 163 | a->type)->hash_func(a->name); |
165 | 164 | } else { |
166 | ret = OPENSSL_LH_strhash(a->name); | |
165 | ret = openssl_lh_strcasehash(a->name); | |
167 | 166 | } |
168 | 167 | ret ^= a->type; |
169 | 168 | return ret; |
213 | 212 | if (!OBJ_NAME_init()) |
214 | 213 | return 0; |
215 | 214 | |
216 | CRYPTO_THREAD_write_lock(obj_lock); | |
217 | ||
218 | 215 | alias = type & OBJ_NAME_ALIAS; |
219 | 216 | type &= ~OBJ_NAME_ALIAS; |
220 | 217 | |
228 | 225 | onp->alias = alias; |
229 | 226 | onp->type = type; |
230 | 227 | onp->data = data; |
228 | ||
229 | CRYPTO_THREAD_write_lock(obj_lock); | |
231 | 230 | |
232 | 231 | ret = lh_OBJ_NAME_insert(names_lh, onp); |
233 | 232 | if (ret != NULL) { |
14 | 14 | setup("test_evp"); |
15 | 15 | |
16 | 16 | my @files = ( "evpciph.txt", "evpdigest.txt", "evpencod.txt", "evpkdf.txt", |
17 | "evpmac.txt", "evppbe.txt", "evppkey.txt", "evppkey_ecc.txt" ); | |
17 | "evpmac.txt", "evppbe.txt", "evppkey.txt", "evppkey_ecc.txt", | |
18 | "evpcase.txt" ); | |
18 | 19 | |
19 | 20 | plan tests => scalar(@files); |
20 | 21 |
0 | # | |
1 | # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. | |
2 | # | |
3 | # Licensed under the OpenSSL license (the "License"). You may not use | |
4 | # this file except in compliance with the License. You can obtain a copy | |
5 | # in the file LICENSE in the source distribution or at | |
6 | # https://www.openssl.org/source/license.html | |
7 | ||
8 | # Tests start with one of these keywords | |
9 | # Cipher Decrypt Derive Digest Encoding KDF MAC PBE | |
10 | # PrivPubKeyPair Sign Verify VerifyRecover | |
11 | # and continue until a blank line. Lines starting with a pound sign, | |
12 | # like this prolog, are ignored. | |
13 | ||
14 | # These tests exercise the case insensitive handling of object names. | |
15 | # They are contrived | |
16 | ||
17 | Title = Case insensitive AES tests | |
18 | ||
19 | Cipher = Aes-128-eCb | |
20 | Key = 2B7E151628AED2A6ABF7158809CF4F3C | |
21 | Plaintext = 6BC1BEE22E409F96E93D7E117393172A | |
22 | Ciphertext = 3AD77BB40D7A3660A89ECAF32466EF97 | |
23 | ||
24 | Cipher = AeS-128-cbC | |
25 | Key = 2B7E151628AED2A6ABF7158809CF4F3C | |
26 | IV = 73BED6B8E3C1743B7116E69E22229516 | |
27 | Plaintext = F69F2445DF4F9B17AD2B417BE66C3710 | |
28 | Ciphertext = 3FF1CAA1681FAC09120ECA307586E1A7 | |
29 | ||
30 | Cipher = aES-128-CTR | |
31 | Key = AE6852F8121067CC4BF7A5765577F39E | |
32 | IV = 00000030000000000000000000000001 | |
33 | Operation = ENCRYPT | |
34 | Plaintext = 53696E676C6520626C6F636B206D7367 | |
35 | Ciphertext = E4095D4FB7A7B3792D6175A3261311B8 | |
36 | ||
37 | Cipher = AES-128-GcM | |
38 | Key = 00000000000000000000000000000000 | |
39 | IV = 000000000000000000000000 | |
40 | AAD = | |
41 | Tag = ab6e47d42cec13bdf53a67b21257bddf | |
42 | Plaintext = 00000000000000000000000000000000 | |
43 | Ciphertext = 0388dace60b6a392f328c2b971b2fe78 | |
44 | ||
45 | Title = Case insensitive digest tests | |
46 | ||
47 | Digest = Sha3-256 | |
48 | Input = "" | |
49 | Output = A7FFC6F8BF1ED76651C14756A061D662F580FF4DE43B49FA82D80A4B80F8434A | |
50 | ||
51 | Digest = shA512 | |
52 | Input = "abc" | |
53 | Output = ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f |