• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include "internal.h"
24 
25 
Ciphers(const std::vector<std::string> & args)26 bool Ciphers(const std::vector<std::string> &args) {
27   if (args.size() != 1) {
28     fprintf(stderr, "Usage: bssl ciphers <cipher suite string>\n");
29     return false;
30   }
31 
32   const std::string &ciphers_string = args.back();
33 
34   bssl::UniquePtr<SSL_CTX> ctx(SSL_CTX_new(SSLv23_client_method()));
35   if (!SSL_CTX_set_strict_cipher_list(ctx.get(), ciphers_string.c_str())) {
36     fprintf(stderr, "Failed to parse cipher suite config.\n");
37     ERR_print_errors_fp(stderr);
38     return false;
39   }
40 
41   const struct ssl_cipher_preference_list_st *pref_list = ctx->cipher_list;
42   STACK_OF(SSL_CIPHER) *ciphers = pref_list->ciphers;
43 
44   bool last_in_group = false;
45   for (size_t i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
46     bool in_group = pref_list->in_group_flags[i];
47     const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(ciphers, i);
48 
49     if (in_group && !last_in_group) {
50       printf("[\n  ");
51     } else if (last_in_group) {
52       printf("  ");
53     }
54 
55     printf("%s\n", SSL_CIPHER_standard_name(cipher));
56 
57     if (!in_group && last_in_group) {
58       printf("]\n");
59     }
60     last_in_group = in_group;
61   }
62 
63   return true;
64 }
65