Home | History | Annotate | Line # | Download | only in apps
      1      1.1  christos /*
      2      1.1  christos  * Copyright 1995-2022 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 <stdio.h>
     11      1.1  christos #include <stdlib.h>
     12      1.1  christos #include <string.h>
     13      1.1  christos #include "apps.h"
     14      1.1  christos #include "progs.h"
     15      1.1  christos #include <openssl/err.h>
     16      1.1  christos #include <openssl/ssl.h>
     17      1.1  christos #include "s_apps.h"
     18      1.1  christos 
     19      1.1  christos typedef enum OPTION_choice {
     20      1.1  christos     OPT_COMMON,
     21      1.1  christos     OPT_STDNAME,
     22      1.1  christos     OPT_CONVERT,
     23      1.1  christos     OPT_SSL3,
     24      1.1  christos     OPT_TLS1,
     25      1.1  christos     OPT_TLS1_1,
     26      1.1  christos     OPT_TLS1_2,
     27      1.1  christos     OPT_TLS1_3,
     28      1.1  christos     OPT_PSK,
     29      1.1  christos     OPT_SRP,
     30      1.1  christos     OPT_CIPHERSUITES,
     31  1.1.1.2  christos     OPT_V,
     32  1.1.1.2  christos     OPT_UPPER_V,
     33  1.1.1.2  christos     OPT_S,
     34  1.1.1.2  christos     OPT_PROV_ENUM
     35      1.1  christos } OPTION_CHOICE;
     36      1.1  christos 
     37      1.1  christos const OPTIONS ciphers_options[] = {
     38  1.1.1.2  christos     { OPT_HELP_STR, 1, '-', "Usage: %s [options] [cipher]\n" },
     39      1.1  christos 
     40      1.1  christos     OPT_SECTION("General"),
     41  1.1.1.2  christos     { "help", OPT_HELP, '-', "Display this summary" },
     42      1.1  christos 
     43      1.1  christos     OPT_SECTION("Output"),
     44  1.1.1.2  christos     { "v", OPT_V, '-', "Verbose listing of the SSL/TLS ciphers" },
     45  1.1.1.2  christos     { "V", OPT_UPPER_V, '-', "Even more verbose" },
     46  1.1.1.2  christos     { "stdname", OPT_STDNAME, '-', "Show standard cipher names" },
     47  1.1.1.2  christos     { "convert", OPT_CONVERT, 's', "Convert standard name into OpenSSL name" },
     48      1.1  christos 
     49      1.1  christos     OPT_SECTION("Cipher specification"),
     50  1.1.1.2  christos     { "s", OPT_S, '-', "Only supported ciphers" },
     51      1.1  christos #ifndef OPENSSL_NO_SSL3
     52  1.1.1.2  christos     { "ssl3", OPT_SSL3, '-', "Ciphers compatible with SSL3" },
     53      1.1  christos #endif
     54      1.1  christos #ifndef OPENSSL_NO_TLS1
     55  1.1.1.2  christos     { "tls1", OPT_TLS1, '-', "Ciphers compatible with TLS1" },
     56      1.1  christos #endif
     57      1.1  christos #ifndef OPENSSL_NO_TLS1_1
     58  1.1.1.2  christos     { "tls1_1", OPT_TLS1_1, '-', "Ciphers compatible with TLS1.1" },
     59      1.1  christos #endif
     60      1.1  christos #ifndef OPENSSL_NO_TLS1_2
     61  1.1.1.2  christos     { "tls1_2", OPT_TLS1_2, '-', "Ciphers compatible with TLS1.2" },
     62      1.1  christos #endif
     63      1.1  christos #ifndef OPENSSL_NO_TLS1_3
     64  1.1.1.2  christos     { "tls1_3", OPT_TLS1_3, '-', "Ciphers compatible with TLS1.3" },
     65      1.1  christos #endif
     66      1.1  christos #ifndef OPENSSL_NO_PSK
     67  1.1.1.2  christos     { "psk", OPT_PSK, '-', "Include ciphersuites requiring PSK" },
     68      1.1  christos #endif
     69      1.1  christos #ifndef OPENSSL_NO_SRP
     70  1.1.1.2  christos     { "srp", OPT_SRP, '-', "(deprecated) Include ciphersuites requiring SRP" },
     71      1.1  christos #endif
     72  1.1.1.2  christos     { "ciphersuites", OPT_CIPHERSUITES, 's',
     73  1.1.1.2  christos         "Configure the TLSv1.3 ciphersuites to use" },
     74      1.1  christos     OPT_PROV_OPTIONS,
     75      1.1  christos 
     76      1.1  christos     OPT_PARAMETERS(),
     77  1.1.1.2  christos     { "cipher", 0, 0, "Cipher string to decode (optional)" },
     78  1.1.1.2  christos     { NULL }
     79      1.1  christos };
     80      1.1  christos 
     81      1.1  christos #ifndef OPENSSL_NO_PSK
     82      1.1  christos static unsigned int dummy_psk(SSL *ssl, const char *hint, char *identity,
     83  1.1.1.2  christos     unsigned int max_identity_len,
     84  1.1.1.2  christos     unsigned char *psk,
     85  1.1.1.2  christos     unsigned int max_psk_len)
     86      1.1  christos {
     87      1.1  christos     return 0;
     88      1.1  christos }
     89      1.1  christos #endif
     90      1.1  christos 
     91      1.1  christos int ciphers_main(int argc, char **argv)
     92      1.1  christos {
     93      1.1  christos     SSL_CTX *ctx = NULL;
     94      1.1  christos     SSL *ssl = NULL;
     95      1.1  christos     STACK_OF(SSL_CIPHER) *sk = NULL;
     96      1.1  christos     const SSL_METHOD *meth = TLS_server_method();
     97      1.1  christos     int ret = 1, i, verbose = 0, Verbose = 0, use_supported = 0;
     98      1.1  christos     int stdname = 0;
     99      1.1  christos #ifndef OPENSSL_NO_PSK
    100      1.1  christos     int psk = 0;
    101      1.1  christos #endif
    102      1.1  christos #ifndef OPENSSL_NO_SRP
    103      1.1  christos     int srp = 0;
    104      1.1  christos #endif
    105      1.1  christos     const char *p;
    106      1.1  christos     char *ciphers = NULL, *prog, *convert = NULL, *ciphersuites = NULL;
    107      1.1  christos     char buf[512];
    108      1.1  christos     OPTION_CHOICE o;
    109      1.1  christos     int min_version = 0, max_version = 0;
    110      1.1  christos 
    111      1.1  christos     prog = opt_init(argc, argv, ciphers_options);
    112      1.1  christos     while ((o = opt_next()) != OPT_EOF) {
    113      1.1  christos         switch (o) {
    114      1.1  christos         case OPT_EOF:
    115      1.1  christos         case OPT_ERR:
    116  1.1.1.2  christos         opthelp:
    117      1.1  christos             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
    118      1.1  christos             goto end;
    119      1.1  christos         case OPT_HELP:
    120      1.1  christos             opt_help(ciphers_options);
    121      1.1  christos             ret = 0;
    122      1.1  christos             goto end;
    123      1.1  christos         case OPT_V:
    124      1.1  christos             verbose = 1;
    125      1.1  christos             break;
    126      1.1  christos         case OPT_UPPER_V:
    127      1.1  christos             verbose = Verbose = 1;
    128      1.1  christos             break;
    129      1.1  christos         case OPT_S:
    130      1.1  christos             use_supported = 1;
    131      1.1  christos             break;
    132      1.1  christos         case OPT_STDNAME:
    133      1.1  christos             stdname = verbose = 1;
    134      1.1  christos             break;
    135      1.1  christos         case OPT_CONVERT:
    136      1.1  christos             convert = opt_arg();
    137      1.1  christos             break;
    138      1.1  christos         case OPT_SSL3:
    139      1.1  christos             min_version = SSL3_VERSION;
    140      1.1  christos             max_version = SSL3_VERSION;
    141      1.1  christos             break;
    142      1.1  christos         case OPT_TLS1:
    143      1.1  christos             min_version = TLS1_VERSION;
    144      1.1  christos             max_version = TLS1_VERSION;
    145      1.1  christos             break;
    146      1.1  christos         case OPT_TLS1_1:
    147      1.1  christos             min_version = TLS1_1_VERSION;
    148      1.1  christos             max_version = TLS1_1_VERSION;
    149      1.1  christos             break;
    150      1.1  christos         case OPT_TLS1_2:
    151      1.1  christos             min_version = TLS1_2_VERSION;
    152      1.1  christos             max_version = TLS1_2_VERSION;
    153      1.1  christos             break;
    154      1.1  christos         case OPT_TLS1_3:
    155      1.1  christos             min_version = TLS1_3_VERSION;
    156      1.1  christos             max_version = TLS1_3_VERSION;
    157      1.1  christos             break;
    158      1.1  christos         case OPT_PSK:
    159      1.1  christos #ifndef OPENSSL_NO_PSK
    160      1.1  christos             psk = 1;
    161      1.1  christos #endif
    162      1.1  christos             break;
    163      1.1  christos         case OPT_SRP:
    164      1.1  christos #ifndef OPENSSL_NO_SRP
    165      1.1  christos             srp = 1;
    166      1.1  christos #endif
    167      1.1  christos             break;
    168      1.1  christos         case OPT_CIPHERSUITES:
    169      1.1  christos             ciphersuites = opt_arg();
    170      1.1  christos             break;
    171      1.1  christos         case OPT_PROV_CASES:
    172      1.1  christos             if (!opt_provider(o))
    173      1.1  christos                 goto end;
    174      1.1  christos             break;
    175      1.1  christos         }
    176      1.1  christos     }
    177      1.1  christos 
    178      1.1  christos     /* Optional arg is cipher name. */
    179      1.1  christos     argv = opt_rest();
    180      1.1  christos     if (opt_num_rest() == 1)
    181      1.1  christos         ciphers = argv[0];
    182      1.1  christos     else if (!opt_check_rest_arg(NULL))
    183      1.1  christos         goto opthelp;
    184      1.1  christos 
    185      1.1  christos     if (convert != NULL) {
    186      1.1  christos         BIO_printf(bio_out, "OpenSSL cipher name: %s\n",
    187  1.1.1.2  christos             OPENSSL_cipher_name(convert));
    188      1.1  christos         ret = 0;
    189      1.1  christos         goto end;
    190      1.1  christos     }
    191      1.1  christos 
    192      1.1  christos     ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
    193      1.1  christos     if (ctx == NULL)
    194      1.1  christos         goto err;
    195      1.1  christos     if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
    196      1.1  christos         goto err;
    197      1.1  christos     if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
    198      1.1  christos         goto err;
    199      1.1  christos 
    200      1.1  christos #ifndef OPENSSL_NO_PSK
    201      1.1  christos     if (psk)
    202      1.1  christos         SSL_CTX_set_psk_client_callback(ctx, dummy_psk);
    203      1.1  christos #endif
    204      1.1  christos #ifndef OPENSSL_NO_SRP
    205      1.1  christos     if (srp)
    206      1.1  christos         set_up_dummy_srp(ctx);
    207      1.1  christos #endif
    208      1.1  christos 
    209      1.1  christos     if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites)) {
    210      1.1  christos         BIO_printf(bio_err, "Error setting TLSv1.3 ciphersuites\n");
    211      1.1  christos         goto err;
    212      1.1  christos     }
    213      1.1  christos 
    214      1.1  christos     if (ciphers != NULL) {
    215      1.1  christos         if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {
    216      1.1  christos             BIO_printf(bio_err, "Error in cipher list\n");
    217      1.1  christos             goto err;
    218      1.1  christos         }
    219      1.1  christos     }
    220      1.1  christos     ssl = SSL_new(ctx);
    221      1.1  christos     if (ssl == NULL)
    222      1.1  christos         goto err;
    223      1.1  christos 
    224      1.1  christos     if (use_supported)
    225      1.1  christos         sk = SSL_get1_supported_ciphers(ssl);
    226      1.1  christos     else
    227      1.1  christos         sk = SSL_get_ciphers(ssl);
    228      1.1  christos 
    229      1.1  christos     if (!verbose) {
    230      1.1  christos         for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
    231      1.1  christos             const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
    232      1.1  christos 
    233      1.1  christos             if (!ossl_assert(c != NULL))
    234      1.1  christos                 continue;
    235      1.1  christos 
    236      1.1  christos             p = SSL_CIPHER_get_name(c);
    237      1.1  christos             if (p == NULL)
    238      1.1  christos                 break;
    239      1.1  christos             if (i != 0)
    240      1.1  christos                 BIO_printf(bio_out, ":");
    241      1.1  christos             BIO_printf(bio_out, "%s", p);
    242      1.1  christos         }
    243      1.1  christos         BIO_printf(bio_out, "\n");
    244      1.1  christos     } else {
    245      1.1  christos 
    246      1.1  christos         for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
    247      1.1  christos             const SSL_CIPHER *c;
    248      1.1  christos 
    249      1.1  christos             c = sk_SSL_CIPHER_value(sk, i);
    250      1.1  christos 
    251      1.1  christos             if (!ossl_assert(c != NULL))
    252      1.1  christos                 continue;
    253      1.1  christos 
    254      1.1  christos             if (Verbose) {
    255      1.1  christos                 unsigned long id = SSL_CIPHER_get_id(c);
    256      1.1  christos                 int id0 = (int)(id >> 24);
    257      1.1  christos                 int id1 = (int)((id >> 16) & 0xffL);
    258      1.1  christos                 int id2 = (int)((id >> 8) & 0xffL);
    259      1.1  christos                 int id3 = (int)(id & 0xffL);
    260      1.1  christos 
    261      1.1  christos                 if ((id & 0xff000000L) == 0x03000000L)
    262      1.1  christos                     BIO_printf(bio_out, "          0x%02X,0x%02X - ", id2, id3); /* SSL3
    263      1.1  christos                                                                                   * cipher */
    264      1.1  christos                 else
    265      1.1  christos                     BIO_printf(bio_out, "0x%02X,0x%02X,0x%02X,0x%02X - ", id0, id1, id2, id3); /* whatever */
    266      1.1  christos             }
    267      1.1  christos             if (stdname) {
    268      1.1  christos                 const char *nm = SSL_CIPHER_standard_name(c);
    269      1.1  christos                 if (nm == NULL)
    270      1.1  christos                     nm = "UNKNOWN";
    271      1.1  christos                 BIO_printf(bio_out, "%-45s - ", nm);
    272      1.1  christos             }
    273      1.1  christos             BIO_puts(bio_out, SSL_CIPHER_description(c, buf, sizeof(buf)));
    274      1.1  christos         }
    275      1.1  christos     }
    276      1.1  christos 
    277      1.1  christos     ret = 0;
    278      1.1  christos     goto end;
    279  1.1.1.2  christos err:
    280      1.1  christos     ERR_print_errors(bio_err);
    281  1.1.1.2  christos end:
    282      1.1  christos     if (use_supported)
    283      1.1  christos         sk_SSL_CIPHER_free(sk);
    284      1.1  christos     SSL_CTX_free(ctx);
    285      1.1  christos     SSL_free(ssl);
    286      1.1  christos     return ret;
    287      1.1  christos }
    288