2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
,
46 OPT_CAPATH
, OPT_NOCAFILE
, OPT_NOCAPATH
, OPT_IN
, OPT_INFORM
, OPT_OUT
,
47 OPT_OUTFORM
, OPT_CONTENT
50 const OPTIONS smime_options
[] = {
51 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] cert.pem...\n"},
52 {OPT_HELP_STR
, 1, '-',
53 " cert.pem... recipient certs for encryption\n"},
54 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
55 {"help", OPT_HELP
, '-', "Display this summary"},
56 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt message"},
57 {"decrypt", OPT_DECRYPT
, '-', "Decrypt encrypted message"},
58 {"sign", OPT_SIGN
, '-', "Sign message"},
59 {"verify", OPT_VERIFY
, '-', "Verify signed message"},
60 {"pk7out", OPT_PK7OUT
, '-', "Output PKCS#7 structure"},
61 {"nointern", OPT_NOINTERN
, '-',
62 "Don't search certificates in message for signer"},
63 {"nosigs", OPT_NOSIGS
, '-', "Don't verify message signature"},
64 {"noverify", OPT_NOVERIFY
, '-', "Don't verify signers certificate"},
65 {"nocerts", OPT_NOCERTS
, '-',
66 "Don't include signers certificate when signing"},
67 {"nodetach", OPT_NODETACH
, '-', "Use opaque signing"},
68 {"noattr", OPT_NOATTR
, '-', "Don't include any signed attributes"},
69 {"binary", OPT_BINARY
, '-', "Don't translate message to text"},
70 {"certfile", OPT_CERTFILE
, '<', "Other certificates file"},
71 {"signer", OPT_SIGNER
, 's', "Signer certificate file"},
72 {"recip", OPT_RECIP
, '<', "Recipient certificate file for decryption"},
73 {"in", OPT_IN
, '<', "Input file"},
74 {"inform", OPT_INFORM
, 'c', "Input format SMIME (default), PEM or DER"},
75 {"inkey", OPT_INKEY
, 's',
76 "Input private key (if not signer or recipient)"},
77 {"keyform", OPT_KEYFORM
, 'f', "Input private key format (PEM or ENGINE)"},
78 {"out", OPT_OUT
, '>', "Output file"},
79 {"outform", OPT_OUTFORM
, 'c',
80 "Output format SMIME (default), PEM or DER"},
81 {"content", OPT_CONTENT
, '<',
82 "Supply or override content for detached signature"},
83 {"to", OPT_TO
, 's', "To address"},
84 {"from", OPT_FROM
, 's', "From address"},
85 {"subject", OPT_SUBJECT
, 's', "Subject"},
86 {"text", OPT_TEXT
, '-', "Include or delete text MIME headers"},
87 {"CApath", OPT_CAPATH
, '/', "Trusted certificates directory"},
88 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
89 {"no-CAfile", OPT_NOCAFILE
, '-',
90 "Do not load the default certificates file"},
91 {"no-CApath", OPT_NOCAPATH
, '-',
92 "Do not load certificates from the default certificates directory"},
93 {"resign", OPT_RESIGN
, '-', "Resign a signed message"},
94 {"nochain", OPT_NOCHAIN
, '-',
95 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
96 {"nosmimecap", OPT_NOSMIMECAP
, '-', "Omit the SMIMECapabilities attribute"},
97 {"stream", OPT_STREAM
, '-', "Enable CMS streaming" },
98 {"indef", OPT_INDEF
, '-', "Same as -stream" },
99 {"noindef", OPT_NOINDEF
, '-', "Disable CMS streaming"},
100 {"crlfeol", OPT_CRLFEOL
, '-', "Use CRLF as EOL termination instead of CR only"},
102 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
103 {"md", OPT_MD
, 's', "Digest algorithm to use when signing or resigning"},
104 {"", OPT_CIPHER
, '-', "Any supported cipher"},
106 #ifndef OPENSSL_NO_ENGINE
107 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
112 int smime_main(int argc
, char **argv
)
114 BIO
*in
= NULL
, *out
= NULL
, *indata
= NULL
;
115 EVP_PKEY
*key
= NULL
;
117 STACK_OF(OPENSSL_STRING
) *sksigners
= NULL
, *skkeys
= NULL
;
118 STACK_OF(X509
) *encerts
= NULL
, *other
= NULL
;
119 X509
*cert
= NULL
, *recip
= NULL
, *signer
= NULL
;
120 X509_STORE
*store
= NULL
;
121 X509_VERIFY_PARAM
*vpm
= NULL
;
122 const EVP_CIPHER
*cipher
= NULL
;
123 const EVP_MD
*sign_md
= NULL
;
124 const char *CAfile
= NULL
, *CApath
= NULL
, *prog
= NULL
;
125 char *certfile
= NULL
, *keyfile
= NULL
, *contfile
= NULL
;
126 char *infile
= NULL
, *outfile
= NULL
, *signerfile
= NULL
, *recipfile
= NULL
;
127 char *passinarg
= NULL
, *passin
= NULL
, *to
= NULL
, *from
= NULL
, *subject
= NULL
;
129 int noCApath
= 0, noCAfile
= 0;
130 int flags
= PKCS7_DETACHED
, operation
= 0, ret
= 0, indef
= 0;
131 int informat
= FORMAT_SMIME
, outformat
= FORMAT_SMIME
, keyform
=
133 int vpmtouched
= 0, rv
= 0;
135 const char *mime_eol
= "\n";
137 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
140 prog
= opt_init(argc
, argv
, smime_options
);
141 while ((o
= opt_next()) != OPT_EOF
) {
146 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
149 opt_help(smime_options
);
153 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &informat
))
160 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &outformat
))
167 operation
= SMIME_ENCRYPT
;
170 operation
= SMIME_DECRYPT
;
173 operation
= SMIME_SIGN
;
176 operation
= SMIME_RESIGN
;
179 operation
= SMIME_VERIFY
;
182 operation
= SMIME_PK7OUT
;
188 flags
|= PKCS7_NOINTERN
;
191 flags
|= PKCS7_NOVERIFY
;
194 flags
|= PKCS7_NOCHAIN
;
197 flags
|= PKCS7_NOCERTS
;
200 flags
|= PKCS7_NOATTR
;
203 flags
&= ~PKCS7_DETACHED
;
206 flags
|= PKCS7_NOSMIMECAP
;
209 flags
|= PKCS7_BINARY
;
212 flags
|= PKCS7_NOSIGS
;
222 flags
|= PKCS7_CRLFEOL
;
230 e
= setup_engine(opt_arg(), 0);
233 passinarg
= opt_arg();
245 /* If previous -signer argument add signer to list */
246 if (signerfile
!= NULL
) {
247 if (sksigners
== NULL
248 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
250 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
252 keyfile
= signerfile
;
254 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
256 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
259 signerfile
= opt_arg();
262 recipfile
= opt_arg();
265 if (!opt_md(opt_arg(), &sign_md
))
269 if (!opt_cipher(opt_unknown(), &cipher
))
273 /* If previous -inkey argument add signer to list */
274 if (keyfile
!= NULL
) {
275 if (signerfile
== NULL
) {
277 "%s: Must have -signer before -inkey\n", prog
);
280 if (sksigners
== NULL
281 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
283 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
286 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
288 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
293 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
297 certfile
= opt_arg();
312 contfile
= opt_arg();
315 if (!opt_verify(o
, vpm
))
321 argc
= opt_num_rest();
324 if (!(operation
& SMIME_SIGNERS
) && (skkeys
!= NULL
|| sksigners
!= NULL
)) {
325 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
329 if (operation
& SMIME_SIGNERS
) {
330 /* Check to see if any final signer needs to be appended */
331 if (keyfile
&& !signerfile
) {
332 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
335 if (signerfile
!= NULL
) {
336 if (sksigners
== NULL
337 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
339 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
340 if (!skkeys
&& (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
343 keyfile
= signerfile
;
344 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
346 if (sksigners
== NULL
) {
347 BIO_printf(bio_err
, "No signer certificate specified\n");
352 } else if (operation
== SMIME_DECRYPT
) {
353 if (recipfile
== NULL
&& keyfile
== NULL
) {
355 "No recipient certificate or key specified\n");
358 } else if (operation
== SMIME_ENCRYPT
) {
360 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
363 } else if (!operation
) {
367 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
368 BIO_printf(bio_err
, "Error getting password\n");
374 if (!(operation
& SMIME_SIGNERS
))
375 flags
&= ~PKCS7_DETACHED
;
377 if (!(operation
& SMIME_OP
)) {
378 if (flags
& PKCS7_BINARY
)
379 outformat
= FORMAT_BINARY
;
382 if (!(operation
& SMIME_IP
)) {
383 if (flags
& PKCS7_BINARY
)
384 informat
= FORMAT_BINARY
;
387 if (operation
== SMIME_ENCRYPT
) {
388 if (cipher
== NULL
) {
389 #ifndef OPENSSL_NO_DES
390 cipher
= EVP_des_ede3_cbc();
392 BIO_printf(bio_err
, "No cipher selected\n");
396 encerts
= sk_X509_new_null();
399 while (*argv
!= NULL
) {
400 cert
= load_cert(*argv
, FORMAT_PEM
,
401 "recipient certificate file");
404 sk_X509_push(encerts
, cert
);
410 if (certfile
!= NULL
) {
411 if (!load_certs(certfile
, &other
, FORMAT_PEM
, NULL
,
412 "certificate file")) {
413 ERR_print_errors(bio_err
);
418 if (recipfile
!= NULL
&& (operation
== SMIME_DECRYPT
)) {
419 if ((recip
= load_cert(recipfile
, FORMAT_PEM
,
420 "recipient certificate file")) == NULL
) {
421 ERR_print_errors(bio_err
);
426 if (operation
== SMIME_DECRYPT
) {
429 } else if (operation
== SMIME_SIGN
) {
431 keyfile
= signerfile
;
436 if (keyfile
!= NULL
) {
437 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
442 in
= bio_open_default(infile
, 'r', informat
);
446 if (operation
& SMIME_IP
) {
447 if (informat
== FORMAT_SMIME
) {
448 p7
= SMIME_read_PKCS7(in
, &indata
);
449 } else if (informat
== FORMAT_PEM
) {
450 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
451 } else if (informat
== FORMAT_ASN1
) {
452 p7
= d2i_PKCS7_bio(in
, NULL
);
454 BIO_printf(bio_err
, "Bad input format for PKCS#7 file\n");
459 BIO_printf(bio_err
, "Error reading S/MIME message\n");
462 if (contfile
!= NULL
) {
464 if ((indata
= BIO_new_file(contfile
, "rb")) == NULL
) {
465 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
471 out
= bio_open_default(outfile
, 'w', outformat
);
475 if (operation
== SMIME_VERIFY
) {
476 if ((store
= setup_verify(CAfile
, CApath
, noCAfile
, noCApath
)) == NULL
)
478 X509_STORE_set_verify_cb(store
, smime_cb
);
480 X509_STORE_set1_param(store
, vpm
);
485 if (operation
== SMIME_ENCRYPT
) {
487 flags
|= PKCS7_STREAM
;
488 p7
= PKCS7_encrypt(encerts
, in
, cipher
, flags
);
489 } else if (operation
& SMIME_SIGNERS
) {
492 * If detached data content we only enable streaming if S/MIME output
495 if (operation
== SMIME_SIGN
) {
496 if (flags
& PKCS7_DETACHED
) {
497 if (outformat
== FORMAT_SMIME
)
498 flags
|= PKCS7_STREAM
;
500 flags
|= PKCS7_STREAM
;
502 flags
|= PKCS7_PARTIAL
;
503 p7
= PKCS7_sign(NULL
, NULL
, other
, in
, flags
);
506 if (flags
& PKCS7_NOCERTS
) {
507 for (i
= 0; i
< sk_X509_num(other
); i
++) {
508 X509
*x
= sk_X509_value(other
, i
);
509 PKCS7_add_certificate(p7
, x
);
513 flags
|= PKCS7_REUSE_DIGEST
;
515 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++) {
516 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
517 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
518 signer
= load_cert(signerfile
, FORMAT_PEM
,
519 "signer certificate");
522 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
525 if (!PKCS7_sign_add_signer(p7
, signer
, key
, sign_md
, flags
))
532 /* If not streaming or resigning finalize structure */
533 if ((operation
== SMIME_SIGN
) && !(flags
& PKCS7_STREAM
)) {
534 if (!PKCS7_final(p7
, in
, flags
))
540 BIO_printf(bio_err
, "Error creating PKCS#7 structure\n");
545 if (operation
== SMIME_DECRYPT
) {
546 if (!PKCS7_decrypt(p7
, key
, recip
, out
, flags
)) {
547 BIO_printf(bio_err
, "Error decrypting PKCS#7 structure\n");
550 } else if (operation
== SMIME_VERIFY
) {
551 STACK_OF(X509
) *signers
;
552 if (PKCS7_verify(p7
, other
, store
, indata
, out
, flags
))
553 BIO_printf(bio_err
, "Verification successful\n");
555 BIO_printf(bio_err
, "Verification failure\n");
558 signers
= PKCS7_get0_signers(p7
, other
, flags
);
559 if (!save_certs(signerfile
, signers
)) {
560 BIO_printf(bio_err
, "Error writing signers to %s\n", signerfile
);
564 sk_X509_free(signers
);
565 } else if (operation
== SMIME_PK7OUT
) {
566 PEM_write_bio_PKCS7(out
, p7
);
569 BIO_printf(out
, "To: %s%s", to
, mime_eol
);
571 BIO_printf(out
, "From: %s%s", from
, mime_eol
);
573 BIO_printf(out
, "Subject: %s%s", subject
, mime_eol
);
574 if (outformat
== FORMAT_SMIME
) {
575 if (operation
== SMIME_RESIGN
)
576 rv
= SMIME_write_PKCS7(out
, p7
, indata
, flags
);
578 rv
= SMIME_write_PKCS7(out
, p7
, in
, flags
);
579 } else if (outformat
== FORMAT_PEM
) {
580 rv
= PEM_write_bio_PKCS7_stream(out
, p7
, in
, flags
);
581 } else if (outformat
== FORMAT_ASN1
) {
582 rv
= i2d_PKCS7_bio_stream(out
, p7
, in
, flags
);
584 BIO_printf(bio_err
, "Bad output format for PKCS#7 file\n");
588 BIO_printf(bio_err
, "Error writing output\n");
596 ERR_print_errors(bio_err
);
597 sk_X509_pop_free(encerts
, X509_free
);
598 sk_X509_pop_free(other
, X509_free
);
599 X509_VERIFY_PARAM_free(vpm
);
600 sk_OPENSSL_STRING_free(sksigners
);
601 sk_OPENSSL_STRING_free(skkeys
);
602 X509_STORE_free(store
);
612 OPENSSL_free(passin
);
616 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
621 if (signerfile
== NULL
)
623 tmp
= BIO_new_file(signerfile
, "w");
626 for (i
= 0; i
< sk_X509_num(signers
); i
++)
627 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
632 /* Minimal callback just to output policy info (if any) */
634 static int smime_cb(int ok
, X509_STORE_CTX
*ctx
)
638 error
= X509_STORE_CTX_get_error(ctx
);
640 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
641 && ((error
!= X509_V_OK
) || (ok
!= 2)))