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