Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 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 | |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <stdlib.h> |
| 20 | |
| 21 | #include <openssl/ssl.h> |
| 22 | |
Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 23 | #include "internal.h" |
| 24 | |
| 25 | |
| 26 | bool Ciphers(const std::vector<std::string> &args) { |
David Benjamin | 388dfa1 | 2017-08-17 13:09:10 -0400 | [diff] [blame] | 27 | bool openssl_name = false; |
| 28 | if (args.size() == 2 && args[0] == "-openssl-name") { |
| 29 | openssl_name = true; |
| 30 | } else if (args.size() != 1) { |
| 31 | fprintf(stderr, |
| 32 | "Usage: bssl ciphers [-openssl-name] <cipher suite string>\n"); |
Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 33 | return false; |
| 34 | } |
| 35 | |
| 36 | const std::string &ciphers_string = args.back(); |
| 37 | |
David Benjamin | 388dfa1 | 2017-08-17 13:09:10 -0400 | [diff] [blame] | 38 | bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method())); |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 39 | if (!SSL_CTX_set_strict_cipher_list(ctx.get(), ciphers_string.c_str())) { |
Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 40 | fprintf(stderr, "Failed to parse cipher suite config.\n"); |
| 41 | ERR_print_errors_fp(stderr); |
| 42 | return false; |
| 43 | } |
| 44 | |
David Benjamin | 388dfa1 | 2017-08-17 13:09:10 -0400 | [diff] [blame] | 45 | STACK_OF(SSL_CIPHER) *ciphers = SSL_CTX_get_ciphers(ctx.get()); |
Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 46 | |
| 47 | bool last_in_group = false; |
| 48 | for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { |
David Benjamin | 388dfa1 | 2017-08-17 13:09:10 -0400 | [diff] [blame] | 49 | bool in_group = SSL_CTX_cipher_in_group(ctx.get(), i); |
Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 50 | const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i); |
| 51 | |
| 52 | if (in_group && !last_in_group) { |
| 53 | printf("[\n "); |
| 54 | } else if (last_in_group) { |
| 55 | printf(" "); |
| 56 | } |
| 57 | |
David Benjamin | 388dfa1 | 2017-08-17 13:09:10 -0400 | [diff] [blame] | 58 | printf("%s\n", openssl_name ? SSL_CIPHER_get_name(cipher) |
| 59 | : SSL_CIPHER_standard_name(cipher)); |
Adam Langley | eb8be01 | 2015-10-27 16:03:52 -0700 | [diff] [blame] | 60 | |
| 61 | if (!in_group && last_in_group) { |
| 62 | printf("]\n"); |
| 63 | } |
| 64 | last_in_group = in_group; |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |