2 * Copyright 1999-2017 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 */
15 #include <openssl/crypto.h>
16 #include <openssl/pem.h>
17 #include <openssl/err.h>
18 #include <openssl/x509_vfy.h>
19 #include <openssl/x509v3.h>
21 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
);
22 static int smime_cb(int ok
, X509_STORE_CTX
*ctx
);
26 #define SMIME_SIGNERS 0x40
27 #define SMIME_ENCRYPT (1 | SMIME_OP)
28 #define SMIME_DECRYPT (2 | SMIME_IP)
29 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
30 #define SMIME_VERIFY (4 | SMIME_IP)
31 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
32 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
34 typedef enum OPTION_choice
{
35 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
36 OPT_ENCRYPT
, OPT_DECRYPT
, OPT_SIGN
, OPT_RESIGN
, OPT_VERIFY
,
37 OPT_PK7OUT
, OPT_TEXT
, OPT_NOINTERN
, OPT_NOVERIFY
, OPT_NOCHAIN
,
38 OPT_NOCERTS
, OPT_NOATTR
, OPT_NODETACH
, OPT_NOSMIMECAP
,
39 OPT_BINARY
, OPT_NOSIGS
, OPT_STREAM
, OPT_INDEF
, OPT_NOINDEF
,
40 OPT_CRLFEOL
, OPT_RAND
, OPT_ENGINE
, OPT_PASSIN
,
41 OPT_TO
, OPT_FROM
, OPT_SUBJECT
, OPT_SIGNER
, OPT_RECIP
, OPT_MD
,
42 OPT_CIPHER
, OPT_INKEY
, OPT_KEYFORM
, OPT_CERTFILE
, OPT_CAFILE
,
44 OPT_CAPATH
, OPT_NOCAFILE
, OPT_NOCAPATH
, OPT_IN
, OPT_INFORM
, OPT_OUT
,
45 OPT_OUTFORM
, OPT_CONTENT
48 const OPTIONS smime_options
[] = {
49 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] cert.pem...\n"},
50 {OPT_HELP_STR
, 1, '-',
51 " cert.pem... recipient certs for encryption\n"},
52 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
53 {"help", OPT_HELP
, '-', "Display this summary"},
54 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt message"},
55 {"decrypt", OPT_DECRYPT
, '-', "Decrypt encrypted message"},
56 {"sign", OPT_SIGN
, '-', "Sign message"},
57 {"verify", OPT_VERIFY
, '-', "Verify signed message"},
58 {"pk7out", OPT_PK7OUT
, '-', "Output PKCS#7 structure"},
59 {"nointern", OPT_NOINTERN
, '-',
60 "Don't search certificates in message for signer"},
61 {"nosigs", OPT_NOSIGS
, '-', "Don't verify message signature"},
62 {"noverify", OPT_NOVERIFY
, '-', "Don't verify signers certificate"},
63 {"nocerts", OPT_NOCERTS
, '-',
64 "Don't include signers certificate when signing"},
65 {"nodetach", OPT_NODETACH
, '-', "Use opaque signing"},
66 {"noattr", OPT_NOATTR
, '-', "Don't include any signed attributes"},
67 {"binary", OPT_BINARY
, '-', "Don't translate message to text"},
68 {"certfile", OPT_CERTFILE
, '<', "Other certificates file"},
69 {"signer", OPT_SIGNER
, 's', "Signer certificate file"},
70 {"recip", OPT_RECIP
, '<', "Recipient certificate file for decryption"},
71 {"in", OPT_IN
, '<', "Input file"},
72 {"inform", OPT_INFORM
, 'c', "Input format SMIME (default), PEM or DER"},
73 {"inkey", OPT_INKEY
, 's',
74 "Input private key (if not signer or recipient)"},
75 {"keyform", OPT_KEYFORM
, 'f', "Input private key format (PEM or ENGINE)"},
76 {"out", OPT_OUT
, '>', "Output file"},
77 {"outform", OPT_OUTFORM
, 'c',
78 "Output format SMIME (default), PEM or DER"},
79 {"content", OPT_CONTENT
, '<',
80 "Supply or override content for detached signature"},
81 {"to", OPT_TO
, 's', "To address"},
82 {"from", OPT_FROM
, 's', "From address"},
83 {"subject", OPT_SUBJECT
, 's', "Subject"},
84 {"text", OPT_TEXT
, '-', "Include or delete text MIME headers"},
85 {"CApath", OPT_CAPATH
, '/', "Trusted certificates directory"},
86 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
87 {"no-CAfile", OPT_NOCAFILE
, '-',
88 "Do not load the default certificates file"},
89 {"no-CApath", OPT_NOCAPATH
, '-',
90 "Do not load certificates from the default certificates directory"},
91 {"resign", OPT_RESIGN
, '-', "Resign a signed message"},
92 {"nochain", OPT_NOCHAIN
, '-',
93 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
94 {"nosmimecap", OPT_NOSMIMECAP
, '-', "Omit the SMIMECapabilities attribute"},
95 {"stream", OPT_STREAM
, '-', "Enable CMS streaming" },
96 {"indef", OPT_INDEF
, '-', "Same as -stream" },
97 {"noindef", OPT_NOINDEF
, '-', "Disable CMS streaming"},
98 {"crlfeol", OPT_CRLFEOL
, '-', "Use CRLF as EOL termination instead of CR only"},
99 {"rand", OPT_RAND
, 's',
100 "Load the file(s) into the random number generator"},
101 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
102 {"md", OPT_MD
, 's', "Digest algorithm to use when signing or resigning"},
103 {"", OPT_CIPHER
, '-', "Any supported cipher"},
105 #ifndef OPENSSL_NO_ENGINE
106 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
111 int smime_main(int argc
, char **argv
)
113 BIO
*in
= NULL
, *out
= NULL
, *indata
= NULL
;
114 EVP_PKEY
*key
= NULL
;
116 STACK_OF(OPENSSL_STRING
) *sksigners
= NULL
, *skkeys
= NULL
;
117 STACK_OF(X509
) *encerts
= NULL
, *other
= NULL
;
118 X509
*cert
= NULL
, *recip
= NULL
, *signer
= NULL
;
119 X509_STORE
*store
= NULL
;
120 X509_VERIFY_PARAM
*vpm
= NULL
;
121 const EVP_CIPHER
*cipher
= NULL
;
122 const EVP_MD
*sign_md
= NULL
;
123 const char *CAfile
= NULL
, *CApath
= NULL
, *prog
= NULL
;
124 char *certfile
= NULL
, *keyfile
= NULL
, *contfile
= NULL
, *inrand
= NULL
;
125 char *infile
= NULL
, *outfile
= NULL
, *signerfile
= NULL
, *recipfile
=
127 char *passinarg
= NULL
, *passin
= NULL
, *to
= NULL
, *from
=
128 NULL
, *subject
= NULL
;
130 int noCApath
= 0, noCAfile
= 0;
131 int flags
= PKCS7_DETACHED
, operation
= 0, ret
= 0, need_rand
= 0, indef
=
133 int informat
= FORMAT_SMIME
, outformat
= FORMAT_SMIME
, keyform
=
135 int vpmtouched
= 0, rv
= 0;
137 const char *mime_eol
= "\n";
139 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
142 prog
= opt_init(argc
, argv
, smime_options
);
143 while ((o
= opt_next()) != OPT_EOF
) {
148 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
151 opt_help(smime_options
);
155 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &informat
))
162 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &outformat
))
169 operation
= SMIME_ENCRYPT
;
172 operation
= SMIME_DECRYPT
;
175 operation
= SMIME_SIGN
;
178 operation
= SMIME_RESIGN
;
181 operation
= SMIME_VERIFY
;
184 operation
= SMIME_PK7OUT
;
190 flags
|= PKCS7_NOINTERN
;
193 flags
|= PKCS7_NOVERIFY
;
196 flags
|= PKCS7_NOCHAIN
;
199 flags
|= PKCS7_NOCERTS
;
202 flags
|= PKCS7_NOATTR
;
205 flags
&= ~PKCS7_DETACHED
;
208 flags
|= PKCS7_NOSMIMECAP
;
211 flags
|= PKCS7_BINARY
;
214 flags
|= PKCS7_NOSIGS
;
224 flags
|= PKCS7_CRLFEOL
;
232 e
= setup_engine(opt_arg(), 0);
235 passinarg
= opt_arg();
247 /* If previous -signer argument add signer to list */
248 if (signerfile
!= NULL
) {
249 if (sksigners
== NULL
250 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
252 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
254 keyfile
= signerfile
;
256 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
258 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
261 signerfile
= opt_arg();
264 recipfile
= opt_arg();
267 if (!opt_md(opt_arg(), &sign_md
))
271 if (!opt_cipher(opt_unknown(), &cipher
))
275 /* If previous -inkey argument add signer to list */
276 if (keyfile
!= NULL
) {
277 if (signerfile
== NULL
) {
279 "%s: Must have -signer before -inkey\n", prog
);
282 if (sksigners
== NULL
283 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
285 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
288 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
290 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
295 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
299 certfile
= opt_arg();
314 contfile
= opt_arg();
317 if (!opt_verify(o
, vpm
))
323 argc
= opt_num_rest();
326 if (!(operation
& SMIME_SIGNERS
) && (skkeys
!= NULL
|| sksigners
!= NULL
)) {
327 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
331 if (operation
& SMIME_SIGNERS
) {
332 /* Check to see if any final signer needs to be appended */
333 if (keyfile
&& !signerfile
) {
334 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
337 if (signerfile
!= NULL
) {
338 if (sksigners
== NULL
339 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
341 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
342 if (!skkeys
&& (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
345 keyfile
= signerfile
;
346 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
348 if (sksigners
== NULL
) {
349 BIO_printf(bio_err
, "No signer certificate specified\n");
355 } else if (operation
== SMIME_DECRYPT
) {
356 if (recipfile
== NULL
&& keyfile
== NULL
) {
358 "No recipient certificate or key specified\n");
361 } else if (operation
== SMIME_ENCRYPT
) {
363 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
367 } else if (!operation
) {
371 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
372 BIO_printf(bio_err
, "Error getting password\n");
377 app_RAND_load_file(NULL
, (inrand
!= NULL
));
379 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
380 app_RAND_load_files(inrand
));
385 if (!(operation
& SMIME_SIGNERS
))
386 flags
&= ~PKCS7_DETACHED
;
388 if (!(operation
& SMIME_OP
)) {
389 if (flags
& PKCS7_BINARY
)
390 outformat
= FORMAT_BINARY
;
393 if (!(operation
& SMIME_IP
)) {
394 if (flags
& PKCS7_BINARY
)
395 informat
= FORMAT_BINARY
;
398 if (operation
== SMIME_ENCRYPT
) {
399 if (cipher
== NULL
) {
400 #ifndef OPENSSL_NO_DES
401 cipher
= EVP_des_ede3_cbc();
403 BIO_printf(bio_err
, "No cipher selected\n");
407 encerts
= sk_X509_new_null();
410 while (*argv
!= NULL
) {
411 cert
= load_cert(*argv
, FORMAT_PEM
,
412 "recipient certificate file");
415 sk_X509_push(encerts
, cert
);
421 if (certfile
!= NULL
) {
422 if (!load_certs(certfile
, &other
, FORMAT_PEM
, NULL
,
423 "certificate file")) {
424 ERR_print_errors(bio_err
);
429 if (recipfile
!= NULL
&& (operation
== SMIME_DECRYPT
)) {
430 if ((recip
= load_cert(recipfile
, FORMAT_PEM
,
431 "recipient certificate file")) == NULL
) {
432 ERR_print_errors(bio_err
);
437 if (operation
== SMIME_DECRYPT
) {
440 } else if (operation
== SMIME_SIGN
) {
442 keyfile
= signerfile
;
447 if (keyfile
!= NULL
) {
448 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
453 in
= bio_open_default(infile
, 'r', informat
);
457 if (operation
& SMIME_IP
) {
458 if (informat
== FORMAT_SMIME
) {
459 p7
= SMIME_read_PKCS7(in
, &indata
);
460 } else if (informat
== FORMAT_PEM
) {
461 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
462 } else if (informat
== FORMAT_ASN1
) {
463 p7
= d2i_PKCS7_bio(in
, NULL
);
465 BIO_printf(bio_err
, "Bad input format for PKCS#7 file\n");
470 BIO_printf(bio_err
, "Error reading S/MIME message\n");
473 if (contfile
!= NULL
) {
475 if ((indata
= BIO_new_file(contfile
, "rb")) == NULL
) {
476 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
482 out
= bio_open_default(outfile
, 'w', outformat
);
486 if (operation
== SMIME_VERIFY
) {
487 if ((store
= setup_verify(CAfile
, CApath
, noCAfile
, noCApath
)) == NULL
)
489 X509_STORE_set_verify_cb(store
, smime_cb
);
491 X509_STORE_set1_param(store
, vpm
);
496 if (operation
== SMIME_ENCRYPT
) {
498 flags
|= PKCS7_STREAM
;
499 p7
= PKCS7_encrypt(encerts
, in
, cipher
, flags
);
500 } else if (operation
& SMIME_SIGNERS
) {
503 * If detached data content we only enable streaming if S/MIME output
506 if (operation
== SMIME_SIGN
) {
507 if (flags
& PKCS7_DETACHED
) {
508 if (outformat
== FORMAT_SMIME
)
509 flags
|= PKCS7_STREAM
;
511 flags
|= PKCS7_STREAM
;
513 flags
|= PKCS7_PARTIAL
;
514 p7
= PKCS7_sign(NULL
, NULL
, other
, in
, flags
);
517 if (flags
& PKCS7_NOCERTS
) {
518 for (i
= 0; i
< sk_X509_num(other
); i
++) {
519 X509
*x
= sk_X509_value(other
, i
);
520 PKCS7_add_certificate(p7
, x
);
524 flags
|= PKCS7_REUSE_DIGEST
;
526 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++) {
527 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
528 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
529 signer
= load_cert(signerfile
, FORMAT_PEM
,
530 "signer certificate");
533 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
536 if (!PKCS7_sign_add_signer(p7
, signer
, key
, sign_md
, flags
))
543 /* If not streaming or resigning finalize structure */
544 if ((operation
== SMIME_SIGN
) && !(flags
& PKCS7_STREAM
)) {
545 if (!PKCS7_final(p7
, in
, flags
))
551 BIO_printf(bio_err
, "Error creating PKCS#7 structure\n");
556 if (operation
== SMIME_DECRYPT
) {
557 if (!PKCS7_decrypt(p7
, key
, recip
, out
, flags
)) {
558 BIO_printf(bio_err
, "Error decrypting PKCS#7 structure\n");
561 } else if (operation
== SMIME_VERIFY
) {
562 STACK_OF(X509
) *signers
;
563 if (PKCS7_verify(p7
, other
, store
, indata
, out
, flags
))
564 BIO_printf(bio_err
, "Verification successful\n");
566 BIO_printf(bio_err
, "Verification failure\n");
569 signers
= PKCS7_get0_signers(p7
, other
, flags
);
570 if (!save_certs(signerfile
, signers
)) {
571 BIO_printf(bio_err
, "Error writing signers to %s\n", signerfile
);
575 sk_X509_free(signers
);
576 } else if (operation
== SMIME_PK7OUT
) {
577 PEM_write_bio_PKCS7(out
, p7
);
580 BIO_printf(out
, "To: %s%s", to
, mime_eol
);
582 BIO_printf(out
, "From: %s%s", from
, mime_eol
);
584 BIO_printf(out
, "Subject: %s%s", subject
, mime_eol
);
585 if (outformat
== FORMAT_SMIME
) {
586 if (operation
== SMIME_RESIGN
)
587 rv
= SMIME_write_PKCS7(out
, p7
, indata
, flags
);
589 rv
= SMIME_write_PKCS7(out
, p7
, in
, flags
);
590 } else if (outformat
== FORMAT_PEM
) {
591 rv
= PEM_write_bio_PKCS7_stream(out
, p7
, in
, flags
);
592 } else if (outformat
== FORMAT_ASN1
) {
593 rv
= i2d_PKCS7_bio_stream(out
, p7
, in
, flags
);
595 BIO_printf(bio_err
, "Bad output format for PKCS#7 file\n");
599 BIO_printf(bio_err
, "Error writing output\n");
607 app_RAND_write_file(NULL
);
609 ERR_print_errors(bio_err
);
610 sk_X509_pop_free(encerts
, X509_free
);
611 sk_X509_pop_free(other
, X509_free
);
612 X509_VERIFY_PARAM_free(vpm
);
613 sk_OPENSSL_STRING_free(sksigners
);
614 sk_OPENSSL_STRING_free(skkeys
);
615 X509_STORE_free(store
);
625 OPENSSL_free(passin
);
629 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
634 if (signerfile
== NULL
)
636 tmp
= BIO_new_file(signerfile
, "w");
639 for (i
= 0; i
< sk_X509_num(signers
); i
++)
640 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
645 /* Minimal callback just to output policy info (if any) */
647 static int smime_cb(int ok
, X509_STORE_CTX
*ctx
)
651 error
= X509_STORE_CTX_get_error(ctx
);
653 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
654 && ((error
!= X509_V_OK
) || (ok
!= 2)))