ciphers.c 7.7 KB

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