Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2016-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");
      5      1.1  christos  * you may not use this file except in compliance with the License.
      6      1.1  christos  * You may obtain a copy of the License at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  * or in the file LICENSE in the source distribution.
      9      1.1  christos  */
     10      1.1  christos 
     11      1.1  christos #include <stdio.h>
     12      1.1  christos #include <string.h>
     13      1.1  christos 
     14      1.1  christos #include <openssl/opensslconf.h>
     15      1.1  christos #include <openssl/err.h>
     16      1.1  christos #include <openssl/e_os2.h>
     17      1.1  christos #include <openssl/ssl.h>
     18      1.1  christos #include <openssl/ssl3.h>
     19      1.1  christos #include <openssl/tls1.h>
     20      1.1  christos 
     21      1.1  christos #include "internal/nelem.h"
     22      1.1  christos #include "testutil.h"
     23      1.1  christos 
     24      1.1  christos typedef struct cipherlist_test_fixture {
     25      1.1  christos     const char *test_case_name;
     26      1.1  christos     SSL_CTX *server;
     27      1.1  christos     SSL_CTX *client;
     28      1.1  christos } CIPHERLIST_TEST_FIXTURE;
     29      1.1  christos 
     30      1.1  christos static void tear_down(CIPHERLIST_TEST_FIXTURE *fixture)
     31      1.1  christos {
     32      1.1  christos     if (fixture != NULL) {
     33      1.1  christos         SSL_CTX_free(fixture->server);
     34      1.1  christos         SSL_CTX_free(fixture->client);
     35      1.1  christos         fixture->server = fixture->client = NULL;
     36      1.1  christos         OPENSSL_free(fixture);
     37      1.1  christos     }
     38      1.1  christos }
     39      1.1  christos 
     40      1.1  christos static CIPHERLIST_TEST_FIXTURE *set_up(const char *const test_case_name)
     41      1.1  christos {
     42      1.1  christos     CIPHERLIST_TEST_FIXTURE *fixture;
     43      1.1  christos 
     44      1.1  christos     if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture))))
     45      1.1  christos         return NULL;
     46      1.1  christos     fixture->test_case_name = test_case_name;
     47      1.1  christos     if (!TEST_ptr(fixture->server = SSL_CTX_new(TLS_server_method()))
     48  1.1.1.2  christos         || !TEST_ptr(fixture->client = SSL_CTX_new(TLS_client_method()))) {
     49      1.1  christos         tear_down(fixture);
     50      1.1  christos         return NULL;
     51      1.1  christos     }
     52      1.1  christos     return fixture;
     53      1.1  christos }
     54      1.1  christos 
     55      1.1  christos /*
     56      1.1  christos  * All ciphers in the DEFAULT cipherlist meet the default security level.
     57      1.1  christos  * However, default supported ciphers exclude SRP and PSK ciphersuites
     58      1.1  christos  * for which no callbacks have been set up.
     59      1.1  christos  *
     60      1.1  christos  * Supported ciphers also exclude TLSv1.2 ciphers if TLSv1.2 is disabled,
     61      1.1  christos  * and individual disabled algorithms. However, NO_RSA, NO_AES and NO_SHA
     62      1.1  christos  * are currently broken and should be considered mission impossible in libssl.
     63      1.1  christos  */
     64      1.1  christos static const uint32_t default_ciphers_in_order[] = {
     65      1.1  christos #ifndef OPENSSL_NO_TLS1_3
     66      1.1  christos     TLS1_3_CK_AES_256_GCM_SHA384,
     67  1.1.1.2  christos #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
     68      1.1  christos     TLS1_3_CK_CHACHA20_POLY1305_SHA256,
     69  1.1.1.2  christos #endif
     70      1.1  christos     TLS1_3_CK_AES_128_GCM_SHA256,
     71      1.1  christos #endif
     72      1.1  christos #ifndef OPENSSL_NO_TLS1_2
     73  1.1.1.2  christos #ifndef OPENSSL_NO_EC
     74      1.1  christos     TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
     75      1.1  christos     TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
     76  1.1.1.2  christos #endif
     77  1.1.1.2  christos #ifndef OPENSSL_NO_DH
     78      1.1  christos     TLS1_CK_DHE_RSA_WITH_AES_256_GCM_SHA384,
     79  1.1.1.2  christos #endif
     80      1.1  christos 
     81  1.1.1.2  christos #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
     82  1.1.1.2  christos #ifndef OPENSSL_NO_EC
     83      1.1  christos     TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
     84      1.1  christos     TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305,
     85  1.1.1.2  christos #endif
     86  1.1.1.2  christos #ifndef OPENSSL_NO_DH
     87      1.1  christos     TLS1_CK_DHE_RSA_WITH_CHACHA20_POLY1305,
     88  1.1.1.2  christos #endif
     89  1.1.1.2  christos #endif /* !OPENSSL_NO_CHACHA && !OPENSSL_NO_POLY1305 */
     90      1.1  christos 
     91  1.1.1.2  christos #ifndef OPENSSL_NO_EC
     92      1.1  christos     TLS1_CK_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
     93      1.1  christos     TLS1_CK_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
     94  1.1.1.2  christos #endif
     95  1.1.1.2  christos #ifndef OPENSSL_NO_DH
     96      1.1  christos     TLS1_CK_DHE_RSA_WITH_AES_128_GCM_SHA256,
     97  1.1.1.2  christos #endif
     98  1.1.1.2  christos #ifndef OPENSSL_NO_EC
     99      1.1  christos     TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
    100      1.1  christos     TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
    101  1.1.1.2  christos #endif
    102  1.1.1.2  christos #ifndef OPENSSL_NO_DH
    103      1.1  christos     TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
    104  1.1.1.2  christos #endif
    105  1.1.1.2  christos #ifndef OPENSSL_NO_EC
    106      1.1  christos     TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
    107      1.1  christos     TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
    108  1.1.1.2  christos #endif
    109  1.1.1.2  christos #ifndef OPENSSL_NO_DH
    110      1.1  christos     TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
    111  1.1.1.2  christos #endif
    112  1.1.1.2  christos #endif /* !OPENSSL_NO_TLS1_2 */
    113      1.1  christos 
    114      1.1  christos #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
    115  1.1.1.2  christos /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
    116  1.1.1.2  christos #ifndef OPENSSL_NO_EC
    117      1.1  christos     TLS1_CK_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
    118      1.1  christos     TLS1_CK_ECDHE_RSA_WITH_AES_256_CBC_SHA,
    119  1.1.1.2  christos #endif
    120  1.1.1.2  christos #ifndef OPENSSL_NO_DH
    121      1.1  christos     TLS1_CK_DHE_RSA_WITH_AES_256_SHA,
    122  1.1.1.2  christos #endif
    123  1.1.1.2  christos #ifndef OPENSSL_NO_EC
    124      1.1  christos     TLS1_CK_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
    125      1.1  christos     TLS1_CK_ECDHE_RSA_WITH_AES_128_CBC_SHA,
    126  1.1.1.2  christos #endif
    127  1.1.1.2  christos #ifndef OPENSSL_NO_DH
    128      1.1  christos     TLS1_CK_DHE_RSA_WITH_AES_128_SHA,
    129  1.1.1.2  christos #endif
    130      1.1  christos #endif /* !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3) */
    131      1.1  christos 
    132      1.1  christos #ifndef OPENSSL_NO_TLS1_2
    133      1.1  christos     TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
    134      1.1  christos     TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
    135      1.1  christos #endif
    136      1.1  christos #ifndef OPENSSL_NO_TLS1_2
    137      1.1  christos     TLS1_CK_RSA_WITH_AES_256_SHA256,
    138      1.1  christos     TLS1_CK_RSA_WITH_AES_128_SHA256,
    139      1.1  christos #endif
    140      1.1  christos #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
    141      1.1  christos     /* These won't be usable if TLSv1.3 is available but TLSv1.2 isn't */
    142      1.1  christos     TLS1_CK_RSA_WITH_AES_256_SHA,
    143      1.1  christos     TLS1_CK_RSA_WITH_AES_128_SHA,
    144      1.1  christos #endif
    145      1.1  christos };
    146      1.1  christos 
    147      1.1  christos static int test_default_cipherlist(SSL_CTX *ctx)
    148      1.1  christos {
    149      1.1  christos     STACK_OF(SSL_CIPHER) *ciphers = NULL;
    150      1.1  christos     SSL *ssl = NULL;
    151      1.1  christos     int i, ret = 0, num_expected_ciphers, num_ciphers;
    152      1.1  christos     uint32_t expected_cipher_id, cipher_id;
    153      1.1  christos 
    154      1.1  christos     if (ctx == NULL)
    155      1.1  christos         return 0;
    156      1.1  christos 
    157      1.1  christos     if (!TEST_ptr(ssl = SSL_new(ctx))
    158  1.1.1.2  christos         || !TEST_ptr(ciphers = SSL_get1_supported_ciphers(ssl)))
    159      1.1  christos         goto err;
    160      1.1  christos 
    161      1.1  christos     num_expected_ciphers = OSSL_NELEM(default_ciphers_in_order);
    162      1.1  christos     num_ciphers = sk_SSL_CIPHER_num(ciphers);
    163      1.1  christos     if (!TEST_int_eq(num_ciphers, num_expected_ciphers))
    164      1.1  christos         goto err;
    165      1.1  christos 
    166      1.1  christos     for (i = 0; i < num_ciphers; i++) {
    167      1.1  christos         expected_cipher_id = default_ciphers_in_order[i];
    168      1.1  christos         cipher_id = SSL_CIPHER_get_id(sk_SSL_CIPHER_value(ciphers, i));
    169      1.1  christos         if (!TEST_int_eq(cipher_id, expected_cipher_id)) {
    170      1.1  christos             TEST_info("Wrong cipher at position %d", i);
    171      1.1  christos             goto err;
    172      1.1  christos         }
    173      1.1  christos     }
    174      1.1  christos 
    175      1.1  christos     ret = 1;
    176      1.1  christos 
    177  1.1.1.2  christos err:
    178      1.1  christos     sk_SSL_CIPHER_free(ciphers);
    179      1.1  christos     SSL_free(ssl);
    180      1.1  christos     return ret;
    181      1.1  christos }
    182      1.1  christos 
    183      1.1  christos static int execute_test(CIPHERLIST_TEST_FIXTURE *fixture)
    184      1.1  christos {
    185      1.1  christos     return fixture != NULL
    186      1.1  christos         && test_default_cipherlist(fixture->server)
    187      1.1  christos         && test_default_cipherlist(fixture->client);
    188      1.1  christos }
    189      1.1  christos 
    190      1.1  christos #define SETUP_CIPHERLIST_TEST_FIXTURE() \
    191      1.1  christos     SETUP_TEST_FIXTURE(CIPHERLIST_TEST_FIXTURE, set_up)
    192      1.1  christos 
    193      1.1  christos #define EXECUTE_CIPHERLIST_TEST() \
    194      1.1  christos     EXECUTE_TEST(execute_test, tear_down)
    195      1.1  christos 
    196      1.1  christos static int test_default_cipherlist_implicit(void)
    197      1.1  christos {
    198      1.1  christos     SETUP_CIPHERLIST_TEST_FIXTURE();
    199      1.1  christos     EXECUTE_CIPHERLIST_TEST();
    200      1.1  christos     return result;
    201      1.1  christos }
    202      1.1  christos 
    203      1.1  christos static int test_default_cipherlist_explicit(void)
    204      1.1  christos {
    205      1.1  christos     SETUP_CIPHERLIST_TEST_FIXTURE();
    206      1.1  christos     if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, "DEFAULT"))
    207  1.1.1.2  christos         || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, "DEFAULT"))) {
    208      1.1  christos         tear_down(fixture);
    209      1.1  christos         fixture = NULL;
    210      1.1  christos     }
    211      1.1  christos     EXECUTE_CIPHERLIST_TEST();
    212      1.1  christos     return result;
    213      1.1  christos }
    214      1.1  christos 
    215      1.1  christos /* SSL_CTX_set_cipher_list() should fail if it clears all TLSv1.2 ciphers. */
    216      1.1  christos static int test_default_cipherlist_clear(void)
    217      1.1  christos {
    218      1.1  christos     SSL *s = NULL;
    219      1.1  christos     SETUP_CIPHERLIST_TEST_FIXTURE();
    220      1.1  christos 
    221      1.1  christos     if (!TEST_int_eq(SSL_CTX_set_cipher_list(fixture->server, "no-such"), 0))
    222      1.1  christos         goto end;
    223      1.1  christos 
    224      1.1  christos     if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_CIPHER_MATCH))
    225      1.1  christos         goto end;
    226      1.1  christos 
    227      1.1  christos     s = SSL_new(fixture->client);
    228      1.1  christos 
    229      1.1  christos     if (!TEST_ptr(s))
    230  1.1.1.2  christos         goto end;
    231      1.1  christos 
    232      1.1  christos     if (!TEST_int_eq(SSL_set_cipher_list(s, "no-such"), 0))
    233      1.1  christos         goto end;
    234      1.1  christos 
    235      1.1  christos     if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
    236  1.1.1.2  christos             SSL_R_NO_CIPHER_MATCH))
    237      1.1  christos         goto end;
    238      1.1  christos 
    239      1.1  christos     result = 1;
    240      1.1  christos end:
    241      1.1  christos     SSL_free(s);
    242      1.1  christos     tear_down(fixture);
    243      1.1  christos     return result;
    244      1.1  christos }
    245      1.1  christos 
    246      1.1  christos /* SSL_CTX_set_cipher_list matching with cipher standard name */
    247      1.1  christos static int test_stdname_cipherlist(void)
    248      1.1  christos {
    249      1.1  christos     SETUP_CIPHERLIST_TEST_FIXTURE();
    250      1.1  christos     if (!TEST_true(SSL_CTX_set_cipher_list(fixture->server, TLS1_RFC_RSA_WITH_AES_128_SHA))
    251  1.1.1.2  christos         || !TEST_true(SSL_CTX_set_cipher_list(fixture->client, TLS1_RFC_RSA_WITH_AES_128_SHA))) {
    252      1.1  christos         goto end;
    253      1.1  christos     }
    254      1.1  christos     result = 1;
    255      1.1  christos end:
    256      1.1  christos     tear_down(fixture);
    257      1.1  christos     fixture = NULL;
    258      1.1  christos     return result;
    259      1.1  christos }
    260      1.1  christos 
    261      1.1  christos int setup_tests(void)
    262      1.1  christos {
    263      1.1  christos     ADD_TEST(test_default_cipherlist_implicit);
    264      1.1  christos     ADD_TEST(test_default_cipherlist_explicit);
    265      1.1  christos     ADD_TEST(test_default_cipherlist_clear);
    266      1.1  christos     ADD_TEST(test_stdname_cipherlist);
    267      1.1  christos     return 1;
    268      1.1  christos }
    269