Home | History | Annotate | Line # | Download | only in test
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  *
      4  1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6  1.1  christos  * in the file LICENSE in the source distribution or at
      7  1.1  christos  * https://www.openssl.org/source/license.html
      8  1.1  christos  */
      9  1.1  christos 
     10  1.1  christos #include <openssl/evp.h>
     11  1.1  christos #include "internal/namemap.h"
     12  1.1  christos #include "testutil.h"
     13  1.1  christos 
     14  1.1  christos #define NAME1 "name1"
     15  1.1  christos #define NAME2 "name2"
     16  1.1  christos #define ALIAS1 "alias1"
     17  1.1  christos #define ALIAS1_UC "ALIAS1"
     18  1.1  christos 
     19  1.1  christos static int test_namemap_empty(void)
     20  1.1  christos {
     21  1.1  christos     OSSL_NAMEMAP *nm = NULL;
     22  1.1  christos     int ok;
     23  1.1  christos 
     24  1.1  christos     ok = TEST_int_eq(ossl_namemap_empty(NULL), 1)
     25  1.1  christos          && TEST_ptr(nm = ossl_namemap_new())
     26  1.1  christos          && TEST_int_eq(ossl_namemap_empty(nm), 1)
     27  1.1  christos          && TEST_int_ne(ossl_namemap_add_name(nm, 0, NAME1), 0)
     28  1.1  christos          && TEST_int_eq(ossl_namemap_empty(nm), 0);
     29  1.1  christos     ossl_namemap_free(nm);
     30  1.1  christos     return ok;
     31  1.1  christos }
     32  1.1  christos 
     33  1.1  christos static int test_namemap(OSSL_NAMEMAP *nm)
     34  1.1  christos {
     35  1.1  christos     int num1 = ossl_namemap_add_name(nm, 0, NAME1);
     36  1.1  christos     int num2 = ossl_namemap_add_name(nm, 0, NAME2);
     37  1.1  christos     int num3 = ossl_namemap_add_name(nm, num1, ALIAS1);
     38  1.1  christos     int num4 = ossl_namemap_add_name(nm, 0, ALIAS1_UC);
     39  1.1  christos     int check1 = ossl_namemap_name2num(nm, NAME1);
     40  1.1  christos     int check2 = ossl_namemap_name2num(nm, NAME2);
     41  1.1  christos     int check3 = ossl_namemap_name2num(nm, ALIAS1);
     42  1.1  christos     int check4 = ossl_namemap_name2num(nm, ALIAS1_UC);
     43  1.1  christos     int false1 = ossl_namemap_name2num(nm, "cookie");
     44  1.1  christos 
     45  1.1  christos     return TEST_int_ne(num1, 0)
     46  1.1  christos         && TEST_int_ne(num2, 0)
     47  1.1  christos         && TEST_int_eq(num1, num3)
     48  1.1  christos         && TEST_int_eq(num3, num4)
     49  1.1  christos         && TEST_int_eq(num1, check1)
     50  1.1  christos         && TEST_int_eq(num2, check2)
     51  1.1  christos         && TEST_int_eq(num3, check3)
     52  1.1  christos         && TEST_int_eq(num4, check4)
     53  1.1  christos         && TEST_int_eq(false1, 0);
     54  1.1  christos }
     55  1.1  christos 
     56  1.1  christos static int test_namemap_independent(void)
     57  1.1  christos {
     58  1.1  christos     OSSL_NAMEMAP *nm = ossl_namemap_new();
     59  1.1  christos     int ok = TEST_ptr(nm) && test_namemap(nm);
     60  1.1  christos 
     61  1.1  christos     ossl_namemap_free(nm);
     62  1.1  christos     return ok;
     63  1.1  christos }
     64  1.1  christos 
     65  1.1  christos static int test_namemap_stored(void)
     66  1.1  christos {
     67  1.1  christos     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
     68  1.1  christos 
     69  1.1  christos     return TEST_ptr(nm)
     70  1.1  christos         && test_namemap(nm);
     71  1.1  christos }
     72  1.1  christos 
     73  1.1  christos /*
     74  1.1  christos  * Test that EVP_get_digestbyname() will use the namemap when it can't find
     75  1.1  christos  * entries in the legacy method database.
     76  1.1  christos  */
     77  1.1  christos static int test_digestbyname(void)
     78  1.1  christos {
     79  1.1  christos     int id;
     80  1.1  christos     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
     81  1.1  christos     const EVP_MD *sha256, *foo;
     82  1.1  christos 
     83  1.1  christos     if (!TEST_ptr(nm))
     84  1.1  christos         return 0;
     85  1.1  christos     id = ossl_namemap_add_name(nm, 0, "SHA256");
     86  1.1  christos     if (!TEST_int_ne(id, 0))
     87  1.1  christos         return 0;
     88  1.1  christos     if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "foo"), id))
     89  1.1  christos         return 0;
     90  1.1  christos 
     91  1.1  christos     sha256 = EVP_get_digestbyname("SHA256");
     92  1.1  christos     if (!TEST_ptr(sha256))
     93  1.1  christos         return 0;
     94  1.1  christos     foo = EVP_get_digestbyname("foo");
     95  1.1  christos     if (!TEST_ptr_eq(sha256, foo))
     96  1.1  christos         return 0;
     97  1.1  christos 
     98  1.1  christos     return 1;
     99  1.1  christos }
    100  1.1  christos 
    101  1.1  christos /*
    102  1.1  christos  * Test that EVP_get_cipherbyname() will use the namemap when it can't find
    103  1.1  christos  * entries in the legacy method database.
    104  1.1  christos  */
    105  1.1  christos static int test_cipherbyname(void)
    106  1.1  christos {
    107  1.1  christos     int id;
    108  1.1  christos     OSSL_NAMEMAP *nm = ossl_namemap_stored(NULL);
    109  1.1  christos     const EVP_CIPHER *aes128, *bar;
    110  1.1  christos 
    111  1.1  christos     if (!TEST_ptr(nm))
    112  1.1  christos         return 0;
    113  1.1  christos     id = ossl_namemap_add_name(nm, 0, "AES-128-CBC");
    114  1.1  christos     if (!TEST_int_ne(id, 0))
    115  1.1  christos         return 0;
    116  1.1  christos     if (!TEST_int_eq(ossl_namemap_add_name(nm, id, "bar"), id))
    117  1.1  christos         return 0;
    118  1.1  christos 
    119  1.1  christos     aes128 = EVP_get_cipherbyname("AES-128-CBC");
    120  1.1  christos     if (!TEST_ptr(aes128))
    121  1.1  christos         return 0;
    122  1.1  christos     bar = EVP_get_cipherbyname("bar");
    123  1.1  christos     if (!TEST_ptr_eq(aes128, bar))
    124  1.1  christos         return 0;
    125  1.1  christos 
    126  1.1  christos     return 1;
    127  1.1  christos }
    128  1.1  christos 
    129  1.1  christos /*
    130  1.1  christos  * Test that EVP_CIPHER_is_a() responds appropriately, even for ciphers that
    131  1.1  christos  * are entirely legacy.
    132  1.1  christos  */
    133  1.1  christos static int test_cipher_is_a(void)
    134  1.1  christos {
    135  1.1  christos     EVP_CIPHER *fetched = EVP_CIPHER_fetch(NULL, "AES-256-CCM", NULL);
    136  1.1  christos     int rv = 1;
    137  1.1  christos 
    138  1.1  christos     if (!TEST_ptr(fetched))
    139  1.1  christos         return 0;
    140  1.1  christos     if (!TEST_true(EVP_CIPHER_is_a(fetched, "id-aes256-CCM"))
    141  1.1  christos         || !TEST_false(EVP_CIPHER_is_a(fetched, "AES-128-GCM")))
    142  1.1  christos         rv = 0;
    143  1.1  christos     if (!TEST_true(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-256-GCM"))
    144  1.1  christos         || !TEST_false(EVP_CIPHER_is_a(EVP_aes_256_gcm(), "AES-128-CCM")))
    145  1.1  christos         rv = 0;
    146  1.1  christos 
    147  1.1  christos     EVP_CIPHER_free(fetched);
    148  1.1  christos     return rv;
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos /*
    152  1.1  christos  * Test that EVP_MD_is_a() responds appropriately, even for MDs that are
    153  1.1  christos  * entirely legacy.
    154  1.1  christos  */
    155  1.1  christos static int test_digest_is_a(void)
    156  1.1  christos {
    157  1.1  christos     EVP_MD *fetched = EVP_MD_fetch(NULL, "SHA2-512", NULL);
    158  1.1  christos     int rv = 1;
    159  1.1  christos 
    160  1.1  christos     if (!TEST_ptr(fetched))
    161  1.1  christos         return 0;
    162  1.1  christos     if (!TEST_true(EVP_MD_is_a(fetched, "SHA512"))
    163  1.1  christos         || !TEST_false(EVP_MD_is_a(fetched, "SHA1")))
    164  1.1  christos         rv = 0;
    165  1.1  christos     if (!TEST_true(EVP_MD_is_a(EVP_sha256(), "SHA2-256"))
    166  1.1  christos         || !TEST_false(EVP_MD_is_a(EVP_sha256(), "SHA3-256")))
    167  1.1  christos         rv = 0;
    168  1.1  christos 
    169  1.1  christos     EVP_MD_free(fetched);
    170  1.1  christos     return rv;
    171  1.1  christos }
    172  1.1  christos 
    173  1.1  christos int setup_tests(void)
    174  1.1  christos {
    175  1.1  christos     ADD_TEST(test_namemap_empty);
    176  1.1  christos     ADD_TEST(test_namemap_independent);
    177  1.1  christos     ADD_TEST(test_namemap_stored);
    178  1.1  christos     ADD_TEST(test_digestbyname);
    179  1.1  christos     ADD_TEST(test_cipherbyname);
    180  1.1  christos     ADD_TEST(test_digest_is_a);
    181  1.1  christos     ADD_TEST(test_cipher_is_a);
    182  1.1  christos     return 1;
    183  1.1  christos }
    184