ciphers.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright 1995-2022 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. #include "s_apps.h"
  17. typedef enum OPTION_choice {
  18. OPT_COMMON,
  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, '-', "(deprecated) 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. int ciphers_main(int argc, char **argv)
  80. {
  81. SSL_CTX *ctx = NULL;
  82. SSL *ssl = NULL;
  83. STACK_OF(SSL_CIPHER) *sk = NULL;
  84. const SSL_METHOD *meth = TLS_server_method();
  85. int ret = 1, i, verbose = 0, Verbose = 0, use_supported = 0;
  86. int stdname = 0;
  87. #ifndef OPENSSL_NO_PSK
  88. int psk = 0;
  89. #endif
  90. #ifndef OPENSSL_NO_SRP
  91. int srp = 0;
  92. #endif
  93. const char *p;
  94. char *ciphers = NULL, *prog, *convert = NULL, *ciphersuites = NULL;
  95. char buf[512];
  96. OPTION_CHOICE o;
  97. int min_version = 0, max_version = 0;
  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. case OPT_CIPHERSUITES:
  156. ciphersuites = opt_arg();
  157. break;
  158. case OPT_PROV_CASES:
  159. if (!opt_provider(o))
  160. goto end;
  161. break;
  162. }
  163. }
  164. /* Optional arg is cipher name. */
  165. argv = opt_rest();
  166. argc = opt_num_rest();
  167. if (argc == 1)
  168. ciphers = argv[0];
  169. else if (argc != 0)
  170. goto opthelp;
  171. if (convert != NULL) {
  172. BIO_printf(bio_out, "OpenSSL cipher name: %s\n",
  173. OPENSSL_cipher_name(convert));
  174. ret = 0;
  175. goto end;
  176. }
  177. ctx = SSL_CTX_new_ex(app_get0_libctx(), app_get0_propq(), meth);
  178. if (ctx == NULL)
  179. goto err;
  180. if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
  181. goto err;
  182. if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
  183. goto err;
  184. #ifndef OPENSSL_NO_PSK
  185. if (psk)
  186. SSL_CTX_set_psk_client_callback(ctx, dummy_psk);
  187. #endif
  188. #ifndef OPENSSL_NO_SRP
  189. if (srp)
  190. set_up_dummy_srp(ctx);
  191. #endif
  192. if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites)) {
  193. BIO_printf(bio_err, "Error setting TLSv1.3 ciphersuites\n");
  194. goto err;
  195. }
  196. if (ciphers != NULL) {
  197. if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {
  198. BIO_printf(bio_err, "Error in cipher list\n");
  199. goto err;
  200. }
  201. }
  202. ssl = SSL_new(ctx);
  203. if (ssl == NULL)
  204. goto err;
  205. if (use_supported)
  206. sk = SSL_get1_supported_ciphers(ssl);
  207. else
  208. sk = SSL_get_ciphers(ssl);
  209. if (!verbose) {
  210. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  211. const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
  212. if (!ossl_assert(c != NULL))
  213. continue;
  214. p = SSL_CIPHER_get_name(c);
  215. if (p == NULL)
  216. break;
  217. if (i != 0)
  218. BIO_printf(bio_out, ":");
  219. BIO_printf(bio_out, "%s", p);
  220. }
  221. BIO_printf(bio_out, "\n");
  222. } else {
  223. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  224. const SSL_CIPHER *c;
  225. c = sk_SSL_CIPHER_value(sk, i);
  226. if (!ossl_assert(c != NULL))
  227. continue;
  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. }