2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* S/MIME utility function */
16 #include <openssl/crypto.h>
17 #include <openssl/pem.h>
18 #include <openssl/err.h>
19 #include <openssl/x509_vfy.h>
20 #include <openssl/x509v3.h>
22 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
);
23 static int smime_cb(int ok
, X509_STORE_CTX
*ctx
);
27 #define SMIME_SIGNERS 0x40
28 #define SMIME_ENCRYPT (1 | SMIME_OP)
29 #define SMIME_DECRYPT (2 | SMIME_IP)
30 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
31 #define SMIME_VERIFY (4 | SMIME_IP)
32 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
33 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
35 typedef enum OPTION_choice
{
36 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
37 OPT_ENCRYPT
, OPT_DECRYPT
, OPT_SIGN
, OPT_RESIGN
, OPT_VERIFY
,
38 OPT_PK7OUT
, OPT_TEXT
, OPT_NOINTERN
, OPT_NOVERIFY
, OPT_NOCHAIN
,
39 OPT_NOCERTS
, OPT_NOATTR
, OPT_NODETACH
, OPT_NOSMIMECAP
,
40 OPT_BINARY
, OPT_NOSIGS
, OPT_STREAM
, OPT_INDEF
, OPT_NOINDEF
,
41 OPT_CRLFEOL
, OPT_ENGINE
, OPT_PASSIN
,
42 OPT_TO
, OPT_FROM
, OPT_SUBJECT
, OPT_SIGNER
, OPT_RECIP
, OPT_MD
,
43 OPT_CIPHER
, OPT_INKEY
, OPT_KEYFORM
, OPT_CERTFILE
, OPT_CAFILE
,
44 OPT_CAPATH
, OPT_CASTORE
, OPT_NOCAFILE
, OPT_NOCAPATH
, OPT_NOCASTORE
,
45 OPT_R_ENUM
, OPT_PROV_ENUM
,
47 OPT_IN
, OPT_INFORM
, OPT_OUT
,
48 OPT_OUTFORM
, OPT_CONTENT
51 const OPTIONS smime_options
[] = {
52 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [cert...]\n"},
54 OPT_SECTION("General"),
55 {"help", OPT_HELP
, '-', "Display this summary"},
56 {"in", OPT_IN
, '<', "Input file"},
57 {"inform", OPT_INFORM
, 'c', "Input format SMIME (default), PEM or DER"},
58 {"out", OPT_OUT
, '>', "Output file"},
59 {"outform", OPT_OUTFORM
, 'c',
60 "Output format SMIME (default), PEM or DER"},
61 {"inkey", OPT_INKEY
, 's',
62 "Input private key (if not signer or recipient)"},
63 {"keyform", OPT_KEYFORM
, 'f', "Input private key format (PEM or ENGINE)"},
64 #ifndef OPENSSL_NO_ENGINE
65 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
67 {"stream", OPT_STREAM
, '-', "Enable CMS streaming" },
68 {"indef", OPT_INDEF
, '-', "Same as -stream" },
69 {"noindef", OPT_NOINDEF
, '-', "Disable CMS streaming"},
71 OPT_SECTION("Action"),
72 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt message"},
73 {"decrypt", OPT_DECRYPT
, '-', "Decrypt encrypted message"},
74 {"sign", OPT_SIGN
, '-', "Sign message"},
75 {"resign", OPT_RESIGN
, '-', "Resign a signed message"},
76 {"verify", OPT_VERIFY
, '-', "Verify signed message"},
78 OPT_SECTION("Signing/Encryption"),
79 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
80 {"md", OPT_MD
, 's', "Digest algorithm to use when signing or resigning"},
81 {"", OPT_CIPHER
, '-', "Any supported cipher"},
82 {"pk7out", OPT_PK7OUT
, '-', "Output PKCS#7 structure"},
83 {"nointern", OPT_NOINTERN
, '-',
84 "Don't search certificates in message for signer"},
85 {"nodetach", OPT_NODETACH
, '-', "Use opaque signing"},
86 {"noattr", OPT_NOATTR
, '-', "Don't include any signed attributes"},
87 {"binary", OPT_BINARY
, '-', "Don't translate message to text"},
88 {"signer", OPT_SIGNER
, 's', "Signer certificate file"},
89 {"content", OPT_CONTENT
, '<',
90 "Supply or override content for detached signature"},
91 {"nocerts", OPT_NOCERTS
, '-',
92 "Don't include signers certificate when signing"},
94 OPT_SECTION("Verification/Decryption"),
95 {"nosigs", OPT_NOSIGS
, '-', "Don't verify message signature"},
96 {"noverify", OPT_NOVERIFY
, '-', "Don't verify signers certificate"},
98 {"certfile", OPT_CERTFILE
, '<', "Other certificates file"},
99 {"recip", OPT_RECIP
, '<', "Recipient certificate file for decryption"},
101 OPT_SECTION("Email"),
102 {"to", OPT_TO
, 's', "To address"},
103 {"from", OPT_FROM
, 's', "From address"},
104 {"subject", OPT_SUBJECT
, 's', "Subject"},
105 {"text", OPT_TEXT
, '-', "Include or delete text MIME headers"},
106 {"nosmimecap", OPT_NOSMIMECAP
, '-', "Omit the SMIMECapabilities attribute"},
108 OPT_SECTION("Certificate chain"),
109 {"CApath", OPT_CAPATH
, '/', "Trusted certificates directory"},
110 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
111 {"CAstore", OPT_CASTORE
, ':', "Trusted certificates store URI"},
112 {"no-CAfile", OPT_NOCAFILE
, '-',
113 "Do not load the default certificates file"},
114 {"no-CApath", OPT_NOCAPATH
, '-',
115 "Do not load certificates from the default certificates directory"},
116 {"no-CAstore", OPT_NOCASTORE
, '-',
117 "Do not load certificates from the default certificates store"},
118 {"nochain", OPT_NOCHAIN
, '-',
119 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
120 {"crlfeol", OPT_CRLFEOL
, '-', "Use CRLF as EOL termination instead of CR only"},
127 {"cert", 0, 0, "Recipient certs, used when encrypting"},
131 int smime_main(int argc
, char **argv
)
133 BIO
*in
= NULL
, *out
= NULL
, *indata
= NULL
;
134 EVP_PKEY
*key
= NULL
;
136 STACK_OF(OPENSSL_STRING
) *sksigners
= NULL
, *skkeys
= NULL
;
137 STACK_OF(X509
) *encerts
= NULL
, *other
= NULL
;
138 X509
*cert
= NULL
, *recip
= NULL
, *signer
= NULL
;
139 X509_STORE
*store
= NULL
;
140 X509_VERIFY_PARAM
*vpm
= NULL
;
141 const EVP_CIPHER
*cipher
= NULL
;
142 const EVP_MD
*sign_md
= NULL
;
143 const char *CAfile
= NULL
, *CApath
= NULL
, *CAstore
= NULL
, *prog
= NULL
;
144 char *certfile
= NULL
, *keyfile
= NULL
, *contfile
= NULL
;
145 char *infile
= NULL
, *outfile
= NULL
, *signerfile
= NULL
, *recipfile
= NULL
;
146 char *passinarg
= NULL
, *passin
= NULL
, *to
= NULL
, *from
= NULL
, *subject
= NULL
;
148 int noCApath
= 0, noCAfile
= 0, noCAstore
= 0;
149 int flags
= PKCS7_DETACHED
, operation
= 0, ret
= 0, indef
= 0;
150 int informat
= FORMAT_SMIME
, outformat
= FORMAT_SMIME
, keyform
=
152 int vpmtouched
= 0, rv
= 0;
154 const char *mime_eol
= "\n";
156 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
159 prog
= opt_init(argc
, argv
, smime_options
);
160 while ((o
= opt_next()) != OPT_EOF
) {
165 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
168 opt_help(smime_options
);
172 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &informat
))
179 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &outformat
))
186 operation
= SMIME_ENCRYPT
;
189 operation
= SMIME_DECRYPT
;
192 operation
= SMIME_SIGN
;
195 operation
= SMIME_RESIGN
;
198 operation
= SMIME_VERIFY
;
201 operation
= SMIME_PK7OUT
;
207 flags
|= PKCS7_NOINTERN
;
210 flags
|= PKCS7_NOVERIFY
;
213 flags
|= PKCS7_NOCHAIN
;
216 flags
|= PKCS7_NOCERTS
;
219 flags
|= PKCS7_NOATTR
;
222 flags
&= ~PKCS7_DETACHED
;
225 flags
|= PKCS7_NOSMIMECAP
;
228 flags
|= PKCS7_BINARY
;
231 flags
|= PKCS7_NOSIGS
;
241 flags
|= PKCS7_CRLFEOL
;
249 if (!opt_provider(o
))
253 e
= setup_engine(opt_arg(), 0);
256 passinarg
= opt_arg();
268 /* If previous -signer argument add signer to list */
269 if (signerfile
!= NULL
) {
270 if (sksigners
== NULL
271 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
273 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
275 keyfile
= signerfile
;
277 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
279 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
282 signerfile
= opt_arg();
285 recipfile
= opt_arg();
288 if (!opt_md(opt_arg(), &sign_md
))
292 if (!opt_cipher(opt_unknown(), &cipher
))
296 /* If previous -inkey argument add signer to list */
297 if (keyfile
!= NULL
) {
298 if (signerfile
== NULL
) {
300 "%s: Must have -signer before -inkey\n", prog
);
303 if (sksigners
== NULL
304 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
306 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
309 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
311 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
316 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
320 certfile
= opt_arg();
341 contfile
= opt_arg();
344 if (!opt_verify(o
, vpm
))
350 argc
= opt_num_rest();
353 if (!(operation
& SMIME_SIGNERS
) && (skkeys
!= NULL
|| sksigners
!= NULL
)) {
354 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
358 if (operation
& SMIME_SIGNERS
) {
359 /* Check to see if any final signer needs to be appended */
360 if (keyfile
&& !signerfile
) {
361 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
364 if (signerfile
!= NULL
) {
365 if (sksigners
== NULL
366 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
368 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
369 if (!skkeys
&& (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
372 keyfile
= signerfile
;
373 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
375 if (sksigners
== NULL
) {
376 BIO_printf(bio_err
, "No signer certificate specified\n");
381 } else if (operation
== SMIME_DECRYPT
) {
382 if (recipfile
== NULL
&& keyfile
== NULL
) {
384 "No recipient certificate or key specified\n");
387 } else if (operation
== SMIME_ENCRYPT
) {
389 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
392 } else if (!operation
) {
396 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
397 BIO_printf(bio_err
, "Error getting password\n");
403 if (!(operation
& SMIME_SIGNERS
))
404 flags
&= ~PKCS7_DETACHED
;
406 if (!(operation
& SMIME_OP
)) {
407 if (flags
& PKCS7_BINARY
)
408 outformat
= FORMAT_BINARY
;
411 if (!(operation
& SMIME_IP
)) {
412 if (flags
& PKCS7_BINARY
)
413 informat
= FORMAT_BINARY
;
416 if (operation
== SMIME_ENCRYPT
) {
417 if (cipher
== NULL
) {
418 #ifndef OPENSSL_NO_DES
419 cipher
= EVP_des_ede3_cbc();
421 BIO_printf(bio_err
, "No cipher selected\n");
425 encerts
= sk_X509_new_null();
428 while (*argv
!= NULL
) {
429 cert
= load_cert(*argv
, FORMAT_PEM
,
430 "recipient certificate file");
433 sk_X509_push(encerts
, cert
);
439 if (certfile
!= NULL
) {
440 if (!load_certs(certfile
, &other
, FORMAT_PEM
, NULL
,
441 "certificate file")) {
442 ERR_print_errors(bio_err
);
447 if (recipfile
!= NULL
&& (operation
== SMIME_DECRYPT
)) {
448 if ((recip
= load_cert(recipfile
, FORMAT_PEM
,
449 "recipient certificate file")) == NULL
) {
450 ERR_print_errors(bio_err
);
455 if (operation
== SMIME_DECRYPT
) {
458 } else if (operation
== SMIME_SIGN
) {
460 keyfile
= signerfile
;
465 if (keyfile
!= NULL
) {
466 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
471 in
= bio_open_default(infile
, 'r', informat
);
475 if (operation
& SMIME_IP
) {
476 if (informat
== FORMAT_SMIME
) {
477 p7
= SMIME_read_PKCS7(in
, &indata
);
478 } else if (informat
== FORMAT_PEM
) {
479 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
480 } else if (informat
== FORMAT_ASN1
) {
481 p7
= d2i_PKCS7_bio(in
, NULL
);
483 BIO_printf(bio_err
, "Bad input format for PKCS#7 file\n");
488 BIO_printf(bio_err
, "Error reading S/MIME message\n");
491 if (contfile
!= NULL
) {
493 if ((indata
= BIO_new_file(contfile
, "rb")) == NULL
) {
494 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
500 out
= bio_open_default(outfile
, 'w', outformat
);
504 if (operation
== SMIME_VERIFY
) {
505 if ((store
= setup_verify(CAfile
, noCAfile
, CApath
, noCApath
,
506 CAstore
, noCAstore
)) == NULL
)
508 X509_STORE_set_verify_cb(store
, smime_cb
);
510 X509_STORE_set1_param(store
, vpm
);
515 if (operation
== SMIME_ENCRYPT
) {
517 flags
|= PKCS7_STREAM
;
518 p7
= PKCS7_encrypt(encerts
, in
, cipher
, flags
);
519 } else if (operation
& SMIME_SIGNERS
) {
522 * If detached data content we only enable streaming if S/MIME output
525 if (operation
== SMIME_SIGN
) {
526 if (flags
& PKCS7_DETACHED
) {
527 if (outformat
== FORMAT_SMIME
)
528 flags
|= PKCS7_STREAM
;
530 flags
|= PKCS7_STREAM
;
532 flags
|= PKCS7_PARTIAL
;
533 p7
= PKCS7_sign(NULL
, NULL
, other
, in
, flags
);
536 if (flags
& PKCS7_NOCERTS
) {
537 for (i
= 0; i
< sk_X509_num(other
); i
++) {
538 X509
*x
= sk_X509_value(other
, i
);
539 PKCS7_add_certificate(p7
, x
);
543 flags
|= PKCS7_REUSE_DIGEST
;
545 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++) {
546 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
547 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
548 signer
= load_cert(signerfile
, FORMAT_PEM
,
549 "signer certificate");
552 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
555 if (!PKCS7_sign_add_signer(p7
, signer
, key
, sign_md
, flags
))
562 /* If not streaming or resigning finalize structure */
563 if ((operation
== SMIME_SIGN
) && !(flags
& PKCS7_STREAM
)) {
564 if (!PKCS7_final(p7
, in
, flags
))
570 BIO_printf(bio_err
, "Error creating PKCS#7 structure\n");
575 if (operation
== SMIME_DECRYPT
) {
576 if (!PKCS7_decrypt(p7
, key
, recip
, out
, flags
)) {
577 BIO_printf(bio_err
, "Error decrypting PKCS#7 structure\n");
580 } else if (operation
== SMIME_VERIFY
) {
581 STACK_OF(X509
) *signers
;
582 if (PKCS7_verify(p7
, other
, store
, indata
, out
, flags
))
583 BIO_printf(bio_err
, "Verification successful\n");
585 BIO_printf(bio_err
, "Verification failure\n");
588 signers
= PKCS7_get0_signers(p7
, other
, flags
);
589 if (!save_certs(signerfile
, signers
)) {
590 BIO_printf(bio_err
, "Error writing signers to %s\n", signerfile
);
594 sk_X509_free(signers
);
595 } else if (operation
== SMIME_PK7OUT
) {
596 PEM_write_bio_PKCS7(out
, p7
);
599 BIO_printf(out
, "To: %s%s", to
, mime_eol
);
601 BIO_printf(out
, "From: %s%s", from
, mime_eol
);
603 BIO_printf(out
, "Subject: %s%s", subject
, mime_eol
);
604 if (outformat
== FORMAT_SMIME
) {
605 if (operation
== SMIME_RESIGN
)
606 rv
= SMIME_write_PKCS7(out
, p7
, indata
, flags
);
608 rv
= SMIME_write_PKCS7(out
, p7
, in
, flags
);
609 } else if (outformat
== FORMAT_PEM
) {
610 rv
= PEM_write_bio_PKCS7_stream(out
, p7
, in
, flags
);
611 } else if (outformat
== FORMAT_ASN1
) {
612 rv
= i2d_PKCS7_bio_stream(out
, p7
, in
, flags
);
614 BIO_printf(bio_err
, "Bad output format for PKCS#7 file\n");
618 BIO_printf(bio_err
, "Error writing output\n");
626 ERR_print_errors(bio_err
);
627 sk_X509_pop_free(encerts
, X509_free
);
628 sk_X509_pop_free(other
, X509_free
);
629 X509_VERIFY_PARAM_free(vpm
);
630 sk_OPENSSL_STRING_free(sksigners
);
631 sk_OPENSSL_STRING_free(skkeys
);
632 X509_STORE_free(store
);
642 OPENSSL_free(passin
);
646 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
651 if (signerfile
== NULL
)
653 tmp
= BIO_new_file(signerfile
, "w");
656 for (i
= 0; i
< sk_X509_num(signers
); i
++)
657 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
662 /* Minimal callback just to output policy info (if any) */
664 static int smime_cb(int ok
, X509_STORE_CTX
*ctx
)
668 error
= X509_STORE_CTX_get_error(ctx
);
670 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
671 && ((error
!= X509_V_OK
) || (ok
!= 2)))