sl@0: /* apps/engine.c -*- mode: C; c-file-style: "eay" -*- */ sl@0: /* Written by Richard Levitte for the OpenSSL sl@0: * project 2000. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #ifdef OPENSSL_NO_STDIO sl@0: #define APPS_WIN16 sl@0: #endif sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: #undef PROG sl@0: #define PROG engine_main sl@0: sl@0: static const char *engine_usage[]={ sl@0: "usage: engine opts [engine ...]\n", sl@0: " -v[v[v[v]]] - verbose mode, for each engine, list its 'control commands'\n", sl@0: " -vv will additionally display each command's description\n", sl@0: " -vvv will also add the input flags for each command\n", sl@0: " -vvvv will also show internal input flags\n", sl@0: " -c - for each engine, also list the capabilities\n", sl@0: " -t[t] - for each engine, check that they are really available\n", sl@0: " -tt will display error trace for unavailable engines\n", sl@0: " -pre - runs command 'cmd' against the ENGINE before any attempts\n", sl@0: " to load it (if -t is used)\n", sl@0: " -post - runs command 'cmd' against the ENGINE after loading it\n", sl@0: " (only used if -t is also provided)\n", sl@0: " NB: -pre and -post will be applied to all ENGINEs supplied on the command\n", sl@0: " line, or all supported ENGINEs if none are specified.\n", sl@0: " Eg. '-pre \"SO_PATH:/lib/libdriver.so\"' calls command \"SO_PATH\" with\n", sl@0: " argument \"/lib/libdriver.so\".\n", sl@0: NULL sl@0: }; sl@0: sl@0: static void identity(void *ptr) sl@0: { sl@0: return; sl@0: } sl@0: sl@0: static int append_buf(char **buf, const char *s, int *size, int step) sl@0: { sl@0: int l = strlen(s); sl@0: sl@0: if (*buf == NULL) sl@0: { sl@0: *size = step; sl@0: *buf = OPENSSL_malloc(*size); sl@0: if (*buf == NULL) sl@0: return 0; sl@0: **buf = '\0'; sl@0: } sl@0: sl@0: if (**buf != '\0') sl@0: l += 2; /* ", " */ sl@0: sl@0: if (strlen(*buf) + strlen(s) >= (unsigned int)*size) sl@0: { sl@0: *size += step; sl@0: *buf = OPENSSL_realloc(*buf, *size); sl@0: } sl@0: sl@0: if (*buf == NULL) sl@0: return 0; sl@0: sl@0: if (**buf != '\0') sl@0: BUF_strlcat(*buf, ", ", *size); sl@0: BUF_strlcat(*buf, s, *size); sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: static int util_flags(BIO *bio_out, unsigned int flags, const char *indent) sl@0: { sl@0: int started = 0, err = 0; sl@0: /* Indent before displaying input flags */ sl@0: BIO_printf(bio_out, "%s%s(input flags): ", indent, indent); sl@0: if(flags == 0) sl@0: { sl@0: BIO_printf(bio_out, "\n"); sl@0: return 1; sl@0: } sl@0: /* If the object is internal, mark it in a way that shows instead of sl@0: * having it part of all the other flags, even if it really is. */ sl@0: if(flags & ENGINE_CMD_FLAG_INTERNAL) sl@0: { sl@0: BIO_printf(bio_out, "[Internal] "); sl@0: } sl@0: sl@0: if(flags & ENGINE_CMD_FLAG_NUMERIC) sl@0: { sl@0: if(started) sl@0: { sl@0: BIO_printf(bio_out, "|"); sl@0: err = 1; sl@0: } sl@0: BIO_printf(bio_out, "NUMERIC"); sl@0: started = 1; sl@0: } sl@0: /* Now we check that no combinations of the mutually exclusive NUMERIC, sl@0: * STRING, and NO_INPUT flags have been used. Future flags that can be sl@0: * OR'd together with these would need to added after these to preserve sl@0: * the testing logic. */ sl@0: if(flags & ENGINE_CMD_FLAG_STRING) sl@0: { sl@0: if(started) sl@0: { sl@0: BIO_printf(bio_out, "|"); sl@0: err = 1; sl@0: } sl@0: BIO_printf(bio_out, "STRING"); sl@0: started = 1; sl@0: } sl@0: if(flags & ENGINE_CMD_FLAG_NO_INPUT) sl@0: { sl@0: if(started) sl@0: { sl@0: BIO_printf(bio_out, "|"); sl@0: err = 1; sl@0: } sl@0: BIO_printf(bio_out, "NO_INPUT"); sl@0: started = 1; sl@0: } sl@0: /* Check for unknown flags */ sl@0: flags = flags & ~ENGINE_CMD_FLAG_NUMERIC & sl@0: ~ENGINE_CMD_FLAG_STRING & sl@0: ~ENGINE_CMD_FLAG_NO_INPUT & sl@0: ~ENGINE_CMD_FLAG_INTERNAL; sl@0: if(flags) sl@0: { sl@0: if(started) BIO_printf(bio_out, "|"); sl@0: BIO_printf(bio_out, "<0x%04X>", flags); sl@0: } sl@0: if(err) sl@0: BIO_printf(bio_out, " "); sl@0: BIO_printf(bio_out, "\n"); sl@0: return 1; sl@0: } sl@0: sl@0: static int util_verbose(ENGINE *e, int verbose, BIO *bio_out, const char *indent) sl@0: { sl@0: static const int line_wrap = 78; sl@0: int num; sl@0: int ret = 0; sl@0: char *name = NULL; sl@0: char *desc = NULL; sl@0: int flags; sl@0: int xpos = 0; sl@0: STACK *cmds = NULL; sl@0: if(!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) || sl@0: ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE, sl@0: 0, NULL, NULL)) <= 0)) sl@0: { sl@0: #if 0 sl@0: BIO_printf(bio_out, "%s\n", indent); sl@0: #endif sl@0: return 1; sl@0: } sl@0: sl@0: cmds = sk_new_null(); sl@0: sl@0: if(!cmds) sl@0: goto err; sl@0: do { sl@0: int len; sl@0: /* Get the command input flags */ sl@0: if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num, sl@0: NULL, NULL)) < 0) sl@0: goto err; sl@0: if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) sl@0: { sl@0: /* Get the command name */ sl@0: if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num, sl@0: NULL, NULL)) <= 0) sl@0: goto err; sl@0: if((name = OPENSSL_malloc(len + 1)) == NULL) sl@0: goto err; sl@0: if(ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name, sl@0: NULL) <= 0) sl@0: goto err; sl@0: /* Get the command description */ sl@0: if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num, sl@0: NULL, NULL)) < 0) sl@0: goto err; sl@0: if(len > 0) sl@0: { sl@0: if((desc = OPENSSL_malloc(len + 1)) == NULL) sl@0: goto err; sl@0: if(ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc, sl@0: NULL) <= 0) sl@0: goto err; sl@0: } sl@0: /* Now decide on the output */ sl@0: if(xpos == 0) sl@0: /* Do an indent */ sl@0: xpos = BIO_printf(bio_out, indent); sl@0: else sl@0: /* Otherwise prepend a ", " */ sl@0: xpos += BIO_printf(bio_out, ", "); sl@0: if(verbose == 1) sl@0: { sl@0: /* We're just listing names, comma-delimited */ sl@0: if((xpos > (int)strlen(indent)) && sl@0: (xpos + (int)strlen(name) > line_wrap)) sl@0: { sl@0: BIO_printf(bio_out, "\n"); sl@0: xpos = BIO_printf(bio_out, indent); sl@0: } sl@0: xpos += BIO_printf(bio_out, "%s", name); sl@0: } sl@0: else sl@0: { sl@0: /* We're listing names plus descriptions */ sl@0: BIO_printf(bio_out, "%s: %s\n", name, sl@0: (desc == NULL) ? "" : desc); sl@0: /* ... and sometimes input flags */ sl@0: if((verbose >= 3) && !util_flags(bio_out, flags, sl@0: indent)) sl@0: goto err; sl@0: xpos = 0; sl@0: } sl@0: } sl@0: OPENSSL_free(name); name = NULL; sl@0: if(desc) { OPENSSL_free(desc); desc = NULL; } sl@0: /* Move to the next command */ sl@0: num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, sl@0: num, NULL, NULL); sl@0: } while(num > 0); sl@0: if(xpos > 0) sl@0: BIO_printf(bio_out, "\n"); sl@0: ret = 1; sl@0: err: sl@0: if(cmds) sk_pop_free(cmds, identity); sl@0: if(name) OPENSSL_free(name); sl@0: if(desc) OPENSSL_free(desc); sl@0: return ret; sl@0: } sl@0: sl@0: static void util_do_cmds(ENGINE *e, STACK *cmds, BIO *bio_out, const char *indent) sl@0: { sl@0: int loop, res, num = sk_num(cmds); sl@0: if(num < 0) sl@0: { sl@0: BIO_printf(bio_out, "[Error]: internal stack error\n"); sl@0: return; sl@0: } sl@0: for(loop = 0; loop < num; loop++) sl@0: { sl@0: char buf[256]; sl@0: const char *cmd, *arg; sl@0: cmd = sk_value(cmds, loop); sl@0: res = 1; /* assume success */ sl@0: /* Check if this command has no ":arg" */ sl@0: if((arg = strstr(cmd, ":")) == NULL) sl@0: { sl@0: if(!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0)) sl@0: res = 0; sl@0: } sl@0: else sl@0: { sl@0: if((int)(arg - cmd) > 254) sl@0: { sl@0: BIO_printf(bio_out,"[Error]: command name too long\n"); sl@0: return; sl@0: } sl@0: memcpy(buf, cmd, (int)(arg - cmd)); sl@0: buf[arg-cmd] = '\0'; sl@0: arg++; /* Move past the ":" */ sl@0: /* Call the command with the argument */ sl@0: if(!ENGINE_ctrl_cmd_string(e, buf, arg, 0)) sl@0: res = 0; sl@0: } sl@0: if(res) sl@0: BIO_printf(bio_out, "[Success]: %s\n", cmd); sl@0: else sl@0: { sl@0: BIO_printf(bio_out, "[Failure]: %s\n", cmd); sl@0: ERR_print_errors(bio_out); sl@0: } sl@0: } sl@0: } sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: int ret=1,i; sl@0: const char **pp; sl@0: int verbose=0, list_cap=0, test_avail=0, test_avail_noise = 0; sl@0: ENGINE *e; sl@0: STACK *engines = sk_new_null(); sl@0: STACK *pre_cmds = sk_new_null(); sl@0: STACK *post_cmds = sk_new_null(); sl@0: int badops=1; sl@0: BIO *bio_out=NULL; sl@0: const char *indent = " "; sl@0: sl@0: apps_startup(); sl@0: SSL_load_error_strings(); sl@0: sl@0: if (bio_err == NULL) sl@0: bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: bio_out=BIO_new_fp(stdout,BIO_NOCLOSE); sl@0: sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: bio_out = BIO_push(tmpbio, bio_out); sl@0: } sl@0: #endif sl@0: sl@0: argc--; sl@0: argv++; sl@0: while (argc >= 1) sl@0: { sl@0: if (strncmp(*argv,"-v",2) == 0) sl@0: { sl@0: if(strspn(*argv + 1, "v") < strlen(*argv + 1)) sl@0: goto skip_arg_loop; sl@0: if((verbose=strlen(*argv + 1)) > 4) sl@0: goto skip_arg_loop; sl@0: } sl@0: else if (strcmp(*argv,"-c") == 0) sl@0: list_cap=1; sl@0: else if (strncmp(*argv,"-t",2) == 0) sl@0: { sl@0: test_avail=1; sl@0: if(strspn(*argv + 1, "t") < strlen(*argv + 1)) sl@0: goto skip_arg_loop; sl@0: if((test_avail_noise = strlen(*argv + 1) - 1) > 1) sl@0: goto skip_arg_loop; sl@0: } sl@0: else if (strcmp(*argv,"-pre") == 0) sl@0: { sl@0: argc--; argv++; sl@0: if (argc == 0) sl@0: goto skip_arg_loop; sl@0: sk_push(pre_cmds,*argv); sl@0: } sl@0: else if (strcmp(*argv,"-post") == 0) sl@0: { sl@0: argc--; argv++; sl@0: if (argc == 0) sl@0: goto skip_arg_loop; sl@0: sk_push(post_cmds,*argv); sl@0: } sl@0: else if ((strncmp(*argv,"-h",2) == 0) || sl@0: (strcmp(*argv,"-?") == 0)) sl@0: goto skip_arg_loop; sl@0: else sl@0: sk_push(engines,*argv); sl@0: argc--; sl@0: argv++; sl@0: } sl@0: /* Looks like everything went OK */ sl@0: badops = 0; sl@0: skip_arg_loop: sl@0: sl@0: if (badops) sl@0: { sl@0: for (pp=engine_usage; (*pp != NULL); pp++) sl@0: BIO_printf(bio_err,"%s",*pp); sl@0: goto end; sl@0: } sl@0: sl@0: if (sk_num(engines) == 0) sl@0: { sl@0: for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) sl@0: { sl@0: sk_push(engines,(char *)ENGINE_get_id(e)); sl@0: } sl@0: } sl@0: sl@0: for (i=0; i 0) && !util_verbose(e, verbose, bio_out, indent)) sl@0: goto end; sl@0: ENGINE_free(e); sl@0: } sl@0: else sl@0: ERR_print_errors(bio_err); sl@0: } sl@0: sl@0: ret=0; sl@0: end: sl@0: sl@0: ERR_print_errors(bio_err); sl@0: sk_pop_free(engines, identity); sl@0: sk_pop_free(pre_cmds, identity); sl@0: sk_pop_free(post_cmds, identity); sl@0: if (bio_out != NULL) BIO_free_all(bio_out); sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ret); sl@0: } sl@0: #else sl@0: sl@0: # if PEDANTIC sl@0: static void *dummy=&dummy; sl@0: # endif sl@0: sl@0: #endif