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

Annotation of src/usr.bin/openssl/openssl.c, Revision 1.15

1.15    ! deraadt     1: /* $OpenBSD: openssl.c,v 1.14 2015/10/07 05:21:41 deraadt Exp $ */
1.1       jsing       2: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
                      3:  * All rights reserved.
                      4:  *
                      5:  * This package is an SSL implementation written
                      6:  * by Eric Young (eay@cryptsoft.com).
                      7:  * The implementation was written so as to conform with Netscapes SSL.
                      8:  *
                      9:  * This library is free for commercial and non-commercial use as long as
                     10:  * the following conditions are aheared to.  The following conditions
                     11:  * apply to all code found in this distribution, be it the RC4, RSA,
                     12:  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
                     13:  * included with this distribution is covered by the same copyright terms
                     14:  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
                     15:  *
                     16:  * Copyright remains Eric Young's, and as such any Copyright notices in
                     17:  * the code are not to be removed.
                     18:  * If this package is used in a product, Eric Young should be given attribution
                     19:  * as the author of the parts of the library used.
                     20:  * This can be in the form of a textual message at program startup or
                     21:  * in documentation (online or textual) provided with the package.
                     22:  *
                     23:  * Redistribution and use in source and binary forms, with or without
                     24:  * modification, are permitted provided that the following conditions
                     25:  * are met:
                     26:  * 1. Redistributions of source code must retain the copyright
                     27:  *    notice, this list of conditions and the following disclaimer.
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer in the
                     30:  *    documentation and/or other materials provided with the distribution.
                     31:  * 3. All advertising materials mentioning features or use of this software
                     32:  *    must display the following acknowledgement:
                     33:  *    "This product includes cryptographic software written by
                     34:  *     Eric Young (eay@cryptsoft.com)"
                     35:  *    The word 'cryptographic' can be left out if the rouines from the library
                     36:  *    being used are not cryptographic related :-).
                     37:  * 4. If you include any Windows specific code (or a derivative thereof) from
                     38:  *    the apps directory (application code) you must include an acknowledgement:
                     39:  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
                     40:  *
                     41:  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
                     42:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     43:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     44:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     45:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     46:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     47:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     48:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     49:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     50:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     51:  * SUCH DAMAGE.
                     52:  *
                     53:  * The licence and distribution terms for any publically available version or
                     54:  * derivative of this code cannot be changed.  i.e. this code cannot simply be
                     55:  * copied and put under another distribution licence
                     56:  * [including the GNU Public Licence.]
                     57:  */
                     58: /* ====================================================================
                     59:  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
                     60:  *
                     61:  * Redistribution and use in source and binary forms, with or without
                     62:  * modification, are permitted provided that the following conditions
                     63:  * are met:
                     64:  *
                     65:  * 1. Redistributions of source code must retain the above copyright
                     66:  *    notice, this list of conditions and the following disclaimer.
                     67:  *
                     68:  * 2. Redistributions in binary form must reproduce the above copyright
                     69:  *    notice, this list of conditions and the following disclaimer in
                     70:  *    the documentation and/or other materials provided with the
                     71:  *    distribution.
                     72:  *
                     73:  * 3. All advertising materials mentioning features or use of this
                     74:  *    software must display the following acknowledgment:
                     75:  *    "This product includes software developed by the OpenSSL Project
                     76:  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
                     77:  *
                     78:  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
                     79:  *    endorse or promote products derived from this software without
                     80:  *    prior written permission. For written permission, please contact
                     81:  *    openssl-core@openssl.org.
                     82:  *
                     83:  * 5. Products derived from this software may not be called "OpenSSL"
                     84:  *    nor may "OpenSSL" appear in their names without prior written
                     85:  *    permission of the OpenSSL Project.
                     86:  *
                     87:  * 6. Redistributions of any form whatsoever must retain the following
                     88:  *    acknowledgment:
                     89:  *    "This product includes software developed by the OpenSSL Project
                     90:  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
                     91:  *
                     92:  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
                     93:  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     94:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     95:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
                     96:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     97:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     98:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                     99:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                    100:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                    101:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                    102:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
                    103:  * OF THE POSSIBILITY OF SUCH DAMAGE.
                    104:  * ====================================================================
                    105:  *
                    106:  * This product includes cryptographic software written by Eric Young
                    107:  * (eay@cryptsoft.com).  This product includes software written by Tim
                    108:  * Hudson (tjh@cryptsoft.com).
                    109:  *
                    110:  */
                    111:
                    112: #include <err.h>
                    113: #include <signal.h>
                    114: #include <stdio.h>
                    115: #include <string.h>
                    116: #include <stdlib.h>
1.14      deraadt   117: #include <unistd.h>
1.1       jsing     118:
                    119: #include "apps.h"
                    120:
                    121: #include <openssl/bio.h>
                    122: #include <openssl/conf.h>
                    123: #include <openssl/crypto.h>
                    124: #include <openssl/err.h>
                    125: #include <openssl/lhash.h>
                    126: #include <openssl/pem.h>
                    127: #include <openssl/ssl.h>
                    128: #include <openssl/x509.h>
                    129:
                    130: #include "progs.h"
                    131: #include "s_apps.h"
1.5       jsing     132:
                    133: #define FUNC_TYPE_GENERAL       1
                    134: #define FUNC_TYPE_MD            2
                    135: #define FUNC_TYPE_CIPHER        3
                    136: #define FUNC_TYPE_PKEY          4
                    137: #define FUNC_TYPE_MD_ALG        5
                    138: #define FUNC_TYPE_CIPHER_ALG    6
                    139:
                    140: typedef struct {
                    141:         int type;
                    142:         const char *name;
                    143:         int (*func)(int argc, char **argv);
                    144: } FUNCTION;
                    145:
                    146: DECLARE_LHASH_OF(FUNCTION);
1.4       deraadt   147:
                    148: FUNCTION functions[] = {
                    149:
                    150:        /* General functions. */
                    151:        { FUNC_TYPE_GENERAL, "asn1parse", asn1parse_main },
                    152:        { FUNC_TYPE_GENERAL, "ca", ca_main },
                    153:        { FUNC_TYPE_GENERAL, "certhash", certhash_main },
                    154:        { FUNC_TYPE_GENERAL, "ciphers", ciphers_main },
                    155: #ifndef OPENSSL_NO_CMS
                    156:        { FUNC_TYPE_GENERAL, "cms", cms_main },
                    157: #endif
                    158:        { FUNC_TYPE_GENERAL, "crl2pkcs7", crl2pkcs7_main },
                    159:        { FUNC_TYPE_GENERAL, "crl", crl_main },
                    160:        { FUNC_TYPE_GENERAL, "dgst", dgst_main },
                    161:        { FUNC_TYPE_GENERAL, "enc", enc_main },
                    162:        { FUNC_TYPE_GENERAL, "errstr", errstr_main },
                    163:        { FUNC_TYPE_GENERAL, "genpkey", genpkey_main },
                    164:        { FUNC_TYPE_GENERAL, "nseq", nseq_main },
                    165: #ifndef OPENSSL_NO_OCSP
                    166:        { FUNC_TYPE_GENERAL, "ocsp", ocsp_main },
                    167: #endif
                    168:        { FUNC_TYPE_GENERAL, "passwd", passwd_main },
                    169:        { FUNC_TYPE_GENERAL, "pkcs7", pkcs7_main },
                    170:        { FUNC_TYPE_GENERAL, "pkcs8", pkcs8_main },
                    171: #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
                    172:        { FUNC_TYPE_GENERAL, "pkcs12", pkcs12_main },
                    173: #endif
                    174:        { FUNC_TYPE_GENERAL, "pkey", pkey_main },
                    175:        { FUNC_TYPE_GENERAL, "pkeyparam", pkeyparam_main },
                    176:        { FUNC_TYPE_GENERAL, "pkeyutl", pkeyutl_main },
                    177:        { FUNC_TYPE_GENERAL, "prime", prime_main },
                    178:        { FUNC_TYPE_GENERAL, "rand", rand_main },
                    179:        { FUNC_TYPE_GENERAL, "req", req_main },
                    180:        { FUNC_TYPE_GENERAL, "s_client", s_client_main },
                    181:        { FUNC_TYPE_GENERAL, "s_server", s_server_main },
                    182:        { FUNC_TYPE_GENERAL, "s_time", s_time_main },
                    183:        { FUNC_TYPE_GENERAL, "sess_id", sess_id_main },
                    184:        { FUNC_TYPE_GENERAL, "smime", smime_main },
                    185: #ifndef OPENSSL_NO_SPEED
                    186:        { FUNC_TYPE_GENERAL, "speed", speed_main },
                    187: #endif
                    188:        { FUNC_TYPE_GENERAL, "spkac", spkac_main },
                    189:        { FUNC_TYPE_GENERAL, "ts", ts_main },
                    190:        { FUNC_TYPE_GENERAL, "verify", verify_main },
                    191:        { FUNC_TYPE_GENERAL, "version", version_main },
                    192:        { FUNC_TYPE_GENERAL, "x509", x509_main },
                    193:
                    194: #ifndef OPENSSL_NO_DH
                    195:        { FUNC_TYPE_GENERAL, "dh", dh_main },
                    196:        { FUNC_TYPE_GENERAL, "dhparam", dhparam_main },
                    197:        { FUNC_TYPE_GENERAL, "gendh", gendh_main },
                    198: #endif
                    199: #ifndef OPENSSL_NO_DSA
                    200:        { FUNC_TYPE_GENERAL, "dsa", dsa_main },
                    201:        { FUNC_TYPE_GENERAL, "dsaparam", dsaparam_main },
                    202:        { FUNC_TYPE_GENERAL, "gendsa", gendsa_main },
                    203: #endif
                    204: #ifndef OPENSSL_NO_EC
                    205:        { FUNC_TYPE_GENERAL, "ec", ec_main },
                    206:        { FUNC_TYPE_GENERAL, "ecparam", ecparam_main },
                    207: #endif
                    208: #ifndef OPENSSL_NO_RSA
                    209:        { FUNC_TYPE_GENERAL, "genrsa", genrsa_main },
                    210:        { FUNC_TYPE_GENERAL, "rsa", rsa_main },
                    211:        { FUNC_TYPE_GENERAL, "rsautl", rsautl_main },
                    212: #endif
                    213:
                    214:        /* Message Digests. */
                    215: #ifndef OPENSSL_NO_GOST
                    216:        { FUNC_TYPE_MD, "gost-mac", dgst_main },
                    217:        { FUNC_TYPE_MD, "md_gost94", dgst_main },
                    218:        { FUNC_TYPE_MD, "streebog256", dgst_main },
                    219:        { FUNC_TYPE_MD, "streebog512", dgst_main },
1.12      doug      220: #endif
                    221: #ifndef OPENSSL_NO_MD4
                    222:        { FUNC_TYPE_MD, "md4", dgst_main },
1.4       deraadt   223: #endif
                    224: #ifndef OPENSSL_NO_MD5
                    225:        { FUNC_TYPE_MD, "md5", dgst_main },
                    226: #endif
                    227: #ifndef OPENSSL_NO_RIPEMD160
                    228:        { FUNC_TYPE_MD, "ripemd160", dgst_main },
                    229: #endif
                    230: #ifndef OPENSSL_NO_SHA1
                    231:        { FUNC_TYPE_MD, "sha1", dgst_main },
                    232: #endif
                    233: #ifndef OPENSSL_NO_SHA224
                    234:        { FUNC_TYPE_MD, "sha224", dgst_main },
                    235: #endif
                    236: #ifndef OPENSSL_NO_SHA256
                    237:        { FUNC_TYPE_MD, "sha256", dgst_main },
                    238: #endif
                    239: #ifndef OPENSSL_NO_SHA384
                    240:        { FUNC_TYPE_MD, "sha384", dgst_main },
                    241: #endif
                    242: #ifndef OPENSSL_NO_SHA512
                    243:        { FUNC_TYPE_MD, "sha512", dgst_main },
                    244: #endif
                    245: #ifndef OPENSSL_NO_WHIRLPOOL
                    246:        { FUNC_TYPE_MD, "whirlpool", dgst_main },
                    247: #endif
                    248:
                    249:        /* Ciphers. */
                    250:        { FUNC_TYPE_CIPHER, "base64", enc_main },
                    251: #ifndef OPENSSL_NO_AES
                    252:        { FUNC_TYPE_CIPHER, "aes-128-cbc", enc_main },
                    253:        { FUNC_TYPE_CIPHER, "aes-128-ecb", enc_main },
                    254:        { FUNC_TYPE_CIPHER, "aes-192-cbc", enc_main },
                    255:        { FUNC_TYPE_CIPHER, "aes-192-ecb", enc_main },
                    256:        { FUNC_TYPE_CIPHER, "aes-256-cbc", enc_main },
                    257:        { FUNC_TYPE_CIPHER, "aes-256-ecb", enc_main },
                    258: #endif
                    259: #ifndef OPENSSL_NO_BF
                    260:        { FUNC_TYPE_CIPHER, "bf", enc_main },
                    261:        { FUNC_TYPE_CIPHER, "bf-cbc", enc_main },
                    262:        { FUNC_TYPE_CIPHER, "bf-ecb", enc_main },
                    263:        { FUNC_TYPE_CIPHER, "bf-cfb", enc_main },
                    264:        { FUNC_TYPE_CIPHER, "bf-ofb", enc_main },
                    265: #endif
                    266: #ifndef OPENSSL_NO_CAMELLIA
                    267:        { FUNC_TYPE_CIPHER, "camellia-128-cbc", enc_main },
                    268:        { FUNC_TYPE_CIPHER, "camellia-128-ecb", enc_main },
                    269:        { FUNC_TYPE_CIPHER, "camellia-192-cbc", enc_main },
                    270:        { FUNC_TYPE_CIPHER, "camellia-192-ecb", enc_main },
                    271:        { FUNC_TYPE_CIPHER, "camellia-256-cbc", enc_main },
                    272:        { FUNC_TYPE_CIPHER, "camellia-256-ecb", enc_main },
                    273: #endif
                    274: #ifndef OPENSSL_NO_CAST
                    275:        { FUNC_TYPE_CIPHER, "cast", enc_main },
                    276:        { FUNC_TYPE_CIPHER, "cast5-cbc", enc_main },
                    277:        { FUNC_TYPE_CIPHER, "cast5-ecb", enc_main },
                    278:        { FUNC_TYPE_CIPHER, "cast5-cfb", enc_main },
                    279:        { FUNC_TYPE_CIPHER, "cast5-ofb", enc_main },
                    280:        { FUNC_TYPE_CIPHER, "cast-cbc", enc_main },
                    281: #endif
                    282: #ifndef OPENSSL_NO_CHACHA
                    283:        { FUNC_TYPE_CIPHER, "chacha", enc_main },
                    284: #endif
                    285: #ifndef OPENSSL_NO_DES
                    286:        { FUNC_TYPE_CIPHER, "des", enc_main },
                    287:        { FUNC_TYPE_CIPHER, "des3", enc_main },
                    288:        { FUNC_TYPE_CIPHER, "desx", enc_main },
                    289:        { FUNC_TYPE_CIPHER, "des-ecb", enc_main },
                    290:        { FUNC_TYPE_CIPHER, "des-ede", enc_main },
                    291:        { FUNC_TYPE_CIPHER, "des-ede3", enc_main },
                    292:        { FUNC_TYPE_CIPHER, "des-cbc", enc_main },
                    293:        { FUNC_TYPE_CIPHER, "des-ede-cbc", enc_main },
                    294:        { FUNC_TYPE_CIPHER, "des-ede3-cbc", enc_main },
                    295:        { FUNC_TYPE_CIPHER, "des-cfb", enc_main },
                    296:        { FUNC_TYPE_CIPHER, "des-ede-cfb", enc_main },
                    297:        { FUNC_TYPE_CIPHER, "des-ede3-cfb", enc_main },
                    298:        { FUNC_TYPE_CIPHER, "des-ofb", enc_main },
                    299:        { FUNC_TYPE_CIPHER, "des-ede-ofb", enc_main },
                    300:        { FUNC_TYPE_CIPHER, "des-ede3-ofb", enc_main },
                    301: #endif
                    302: #ifndef OPENSSL_NO_IDEA
                    303:        { FUNC_TYPE_CIPHER, "idea", enc_main },
                    304:        { FUNC_TYPE_CIPHER, "idea-cbc", enc_main },
                    305:        { FUNC_TYPE_CIPHER, "idea-ecb", enc_main },
                    306:        { FUNC_TYPE_CIPHER, "idea-cfb", enc_main },
                    307:        { FUNC_TYPE_CIPHER, "idea-ofb", enc_main },
                    308: #endif
                    309: #ifndef OPENSSL_NO_RC2
                    310:        { FUNC_TYPE_CIPHER, "rc2", enc_main },
                    311:        { FUNC_TYPE_CIPHER, "rc2-cbc", enc_main },
                    312:        { FUNC_TYPE_CIPHER, "rc2-ecb", enc_main },
                    313:        { FUNC_TYPE_CIPHER, "rc2-cfb", enc_main },
                    314:        { FUNC_TYPE_CIPHER, "rc2-ofb", enc_main },
                    315:        { FUNC_TYPE_CIPHER, "rc2-64-cbc", enc_main },
                    316:        { FUNC_TYPE_CIPHER, "rc2-40-cbc", enc_main },
                    317: #endif
                    318: #ifndef OPENSSL_NO_RC4
                    319:        { FUNC_TYPE_CIPHER, "rc4", enc_main },
                    320:        { FUNC_TYPE_CIPHER, "rc4-40", enc_main },
                    321: #endif
                    322: #ifdef ZLIB
                    323:        { FUNC_TYPE_CIPHER, "zlib", enc_main },
                    324: #endif
                    325:
                    326:        { 0, NULL, NULL }
                    327: };
1.1       jsing     328:
                    329: static void openssl_startup(void);
                    330: static void openssl_shutdown(void);
                    331:
                    332: /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
                    333:  * base prototypes (we cast each variable inside the function to the required
                    334:  * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
                    335:  * functions. */
                    336:
                    337: static LHASH_OF(FUNCTION) *prog_init(void);
                    338: static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
                    339: static void list_pkey(BIO * out);
                    340: static void list_cipher(BIO * out);
                    341: static void list_md(BIO * out);
                    342: char *default_config_file = NULL;
                    343:
                    344: CONF *config = NULL;
                    345: BIO *bio_err = NULL;
                    346:
                    347: static void
                    348: lock_dbg_cb(int mode, int type, const char *file, int line)
                    349: {
                    350:        static int modes[CRYPTO_NUM_LOCKS];     /* = {0, 0, ... } */
                    351:        const char *errstr = NULL;
                    352:        int rw;
                    353:
                    354:        rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
                    355:        if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
                    356:                errstr = "invalid mode";
                    357:                goto err;
                    358:        }
                    359:        if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
                    360:                errstr = "type out of bounds";
                    361:                goto err;
                    362:        }
                    363:        if (mode & CRYPTO_LOCK) {
                    364:                if (modes[type]) {
                    365:                        errstr = "already locked";
                    366:                        /*
                    367:                         * must not happen in a single-threaded program
                    368:                         * (would deadlock)
                    369:                         */
                    370:                        goto err;
                    371:                }
                    372:                modes[type] = rw;
                    373:        } else if (mode & CRYPTO_UNLOCK) {
                    374:                if (!modes[type]) {
                    375:                        errstr = "not locked";
                    376:                        goto err;
                    377:                }
                    378:                if (modes[type] != rw) {
                    379:                        errstr = (rw == CRYPTO_READ) ?
                    380:                            "CRYPTO_r_unlock on write lock" :
                    381:                            "CRYPTO_w_unlock on read lock";
                    382:                }
                    383:                modes[type] = 0;
                    384:        } else {
                    385:                errstr = "invalid mode";
                    386:                goto err;
                    387:        }
                    388:
                    389: err:
                    390:        if (errstr) {
                    391:                /* we cannot use bio_err here */
                    392:                fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
                    393:                    errstr, mode, type, file, line);
                    394:        }
                    395: }
                    396:
                    397: static void
                    398: openssl_startup(void)
                    399: {
                    400:        signal(SIGPIPE, SIG_IGN);
                    401:
                    402:        OpenSSL_add_all_algorithms();
                    403:        SSL_library_init();
                    404:        SSL_load_error_strings();
                    405:
1.10      bcook     406:        setup_ui();
1.1       jsing     407: }
                    408:
                    409: static void
                    410: openssl_shutdown(void)
                    411: {
                    412:        CONF_modules_unload(1);
1.10      bcook     413:        destroy_ui();
1.1       jsing     414:        OBJ_cleanup();
                    415:        EVP_cleanup();
                    416:        CRYPTO_cleanup_all_ex_data();
                    417:        ERR_remove_thread_state(NULL);
                    418:        ERR_free_strings();
                    419: }
                    420:
                    421: int
                    422: main(int argc, char **argv)
                    423: {
                    424:        ARGS arg;
                    425: #define PROG_NAME_SIZE 39
                    426:        char pname[PROG_NAME_SIZE + 1];
                    427:        FUNCTION f, *fp;
                    428:        const char *prompt;
                    429:        char buf[1024];
                    430:        char *to_free = NULL;
                    431:        int n, i, ret = 0;
                    432:        char *p;
                    433:        LHASH_OF(FUNCTION) * prog = NULL;
                    434:        long errline;
                    435:
                    436:        arg.data = NULL;
                    437:        arg.count = 0;
1.14      deraadt   438:
1.15    ! deraadt   439:        if (pledge("stdio inet rpath cpath wpath proc", NULL) == -1) {
        !           440:                fprintf(stderr, "openssl: pledge: %s\n", strerror(errno));
1.14      deraadt   441:                exit(1);
                    442:        }
1.1       jsing     443:
                    444:        bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
                    445:        if (bio_err == NULL) {
                    446:                fprintf(stderr, "openssl: failed to initialise bio_err\n");
1.3       bcook     447:                exit(1);
                    448:        }
                    449:
                    450:        if (BIO_sock_init() != 1) {
                    451:                BIO_printf(bio_err, "BIO_sock_init failed\n");
1.1       jsing     452:                exit(1);
                    453:        }
                    454:
                    455:        CRYPTO_set_locking_callback(lock_dbg_cb);
                    456:
                    457:        openssl_startup();
                    458:
                    459:        /* Lets load up our environment a little */
                    460:        p = getenv("OPENSSL_CONF");
                    461:        if (p == NULL) {
                    462:                p = to_free = make_config_name();
                    463:                if (p == NULL) {
                    464:                        BIO_printf(bio_err, "error making config file name\n");
                    465:                        goto end;
                    466:                }
                    467:        }
                    468:
                    469:        default_config_file = p;
                    470:
                    471:        config = NCONF_new(NULL);
                    472:        i = NCONF_load(config, p, &errline);
                    473:        if (i == 0) {
                    474:                if (ERR_GET_REASON(ERR_peek_last_error()) ==
                    475:                    CONF_R_NO_SUCH_FILE) {
                    476:                        BIO_printf(bio_err,
                    477:                            "WARNING: can't open config file: %s\n", p);
                    478:                        ERR_clear_error();
                    479:                        NCONF_free(config);
                    480:                        config = NULL;
                    481:                } else {
                    482:                        ERR_print_errors(bio_err);
                    483:                        NCONF_free(config);
                    484:                        exit(1);
                    485:                }
                    486:        }
                    487:
                    488:        if (!load_config(bio_err, NULL)) {
                    489:                BIO_printf(bio_err, "failed to load configuration\n");
                    490:                goto end;
                    491:        }
                    492:
                    493:        prog = prog_init();
                    494:
                    495:        /* first check the program name */
                    496:        program_name(argv[0], pname, sizeof pname);
                    497:
                    498:        f.name = pname;
                    499:        fp = lh_FUNCTION_retrieve(prog, &f);
                    500:        if (fp != NULL) {
                    501:                argv[0] = pname;
                    502:                ret = fp->func(argc, argv);
                    503:                goto end;
                    504:        }
                    505:        /*
                    506:         * ok, now check that there are not arguments, if there are, run with
                    507:         * them, shifting the ssleay off the front
                    508:         */
                    509:        if (argc != 1) {
                    510:                argc--;
                    511:                argv++;
                    512:                ret = do_cmd(prog, argc, argv);
                    513:                if (ret < 0)
                    514:                        ret = 0;
                    515:                goto end;
                    516:        }
                    517:        /* ok, lets enter the old 'OpenSSL>' mode */
                    518:
                    519:        for (;;) {
                    520:                ret = 0;
                    521:                p = buf;
                    522:                n = sizeof buf;
                    523:                i = 0;
                    524:                for (;;) {
                    525:                        p[0] = '\0';
                    526:                        if (i++)
                    527:                                prompt = ">";
                    528:                        else
                    529:                                prompt = "OpenSSL> ";
                    530:                        fputs(prompt, stdout);
                    531:                        fflush(stdout);
                    532:                        if (!fgets(p, n, stdin))
                    533:                                goto end;
                    534:                        if (p[0] == '\0')
                    535:                                goto end;
                    536:                        i = strlen(p);
                    537:                        if (i <= 1)
                    538:                                break;
                    539:                        if (p[i - 2] != '\\')
                    540:                                break;
                    541:                        i -= 2;
                    542:                        p += i;
                    543:                        n -= i;
                    544:                }
                    545:                if (!chopup_args(&arg, buf, &argc, &argv))
                    546:                        break;
                    547:
                    548:                ret = do_cmd(prog, argc, argv);
                    549:                if (ret < 0) {
                    550:                        ret = 0;
                    551:                        goto end;
                    552:                }
                    553:                if (ret != 0)
                    554:                        BIO_printf(bio_err, "error in %s\n", argv[0]);
                    555:                (void) BIO_flush(bio_err);
                    556:        }
                    557:        BIO_printf(bio_err, "bad exit\n");
                    558:        ret = 1;
                    559:
                    560: end:
                    561:        free(to_free);
                    562:
                    563:        if (config != NULL) {
                    564:                NCONF_free(config);
                    565:                config = NULL;
                    566:        }
                    567:        if (prog != NULL)
                    568:                lh_FUNCTION_free(prog);
                    569:        free(arg.data);
                    570:
                    571:        openssl_shutdown();
                    572:
                    573:        if (bio_err != NULL) {
                    574:                BIO_free(bio_err);
                    575:                bio_err = NULL;
                    576:        }
                    577:        return (ret);
                    578: }
                    579:
                    580: #define LIST_STANDARD_COMMANDS "list-standard-commands"
                    581: #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
                    582: #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
                    583: #define LIST_CIPHER_COMMANDS "list-cipher-commands"
                    584: #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
                    585: #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
                    586:
                    587:
                    588: static int
                    589: do_cmd(LHASH_OF(FUNCTION) * prog, int argc, char *argv[])
                    590: {
                    591:        FUNCTION f, *fp;
                    592:        int i, ret = 1, tp, nl;
                    593:
                    594:        if ((argc <= 0) || (argv[0] == NULL)) {
                    595:                ret = 0;
                    596:                goto end;
                    597:        }
                    598:        f.name = argv[0];
                    599:        fp = lh_FUNCTION_retrieve(prog, &f);
                    600:        if (fp == NULL) {
                    601:                if (EVP_get_digestbyname(argv[0])) {
                    602:                        f.type = FUNC_TYPE_MD;
                    603:                        f.func = dgst_main;
                    604:                        fp = &f;
                    605:                } else if (EVP_get_cipherbyname(argv[0])) {
                    606:                        f.type = FUNC_TYPE_CIPHER;
                    607:                        f.func = enc_main;
                    608:                        fp = &f;
                    609:                }
                    610:        }
                    611:        if (fp != NULL) {
                    612:                ret = fp->func(argc, argv);
                    613:        } else if ((strncmp(argv[0], "no-", 3)) == 0) {
                    614:                BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
                    615:                f.name = argv[0] + 3;
                    616:                ret = (lh_FUNCTION_retrieve(prog, &f) != NULL);
                    617:                if (!ret)
                    618:                        BIO_printf(bio_stdout, "%s\n", argv[0]);
                    619:                else
                    620:                        BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
                    621:                BIO_free_all(bio_stdout);
                    622:                goto end;
                    623:        } else if ((strcmp(argv[0], "quit") == 0) ||
                    624:            (strcmp(argv[0], "q") == 0) ||
                    625:            (strcmp(argv[0], "exit") == 0) ||
                    626:            (strcmp(argv[0], "bye") == 0)) {
                    627:                ret = -1;
                    628:                goto end;
                    629:        } else if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
                    630:            (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
                    631:            (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
                    632:            (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) ||
                    633:            (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) ||
                    634:            (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) {
                    635:                int list_type;
                    636:                BIO *bio_stdout;
                    637:
                    638:                if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
                    639:                        list_type = FUNC_TYPE_GENERAL;
                    640:                else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
                    641:                        list_type = FUNC_TYPE_MD;
                    642:                else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
                    643:                        list_type = FUNC_TYPE_MD_ALG;
                    644:                else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)
                    645:                        list_type = FUNC_TYPE_PKEY;
                    646:                else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0)
                    647:                        list_type = FUNC_TYPE_CIPHER_ALG;
                    648:                else            /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
                    649:                        list_type = FUNC_TYPE_CIPHER;
                    650:                bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
                    651:
                    652:                if (list_type == FUNC_TYPE_PKEY)
                    653:                        list_pkey(bio_stdout);
                    654:                if (list_type == FUNC_TYPE_MD_ALG)
                    655:                        list_md(bio_stdout);
                    656:                if (list_type == FUNC_TYPE_CIPHER_ALG)
                    657:                        list_cipher(bio_stdout);
                    658:                else {
                    659:                        for (fp = functions; fp->name != NULL; fp++)
                    660:                                if (fp->type == list_type)
                    661:                                        BIO_printf(bio_stdout, "%s\n",
                    662:                                            fp->name);
                    663:                }
                    664:                BIO_free_all(bio_stdout);
                    665:                ret = 0;
                    666:                goto end;
                    667:        } else {
                    668:                BIO_printf(bio_err,
                    669:                    "openssl:Error: '%s' is an invalid command.\n",
                    670:                    argv[0]);
                    671:                BIO_printf(bio_err, "\nStandard commands");
                    672:                i = 0;
                    673:                tp = 0;
                    674:                for (fp = functions; fp->name != NULL; fp++) {
                    675:                        nl = 0;
                    676: #ifdef OPENSSL_NO_CAMELLIA
                    677:                        if (((i++) % 5) == 0)
                    678: #else
                    679:                        if (((i++) % 4) == 0)
                    680: #endif
                    681:                        {
                    682:                                BIO_printf(bio_err, "\n");
                    683:                                nl = 1;
                    684:                        }
                    685:                        if (fp->type != tp) {
                    686:                                tp = fp->type;
                    687:                                if (!nl)
                    688:                                        BIO_printf(bio_err, "\n");
                    689:                                if (tp == FUNC_TYPE_MD) {
                    690:                                        i = 1;
                    691:                                        BIO_printf(bio_err,
                    692:                                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
                    693:                                } else if (tp == FUNC_TYPE_CIPHER) {
                    694:                                        i = 1;
                    695:                                        BIO_printf(bio_err, "\nCipher commands (see the `enc' command for more details)\n");
                    696:                                }
                    697:                        }
                    698: #ifdef OPENSSL_NO_CAMELLIA
                    699:                        BIO_printf(bio_err, "%-15s", fp->name);
                    700: #else
                    701:                        BIO_printf(bio_err, "%-18s", fp->name);
                    702: #endif
                    703:                }
                    704:                BIO_printf(bio_err, "\n\n");
                    705:                ret = 0;
                    706:        }
                    707: end:
                    708:        return (ret);
                    709: }
                    710:
                    711: static int
                    712: SortFnByName(const void *_f1, const void *_f2)
                    713: {
                    714:        const FUNCTION *f1 = _f1;
                    715:        const FUNCTION *f2 = _f2;
                    716:
                    717:        if (f1->type != f2->type)
                    718:                return f1->type - f2->type;
                    719:        return strcmp(f1->name, f2->name);
                    720: }
                    721:
                    722: static void
                    723: list_pkey(BIO * out)
                    724: {
                    725:        int i;
                    726:
                    727:        for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
                    728:                const EVP_PKEY_ASN1_METHOD *ameth;
                    729:                int pkey_id, pkey_base_id, pkey_flags;
                    730:                const char *pinfo, *pem_str;
                    731:                ameth = EVP_PKEY_asn1_get0(i);
                    732:                EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
                    733:                    &pinfo, &pem_str, ameth);
                    734:                if (pkey_flags & ASN1_PKEY_ALIAS) {
                    735:                        BIO_printf(out, "Name: %s\n",
                    736:                            OBJ_nid2ln(pkey_id));
                    737:                        BIO_printf(out, "\tType: Alias to %s\n",
                    738:                            OBJ_nid2ln(pkey_base_id));
                    739:                } else {
                    740:                        BIO_printf(out, "Name: %s\n", pinfo);
                    741:                        BIO_printf(out, "\tType: %s Algorithm\n",
                    742:                            pkey_flags & ASN1_PKEY_DYNAMIC ?
                    743:                            "External" : "Builtin");
                    744:                        BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
                    745:                        if (pem_str == NULL)
                    746:                                pem_str = "(none)";
                    747:                        BIO_printf(out, "\tPEM string: %s\n", pem_str);
                    748:                }
                    749:
                    750:        }
                    751: }
                    752:
                    753: static void
                    754: list_cipher_fn(const EVP_CIPHER * c, const char *from, const char *to,
                    755:     void *arg)
                    756: {
                    757:        if (c)
                    758:                BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
                    759:        else {
                    760:                if (!from)
                    761:                        from = "<undefined>";
                    762:                if (!to)
                    763:                        to = "<undefined>";
                    764:                BIO_printf(arg, "%s => %s\n", from, to);
                    765:        }
                    766: }
                    767:
                    768: static void
                    769: list_cipher(BIO * out)
                    770: {
                    771:        EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
                    772: }
                    773:
                    774: static void
                    775: list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg)
                    776: {
                    777:        if (m)
                    778:                BIO_printf(arg, "%s\n", EVP_MD_name(m));
                    779:        else {
                    780:                if (!from)
                    781:                        from = "<undefined>";
                    782:                if (!to)
                    783:                        to = "<undefined>";
                    784:                BIO_printf(arg, "%s => %s\n", from, to);
                    785:        }
                    786: }
                    787:
                    788: static void
                    789: list_md(BIO * out)
                    790: {
                    791:        EVP_MD_do_all_sorted(list_md_fn, out);
                    792: }
                    793:
                    794: static int
                    795: function_cmp(const FUNCTION * a, const FUNCTION * b)
                    796: {
                    797:        return strncmp(a->name, b->name, 8);
                    798: }
                    799:
                    800: static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
                    801:
                    802: static unsigned long
                    803: function_hash(const FUNCTION * a)
                    804: {
                    805:        return lh_strhash(a->name);
                    806: }
                    807:
                    808: static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
                    809:
                    810: static LHASH_OF(FUNCTION) *
                    811: prog_init(void)
                    812: {
                    813:        LHASH_OF(FUNCTION) * ret;
                    814:        FUNCTION *f;
                    815:        size_t i;
                    816:
                    817:        /* Purely so it looks nice when the user hits ? */
                    818:        for (i = 0, f = functions; f->name != NULL; ++f, ++i)
                    819:                ;
                    820:        qsort(functions, i, sizeof *functions, SortFnByName);
                    821:
                    822:        if ((ret = lh_FUNCTION_new()) == NULL)
                    823:                return (NULL);
                    824:
                    825:        for (f = functions; f->name != NULL; f++)
                    826:                (void) lh_FUNCTION_insert(ret, f);
                    827:        return (ret);
                    828: }