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