1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/topenssl/src/engine.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,544 @@
1.4 +/* apps/engine.c -*- mode: C; c-file-style: "eay" -*- */
1.5 +/* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
1.6 + * project 2000.
1.7 + */
1.8 +/* ====================================================================
1.9 + * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
1.10 + *
1.11 + * Redistribution and use in source and binary forms, with or without
1.12 + * modification, are permitted provided that the following conditions
1.13 + * are met:
1.14 + *
1.15 + * 1. Redistributions of source code must retain the above copyright
1.16 + * notice, this list of conditions and the following disclaimer.
1.17 + *
1.18 + * 2. Redistributions in binary form must reproduce the above copyright
1.19 + * notice, this list of conditions and the following disclaimer in
1.20 + * the documentation and/or other materials provided with the
1.21 + * distribution.
1.22 + *
1.23 + * 3. All advertising materials mentioning features or use of this
1.24 + * software must display the following acknowledgment:
1.25 + * "This product includes software developed by the OpenSSL Project
1.26 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.27 + *
1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.29 + * endorse or promote products derived from this software without
1.30 + * prior written permission. For written permission, please contact
1.31 + * licensing@OpenSSL.org.
1.32 + *
1.33 + * 5. Products derived from this software may not be called "OpenSSL"
1.34 + * nor may "OpenSSL" appear in their names without prior written
1.35 + * permission of the OpenSSL Project.
1.36 + *
1.37 + * 6. Redistributions of any form whatsoever must retain the following
1.38 + * acknowledgment:
1.39 + * "This product includes software developed by the OpenSSL Project
1.40 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.41 + *
1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.45 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.54 + * ====================================================================
1.55 + *
1.56 + * This product includes cryptographic software written by Eric Young
1.57 + * (eay@cryptsoft.com). This product includes software written by Tim
1.58 + * Hudson (tjh@cryptsoft.com).
1.59 + *
1.60 + */
1.61 +
1.62 +#ifndef OPENSSL_NO_ENGINE
1.63 +
1.64 +#include <stdio.h>
1.65 +#include <stdlib.h>
1.66 +#include <string.h>
1.67 +#ifdef OPENSSL_NO_STDIO
1.68 +#define APPS_WIN16
1.69 +#endif
1.70 +#include "apps.h"
1.71 +#include <openssl/err.h>
1.72 +#include <openssl/engine.h>
1.73 +#include <openssl/ssl.h>
1.74 +
1.75 +
1.76 +#undef PROG
1.77 +#define PROG engine_main
1.78 +
1.79 +static const char *engine_usage[]={
1.80 +"usage: engine opts [engine ...]\n",
1.81 +" -v[v[v[v]]] - verbose mode, for each engine, list its 'control commands'\n",
1.82 +" -vv will additionally display each command's description\n",
1.83 +" -vvv will also add the input flags for each command\n",
1.84 +" -vvvv will also show internal input flags\n",
1.85 +" -c - for each engine, also list the capabilities\n",
1.86 +" -t[t] - for each engine, check that they are really available\n",
1.87 +" -tt will display error trace for unavailable engines\n",
1.88 +" -pre <cmd> - runs command 'cmd' against the ENGINE before any attempts\n",
1.89 +" to load it (if -t is used)\n",
1.90 +" -post <cmd> - runs command 'cmd' against the ENGINE after loading it\n",
1.91 +" (only used if -t is also provided)\n",
1.92 +" NB: -pre and -post will be applied to all ENGINEs supplied on the command\n",
1.93 +" line, or all supported ENGINEs if none are specified.\n",
1.94 +" Eg. '-pre \"SO_PATH:/lib/libdriver.so\"' calls command \"SO_PATH\" with\n",
1.95 +" argument \"/lib/libdriver.so\".\n",
1.96 +NULL
1.97 +};
1.98 +
1.99 +static void identity(void *ptr)
1.100 + {
1.101 + return;
1.102 + }
1.103 +
1.104 +static int append_buf(char **buf, const char *s, int *size, int step)
1.105 + {
1.106 + int l = strlen(s);
1.107 +
1.108 + if (*buf == NULL)
1.109 + {
1.110 + *size = step;
1.111 + *buf = OPENSSL_malloc(*size);
1.112 + if (*buf == NULL)
1.113 + return 0;
1.114 + **buf = '\0';
1.115 + }
1.116 +
1.117 + if (**buf != '\0')
1.118 + l += 2; /* ", " */
1.119 +
1.120 + if (strlen(*buf) + strlen(s) >= (unsigned int)*size)
1.121 + {
1.122 + *size += step;
1.123 + *buf = OPENSSL_realloc(*buf, *size);
1.124 + }
1.125 +
1.126 + if (*buf == NULL)
1.127 + return 0;
1.128 +
1.129 + if (**buf != '\0')
1.130 + BUF_strlcat(*buf, ", ", *size);
1.131 + BUF_strlcat(*buf, s, *size);
1.132 +
1.133 + return 1;
1.134 + }
1.135 +
1.136 +static int util_flags(BIO *bio_out, unsigned int flags, const char *indent)
1.137 + {
1.138 + int started = 0, err = 0;
1.139 + /* Indent before displaying input flags */
1.140 + BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
1.141 + if(flags == 0)
1.142 + {
1.143 + BIO_printf(bio_out, "<no flags>\n");
1.144 + return 1;
1.145 + }
1.146 + /* If the object is internal, mark it in a way that shows instead of
1.147 + * having it part of all the other flags, even if it really is. */
1.148 + if(flags & ENGINE_CMD_FLAG_INTERNAL)
1.149 + {
1.150 + BIO_printf(bio_out, "[Internal] ");
1.151 + }
1.152 +
1.153 + if(flags & ENGINE_CMD_FLAG_NUMERIC)
1.154 + {
1.155 + if(started)
1.156 + {
1.157 + BIO_printf(bio_out, "|");
1.158 + err = 1;
1.159 + }
1.160 + BIO_printf(bio_out, "NUMERIC");
1.161 + started = 1;
1.162 + }
1.163 + /* Now we check that no combinations of the mutually exclusive NUMERIC,
1.164 + * STRING, and NO_INPUT flags have been used. Future flags that can be
1.165 + * OR'd together with these would need to added after these to preserve
1.166 + * the testing logic. */
1.167 + if(flags & ENGINE_CMD_FLAG_STRING)
1.168 + {
1.169 + if(started)
1.170 + {
1.171 + BIO_printf(bio_out, "|");
1.172 + err = 1;
1.173 + }
1.174 + BIO_printf(bio_out, "STRING");
1.175 + started = 1;
1.176 + }
1.177 + if(flags & ENGINE_CMD_FLAG_NO_INPUT)
1.178 + {
1.179 + if(started)
1.180 + {
1.181 + BIO_printf(bio_out, "|");
1.182 + err = 1;
1.183 + }
1.184 + BIO_printf(bio_out, "NO_INPUT");
1.185 + started = 1;
1.186 + }
1.187 + /* Check for unknown flags */
1.188 + flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
1.189 + ~ENGINE_CMD_FLAG_STRING &
1.190 + ~ENGINE_CMD_FLAG_NO_INPUT &
1.191 + ~ENGINE_CMD_FLAG_INTERNAL;
1.192 + if(flags)
1.193 + {
1.194 + if(started) BIO_printf(bio_out, "|");
1.195 + BIO_printf(bio_out, "<0x%04X>", flags);
1.196 + }
1.197 + if(err)
1.198 + BIO_printf(bio_out, " <illegal flags!>");
1.199 + BIO_printf(bio_out, "\n");
1.200 + return 1;
1.201 + }
1.202 +
1.203 +static int util_verbose(ENGINE *e, int verbose, BIO *bio_out, const char *indent)
1.204 + {
1.205 + static const int line_wrap = 78;
1.206 + int num;
1.207 + int ret = 0;
1.208 + char *name = NULL;
1.209 + char *desc = NULL;
1.210 + int flags;
1.211 + int xpos = 0;
1.212 + STACK *cmds = NULL;
1.213 + if(!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
1.214 + ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
1.215 + 0, NULL, NULL)) <= 0))
1.216 + {
1.217 +#if 0
1.218 + BIO_printf(bio_out, "%s<no control commands>\n", indent);
1.219 +#endif
1.220 + return 1;
1.221 + }
1.222 +
1.223 + cmds = sk_new_null();
1.224 +
1.225 + if(!cmds)
1.226 + goto err;
1.227 + do {
1.228 + int len;
1.229 + /* Get the command input flags */
1.230 + if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
1.231 + NULL, NULL)) < 0)
1.232 + goto err;
1.233 + if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4)
1.234 + {
1.235 + /* Get the command name */
1.236 + if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
1.237 + NULL, NULL)) <= 0)
1.238 + goto err;
1.239 + if((name = OPENSSL_malloc(len + 1)) == NULL)
1.240 + goto err;
1.241 + if(ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
1.242 + NULL) <= 0)
1.243 + goto err;
1.244 + /* Get the command description */
1.245 + if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
1.246 + NULL, NULL)) < 0)
1.247 + goto err;
1.248 + if(len > 0)
1.249 + {
1.250 + if((desc = OPENSSL_malloc(len + 1)) == NULL)
1.251 + goto err;
1.252 + if(ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
1.253 + NULL) <= 0)
1.254 + goto err;
1.255 + }
1.256 + /* Now decide on the output */
1.257 + if(xpos == 0)
1.258 + /* Do an indent */
1.259 + xpos = BIO_printf(bio_out, indent);
1.260 + else
1.261 + /* Otherwise prepend a ", " */
1.262 + xpos += BIO_printf(bio_out, ", ");
1.263 + if(verbose == 1)
1.264 + {
1.265 + /* We're just listing names, comma-delimited */
1.266 + if((xpos > (int)strlen(indent)) &&
1.267 + (xpos + (int)strlen(name) > line_wrap))
1.268 + {
1.269 + BIO_printf(bio_out, "\n");
1.270 + xpos = BIO_printf(bio_out, indent);
1.271 + }
1.272 + xpos += BIO_printf(bio_out, "%s", name);
1.273 + }
1.274 + else
1.275 + {
1.276 + /* We're listing names plus descriptions */
1.277 + BIO_printf(bio_out, "%s: %s\n", name,
1.278 + (desc == NULL) ? "<no description>" : desc);
1.279 + /* ... and sometimes input flags */
1.280 + if((verbose >= 3) && !util_flags(bio_out, flags,
1.281 + indent))
1.282 + goto err;
1.283 + xpos = 0;
1.284 + }
1.285 + }
1.286 + OPENSSL_free(name); name = NULL;
1.287 + if(desc) { OPENSSL_free(desc); desc = NULL; }
1.288 + /* Move to the next command */
1.289 + num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE,
1.290 + num, NULL, NULL);
1.291 + } while(num > 0);
1.292 + if(xpos > 0)
1.293 + BIO_printf(bio_out, "\n");
1.294 + ret = 1;
1.295 +err:
1.296 + if(cmds) sk_pop_free(cmds, identity);
1.297 + if(name) OPENSSL_free(name);
1.298 + if(desc) OPENSSL_free(desc);
1.299 + return ret;
1.300 + }
1.301 +
1.302 +static void util_do_cmds(ENGINE *e, STACK *cmds, BIO *bio_out, const char *indent)
1.303 + {
1.304 + int loop, res, num = sk_num(cmds);
1.305 + if(num < 0)
1.306 + {
1.307 + BIO_printf(bio_out, "[Error]: internal stack error\n");
1.308 + return;
1.309 + }
1.310 + for(loop = 0; loop < num; loop++)
1.311 + {
1.312 + char buf[256];
1.313 + const char *cmd, *arg;
1.314 + cmd = sk_value(cmds, loop);
1.315 + res = 1; /* assume success */
1.316 + /* Check if this command has no ":arg" */
1.317 + if((arg = strstr(cmd, ":")) == NULL)
1.318 + {
1.319 + if(!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
1.320 + res = 0;
1.321 + }
1.322 + else
1.323 + {
1.324 + if((int)(arg - cmd) > 254)
1.325 + {
1.326 + BIO_printf(bio_out,"[Error]: command name too long\n");
1.327 + return;
1.328 + }
1.329 + memcpy(buf, cmd, (int)(arg - cmd));
1.330 + buf[arg-cmd] = '\0';
1.331 + arg++; /* Move past the ":" */
1.332 + /* Call the command with the argument */
1.333 + if(!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
1.334 + res = 0;
1.335 + }
1.336 + if(res)
1.337 + BIO_printf(bio_out, "[Success]: %s\n", cmd);
1.338 + else
1.339 + {
1.340 + BIO_printf(bio_out, "[Failure]: %s\n", cmd);
1.341 + ERR_print_errors(bio_out);
1.342 + }
1.343 + }
1.344 + }
1.345 +
1.346 +int MAIN(int, char **);
1.347 +
1.348 +int MAIN(int argc, char **argv)
1.349 + {
1.350 + int ret=1,i;
1.351 + const char **pp;
1.352 + int verbose=0, list_cap=0, test_avail=0, test_avail_noise = 0;
1.353 + ENGINE *e;
1.354 + STACK *engines = sk_new_null();
1.355 + STACK *pre_cmds = sk_new_null();
1.356 + STACK *post_cmds = sk_new_null();
1.357 + int badops=1;
1.358 + BIO *bio_out=NULL;
1.359 + const char *indent = " ";
1.360 +
1.361 + apps_startup();
1.362 + SSL_load_error_strings();
1.363 +
1.364 + if (bio_err == NULL)
1.365 + bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
1.366 +
1.367 + if (!load_config(bio_err, NULL))
1.368 + goto end;
1.369 + bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
1.370 +
1.371 +#ifdef OPENSSL_SYS_VMS
1.372 + {
1.373 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.374 + bio_out = BIO_push(tmpbio, bio_out);
1.375 + }
1.376 +#endif
1.377 +
1.378 + argc--;
1.379 + argv++;
1.380 + while (argc >= 1)
1.381 + {
1.382 + if (strncmp(*argv,"-v",2) == 0)
1.383 + {
1.384 + if(strspn(*argv + 1, "v") < strlen(*argv + 1))
1.385 + goto skip_arg_loop;
1.386 + if((verbose=strlen(*argv + 1)) > 4)
1.387 + goto skip_arg_loop;
1.388 + }
1.389 + else if (strcmp(*argv,"-c") == 0)
1.390 + list_cap=1;
1.391 + else if (strncmp(*argv,"-t",2) == 0)
1.392 + {
1.393 + test_avail=1;
1.394 + if(strspn(*argv + 1, "t") < strlen(*argv + 1))
1.395 + goto skip_arg_loop;
1.396 + if((test_avail_noise = strlen(*argv + 1) - 1) > 1)
1.397 + goto skip_arg_loop;
1.398 + }
1.399 + else if (strcmp(*argv,"-pre") == 0)
1.400 + {
1.401 + argc--; argv++;
1.402 + if (argc == 0)
1.403 + goto skip_arg_loop;
1.404 + sk_push(pre_cmds,*argv);
1.405 + }
1.406 + else if (strcmp(*argv,"-post") == 0)
1.407 + {
1.408 + argc--; argv++;
1.409 + if (argc == 0)
1.410 + goto skip_arg_loop;
1.411 + sk_push(post_cmds,*argv);
1.412 + }
1.413 + else if ((strncmp(*argv,"-h",2) == 0) ||
1.414 + (strcmp(*argv,"-?") == 0))
1.415 + goto skip_arg_loop;
1.416 + else
1.417 + sk_push(engines,*argv);
1.418 + argc--;
1.419 + argv++;
1.420 + }
1.421 + /* Looks like everything went OK */
1.422 + badops = 0;
1.423 +skip_arg_loop:
1.424 +
1.425 + if (badops)
1.426 + {
1.427 + for (pp=engine_usage; (*pp != NULL); pp++)
1.428 + BIO_printf(bio_err,"%s",*pp);
1.429 + goto end;
1.430 + }
1.431 +
1.432 + if (sk_num(engines) == 0)
1.433 + {
1.434 + for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
1.435 + {
1.436 + sk_push(engines,(char *)ENGINE_get_id(e));
1.437 + }
1.438 + }
1.439 +
1.440 + for (i=0; i<sk_num(engines); i++)
1.441 + {
1.442 + const char *id = sk_value(engines,i);
1.443 + if ((e = ENGINE_by_id(id)) != NULL)
1.444 + {
1.445 + const char *name = ENGINE_get_name(e);
1.446 + /* Do "id" first, then "name". Easier to auto-parse. */
1.447 + BIO_printf(bio_out, "(%s) %s\n", id, name);
1.448 + util_do_cmds(e, pre_cmds, bio_out, indent);
1.449 + if (strcmp(ENGINE_get_id(e), id) != 0)
1.450 + {
1.451 + BIO_printf(bio_out, "Loaded: (%s) %s\n",
1.452 + ENGINE_get_id(e), ENGINE_get_name(e));
1.453 + }
1.454 + if (list_cap)
1.455 + {
1.456 + int cap_size = 256;
1.457 + char *cap_buf = NULL;
1.458 + int k,n;
1.459 + const int *nids;
1.460 + ENGINE_CIPHERS_PTR fn_c;
1.461 + ENGINE_DIGESTS_PTR fn_d;
1.462 +
1.463 + if (ENGINE_get_RSA(e) != NULL
1.464 + && !append_buf(&cap_buf, "RSA",
1.465 + &cap_size, 256))
1.466 + goto end;
1.467 + if (ENGINE_get_DSA(e) != NULL
1.468 + && !append_buf(&cap_buf, "DSA",
1.469 + &cap_size, 256))
1.470 + goto end;
1.471 + if (ENGINE_get_DH(e) != NULL
1.472 + && !append_buf(&cap_buf, "DH",
1.473 + &cap_size, 256))
1.474 + goto end;
1.475 + if (ENGINE_get_RAND(e) != NULL
1.476 + && !append_buf(&cap_buf, "RAND",
1.477 + &cap_size, 256))
1.478 + goto end;
1.479 +
1.480 + fn_c = ENGINE_get_ciphers(e);
1.481 + if(!fn_c) goto skip_ciphers;
1.482 + n = fn_c(e, NULL, &nids, 0);
1.483 + for(k=0 ; k < n ; ++k)
1.484 + if(!append_buf(&cap_buf,
1.485 + OBJ_nid2sn(nids[k]),
1.486 + &cap_size, 256))
1.487 + goto end;
1.488 +
1.489 +skip_ciphers:
1.490 + fn_d = ENGINE_get_digests(e);
1.491 + if(!fn_d) goto skip_digests;
1.492 + n = fn_d(e, NULL, &nids, 0);
1.493 + for(k=0 ; k < n ; ++k)
1.494 + if(!append_buf(&cap_buf,
1.495 + OBJ_nid2sn(nids[k]),
1.496 + &cap_size, 256))
1.497 + goto end;
1.498 +
1.499 +skip_digests:
1.500 + if (cap_buf && (*cap_buf != '\0'))
1.501 + BIO_printf(bio_out, " [%s]\n", cap_buf);
1.502 +
1.503 + OPENSSL_free(cap_buf);
1.504 + }
1.505 + if(test_avail)
1.506 + {
1.507 + BIO_printf(bio_out, "%s", indent);
1.508 + if (ENGINE_init(e))
1.509 + {
1.510 + BIO_printf(bio_out, "[ available ]\n");
1.511 + util_do_cmds(e, post_cmds, bio_out, indent);
1.512 + ENGINE_finish(e);
1.513 + }
1.514 + else
1.515 + {
1.516 + BIO_printf(bio_out, "[ unavailable ]\n");
1.517 + if(test_avail_noise)
1.518 + ERR_print_errors_fp(stdout);
1.519 + ERR_clear_error();
1.520 + }
1.521 + }
1.522 + if((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
1.523 + goto end;
1.524 + ENGINE_free(e);
1.525 + }
1.526 + else
1.527 + ERR_print_errors(bio_err);
1.528 + }
1.529 +
1.530 + ret=0;
1.531 +end:
1.532 +
1.533 + ERR_print_errors(bio_err);
1.534 + sk_pop_free(engines, identity);
1.535 + sk_pop_free(pre_cmds, identity);
1.536 + sk_pop_free(post_cmds, identity);
1.537 + if (bio_out != NULL) BIO_free_all(bio_out);
1.538 + apps_shutdown();
1.539 + OPENSSL_EXIT(ret);
1.540 + }
1.541 +#else
1.542 +
1.543 +# if PEDANTIC
1.544 +static void *dummy=&dummy;
1.545 +# endif
1.546 +
1.547 +#endif