3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
60 /* S/MIME utility function */
65 #include <openssl/crypto.h>
66 #include <openssl/pem.h>
67 #include <openssl/err.h>
68 #include <openssl/x509_vfy.h>
69 #include <openssl/x509v3.h>
72 #define PROG smime_main
73 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
74 static int smime_cb(int ok, X509_STORE_CTX *ctx);
78 #define SMIME_SIGNERS 0x40
79 #define SMIME_ENCRYPT (1 | SMIME_OP)
80 #define SMIME_DECRYPT (2 | SMIME_IP)
81 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
82 #define SMIME_VERIFY (4 | SMIME_IP)
83 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
84 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
86 int MAIN(int, char **);
88 int MAIN(int argc, char **argv)
94 const char *inmode = "r", *outmode = "w";
95 char *infile = NULL, *outfile = NULL;
96 char *signerfile = NULL, *recipfile = NULL;
97 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
98 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
99 const EVP_CIPHER *cipher = NULL;
101 X509_STORE *store = NULL;
102 X509 *cert = NULL, *recip = NULL, *signer = NULL;
103 EVP_PKEY *key = NULL;
104 STACK_OF(X509) *encerts = NULL, *other = NULL;
105 BIO *in = NULL, *out = NULL, *indata = NULL;
107 int flags = PKCS7_DETACHED;
108 char *to = NULL, *from = NULL, *subject = NULL;
109 char *CAfile = NULL, *CApath = NULL;
110 char *passargin = NULL, *passin = NULL;
114 const EVP_MD *sign_md = NULL;
115 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
116 int keyform = FORMAT_PEM;
119 X509_VERIFY_PARAM *vpm = NULL;
126 if (bio_err == NULL) {
127 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
128 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
131 if (!load_config(bio_err, NULL))
134 while (!badarg && *args && *args[0] == '-') {
135 if (!strcmp(*args, "-encrypt"))
136 operation = SMIME_ENCRYPT;
137 else if (!strcmp(*args, "-decrypt"))
138 operation = SMIME_DECRYPT;
139 else if (!strcmp(*args, "-sign"))
140 operation = SMIME_SIGN;
141 else if (!strcmp(*args, "-resign"))
142 operation = SMIME_RESIGN;
143 else if (!strcmp(*args, "-verify"))
144 operation = SMIME_VERIFY;
145 else if (!strcmp(*args, "-pk7out"))
146 operation = SMIME_PK7OUT;
147 #ifndef OPENSSL_NO_DES
148 else if (!strcmp(*args, "-des3"))
149 cipher = EVP_des_ede3_cbc();
150 else if (!strcmp(*args, "-des"))
151 cipher = EVP_des_cbc();
153 #ifndef OPENSSL_NO_SEED
154 else if (!strcmp(*args, "-seed"))
155 cipher = EVP_seed_cbc();
157 #ifndef OPENSSL_NO_RC2
158 else if (!strcmp(*args, "-rc2-40"))
159 cipher = EVP_rc2_40_cbc();
160 else if (!strcmp(*args, "-rc2-128"))
161 cipher = EVP_rc2_cbc();
162 else if (!strcmp(*args, "-rc2-64"))
163 cipher = EVP_rc2_64_cbc();
165 #ifndef OPENSSL_NO_AES
166 else if (!strcmp(*args, "-aes128"))
167 cipher = EVP_aes_128_cbc();
168 else if (!strcmp(*args, "-aes192"))
169 cipher = EVP_aes_192_cbc();
170 else if (!strcmp(*args, "-aes256"))
171 cipher = EVP_aes_256_cbc();
173 #ifndef OPENSSL_NO_CAMELLIA
174 else if (!strcmp(*args, "-camellia128"))
175 cipher = EVP_camellia_128_cbc();
176 else if (!strcmp(*args, "-camellia192"))
177 cipher = EVP_camellia_192_cbc();
178 else if (!strcmp(*args, "-camellia256"))
179 cipher = EVP_camellia_256_cbc();
181 else if (!strcmp(*args, "-text"))
183 else if (!strcmp(*args, "-nointern"))
184 flags |= PKCS7_NOINTERN;
185 else if (!strcmp(*args, "-noverify"))
186 flags |= PKCS7_NOVERIFY;
187 else if (!strcmp(*args, "-nochain"))
188 flags |= PKCS7_NOCHAIN;
189 else if (!strcmp(*args, "-nocerts"))
190 flags |= PKCS7_NOCERTS;
191 else if (!strcmp(*args, "-noattr"))
192 flags |= PKCS7_NOATTR;
193 else if (!strcmp(*args, "-nodetach"))
194 flags &= ~PKCS7_DETACHED;
195 else if (!strcmp(*args, "-nosmimecap"))
196 flags |= PKCS7_NOSMIMECAP;
197 else if (!strcmp(*args, "-binary"))
198 flags |= PKCS7_BINARY;
199 else if (!strcmp(*args, "-nosigs"))
200 flags |= PKCS7_NOSIGS;
201 else if (!strcmp(*args, "-stream"))
203 else if (!strcmp(*args, "-indef"))
205 else if (!strcmp(*args, "-noindef"))
207 else if (!strcmp(*args, "-nooldmime"))
208 flags |= PKCS7_NOOLDMIMETYPE;
209 else if (!strcmp(*args, "-crlfeol"))
210 flags |= PKCS7_CRLFEOL;
211 else if (!strcmp(*args, "-rand")) {
218 #ifndef OPENSSL_NO_ENGINE
219 else if (!strcmp(*args, "-engine")) {
225 else if (!strcmp(*args, "-passin")) {
229 } else if (!strcmp(*args, "-to")) {
233 } else if (!strcmp(*args, "-from")) {
237 } else if (!strcmp(*args, "-subject")) {
241 } else if (!strcmp(*args, "-signer")) {
244 /* If previous -signer argument add signer to list */
248 sksigners = sk_OPENSSL_STRING_new_null();
249 sk_OPENSSL_STRING_push(sksigners, signerfile);
251 keyfile = signerfile;
253 skkeys = sk_OPENSSL_STRING_new_null();
254 sk_OPENSSL_STRING_push(skkeys, keyfile);
257 signerfile = *++args;
258 } else if (!strcmp(*args, "-recip")) {
262 } else if (!strcmp(*args, "-md")) {
265 sign_md = EVP_get_digestbyname(*++args);
266 if (sign_md == NULL) {
267 BIO_printf(bio_err, "Unknown digest %s\n", *args);
270 } else if (!strcmp(*args, "-inkey")) {
273 /* If previous -inkey arument add signer to list */
276 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
280 sksigners = sk_OPENSSL_STRING_new_null();
281 sk_OPENSSL_STRING_push(sksigners, signerfile);
284 skkeys = sk_OPENSSL_STRING_new_null();
285 sk_OPENSSL_STRING_push(skkeys, keyfile);
288 } else if (!strcmp(*args, "-keyform")) {
291 keyform = str2fmt(*++args);
292 } else if (!strcmp(*args, "-certfile")) {
296 } else if (!strcmp(*args, "-CAfile")) {
300 } else if (!strcmp(*args, "-CApath")) {
304 } else if (!strcmp(*args, "-in")) {
308 } else if (!strcmp(*args, "-inform")) {
311 informat = str2fmt(*++args);
312 } else if (!strcmp(*args, "-outform")) {
315 outformat = str2fmt(*++args);
316 } else if (!strcmp(*args, "-out")) {
320 } else if (!strcmp(*args, "-content")) {
324 } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
326 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
331 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
332 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
336 if (operation & SMIME_SIGNERS) {
337 /* Check to see if any final signer needs to be appended */
338 if (keyfile && !signerfile) {
339 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
344 sksigners = sk_OPENSSL_STRING_new_null();
345 sk_OPENSSL_STRING_push(sksigners, signerfile);
347 skkeys = sk_OPENSSL_STRING_new_null();
349 keyfile = signerfile;
350 sk_OPENSSL_STRING_push(skkeys, keyfile);
353 BIO_printf(bio_err, "No signer certificate specified\n");
359 } else if (operation == SMIME_DECRYPT) {
360 if (!recipfile && !keyfile) {
362 "No recipient certificate or key specified\n");
365 } else if (operation == SMIME_ENCRYPT) {
367 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
371 } else if (!operation)
376 BIO_printf(bio_err, "Usage smime [options] cert.pem ...\n");
377 BIO_printf(bio_err, "where options are\n");
378 BIO_printf(bio_err, "-encrypt encrypt message\n");
379 BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
380 BIO_printf(bio_err, "-sign sign message\n");
381 BIO_printf(bio_err, "-verify verify signed message\n");
382 BIO_printf(bio_err, "-pk7out output PKCS#7 structure\n");
383 #ifndef OPENSSL_NO_DES
384 BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
385 BIO_printf(bio_err, "-des encrypt with DES\n");
387 #ifndef OPENSSL_NO_SEED
388 BIO_printf(bio_err, "-seed encrypt with SEED\n");
390 #ifndef OPENSSL_NO_RC2
391 BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
392 BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
393 BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
395 #ifndef OPENSSL_NO_AES
396 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
398 " encrypt PEM output with cbc aes\n");
400 #ifndef OPENSSL_NO_CAMELLIA
401 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
403 " encrypt PEM output with cbc camellia\n");
406 "-nointern don't search certificates in message for signer\n");
408 "-nosigs don't verify message signature\n");
410 "-noverify don't verify signers certificate\n");
412 "-nocerts don't include signers certificate when signing\n");
413 BIO_printf(bio_err, "-nodetach use opaque signing\n");
415 "-noattr don't include any signed attributes\n");
417 "-binary don't translate message to text\n");
418 BIO_printf(bio_err, "-certfile file other certificates file\n");
419 BIO_printf(bio_err, "-signer file signer certificate file\n");
421 "-recip file recipient certificate file for decryption\n");
422 BIO_printf(bio_err, "-in file input file\n");
424 "-inform arg input format SMIME (default), PEM or DER\n");
426 "-inkey file input private key (if not signer or recipient)\n");
428 "-keyform arg input private key format (PEM or ENGINE)\n");
429 BIO_printf(bio_err, "-out file output file\n");
431 "-outform arg output format SMIME (default), PEM or DER\n");
433 "-content file supply or override content for detached signature\n");
434 BIO_printf(bio_err, "-to addr to address\n");
435 BIO_printf(bio_err, "-from ad from address\n");
436 BIO_printf(bio_err, "-subject s subject\n");
438 "-text include or delete text MIME headers\n");
440 "-CApath dir trusted certificates directory\n");
441 BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
443 "-no_alt_chains only ever use the first certificate chain found\n");
445 "-crl_check check revocation status of signer's certificate using CRLs\n");
447 "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
448 #ifndef OPENSSL_NO_ENGINE
450 "-engine e use engine e, possibly a hardware device.\n");
452 BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
453 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
454 LIST_SEPARATOR_CHAR);
456 " load the file (or the files in the directory) into\n");
457 BIO_printf(bio_err, " the random number generator\n");
459 "cert.pem recipient certificate(s) for encryption\n");
462 e = setup_engine(bio_err, engine, 0);
464 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
465 BIO_printf(bio_err, "Error getting password\n");
470 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
472 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
473 app_RAND_load_files(inrand));
478 if (!(operation & SMIME_SIGNERS))
479 flags &= ~PKCS7_DETACHED;
481 if (operation & SMIME_OP) {
482 if (outformat == FORMAT_ASN1)
485 if (flags & PKCS7_BINARY)
489 if (operation & SMIME_IP) {
490 if (informat == FORMAT_ASN1)
493 if (flags & PKCS7_BINARY)
497 if (operation == SMIME_ENCRYPT) {
499 #ifndef OPENSSL_NO_DES
500 cipher = EVP_des_ede3_cbc();
502 BIO_printf(bio_err, "No cipher selected\n");
506 encerts = sk_X509_new_null();
508 if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
509 NULL, e, "recipient certificate file"))) {
510 #if 0 /* An appropriate message is already printed */
512 "Can't read recipient certificate file %s\n",
517 sk_X509_push(encerts, cert);
524 if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
525 e, "certificate file"))) {
526 ERR_print_errors(bio_err);
531 if (recipfile && (operation == SMIME_DECRYPT)) {
532 if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
533 e, "recipient certificate file"))) {
534 ERR_print_errors(bio_err);
539 if (operation == SMIME_DECRYPT) {
542 } else if (operation == SMIME_SIGN) {
544 keyfile = signerfile;
549 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
556 if (!(in = BIO_new_file(infile, inmode))) {
557 BIO_printf(bio_err, "Can't open input file %s\n", infile);
561 in = BIO_new_fp(stdin, BIO_NOCLOSE);
563 if (operation & SMIME_IP) {
564 if (informat == FORMAT_SMIME)
565 p7 = SMIME_read_PKCS7(in, &indata);
566 else if (informat == FORMAT_PEM)
567 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
568 else if (informat == FORMAT_ASN1)
569 p7 = d2i_PKCS7_bio(in, NULL);
571 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
576 BIO_printf(bio_err, "Error reading S/MIME message\n");
581 if (!(indata = BIO_new_file(contfile, "rb"))) {
582 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
589 if (!(out = BIO_new_file(outfile, outmode))) {
590 BIO_printf(bio_err, "Can't open output file %s\n", outfile);
594 out = BIO_new_fp(stdout, BIO_NOCLOSE);
595 #ifdef OPENSSL_SYS_VMS
597 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
598 out = BIO_push(tmpbio, out);
603 if (operation == SMIME_VERIFY) {
604 if (!(store = setup_verify(bio_err, CAfile, CApath)))
606 X509_STORE_set_verify_cb(store, smime_cb);
608 X509_STORE_set1_param(store, vpm);
613 if (operation == SMIME_ENCRYPT) {
615 flags |= PKCS7_STREAM;
616 p7 = PKCS7_encrypt(encerts, in, cipher, flags);
617 } else if (operation & SMIME_SIGNERS) {
620 * If detached data content we only enable streaming if S/MIME output
623 if (operation == SMIME_SIGN) {
624 if (flags & PKCS7_DETACHED) {
625 if (outformat == FORMAT_SMIME)
626 flags |= PKCS7_STREAM;
628 flags |= PKCS7_STREAM;
629 flags |= PKCS7_PARTIAL;
630 p7 = PKCS7_sign(NULL, NULL, other, in, flags);
633 if (flags & PKCS7_NOCERTS) {
634 for (i = 0; i < sk_X509_num(other); i++) {
635 X509 *x = sk_X509_value(other, i);
636 PKCS7_add_certificate(p7, x);
640 flags |= PKCS7_REUSE_DIGEST;
641 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
642 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
643 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
644 signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
645 e, "signer certificate");
648 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
652 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
659 /* If not streaming or resigning finalize structure */
660 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
661 if (!PKCS7_final(p7, in, flags))
667 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
672 if (operation == SMIME_DECRYPT) {
673 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
674 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
677 } else if (operation == SMIME_VERIFY) {
678 STACK_OF(X509) *signers;
679 if (PKCS7_verify(p7, other, store, indata, out, flags))
680 BIO_printf(bio_err, "Verification successful\n");
682 BIO_printf(bio_err, "Verification failure\n");
685 signers = PKCS7_get0_signers(p7, other, flags);
686 if (!save_certs(signerfile, signers)) {
687 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
691 sk_X509_free(signers);
692 } else if (operation == SMIME_PK7OUT)
693 PEM_write_bio_PKCS7(out, p7);
696 BIO_printf(out, "To: %s\n", to);
698 BIO_printf(out, "From: %s\n", from);
700 BIO_printf(out, "Subject: %s\n", subject);
701 if (outformat == FORMAT_SMIME) {
702 if (operation == SMIME_RESIGN)
703 SMIME_write_PKCS7(out, p7, indata, flags);
705 SMIME_write_PKCS7(out, p7, in, flags);
706 } else if (outformat == FORMAT_PEM)
707 PEM_write_bio_PKCS7_stream(out, p7, in, flags);
708 else if (outformat == FORMAT_ASN1)
709 i2d_PKCS7_bio_stream(out, p7, in, flags);
711 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
718 app_RAND_write_file(NULL, bio_err);
720 ERR_print_errors(bio_err);
721 sk_X509_pop_free(encerts, X509_free);
722 sk_X509_pop_free(other, X509_free);
724 X509_VERIFY_PARAM_free(vpm);
726 sk_OPENSSL_STRING_free(sksigners);
728 sk_OPENSSL_STRING_free(skkeys);
729 X509_STORE_free(store);
740 OPENSSL_free(passin);
744 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
750 tmp = BIO_new_file(signerfile, "w");
753 for (i = 0; i < sk_X509_num(signers); i++)
754 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
759 /* Minimal callback just to output policy info (if any) */
761 static int smime_cb(int ok, X509_STORE_CTX *ctx)
765 error = X509_STORE_CTX_get_error(ctx);
767 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
768 && ((error != X509_V_OK) || (ok != 2)))
771 policies_print(NULL, ctx);