ciphers.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <string>
  15. #include <vector>
  16. #include <stdint.h>
  17. #include <stdlib.h>
  18. #include <openssl/ssl.h>
  19. #include "internal.h"
  20. bool Ciphers(const std::vector<std::string> &args) {
  21. bool openssl_name = false;
  22. if (args.size() == 2 && args[0] == "-openssl-name") {
  23. openssl_name = true;
  24. } else if (args.size() != 1) {
  25. fprintf(stderr,
  26. "Usage: bssl ciphers [-openssl-name] <cipher suite string>\n");
  27. return false;
  28. }
  29. const std::string &ciphers_string = args.back();
  30. bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
  31. if (!SSL_CTX_set_strict_cipher_list(ctx.get(), ciphers_string.c_str())) {
  32. fprintf(stderr, "Failed to parse cipher suite config.\n");
  33. ERR_print_errors_fp(stderr);
  34. return false;
  35. }
  36. STACK_OF(SSL_CIPHER) *ciphers = SSL_CTX_get_ciphers(ctx.get());
  37. bool last_in_group = false;
  38. for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  39. bool in_group = SSL_CTX_cipher_in_group(ctx.get(), i);
  40. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
  41. if (in_group && !last_in_group) {
  42. printf("[\n ");
  43. } else if (last_in_group) {
  44. printf(" ");
  45. }
  46. printf("%s\n", openssl_name ? SSL_CIPHER_get_name(cipher)
  47. : SSL_CIPHER_standard_name(cipher));
  48. if (!in_group && last_in_group) {
  49. printf("]\n");
  50. }
  51. last_in_group = in_group;
  52. }
  53. return true;
  54. }