2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 /* S/MIME utility function */
64 #include <openssl/crypto.h>
65 #include <openssl/pem.h>
66 #include <openssl/err.h>
67 #include <openssl/x509_vfy.h>
68 #include <openssl/x509v3.h>
70 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
71 static int smime_cb(int ok, X509_STORE_CTX *ctx);
75 #define SMIME_SIGNERS 0x40
76 #define SMIME_ENCRYPT (1 | SMIME_OP)
77 #define SMIME_DECRYPT (2 | SMIME_IP)
78 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
79 #define SMIME_VERIFY (4 | SMIME_IP)
80 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
81 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
83 typedef enum OPTION_choice {
84 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
85 OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY,
86 OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN,
87 OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP,
88 OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF,
89 OPT_NOOLDMIME, OPT_CRLFEOL, OPT_RAND, OPT_ENGINE, OPT_PASSIN,
90 OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD,
91 OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE,
93 OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, OPT_IN, OPT_INFORM, OPT_OUT,
94 OPT_OUTFORM, OPT_CONTENT
97 OPTIONS smime_options[] = {
98 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
99 {OPT_HELP_STR, 1, '-',
100 " cert.pem... recipient certs for encryption\n"},
101 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
102 {"help", OPT_HELP, '-', "Display this summary"},
103 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
104 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
105 {"sign", OPT_SIGN, '-', "Sign message"},
106 {"verify", OPT_VERIFY, '-', "Verify signed message"},
107 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
108 {"nointern", OPT_NOINTERN, '-',
109 "Don't search certificates in message for signer"},
110 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
111 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
112 {"nocerts", OPT_NOCERTS, '-',
113 "Don't include signers certificate when signing"},
114 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
115 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
116 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
117 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
118 {"signer", OPT_SIGNER, '<', "Signer certificate file"},
119 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
120 {"in", OPT_IN, '<', "Input file"},
121 {"inform", OPT_INFORM, 'F', "Input format SMIME (default), PEM or DER"},
122 {"inkey", OPT_INKEY, '<',
123 "Input private key (if not signer or recipient)"},
124 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
125 {"out", OPT_OUT, '>', "Output file"},
126 {"outform", OPT_OUTFORM, 'F',
127 "Output format SMIME (default), PEM or DER"},
128 {"content", OPT_CONTENT, '<',
129 "Supply or override content for detached signature"},
130 {"to", OPT_TO, 's', "To address"},
131 {"from", OPT_FROM, 's', "From address"},
132 {"subject", OPT_SUBJECT, 's', "Subject"},
133 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
134 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
135 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
136 {"no-CAfile", OPT_NOCAFILE, '-',
137 "Do not load the default certificates file"},
138 {"no-CApath", OPT_NOCAPATH, '-',
139 "Do not load certificates from the default certificates directory"},
140 {"resign", OPT_RESIGN, '-'},
141 {"nochain", OPT_NOCHAIN, '-'},
142 {"nosmimecap", OPT_NOSMIMECAP, '-'},
143 {"stream", OPT_STREAM, '-'},
144 {"indef", OPT_INDEF, '-'},
145 {"noindef", OPT_NOINDEF, '-'},
146 {"nooldmime", OPT_NOOLDMIME, '-'},
147 {"crlfeol", OPT_CRLFEOL, '-'},
148 {"rand", OPT_RAND, 's',
149 "Load the file(s) into the random number generator"},
150 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
152 {"", OPT_CIPHER, '-', "Any supported cipher"},
154 #ifndef OPENSSL_NO_ENGINE
155 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
160 int smime_main(int argc, char **argv)
162 BIO *in = NULL, *out = NULL, *indata = NULL;
163 EVP_PKEY *key = NULL;
165 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
166 STACK_OF(X509) *encerts = NULL, *other = NULL;
167 X509 *cert = NULL, *recip = NULL, *signer = NULL;
168 X509_STORE *store = NULL;
169 X509_VERIFY_PARAM *vpm = NULL;
170 const EVP_CIPHER *cipher = NULL;
171 const EVP_MD *sign_md = NULL;
172 char *CAfile = NULL, *CApath = NULL, *inrand = NULL;
173 char *certfile = NULL, *keyfile = NULL, *contfile = NULL, *prog;
174 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile =
176 char *passinarg = NULL, *passin = NULL, *to = NULL, *from =
177 NULL, *subject = NULL;
179 int noCApath = 0, noCAfile = 0;
180 int flags = PKCS7_DETACHED, operation = 0, ret = 0, need_rand = 0, indef =
182 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
184 int vpmtouched = 0, rv = 0;
187 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
190 prog = opt_init(argc, argv, smime_options);
191 while ((o = opt_next()) != OPT_EOF) {
196 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
199 opt_help(smime_options);
203 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
210 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
217 operation = SMIME_ENCRYPT;
220 operation = SMIME_DECRYPT;
223 operation = SMIME_SIGN;
226 operation = SMIME_RESIGN;
229 operation = SMIME_VERIFY;
232 operation = SMIME_PK7OUT;
238 flags |= PKCS7_NOINTERN;
241 flags |= PKCS7_NOVERIFY;
244 flags |= PKCS7_NOCHAIN;
247 flags |= PKCS7_NOCERTS;
250 flags |= PKCS7_NOATTR;
253 flags &= ~PKCS7_DETACHED;
256 flags |= PKCS7_NOSMIMECAP;
259 flags |= PKCS7_BINARY;
262 flags |= PKCS7_NOSIGS;
272 flags |= PKCS7_NOOLDMIMETYPE;
275 flags |= PKCS7_CRLFEOL;
282 e = setup_engine(opt_arg(), 0);
285 passinarg = opt_arg();
297 /* If previous -signer argument add signer to list */
299 if (sksigners == NULL
300 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
302 sk_OPENSSL_STRING_push(sksigners, signerfile);
304 keyfile = signerfile;
306 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
308 sk_OPENSSL_STRING_push(skkeys, keyfile);
311 signerfile = opt_arg();
314 recipfile = opt_arg();
317 if (!opt_md(opt_arg(), &sign_md))
321 if (!opt_cipher(opt_unknown(), &cipher))
325 /* If previous -inkey arument add signer to list */
327 if (signerfile == NULL) {
329 "%s: Must have -signer before -inkey\n", prog);
332 if (sksigners == NULL
333 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
335 sk_OPENSSL_STRING_push(sksigners, signerfile);
338 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
340 sk_OPENSSL_STRING_push(skkeys, keyfile);
345 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
349 certfile = opt_arg();
364 contfile = opt_arg();
367 if (!opt_verify(o, vpm))
373 argc = opt_num_rest();
376 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
377 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
381 if (operation & SMIME_SIGNERS) {
382 /* Check to see if any final signer needs to be appended */
383 if (keyfile && !signerfile) {
384 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
389 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
391 sk_OPENSSL_STRING_push(sksigners, signerfile);
392 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
395 keyfile = signerfile;
396 sk_OPENSSL_STRING_push(skkeys, keyfile);
399 BIO_printf(bio_err, "No signer certificate specified\n");
405 } else if (operation == SMIME_DECRYPT) {
406 if (!recipfile && !keyfile) {
408 "No recipient certificate or key specified\n");
411 } else if (operation == SMIME_ENCRYPT) {
413 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
417 } else if (!operation)
420 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
421 BIO_printf(bio_err, "Error getting password\n");
426 app_RAND_load_file(NULL, (inrand != NULL));
428 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
429 app_RAND_load_files(inrand));
434 if (!(operation & SMIME_SIGNERS))
435 flags &= ~PKCS7_DETACHED;
437 if (!(operation & SMIME_OP)) {
438 if (flags & PKCS7_BINARY)
439 outformat = FORMAT_BINARY;
442 if (!(operation & SMIME_IP)) {
443 if (flags & PKCS7_BINARY)
444 informat = FORMAT_BINARY;
447 if (operation == SMIME_ENCRYPT) {
449 #ifndef OPENSSL_NO_DES
450 cipher = EVP_des_ede3_cbc();
452 BIO_printf(bio_err, "No cipher selected\n");
456 encerts = sk_X509_new_null();
460 cert = load_cert(*argv, FORMAT_PEM,
461 "recipient certificate file");
464 sk_X509_push(encerts, cert);
471 if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
472 "certificate file")) {
473 ERR_print_errors(bio_err);
478 if (recipfile && (operation == SMIME_DECRYPT)) {
479 if ((recip = load_cert(recipfile, FORMAT_PEM,
480 "recipient certificate file")) == NULL) {
481 ERR_print_errors(bio_err);
486 if (operation == SMIME_DECRYPT) {
489 } else if (operation == SMIME_SIGN) {
491 keyfile = signerfile;
496 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
501 in = bio_open_default(infile, 'r', informat);
505 if (operation & SMIME_IP) {
506 if (informat == FORMAT_SMIME)
507 p7 = SMIME_read_PKCS7(in, &indata);
508 else if (informat == FORMAT_PEM)
509 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
510 else if (informat == FORMAT_ASN1)
511 p7 = d2i_PKCS7_bio(in, NULL);
513 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
518 BIO_printf(bio_err, "Error reading S/MIME message\n");
523 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
524 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
530 out = bio_open_default(outfile, 'w', outformat);
534 if (operation == SMIME_VERIFY) {
535 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
537 X509_STORE_set_verify_cb(store, smime_cb);
539 X509_STORE_set1_param(store, vpm);
544 if (operation == SMIME_ENCRYPT) {
546 flags |= PKCS7_STREAM;
547 p7 = PKCS7_encrypt(encerts, in, cipher, flags);
548 } else if (operation & SMIME_SIGNERS) {
551 * If detached data content we only enable streaming if S/MIME output
554 if (operation == SMIME_SIGN) {
555 if (flags & PKCS7_DETACHED) {
556 if (outformat == FORMAT_SMIME)
557 flags |= PKCS7_STREAM;
559 flags |= PKCS7_STREAM;
560 flags |= PKCS7_PARTIAL;
561 p7 = PKCS7_sign(NULL, NULL, other, in, flags);
564 if (flags & PKCS7_NOCERTS) {
565 for (i = 0; i < sk_X509_num(other); i++) {
566 X509 *x = sk_X509_value(other, i);
567 PKCS7_add_certificate(p7, x);
571 flags |= PKCS7_REUSE_DIGEST;
572 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
573 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
574 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
575 signer = load_cert(signerfile, FORMAT_PEM,
576 "signer certificate");
579 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
582 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
589 /* If not streaming or resigning finalize structure */
590 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
591 if (!PKCS7_final(p7, in, flags))
597 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
602 if (operation == SMIME_DECRYPT) {
603 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
604 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
607 } else if (operation == SMIME_VERIFY) {
608 STACK_OF(X509) *signers;
609 if (PKCS7_verify(p7, other, store, indata, out, flags))
610 BIO_printf(bio_err, "Verification successful\n");
612 BIO_printf(bio_err, "Verification failure\n");
615 signers = PKCS7_get0_signers(p7, other, flags);
616 if (!save_certs(signerfile, signers)) {
617 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
621 sk_X509_free(signers);
622 } else if (operation == SMIME_PK7OUT)
623 PEM_write_bio_PKCS7(out, p7);
626 BIO_printf(out, "To: %s\n", to);
628 BIO_printf(out, "From: %s\n", from);
630 BIO_printf(out, "Subject: %s\n", subject);
631 if (outformat == FORMAT_SMIME) {
632 if (operation == SMIME_RESIGN)
633 rv = SMIME_write_PKCS7(out, p7, indata, flags);
635 rv = SMIME_write_PKCS7(out, p7, in, flags);
636 } else if (outformat == FORMAT_PEM)
637 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
638 else if (outformat == FORMAT_ASN1)
639 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
641 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
645 BIO_printf(bio_err, "Error writing output\n");
653 app_RAND_write_file(NULL);
655 ERR_print_errors(bio_err);
656 sk_X509_pop_free(encerts, X509_free);
657 sk_X509_pop_free(other, X509_free);
658 X509_VERIFY_PARAM_free(vpm);
659 sk_OPENSSL_STRING_free(sksigners);
660 sk_OPENSSL_STRING_free(skkeys);
661 X509_STORE_free(store);
670 OPENSSL_free(passin);
674 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
680 tmp = BIO_new_file(signerfile, "w");
683 for (i = 0; i < sk_X509_num(signers); i++)
684 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
689 /* Minimal callback just to output policy info (if any) */
691 static int smime_cb(int ok, X509_STORE_CTX *ctx)
695 error = X509_STORE_CTX_get_error(ctx);
697 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
698 && ((error != X509_V_OK) || (ok != 2)))