ciphers.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. typedef enum OPTION_choice {
  17. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  18. OPT_STDNAME,
  19. OPT_CONVERT,
  20. OPT_SSL3,
  21. OPT_TLS1,
  22. OPT_TLS1_1,
  23. OPT_TLS1_2,
  24. OPT_TLS1_3,
  25. OPT_PSK,
  26. OPT_SRP,
  27. OPT_CIPHERSUITES,
  28. OPT_V, OPT_UPPER_V, OPT_S, OPT_PROV_ENUM
  29. } OPTION_CHOICE;
  30. const OPTIONS ciphers_options[] = {
  31. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cipher]\n"},
  32. OPT_SECTION("General"),
  33. {"help", OPT_HELP, '-', "Display this summary"},
  34. OPT_SECTION("Output"),
  35. {"v", OPT_V, '-', "Verbose listing of the SSL/TLS ciphers"},
  36. {"V", OPT_UPPER_V, '-', "Even more verbose"},
  37. {"stdname", OPT_STDNAME, '-', "Show standard cipher names"},
  38. {"convert", OPT_CONVERT, 's', "Convert standard name into OpenSSL name"},
  39. OPT_SECTION("Cipher specification"),
  40. {"s", OPT_S, '-', "Only supported ciphers"},
  41. #ifndef OPENSSL_NO_SSL3
  42. {"ssl3", OPT_SSL3, '-', "Ciphers compatible with SSL3"},
  43. #endif
  44. #ifndef OPENSSL_NO_TLS1
  45. {"tls1", OPT_TLS1, '-', "Ciphers compatible with TLS1"},
  46. #endif
  47. #ifndef OPENSSL_NO_TLS1_1
  48. {"tls1_1", OPT_TLS1_1, '-', "Ciphers compatible with TLS1.1"},
  49. #endif
  50. #ifndef OPENSSL_NO_TLS1_2
  51. {"tls1_2", OPT_TLS1_2, '-', "Ciphers compatible with TLS1.2"},
  52. #endif
  53. #ifndef OPENSSL_NO_TLS1_3
  54. {"tls1_3", OPT_TLS1_3, '-', "Ciphers compatible with TLS1.3"},
  55. #endif
  56. #ifndef OPENSSL_NO_PSK
  57. {"psk", OPT_PSK, '-', "Include ciphersuites requiring PSK"},
  58. #endif
  59. #ifndef OPENSSL_NO_SRP
  60. {"srp", OPT_SRP, '-', "Include ciphersuites requiring SRP"},
  61. #endif
  62. {"ciphersuites", OPT_CIPHERSUITES, 's',
  63. "Configure the TLSv1.3 ciphersuites to use"},
  64. OPT_PROV_OPTIONS,
  65. OPT_PARAMETERS(),
  66. {"cipher", 0, 0, "Cipher string to decode (optional)"},
  67. {NULL}
  68. };
  69. #ifndef OPENSSL_NO_PSK
  70. static unsigned int dummy_psk(SSL *ssl, const char *hint, char *identity,
  71. unsigned int max_identity_len,
  72. unsigned char *psk,
  73. unsigned int max_psk_len)
  74. {
  75. return 0;
  76. }
  77. #endif
  78. #ifndef OPENSSL_NO_SRP
  79. static char *dummy_srp(SSL *ssl, void *arg)
  80. {
  81. return "";
  82. }
  83. #endif
  84. int ciphers_main(int argc, char **argv)
  85. {
  86. SSL_CTX *ctx = NULL;
  87. SSL *ssl = NULL;
  88. STACK_OF(SSL_CIPHER) *sk = NULL;
  89. const SSL_METHOD *meth = TLS_server_method();
  90. int ret = 1, i, verbose = 0, Verbose = 0, use_supported = 0;
  91. int stdname = 0;
  92. #ifndef OPENSSL_NO_PSK
  93. int psk = 0;
  94. #endif
  95. #ifndef OPENSSL_NO_SRP
  96. int srp = 0;
  97. #endif
  98. const char *p;
  99. char *ciphers = NULL, *prog, *convert = NULL, *ciphersuites = NULL;
  100. char buf[512];
  101. OPTION_CHOICE o;
  102. int min_version = 0, max_version = 0;
  103. prog = opt_init(argc, argv, ciphers_options);
  104. while ((o = opt_next()) != OPT_EOF) {
  105. switch (o) {
  106. case OPT_EOF:
  107. case OPT_ERR:
  108. opthelp:
  109. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  110. goto end;
  111. case OPT_HELP:
  112. opt_help(ciphers_options);
  113. ret = 0;
  114. goto end;
  115. case OPT_V:
  116. verbose = 1;
  117. break;
  118. case OPT_UPPER_V:
  119. verbose = Verbose = 1;
  120. break;
  121. case OPT_S:
  122. use_supported = 1;
  123. break;
  124. case OPT_STDNAME:
  125. stdname = verbose = 1;
  126. break;
  127. case OPT_CONVERT:
  128. convert = opt_arg();
  129. break;
  130. case OPT_SSL3:
  131. min_version = SSL3_VERSION;
  132. max_version = SSL3_VERSION;
  133. break;
  134. case OPT_TLS1:
  135. min_version = TLS1_VERSION;
  136. max_version = TLS1_VERSION;
  137. break;
  138. case OPT_TLS1_1:
  139. min_version = TLS1_1_VERSION;
  140. max_version = TLS1_1_VERSION;
  141. break;
  142. case OPT_TLS1_2:
  143. min_version = TLS1_2_VERSION;
  144. max_version = TLS1_2_VERSION;
  145. break;
  146. case OPT_TLS1_3:
  147. min_version = TLS1_3_VERSION;
  148. max_version = TLS1_3_VERSION;
  149. break;
  150. case OPT_PSK:
  151. #ifndef OPENSSL_NO_PSK
  152. psk = 1;
  153. #endif
  154. break;
  155. case OPT_SRP:
  156. #ifndef OPENSSL_NO_SRP
  157. srp = 1;
  158. #endif
  159. break;
  160. case OPT_CIPHERSUITES:
  161. ciphersuites = opt_arg();
  162. break;
  163. case OPT_PROV_CASES:
  164. if (!opt_provider(o))
  165. goto end;
  166. break;
  167. }
  168. }
  169. /* Optional arg is cipher name. */
  170. argv = opt_rest();
  171. argc = opt_num_rest();
  172. if (argc == 1)
  173. ciphers = argv[0];
  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. }