1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
874664626SKris Kennaway */
974664626SKris Kennaway
1074664626SKris Kennaway #include <stdio.h>
1174664626SKris Kennaway #include <stdlib.h>
1274664626SKris Kennaway #include <string.h>
1374664626SKris Kennaway #include "apps.h"
14e71b7053SJung-uk Kim #include "progs.h"
1574664626SKris Kennaway #include <openssl/err.h>
1674664626SKris Kennaway #include <openssl/ssl.h>
17*b077aed3SPierre Pronchery #include "s_apps.h"
1874664626SKris Kennaway
19e71b7053SJung-uk Kim typedef enum OPTION_choice {
20*b077aed3SPierre Pronchery OPT_COMMON,
21e71b7053SJung-uk Kim OPT_STDNAME,
22e71b7053SJung-uk Kim OPT_CONVERT,
23e71b7053SJung-uk Kim OPT_SSL3,
24e71b7053SJung-uk Kim OPT_TLS1,
25e71b7053SJung-uk Kim OPT_TLS1_1,
26e71b7053SJung-uk Kim OPT_TLS1_2,
27e71b7053SJung-uk Kim OPT_TLS1_3,
28e71b7053SJung-uk Kim OPT_PSK,
29e71b7053SJung-uk Kim OPT_SRP,
30e71b7053SJung-uk Kim OPT_CIPHERSUITES,
31*b077aed3SPierre Pronchery OPT_V, OPT_UPPER_V, OPT_S, OPT_PROV_ENUM
32e71b7053SJung-uk Kim } OPTION_CHOICE;
3374664626SKris Kennaway
34e71b7053SJung-uk Kim const OPTIONS ciphers_options[] = {
35*b077aed3SPierre Pronchery {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cipher]\n"},
36*b077aed3SPierre Pronchery
37*b077aed3SPierre Pronchery OPT_SECTION("General"),
38e71b7053SJung-uk Kim {"help", OPT_HELP, '-', "Display this summary"},
39*b077aed3SPierre Pronchery
40*b077aed3SPierre Pronchery OPT_SECTION("Output"),
41e71b7053SJung-uk Kim {"v", OPT_V, '-', "Verbose listing of the SSL/TLS ciphers"},
42e71b7053SJung-uk Kim {"V", OPT_UPPER_V, '-', "Even more verbose"},
43*b077aed3SPierre Pronchery {"stdname", OPT_STDNAME, '-', "Show standard cipher names"},
44*b077aed3SPierre Pronchery {"convert", OPT_CONVERT, 's', "Convert standard name into OpenSSL name"},
45*b077aed3SPierre Pronchery
46*b077aed3SPierre Pronchery OPT_SECTION("Cipher specification"),
47e71b7053SJung-uk Kim {"s", OPT_S, '-', "Only supported ciphers"},
485c87c606SMark Murray #ifndef OPENSSL_NO_SSL3
49*b077aed3SPierre Pronchery {"ssl3", OPT_SSL3, '-', "Ciphers compatible with SSL3"},
5074664626SKris Kennaway #endif
515c87c606SMark Murray #ifndef OPENSSL_NO_TLS1
52*b077aed3SPierre Pronchery {"tls1", OPT_TLS1, '-', "Ciphers compatible with TLS1"},
53ddd58736SKris Kennaway #endif
54e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_1
55*b077aed3SPierre Pronchery {"tls1_1", OPT_TLS1_1, '-', "Ciphers compatible with TLS1.1"},
56e71b7053SJung-uk Kim #endif
57e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_2
58*b077aed3SPierre Pronchery {"tls1_2", OPT_TLS1_2, '-', "Ciphers compatible with TLS1.2"},
59e71b7053SJung-uk Kim #endif
60e71b7053SJung-uk Kim #ifndef OPENSSL_NO_TLS1_3
61*b077aed3SPierre Pronchery {"tls1_3", OPT_TLS1_3, '-', "Ciphers compatible with TLS1.3"},
62e71b7053SJung-uk Kim #endif
63e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
64*b077aed3SPierre Pronchery {"psk", OPT_PSK, '-', "Include ciphersuites requiring PSK"},
65e71b7053SJung-uk Kim #endif
66e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
67*b077aed3SPierre Pronchery {"srp", OPT_SRP, '-', "(deprecated) Include ciphersuites requiring SRP"},
68e71b7053SJung-uk Kim #endif
69e71b7053SJung-uk Kim {"ciphersuites", OPT_CIPHERSUITES, 's',
70e71b7053SJung-uk Kim "Configure the TLSv1.3 ciphersuites to use"},
71*b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
72*b077aed3SPierre Pronchery
73*b077aed3SPierre Pronchery OPT_PARAMETERS(),
74*b077aed3SPierre Pronchery {"cipher", 0, 0, "Cipher string to decode (optional)"},
75e71b7053SJung-uk Kim {NULL}
76e71b7053SJung-uk Kim };
7774664626SKris Kennaway
78e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
dummy_psk(SSL * ssl,const char * hint,char * identity,unsigned int max_identity_len,unsigned char * psk,unsigned int max_psk_len)79e71b7053SJung-uk Kim static unsigned int dummy_psk(SSL *ssl, const char *hint, char *identity,
80e71b7053SJung-uk Kim unsigned int max_identity_len,
81e71b7053SJung-uk Kim unsigned char *psk,
82e71b7053SJung-uk Kim unsigned int max_psk_len)
83e71b7053SJung-uk Kim {
84e71b7053SJung-uk Kim return 0;
85e71b7053SJung-uk Kim }
86e71b7053SJung-uk Kim #endif
87e71b7053SJung-uk Kim
ciphers_main(int argc,char ** argv)88e71b7053SJung-uk Kim int ciphers_main(int argc, char **argv)
89e71b7053SJung-uk Kim {
90e71b7053SJung-uk Kim SSL_CTX *ctx = NULL;
91e71b7053SJung-uk Kim SSL *ssl = NULL;
92e71b7053SJung-uk Kim STACK_OF(SSL_CIPHER) *sk = NULL;
93e71b7053SJung-uk Kim const SSL_METHOD *meth = TLS_server_method();
94e71b7053SJung-uk Kim int ret = 1, i, verbose = 0, Verbose = 0, use_supported = 0;
95e71b7053SJung-uk Kim int stdname = 0;
96e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
97e71b7053SJung-uk Kim int psk = 0;
98e71b7053SJung-uk Kim #endif
99e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
100e71b7053SJung-uk Kim int srp = 0;
101e71b7053SJung-uk Kim #endif
102e71b7053SJung-uk Kim const char *p;
103e71b7053SJung-uk Kim char *ciphers = NULL, *prog, *convert = NULL, *ciphersuites = NULL;
104e71b7053SJung-uk Kim char buf[512];
105e71b7053SJung-uk Kim OPTION_CHOICE o;
106e71b7053SJung-uk Kim int min_version = 0, max_version = 0;
107e71b7053SJung-uk Kim
108e71b7053SJung-uk Kim prog = opt_init(argc, argv, ciphers_options);
109e71b7053SJung-uk Kim while ((o = opt_next()) != OPT_EOF) {
110e71b7053SJung-uk Kim switch (o) {
111e71b7053SJung-uk Kim case OPT_EOF:
112e71b7053SJung-uk Kim case OPT_ERR:
113e71b7053SJung-uk Kim opthelp:
114e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
115e71b7053SJung-uk Kim goto end;
116e71b7053SJung-uk Kim case OPT_HELP:
117e71b7053SJung-uk Kim opt_help(ciphers_options);
118e71b7053SJung-uk Kim ret = 0;
119e71b7053SJung-uk Kim goto end;
120e71b7053SJung-uk Kim case OPT_V:
121e71b7053SJung-uk Kim verbose = 1;
122e71b7053SJung-uk Kim break;
123e71b7053SJung-uk Kim case OPT_UPPER_V:
124e71b7053SJung-uk Kim verbose = Verbose = 1;
125e71b7053SJung-uk Kim break;
126e71b7053SJung-uk Kim case OPT_S:
127e71b7053SJung-uk Kim use_supported = 1;
128e71b7053SJung-uk Kim break;
129e71b7053SJung-uk Kim case OPT_STDNAME:
130e71b7053SJung-uk Kim stdname = verbose = 1;
131e71b7053SJung-uk Kim break;
132e71b7053SJung-uk Kim case OPT_CONVERT:
133e71b7053SJung-uk Kim convert = opt_arg();
134e71b7053SJung-uk Kim break;
135e71b7053SJung-uk Kim case OPT_SSL3:
136e71b7053SJung-uk Kim min_version = SSL3_VERSION;
137e71b7053SJung-uk Kim max_version = SSL3_VERSION;
138e71b7053SJung-uk Kim break;
139e71b7053SJung-uk Kim case OPT_TLS1:
140e71b7053SJung-uk Kim min_version = TLS1_VERSION;
141e71b7053SJung-uk Kim max_version = TLS1_VERSION;
142e71b7053SJung-uk Kim break;
143e71b7053SJung-uk Kim case OPT_TLS1_1:
144e71b7053SJung-uk Kim min_version = TLS1_1_VERSION;
145e71b7053SJung-uk Kim max_version = TLS1_1_VERSION;
146e71b7053SJung-uk Kim break;
147e71b7053SJung-uk Kim case OPT_TLS1_2:
148e71b7053SJung-uk Kim min_version = TLS1_2_VERSION;
149e71b7053SJung-uk Kim max_version = TLS1_2_VERSION;
150e71b7053SJung-uk Kim break;
151e71b7053SJung-uk Kim case OPT_TLS1_3:
152e71b7053SJung-uk Kim min_version = TLS1_3_VERSION;
153e71b7053SJung-uk Kim max_version = TLS1_3_VERSION;
154e71b7053SJung-uk Kim break;
155e71b7053SJung-uk Kim case OPT_PSK:
156e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
157e71b7053SJung-uk Kim psk = 1;
158e71b7053SJung-uk Kim #endif
159e71b7053SJung-uk Kim break;
160e71b7053SJung-uk Kim case OPT_SRP:
161e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
162e71b7053SJung-uk Kim srp = 1;
163e71b7053SJung-uk Kim #endif
164e71b7053SJung-uk Kim break;
165e71b7053SJung-uk Kim case OPT_CIPHERSUITES:
166e71b7053SJung-uk Kim ciphersuites = opt_arg();
167e71b7053SJung-uk Kim break;
168*b077aed3SPierre Pronchery case OPT_PROV_CASES:
169*b077aed3SPierre Pronchery if (!opt_provider(o))
170*b077aed3SPierre Pronchery goto end;
171*b077aed3SPierre Pronchery break;
172e71b7053SJung-uk Kim }
173e71b7053SJung-uk Kim }
174*b077aed3SPierre Pronchery
175*b077aed3SPierre Pronchery /* Optional arg is cipher name. */
176e71b7053SJung-uk Kim argv = opt_rest();
177e71b7053SJung-uk Kim argc = opt_num_rest();
178e71b7053SJung-uk Kim if (argc == 1)
179*b077aed3SPierre Pronchery ciphers = argv[0];
180e71b7053SJung-uk Kim else if (argc != 0)
181e71b7053SJung-uk Kim goto opthelp;
182e71b7053SJung-uk Kim
183e71b7053SJung-uk Kim if (convert != NULL) {
184e71b7053SJung-uk Kim BIO_printf(bio_out, "OpenSSL cipher name: %s\n",
185e71b7053SJung-uk Kim OPENSSL_cipher_name(convert));
186b2bf0c7eSJung-uk Kim ret = 0;
18774664626SKris Kennaway goto end;
18874664626SKris Kennaway }
18974664626SKris Kennaway
190*b077aed3SPierre Pronchery ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
1916f9291ceSJung-uk Kim if (ctx == NULL)
1926f9291ceSJung-uk Kim goto err;
193e71b7053SJung-uk Kim if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
194e71b7053SJung-uk Kim goto err;
195e71b7053SJung-uk Kim if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
196e71b7053SJung-uk Kim goto err;
197e71b7053SJung-uk Kim
198e71b7053SJung-uk Kim #ifndef OPENSSL_NO_PSK
199e71b7053SJung-uk Kim if (psk)
200e71b7053SJung-uk Kim SSL_CTX_set_psk_client_callback(ctx, dummy_psk);
201e71b7053SJung-uk Kim #endif
202e71b7053SJung-uk Kim #ifndef OPENSSL_NO_SRP
203e71b7053SJung-uk Kim if (srp)
204*b077aed3SPierre Pronchery set_up_dummy_srp(ctx);
205e71b7053SJung-uk Kim #endif
206e71b7053SJung-uk Kim
207e71b7053SJung-uk Kim if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites)) {
208e71b7053SJung-uk Kim BIO_printf(bio_err, "Error setting TLSv1.3 ciphersuites\n");
209e71b7053SJung-uk Kim goto err;
210e71b7053SJung-uk Kim }
211e71b7053SJung-uk Kim
212f579bf8eSKris Kennaway if (ciphers != NULL) {
213f579bf8eSKris Kennaway if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {
214f579bf8eSKris Kennaway BIO_printf(bio_err, "Error in cipher list\n");
215f579bf8eSKris Kennaway goto err;
216f579bf8eSKris Kennaway }
217f579bf8eSKris Kennaway }
21874664626SKris Kennaway ssl = SSL_new(ctx);
2196f9291ceSJung-uk Kim if (ssl == NULL)
2206f9291ceSJung-uk Kim goto err;
22174664626SKris Kennaway
222e71b7053SJung-uk Kim if (use_supported)
223e71b7053SJung-uk Kim sk = SSL_get1_supported_ciphers(ssl);
224e71b7053SJung-uk Kim else
225e71b7053SJung-uk Kim sk = SSL_get_ciphers(ssl);
226e71b7053SJung-uk Kim
2276f9291ceSJung-uk Kim if (!verbose) {
228e71b7053SJung-uk Kim for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
229e71b7053SJung-uk Kim const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
230*b077aed3SPierre Pronchery
231*b077aed3SPierre Pronchery if (!ossl_assert(c != NULL))
232*b077aed3SPierre Pronchery continue;
233*b077aed3SPierre Pronchery
234e71b7053SJung-uk Kim p = SSL_CIPHER_get_name(c);
2356f9291ceSJung-uk Kim if (p == NULL)
2366f9291ceSJung-uk Kim break;
2376f9291ceSJung-uk Kim if (i != 0)
238e71b7053SJung-uk Kim BIO_printf(bio_out, ":");
239e71b7053SJung-uk Kim BIO_printf(bio_out, "%s", p);
24074664626SKris Kennaway }
241e71b7053SJung-uk Kim BIO_printf(bio_out, "\n");
242e71b7053SJung-uk Kim } else {
24374664626SKris Kennaway
2446f9291ceSJung-uk Kim for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
245e71b7053SJung-uk Kim const SSL_CIPHER *c;
2461f13597dSJung-uk Kim
2471f13597dSJung-uk Kim c = sk_SSL_CIPHER_value(sk, i);
2481f13597dSJung-uk Kim
249*b077aed3SPierre Pronchery if (!ossl_assert(c != NULL))
250*b077aed3SPierre Pronchery continue;
251*b077aed3SPierre Pronchery
2526f9291ceSJung-uk Kim if (Verbose) {
2531f13597dSJung-uk Kim unsigned long id = SSL_CIPHER_get_id(c);
2541f13597dSJung-uk Kim int id0 = (int)(id >> 24);
2551f13597dSJung-uk Kim int id1 = (int)((id >> 16) & 0xffL);
2561f13597dSJung-uk Kim int id2 = (int)((id >> 8) & 0xffL);
2571f13597dSJung-uk Kim int id3 = (int)(id & 0xffL);
2581f13597dSJung-uk Kim
259e71b7053SJung-uk Kim if ((id & 0xff000000L) == 0x03000000L)
260e71b7053SJung-uk Kim BIO_printf(bio_out, " 0x%02X,0x%02X - ", id2, id3); /* SSL3
261e71b7053SJung-uk Kim * cipher */
262e71b7053SJung-uk Kim else
263e71b7053SJung-uk Kim BIO_printf(bio_out, "0x%02X,0x%02X,0x%02X,0x%02X - ", id0, id1, id2, id3); /* whatever */
2646f9291ceSJung-uk Kim }
2657bded2dbSJung-uk Kim if (stdname) {
2667bded2dbSJung-uk Kim const char *nm = SSL_CIPHER_standard_name(c);
2677bded2dbSJung-uk Kim if (nm == NULL)
2687bded2dbSJung-uk Kim nm = "UNKNOWN";
269*b077aed3SPierre Pronchery BIO_printf(bio_out, "%-45s - ", nm);
2707bded2dbSJung-uk Kim }
271e71b7053SJung-uk Kim BIO_puts(bio_out, SSL_CIPHER_description(c, buf, sizeof(buf)));
27274664626SKris Kennaway }
27374664626SKris Kennaway }
27474664626SKris Kennaway
27574664626SKris Kennaway ret = 0;
276e71b7053SJung-uk Kim goto end;
27774664626SKris Kennaway err:
27874664626SKris Kennaway ERR_print_errors(bio_err);
27974664626SKris Kennaway end:
280e71b7053SJung-uk Kim if (use_supported)
281e71b7053SJung-uk Kim sk_SSL_CIPHER_free(sk);
2826f9291ceSJung-uk Kim SSL_CTX_free(ctx);
2836f9291ceSJung-uk Kim SSL_free(ssl);
284e71b7053SJung-uk Kim return ret;
28574664626SKris Kennaway }
286