2 * Copyright 1999-2018 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,
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.pem...\n"},
53 {OPT_HELP_STR, 1, '-',
54 " cert.pem... recipient certs for encryption\n"},
55 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
56 {"help", OPT_HELP, '-', "Display this summary"},
57 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
58 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
59 {"sign", OPT_SIGN, '-', "Sign message"},
60 {"verify", OPT_VERIFY, '-', "Verify signed message"},
61 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
62 {"nointern", OPT_NOINTERN, '-',
63 "Don't search certificates in message for signer"},
64 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
65 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
66 {"nocerts", OPT_NOCERTS, '-',
67 "Don't include signers certificate when signing"},
68 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
69 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
70 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
71 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
72 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
73 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
74 {"in", OPT_IN, '<', "Input file"},
75 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
76 {"inkey", OPT_INKEY, 's',
77 "Input private key (if not signer or recipient)"},
78 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
79 {"out", OPT_OUT, '>', "Output file"},
80 {"outform", OPT_OUTFORM, 'c',
81 "Output format SMIME (default), PEM or DER"},
82 {"content", OPT_CONTENT, '<',
83 "Supply or override content for detached signature"},
84 {"to", OPT_TO, 's', "To address"},
85 {"from", OPT_FROM, 's', "From address"},
86 {"subject", OPT_SUBJECT, 's', "Subject"},
87 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
88 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
89 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
90 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
91 {"no-CAfile", OPT_NOCAFILE, '-',
92 "Do not load the default certificates file"},
93 {"no-CApath", OPT_NOCAPATH, '-',
94 "Do not load certificates from the default certificates directory"},
95 {"no-CAstore", OPT_NOCASTORE, '-',
96 "Do not load certificates from the default certificates store"},
97 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
98 {"nochain", OPT_NOCHAIN, '-',
99 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
100 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
101 {"stream", OPT_STREAM, '-', "Enable CMS streaming" },
102 {"indef", OPT_INDEF, '-', "Same as -stream" },
103 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
104 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"},
106 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
107 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
108 {"", OPT_CIPHER, '-', "Any supported cipher"},
110 #ifndef OPENSSL_NO_ENGINE
111 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
116 int smime_main(int argc, char **argv)
118 BIO *in = NULL, *out = NULL, *indata = NULL;
119 EVP_PKEY *key = NULL;
121 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
122 STACK_OF(X509) *encerts = NULL, *other = NULL;
123 X509 *cert = NULL, *recip = NULL, *signer = NULL;
124 X509_STORE *store = NULL;
125 X509_VERIFY_PARAM *vpm = NULL;
126 const EVP_CIPHER *cipher = NULL;
127 const EVP_MD *sign_md = NULL;
128 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog = NULL;
129 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
130 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;
131 char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL, *subject = NULL;
133 int noCApath = 0, noCAfile = 0, noCAstore = 0;
134 int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
135 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
137 int vpmtouched = 0, rv = 0;
139 const char *mime_eol = "\n";
141 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
144 prog = opt_init(argc, argv, smime_options);
145 while ((o = opt_next()) != OPT_EOF) {
150 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
153 opt_help(smime_options);
157 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
164 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
171 operation = SMIME_ENCRYPT;
174 operation = SMIME_DECRYPT;
177 operation = SMIME_SIGN;
180 operation = SMIME_RESIGN;
183 operation = SMIME_VERIFY;
186 operation = SMIME_PK7OUT;
192 flags |= PKCS7_NOINTERN;
195 flags |= PKCS7_NOVERIFY;
198 flags |= PKCS7_NOCHAIN;
201 flags |= PKCS7_NOCERTS;
204 flags |= PKCS7_NOATTR;
207 flags &= ~PKCS7_DETACHED;
210 flags |= PKCS7_NOSMIMECAP;
213 flags |= PKCS7_BINARY;
216 flags |= PKCS7_NOSIGS;
226 flags |= PKCS7_CRLFEOL;
234 e = setup_engine(opt_arg(), 0);
237 passinarg = opt_arg();
249 /* If previous -signer argument add signer to list */
250 if (signerfile != NULL) {
251 if (sksigners == NULL
252 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
254 sk_OPENSSL_STRING_push(sksigners, signerfile);
256 keyfile = signerfile;
258 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
260 sk_OPENSSL_STRING_push(skkeys, keyfile);
263 signerfile = opt_arg();
266 recipfile = opt_arg();
269 if (!opt_md(opt_arg(), &sign_md))
273 if (!opt_cipher(opt_unknown(), &cipher))
277 /* If previous -inkey argument add signer to list */
278 if (keyfile != NULL) {
279 if (signerfile == NULL) {
281 "%s: Must have -signer before -inkey\n", prog);
284 if (sksigners == NULL
285 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
287 sk_OPENSSL_STRING_push(sksigners, signerfile);
290 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
292 sk_OPENSSL_STRING_push(skkeys, keyfile);
297 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
301 certfile = opt_arg();
322 contfile = opt_arg();
325 if (!opt_verify(o, vpm))
331 argc = opt_num_rest();
334 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
335 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
339 if (operation & SMIME_SIGNERS) {
340 /* Check to see if any final signer needs to be appended */
341 if (keyfile && !signerfile) {
342 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
345 if (signerfile != NULL) {
346 if (sksigners == NULL
347 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
349 sk_OPENSSL_STRING_push(sksigners, signerfile);
350 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
353 keyfile = signerfile;
354 sk_OPENSSL_STRING_push(skkeys, keyfile);
356 if (sksigners == NULL) {
357 BIO_printf(bio_err, "No signer certificate specified\n");
362 } else if (operation == SMIME_DECRYPT) {
363 if (recipfile == NULL && keyfile == NULL) {
365 "No recipient certificate or key specified\n");
368 } else if (operation == SMIME_ENCRYPT) {
370 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
373 } else if (!operation) {
377 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
378 BIO_printf(bio_err, "Error getting password\n");
384 if (!(operation & SMIME_SIGNERS))
385 flags &= ~PKCS7_DETACHED;
387 if (!(operation & SMIME_OP)) {
388 if (flags & PKCS7_BINARY)
389 outformat = FORMAT_BINARY;
392 if (!(operation & SMIME_IP)) {
393 if (flags & PKCS7_BINARY)
394 informat = FORMAT_BINARY;
397 if (operation == SMIME_ENCRYPT) {
398 if (cipher == NULL) {
399 #ifndef OPENSSL_NO_DES
400 cipher = EVP_des_ede3_cbc();
402 BIO_printf(bio_err, "No cipher selected\n");
406 encerts = sk_X509_new_null();
409 while (*argv != NULL) {
410 cert = load_cert(*argv, FORMAT_PEM,
411 "recipient certificate file");
414 sk_X509_push(encerts, cert);
420 if (certfile != NULL) {
421 if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
422 "certificate file")) {
423 ERR_print_errors(bio_err);
428 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
429 if ((recip = load_cert(recipfile, FORMAT_PEM,
430 "recipient certificate file")) == NULL) {
431 ERR_print_errors(bio_err);
436 if (operation == SMIME_DECRYPT) {
439 } else if (operation == SMIME_SIGN) {
441 keyfile = signerfile;
446 if (keyfile != NULL) {
447 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
452 in = bio_open_default(infile, 'r', informat);
456 if (operation & SMIME_IP) {
457 if (informat == FORMAT_SMIME) {
458 p7 = SMIME_read_PKCS7(in, &indata);
459 } else if (informat == FORMAT_PEM) {
460 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
461 } else if (informat == FORMAT_ASN1) {
462 p7 = d2i_PKCS7_bio(in, NULL);
464 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
469 BIO_printf(bio_err, "Error reading S/MIME message\n");
472 if (contfile != NULL) {
474 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
475 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
481 out = bio_open_default(outfile, 'w', outformat);
485 if (operation == SMIME_VERIFY) {
486 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
487 CAstore, noCAstore)) == 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 ERR_print_errors(bio_err);
608 sk_X509_pop_free(encerts, X509_free);
609 sk_X509_pop_free(other, X509_free);
610 X509_VERIFY_PARAM_free(vpm);
611 sk_OPENSSL_STRING_free(sksigners);
612 sk_OPENSSL_STRING_free(skkeys);
613 X509_STORE_free(store);
623 OPENSSL_free(passin);
627 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
632 if (signerfile == NULL)
634 tmp = BIO_new_file(signerfile, "w");
637 for (i = 0; i < sk_X509_num(signers); i++)
638 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
643 /* Minimal callback just to output policy info (if any) */
645 static int smime_cb(int ok, X509_STORE_CTX *ctx)
649 error = X509_STORE_CTX_get_error(ctx);
651 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
652 && ((error != X509_V_OK) || (ok != 2)))