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);
77 #define SMIME_ENCRYPT (1 | SMIME_OP)
78 #define SMIME_DECRYPT 2
79 #define SMIME_SIGN (3 | SMIME_OP)
80 #define SMIME_VERIFY 4
81 #define SMIME_PK7OUT 5
83 int MAIN(int, char **);
85 int MAIN(int argc, char **argv)
91 const char *inmode = "r", *outmode = "w";
92 char *infile = NULL, *outfile = NULL;
93 char *signerfile = NULL, *recipfile = NULL;
94 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
95 const EVP_CIPHER *cipher = NULL;
97 X509_STORE *store = NULL;
98 X509 *cert = NULL, *recip = NULL, *signer = NULL;
100 STACK_OF(X509) *encerts = NULL, *other = NULL;
101 BIO *in = NULL, *out = NULL, *indata = NULL;
103 int flags = PKCS7_DETACHED;
104 char *to = NULL, *from = NULL, *subject = NULL;
105 char *CAfile = NULL, *CApath = NULL;
106 char *passargin = NULL, *passin = NULL;
109 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
110 int keyform = FORMAT_PEM;
111 #ifndef OPENSSL_NO_ENGINE
115 X509_VERIFY_PARAM *vpm = NULL;
122 if (bio_err == NULL) {
123 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
124 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
127 if (!load_config(bio_err, NULL))
130 while (!badarg && *args && *args[0] == '-') {
131 if (!strcmp(*args, "-encrypt"))
132 operation = SMIME_ENCRYPT;
133 else if (!strcmp(*args, "-decrypt"))
134 operation = SMIME_DECRYPT;
135 else if (!strcmp(*args, "-sign"))
136 operation = SMIME_SIGN;
137 else if (!strcmp(*args, "-verify"))
138 operation = SMIME_VERIFY;
139 else if (!strcmp(*args, "-pk7out"))
140 operation = SMIME_PK7OUT;
141 #ifndef OPENSSL_NO_DES
142 else if (!strcmp(*args, "-des3"))
143 cipher = EVP_des_ede3_cbc();
144 else if (!strcmp(*args, "-des"))
145 cipher = EVP_des_cbc();
147 #ifndef OPENSSL_NO_SEED
148 else if (!strcmp(*args, "-seed"))
149 cipher = EVP_seed_cbc();
151 #ifndef OPENSSL_NO_RC2
152 else if (!strcmp(*args, "-rc2-40"))
153 cipher = EVP_rc2_40_cbc();
154 else if (!strcmp(*args, "-rc2-128"))
155 cipher = EVP_rc2_cbc();
156 else if (!strcmp(*args, "-rc2-64"))
157 cipher = EVP_rc2_64_cbc();
159 #ifndef OPENSSL_NO_AES
160 else if (!strcmp(*args, "-aes128"))
161 cipher = EVP_aes_128_cbc();
162 else if (!strcmp(*args, "-aes192"))
163 cipher = EVP_aes_192_cbc();
164 else if (!strcmp(*args, "-aes256"))
165 cipher = EVP_aes_256_cbc();
167 #ifndef OPENSSL_NO_CAMELLIA
168 else if (!strcmp(*args, "-camellia128"))
169 cipher = EVP_camellia_128_cbc();
170 else if (!strcmp(*args, "-camellia192"))
171 cipher = EVP_camellia_192_cbc();
172 else if (!strcmp(*args, "-camellia256"))
173 cipher = EVP_camellia_256_cbc();
175 else if (!strcmp(*args, "-text"))
177 else if (!strcmp(*args, "-nointern"))
178 flags |= PKCS7_NOINTERN;
179 else if (!strcmp(*args, "-noverify"))
180 flags |= PKCS7_NOVERIFY;
181 else if (!strcmp(*args, "-nochain"))
182 flags |= PKCS7_NOCHAIN;
183 else if (!strcmp(*args, "-nocerts"))
184 flags |= PKCS7_NOCERTS;
185 else if (!strcmp(*args, "-noattr"))
186 flags |= PKCS7_NOATTR;
187 else if (!strcmp(*args, "-nodetach"))
188 flags &= ~PKCS7_DETACHED;
189 else if (!strcmp(*args, "-nosmimecap"))
190 flags |= PKCS7_NOSMIMECAP;
191 else if (!strcmp(*args, "-binary"))
192 flags |= PKCS7_BINARY;
193 else if (!strcmp(*args, "-nosigs"))
194 flags |= PKCS7_NOSIGS;
195 else if (!strcmp(*args, "-nooldmime"))
196 flags |= PKCS7_NOOLDMIMETYPE;
197 else if (!strcmp(*args, "-crlfeol"))
198 flags |= PKCS7_CRLFEOL;
199 else if (!strcmp(*args, "-rand")) {
207 #ifndef OPENSSL_NO_ENGINE
208 else if (!strcmp(*args, "-engine")) {
216 else if (!strcmp(*args, "-passin")) {
222 } else if (!strcmp(*args, "-to")) {
228 } else if (!strcmp(*args, "-from")) {
234 } else if (!strcmp(*args, "-subject")) {
240 } else if (!strcmp(*args, "-signer")) {
246 } else if (!strcmp(*args, "-recip")) {
252 } else if (!strcmp(*args, "-inkey")) {
258 } else if (!strcmp(*args, "-keyform")) {
261 keyform = str2fmt(*args);
264 } else if (!strcmp(*args, "-certfile")) {
270 } else if (!strcmp(*args, "-CAfile")) {
276 } else if (!strcmp(*args, "-CApath")) {
282 } else if (!strcmp(*args, "-in")) {
288 } else if (!strcmp(*args, "-inform")) {
291 informat = str2fmt(*args);
294 } else if (!strcmp(*args, "-outform")) {
297 outformat = str2fmt(*args);
300 } else if (!strcmp(*args, "-out")) {
306 } else if (!strcmp(*args, "-content")) {
312 } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
319 if (operation == SMIME_SIGN) {
321 BIO_printf(bio_err, "No signer certificate specified\n");
325 } else if (operation == SMIME_DECRYPT) {
326 if (!recipfile && !keyfile) {
328 "No recipient certificate or key specified\n");
331 } else if (operation == SMIME_ENCRYPT) {
333 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
337 } else if (!operation)
341 BIO_printf(bio_err, "Usage smime [options] cert.pem ...\n");
342 BIO_printf(bio_err, "where options are\n");
343 BIO_printf(bio_err, "-encrypt encrypt message\n");
344 BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
345 BIO_printf(bio_err, "-sign sign message\n");
346 BIO_printf(bio_err, "-verify verify signed message\n");
347 BIO_printf(bio_err, "-pk7out output PKCS#7 structure\n");
348 #ifndef OPENSSL_NO_DES
349 BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
350 BIO_printf(bio_err, "-des encrypt with DES\n");
352 #ifndef OPENSSL_NO_SEED
353 BIO_printf(bio_err, "-seed encrypt with SEED\n");
355 #ifndef OPENSSL_NO_RC2
356 BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
357 BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
358 BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
360 #ifndef OPENSSL_NO_AES
361 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
363 " encrypt PEM output with cbc aes\n");
365 #ifndef OPENSSL_NO_CAMELLIA
366 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
368 " encrypt PEM output with cbc camellia\n");
371 "-nointern don't search certificates in message for signer\n");
373 "-nosigs don't verify message signature\n");
375 "-noverify don't verify signers certificate\n");
377 "-nocerts don't include signers certificate when signing\n");
378 BIO_printf(bio_err, "-nodetach use opaque signing\n");
380 "-noattr don't include any signed attributes\n");
382 "-binary don't translate message to text\n");
383 BIO_printf(bio_err, "-certfile file other certificates file\n");
384 BIO_printf(bio_err, "-signer file signer certificate file\n");
386 "-recip file recipient certificate file for decryption\n");
387 BIO_printf(bio_err, "-in file input file\n");
389 "-inform arg input format SMIME (default), PEM or DER\n");
391 "-inkey file input private key (if not signer or recipient)\n");
393 "-keyform arg input private key format (PEM or ENGINE)\n");
394 BIO_printf(bio_err, "-out file output file\n");
396 "-outform arg output format SMIME (default), PEM or DER\n");
398 "-content file supply or override content for detached signature\n");
399 BIO_printf(bio_err, "-to addr to address\n");
400 BIO_printf(bio_err, "-from ad from address\n");
401 BIO_printf(bio_err, "-subject s subject\n");
403 "-text include or delete text MIME headers\n");
405 "-CApath dir trusted certificates directory\n");
406 BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
408 "-crl_check check revocation status of signer's certificate using CRLs\n");
410 "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
411 #ifndef OPENSSL_NO_ENGINE
413 "-engine e use engine e, possibly a hardware device.\n");
415 BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
416 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
417 LIST_SEPARATOR_CHAR);
419 " load the file (or the files in the directory) into\n");
420 BIO_printf(bio_err, " the random number generator\n");
422 "cert.pem recipient certificate(s) for encryption\n");
425 #ifndef OPENSSL_NO_ENGINE
426 e = setup_engine(bio_err, engine, 0);
429 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
430 BIO_printf(bio_err, "Error getting password\n");
435 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
437 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
438 app_RAND_load_files(inrand));
443 if (operation != SMIME_SIGN)
444 flags &= ~PKCS7_DETACHED;
446 if (operation & SMIME_OP) {
447 if (flags & PKCS7_BINARY)
449 if (outformat == FORMAT_ASN1)
452 if (flags & PKCS7_BINARY)
454 if (informat == FORMAT_ASN1)
458 if (operation == SMIME_ENCRYPT) {
460 #ifndef OPENSSL_NO_DES
461 cipher = EVP_des_ede3_cbc();
463 BIO_printf(bio_err, "No cipher selected\n");
467 encerts = sk_X509_new_null();
469 if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
470 NULL, e, "recipient certificate file"))) {
471 #if 0 /* An appropriate message is already printed */
473 "Can't read recipient certificate file %s\n",
478 sk_X509_push(encerts, cert);
484 if (signerfile && (operation == SMIME_SIGN)) {
485 if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
486 e, "signer certificate"))) {
487 #if 0 /* An appropri message has already been
489 BIO_printf(bio_err, "Can't read signer certificate file %s\n",
497 if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
498 e, "certificate file"))) {
499 #if 0 /* An appropriate message has already been
501 BIO_printf(bio_err, "Can't read certificate file %s\n", certfile);
503 ERR_print_errors(bio_err);
508 if (recipfile && (operation == SMIME_DECRYPT)) {
509 if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
510 e, "recipient certificate file"))) {
511 #if 0 /* An appropriate message has alrady been
513 BIO_printf(bio_err, "Can't read recipient certificate file %s\n",
516 ERR_print_errors(bio_err);
521 if (operation == SMIME_DECRYPT) {
524 } else if (operation == SMIME_SIGN) {
526 keyfile = signerfile;
531 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
538 if (!(in = BIO_new_file(infile, inmode))) {
539 BIO_printf(bio_err, "Can't open input file %s\n", infile);
543 in = BIO_new_fp(stdin, BIO_NOCLOSE);
546 if (!(out = BIO_new_file(outfile, outmode))) {
547 BIO_printf(bio_err, "Can't open output file %s\n", outfile);
551 out = BIO_new_fp(stdout, BIO_NOCLOSE);
552 #ifdef OPENSSL_SYS_VMS
554 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
555 out = BIO_push(tmpbio, out);
560 if (operation == SMIME_VERIFY) {
561 if (!(store = setup_verify(bio_err, CAfile, CApath)))
563 X509_STORE_set_verify_cb_func(store, smime_cb);
565 X509_STORE_set1_param(store, vpm);
570 if (operation == SMIME_ENCRYPT)
571 p7 = PKCS7_encrypt(encerts, in, cipher, flags);
572 else if (operation == SMIME_SIGN) {
574 * If detached data and SMIME output enable partial signing.
576 if ((flags & PKCS7_DETACHED) && (outformat == FORMAT_SMIME))
577 flags |= PKCS7_STREAM;
578 p7 = PKCS7_sign(signer, key, other, in, flags);
580 if (informat == FORMAT_SMIME)
581 p7 = SMIME_read_PKCS7(in, &indata);
582 else if (informat == FORMAT_PEM)
583 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
584 else if (informat == FORMAT_ASN1)
585 p7 = d2i_PKCS7_bio(in, NULL);
587 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
592 BIO_printf(bio_err, "Error reading S/MIME message\n");
597 if (!(indata = BIO_new_file(contfile, "rb"))) {
598 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
605 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
610 if (operation == SMIME_DECRYPT) {
611 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
612 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
615 } else if (operation == SMIME_VERIFY) {
616 STACK_OF(X509) *signers;
617 if (PKCS7_verify(p7, other, store, indata, out, flags))
618 BIO_printf(bio_err, "Verification successful\n");
620 BIO_printf(bio_err, "Verification failure\n");
623 signers = PKCS7_get0_signers(p7, other, flags);
624 if (!save_certs(signerfile, signers)) {
625 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
629 sk_X509_free(signers);
630 } else if (operation == SMIME_PK7OUT)
631 PEM_write_bio_PKCS7(out, p7);
634 BIO_printf(out, "To: %s\n", to);
636 BIO_printf(out, "From: %s\n", from);
638 BIO_printf(out, "Subject: %s\n", subject);
639 if (outformat == FORMAT_SMIME)
640 SMIME_write_PKCS7(out, p7, in, flags);
641 else if (outformat == FORMAT_PEM)
642 PEM_write_bio_PKCS7(out, p7);
643 else if (outformat == FORMAT_ASN1)
644 i2d_PKCS7_bio(out, p7);
646 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
653 app_RAND_write_file(NULL, bio_err);
655 ERR_print_errors(bio_err);
656 sk_X509_pop_free(encerts, X509_free);
657 sk_X509_pop_free(other, X509_free);
659 X509_VERIFY_PARAM_free(vpm);
660 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)))
701 policies_print(NULL, ctx);