Annotation of src/usr.bin/openssl/ciphers.c, Revision 1.5
1.5 ! doug 1: /* $OpenBSD: ciphers.c,v 1.4 2015/03/02 07:51:25 bcook Exp $ */
1.2 jsing 2: /*
3: * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 jsing 16: */
17:
18: #include <stdio.h>
19: #include <stdlib.h>
20:
21: #include <openssl/err.h>
22: #include <openssl/ssl.h>
23:
1.2 jsing 24: #include "apps.h"
25:
26: struct {
27: int usage;
28: int verbose;
29: } ciphers_config;
30:
31: struct option ciphers_options[] = {
32: {
33: .name = "h",
34: .type = OPTION_FLAG,
35: .opt.flag = &ciphers_config.usage,
36: },
37: {
38: .name = "?",
39: .type = OPTION_FLAG,
40: .opt.flag = &ciphers_config.usage,
41: },
42: {
43: .name = "tls1",
1.5 ! doug 44: .desc = "This option is deprecated since it is the default",
! 45: .type = OPTION_DISCARD,
1.2 jsing 46: },
47: {
48: .name = "v",
49: .desc = "Provide cipher listing",
50: .type = OPTION_VALUE,
51: .opt.value = &ciphers_config.verbose,
52: .value = 1,
53: },
54: {
55: .name = "V",
56: .desc = "Provide cipher listing with cipher suite values",
57: .type = OPTION_VALUE,
58: .opt.value = &ciphers_config.verbose,
59: .value = 2,
60: },
1.3 jsing 61: { NULL },
1.1 jsing 62: };
63:
1.2 jsing 64: static void
65: ciphers_usage(void)
66: {
1.5 ! doug 67: fprintf(stderr, "usage: ciphers [-hVv] [-tls1] [cipherlist]\n");
1.2 jsing 68: options_usage(ciphers_options);
69: }
1.1 jsing 70:
71: int
72: ciphers_main(int argc, char **argv)
73: {
1.2 jsing 74: char *cipherlist = NULL;
75: STACK_OF(SSL_CIPHER) *ciphers;
76: const SSL_CIPHER *cipher;
77: SSL_CTX *ssl_ctx = NULL;
1.1 jsing 78: SSL *ssl = NULL;
1.2 jsing 79: uint16_t value;
80: int i, rv = 0;
1.1 jsing 81: char *desc;
1.3 jsing 82:
83: memset(&ciphers_config, 0, sizeof(ciphers_config));
1.1 jsing 84:
1.2 jsing 85: if (options_parse(argc, argv, ciphers_options, &cipherlist,
86: NULL) != 0) {
87: ciphers_usage();
88: return (1);
89: }
1.1 jsing 90:
1.2 jsing 91: if (ciphers_config.usage) {
92: ciphers_usage();
93: return (1);
1.1 jsing 94: }
95:
1.5 ! doug 96: if ((ssl_ctx = SSL_CTX_new(TLSv1_client_method())) == NULL)
1.1 jsing 97: goto err;
1.2 jsing 98:
99: if (cipherlist != NULL) {
100: if (SSL_CTX_set_cipher_list(ssl_ctx, cipherlist) == 0)
1.1 jsing 101: goto err;
102: }
1.2 jsing 103:
104: if ((ssl = SSL_new(ssl_ctx)) == NULL)
1.1 jsing 105: goto err;
106:
1.2 jsing 107: if ((ciphers = SSL_get_ciphers(ssl)) == NULL)
108: goto err;
109:
110: for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
111: cipher = sk_SSL_CIPHER_value(ciphers, i);
112: if (ciphers_config.verbose == 0) {
113: fprintf(stdout, "%s%s", (i ? ":" : ""),
114: SSL_CIPHER_get_name(cipher));
115: continue;
116: }
117: if (ciphers_config.verbose > 1) {
118: value = SSL_CIPHER_get_value(cipher);
1.4 bcook 119: fprintf(stdout, "%-*s0x%02X,0x%02X - ", 10, "",
1.2 jsing 120: ((value >> 8) & 0xff), (value & 0xff));
1.1 jsing 121: }
1.2 jsing 122: desc = SSL_CIPHER_description(cipher, NULL, 0);
123: if (strcmp(desc, "OPENSSL_malloc Error") == 0) {
124: fprintf(stderr, "out of memory\n");
125: goto err;
1.1 jsing 126: }
1.2 jsing 127: fprintf(stdout, "%s", desc);
128: free(desc);
1.1 jsing 129: }
1.2 jsing 130: if (ciphers_config.verbose == 0)
131: fprintf(stdout, "\n");
132:
133: goto done;
1.1 jsing 134:
135: err:
1.2 jsing 136: ERR_print_errors_fp(stderr);
137: rv = 1;
1.1 jsing 138:
1.2 jsing 139: done:
140: SSL_CTX_free(ssl_ctx);
141: SSL_free(ssl);
1.1 jsing 142:
1.2 jsing 143: return (rv);
1.1 jsing 144: }