ciphers.c 7.2 KB

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