sl@0: /* apps/dgst.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. 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: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #undef BUFSIZE sl@0: #define BUFSIZE 1024*8 sl@0: sl@0: #undef PROG sl@0: #define PROG dgst_main sl@0: sl@0: int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, sl@0: EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title, sl@0: const char *file,BIO *bmd,const char *hmac_key); sl@0: sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: ENGINE *e = NULL; sl@0: unsigned char *buf=NULL; sl@0: int i,err=0; sl@0: const EVP_MD *md=NULL,*m; sl@0: BIO *in=NULL,*inp; sl@0: BIO *bmd=NULL; sl@0: BIO *out = NULL; sl@0: const char *name; sl@0: #define PROG_NAME_SIZE 39 sl@0: char pname[PROG_NAME_SIZE+1]; sl@0: int separator=0; sl@0: int debug=0; sl@0: int keyform=FORMAT_PEM; sl@0: const char *outfile = NULL, *keyfile = NULL; sl@0: const char *sigfile = NULL, *randfile = NULL; sl@0: int out_bin = -1, want_pub = 0, do_verify = 0; sl@0: EVP_PKEY *sigkey = NULL; sl@0: unsigned char *sigbuf = NULL; sl@0: int siglen = 0; sl@0: char *passargin = NULL, *passin = NULL; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine=NULL; sl@0: #endif sl@0: char *hmac_key=NULL; sl@0: sl@0: apps_startup(); sl@0: sl@0: if ((buf=(unsigned char *)OPENSSL_malloc(BUFSIZE)) == NULL) sl@0: { sl@0: BIO_printf(bio_err,"out of memory\n"); sl@0: goto end; sl@0: } sl@0: if (bio_err == NULL) sl@0: if ((bio_err=BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: sl@0: sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: sl@0: /* first check the program name */ sl@0: program_name(argv[0],pname,sizeof pname); sl@0: sl@0: md=EVP_get_digestbyname(pname); sl@0: sl@0: argc--; sl@0: argv++; sl@0: while (argc > 0) sl@0: { sl@0: if ((*argv)[0] != '-') break; sl@0: if (strcmp(*argv,"-c") == 0) sl@0: separator=1; sl@0: else if (strcmp(*argv,"-rand") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: randfile=*(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-out") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: outfile=*(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-sign") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: keyfile=*(++argv); sl@0: } sl@0: else if (!strcmp(*argv,"-passin")) sl@0: { sl@0: if (--argc < 1) sl@0: break; sl@0: passargin=*++argv; sl@0: } sl@0: else if (strcmp(*argv,"-verify") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: keyfile=*(++argv); sl@0: want_pub = 1; sl@0: do_verify = 1; sl@0: } sl@0: else if (strcmp(*argv,"-prverify") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: keyfile=*(++argv); sl@0: do_verify = 1; sl@0: } sl@0: else if (strcmp(*argv,"-signature") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: sigfile=*(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-keyform") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: keyform=str2fmt(*(++argv)); sl@0: } sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: else if (strcmp(*argv,"-engine") == 0) sl@0: { sl@0: if (--argc < 1) break; sl@0: engine= *(++argv); sl@0: } sl@0: #endif sl@0: else if (strcmp(*argv,"-hex") == 0) sl@0: out_bin = 0; sl@0: else if (strcmp(*argv,"-binary") == 0) sl@0: out_bin = 1; sl@0: else if (strcmp(*argv,"-d") == 0) sl@0: debug=1; sl@0: else if (!strcmp(*argv,"-hmac")) sl@0: { sl@0: if (--argc < 1) sl@0: break; sl@0: hmac_key=*++argv; sl@0: } sl@0: else if ((m=EVP_get_digestbyname(&((*argv)[1]))) != NULL) sl@0: md=m; sl@0: else sl@0: break; sl@0: argc--; sl@0: argv++; sl@0: } sl@0: sl@0: if (md == NULL) sl@0: md=EVP_md5(); sl@0: sl@0: if(do_verify && !sigfile) { sl@0: BIO_printf(bio_err, "No signature to verify: use the -signature option\n"); sl@0: err = 1; sl@0: goto end; sl@0: } sl@0: sl@0: if ((argc > 0) && (argv[0][0] == '-')) /* bad option */ sl@0: { sl@0: BIO_printf(bio_err,"unknown option '%s'\n",*argv); sl@0: BIO_printf(bio_err,"options are\n"); sl@0: BIO_printf(bio_err,"-c to output the digest with separating colons\n"); sl@0: BIO_printf(bio_err,"-d to output debug info\n"); sl@0: BIO_printf(bio_err,"-hex output as hex dump\n"); sl@0: BIO_printf(bio_err,"-binary output in binary form\n"); sl@0: BIO_printf(bio_err,"-sign file sign digest using private key in file\n"); sl@0: BIO_printf(bio_err,"-verify file verify a signature using public key in file\n"); sl@0: BIO_printf(bio_err,"-prverify file verify a signature using private key in file\n"); sl@0: BIO_printf(bio_err,"-keyform arg key file format (PEM or ENGINE)\n"); sl@0: BIO_printf(bio_err,"-signature file signature to verify\n"); sl@0: BIO_printf(bio_err,"-binary output in binary form\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: BIO_printf(bio_err,"-engine e use engine e, possibly a hardware device.\n"); sl@0: #endif sl@0: sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm (default)\n", sl@0: LN_md5,LN_md5); sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_md4,LN_md4); sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_md2,LN_md2); sl@0: #ifndef OPENSSL_NO_SHA sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_sha1,LN_sha1); sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_sha,LN_sha); sl@0: #ifndef OPENSSL_NO_SHA256 sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_sha224,LN_sha224); sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_sha256,LN_sha256); sl@0: #endif sl@0: #ifndef OPENSSL_NO_SHA512 sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_sha384,LN_sha384); sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_sha512,LN_sha512); sl@0: #endif sl@0: #endif sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_mdc2,LN_mdc2); sl@0: BIO_printf(bio_err,"-%-14s to use the %s message digest algorithm\n", sl@0: LN_ripemd160,LN_ripemd160); sl@0: err=1; sl@0: goto end; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: e = setup_engine(bio_err, engine, 0); sl@0: #endif sl@0: sl@0: in=BIO_new(BIO_s_file()); sl@0: bmd=BIO_new(BIO_f_md()); sl@0: if (debug) sl@0: { sl@0: BIO_set_callback(in,BIO_debug_callback); sl@0: /* needed for windows 3.1 */ sl@0: BIO_set_callback_arg(in,(char *)bio_err); sl@0: } sl@0: sl@0: if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) sl@0: { sl@0: BIO_printf(bio_err, "Error getting password\n"); sl@0: goto end; sl@0: } sl@0: sl@0: if ((in == NULL) || (bmd == NULL)) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: if(out_bin == -1) { sl@0: if(keyfile) out_bin = 1; sl@0: else out_bin = 0; sl@0: } sl@0: sl@0: if(randfile) sl@0: app_RAND_load_file(randfile, bio_err, 0); sl@0: sl@0: if(outfile) { sl@0: if(out_bin) sl@0: out = BIO_new_file(outfile, "wb"); sl@0: else out = BIO_new_file(outfile, "w"); sl@0: } else { sl@0: sl@0: out = BIO_new_fp(stdout, BIO_NOCLOSE); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: out = BIO_push(tmpbio, out); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: if(!out) { sl@0: BIO_printf(bio_err, "Error opening output file %s\n", sl@0: outfile ? outfile : "(stdout)"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: if(keyfile) sl@0: { sl@0: if (want_pub) sl@0: sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL, sl@0: e, "key file"); sl@0: else sl@0: sigkey = load_key(bio_err, keyfile, keyform, 0, passin, sl@0: e, "key file"); sl@0: if (!sigkey) sl@0: { sl@0: /* load_[pub]key() has already printed an appropriate sl@0: message */ sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: if(sigfile && sigkey) { sl@0: BIO *sigbio; sl@0: sigbio = BIO_new_file(sigfile, "rb"); sl@0: siglen = EVP_PKEY_size(sigkey); sl@0: sigbuf = OPENSSL_malloc(siglen); sl@0: if(!sigbio) { sl@0: BIO_printf(bio_err, "Error opening signature file %s\n", sl@0: sigfile); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: siglen = BIO_read(sigbio, sigbuf, siglen); sl@0: BIO_free(sigbio); sl@0: if(siglen <= 0) { sl@0: BIO_printf(bio_err, "Error reading signature file %s\n", sl@0: sigfile); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: /* we use md as a filter, reading from 'in' */ sl@0: if (!BIO_set_md(bmd,md)) sl@0: { sl@0: BIO_printf(bio_err, "Error setting digest %s\n", pname); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: inp=BIO_push(bmd,in); sl@0: sl@0: if (argc == 0) sl@0: { sl@0: sl@0: BIO_set_fp(in,stdin,BIO_NOCLOSE); sl@0: err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf, sl@0: siglen,"","(stdin)",bmd,hmac_key); sl@0: } sl@0: else sl@0: { sl@0: name=OBJ_nid2sn(md->type); sl@0: for (i=0; i 0) sl@0: BIO_printf(out, "Verified OK\n"); sl@0: else if(i == 0) sl@0: { sl@0: BIO_printf(out, "Verification Failure\n"); sl@0: return 1; sl@0: } sl@0: else sl@0: { sl@0: BIO_printf(bio_err, "Error Verifying Data\n"); sl@0: ERR_print_errors(bio_err); sl@0: return 1; sl@0: } sl@0: return 0; sl@0: } sl@0: if(key) sl@0: { sl@0: EVP_MD_CTX *ctx; sl@0: BIO_get_md_ctx(bp, &ctx); sl@0: if(!EVP_SignFinal(ctx, buf, (unsigned int *)&len, key)) sl@0: { sl@0: BIO_printf(bio_err, "Error Signing Data\n"); sl@0: ERR_print_errors(bio_err); sl@0: return 1; sl@0: } sl@0: } sl@0: else if(hmac_key) sl@0: { sl@0: HMAC_Final(&hmac_ctx,buf,&len); sl@0: HMAC_CTX_cleanup(&hmac_ctx); sl@0: } sl@0: else sl@0: len=BIO_gets(bp,(char *)buf,BUFSIZE); sl@0: sl@0: if(binout) BIO_write(out, buf, len); sl@0: else sl@0: { sl@0: BIO_write(out,title,strlen(title)); sl@0: for (i=0; i<(int)len; i++) sl@0: { sl@0: if (sep && (i != 0)) sl@0: BIO_printf(out, ":"); sl@0: BIO_printf(out, "%02x",buf[i]); sl@0: } sl@0: BIO_printf(out, "\n"); sl@0: } sl@0: if (hmac_key) sl@0: { sl@0: BIO_set_md_ctx(bmd,md_ctx); sl@0: } sl@0: return 0; sl@0: } sl@0: