ciphers.c 6.6 KB

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