1*23cc9d40Stb /* $OpenBSD: openssl.c,v 1.39 2025/01/02 13:10:03 tb Exp $ */ 2dab3f910Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3dab3f910Sjsing * All rights reserved. 4dab3f910Sjsing * 5dab3f910Sjsing * This package is an SSL implementation written 6dab3f910Sjsing * by Eric Young (eay@cryptsoft.com). 7dab3f910Sjsing * The implementation was written so as to conform with Netscapes SSL. 8dab3f910Sjsing * 9dab3f910Sjsing * This library is free for commercial and non-commercial use as long as 10dab3f910Sjsing * the following conditions are aheared to. The following conditions 11dab3f910Sjsing * apply to all code found in this distribution, be it the RC4, RSA, 12dab3f910Sjsing * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13dab3f910Sjsing * included with this distribution is covered by the same copyright terms 14dab3f910Sjsing * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15dab3f910Sjsing * 16dab3f910Sjsing * Copyright remains Eric Young's, and as such any Copyright notices in 17dab3f910Sjsing * the code are not to be removed. 18dab3f910Sjsing * If this package is used in a product, Eric Young should be given attribution 19dab3f910Sjsing * as the author of the parts of the library used. 20dab3f910Sjsing * This can be in the form of a textual message at program startup or 21dab3f910Sjsing * in documentation (online or textual) provided with the package. 22dab3f910Sjsing * 23dab3f910Sjsing * Redistribution and use in source and binary forms, with or without 24dab3f910Sjsing * modification, are permitted provided that the following conditions 25dab3f910Sjsing * are met: 26dab3f910Sjsing * 1. Redistributions of source code must retain the copyright 27dab3f910Sjsing * notice, this list of conditions and the following disclaimer. 28dab3f910Sjsing * 2. Redistributions in binary form must reproduce the above copyright 29dab3f910Sjsing * notice, this list of conditions and the following disclaimer in the 30dab3f910Sjsing * documentation and/or other materials provided with the distribution. 31dab3f910Sjsing * 3. All advertising materials mentioning features or use of this software 32dab3f910Sjsing * must display the following acknowledgement: 33dab3f910Sjsing * "This product includes cryptographic software written by 34dab3f910Sjsing * Eric Young (eay@cryptsoft.com)" 35dab3f910Sjsing * The word 'cryptographic' can be left out if the rouines from the library 36dab3f910Sjsing * being used are not cryptographic related :-). 37dab3f910Sjsing * 4. If you include any Windows specific code (or a derivative thereof) from 38dab3f910Sjsing * the apps directory (application code) you must include an acknowledgement: 39dab3f910Sjsing * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40dab3f910Sjsing * 41dab3f910Sjsing * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42dab3f910Sjsing * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43dab3f910Sjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44dab3f910Sjsing * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45dab3f910Sjsing * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46dab3f910Sjsing * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47dab3f910Sjsing * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48dab3f910Sjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49dab3f910Sjsing * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50dab3f910Sjsing * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51dab3f910Sjsing * SUCH DAMAGE. 52dab3f910Sjsing * 53dab3f910Sjsing * The licence and distribution terms for any publically available version or 54dab3f910Sjsing * derivative of this code cannot be changed. i.e. this code cannot simply be 55dab3f910Sjsing * copied and put under another distribution licence 56dab3f910Sjsing * [including the GNU Public Licence.] 57dab3f910Sjsing */ 58dab3f910Sjsing /* ==================================================================== 59dab3f910Sjsing * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved. 60dab3f910Sjsing * 61dab3f910Sjsing * Redistribution and use in source and binary forms, with or without 62dab3f910Sjsing * modification, are permitted provided that the following conditions 63dab3f910Sjsing * are met: 64dab3f910Sjsing * 65dab3f910Sjsing * 1. Redistributions of source code must retain the above copyright 66dab3f910Sjsing * notice, this list of conditions and the following disclaimer. 67dab3f910Sjsing * 68dab3f910Sjsing * 2. Redistributions in binary form must reproduce the above copyright 69dab3f910Sjsing * notice, this list of conditions and the following disclaimer in 70dab3f910Sjsing * the documentation and/or other materials provided with the 71dab3f910Sjsing * distribution. 72dab3f910Sjsing * 73dab3f910Sjsing * 3. All advertising materials mentioning features or use of this 74dab3f910Sjsing * software must display the following acknowledgment: 75dab3f910Sjsing * "This product includes software developed by the OpenSSL Project 76dab3f910Sjsing * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77dab3f910Sjsing * 78dab3f910Sjsing * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79dab3f910Sjsing * endorse or promote products derived from this software without 80dab3f910Sjsing * prior written permission. For written permission, please contact 81dab3f910Sjsing * openssl-core@openssl.org. 82dab3f910Sjsing * 83dab3f910Sjsing * 5. Products derived from this software may not be called "OpenSSL" 84dab3f910Sjsing * nor may "OpenSSL" appear in their names without prior written 85dab3f910Sjsing * permission of the OpenSSL Project. 86dab3f910Sjsing * 87dab3f910Sjsing * 6. Redistributions of any form whatsoever must retain the following 88dab3f910Sjsing * acknowledgment: 89dab3f910Sjsing * "This product includes software developed by the OpenSSL Project 90dab3f910Sjsing * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91dab3f910Sjsing * 92dab3f910Sjsing * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93dab3f910Sjsing * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94dab3f910Sjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95dab3f910Sjsing * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96dab3f910Sjsing * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97dab3f910Sjsing * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98dab3f910Sjsing * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99dab3f910Sjsing * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100dab3f910Sjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101dab3f910Sjsing * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102dab3f910Sjsing * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103dab3f910Sjsing * OF THE POSSIBILITY OF SUCH DAMAGE. 104dab3f910Sjsing * ==================================================================== 105dab3f910Sjsing * 106dab3f910Sjsing * This product includes cryptographic software written by Eric Young 107dab3f910Sjsing * (eay@cryptsoft.com). This product includes software written by Tim 108dab3f910Sjsing * Hudson (tjh@cryptsoft.com). 109dab3f910Sjsing * 110dab3f910Sjsing */ 111dab3f910Sjsing 112dab3f910Sjsing #include <err.h> 113dab3f910Sjsing #include <signal.h> 114dab3f910Sjsing #include <stdio.h> 115dab3f910Sjsing #include <string.h> 116dab3f910Sjsing #include <stdlib.h> 1172d32f956Sderaadt #include <unistd.h> 118dab3f910Sjsing 119dab3f910Sjsing #include "apps.h" 120dab3f910Sjsing 121dab3f910Sjsing #include <openssl/bio.h> 122dab3f910Sjsing #include <openssl/conf.h> 123dab3f910Sjsing #include <openssl/crypto.h> 124dab3f910Sjsing #include <openssl/err.h> 125dab3f910Sjsing #include <openssl/lhash.h> 126dab3f910Sjsing #include <openssl/pem.h> 127dab3f910Sjsing #include <openssl/ssl.h> 128dab3f910Sjsing #include <openssl/x509.h> 129dab3f910Sjsing 13020c6dab5Sjsing #define FUNC_TYPE_GENERAL 1 13120c6dab5Sjsing #define FUNC_TYPE_MD 2 13220c6dab5Sjsing #define FUNC_TYPE_CIPHER 3 13320c6dab5Sjsing #define FUNC_TYPE_PKEY 4 13420c6dab5Sjsing #define FUNC_TYPE_MD_ALG 5 13520c6dab5Sjsing #define FUNC_TYPE_CIPHER_ALG 6 13620c6dab5Sjsing 13720c6dab5Sjsing typedef struct { 13820c6dab5Sjsing int type; 13920c6dab5Sjsing const char *name; 14020c6dab5Sjsing int (*func)(int argc, char **argv); 14120c6dab5Sjsing } FUNCTION; 14220c6dab5Sjsing 14320c6dab5Sjsing DECLARE_LHASH_OF(FUNCTION); 14420c6dab5Sjsing 145281bd8e6Sderaadt FUNCTION functions[] = { 146281bd8e6Sderaadt 147281bd8e6Sderaadt /* General functions. */ 148281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "asn1parse", asn1parse_main }, 149281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "ca", ca_main }, 150281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "certhash", certhash_main }, 151281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "ciphers", ciphers_main }, 152a9821b6aSjsing #ifndef OPENSSL_NO_CMS 153a9821b6aSjsing { FUNC_TYPE_GENERAL, "cms", cms_main }, 154a9821b6aSjsing #endif 155281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "crl2pkcs7", crl2pkcs7_main }, 156281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "crl", crl_main }, 157281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "dgst", dgst_main }, 158281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "enc", enc_main }, 159281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "errstr", errstr_main }, 160281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "genpkey", genpkey_main }, 161281bd8e6Sderaadt #ifndef OPENSSL_NO_OCSP 162281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "ocsp", ocsp_main }, 163281bd8e6Sderaadt #endif 164281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "passwd", passwd_main }, 165281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "pkcs7", pkcs7_main }, 166281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "pkcs8", pkcs8_main }, 167281bd8e6Sderaadt #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA1) 168281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "pkcs12", pkcs12_main }, 169281bd8e6Sderaadt #endif 170281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "pkey", pkey_main }, 171281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "pkeyparam", pkeyparam_main }, 172281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "pkeyutl", pkeyutl_main }, 173281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "prime", prime_main }, 174281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "rand", rand_main }, 175281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "req", req_main }, 176281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "s_client", s_client_main }, 177281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "s_server", s_server_main }, 178281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "s_time", s_time_main }, 179281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "sess_id", sess_id_main }, 180281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "smime", smime_main }, 181281bd8e6Sderaadt #ifndef OPENSSL_NO_SPEED 182281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "speed", speed_main }, 183281bd8e6Sderaadt #endif 184281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "ts", ts_main }, 185281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "verify", verify_main }, 186281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "version", version_main }, 187281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "x509", x509_main }, 188281bd8e6Sderaadt 189281bd8e6Sderaadt #ifndef OPENSSL_NO_DH 190281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "dh", dh_main }, 191281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "dhparam", dhparam_main }, 192281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "gendh", gendh_main }, 193281bd8e6Sderaadt #endif 194281bd8e6Sderaadt #ifndef OPENSSL_NO_DSA 195281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "dsa", dsa_main }, 196281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "dsaparam", dsaparam_main }, 197281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "gendsa", gendsa_main }, 198281bd8e6Sderaadt #endif 199281bd8e6Sderaadt #ifndef OPENSSL_NO_EC 200281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "ec", ec_main }, 201281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "ecparam", ecparam_main }, 202281bd8e6Sderaadt #endif 203281bd8e6Sderaadt #ifndef OPENSSL_NO_RSA 204281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "genrsa", genrsa_main }, 205281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "rsa", rsa_main }, 206281bd8e6Sderaadt { FUNC_TYPE_GENERAL, "rsautl", rsautl_main }, 207281bd8e6Sderaadt #endif 208281bd8e6Sderaadt 209281bd8e6Sderaadt /* Message Digests. */ 21000b7ba7eSdoug #ifndef OPENSSL_NO_MD4 21100b7ba7eSdoug { FUNC_TYPE_MD, "md4", dgst_main }, 21200b7ba7eSdoug #endif 213281bd8e6Sderaadt #ifndef OPENSSL_NO_MD5 214281bd8e6Sderaadt { FUNC_TYPE_MD, "md5", dgst_main }, 215281bd8e6Sderaadt #endif 216281bd8e6Sderaadt #ifndef OPENSSL_NO_RIPEMD160 217281bd8e6Sderaadt { FUNC_TYPE_MD, "ripemd160", dgst_main }, 218281bd8e6Sderaadt #endif 219281bd8e6Sderaadt #ifndef OPENSSL_NO_SHA1 220281bd8e6Sderaadt { FUNC_TYPE_MD, "sha1", dgst_main }, 221281bd8e6Sderaadt #endif 222281bd8e6Sderaadt #ifndef OPENSSL_NO_SHA224 223281bd8e6Sderaadt { FUNC_TYPE_MD, "sha224", dgst_main }, 224281bd8e6Sderaadt #endif 225281bd8e6Sderaadt #ifndef OPENSSL_NO_SHA256 226281bd8e6Sderaadt { FUNC_TYPE_MD, "sha256", dgst_main }, 227281bd8e6Sderaadt #endif 228281bd8e6Sderaadt #ifndef OPENSSL_NO_SHA384 229281bd8e6Sderaadt { FUNC_TYPE_MD, "sha384", dgst_main }, 230281bd8e6Sderaadt #endif 231281bd8e6Sderaadt #ifndef OPENSSL_NO_SHA512 232281bd8e6Sderaadt { FUNC_TYPE_MD, "sha512", dgst_main }, 233281bd8e6Sderaadt #endif 234a4b5db7eStb #ifndef OPENSSL_NO_SM3 235a4b5db7eStb { FUNC_TYPE_MD, "sm3", dgst_main }, 236a4b5db7eStb { FUNC_TYPE_MD, "sm3WithRSAEncryption", dgst_main }, 237a4b5db7eStb #endif 238281bd8e6Sderaadt #ifndef OPENSSL_NO_WHIRLPOOL 239281bd8e6Sderaadt { FUNC_TYPE_MD, "whirlpool", dgst_main }, 240281bd8e6Sderaadt #endif 241281bd8e6Sderaadt 242281bd8e6Sderaadt /* Ciphers. */ 243281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "base64", enc_main }, 244281bd8e6Sderaadt #ifndef OPENSSL_NO_AES 245281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "aes-128-cbc", enc_main }, 246281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "aes-128-ecb", enc_main }, 247281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "aes-192-cbc", enc_main }, 248281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "aes-192-ecb", enc_main }, 249281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "aes-256-cbc", enc_main }, 250281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "aes-256-ecb", enc_main }, 251281bd8e6Sderaadt #endif 252281bd8e6Sderaadt #ifndef OPENSSL_NO_BF 253281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "bf", enc_main }, 254281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "bf-cbc", enc_main }, 255281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "bf-ecb", enc_main }, 256281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "bf-cfb", enc_main }, 257281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "bf-ofb", enc_main }, 258281bd8e6Sderaadt #endif 259281bd8e6Sderaadt #ifndef OPENSSL_NO_CAMELLIA 260281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "camellia-128-cbc", enc_main }, 261281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "camellia-128-ecb", enc_main }, 262281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "camellia-192-cbc", enc_main }, 263281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "camellia-192-ecb", enc_main }, 264281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "camellia-256-cbc", enc_main }, 265281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "camellia-256-ecb", enc_main }, 266281bd8e6Sderaadt #endif 267281bd8e6Sderaadt #ifndef OPENSSL_NO_CAST 268281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "cast", enc_main }, 269281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "cast5-cbc", enc_main }, 270281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "cast5-ecb", enc_main }, 271281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "cast5-cfb", enc_main }, 272281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "cast5-ofb", enc_main }, 273281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "cast-cbc", enc_main }, 274281bd8e6Sderaadt #endif 275281bd8e6Sderaadt #ifndef OPENSSL_NO_CHACHA 276281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "chacha", enc_main }, 277281bd8e6Sderaadt #endif 278281bd8e6Sderaadt #ifndef OPENSSL_NO_DES 279281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des", enc_main }, 280281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des3", enc_main }, 281281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "desx", enc_main }, 282281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ecb", enc_main }, 283281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede", enc_main }, 284281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede3", enc_main }, 285281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-cbc", enc_main }, 286281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede-cbc", enc_main }, 287281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede3-cbc", enc_main }, 288281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-cfb", enc_main }, 289281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede-cfb", enc_main }, 290281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede3-cfb", enc_main }, 291281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ofb", enc_main }, 292281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede-ofb", enc_main }, 293281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "des-ede3-ofb", enc_main }, 294281bd8e6Sderaadt #endif 295281bd8e6Sderaadt #ifndef OPENSSL_NO_IDEA 296281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "idea", enc_main }, 297281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "idea-cbc", enc_main }, 298281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "idea-ecb", enc_main }, 299281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "idea-cfb", enc_main }, 300281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "idea-ofb", enc_main }, 301281bd8e6Sderaadt #endif 302281bd8e6Sderaadt #ifndef OPENSSL_NO_RC2 303281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc2", enc_main }, 304281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc2-cbc", enc_main }, 305281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc2-ecb", enc_main }, 306281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc2-cfb", enc_main }, 307281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc2-ofb", enc_main }, 308281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc2-64-cbc", enc_main }, 309281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc2-40-cbc", enc_main }, 310281bd8e6Sderaadt #endif 311281bd8e6Sderaadt #ifndef OPENSSL_NO_RC4 312281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc4", enc_main }, 313281bd8e6Sderaadt { FUNC_TYPE_CIPHER, "rc4-40", enc_main }, 314281bd8e6Sderaadt #endif 315bdd6a469Stb #ifndef OPENSSL_NO_SM4 316bdd6a469Stb { FUNC_TYPE_CIPHER, "sm4", enc_main }, 317bdd6a469Stb { FUNC_TYPE_CIPHER, "sm4-ecb", enc_main }, 318bdd6a469Stb { FUNC_TYPE_CIPHER, "sm4-cbc", enc_main }, 319bdd6a469Stb { FUNC_TYPE_CIPHER, "sm4-ofb", enc_main }, 320bdd6a469Stb { FUNC_TYPE_CIPHER, "sm4-cfb", enc_main }, 321bdd6a469Stb #endif 322281bd8e6Sderaadt { 0, NULL, NULL } 323281bd8e6Sderaadt }; 324281bd8e6Sderaadt 325dab3f910Sjsing static void openssl_startup(void); 326dab3f910Sjsing static void openssl_shutdown(void); 327dab3f910Sjsing 328dab3f910Sjsing /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the 329dab3f910Sjsing * base prototypes (we cast each variable inside the function to the required 330dab3f910Sjsing * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper 331dab3f910Sjsing * functions. */ 332dab3f910Sjsing 333dab3f910Sjsing static LHASH_OF(FUNCTION) *prog_init(void); 334dab3f910Sjsing static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]); 33546f4e7faSjoshua static void print_help(void); 336dab3f910Sjsing static void list_pkey(BIO * out); 337dab3f910Sjsing static void list_cipher(BIO * out); 338dab3f910Sjsing static void list_md(BIO * out); 339dab3f910Sjsing char *default_config_file = NULL; 340dab3f910Sjsing 341dab3f910Sjsing CONF *config = NULL; 342dab3f910Sjsing BIO *bio_err = NULL; 343dab3f910Sjsing 344dab3f910Sjsing static void 345dab3f910Sjsing openssl_startup(void) 346dab3f910Sjsing { 347dab3f910Sjsing signal(SIGPIPE, SIG_IGN); 348dab3f910Sjsing 349dab3f910Sjsing OpenSSL_add_all_algorithms(); 350dab3f910Sjsing SSL_library_init(); 351dab3f910Sjsing SSL_load_error_strings(); 352dab3f910Sjsing 3535827cb97Sbcook setup_ui(); 354dab3f910Sjsing } 355dab3f910Sjsing 356dab3f910Sjsing static void 357dab3f910Sjsing openssl_shutdown(void) 358dab3f910Sjsing { 359dab3f910Sjsing CONF_modules_unload(1); 3605827cb97Sbcook destroy_ui(); 361dab3f910Sjsing OBJ_cleanup(); 362dab3f910Sjsing EVP_cleanup(); 363dab3f910Sjsing CRYPTO_cleanup_all_ex_data(); 364dab3f910Sjsing ERR_remove_thread_state(NULL); 365dab3f910Sjsing ERR_free_strings(); 366dab3f910Sjsing } 367dab3f910Sjsing 368dab3f910Sjsing int 369dab3f910Sjsing main(int argc, char **argv) 370dab3f910Sjsing { 371dab3f910Sjsing char *to_free = NULL; 37246f4e7faSjoshua int i, ret = 0; 373dab3f910Sjsing char *p; 374dab3f910Sjsing LHASH_OF(FUNCTION) * prog = NULL; 375dab3f910Sjsing long errline; 376dab3f910Sjsing 37751811eadSderaadt if (pledge("stdio cpath wpath rpath inet dns proc flock tty", NULL) == -1) { 3780bd1216cSderaadt fprintf(stderr, "openssl: pledge: %s\n", strerror(errno)); 3792d32f956Sderaadt exit(1); 3802d32f956Sderaadt } 3812d32f956Sderaadt 382dab3f910Sjsing bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); 383dab3f910Sjsing if (bio_err == NULL) { 384dab3f910Sjsing fprintf(stderr, "openssl: failed to initialise bio_err\n"); 385dab3f910Sjsing exit(1); 386dab3f910Sjsing } 387dab3f910Sjsing 3885915c0a5Sbcook if (BIO_sock_init() != 1) { 3895915c0a5Sbcook BIO_printf(bio_err, "BIO_sock_init failed\n"); 3905915c0a5Sbcook exit(1); 3915915c0a5Sbcook } 3925915c0a5Sbcook 393dab3f910Sjsing openssl_startup(); 394dab3f910Sjsing 395dab3f910Sjsing /* Lets load up our environment a little */ 396dab3f910Sjsing p = getenv("OPENSSL_CONF"); 397dab3f910Sjsing if (p == NULL) { 398dab3f910Sjsing p = to_free = make_config_name(); 399dab3f910Sjsing if (p == NULL) { 400dab3f910Sjsing BIO_printf(bio_err, "error making config file name\n"); 401dab3f910Sjsing goto end; 402dab3f910Sjsing } 403dab3f910Sjsing } 404dab3f910Sjsing 405dab3f910Sjsing default_config_file = p; 406dab3f910Sjsing 407dab3f910Sjsing config = NCONF_new(NULL); 408dab3f910Sjsing i = NCONF_load(config, p, &errline); 409dab3f910Sjsing if (i == 0) { 410dab3f910Sjsing if (ERR_GET_REASON(ERR_peek_last_error()) == 411dab3f910Sjsing CONF_R_NO_SUCH_FILE) { 412dab3f910Sjsing BIO_printf(bio_err, 413dab3f910Sjsing "WARNING: can't open config file: %s\n", p); 414dab3f910Sjsing ERR_clear_error(); 415dab3f910Sjsing NCONF_free(config); 416dab3f910Sjsing config = NULL; 417dab3f910Sjsing } else { 418dab3f910Sjsing ERR_print_errors(bio_err); 419dab3f910Sjsing NCONF_free(config); 420dab3f910Sjsing exit(1); 421dab3f910Sjsing } 422dab3f910Sjsing } 423dab3f910Sjsing 424dab3f910Sjsing if (!load_config(bio_err, NULL)) { 425dab3f910Sjsing BIO_printf(bio_err, "failed to load configuration\n"); 426dab3f910Sjsing goto end; 427dab3f910Sjsing } 428dab3f910Sjsing 429dab3f910Sjsing prog = prog_init(); 430dab3f910Sjsing 431dab3f910Sjsing /* 432dab3f910Sjsing * ok, now check that there are not arguments, if there are, run with 43346f4e7faSjoshua * them, shifting the executable name off the front 434dab3f910Sjsing */ 435dab3f910Sjsing argc--; 436dab3f910Sjsing argv++; 4379bc487adSdoug 43846f4e7faSjoshua if (argc < 1) { 43946f4e7faSjoshua print_help(); 44046f4e7faSjoshua goto end; 44146f4e7faSjoshua } 44246f4e7faSjoshua 443dab3f910Sjsing ret = do_cmd(prog, argc, argv); 444dab3f910Sjsing if (ret < 0) 445dab3f910Sjsing ret = 0; 446dab3f910Sjsing 447dab3f910Sjsing end: 448dab3f910Sjsing free(to_free); 449dab3f910Sjsing 450dab3f910Sjsing if (config != NULL) { 451dab3f910Sjsing NCONF_free(config); 452dab3f910Sjsing config = NULL; 453dab3f910Sjsing } 454dab3f910Sjsing if (prog != NULL) 455dab3f910Sjsing lh_FUNCTION_free(prog); 456dab3f910Sjsing 457dab3f910Sjsing openssl_shutdown(); 458dab3f910Sjsing 459dab3f910Sjsing if (bio_err != NULL) { 460dab3f910Sjsing BIO_free(bio_err); 461dab3f910Sjsing bio_err = NULL; 462dab3f910Sjsing } 463dab3f910Sjsing return (ret); 464dab3f910Sjsing } 465dab3f910Sjsing 466dab3f910Sjsing #define LIST_STANDARD_COMMANDS "list-standard-commands" 467dab3f910Sjsing #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands" 468dab3f910Sjsing #define LIST_MESSAGE_DIGEST_ALGORITHMS "list-message-digest-algorithms" 469dab3f910Sjsing #define LIST_CIPHER_COMMANDS "list-cipher-commands" 470dab3f910Sjsing #define LIST_CIPHER_ALGORITHMS "list-cipher-algorithms" 471dab3f910Sjsing #define LIST_PUBLIC_KEY_ALGORITHMS "list-public-key-algorithms" 472dab3f910Sjsing 473dab3f910Sjsing 474dab3f910Sjsing static int 475dab3f910Sjsing do_cmd(LHASH_OF(FUNCTION) * prog, int argc, char *argv[]) 476dab3f910Sjsing { 477dab3f910Sjsing FUNCTION f, *fp; 47846f4e7faSjoshua int ret = 1; 479dab3f910Sjsing 480fed78849Sjoshua if (argc <= 0 || argv[0] == NULL) 481fed78849Sjoshua return 0; 482fed78849Sjoshua 483dab3f910Sjsing f.name = argv[0]; 484dab3f910Sjsing fp = lh_FUNCTION_retrieve(prog, &f); 485dab3f910Sjsing if (fp == NULL) { 486dab3f910Sjsing if (EVP_get_digestbyname(argv[0])) { 487dab3f910Sjsing f.type = FUNC_TYPE_MD; 488dab3f910Sjsing f.func = dgst_main; 489dab3f910Sjsing fp = &f; 490dab3f910Sjsing } else if (EVP_get_cipherbyname(argv[0])) { 491dab3f910Sjsing f.type = FUNC_TYPE_CIPHER; 492dab3f910Sjsing f.func = enc_main; 493dab3f910Sjsing fp = &f; 494dab3f910Sjsing } 495dab3f910Sjsing } 496fed78849Sjoshua 497fed78849Sjoshua if (fp != NULL) 498fed78849Sjoshua return fp->func(argc, argv); 499fed78849Sjoshua 500fed78849Sjoshua if (strcmp(argv[0], "help") == 0) { 501fed78849Sjoshua print_help(); 502fed78849Sjoshua return 0; 503fed78849Sjoshua } 504fed78849Sjoshua 505fed78849Sjoshua if ((strncmp(argv[0], "no-", 3)) == 0) { 506dab3f910Sjsing BIO *bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE); 507dab3f910Sjsing f.name = argv[0] + 3; 508dab3f910Sjsing ret = (lh_FUNCTION_retrieve(prog, &f) != NULL); 509dab3f910Sjsing if (!ret) 510dab3f910Sjsing BIO_printf(bio_stdout, "%s\n", argv[0]); 511dab3f910Sjsing else 512dab3f910Sjsing BIO_printf(bio_stdout, "%s\n", argv[0] + 3); 513dab3f910Sjsing BIO_free_all(bio_stdout); 514fed78849Sjoshua return ret; 515fed78849Sjoshua } 516fed78849Sjoshua 517fed78849Sjoshua if ((strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) || 518dab3f910Sjsing (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) || 519dab3f910Sjsing (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) || 520dab3f910Sjsing (strcmp(argv[0], LIST_CIPHER_COMMANDS) == 0) || 521dab3f910Sjsing (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) || 522dab3f910Sjsing (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0)) { 523dab3f910Sjsing int list_type; 524dab3f910Sjsing BIO *bio_stdout; 525dab3f910Sjsing 526dab3f910Sjsing if (strcmp(argv[0], LIST_STANDARD_COMMANDS) == 0) 527dab3f910Sjsing list_type = FUNC_TYPE_GENERAL; 528dab3f910Sjsing else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_COMMANDS) == 0) 529dab3f910Sjsing list_type = FUNC_TYPE_MD; 530dab3f910Sjsing else if (strcmp(argv[0], LIST_MESSAGE_DIGEST_ALGORITHMS) == 0) 531dab3f910Sjsing list_type = FUNC_TYPE_MD_ALG; 532dab3f910Sjsing else if (strcmp(argv[0], LIST_PUBLIC_KEY_ALGORITHMS) == 0) 533dab3f910Sjsing list_type = FUNC_TYPE_PKEY; 534dab3f910Sjsing else if (strcmp(argv[0], LIST_CIPHER_ALGORITHMS) == 0) 535dab3f910Sjsing list_type = FUNC_TYPE_CIPHER_ALG; 536dab3f910Sjsing else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */ 537dab3f910Sjsing list_type = FUNC_TYPE_CIPHER; 538dab3f910Sjsing bio_stdout = BIO_new_fp(stdout, BIO_NOCLOSE); 539dab3f910Sjsing 540dab3f910Sjsing if (list_type == FUNC_TYPE_PKEY) 541dab3f910Sjsing list_pkey(bio_stdout); 542dab3f910Sjsing if (list_type == FUNC_TYPE_MD_ALG) 543dab3f910Sjsing list_md(bio_stdout); 544dab3f910Sjsing if (list_type == FUNC_TYPE_CIPHER_ALG) 545dab3f910Sjsing list_cipher(bio_stdout); 546dab3f910Sjsing else { 547dab3f910Sjsing for (fp = functions; fp->name != NULL; fp++) 548dab3f910Sjsing if (fp->type == list_type) 549dab3f910Sjsing BIO_printf(bio_stdout, "%s\n", 550dab3f910Sjsing fp->name); 551dab3f910Sjsing } 552dab3f910Sjsing BIO_free_all(bio_stdout); 553fed78849Sjoshua return 0; 554fed78849Sjoshua } 555fed78849Sjoshua 556dab3f910Sjsing BIO_printf(bio_err, 557dab3f910Sjsing "openssl:Error: '%s' is an invalid command.\n", 558dab3f910Sjsing argv[0]); 55946f4e7faSjoshua print_help(); 560fed78849Sjoshua 561fed78849Sjoshua return 1; 56246f4e7faSjoshua } 56346f4e7faSjoshua 56446f4e7faSjoshua static void 56546f4e7faSjoshua print_help(void) 56646f4e7faSjoshua { 56746f4e7faSjoshua FUNCTION *fp; 56846f4e7faSjoshua int i = 0; 56946f4e7faSjoshua int tp = 0; 57046f4e7faSjoshua int nl; 57146f4e7faSjoshua 572dab3f910Sjsing BIO_printf(bio_err, "\nStandard commands"); 573dab3f910Sjsing for (fp = functions; fp->name != NULL; fp++) { 574dab3f910Sjsing nl = 0; 575dab3f910Sjsing #ifdef OPENSSL_NO_CAMELLIA 576dab3f910Sjsing if (((i++) % 5) == 0) 577dab3f910Sjsing #else 578dab3f910Sjsing if (((i++) % 4) == 0) 579dab3f910Sjsing #endif 580dab3f910Sjsing { 581dab3f910Sjsing BIO_printf(bio_err, "\n"); 582dab3f910Sjsing nl = 1; 583dab3f910Sjsing } 584dab3f910Sjsing if (fp->type != tp) { 585dab3f910Sjsing tp = fp->type; 586dab3f910Sjsing if (!nl) 587dab3f910Sjsing BIO_printf(bio_err, "\n"); 588dab3f910Sjsing if (tp == FUNC_TYPE_MD) { 589dab3f910Sjsing i = 1; 590dab3f910Sjsing BIO_printf(bio_err, 591dab3f910Sjsing "\nMessage Digest commands (see the `dgst' command for more details)\n"); 592dab3f910Sjsing } else if (tp == FUNC_TYPE_CIPHER) { 593dab3f910Sjsing i = 1; 594dab3f910Sjsing BIO_printf(bio_err, "\nCipher commands (see the `enc' command for more details)\n"); 595dab3f910Sjsing } 596dab3f910Sjsing } 597dab3f910Sjsing #ifdef OPENSSL_NO_CAMELLIA 598dab3f910Sjsing BIO_printf(bio_err, "%-15s", fp->name); 599dab3f910Sjsing #else 600dab3f910Sjsing BIO_printf(bio_err, "%-18s", fp->name); 601dab3f910Sjsing #endif 602dab3f910Sjsing } 60346f4e7faSjoshua 604dab3f910Sjsing BIO_printf(bio_err, "\n\n"); 605dab3f910Sjsing } 606dab3f910Sjsing 607dab3f910Sjsing static int 608dab3f910Sjsing SortFnByName(const void *_f1, const void *_f2) 609dab3f910Sjsing { 610dab3f910Sjsing const FUNCTION *f1 = _f1; 611dab3f910Sjsing const FUNCTION *f2 = _f2; 612dab3f910Sjsing 613dab3f910Sjsing if (f1->type != f2->type) 614dab3f910Sjsing return f1->type - f2->type; 615dab3f910Sjsing return strcmp(f1->name, f2->name); 616dab3f910Sjsing } 617dab3f910Sjsing 618dab3f910Sjsing static void 619dab3f910Sjsing list_pkey(BIO * out) 620dab3f910Sjsing { 621dab3f910Sjsing int i; 622dab3f910Sjsing 623dab3f910Sjsing for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { 624dab3f910Sjsing const EVP_PKEY_ASN1_METHOD *ameth; 625dab3f910Sjsing int pkey_id, pkey_base_id, pkey_flags; 626dab3f910Sjsing const char *pinfo, *pem_str; 627dab3f910Sjsing ameth = EVP_PKEY_asn1_get0(i); 628dab3f910Sjsing EVP_PKEY_asn1_get0_info(&pkey_id, &pkey_base_id, &pkey_flags, 629dab3f910Sjsing &pinfo, &pem_str, ameth); 630dab3f910Sjsing if (pkey_flags & ASN1_PKEY_ALIAS) { 631dab3f910Sjsing BIO_printf(out, "Name: %s\n", 632dab3f910Sjsing OBJ_nid2ln(pkey_id)); 633dab3f910Sjsing BIO_printf(out, "\tType: Alias to %s\n", 634dab3f910Sjsing OBJ_nid2ln(pkey_base_id)); 635dab3f910Sjsing } else { 636dab3f910Sjsing BIO_printf(out, "Name: %s\n", pinfo); 637dab3f910Sjsing BIO_printf(out, "\tType: %s Algorithm\n", 638dab3f910Sjsing pkey_flags & ASN1_PKEY_DYNAMIC ? 639dab3f910Sjsing "External" : "Builtin"); 640dab3f910Sjsing BIO_printf(out, "\tOID: %s\n", OBJ_nid2ln(pkey_id)); 641dab3f910Sjsing if (pem_str == NULL) 642dab3f910Sjsing pem_str = "(none)"; 643dab3f910Sjsing BIO_printf(out, "\tPEM string: %s\n", pem_str); 644dab3f910Sjsing } 645dab3f910Sjsing 646dab3f910Sjsing } 647dab3f910Sjsing } 648dab3f910Sjsing 649dab3f910Sjsing static void 650dab3f910Sjsing list_cipher_fn(const EVP_CIPHER * c, const char *from, const char *to, 651dab3f910Sjsing void *arg) 652dab3f910Sjsing { 653dab3f910Sjsing if (c) 654dab3f910Sjsing BIO_printf(arg, "%s\n", EVP_CIPHER_name(c)); 655dab3f910Sjsing else { 656dab3f910Sjsing if (!from) 657dab3f910Sjsing from = "<undefined>"; 658dab3f910Sjsing if (!to) 659dab3f910Sjsing to = "<undefined>"; 660dab3f910Sjsing BIO_printf(arg, "%s => %s\n", from, to); 661dab3f910Sjsing } 662dab3f910Sjsing } 663dab3f910Sjsing 664dab3f910Sjsing static void 665dab3f910Sjsing list_cipher(BIO * out) 666dab3f910Sjsing { 667dab3f910Sjsing EVP_CIPHER_do_all_sorted(list_cipher_fn, out); 668dab3f910Sjsing } 669dab3f910Sjsing 670dab3f910Sjsing static void 671dab3f910Sjsing list_md_fn(const EVP_MD * m, const char *from, const char *to, void *arg) 672dab3f910Sjsing { 673dab3f910Sjsing if (m) 674dab3f910Sjsing BIO_printf(arg, "%s\n", EVP_MD_name(m)); 675dab3f910Sjsing else { 676dab3f910Sjsing if (!from) 677dab3f910Sjsing from = "<undefined>"; 678dab3f910Sjsing if (!to) 679dab3f910Sjsing to = "<undefined>"; 680dab3f910Sjsing BIO_printf(arg, "%s => %s\n", from, to); 681dab3f910Sjsing } 682dab3f910Sjsing } 683dab3f910Sjsing 684dab3f910Sjsing static void 685dab3f910Sjsing list_md(BIO * out) 686dab3f910Sjsing { 687dab3f910Sjsing EVP_MD_do_all_sorted(list_md_fn, out); 688dab3f910Sjsing } 689dab3f910Sjsing 690dab3f910Sjsing static int 691dab3f910Sjsing function_cmp(const FUNCTION * a, const FUNCTION * b) 692dab3f910Sjsing { 693dab3f910Sjsing return strncmp(a->name, b->name, 8); 694dab3f910Sjsing } 695dab3f910Sjsing 696dab3f910Sjsing static IMPLEMENT_LHASH_COMP_FN(function, FUNCTION) 697dab3f910Sjsing 698dab3f910Sjsing static unsigned long 699dab3f910Sjsing function_hash(const FUNCTION * a) 700dab3f910Sjsing { 701dab3f910Sjsing return lh_strhash(a->name); 702dab3f910Sjsing } 703dab3f910Sjsing 704dab3f910Sjsing static IMPLEMENT_LHASH_HASH_FN(function, FUNCTION) 705dab3f910Sjsing 706dab3f910Sjsing static LHASH_OF(FUNCTION) * 707dab3f910Sjsing prog_init(void) 708dab3f910Sjsing { 709dab3f910Sjsing LHASH_OF(FUNCTION) * ret; 710dab3f910Sjsing FUNCTION *f; 711dab3f910Sjsing size_t i; 712dab3f910Sjsing 713dab3f910Sjsing /* Purely so it looks nice when the user hits ? */ 714dab3f910Sjsing for (i = 0, f = functions; f->name != NULL; ++f, ++i) 715dab3f910Sjsing ; 716dab3f910Sjsing qsort(functions, i, sizeof *functions, SortFnByName); 717dab3f910Sjsing 718dab3f910Sjsing if ((ret = lh_FUNCTION_new()) == NULL) 719dab3f910Sjsing return (NULL); 720dab3f910Sjsing 721dab3f910Sjsing for (f = functions; f->name != NULL; f++) 722dab3f910Sjsing (void) lh_FUNCTION_insert(ret, f); 723dab3f910Sjsing return (ret); 724dab3f910Sjsing } 725