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