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_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,
45 OPT_CAPATH, OPT_NOCAFILE, OPT_NOCAPATH, OPT_IN, OPT_INFORM, OPT_OUT,
46 OPT_OUTFORM, OPT_CONTENT
49 const OPTIONS smime_options[] = {
50 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
51 {OPT_HELP_STR, 1, '-',
52 " cert.pem... recipient certs for encryption\n"},
53 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
54 {"help", OPT_HELP, '-', "Display this summary"},
55 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
56 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
57 {"sign", OPT_SIGN, '-', "Sign message"},
58 {"verify", OPT_VERIFY, '-', "Verify signed message"},
59 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
60 {"nointern", OPT_NOINTERN, '-',
61 "Don't search certificates in message for signer"},
62 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
63 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
64 {"nocerts", OPT_NOCERTS, '-',
65 "Don't include signers certificate when signing"},
66 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
67 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
68 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
69 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
70 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
71 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
72 {"in", OPT_IN, '<', "Input file"},
73 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
74 {"inkey", OPT_INKEY, 's',
75 "Input private key (if not signer or recipient)"},
76 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
77 {"out", OPT_OUT, '>', "Output file"},
78 {"outform", OPT_OUTFORM, 'c',
79 "Output format SMIME (default), PEM or DER"},
80 {"content", OPT_CONTENT, '<',
81 "Supply or override content for detached signature"},
82 {"to", OPT_TO, 's', "To address"},
83 {"from", OPT_FROM, 's', "From address"},
84 {"subject", OPT_SUBJECT, 's', "Subject"},
85 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
86 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
87 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
88 {"no-CAfile", OPT_NOCAFILE, '-',
89 "Do not load the default certificates file"},
90 {"no-CApath", OPT_NOCAPATH, '-',
91 "Do not load certificates from the default certificates directory"},
92 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
93 {"nochain", OPT_NOCHAIN, '-',
94 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
95 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
96 {"stream", OPT_STREAM, '-', "Enable CMS streaming" },
97 {"indef", OPT_INDEF, '-', "Same as -stream" },
98 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
99 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"},
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;
125 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;
126 char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL, *subject = NULL;
128 int noCApath = 0, noCAfile = 0;
129 int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
130 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
132 int vpmtouched = 0, rv = 0;
134 const char *mime_eol = "\n";
136 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
139 prog = opt_init(argc, argv, smime_options);
140 while ((o = opt_next()) != OPT_EOF) {
145 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
148 opt_help(smime_options);
152 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
159 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
166 operation = SMIME_ENCRYPT;
169 operation = SMIME_DECRYPT;
172 operation = SMIME_SIGN;
175 operation = SMIME_RESIGN;
178 operation = SMIME_VERIFY;
181 operation = SMIME_PK7OUT;
187 flags |= PKCS7_NOINTERN;
190 flags |= PKCS7_NOVERIFY;
193 flags |= PKCS7_NOCHAIN;
196 flags |= PKCS7_NOCERTS;
199 flags |= PKCS7_NOATTR;
202 flags &= ~PKCS7_DETACHED;
205 flags |= PKCS7_NOSMIMECAP;
208 flags |= PKCS7_BINARY;
211 flags |= PKCS7_NOSIGS;
221 flags |= PKCS7_CRLFEOL;
229 e = setup_engine(opt_arg(), 0);
232 passinarg = opt_arg();
244 /* If previous -signer argument add signer to list */
245 if (signerfile != NULL) {
246 if (sksigners == NULL
247 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
249 sk_OPENSSL_STRING_push(sksigners, signerfile);
251 keyfile = signerfile;
253 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
255 sk_OPENSSL_STRING_push(skkeys, keyfile);
258 signerfile = opt_arg();
261 recipfile = opt_arg();
264 if (!opt_md(opt_arg(), &sign_md))
268 if (!opt_cipher(opt_unknown(), &cipher))
272 /* If previous -inkey argument add signer to list */
273 if (keyfile != NULL) {
274 if (signerfile == NULL) {
276 "%s: Must have -signer before -inkey\n", prog);
279 if (sksigners == NULL
280 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
282 sk_OPENSSL_STRING_push(sksigners, signerfile);
285 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
287 sk_OPENSSL_STRING_push(skkeys, keyfile);
292 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
296 certfile = opt_arg();
311 contfile = opt_arg();
314 if (!opt_verify(o, vpm))
320 argc = opt_num_rest();
323 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
324 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
328 if (operation & SMIME_SIGNERS) {
329 /* Check to see if any final signer needs to be appended */
330 if (keyfile && !signerfile) {
331 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
334 if (signerfile != NULL) {
335 if (sksigners == NULL
336 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
338 sk_OPENSSL_STRING_push(sksigners, signerfile);
339 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
342 keyfile = signerfile;
343 sk_OPENSSL_STRING_push(skkeys, keyfile);
345 if (sksigners == NULL) {
346 BIO_printf(bio_err, "No signer certificate specified\n");
351 } else if (operation == SMIME_DECRYPT) {
352 if (recipfile == NULL && keyfile == NULL) {
354 "No recipient certificate or key specified\n");
357 } else if (operation == SMIME_ENCRYPT) {
359 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
362 } else if (!operation) {
366 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
367 BIO_printf(bio_err, "Error getting password\n");
373 if (!(operation & SMIME_SIGNERS))
374 flags &= ~PKCS7_DETACHED;
376 if (!(operation & SMIME_OP)) {
377 if (flags & PKCS7_BINARY)
378 outformat = FORMAT_BINARY;
381 if (!(operation & SMIME_IP)) {
382 if (flags & PKCS7_BINARY)
383 informat = FORMAT_BINARY;
386 if (operation == SMIME_ENCRYPT) {
387 if (cipher == NULL) {
388 #ifndef OPENSSL_NO_DES
389 cipher = EVP_des_ede3_cbc();
391 BIO_printf(bio_err, "No cipher selected\n");
395 encerts = sk_X509_new_null();
398 while (*argv != NULL) {
399 cert = load_cert(*argv, FORMAT_PEM,
400 "recipient certificate file");
403 sk_X509_push(encerts, cert);
409 if (certfile != NULL) {
410 if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
411 "certificate file")) {
412 ERR_print_errors(bio_err);
417 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
418 if ((recip = load_cert(recipfile, FORMAT_PEM,
419 "recipient certificate file")) == NULL) {
420 ERR_print_errors(bio_err);
425 if (operation == SMIME_DECRYPT) {
428 } else if (operation == SMIME_SIGN) {
430 keyfile = signerfile;
435 if (keyfile != NULL) {
436 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
441 in = bio_open_default(infile, 'r', informat);
445 if (operation & SMIME_IP) {
446 if (informat == FORMAT_SMIME) {
447 p7 = SMIME_read_PKCS7(in, &indata);
448 } else if (informat == FORMAT_PEM) {
449 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
450 } else if (informat == FORMAT_ASN1) {
451 p7 = d2i_PKCS7_bio(in, NULL);
453 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
458 BIO_printf(bio_err, "Error reading S/MIME message\n");
461 if (contfile != NULL) {
463 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
464 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
470 out = bio_open_default(outfile, 'w', outformat);
474 if (operation == SMIME_VERIFY) {
475 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
477 X509_STORE_set_verify_cb(store, smime_cb);
479 X509_STORE_set1_param(store, vpm);
484 if (operation == SMIME_ENCRYPT) {
486 flags |= PKCS7_STREAM;
487 p7 = PKCS7_encrypt(encerts, in, cipher, flags);
488 } else if (operation & SMIME_SIGNERS) {
491 * If detached data content we only enable streaming if S/MIME output
494 if (operation == SMIME_SIGN) {
495 if (flags & PKCS7_DETACHED) {
496 if (outformat == FORMAT_SMIME)
497 flags |= PKCS7_STREAM;
499 flags |= PKCS7_STREAM;
501 flags |= PKCS7_PARTIAL;
502 p7 = PKCS7_sign(NULL, NULL, other, in, flags);
505 if (flags & PKCS7_NOCERTS) {
506 for (i = 0; i < sk_X509_num(other); i++) {
507 X509 *x = sk_X509_value(other, i);
508 PKCS7_add_certificate(p7, x);
512 flags |= PKCS7_REUSE_DIGEST;
514 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
515 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
516 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
517 signer = load_cert(signerfile, FORMAT_PEM,
518 "signer certificate");
521 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
524 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
531 /* If not streaming or resigning finalize structure */
532 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
533 if (!PKCS7_final(p7, in, flags))
539 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
544 if (operation == SMIME_DECRYPT) {
545 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
546 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
549 } else if (operation == SMIME_VERIFY) {
550 STACK_OF(X509) *signers;
551 if (PKCS7_verify(p7, other, store, indata, out, flags))
552 BIO_printf(bio_err, "Verification successful\n");
554 BIO_printf(bio_err, "Verification failure\n");
557 signers = PKCS7_get0_signers(p7, other, flags);
558 if (!save_certs(signerfile, signers)) {
559 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
563 sk_X509_free(signers);
564 } else if (operation == SMIME_PK7OUT) {
565 PEM_write_bio_PKCS7(out, p7);
568 BIO_printf(out, "To: %s%s", to, mime_eol);
570 BIO_printf(out, "From: %s%s", from, mime_eol);
572 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
573 if (outformat == FORMAT_SMIME) {
574 if (operation == SMIME_RESIGN)
575 rv = SMIME_write_PKCS7(out, p7, indata, flags);
577 rv = SMIME_write_PKCS7(out, p7, in, flags);
578 } else if (outformat == FORMAT_PEM) {
579 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
580 } else if (outformat == FORMAT_ASN1) {
581 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
583 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
587 BIO_printf(bio_err, "Error writing output\n");
595 ERR_print_errors(bio_err);
596 sk_X509_pop_free(encerts, X509_free);
597 sk_X509_pop_free(other, X509_free);
598 X509_VERIFY_PARAM_free(vpm);
599 sk_OPENSSL_STRING_free(sksigners);
600 sk_OPENSSL_STRING_free(skkeys);
601 X509_STORE_free(store);
611 OPENSSL_free(passin);
615 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
620 if (signerfile == NULL)
622 tmp = BIO_new_file(signerfile, "w");
625 for (i = 0; i < sk_X509_num(signers); i++)
626 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
631 /* Minimal callback just to output policy info (if any) */
633 static int smime_cb(int ok, X509_STORE_CTX *ctx)
637 error = X509_STORE_CTX_get_error(ctx);
639 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
640 && ((error != X509_V_OK) || (ok != 2)))