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