1c7da899bSchristos /*
2*b0d17251Schristos * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3a89c9211Schristos *
4*b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use
5c7da899bSchristos * this file except in compliance with the License. You can obtain a copy
6c7da899bSchristos * in the file LICENSE in the source distribution or at
7c7da899bSchristos * https://www.openssl.org/source/license.html
8a89c9211Schristos */
9a89c9211Schristos
10a89c9211Schristos #include <stdio.h>
11a89c9211Schristos #include <stdlib.h>
12a89c9211Schristos #include <string.h>
13a89c9211Schristos #include "apps.h"
1413d40330Schristos #include "progs.h"
15a89c9211Schristos #include <openssl/err.h>
16a89c9211Schristos #include <openssl/ssl.h>
17*b0d17251Schristos #include "s_apps.h"
18a89c9211Schristos
19c7da899bSchristos typedef enum OPTION_choice {
20*b0d17251Schristos OPT_COMMON,
21c7da899bSchristos OPT_STDNAME,
2213d40330Schristos OPT_CONVERT,
23c7da899bSchristos OPT_SSL3,
24c7da899bSchristos OPT_TLS1,
25c7da899bSchristos OPT_TLS1_1,
26c7da899bSchristos OPT_TLS1_2,
2713d40330Schristos OPT_TLS1_3,
28c7da899bSchristos OPT_PSK,
29c7da899bSchristos OPT_SRP,
3013d40330Schristos OPT_CIPHERSUITES,
31*b0d17251Schristos OPT_V, OPT_UPPER_V, OPT_S, OPT_PROV_ENUM
32c7da899bSchristos } OPTION_CHOICE;
33a89c9211Schristos
3413d40330Schristos const OPTIONS ciphers_options[] = {
35*b0d17251Schristos {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cipher]\n"},
36*b0d17251Schristos
37*b0d17251Schristos OPT_SECTION("General"),
38c7da899bSchristos {"help", OPT_HELP, '-', "Display this summary"},
39*b0d17251Schristos
40*b0d17251Schristos OPT_SECTION("Output"),
41c7da899bSchristos {"v", OPT_V, '-', "Verbose listing of the SSL/TLS ciphers"},
42c7da899bSchristos {"V", OPT_UPPER_V, '-', "Even more verbose"},
43*b0d17251Schristos {"stdname", OPT_STDNAME, '-', "Show standard cipher names"},
44*b0d17251Schristos {"convert", OPT_CONVERT, 's', "Convert standard name into OpenSSL name"},
45*b0d17251Schristos
46*b0d17251Schristos OPT_SECTION("Cipher specification"),
47c7da899bSchristos {"s", OPT_S, '-', "Only supported ciphers"},
48c7da899bSchristos #ifndef OPENSSL_NO_SSL3
49*b0d17251Schristos {"ssl3", OPT_SSL3, '-', "Ciphers compatible with SSL3"},
50c7da899bSchristos #endif
51c7da899bSchristos #ifndef OPENSSL_NO_TLS1
52*b0d17251Schristos {"tls1", OPT_TLS1, '-', "Ciphers compatible with TLS1"},
53c7da899bSchristos #endif
54c7da899bSchristos #ifndef OPENSSL_NO_TLS1_1
55*b0d17251Schristos {"tls1_1", OPT_TLS1_1, '-', "Ciphers compatible with TLS1.1"},
56c7da899bSchristos #endif
57c7da899bSchristos #ifndef OPENSSL_NO_TLS1_2
58*b0d17251Schristos {"tls1_2", OPT_TLS1_2, '-', "Ciphers compatible with TLS1.2"},
59c7da899bSchristos #endif
6013d40330Schristos #ifndef OPENSSL_NO_TLS1_3
61*b0d17251Schristos {"tls1_3", OPT_TLS1_3, '-', "Ciphers compatible with TLS1.3"},
62c7da899bSchristos #endif
63c7da899bSchristos #ifndef OPENSSL_NO_PSK
64*b0d17251Schristos {"psk", OPT_PSK, '-', "Include ciphersuites requiring PSK"},
65c7da899bSchristos #endif
66c7da899bSchristos #ifndef OPENSSL_NO_SRP
67*b0d17251Schristos {"srp", OPT_SRP, '-', "(deprecated) Include ciphersuites requiring SRP"},
68c7da899bSchristos #endif
6913d40330Schristos {"ciphersuites", OPT_CIPHERSUITES, 's',
7013d40330Schristos "Configure the TLSv1.3 ciphersuites to use"},
71*b0d17251Schristos OPT_PROV_OPTIONS,
72*b0d17251Schristos
73*b0d17251Schristos OPT_PARAMETERS(),
74*b0d17251Schristos {"cipher", 0, 0, "Cipher string to decode (optional)"},
75c7da899bSchristos {NULL}
76a89c9211Schristos };
77a89c9211Schristos
78c7da899bSchristos #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)79c7da899bSchristos static unsigned int dummy_psk(SSL *ssl, const char *hint, char *identity,
80c7da899bSchristos unsigned int max_identity_len,
81c7da899bSchristos unsigned char *psk,
82c7da899bSchristos unsigned int max_psk_len)
83a89c9211Schristos {
84c7da899bSchristos return 0;
85c7da899bSchristos }
86c7da899bSchristos #endif
87c7da899bSchristos
ciphers_main(int argc,char ** argv)88c7da899bSchristos int ciphers_main(int argc, char **argv)
89c7da899bSchristos {
90c7da899bSchristos SSL_CTX *ctx = NULL;
91c7da899bSchristos SSL *ssl = NULL;
92c7da899bSchristos STACK_OF(SSL_CIPHER) *sk = NULL;
93c7da899bSchristos const SSL_METHOD *meth = TLS_server_method();
94c7da899bSchristos int ret = 1, i, verbose = 0, Verbose = 0, use_supported = 0;
95218f7bfcSspz int stdname = 0;
96c7da899bSchristos #ifndef OPENSSL_NO_PSK
97c7da899bSchristos int psk = 0;
98a89c9211Schristos #endif
99c7da899bSchristos #ifndef OPENSSL_NO_SRP
100c7da899bSchristos int srp = 0;
101c7da899bSchristos #endif
102c7da899bSchristos const char *p;
10313d40330Schristos char *ciphers = NULL, *prog, *convert = NULL, *ciphersuites = NULL;
104c7da899bSchristos char buf[512];
105c7da899bSchristos OPTION_CHOICE o;
106c7da899bSchristos int min_version = 0, max_version = 0;
107a89c9211Schristos
108c7da899bSchristos prog = opt_init(argc, argv, ciphers_options);
109c7da899bSchristos while ((o = opt_next()) != OPT_EOF) {
110c7da899bSchristos switch (o) {
111c7da899bSchristos case OPT_EOF:
112c7da899bSchristos case OPT_ERR:
113c7da899bSchristos opthelp:
114c7da899bSchristos BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
115c7da899bSchristos goto end;
116c7da899bSchristos case OPT_HELP:
117c7da899bSchristos opt_help(ciphers_options);
118c7da899bSchristos ret = 0;
119c7da899bSchristos goto end;
120c7da899bSchristos case OPT_V:
121a89c9211Schristos verbose = 1;
122c7da899bSchristos break;
123c7da899bSchristos case OPT_UPPER_V:
124a89c9211Schristos verbose = Verbose = 1;
125c7da899bSchristos break;
126c7da899bSchristos case OPT_S:
127c7da899bSchristos use_supported = 1;
128c7da899bSchristos break;
129c7da899bSchristos case OPT_STDNAME:
130218f7bfcSspz stdname = verbose = 1;
13113d40330Schristos break;
13213d40330Schristos case OPT_CONVERT:
13313d40330Schristos convert = opt_arg();
134a89c9211Schristos break;
135c7da899bSchristos case OPT_SSL3:
136c7da899bSchristos min_version = SSL3_VERSION;
137c7da899bSchristos max_version = SSL3_VERSION;
138c7da899bSchristos break;
139c7da899bSchristos case OPT_TLS1:
140c7da899bSchristos min_version = TLS1_VERSION;
141c7da899bSchristos max_version = TLS1_VERSION;
142c7da899bSchristos break;
143c7da899bSchristos case OPT_TLS1_1:
144c7da899bSchristos min_version = TLS1_1_VERSION;
145c7da899bSchristos max_version = TLS1_1_VERSION;
146c7da899bSchristos break;
147c7da899bSchristos case OPT_TLS1_2:
148c7da899bSchristos min_version = TLS1_2_VERSION;
149c7da899bSchristos max_version = TLS1_2_VERSION;
150c7da899bSchristos break;
15113d40330Schristos case OPT_TLS1_3:
15213d40330Schristos min_version = TLS1_3_VERSION;
15313d40330Schristos max_version = TLS1_3_VERSION;
15413d40330Schristos break;
155c7da899bSchristos case OPT_PSK:
156c7da899bSchristos #ifndef OPENSSL_NO_PSK
157c7da899bSchristos psk = 1;
158c7da899bSchristos #endif
159c7da899bSchristos break;
160c7da899bSchristos case OPT_SRP:
161c7da899bSchristos #ifndef OPENSSL_NO_SRP
162c7da899bSchristos srp = 1;
163c7da899bSchristos #endif
164c7da899bSchristos break;
16513d40330Schristos case OPT_CIPHERSUITES:
16613d40330Schristos ciphersuites = opt_arg();
16713d40330Schristos break;
168*b0d17251Schristos case OPT_PROV_CASES:
169*b0d17251Schristos if (!opt_provider(o))
170*b0d17251Schristos goto end;
171*b0d17251Schristos break;
172c7da899bSchristos }
173c7da899bSchristos }
174*b0d17251Schristos
175*b0d17251Schristos /* Optional arg is cipher name. */
176c7da899bSchristos argv = opt_rest();
177c7da899bSchristos argc = opt_num_rest();
178c7da899bSchristos if (argc == 1)
179*b0d17251Schristos ciphers = argv[0];
180c7da899bSchristos else if (argc != 0)
181c7da899bSchristos goto opthelp;
182a89c9211Schristos
18313d40330Schristos if (convert != NULL) {
18413d40330Schristos BIO_printf(bio_out, "OpenSSL cipher name: %s\n",
18513d40330Schristos OPENSSL_cipher_name(convert));
18621497c5cSchristos ret = 0;
18713d40330Schristos goto end;
18813d40330Schristos }
18913d40330Schristos
190*b0d17251Schristos ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
191635165faSspz if (ctx == NULL)
192635165faSspz goto err;
193c7da899bSchristos if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
194c7da899bSchristos goto err;
195c7da899bSchristos if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
196c7da899bSchristos goto err;
197c7da899bSchristos
198c7da899bSchristos #ifndef OPENSSL_NO_PSK
199c7da899bSchristos if (psk)
200c7da899bSchristos SSL_CTX_set_psk_client_callback(ctx, dummy_psk);
201c7da899bSchristos #endif
202c7da899bSchristos #ifndef OPENSSL_NO_SRP
203c7da899bSchristos if (srp)
204*b0d17251Schristos set_up_dummy_srp(ctx);
205c7da899bSchristos #endif
20613d40330Schristos
20713d40330Schristos if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites)) {
20813d40330Schristos BIO_printf(bio_err, "Error setting TLSv1.3 ciphersuites\n");
20913d40330Schristos goto err;
21013d40330Schristos }
21113d40330Schristos
212a89c9211Schristos if (ciphers != NULL) {
213a89c9211Schristos if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {
214a89c9211Schristos BIO_printf(bio_err, "Error in cipher list\n");
215a89c9211Schristos goto err;
216a89c9211Schristos }
217a89c9211Schristos }
218a89c9211Schristos ssl = SSL_new(ctx);
219635165faSspz if (ssl == NULL)
220635165faSspz goto err;
221a89c9211Schristos
222c7da899bSchristos if (use_supported)
223c7da899bSchristos sk = SSL_get1_supported_ciphers(ssl);
224c7da899bSchristos else
225c7da899bSchristos sk = SSL_get_ciphers(ssl);
226c7da899bSchristos
227635165faSspz if (!verbose) {
228c7da899bSchristos for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
229c7da899bSchristos const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
230*b0d17251Schristos
231*b0d17251Schristos if (!ossl_assert(c != NULL))
232*b0d17251Schristos continue;
233*b0d17251Schristos
234c7da899bSchristos p = SSL_CIPHER_get_name(c);
235635165faSspz if (p == NULL)
236635165faSspz break;
237635165faSspz if (i != 0)
238c7da899bSchristos BIO_printf(bio_out, ":");
239c7da899bSchristos BIO_printf(bio_out, "%s", p);
240a89c9211Schristos }
241c7da899bSchristos BIO_printf(bio_out, "\n");
242c7da899bSchristos } else {
243a89c9211Schristos
244635165faSspz for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
245c7da899bSchristos const SSL_CIPHER *c;
246a89c9211Schristos
247a89c9211Schristos c = sk_SSL_CIPHER_value(sk, i);
248a89c9211Schristos
249*b0d17251Schristos if (!ossl_assert(c != NULL))
250*b0d17251Schristos continue;
251*b0d17251Schristos
252635165faSspz if (Verbose) {
2534e3dcb23Sspz unsigned long id = SSL_CIPHER_get_id(c);
254a89c9211Schristos int id0 = (int)(id >> 24);
255a89c9211Schristos int id1 = (int)((id >> 16) & 0xffL);
256a89c9211Schristos int id2 = (int)((id >> 8) & 0xffL);
257a89c9211Schristos int id3 = (int)(id & 0xffL);
258a89c9211Schristos
259c7da899bSchristos if ((id & 0xff000000L) == 0x03000000L)
260c7da899bSchristos BIO_printf(bio_out, " 0x%02X,0x%02X - ", id2, id3); /* SSL3
261c7da899bSchristos * cipher */
262c7da899bSchristos else
263c7da899bSchristos BIO_printf(bio_out, "0x%02X,0x%02X,0x%02X,0x%02X - ", id0, id1, id2, id3); /* whatever */
264a89c9211Schristos }
265218f7bfcSspz if (stdname) {
266218f7bfcSspz const char *nm = SSL_CIPHER_standard_name(c);
267218f7bfcSspz if (nm == NULL)
268218f7bfcSspz nm = "UNKNOWN";
269*b0d17251Schristos BIO_printf(bio_out, "%-45s - ", nm);
270218f7bfcSspz }
27153060421Schristos BIO_puts(bio_out, SSL_CIPHER_description(c, buf, sizeof(buf)));
272a89c9211Schristos }
273a89c9211Schristos }
274a89c9211Schristos
275a89c9211Schristos ret = 0;
276c7da899bSchristos goto end;
277a89c9211Schristos err:
278a89c9211Schristos ERR_print_errors(bio_err);
279a89c9211Schristos end:
280c7da899bSchristos if (use_supported)
281c7da899bSchristos sk_SSL_CIPHER_free(sk);
282635165faSspz SSL_CTX_free(ctx);
283635165faSspz SSL_free(ssl);
28413d40330Schristos return ret;
285a89c9211Schristos }
286