+/* Needed to get the other O_xxx flags. */
+#ifdef OPENSSL_SYS_VMS
+# include <unixio.h>
+#endif
+#define INCLUDE_FUNCTION_TABLE
+#include "apps.h"
+
+
+#ifdef OPENSSL_NO_CAMELLIA
+# define FORMAT "%-15s"
+# define COLUMNS 5
+#else
+# define FORMAT "%-18s"
+# define COLUMNS 4
+#endif
+
+/* Special sentinel to exit the program. */
+#define EXIT_THE_PROGRAM (-1)
+
+/*
+ * The LHASH callbacks ("hash" & "cmp") have been replaced by functions with
+ * the base prototypes (we cast each variable inside the function to the
+ * required type of "FUNCTION*"). This removes the necessity for
+ * macro-generated wrapper functions.
+ */
+DECLARE_LHASH_OF(FUNCTION);
+static LHASH_OF(FUNCTION) *prog_init(void);
+static int do_cmd(LHASH_OF(FUNCTION) *prog, int argc, char *argv[]);
+static void list_pkey(void);
+static void list_type(FUNC_TYPE ft);
+static void list_disabled(void);
+char *default_config_file = NULL;
+
+static CONF *config = NULL;
+BIO *bio_in = NULL;
+BIO *bio_out = NULL;
+BIO *bio_err = NULL;
+
+static void apps_startup()
+{
+#ifdef SIGPIPE
+ signal(SIGPIPE, SIG_IGN);
+#endif
+ CRYPTO_malloc_init();
+ ERR_load_crypto_strings();
+ ERR_load_SSL_strings();
+ OpenSSL_add_all_algorithms();
+ OpenSSL_add_ssl_algorithms();
+ OPENSSL_load_builtin_modules();
+ setup_ui_method();
+ /*SSL_library_init();*/
+#ifndef OPENSSL_NO_ENGINE
+ ENGINE_load_builtin_engines();
+#endif
+}
+
+static void apps_shutdown()
+{
+#ifndef OPENSSL_NO_ENGINE
+ ENGINE_cleanup();
+#endif
+ destroy_ui_method();
+ CONF_modules_unload(1);
+#ifndef OPENSSL_NO_COMP
+ COMP_zlib_cleanup();
+ SSL_COMP_free_compression_methods();
+#endif
+ OBJ_cleanup();
+ EVP_cleanup();
+ CRYPTO_cleanup_all_ex_data();
+ ERR_remove_thread_state(NULL);
+ RAND_cleanup();
+ ERR_free_strings();
+}
+
+static char *make_config_name()
+{
+ const char *t;
+ size_t len;
+ char *p;
+
+ if ((t = getenv("OPENSSL_CONF")) != NULL
+ || (t = getenv("SSLEAY_CONF")) != NULL)
+ return BUF_strdup(t);
+
+ t = X509_get_default_cert_area();
+ len = strlen(t) + 1 + strlen(OPENSSL_CONF) + 1;
+ p = app_malloc(len, "config filename buffer");
+ strcpy(p, t);
+#ifndef OPENSSL_SYS_VMS
+ strcat(p, "/");
+#endif
+ strcat(p, OPENSSL_CONF);
+
+ return p;
+}
+
+static void lock_dbg_cb(int mode, int type, const char *file, int line)
+{
+ static int modes[CRYPTO_NUM_LOCKS];
+ const char *errstr = NULL;
+ int rw = mode & (CRYPTO_READ | CRYPTO_WRITE);
+
+ if (rw != CRYPTO_READ && rw != CRYPTO_WRITE) {
+ errstr = "invalid mode";
+ goto err;
+ }
+
+ if (type < 0 || type >= CRYPTO_NUM_LOCKS) {
+ errstr = "type out of bounds";
+ goto err;
+ }
+
+ if (mode & CRYPTO_LOCK) {
+ if (modes[type]) {
+ errstr = "already locked";
+ /* must not happen in a single-threaded program --> deadlock! */
+ goto err;
+ }
+ modes[type] = rw;
+ } else if (mode & CRYPTO_UNLOCK) {
+ if (!modes[type]) {
+ errstr = "not locked";
+ goto err;
+ }
+
+ if (modes[type] != rw) {
+ errstr = (rw == CRYPTO_READ) ?
+ "CRYPTO_r_unlock on write lock" :
+ "CRYPTO_w_unlock on read lock";
+ }
+
+ modes[type] = 0;
+ } else {
+ errstr = "invalid mode";
+ goto err;
+ }
+
+ err:
+ if (errstr) {
+ BIO_printf(bio_err,
+ "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
+ errstr, mode, type, file, line);
+ }
+}
+
+#if defined( OPENSSL_SYS_VMS)
+extern char **copy_argv(int *argc, char **argv);
+#endif
+
+int main(int argc, char *argv[])
+{
+ FUNCTION f, *fp;
+ LHASH_OF(FUNCTION) *prog = NULL;
+ char **copied_argv = NULL;
+ char *p, *pname;
+ char buf[1024];
+ const char *prompt;
+ ARGS arg;
+ int first, n, i, ret = 0;
+
+ arg.argv = NULL;
+ arg.size = 0;
+
+ /* Set up some of the environment. */
+ default_config_file = make_config_name();
+ bio_in = dup_bio_in(FORMAT_TEXT);
+ bio_out = dup_bio_out(FORMAT_TEXT);
+ bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
+
+#if defined( OPENSSL_SYS_VMS)
+ copied_argv = argv = copy_argv(&argc, argv);
+#endif
+
+ p = getenv("OPENSSL_DEBUG_MEMORY");
+ if (p == NULL)
+ /* if not set, use compiled-in default */
+ ;
+ else if (strcmp(p, "off") != 0) {
+ CRYPTO_malloc_debug_init();
+ CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
+ } else {
+ CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
+ }
+ CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
+ CRYPTO_set_locking_callback(lock_dbg_cb);
+
+ if (getenv("OPENSSL_FIPS")) {
+#ifdef OPENSSL_FIPS
+ if (!FIPS_mode_set(1)) {
+ ERR_load_crypto_strings();
+ ERR_print_errors(bio_err);
+ return 1;
+ }
+#else
+ BIO_printf(bio_err, "FIPS mode not supported.\n");
+ return 1;
+#endif
+ }
+
+ apps_startup();
+
+ /*
+ * If first argument is a colon, skip it. Because in "interactive"
+ * mode our prompt is a colon and we can cut/paste whole lines
+ * by doing this hack.
+ */
+ if (argv[1] && strcmp(argv[1], ":") == 0) {
+ argv[1] = argv[0];
+ argc--;
+ argv++;
+ }
+ prog = prog_init();
+ pname = opt_progname(argv[0]);
+
+ /* first check the program name */
+ f.name = pname;
+ fp = lh_FUNCTION_retrieve(prog, &f);
+ if (fp != NULL) {
+ argv[0] = pname;
+ ret = fp->func(argc, argv);
+ goto end;
+ }
+
+ /* If there is stuff on the command line, run with that. */
+ if (argc != 1) {
+ argc--;
+ argv++;
+ ret = do_cmd(prog, argc, argv);
+ if (ret < 0)
+ ret = 0;
+ goto end;
+ }
+
+ /* ok, lets enter interactive mode */
+ for (;;) {
+ ret = 0;
+ /* Read a line, continue reading if line ends with \ */
+ for (p = buf, n = sizeof buf, i = 0, first = 1; n > 0; first = 0) {
+ prompt = first ? "openssl : " : "> ";
+ p[0] = '\0';
+#ifndef READLINE
+ fputs(prompt, stdout);
+ fflush(stdout);
+ if (!fgets(p, n, stdin))
+ goto end;
+ if (p[0] == '\0')
+ goto end;
+ i = strlen(p);
+ if (i <= 1)
+ break;
+ if (p[i - 2] != '\\')
+ break;
+ i -= 2;
+ p += i;
+ n -= i;
+#else
+ {
+ extern char *readline(const char *);
+ extern void add_history(const char *cp);
+ char *text;
+
+ char *text = readline(prompt);
+ if (text == NULL)
+ goto end;
+ i = strlen(text);
+ if (i == 0 || i > n)
+ break;
+ if (text[i - 1] != '\\') {
+ p += strlen(strcpy(p, text));
+ free(text);
+ add_history(buf);
+ break;
+ }
+
+ text[i - 1] = '\0';
+ p += strlen(strcpy(p, text));
+ free(text);
+ n -= i;
+ }
+#endif
+ }
+
+ if (!chopup_args(&arg, buf)) {
+ BIO_printf(bio_err, "Can't parse (no memory?)\n");
+ break;
+ }
+
+ ret = do_cmd(prog, arg.argc, arg.argv);
+ if (ret == EXIT_THE_PROGRAM) {
+ ret = 0;
+ goto end;
+ }
+ if (ret != 0)
+ BIO_printf(bio_err, "error in %s\n", arg.argv[0]);
+ (void)BIO_flush(bio_out);
+ (void)BIO_flush(bio_err);
+ }
+ ret = 1;
+ end:
+ OPENSSL_free(copied_argv);
+ OPENSSL_free(default_config_file);
+ NCONF_free(config);
+ config = NULL;
+ lh_FUNCTION_free(prog);
+ OPENSSL_free(arg.argv);