2
0

ciphers.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. argv = opt_rest();
  170. argc = opt_num_rest();
  171. if (argc == 1)
  172. ciphers = *argv;
  173. else if (argc != 0)
  174. goto opthelp;
  175. if (convert != NULL) {
  176. BIO_printf(bio_out, "OpenSSL cipher name: %s\n",
  177. OPENSSL_cipher_name(convert));
  178. goto end;
  179. }
  180. ctx = SSL_CTX_new(meth);
  181. if (ctx == NULL)
  182. goto err;
  183. if (SSL_CTX_set_min_proto_version(ctx, min_version) == 0)
  184. goto err;
  185. if (SSL_CTX_set_max_proto_version(ctx, max_version) == 0)
  186. goto err;
  187. #ifndef OPENSSL_NO_PSK
  188. if (psk)
  189. SSL_CTX_set_psk_client_callback(ctx, dummy_psk);
  190. #endif
  191. #ifndef OPENSSL_NO_SRP
  192. if (srp)
  193. SSL_CTX_set_srp_client_pwd_callback(ctx, dummy_srp);
  194. #endif
  195. if (ciphersuites != NULL && !SSL_CTX_set_ciphersuites(ctx, ciphersuites)) {
  196. BIO_printf(bio_err, "Error setting TLSv1.3 ciphersuites\n");
  197. goto err;
  198. }
  199. if (ciphers != NULL) {
  200. if (!SSL_CTX_set_cipher_list(ctx, ciphers)) {
  201. BIO_printf(bio_err, "Error in cipher list\n");
  202. goto err;
  203. }
  204. }
  205. ssl = SSL_new(ctx);
  206. if (ssl == NULL)
  207. goto err;
  208. if (use_supported)
  209. sk = SSL_get1_supported_ciphers(ssl);
  210. else
  211. sk = SSL_get_ciphers(ssl);
  212. if (!verbose) {
  213. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  214. const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, i);
  215. p = SSL_CIPHER_get_name(c);
  216. if (p == NULL)
  217. break;
  218. if (i != 0)
  219. BIO_printf(bio_out, ":");
  220. BIO_printf(bio_out, "%s", p);
  221. }
  222. BIO_printf(bio_out, "\n");
  223. } else {
  224. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  225. const SSL_CIPHER *c;
  226. c = sk_SSL_CIPHER_value(sk, i);
  227. if (Verbose) {
  228. unsigned long id = SSL_CIPHER_get_id(c);
  229. int id0 = (int)(id >> 24);
  230. int id1 = (int)((id >> 16) & 0xffL);
  231. int id2 = (int)((id >> 8) & 0xffL);
  232. int id3 = (int)(id & 0xffL);
  233. if ((id & 0xff000000L) == 0x03000000L)
  234. BIO_printf(bio_out, " 0x%02X,0x%02X - ", id2, id3); /* SSL3
  235. * cipher */
  236. else
  237. BIO_printf(bio_out, "0x%02X,0x%02X,0x%02X,0x%02X - ", id0, id1, id2, id3); /* whatever */
  238. }
  239. if (stdname) {
  240. const char *nm = SSL_CIPHER_standard_name(c);
  241. if (nm == NULL)
  242. nm = "UNKNOWN";
  243. BIO_printf(bio_out, "%-45s - ", nm);
  244. }
  245. BIO_puts(bio_out, SSL_CIPHER_description(c, buf, sizeof(buf)));
  246. }
  247. }
  248. ret = 0;
  249. goto end;
  250. err:
  251. ERR_print_errors(bio_err);
  252. end:
  253. if (use_supported)
  254. sk_SSL_CIPHER_free(sk);
  255. SSL_CTX_free(ctx);
  256. SSL_free(ssl);
  257. return ret;
  258. }