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