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