[BACK]Return to ciphers.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Annotation of src/usr.bin/openssl/ciphers.c, Revision 1.7

1.7     ! doug        1: /* $OpenBSD: ciphers.c,v 1.6 2015/08/19 18:25:31 deraadt 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"
1.6       deraadt    25: #include "progs.h"
1.2       jsing      26:
                     27: struct {
                     28:        int usage;
                     29:        int verbose;
                     30: } ciphers_config;
                     31:
                     32: struct option ciphers_options[] = {
                     33:        {
                     34:                .name = "h",
                     35:                .type = OPTION_FLAG,
                     36:                .opt.flag = &ciphers_config.usage,
                     37:        },
                     38:        {
                     39:                .name = "?",
                     40:                .type = OPTION_FLAG,
                     41:                .opt.flag = &ciphers_config.usage,
                     42:        },
                     43:        {
                     44:                .name = "tls1",
1.5       doug       45:                .desc = "This option is deprecated since it is the default",
                     46:                .type = OPTION_DISCARD,
1.2       jsing      47:        },
                     48:        {
                     49:                .name = "v",
                     50:                .desc = "Provide cipher listing",
                     51:                .type = OPTION_VALUE,
                     52:                .opt.value = &ciphers_config.verbose,
                     53:                .value = 1,
                     54:        },
                     55:        {
                     56:                .name = "V",
                     57:                .desc = "Provide cipher listing with cipher suite values",
                     58:                .type = OPTION_VALUE,
                     59:                .opt.value = &ciphers_config.verbose,
                     60:                .value = 2,
                     61:        },
1.3       jsing      62:        { NULL },
1.1       jsing      63: };
                     64:
1.2       jsing      65: static void
                     66: ciphers_usage(void)
                     67: {
1.5       doug       68:        fprintf(stderr, "usage: ciphers [-hVv] [-tls1] [cipherlist]\n");
1.2       jsing      69:        options_usage(ciphers_options);
                     70: }
1.1       jsing      71:
                     72: int
                     73: ciphers_main(int argc, char **argv)
                     74: {
1.2       jsing      75:        char *cipherlist = NULL;
                     76:        STACK_OF(SSL_CIPHER) *ciphers;
                     77:        const SSL_CIPHER *cipher;
                     78:        SSL_CTX *ssl_ctx = NULL;
1.1       jsing      79:        SSL *ssl = NULL;
1.2       jsing      80:        uint16_t value;
                     81:        int i, rv = 0;
1.1       jsing      82:        char *desc;
1.7     ! doug       83:
        !            84:        if (single_execution) {
        !            85:                if (pledge("stdio rpath", NULL) == -1)
        !            86:                        perror("pledge");
        !            87:        }
1.3       jsing      88:
                     89:        memset(&ciphers_config, 0, sizeof(ciphers_config));
1.1       jsing      90:
1.2       jsing      91:        if (options_parse(argc, argv, ciphers_options, &cipherlist,
                     92:            NULL) != 0) {
                     93:                ciphers_usage();
                     94:                return (1);
                     95:        }
1.1       jsing      96:
1.2       jsing      97:        if (ciphers_config.usage) {
                     98:                ciphers_usage();
                     99:                return (1);
1.1       jsing     100:        }
                    101:
1.5       doug      102:        if ((ssl_ctx = SSL_CTX_new(TLSv1_client_method())) == NULL)
1.1       jsing     103:                goto err;
1.2       jsing     104:
                    105:        if (cipherlist != NULL) {
                    106:                if (SSL_CTX_set_cipher_list(ssl_ctx, cipherlist) == 0)
1.1       jsing     107:                        goto err;
                    108:        }
1.2       jsing     109:
                    110:        if ((ssl = SSL_new(ssl_ctx)) == NULL)
1.1       jsing     111:                goto err;
                    112:
1.2       jsing     113:        if ((ciphers = SSL_get_ciphers(ssl)) == NULL)
                    114:                goto err;
                    115:
                    116:        for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
                    117:                cipher = sk_SSL_CIPHER_value(ciphers, i);
                    118:                if (ciphers_config.verbose == 0) {
                    119:                        fprintf(stdout, "%s%s", (i ? ":" : ""),
                    120:                            SSL_CIPHER_get_name(cipher));
                    121:                        continue;
                    122:                }
                    123:                if (ciphers_config.verbose > 1) {
                    124:                        value = SSL_CIPHER_get_value(cipher);
1.4       bcook     125:                        fprintf(stdout, "%-*s0x%02X,0x%02X - ", 10, "",
1.2       jsing     126:                                ((value >> 8) & 0xff), (value & 0xff));
1.1       jsing     127:                }
1.2       jsing     128:                desc = SSL_CIPHER_description(cipher, NULL, 0);
                    129:                if (strcmp(desc, "OPENSSL_malloc Error") == 0) {
                    130:                        fprintf(stderr, "out of memory\n");
                    131:                        goto err;
1.1       jsing     132:                }
1.2       jsing     133:                fprintf(stdout, "%s", desc);
                    134:                free(desc);
1.1       jsing     135:        }
1.2       jsing     136:        if (ciphers_config.verbose == 0)
                    137:                fprintf(stdout, "\n");
                    138:
                    139:        goto done;
1.1       jsing     140:
                    141: err:
1.2       jsing     142:        ERR_print_errors_fp(stderr);
                    143:        rv = 1;
1.1       jsing     144:
1.2       jsing     145: done:
                    146:        SSL_CTX_free(ssl_ctx);
                    147:        SSL_free(ssl);
1.1       jsing     148:
1.2       jsing     149:        return (rv);
1.1       jsing     150: }