1 // Copyright 2015 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 #include <vector>
17 
18 #include <stdint.h>
19 #include <stdlib.h>
20 
21 #include <openssl/ssl.h>
22 
23 #include "internal.h"
24 
25 
Ciphers(const std::vector<std::string> & args)26 bool Ciphers(const std::vector<std::string> &args) {
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");
33     return false;
34   }
35 
36   const std::string &ciphers_string = args.back();
37 
38   bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(TLS_method()));
39   if (!SSL_CTX_set_strict_cipher_list(ctx.get(), ciphers_string.c_str())) {
40     fprintf(stderr, "Failed to parse cipher suite config.\n");
41     ERR_print_errors_fp(stderr);
42     return false;
43   }
44 
45   STACK_OF(SSL_CIPHER) *ciphers = SSL_CTX_get_ciphers(ctx.get());
46 
47   bool last_in_group = false;
48   for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
49     bool in_group = SSL_CTX_cipher_in_group(ctx.get(), i);
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 
58     printf("%s\n", openssl_name ? SSL_CIPHER_get_name(cipher)
59                                 : SSL_CIPHER_standard_name(cipher));
60 
61     if (!in_group && last_in_group) {
62       printf("]\n");
63     }
64     last_in_group = in_group;
65   }
66 
67   return true;
68 }
69