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