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>
23 DEFINE_STACK_OF_STRING()
25 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
26 static int smime_cb(int ok, X509_STORE_CTX *ctx);
30 #define SMIME_SIGNERS 0x40
31 #define SMIME_ENCRYPT (1 | SMIME_OP)
32 #define SMIME_DECRYPT (2 | SMIME_IP)
33 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
34 #define SMIME_VERIFY (4 | SMIME_IP)
35 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
36 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
38 typedef enum OPTION_choice {
39 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
40 OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY,
41 OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN,
42 OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP,
43 OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF,
44 OPT_CRLFEOL, OPT_ENGINE, OPT_PASSIN,
45 OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD,
46 OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE,
47 OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE, OPT_NOCAPATH, OPT_NOCASTORE,
48 OPT_R_ENUM, OPT_PROV_ENUM,
50 OPT_IN, OPT_INFORM, OPT_OUT,
51 OPT_OUTFORM, OPT_CONTENT
54 const OPTIONS smime_options[] = {
55 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
57 OPT_SECTION("General"),
58 {"help", OPT_HELP, '-', "Display this summary"},
59 {"in", OPT_IN, '<', "Input file"},
60 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
61 {"out", OPT_OUT, '>', "Output file"},
62 {"outform", OPT_OUTFORM, 'c',
63 "Output format SMIME (default), PEM or DER"},
64 {"inkey", OPT_INKEY, 's',
65 "Input private key (if not signer or recipient)"},
66 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
67 #ifndef OPENSSL_NO_ENGINE
68 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
70 {"stream", OPT_STREAM, '-', "Enable CMS streaming" },
71 {"indef", OPT_INDEF, '-', "Same as -stream" },
72 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
74 OPT_SECTION("Action"),
75 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
76 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
77 {"sign", OPT_SIGN, '-', "Sign message"},
78 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
79 {"verify", OPT_VERIFY, '-', "Verify signed message"},
81 OPT_SECTION("Signing/Encryption"),
82 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
83 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
84 {"", OPT_CIPHER, '-', "Any supported cipher"},
85 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
86 {"nointern", OPT_NOINTERN, '-',
87 "Don't search certificates in message for signer"},
88 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
89 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
90 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
91 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
92 {"content", OPT_CONTENT, '<',
93 "Supply or override content for detached signature"},
94 {"nocerts", OPT_NOCERTS, '-',
95 "Don't include signers certificate when signing"},
97 OPT_SECTION("Verification/Decryption"),
98 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
99 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
101 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
102 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
104 OPT_SECTION("Email"),
105 {"to", OPT_TO, 's', "To address"},
106 {"from", OPT_FROM, 's', "From address"},
107 {"subject", OPT_SUBJECT, 's', "Subject"},
108 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
109 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
111 OPT_SECTION("Certificate chain"),
112 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
113 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
114 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
115 {"no-CAfile", OPT_NOCAFILE, '-',
116 "Do not load the default certificates file"},
117 {"no-CApath", OPT_NOCAPATH, '-',
118 "Do not load certificates from the default certificates directory"},
119 {"no-CAstore", OPT_NOCASTORE, '-',
120 "Do not load certificates from the default certificates store"},
121 {"nochain", OPT_NOCHAIN, '-',
122 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
123 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"},
130 {"cert", 0, 0, "Recipient certs, used when encrypting"},
134 int smime_main(int argc, char **argv)
136 BIO *in = NULL, *out = NULL, *indata = NULL;
137 EVP_PKEY *key = NULL;
139 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
140 STACK_OF(X509) *encerts = NULL, *other = NULL;
141 X509 *cert = NULL, *recip = NULL, *signer = NULL;
142 X509_STORE *store = NULL;
143 X509_VERIFY_PARAM *vpm = NULL;
144 const EVP_CIPHER *cipher = NULL;
145 const EVP_MD *sign_md = NULL;
146 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog = NULL;
147 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
148 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;
149 char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL, *subject = NULL;
151 int noCApath = 0, noCAfile = 0, noCAstore = 0;
152 int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
153 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
155 int vpmtouched = 0, rv = 0;
157 const char *mime_eol = "\n";
159 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
162 prog = opt_init(argc, argv, smime_options);
163 while ((o = opt_next()) != OPT_EOF) {
168 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
171 opt_help(smime_options);
175 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
182 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
189 operation = SMIME_ENCRYPT;
192 operation = SMIME_DECRYPT;
195 operation = SMIME_SIGN;
198 operation = SMIME_RESIGN;
201 operation = SMIME_VERIFY;
204 operation = SMIME_PK7OUT;
210 flags |= PKCS7_NOINTERN;
213 flags |= PKCS7_NOVERIFY;
216 flags |= PKCS7_NOCHAIN;
219 flags |= PKCS7_NOCERTS;
222 flags |= PKCS7_NOATTR;
225 flags &= ~PKCS7_DETACHED;
228 flags |= PKCS7_NOSMIMECAP;
231 flags |= PKCS7_BINARY;
234 flags |= PKCS7_NOSIGS;
244 flags |= PKCS7_CRLFEOL;
252 if (!opt_provider(o))
256 e = setup_engine(opt_arg(), 0);
259 passinarg = opt_arg();
271 /* If previous -signer argument add signer to list */
272 if (signerfile != NULL) {
273 if (sksigners == NULL
274 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
276 sk_OPENSSL_STRING_push(sksigners, signerfile);
278 keyfile = signerfile;
280 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
282 sk_OPENSSL_STRING_push(skkeys, keyfile);
285 signerfile = opt_arg();
288 recipfile = opt_arg();
291 if (!opt_md(opt_arg(), &sign_md))
295 if (!opt_cipher(opt_unknown(), &cipher))
299 /* If previous -inkey argument add signer to list */
300 if (keyfile != NULL) {
301 if (signerfile == NULL) {
303 "%s: Must have -signer before -inkey\n", prog);
306 if (sksigners == NULL
307 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
309 sk_OPENSSL_STRING_push(sksigners, signerfile);
312 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
314 sk_OPENSSL_STRING_push(skkeys, keyfile);
319 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
323 certfile = opt_arg();
344 contfile = opt_arg();
347 if (!opt_verify(o, vpm))
353 argc = opt_num_rest();
356 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
357 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
361 if (operation & SMIME_SIGNERS) {
362 /* Check to see if any final signer needs to be appended */
363 if (keyfile && !signerfile) {
364 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
367 if (signerfile != NULL) {
368 if (sksigners == NULL
369 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
371 sk_OPENSSL_STRING_push(sksigners, signerfile);
372 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
375 keyfile = signerfile;
376 sk_OPENSSL_STRING_push(skkeys, keyfile);
378 if (sksigners == NULL) {
379 BIO_printf(bio_err, "No signer certificate specified\n");
384 } else if (operation == SMIME_DECRYPT) {
385 if (recipfile == NULL && keyfile == NULL) {
387 "No recipient certificate or key specified\n");
390 } else if (operation == SMIME_ENCRYPT) {
392 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
395 } else if (!operation) {
399 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
400 BIO_printf(bio_err, "Error getting password\n");
406 if (!(operation & SMIME_SIGNERS))
407 flags &= ~PKCS7_DETACHED;
409 if (!(operation & SMIME_OP)) {
410 if (flags & PKCS7_BINARY)
411 outformat = FORMAT_BINARY;
414 if (!(operation & SMIME_IP)) {
415 if (flags & PKCS7_BINARY)
416 informat = FORMAT_BINARY;
419 if (operation == SMIME_ENCRYPT) {
420 if (cipher == NULL) {
421 #ifndef OPENSSL_NO_DES
422 cipher = EVP_des_ede3_cbc();
424 BIO_printf(bio_err, "No cipher selected\n");
428 encerts = sk_X509_new_null();
431 while (*argv != NULL) {
432 cert = load_cert(*argv, FORMAT_PEM,
433 "recipient certificate file");
436 sk_X509_push(encerts, cert);
442 if (certfile != NULL) {
443 if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
444 "certificate file")) {
445 ERR_print_errors(bio_err);
450 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
451 if ((recip = load_cert(recipfile, FORMAT_PEM,
452 "recipient certificate file")) == NULL) {
453 ERR_print_errors(bio_err);
458 if (operation == SMIME_DECRYPT) {
461 } else if (operation == SMIME_SIGN) {
463 keyfile = signerfile;
468 if (keyfile != NULL) {
469 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
474 in = bio_open_default(infile, 'r', informat);
478 if (operation & SMIME_IP) {
479 if (informat == FORMAT_SMIME) {
480 p7 = SMIME_read_PKCS7(in, &indata);
481 } else if (informat == FORMAT_PEM) {
482 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
483 } else if (informat == FORMAT_ASN1) {
484 p7 = d2i_PKCS7_bio(in, NULL);
486 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
491 BIO_printf(bio_err, "Error reading S/MIME message\n");
494 if (contfile != NULL) {
496 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
497 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
503 out = bio_open_default(outfile, 'w', outformat);
507 if (operation == SMIME_VERIFY) {
508 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
509 CAstore, noCAstore)) == NULL)
511 X509_STORE_set_verify_cb(store, smime_cb);
513 X509_STORE_set1_param(store, vpm);
518 if (operation == SMIME_ENCRYPT) {
520 flags |= PKCS7_STREAM;
521 p7 = PKCS7_encrypt(encerts, in, cipher, flags);
522 } else if (operation & SMIME_SIGNERS) {
525 * If detached data content we only enable streaming if S/MIME output
528 if (operation == SMIME_SIGN) {
529 if (flags & PKCS7_DETACHED) {
530 if (outformat == FORMAT_SMIME)
531 flags |= PKCS7_STREAM;
533 flags |= PKCS7_STREAM;
535 flags |= PKCS7_PARTIAL;
536 p7 = PKCS7_sign(NULL, NULL, other, in, flags);
539 if (flags & PKCS7_NOCERTS) {
540 for (i = 0; i < sk_X509_num(other); i++) {
541 X509 *x = sk_X509_value(other, i);
542 PKCS7_add_certificate(p7, x);
546 flags |= PKCS7_REUSE_DIGEST;
548 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
549 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
550 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
551 signer = load_cert(signerfile, FORMAT_PEM,
552 "signer certificate");
555 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
558 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
565 /* If not streaming or resigning finalize structure */
566 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
567 if (!PKCS7_final(p7, in, flags))
573 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
578 if (operation == SMIME_DECRYPT) {
579 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
580 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
583 } else if (operation == SMIME_VERIFY) {
584 STACK_OF(X509) *signers;
585 if (PKCS7_verify(p7, other, store, indata, out, flags))
586 BIO_printf(bio_err, "Verification successful\n");
588 BIO_printf(bio_err, "Verification failure\n");
591 signers = PKCS7_get0_signers(p7, other, flags);
592 if (!save_certs(signerfile, signers)) {
593 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
597 sk_X509_free(signers);
598 } else if (operation == SMIME_PK7OUT) {
599 PEM_write_bio_PKCS7(out, p7);
602 BIO_printf(out, "To: %s%s", to, mime_eol);
604 BIO_printf(out, "From: %s%s", from, mime_eol);
606 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
607 if (outformat == FORMAT_SMIME) {
608 if (operation == SMIME_RESIGN)
609 rv = SMIME_write_PKCS7(out, p7, indata, flags);
611 rv = SMIME_write_PKCS7(out, p7, in, flags);
612 } else if (outformat == FORMAT_PEM) {
613 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
614 } else if (outformat == FORMAT_ASN1) {
615 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
617 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
621 BIO_printf(bio_err, "Error writing output\n");
629 ERR_print_errors(bio_err);
630 sk_X509_pop_free(encerts, X509_free);
631 sk_X509_pop_free(other, X509_free);
632 X509_VERIFY_PARAM_free(vpm);
633 sk_OPENSSL_STRING_free(sksigners);
634 sk_OPENSSL_STRING_free(skkeys);
635 X509_STORE_free(store);
645 OPENSSL_free(passin);
649 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
654 if (signerfile == NULL)
656 tmp = BIO_new_file(signerfile, "w");
659 for (i = 0; i < sk_X509_num(signers); i++)
660 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
665 /* Minimal callback just to output policy info (if any) */
667 static int smime_cb(int ok, X509_STORE_CTX *ctx)
671 error = X509_STORE_CTX_get_error(ctx);
673 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
674 && ((error != X509_V_OK) || (ok != 2)))