[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.4

1.4     ! deraadt     1: /* $OpenBSD: openssl.c,v 1.3 2015/03/22 10:36:22 bcook 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>
                    117:
                    118: #include "apps.h"
                    119:
                    120: #include <openssl/bio.h>
                    121: #include <openssl/conf.h>
                    122: #include <openssl/crypto.h>
                    123: #include <openssl/err.h>
                    124: #include <openssl/lhash.h>
                    125: #include <openssl/pem.h>
                    126: #include <openssl/ssl.h>
                    127: #include <openssl/x509.h>
                    128:
                    129: #ifndef OPENSSL_NO_ENGINE
                    130: #include <openssl/engine.h>
                    131: #endif
                    132:
                    133: #include "progs.h"
                    134: #include "s_apps.h"
1.4     ! deraadt   135:
        !           136: FUNCTION functions[] = {
        !           137:
        !           138:        /* General functions. */
        !           139:        { FUNC_TYPE_GENERAL, "asn1parse", asn1parse_main },
        !           140:        { FUNC_TYPE_GENERAL, "ca", ca_main },
        !           141:        { FUNC_TYPE_GENERAL, "certhash", certhash_main },
        !           142:        { FUNC_TYPE_GENERAL, "ciphers", ciphers_main },
        !           143: #ifndef OPENSSL_NO_CMS
        !           144:        { FUNC_TYPE_GENERAL, "cms", cms_main },
        !           145: #endif
        !           146:        { FUNC_TYPE_GENERAL, "crl2pkcs7", crl2pkcs7_main },
        !           147:        { FUNC_TYPE_GENERAL, "crl", crl_main },
        !           148:        { FUNC_TYPE_GENERAL, "dgst", dgst_main },
        !           149:        { FUNC_TYPE_GENERAL, "enc", enc_main },
        !           150: #ifndef OPENSSL_NO_ENGINE
        !           151:        { FUNC_TYPE_GENERAL, "engine", engine_main },
        !           152: #endif
        !           153:        { FUNC_TYPE_GENERAL, "errstr", errstr_main },
        !           154:        { FUNC_TYPE_GENERAL, "genpkey", genpkey_main },
        !           155:        { FUNC_TYPE_GENERAL, "nseq", nseq_main },
        !           156: #ifndef OPENSSL_NO_OCSP
        !           157:        { FUNC_TYPE_GENERAL, "ocsp", ocsp_main },
        !           158: #endif
        !           159:        { FUNC_TYPE_GENERAL, "passwd", passwd_main },
        !           160:        { FUNC_TYPE_GENERAL, "pkcs7", pkcs7_main },
        !           161:        { FUNC_TYPE_GENERAL, "pkcs8", pkcs8_main },
        !           162: #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1)
        !           163:        { FUNC_TYPE_GENERAL, "pkcs12", pkcs12_main },
        !           164: #endif
        !           165:        { FUNC_TYPE_GENERAL, "pkey", pkey_main },
        !           166:        { FUNC_TYPE_GENERAL, "pkeyparam", pkeyparam_main },
        !           167:        { FUNC_TYPE_GENERAL, "pkeyutl", pkeyutl_main },
        !           168:        { FUNC_TYPE_GENERAL, "prime", prime_main },
        !           169:        { FUNC_TYPE_GENERAL, "rand", rand_main },
        !           170:        { FUNC_TYPE_GENERAL, "req", req_main },
        !           171:        { FUNC_TYPE_GENERAL, "s_client", s_client_main },
        !           172:        { FUNC_TYPE_GENERAL, "s_server", s_server_main },
        !           173:        { FUNC_TYPE_GENERAL, "s_time", s_time_main },
        !           174:        { FUNC_TYPE_GENERAL, "sess_id", sess_id_main },
        !           175:        { FUNC_TYPE_GENERAL, "smime", smime_main },
        !           176: #ifndef OPENSSL_NO_SPEED
        !           177:        { FUNC_TYPE_GENERAL, "speed", speed_main },
        !           178: #endif
        !           179:        { FUNC_TYPE_GENERAL, "spkac", spkac_main },
        !           180:        { FUNC_TYPE_GENERAL, "ts", ts_main },
        !           181:        { FUNC_TYPE_GENERAL, "verify", verify_main },
        !           182:        { FUNC_TYPE_GENERAL, "version", version_main },
        !           183:        { FUNC_TYPE_GENERAL, "x509", x509_main },
        !           184:
        !           185: #ifndef OPENSSL_NO_DH
        !           186:        { FUNC_TYPE_GENERAL, "dh", dh_main },
        !           187:        { FUNC_TYPE_GENERAL, "dhparam", dhparam_main },
        !           188:        { FUNC_TYPE_GENERAL, "gendh", gendh_main },
        !           189: #endif
        !           190: #ifndef OPENSSL_NO_DSA
        !           191:        { FUNC_TYPE_GENERAL, "dsa", dsa_main },
        !           192:        { FUNC_TYPE_GENERAL, "dsaparam", dsaparam_main },
        !           193:        { FUNC_TYPE_GENERAL, "gendsa", gendsa_main },
        !           194: #endif
        !           195: #ifndef OPENSSL_NO_EC
        !           196:        { FUNC_TYPE_GENERAL, "ec", ec_main },
        !           197:        { FUNC_TYPE_GENERAL, "ecparam", ecparam_main },
        !           198: #endif
        !           199: #ifndef OPENSSL_NO_RSA
        !           200:        { FUNC_TYPE_GENERAL, "genrsa", genrsa_main },
        !           201:        { FUNC_TYPE_GENERAL, "rsa", rsa_main },
        !           202:        { FUNC_TYPE_GENERAL, "rsautl", rsautl_main },
        !           203: #endif
        !           204:
        !           205:        /* Message Digests. */
        !           206: #ifndef OPENSSL_NO_GOST
        !           207:        { FUNC_TYPE_MD, "gost-mac", dgst_main },
        !           208:        { FUNC_TYPE_MD, "md_gost94", dgst_main },
        !           209:        { FUNC_TYPE_MD, "streebog256", dgst_main },
        !           210:        { FUNC_TYPE_MD, "streebog512", dgst_main },
        !           211: #endif
        !           212: #ifndef OPENSSL_NO_MD4
        !           213:        { FUNC_TYPE_MD, "md4", dgst_main },
        !           214: #endif
        !           215: #ifndef OPENSSL_NO_MD5
        !           216:        { FUNC_TYPE_MD, "md5", dgst_main },
        !           217: #endif
        !           218: #ifndef OPENSSL_NO_RIPEMD160
        !           219:        { FUNC_TYPE_MD, "ripemd160", dgst_main },
        !           220: #endif
        !           221: #ifndef OPENSSL_NO_SHA
        !           222:        { FUNC_TYPE_MD, "sha", dgst_main },
        !           223: #endif
        !           224: #ifndef OPENSSL_NO_SHA1
        !           225:        { FUNC_TYPE_MD, "sha1", dgst_main },
        !           226: #endif
        !           227: #ifndef OPENSSL_NO_SHA224
        !           228:        { FUNC_TYPE_MD, "sha224", dgst_main },
        !           229: #endif
        !           230: #ifndef OPENSSL_NO_SHA256
        !           231:        { FUNC_TYPE_MD, "sha256", dgst_main },
        !           232: #endif
        !           233: #ifndef OPENSSL_NO_SHA384
        !           234:        { FUNC_TYPE_MD, "sha384", dgst_main },
        !           235: #endif
        !           236: #ifndef OPENSSL_NO_SHA512
        !           237:        { FUNC_TYPE_MD, "sha512", dgst_main },
        !           238: #endif
        !           239: #ifndef OPENSSL_NO_WHIRLPOOL
        !           240:        { FUNC_TYPE_MD, "whirlpool", dgst_main },
        !           241: #endif
        !           242:
        !           243:        /* Ciphers. */
        !           244:        { FUNC_TYPE_CIPHER, "base64", enc_main },
        !           245: #ifndef OPENSSL_NO_AES
        !           246:        { FUNC_TYPE_CIPHER, "aes-128-cbc", enc_main },
        !           247:        { FUNC_TYPE_CIPHER, "aes-128-ecb", enc_main },
        !           248:        { FUNC_TYPE_CIPHER, "aes-192-cbc", enc_main },
        !           249:        { FUNC_TYPE_CIPHER, "aes-192-ecb", enc_main },
        !           250:        { FUNC_TYPE_CIPHER, "aes-256-cbc", enc_main },
        !           251:        { FUNC_TYPE_CIPHER, "aes-256-ecb", enc_main },
        !           252: #endif
        !           253: #ifndef OPENSSL_NO_BF
        !           254:        { FUNC_TYPE_CIPHER, "bf", enc_main },
        !           255:        { FUNC_TYPE_CIPHER, "bf-cbc", enc_main },
        !           256:        { FUNC_TYPE_CIPHER, "bf-ecb", enc_main },
        !           257:        { FUNC_TYPE_CIPHER, "bf-cfb", enc_main },
        !           258:        { FUNC_TYPE_CIPHER, "bf-ofb", enc_main },
        !           259: #endif
        !           260: #ifndef OPENSSL_NO_CAMELLIA
        !           261:        { FUNC_TYPE_CIPHER, "camellia-128-cbc", enc_main },
        !           262:        { FUNC_TYPE_CIPHER, "camellia-128-ecb", enc_main },
        !           263:        { FUNC_TYPE_CIPHER, "camellia-192-cbc", enc_main },
        !           264:        { FUNC_TYPE_CIPHER, "camellia-192-ecb", enc_main },
        !           265:        { FUNC_TYPE_CIPHER, "camellia-256-cbc", enc_main },
        !           266:        { FUNC_TYPE_CIPHER, "camellia-256-ecb", enc_main },
        !           267: #endif
        !           268: #ifndef OPENSSL_NO_CAST
        !           269:        { FUNC_TYPE_CIPHER, "cast", enc_main },
        !           270:        { FUNC_TYPE_CIPHER, "cast5-cbc", enc_main },
        !           271:        { FUNC_TYPE_CIPHER, "cast5-ecb", enc_main },
        !           272:        { FUNC_TYPE_CIPHER, "cast5-cfb", enc_main },
        !           273:        { FUNC_TYPE_CIPHER, "cast5-ofb", enc_main },
        !           274:        { FUNC_TYPE_CIPHER, "cast-cbc", enc_main },
        !           275: #endif
        !           276: #ifndef OPENSSL_NO_CHACHA
        !           277:        { FUNC_TYPE_CIPHER, "chacha", enc_main },
        !           278: #endif
        !           279: #ifndef OPENSSL_NO_DES
        !           280:        { FUNC_TYPE_CIPHER, "des", enc_main },
        !           281:        { FUNC_TYPE_CIPHER, "des3", enc_main },
        !           282:        { FUNC_TYPE_CIPHER, "desx", enc_main },
        !           283:        { FUNC_TYPE_CIPHER, "des-ecb", enc_main },
        !           284:        { FUNC_TYPE_CIPHER, "des-ede", enc_main },
        !           285:        { FUNC_TYPE_CIPHER, "des-ede3", enc_main },
        !           286:        { FUNC_TYPE_CIPHER, "des-cbc", enc_main },
        !           287:        { FUNC_TYPE_CIPHER, "des-ede-cbc", enc_main },
        !           288:        { FUNC_TYPE_CIPHER, "des-ede3-cbc", enc_main },
        !           289:        { FUNC_TYPE_CIPHER, "des-cfb", enc_main },
        !           290:        { FUNC_TYPE_CIPHER, "des-ede-cfb", enc_main },
        !           291:        { FUNC_TYPE_CIPHER, "des-ede3-cfb", enc_main },
        !           292:        { FUNC_TYPE_CIPHER, "des-ofb", enc_main },
        !           293:        { FUNC_TYPE_CIPHER, "des-ede-ofb", enc_main },
        !           294:        { FUNC_TYPE_CIPHER, "des-ede3-ofb", enc_main },
        !           295: #endif
        !           296: #ifndef OPENSSL_NO_IDEA
        !           297:        { FUNC_TYPE_CIPHER, "idea", enc_main },
        !           298:        { FUNC_TYPE_CIPHER, "idea-cbc", enc_main },
        !           299:        { FUNC_TYPE_CIPHER, "idea-ecb", enc_main },
        !           300:        { FUNC_TYPE_CIPHER, "idea-cfb", enc_main },
        !           301:        { FUNC_TYPE_CIPHER, "idea-ofb", enc_main },
        !           302: #endif
        !           303: #ifndef OPENSSL_NO_RC2
        !           304:        { FUNC_TYPE_CIPHER, "rc2", enc_main },
        !           305:        { FUNC_TYPE_CIPHER, "rc2-cbc", enc_main },
        !           306:        { FUNC_TYPE_CIPHER, "rc2-ecb", enc_main },
        !           307:        { FUNC_TYPE_CIPHER, "rc2-cfb", enc_main },
        !           308:        { FUNC_TYPE_CIPHER, "rc2-ofb", enc_main },
        !           309:        { FUNC_TYPE_CIPHER, "rc2-64-cbc", enc_main },
        !           310:        { FUNC_TYPE_CIPHER, "rc2-40-cbc", enc_main },
        !           311: #endif
        !           312: #ifndef OPENSSL_NO_RC4
        !           313:        { FUNC_TYPE_CIPHER, "rc4", enc_main },
        !           314:        { FUNC_TYPE_CIPHER, "rc4-40", enc_main },
        !           315: #endif
        !           316: #ifndef OPENSSL_NO_RC5
        !           317:        { FUNC_TYPE_CIPHER, "rc5", enc_main },
        !           318:        { FUNC_TYPE_CIPHER, "rc5-cbc", enc_main },
        !           319:        { FUNC_TYPE_CIPHER, "rc5-ecb", enc_main },
        !           320:        { FUNC_TYPE_CIPHER, "rc5-cfb", enc_main },
        !           321:        { FUNC_TYPE_CIPHER, "rc5-ofb", enc_main },
        !           322: #endif
        !           323: #ifdef ZLIB
        !           324:        { FUNC_TYPE_CIPHER, "zlib", enc_main },
        !           325: #endif
        !           326:
        !           327:        { 0, NULL, NULL }
        !           328: };
1.1       jsing     329:
                    330: static void openssl_startup(void);
                    331: static void openssl_shutdown(void);
                    332:
                    333: /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
                    334:  * base prototypes (we cast each variable inside the function to the required
                    335:  * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
                    336:  * functions. */
                    337:
                    338: static LHASH_OF(FUNCTION) *prog_init(void);
                    339: static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
                    340: static void list_pkey(BIO * out);
                    341: static void list_cipher(BIO * out);
                    342: static void list_md(BIO * out);
                    343: char *default_config_file = NULL;
                    344:
                    345: CONF *config = NULL;
                    346: BIO *bio_err = NULL;
                    347:
                    348: static void
                    349: lock_dbg_cb(int mode, int type, const char *file, int line)
                    350: {
                    351:        static int modes[CRYPTO_NUM_LOCKS];     /* = {0, 0, ... } */
                    352:        const char *errstr = NULL;
                    353:        int rw;
                    354:
                    355:        rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
                    356:        if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE))) {
                    357:                errstr = "invalid mode";
                    358:                goto err;
                    359:        }
                    360:        if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
                    361:                errstr = "type out of bounds";
                    362:                goto err;
                    363:        }
                    364:        if (mode & CRYPTO_LOCK) {
                    365:                if (modes[type]) {
                    366:                        errstr = "already locked";
                    367:                        /*
                    368:                         * must not happen in a single-threaded program
                    369:                         * (would deadlock)
                    370:                         */
                    371:                        goto err;
                    372:                }
                    373:                modes[type] = rw;
                    374:        } else if (mode & CRYPTO_UNLOCK) {
                    375:                if (!modes[type]) {
                    376:                        errstr = "not locked";
                    377:                        goto err;
                    378:                }
                    379:                if (modes[type] != rw) {
                    380:                        errstr = (rw == CRYPTO_READ) ?
                    381:                            "CRYPTO_r_unlock on write lock" :
                    382:                            "CRYPTO_w_unlock on read lock";
                    383:                }
                    384:                modes[type] = 0;
                    385:        } else {
                    386:                errstr = "invalid mode";
                    387:                goto err;
                    388:        }
                    389:
                    390: err:
                    391:        if (errstr) {
                    392:                /* we cannot use bio_err here */
                    393:                fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
                    394:                    errstr, mode, type, file, line);
                    395:        }
                    396: }
                    397:
                    398: static void
                    399: openssl_startup(void)
                    400: {
                    401:        signal(SIGPIPE, SIG_IGN);
                    402:
                    403:        CRYPTO_malloc_init();
                    404:        OpenSSL_add_all_algorithms();
                    405:        SSL_library_init();
                    406:        SSL_load_error_strings();
                    407:
                    408: #ifndef OPENSSL_NO_ENGINE
                    409:        ENGINE_load_builtin_engines();
                    410: #endif
                    411:
                    412:        setup_ui_method();
                    413: }
                    414:
                    415: static void
                    416: openssl_shutdown(void)
                    417: {
                    418:        CONF_modules_unload(1);
                    419:        destroy_ui_method();
                    420:        OBJ_cleanup();
                    421:        EVP_cleanup();
                    422:
                    423: #ifndef OPENSSL_NO_ENGINE
                    424:        ENGINE_cleanup();
                    425: #endif
                    426:
                    427:        CRYPTO_cleanup_all_ex_data();
                    428:        ERR_remove_thread_state(NULL);
                    429:        ERR_free_strings();
                    430: }
                    431:
                    432: int
                    433: main(int argc, char **argv)
                    434: {
                    435:        ARGS arg;
                    436: #define PROG_NAME_SIZE 39
                    437:        char pname[PROG_NAME_SIZE + 1];
                    438:        FUNCTION f, *fp;
                    439:        const char *prompt;
                    440:        char buf[1024];
                    441:        char *to_free = NULL;
                    442:        int n, i, ret = 0;
                    443:        char *p;
                    444:        LHASH_OF(FUNCTION) * prog = NULL;
                    445:        long errline;
                    446:
                    447:        arg.data = NULL;
                    448:        arg.count = 0;
                    449:
                    450:        bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
                    451:        if (bio_err == NULL) {
                    452:                fprintf(stderr, "openssl: failed to initialise bio_err\n");
1.3       bcook     453:                exit(1);
                    454:        }
                    455:
                    456:        if (BIO_sock_init() != 1) {
                    457:                BIO_printf(bio_err, "BIO_sock_init failed\n");
1.1       jsing     458:                exit(1);
                    459:        }
                    460:
                    461:        CRYPTO_set_locking_callback(lock_dbg_cb);
                    462:
                    463:        openssl_startup();
                    464:
                    465:        /* Lets load up our environment a little */
                    466:        p = getenv("OPENSSL_CONF");
                    467:        if (p == NULL)
                    468:                p = getenv("SSLEAY_CONF");
                    469:        if (p == NULL) {
                    470:                p = to_free = make_config_name();
                    471:                if (p == NULL) {
                    472:                        BIO_printf(bio_err, "error making config file name\n");
                    473:                        goto end;
                    474:                }
                    475:        }
                    476:
                    477:        default_config_file = p;
                    478:
                    479:        config = NCONF_new(NULL);
                    480:        i = NCONF_load(config, p, &errline);
                    481:        if (i == 0) {
                    482:                if (ERR_GET_REASON(ERR_peek_last_error()) ==
                    483:                    CONF_R_NO_SUCH_FILE) {
                    484:                        BIO_printf(bio_err,
                    485:                            "WARNING: can't open config file: %s\n", p);
                    486:                        ERR_clear_error();
                    487:                        NCONF_free(config);
                    488:                        config = NULL;
                    489:                } else {
                    490:                        ERR_print_errors(bio_err);
                    491:                        NCONF_free(config);
                    492:                        exit(1);
                    493:                }
                    494:        }
                    495:
                    496:        if (!load_config(bio_err, NULL)) {
                    497:                BIO_printf(bio_err, "failed to load configuration\n");
                    498:                goto end;
                    499:        }
                    500:
                    501:        prog = prog_init();
                    502:
                    503:        /* first check the program name */
                    504:        program_name(argv[0], pname, sizeof pname);
                    505:
                    506:        f.name = pname;
                    507:        fp = lh_FUNCTION_retrieve(prog, &f);
                    508:        if (fp != NULL) {
                    509:                argv[0] = pname;
                    510:                ret = fp->func(argc, argv);
                    511:                goto end;
                    512:        }
                    513:        /*
                    514:         * ok, now check that there are not arguments, if there are, run with
                    515:         * them, shifting the ssleay off the front
                    516:         */
                    517:        if (argc != 1) {
                    518:                argc--;
                    519:                argv++;
                    520:                ret = do_cmd(prog, argc, argv);
                    521:                if (ret < 0)
                    522:                        ret = 0;
                    523:                goto end;
                    524:        }
                    525:        /* ok, lets enter the old 'OpenSSL>' mode */
                    526:
                    527:        for (;;) {
                    528:                ret = 0;
                    529:                p = buf;
                    530:                n = sizeof buf;
                    531:                i = 0;
                    532:                for (;;) {
                    533:                        p[0] = '\0';
                    534:                        if (i++)
                    535:                                prompt = ">";
                    536:                        else
                    537:                                prompt = "OpenSSL> ";
                    538:                        fputs(prompt, stdout);
                    539:                        fflush(stdout);
                    540:                        if (!fgets(p, n, stdin))
                    541:                                goto end;
                    542:                        if (p[0] == '\0')
                    543:                                goto end;
                    544:                        i = strlen(p);
                    545:                        if (i <= 1)
                    546:                                break;
                    547:                        if (p[i - 2] != '\\')
                    548:                                break;
                    549:                        i -= 2;
                    550:                        p += i;
                    551:                        n -= i;
                    552:                }
                    553:                if (!chopup_args(&arg, buf, &argc, &argv))
                    554:                        break;
                    555:
                    556:                ret = do_cmd(prog, argc, argv);
                    557:                if (ret < 0) {
                    558:                        ret = 0;
                    559:                        goto end;
                    560:                }
                    561:                if (ret != 0)
                    562:                        BIO_printf(bio_err, "error in %s\n", argv[0]);
                    563:                (void) BIO_flush(bio_err);
                    564:        }
                    565:        BIO_printf(bio_err, "bad exit\n");
                    566:        ret = 1;
                    567:
                    568: end:
                    569:        free(to_free);
                    570:
                    571:        if (config != NULL) {
                    572:                NCONF_free(config);
                    573:                config = NULL;
                    574:        }
                    575:        if (prog != NULL)
                    576:                lh_FUNCTION_free(prog);
                    577:        free(arg.data);
                    578:
                    579:        openssl_shutdown();
                    580:
                    581:        if (bio_err != NULL) {
                    582:                BIO_free(bio_err);
                    583:                bio_err = NULL;
                    584:        }
                    585:        return (ret);
                    586: }
                    587:
                    588: #define LIST_STANDARD_COMMANDS "list-standard-commands"
                    589: #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
                    590: #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms"
                    591: #define LIST_CIPHER_COMMANDS "list-cipher-commands"
                    592: #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms"
                    593: #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms"
                    594:
                    595:
                    596: static int
                    597: do_cmd(LHASH_OF(FUNCTION) * prog, int argc, char *argv[])
                    598: {
                    599:        FUNCTION f, *fp;
                    600:        int i, ret = 1, tp, nl;
                    601:
                    602:        if ((argc <= 0) || (argv[0] == NULL)) {
                    603:                ret = 0;
                    604:                goto end;
                    605:        }
                    606:        f.name = argv[0];
                    607:        fp = lh_FUNCTION_retrieve(prog, &f);
                    608:        if (fp == NULL) {
                    609:                if (EVP_get_digestbyname(argv[0])) {
                    610:                        f.type = FUNC_TYPE_MD;
                    611:                        f.func = dgst_main;
                    612:                        fp = &f;
                    613:                } else if (EVP_get_cipherbyname(argv[0])) {
                    614:                        f.type = FUNC_TYPE_CIPHER;
                    615:                        f.func = enc_main;
                    616:                        fp = &f;
                    617:                }
                    618:        }
                    619:        if (fp != NULL) {
                    620:                ret = fp->func(argc, argv);
                    621:        } else if ((strncmp(argv[0], "no-", 3)) == 0) {
                    622:                BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
                    623:                f.name = argv[0] + 3;
                    624:                ret = (lh_FUNCTION_retrieve(prog, &f) != NULL);
                    625:                if (!ret)
                    626:                        BIO_printf(bio_stdout, "%s\n", argv[0]);
                    627:                else
                    628:                        BIO_printf(bio_stdout, "%s\n", argv[0] + 3);
                    629:                BIO_free_all(bio_stdout);
                    630:                goto end;
                    631:        } else if ((strcmp(argv[0], "quit") == 0) ||
                    632:            (strcmp(argv[0], "q") == 0) ||
                    633:            (strcmp(argv[0], "exit") == 0) ||
                    634:            (strcmp(argv[0], "bye") == 0)) {
                    635:                ret = -1;
                    636:                goto end;
                    637:        } else if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) ||
                    638:            (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
                    639:            (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) ||
                    640:            (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) ||
                    641:            (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) ||
                    642:            (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) {
                    643:                int list_type;
                    644:                BIO *bio_stdout;
                    645:
                    646:                if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0)
                    647:                        list_type = FUNC_TYPE_GENERAL;
                    648:                else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0)
                    649:                        list_type = FUNC_TYPE_MD;
                    650:                else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0)
                    651:                        list_type = FUNC_TYPE_MD_ALG;
                    652:                else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)
                    653:                        list_type = FUNC_TYPE_PKEY;
                    654:                else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0)
                    655:                        list_type = FUNC_TYPE_CIPHER_ALG;
                    656:                else            /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
                    657:                        list_type = FUNC_TYPE_CIPHER;
                    658:                bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE);
                    659:
                    660:                if (list_type == FUNC_TYPE_PKEY)
                    661:                        list_pkey(bio_stdout);
                    662:                if (list_type == FUNC_TYPE_MD_ALG)
                    663:                        list_md(bio_stdout);
                    664:                if (list_type == FUNC_TYPE_CIPHER_ALG)
                    665:                        list_cipher(bio_stdout);
                    666:                else {
                    667:                        for (fp = functions; fp->name != NULL; fp++)
                    668:                                if (fp->type == list_type)
                    669:                                        BIO_printf(bio_stdout, "%s\n",
                    670:                                            fp->name);
                    671:                }
                    672:                BIO_free_all(bio_stdout);
                    673:                ret = 0;
                    674:                goto end;
                    675:        } else {
                    676:                BIO_printf(bio_err,
                    677:                    "openssl:Error: '%s' is an invalid command.\n",
                    678:                    argv[0]);
                    679:                BIO_printf(bio_err, "\nStandard commands");
                    680:                i = 0;
                    681:                tp = 0;
                    682:                for (fp = functions; fp->name != NULL; fp++) {
                    683:                        nl = 0;
                    684: #ifdef OPENSSL_NO_CAMELLIA
                    685:                        if (((i++) % 5) == 0)
                    686: #else
                    687:                        if (((i++) % 4) == 0)
                    688: #endif
                    689:                        {
                    690:                                BIO_printf(bio_err, "\n");
                    691:                                nl = 1;
                    692:                        }
                    693:                        if (fp->type != tp) {
                    694:                                tp = fp->type;
                    695:                                if (!nl)
                    696:                                        BIO_printf(bio_err, "\n");
                    697:                                if (tp == FUNC_TYPE_MD) {
                    698:                                        i = 1;
                    699:                                        BIO_printf(bio_err,
                    700:                                            "\nMessage Digest commands (see the `dgst' command for more details)\n");
                    701:                                } else if (tp == FUNC_TYPE_CIPHER) {
                    702:                                        i = 1;
                    703:                                        BIO_printf(bio_err, "\nCipher commands (see the `enc' command for more details)\n");
                    704:                                }
                    705:                        }
                    706: #ifdef OPENSSL_NO_CAMELLIA
                    707:                        BIO_printf(bio_err, "%-15s", fp->name);
                    708: #else
                    709:                        BIO_printf(bio_err, "%-18s", fp->name);
                    710: #endif
                    711:                }
                    712:                BIO_printf(bio_err, "\n\n");
                    713:                ret = 0;
                    714:        }
                    715: end:
                    716:        return (ret);
                    717: }
                    718:
                    719: static int
                    720: SortFnByName(const void *_f1, const void *_f2)
                    721: {
                    722:        const FUNCTION *f1 = _f1;
                    723:        const FUNCTION *f2 = _f2;
                    724:
                    725:        if (f1->type != f2->type)
                    726:                return f1->type - f2->type;
                    727:        return strcmp(f1->name, f2->name);
                    728: }
                    729:
                    730: static void
                    731: list_pkey(BIO * out)
                    732: {
                    733:        int i;
                    734:
                    735:        for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
                    736:                const EVP_PKEY_ASN1_METHOD *ameth;
                    737:                int pkey_id, pkey_base_id, pkey_flags;
                    738:                const char *pinfo, *pem_str;
                    739:                ameth = EVP_PKEY_asn1_get0(i);
                    740:                EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags,
                    741:                    &pinfo, &pem_str, ameth);
                    742:                if (pkey_flags & ASN1_PKEY_ALIAS) {
                    743:                        BIO_printf(out, "Name: %s\n",
                    744:                            OBJ_nid2ln(pkey_id));
                    745:                        BIO_printf(out, "\tType: Alias to %s\n",
                    746:                            OBJ_nid2ln(pkey_base_id));
                    747:                } else {
                    748:                        BIO_printf(out, "Name: %s\n", pinfo);
                    749:                        BIO_printf(out, "\tType: %s Algorithm\n",
                    750:                            pkey_flags & ASN1_PKEY_DYNAMIC ?
                    751:                            "External" : "Builtin");
                    752:                        BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id));
                    753:                        if (pem_str == NULL)
                    754:                                pem_str = "(none)";
                    755:                        BIO_printf(out, "\tPEM string: %s\n", pem_str);
                    756:                }
                    757:
                    758:        }
                    759: }
                    760:
                    761: static void
                    762: list_cipher_fn(const EVP_CIPHER * c, const char *from, const char *to,
                    763:     void *arg)
                    764: {
                    765:        if (c)
                    766:                BIO_printf(arg, "%s\n", EVP_CIPHER_name(c));
                    767:        else {
                    768:                if (!from)
                    769:                        from = "<undefined>";
                    770:                if (!to)
                    771:                        to = "<undefined>";
                    772:                BIO_printf(arg, "%s => %s\n", from, to);
                    773:        }
                    774: }
                    775:
                    776: static void
                    777: list_cipher(BIO * out)
                    778: {
                    779:        EVP_CIPHER_do_all_sorted(list_cipher_fn, out);
                    780: }
                    781:
                    782: static void
                    783: list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg)
                    784: {
                    785:        if (m)
                    786:                BIO_printf(arg, "%s\n", EVP_MD_name(m));
                    787:        else {
                    788:                if (!from)
                    789:                        from = "<undefined>";
                    790:                if (!to)
                    791:                        to = "<undefined>";
                    792:                BIO_printf(arg, "%s => %s\n", from, to);
                    793:        }
                    794: }
                    795:
                    796: static void
                    797: list_md(BIO * out)
                    798: {
                    799:        EVP_MD_do_all_sorted(list_md_fn, out);
                    800: }
                    801:
                    802: static int
                    803: function_cmp(const FUNCTION * a, const FUNCTION * b)
                    804: {
                    805:        return strncmp(a->name, b->name, 8);
                    806: }
                    807:
                    808: static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION)
                    809:
                    810: static unsigned long
                    811: function_hash(const FUNCTION * a)
                    812: {
                    813:        return lh_strhash(a->name);
                    814: }
                    815:
                    816: static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION)
                    817:
                    818: static LHASH_OF(FUNCTION) *
                    819: prog_init(void)
                    820: {
                    821:        LHASH_OF(FUNCTION) * ret;
                    822:        FUNCTION *f;
                    823:        size_t i;
                    824:
                    825:        /* Purely so it looks nice when the user hits ? */
                    826:        for (i = 0, f = functions; f->name != NULL; ++f, ++i)
                    827:                ;
                    828:        qsort(functions, i, sizeof *functions, SortFnByName);
                    829:
                    830:        if ((ret = lh_FUNCTION_new()) == NULL)
                    831:                return (NULL);
                    832:
                    833:        for (f = functions; f->name != NULL; f++)
                    834:                (void) lh_FUNCTION_insert(ret, f);
                    835:        return (ret);
                    836: }