2 * Copyright 1999-2023 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_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
32 #define SMIME_VERIFY (4 | SMIME_IP)
33 #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
35 typedef enum OPTION_choice {
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, OPT_CONFIG,
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 (ENGINE, other values ignored)"},
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"},
72 OPT_SECTION("Action"),
73 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
74 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
75 {"sign", OPT_SIGN, '-', "Sign message"},
76 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
77 {"verify", OPT_VERIFY, '-', "Verify signed message"},
78 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
80 OPT_SECTION("Signing/Encryption"),
81 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
82 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
83 {"", OPT_CIPHER, '-', "Any supported cipher"},
84 {"nointern", OPT_NOINTERN, '-',
85 "Don't search certificates in message for signer"},
86 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
87 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
88 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
89 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
90 {"content", OPT_CONTENT, '<',
91 "Supply or override content for detached signature"},
92 {"nocerts", OPT_NOCERTS, '-',
93 "Don't include signers certificate when signing"},
95 OPT_SECTION("Verification/Decryption"),
96 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
97 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
99 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
100 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
102 OPT_SECTION("Email"),
103 {"to", OPT_TO, 's', "To address"},
104 {"from", OPT_FROM, 's', "From address"},
105 {"subject", OPT_SUBJECT, 's', "Subject"},
106 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
107 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
109 OPT_SECTION("Certificate chain"),
110 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
111 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
112 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
113 {"no-CAfile", OPT_NOCAFILE, '-',
114 "Do not load the default certificates file"},
115 {"no-CApath", OPT_NOCAPATH, '-',
116 "Do not load certificates from the default certificates directory"},
117 {"no-CAstore", OPT_NOCASTORE, '-',
118 "Do not load certificates from the default certificates store"},
119 {"nochain", OPT_NOCHAIN, '-',
120 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
121 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"},
128 {"cert", 0, 0, "Recipient certs, used when encrypting"},
132 static const char *operation_name(int operation)
148 return "(invalid operation)";
152 #define SET_OPERATION(op) \
153 ((operation != 0 && (operation != (op))) \
154 ? 0 * BIO_printf(bio_err, "%s: Cannot use -%s together with -%s\n", \
155 prog, operation_name(op), operation_name(operation)) \
156 : (operation = (op)))
158 int smime_main(int argc, char **argv)
161 BIO *in = NULL, *out = NULL, *indata = NULL;
162 EVP_PKEY *key = NULL;
164 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
165 STACK_OF(X509) *encerts = NULL, *other = NULL;
166 X509 *cert = NULL, *recip = NULL, *signer = NULL;
167 X509_STORE *store = NULL;
168 X509_VERIFY_PARAM *vpm = NULL;
169 EVP_CIPHER *cipher = NULL;
170 EVP_MD *sign_md = NULL;
171 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog = NULL;
172 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
173 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;
174 char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL;
175 char *subject = NULL, *digestname = NULL, *ciphername = NULL;
177 int noCApath = 0, noCAfile = 0, noCAstore = 0;
178 int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
179 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
181 int vpmtouched = 0, rv = 0;
183 const char *mime_eol = "\n";
184 OSSL_LIB_CTX *libctx = app_get0_libctx();
186 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
189 opt_set_unknown_name("cipher");
190 prog = opt_init(argc, argv, smime_options);
191 while ((o = opt_next()) != OPT_EOF) {
196 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
199 opt_help(smime_options);
203 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
210 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
217 if (!SET_OPERATION(SMIME_ENCRYPT))
221 if (!SET_OPERATION(SMIME_DECRYPT))
225 if (!SET_OPERATION(SMIME_SIGN))
229 if (!SET_OPERATION(SMIME_RESIGN))
233 if (!SET_OPERATION(SMIME_VERIFY))
237 if (!SET_OPERATION(SMIME_PK7OUT))
244 flags |= PKCS7_NOINTERN;
247 flags |= PKCS7_NOVERIFY;
250 flags |= PKCS7_NOCHAIN;
253 flags |= PKCS7_NOCERTS;
256 flags |= PKCS7_NOATTR;
259 flags &= ~PKCS7_DETACHED;
262 flags |= PKCS7_NOSMIMECAP;
265 flags |= PKCS7_BINARY;
268 flags |= PKCS7_NOSIGS;
278 flags |= PKCS7_CRLFEOL;
286 if (!opt_provider(o))
290 conf = app_load_config_modules(opt_arg());
295 e = setup_engine(opt_arg(), 0);
298 passinarg = opt_arg();
310 /* If previous -signer argument add signer to list */
311 if (signerfile != NULL) {
312 if (sksigners == NULL
313 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
315 sk_OPENSSL_STRING_push(sksigners, signerfile);
317 keyfile = signerfile;
319 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
321 sk_OPENSSL_STRING_push(skkeys, keyfile);
324 signerfile = opt_arg();
327 recipfile = opt_arg();
330 digestname = opt_arg();
333 ciphername = opt_unknown();
336 /* If previous -inkey argument add signer to list */
337 if (keyfile != NULL) {
338 if (signerfile == NULL) {
340 "%s: Must have -signer before -inkey\n", prog);
343 if (sksigners == NULL
344 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
346 sk_OPENSSL_STRING_push(sksigners, signerfile);
349 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
351 sk_OPENSSL_STRING_push(skkeys, keyfile);
356 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
360 certfile = opt_arg();
381 contfile = opt_arg();
384 if (!opt_verify(o, vpm))
391 /* Extra arguments are files with recipient keys. */
392 argc = opt_num_rest();
395 if (!app_RAND_load())
398 if (digestname != NULL) {
399 if (!opt_md(digestname, &sign_md))
402 if (!opt_cipher_any(ciphername, &cipher))
404 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
405 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
410 "No operation (-encrypt|-sign|...) specified\n");
414 if (operation & SMIME_SIGNERS) {
415 /* Check to see if any final signer needs to be appended */
416 if (keyfile && !signerfile) {
417 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
420 if (signerfile != NULL) {
421 if (sksigners == NULL
422 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
424 sk_OPENSSL_STRING_push(sksigners, signerfile);
425 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
428 keyfile = signerfile;
429 sk_OPENSSL_STRING_push(skkeys, keyfile);
431 if (sksigners == NULL) {
432 BIO_printf(bio_err, "No signer certificate specified\n");
437 } else if (operation == SMIME_DECRYPT) {
438 if (recipfile == NULL && keyfile == NULL) {
440 "No recipient certificate or key specified\n");
443 } else if (operation == SMIME_ENCRYPT) {
445 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
450 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
451 BIO_printf(bio_err, "Error getting password\n");
457 if (!(operation & SMIME_SIGNERS))
458 flags &= ~PKCS7_DETACHED;
460 if (!(operation & SMIME_OP)) {
461 if (flags & PKCS7_BINARY)
462 outformat = FORMAT_BINARY;
465 if (!(operation & SMIME_IP)) {
466 if (flags & PKCS7_BINARY)
467 informat = FORMAT_BINARY;
470 if (operation == SMIME_ENCRYPT) {
471 if (cipher == NULL) {
472 #ifndef OPENSSL_NO_DES
473 cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
475 BIO_printf(bio_err, "No cipher selected\n");
479 encerts = sk_X509_new_null();
482 while (*argv != NULL) {
483 cert = load_cert(*argv, FORMAT_UNDEF,
484 "recipient certificate file");
487 if (!sk_X509_push(encerts, cert))
494 if (certfile != NULL) {
495 if (!load_certs(certfile, 0, &other, NULL, "certificates")) {
496 ERR_print_errors(bio_err);
501 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
502 if ((recip = load_cert(recipfile, FORMAT_UNDEF,
503 "recipient certificate file")) == NULL) {
504 ERR_print_errors(bio_err);
509 if (operation == SMIME_DECRYPT) {
512 } else if (operation == SMIME_SIGN) {
514 keyfile = signerfile;
519 if (keyfile != NULL) {
520 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
525 in = bio_open_default(infile, 'r', informat);
529 if (operation & SMIME_IP) {
532 p7 = PKCS7_new_ex(libctx, app_get0_propq());
534 BIO_printf(bio_err, "Error allocating PKCS7 object\n");
537 if (informat == FORMAT_SMIME) {
538 p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
539 } else if (informat == FORMAT_PEM) {
540 p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
541 } else if (informat == FORMAT_ASN1) {
542 p7_in = d2i_PKCS7_bio(in, &p7);
544 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
549 BIO_printf(bio_err, "Error reading S/MIME message\n");
552 if (contfile != NULL) {
554 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
555 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
561 out = bio_open_default(outfile, 'w', outformat);
565 if (operation == SMIME_VERIFY) {
566 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
567 CAstore, noCAstore)) == NULL)
569 X509_STORE_set_verify_cb(store, smime_cb);
571 X509_STORE_set1_param(store, vpm);
576 if (operation == SMIME_ENCRYPT) {
578 flags |= PKCS7_STREAM;
579 p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
580 } else if (operation & SMIME_SIGNERS) {
583 * If detached data content we only enable streaming if S/MIME output
586 if (operation == SMIME_SIGN) {
587 if (flags & PKCS7_DETACHED) {
588 if (outformat == FORMAT_SMIME)
589 flags |= PKCS7_STREAM;
591 flags |= PKCS7_STREAM;
593 flags |= PKCS7_PARTIAL;
594 p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
597 if (flags & PKCS7_NOCERTS) {
598 for (i = 0; i < sk_X509_num(other); i++) {
599 X509 *x = sk_X509_value(other, i);
600 PKCS7_add_certificate(p7, x);
604 flags |= PKCS7_REUSE_DIGEST;
606 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
607 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
608 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
609 signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
612 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
616 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
623 /* If not streaming or resigning finalize structure */
624 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
625 if (!PKCS7_final(p7, in, flags))
631 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
636 if (operation == SMIME_DECRYPT) {
637 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
638 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
641 } else if (operation == SMIME_VERIFY) {
642 STACK_OF(X509) *signers;
643 if (PKCS7_verify(p7, other, store, indata, out, flags))
644 BIO_printf(bio_err, "Verification successful\n");
646 BIO_printf(bio_err, "Verification failure\n");
649 signers = PKCS7_get0_signers(p7, other, flags);
650 if (!save_certs(signerfile, signers)) {
651 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
655 sk_X509_free(signers);
656 } else if (operation == SMIME_PK7OUT) {
657 PEM_write_bio_PKCS7(out, p7);
660 BIO_printf(out, "To: %s%s", to, mime_eol);
662 BIO_printf(out, "From: %s%s", from, mime_eol);
664 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
665 if (outformat == FORMAT_SMIME) {
666 if (operation == SMIME_RESIGN)
667 rv = SMIME_write_PKCS7(out, p7, indata, flags);
669 rv = SMIME_write_PKCS7(out, p7, in, flags);
670 } else if (outformat == FORMAT_PEM) {
671 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
672 } else if (outformat == FORMAT_ASN1) {
673 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
675 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
679 BIO_printf(bio_err, "Error writing output\n");
687 ERR_print_errors(bio_err);
688 OSSL_STACK_OF_X509_free(encerts);
689 OSSL_STACK_OF_X509_free(other);
690 X509_VERIFY_PARAM_free(vpm);
691 sk_OPENSSL_STRING_free(sksigners);
692 sk_OPENSSL_STRING_free(skkeys);
693 X509_STORE_free(store);
698 EVP_MD_free(sign_md);
699 EVP_CIPHER_free(cipher);
705 OPENSSL_free(passin);
710 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
715 if (signerfile == NULL)
717 tmp = BIO_new_file(signerfile, "w");
720 for (i = 0; i < sk_X509_num(signers); i++)
721 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
726 /* Minimal callback just to output policy info (if any) */
728 static int smime_cb(int ok, X509_STORE_CTX *ctx)
732 error = X509_STORE_CTX_get_error(ctx);
734 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
735 && ((error != X509_V_OK) || (ok != 2)))