1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/dgst.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,482 @@
1.4 +/* apps/dgst.c */
1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.6 + * All rights reserved.
1.7 + *
1.8 + * This package is an SSL implementation written
1.9 + * by Eric Young (eay@cryptsoft.com).
1.10 + * The implementation was written so as to conform with Netscapes SSL.
1.11 + *
1.12 + * This library is free for commercial and non-commercial use as long as
1.13 + * the following conditions are aheared to. The following conditions
1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
1.15 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.16 + * included with this distribution is covered by the same copyright terms
1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.18 + *
1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.20 + * the code are not to be removed.
1.21 + * If this package is used in a product, Eric Young should be given attribution
1.22 + * as the author of the parts of the library used.
1.23 + * This can be in the form of a textual message at program startup or
1.24 + * in documentation (online or textual) provided with the package.
1.25 + *
1.26 + * Redistribution and use in source and binary forms, with or without
1.27 + * modification, are permitted provided that the following conditions
1.28 + * are met:
1.29 + * 1. Redistributions of source code must retain the copyright
1.30 + * notice, this list of conditions and the following disclaimer.
1.31 + * 2. Redistributions in binary form must reproduce the above copyright
1.32 + * notice, this list of conditions and the following disclaimer in the
1.33 + * documentation and/or other materials provided with the distribution.
1.34 + * 3. All advertising materials mentioning features or use of this software
1.35 + * must display the following acknowledgement:
1.36 + * "This product includes cryptographic software written by
1.37 + * Eric Young (eay@cryptsoft.com)"
1.38 + * The word 'cryptographic' can be left out if the rouines from the library
1.39 + * being used are not cryptographic related :-).
1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.41 + * the apps directory (application code) you must include an acknowledgement:
1.42 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.43 + *
1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.47 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.54 + * SUCH DAMAGE.
1.55 + *
1.56 + * The licence and distribution terms for any publically available version or
1.57 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.58 + * copied and put under another distribution licence
1.59 + * [including the GNU Public Licence.]
1.60 + */
1.61 +
1.62 +#include <stdio.h>
1.63 +#include <string.h>
1.64 +#include <stdlib.h>
1.65 +#include "apps.h"
1.66 +#include <openssl/bio.h>
1.67 +#include <openssl/err.h>
1.68 +#include <openssl/evp.h>
1.69 +#include <openssl/objects.h>
1.70 +#include <openssl/x509.h>
1.71 +#include <openssl/pem.h>
1.72 +
1.73 +#undef BUFSIZE
1.74 +#define BUFSIZE 1024*8
1.75 +
1.76 +#undef PROG
1.77 +#define PROG dgst_main
1.78 +
1.79 +int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
1.80 + EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
1.81 + const char *file);
1.82 +
1.83 +
1.84 +int MAIN(int, char **);
1.85 +
1.86 +int MAIN(int argc, char **argv)
1.87 + {
1.88 + ENGINE *e = NULL;
1.89 + unsigned char *buf=NULL;
1.90 + int i,err=0;
1.91 + const EVP_MD *md=NULL,*m;
1.92 + BIO *in=NULL,*inp;
1.93 + BIO *bmd=NULL;
1.94 + BIO *out = NULL;
1.95 + const char *name;
1.96 +#define PROG_NAME_SIZE 39
1.97 + char pname[PROG_NAME_SIZE+1];
1.98 + int separator=0;
1.99 + int debug=0;
1.100 + int keyform=FORMAT_PEM;
1.101 + const char *outfile = NULL, *keyfile = NULL;
1.102 + const char *sigfile = NULL, *randfile = NULL;
1.103 + int out_bin = -1, want_pub = 0, do_verify = 0;
1.104 + EVP_PKEY *sigkey = NULL;
1.105 + unsigned char *sigbuf = NULL;
1.106 + int siglen = 0;
1.107 + char *passargin = NULL, *passin = NULL;
1.108 +#ifndef OPENSSL_NO_ENGINE
1.109 + char *engine=NULL;
1.110 +#endif
1.111 +
1.112 + apps_startup();
1.113 +
1.114 + if ((buf=(unsigned char *)OPENSSL_malloc(BUFSIZE)) == NULL)
1.115 + {
1.116 + BIO_printf(bio_err,"out of memory\n");
1.117 + goto end;
1.118 + }
1.119 + if (bio_err == NULL)
1.120 + if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1.121 + BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1.122 +
1.123 +
1.124 +
1.125 + if (!load_config(bio_err, NULL))
1.126 + goto end;
1.127 +
1.128 + /* first check the program name */
1.129 + program_name(argv[0],pname,sizeof pname);
1.130 +
1.131 + md=EVP_get_digestbyname(pname);
1.132 +
1.133 + argc--;
1.134 + argv++;
1.135 + while (argc > 0)
1.136 + {
1.137 + if ((*argv)[0] != '-') break;
1.138 + if (strcmp(*argv,"-c") == 0)
1.139 + separator=1;
1.140 + else if (strcmp(*argv,"-rand") == 0)
1.141 + {
1.142 + if (--argc < 1) break;
1.143 + randfile=*(++argv);
1.144 + }
1.145 + else if (strcmp(*argv,"-out") == 0)
1.146 + {
1.147 + if (--argc < 1) break;
1.148 + outfile=*(++argv);
1.149 + }
1.150 + else if (strcmp(*argv,"-sign") == 0)
1.151 + {
1.152 + if (--argc < 1) break;
1.153 + keyfile=*(++argv);
1.154 + }
1.155 + else if (!strcmp(*argv,"-passin"))
1.156 + {
1.157 + if (--argc < 1)
1.158 + break;
1.159 + passargin=*++argv;
1.160 + }
1.161 + else if (strcmp(*argv,"-verify") == 0)
1.162 + {
1.163 + if (--argc < 1) break;
1.164 + keyfile=*(++argv);
1.165 + want_pub = 1;
1.166 + do_verify = 1;
1.167 + }
1.168 + else if (strcmp(*argv,"-prverify") == 0)
1.169 + {
1.170 + if (--argc < 1) break;
1.171 + keyfile=*(++argv);
1.172 + do_verify = 1;
1.173 + }
1.174 + else if (strcmp(*argv,"-signature") == 0)
1.175 + {
1.176 + if (--argc < 1) break;
1.177 + sigfile=*(++argv);
1.178 + }
1.179 + else if (strcmp(*argv,"-keyform") == 0)
1.180 + {
1.181 + if (--argc < 1) break;
1.182 + keyform=str2fmt(*(++argv));
1.183 + }
1.184 +#ifndef OPENSSL_NO_ENGINE
1.185 + else if (strcmp(*argv,"-engine") == 0)
1.186 + {
1.187 + if (--argc < 1) break;
1.188 + engine= *(++argv);
1.189 + }
1.190 +#endif
1.191 + else if (strcmp(*argv,"-hex") == 0)
1.192 + out_bin = 0;
1.193 + else if (strcmp(*argv,"-binary") == 0)
1.194 + out_bin = 1;
1.195 + else if (strcmp(*argv,"-d") == 0)
1.196 + debug=1;
1.197 + else if ((m=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
1.198 + md=m;
1.199 + else
1.200 + break;
1.201 + argc--;
1.202 + argv++;
1.203 + }
1.204 +
1.205 + if (md == NULL)
1.206 + md=EVP_md5();
1.207 +
1.208 + if(do_verify && !sigfile) {
1.209 + BIO_printf(bio_err, "No signature to verify: use the -signature option\n");
1.210 + err = 1;
1.211 + goto end;
1.212 + }
1.213 +
1.214 + if ((argc > 0) && (argv[0][0] == '-')) /* bad option */
1.215 + {
1.216 + BIO_printf(bio_err,"unknown option '%s'\n",*argv);
1.217 + BIO_printf(bio_err,"options are\n");
1.218 + BIO_printf(bio_err,"-c to output the digest with separating colons\n");
1.219 + BIO_printf(bio_err,"-d to output debug info\n");
1.220 + BIO_printf(bio_err,"-hex output as hex dump\n");
1.221 + BIO_printf(bio_err,"-binary output in binary form\n");
1.222 + BIO_printf(bio_err,"-sign file sign digest using private key in file\n");
1.223 + BIO_printf(bio_err,"-verify file verify a signature using public key in file\n");
1.224 + BIO_printf(bio_err,"-prverify file verify a signature using private key in file\n");
1.225 + BIO_printf(bio_err,"-keyform arg key file format (PEM or ENGINE)\n");
1.226 + BIO_printf(bio_err,"-signature file signature to verify\n");
1.227 + BIO_printf(bio_err,"-binary output in binary form\n");
1.228 +#ifndef OPENSSL_NO_ENGINE
1.229 + BIO_printf(bio_err,"-engine e use engine e, possibly a hardware device.\n");
1.230 +#endif
1.231 +
1.232 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm (default)\n",
1.233 + LN_md5,LN_md5);
1.234 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.235 + LN_md4,LN_md4);
1.236 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.237 + LN_md2,LN_md2);
1.238 +#ifndef OPENSSL_NO_SHA
1.239 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.240 + LN_sha1,LN_sha1);
1.241 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.242 + LN_sha,LN_sha);
1.243 +#ifndef OPENSSL_NO_SHA256
1.244 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.245 + LN_sha256,LN_sha256);
1.246 +#endif
1.247 +#ifndef OPENSSL_NO_SHA512
1.248 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.249 + LN_sha512,LN_sha512);
1.250 +#endif
1.251 +#endif
1.252 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.253 + LN_mdc2,LN_mdc2);
1.254 + BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
1.255 + LN_ripemd160,LN_ripemd160);
1.256 + err=1;
1.257 + goto end;
1.258 + }
1.259 +
1.260 +#ifndef OPENSSL_NO_ENGINE
1.261 + e = setup_engine(bio_err, engine, 0);
1.262 +#endif
1.263 +
1.264 + in=BIO_new(BIO_s_file());
1.265 + bmd=BIO_new(BIO_f_md());
1.266 + if (debug)
1.267 + {
1.268 + BIO_set_callback(in,BIO_debug_callback);
1.269 + /* needed for windows 3.1 */
1.270 + BIO_set_callback_arg(in,(char *)bio_err);
1.271 + }
1.272 +
1.273 + if(!app_passwd(bio_err, passargin, NULL, &passin, NULL))
1.274 + {
1.275 + BIO_printf(bio_err, "Error getting password\n");
1.276 + goto end;
1.277 + }
1.278 +
1.279 + if ((in == NULL) || (bmd == NULL))
1.280 + {
1.281 + ERR_print_errors(bio_err);
1.282 + goto end;
1.283 + }
1.284 +
1.285 + if(out_bin == -1) {
1.286 + if(keyfile) out_bin = 1;
1.287 + else out_bin = 0;
1.288 + }
1.289 +
1.290 + if(randfile)
1.291 + app_RAND_load_file(randfile, bio_err, 0);
1.292 +
1.293 + if(outfile) {
1.294 + if(out_bin)
1.295 + out = BIO_new_file(outfile, "wb");
1.296 + else out = BIO_new_file(outfile, "w");
1.297 + } else {
1.298 +
1.299 + out = BIO_new_fp(stdout, BIO_NOCLOSE);
1.300 +#ifdef OPENSSL_SYS_VMS
1.301 + {
1.302 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.303 + out = BIO_push(tmpbio, out);
1.304 + }
1.305 +#endif
1.306 + }
1.307 +
1.308 + if(!out) {
1.309 + BIO_printf(bio_err, "Error opening output file %s\n",
1.310 + outfile ? outfile : "(stdout)");
1.311 + ERR_print_errors(bio_err);
1.312 + goto end;
1.313 + }
1.314 +
1.315 + if(keyfile)
1.316 + {
1.317 + if (want_pub)
1.318 + sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL,
1.319 + e, "key file");
1.320 + else
1.321 + sigkey = load_key(bio_err, keyfile, keyform, 0, passin,
1.322 + e, "key file");
1.323 + if (!sigkey)
1.324 + {
1.325 + /* load_[pub]key() has already printed an appropriate
1.326 + message */
1.327 + goto end;
1.328 + }
1.329 + }
1.330 +
1.331 + if(sigfile && sigkey) {
1.332 + BIO *sigbio;
1.333 + sigbio = BIO_new_file(sigfile, "rb");
1.334 + siglen = EVP_PKEY_size(sigkey);
1.335 + sigbuf = OPENSSL_malloc(siglen);
1.336 + if(!sigbio) {
1.337 + BIO_printf(bio_err, "Error opening signature file %s\n",
1.338 + sigfile);
1.339 + ERR_print_errors(bio_err);
1.340 + goto end;
1.341 + }
1.342 + siglen = BIO_read(sigbio, sigbuf, siglen);
1.343 + BIO_free(sigbio);
1.344 + if(siglen <= 0) {
1.345 + BIO_printf(bio_err, "Error reading signature file %s\n",
1.346 + sigfile);
1.347 + ERR_print_errors(bio_err);
1.348 + goto end;
1.349 + }
1.350 + }
1.351 +
1.352 +
1.353 +
1.354 + /* we use md as a filter, reading from 'in' */
1.355 + if (!BIO_set_md(bmd,md))
1.356 + {
1.357 + BIO_printf(bio_err, "Error setting digest %s\n", pname);
1.358 + ERR_print_errors(bio_err);
1.359 + goto end;
1.360 + }
1.361 +
1.362 + inp=BIO_push(bmd,in);
1.363 +
1.364 + if (argc == 0)
1.365 + {
1.366 +
1.367 + BIO_set_fp(in,stdin,BIO_NOCLOSE);
1.368 + err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf,
1.369 + siglen,"","(stdin)");
1.370 + }
1.371 + else
1.372 + {
1.373 + name=OBJ_nid2sn(md->type);
1.374 + for (i=0; i<argc; i++)
1.375 + {
1.376 + char *tmp,*tofree=NULL;
1.377 + int r;
1.378 +
1.379 + if (BIO_read_filename(in,argv[i]) <= 0)
1.380 + {
1.381 + perror(argv[i]);
1.382 + err++;
1.383 + continue;
1.384 + }
1.385 + if(!out_bin)
1.386 + {
1.387 + size_t len = strlen(name)+strlen(argv[i])+5;
1.388 + tmp=tofree=OPENSSL_malloc(len);
1.389 + BIO_snprintf(tmp,len,"%s(%s)= ",name,argv[i]);
1.390 + }
1.391 + else
1.392 + tmp="";
1.393 + r=do_fp(out,buf,inp,separator,out_bin,sigkey,sigbuf,
1.394 + siglen,tmp,argv[i]);
1.395 + if(r)
1.396 + err=r;
1.397 + if(tofree)
1.398 + OPENSSL_free(tofree);
1.399 + (void)BIO_reset(bmd);
1.400 + }
1.401 + }
1.402 +end:
1.403 + if (buf != NULL)
1.404 + {
1.405 + OPENSSL_cleanse(buf,BUFSIZE);
1.406 + OPENSSL_free(buf);
1.407 + }
1.408 + if (in != NULL) BIO_free(in);
1.409 + if (passin)
1.410 + OPENSSL_free(passin);
1.411 + BIO_free_all(out);
1.412 + EVP_PKEY_free(sigkey);
1.413 + if(sigbuf) OPENSSL_free(sigbuf);
1.414 + if (bmd != NULL) BIO_free(bmd);
1.415 + apps_shutdown();
1.416 + OPENSSL_EXIT(err);
1.417 + }
1.418 +
1.419 +int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
1.420 + EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
1.421 + const char *file)
1.422 + {
1.423 + int len;
1.424 + int i;
1.425 +
1.426 + for (;;)
1.427 + {
1.428 + i=BIO_read(bp,(char *)buf,BUFSIZE);
1.429 + if(i < 0)
1.430 + {
1.431 + BIO_printf(bio_err, "Read Error in %s\n",file);
1.432 + ERR_print_errors(bio_err);
1.433 + return 1;
1.434 + }
1.435 + if (i == 0) break;
1.436 + }
1.437 + if(sigin)
1.438 + {
1.439 + EVP_MD_CTX *ctx;
1.440 + BIO_get_md_ctx(bp, &ctx);
1.441 + i = EVP_VerifyFinal(ctx, sigin, (unsigned int)siglen, key);
1.442 + if(i > 0)
1.443 + BIO_printf(out, "Verified OK\n");
1.444 + else if(i == 0)
1.445 + {
1.446 + BIO_printf(out, "Verification Failure\n");
1.447 + return 1;
1.448 + }
1.449 + else
1.450 + {
1.451 + BIO_printf(bio_err, "Error Verifying Data\n");
1.452 + ERR_print_errors(bio_err);
1.453 + return 1;
1.454 + }
1.455 + return 0;
1.456 + }
1.457 + if(key)
1.458 + {
1.459 + EVP_MD_CTX *ctx;
1.460 + BIO_get_md_ctx(bp, &ctx);
1.461 + if(!EVP_SignFinal(ctx, buf, (unsigned int *)&len, key))
1.462 + {
1.463 + BIO_printf(bio_err, "Error Signing Data\n");
1.464 + ERR_print_errors(bio_err);
1.465 + return 1;
1.466 + }
1.467 + }
1.468 + else
1.469 + len=BIO_gets(bp,(char *)buf,BUFSIZE);
1.470 +
1.471 + if(binout) BIO_write(out, buf, len);
1.472 + else
1.473 + {
1.474 + BIO_write(out,title,strlen(title));
1.475 + for (i=0; i<len; i++)
1.476 + {
1.477 + if (sep && (i != 0))
1.478 + BIO_printf(out, ":");
1.479 + BIO_printf(out, "%02x",buf[i]);
1.480 + }
1.481 + BIO_printf(out, "\n");
1.482 + }
1.483 + return 0;
1.484 + }
1.485 +