2 * Copyright 2008-2019 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 /* CMS utility function */
17 #ifndef OPENSSL_NO_CMS
19 # include <openssl/crypto.h>
20 # include <openssl/pem.h>
21 # include <openssl/err.h>
22 # include <openssl/x509_vfy.h>
23 # include <openssl/x509v3.h>
24 # include <openssl/cms.h>
26 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
27 static int cms_cb(int ok, X509_STORE_CTX *ctx);
28 static void receipt_request_print(CMS_ContentInfo *cms);
29 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
30 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
32 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
33 STACK_OF(OPENSSL_STRING) *param);
35 # define SMIME_OP 0x10
36 # define SMIME_IP 0x20
37 # define SMIME_SIGNERS 0x40
38 # define SMIME_ENCRYPT (1 | SMIME_OP)
39 # define SMIME_DECRYPT (2 | SMIME_IP)
40 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
41 # define SMIME_VERIFY (4 | SMIME_IP)
42 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
43 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
44 # define SMIME_DATAOUT (7 | SMIME_IP)
45 # define SMIME_DATA_CREATE (8 | SMIME_OP)
46 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
47 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
48 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
49 # define SMIME_COMPRESS (12 | SMIME_OP)
50 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
51 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
52 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
53 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
55 static int verify_err = 0;
57 typedef struct cms_key_param_st cms_key_param;
59 struct cms_key_param_st {
61 STACK_OF(OPENSSL_STRING) *param;
65 typedef enum OPTION_choice {
66 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
67 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENCRYPT,
68 OPT_DECRYPT, OPT_SIGN, OPT_CADES, OPT_SIGN_RECEIPT, OPT_RESIGN,
69 OPT_VERIFY, OPT_VERIFY_RETCODE, OPT_VERIFY_RECEIPT,
70 OPT_CMSOUT, OPT_DATA_OUT, OPT_DATA_CREATE, OPT_DIGEST_VERIFY,
71 OPT_DIGEST_CREATE, OPT_COMPRESS, OPT_UNCOMPRESS,
72 OPT_ED_DECRYPT, OPT_ED_ENCRYPT, OPT_DEBUG_DECRYPT, OPT_TEXT,
73 OPT_ASCIICRLF, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCERTS,
74 OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP, OPT_BINARY, OPT_KEYID,
75 OPT_NOSIGS, OPT_NO_CONTENT_VERIFY, OPT_NO_ATTR_VERIFY, OPT_INDEF,
76 OPT_NOINDEF, OPT_CRLFEOL, OPT_NOOUT, OPT_RR_PRINT,
77 OPT_RR_ALL, OPT_RR_FIRST, OPT_RCTFORM, OPT_CERTFILE, OPT_CAFILE,
78 OPT_CAPATH, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
79 OPT_CONTENT, OPT_PRINT,
80 OPT_SECRETKEY, OPT_SECRETKEYID, OPT_PWRI_PASSWORD, OPT_ECONTENT_TYPE,
81 OPT_PASSIN, OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP,
82 OPT_CERTSOUT, OPT_MD, OPT_INKEY, OPT_KEYFORM, OPT_KEYOPT, OPT_RR_FROM,
83 OPT_RR_TO, OPT_AES128_WRAP, OPT_AES192_WRAP, OPT_AES256_WRAP,
84 OPT_3DES_WRAP, OPT_WRAP, OPT_ENGINE,
91 const OPTIONS cms_options[] = {
92 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
94 OPT_SECTION("General"),
95 {"help", OPT_HELP, '-', "Display this summary"},
96 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
97 {"outform", OPT_OUTFORM, 'c',
98 "Output format SMIME (default), PEM or DER"},
99 {"in", OPT_IN, '<', "Input file"},
100 {"out", OPT_OUT, '>', "Output file"},
101 {"debug_decrypt", OPT_DEBUG_DECRYPT, '-',
102 "Disable MMA protection and return an error if no recipient found"
103 " (see documentation)"},
104 {"stream", OPT_INDEF, '-', "Enable CMS streaming"},
105 {"indef", OPT_INDEF, '-', "Same as -stream"},
106 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
107 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only" },
108 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
109 {"CApath", OPT_CAPATH, '/', "trusted certificates directory"},
110 {"CAstore", OPT_CASTORE, ':', "trusted certificates store URI"},
111 {"no-CAfile", OPT_NOCAFILE, '-',
112 "Do not load the default certificates file"},
113 {"no-CApath", OPT_NOCAPATH, '-',
114 "Do not load certificates from the default certificates directory"},
115 {"no-CAstore", OPT_NOCASTORE, '-',
116 "Do not load certificates from the default certificates store"},
117 # ifndef OPENSSL_NO_ENGINE
118 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
121 OPT_SECTION("Action"),
122 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
123 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
124 {"sign", OPT_SIGN, '-', "Sign message"},
125 {"sign_receipt", OPT_SIGN_RECEIPT, '-', "Generate a signed receipt for the message"},
126 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
127 {"cades", OPT_CADES, '-', "Include signer certificate digest"},
128 {"verify", OPT_VERIFY, '-', "Verify signed message"},
129 {"verify_retcode", OPT_VERIFY_RETCODE, '-',
130 "Exit non-zero on verification failure"},
131 {"verify_receipt", OPT_VERIFY_RECEIPT, '<',
132 "Verify receipts; exit if receipt signatures do not verify"},
133 {"digest_verify", OPT_DIGEST_VERIFY, '-',
134 "Verify a CMS \"DigestedData\" object and output it"},
135 {"digest_create", OPT_DIGEST_CREATE, '-',
136 "Create a CMS \"DigestedData\" object"},
137 {"compress", OPT_COMPRESS, '-', "Create a CMS \"CompressedData\" object"},
138 {"uncompress", OPT_UNCOMPRESS, '-',
139 "Uncompress a CMS \"CompressedData\" object"},
140 {"EncryptedData_decrypt", OPT_ED_DECRYPT, '-',
141 "Decrypt CMS \"EncryptedData\" object using symmetric key"},
142 {"EncryptedData_encrypt", OPT_ED_ENCRYPT, '-',
143 "Create CMS \"EncryptedData\" object using symmetric key"},
144 {"data_out", OPT_DATA_OUT, '-', "Copy CMS \"Data\" object to output"},
145 {"data_create", OPT_DATA_CREATE, '-', "Create a CMS \"Data\" object"},
146 {"cmsout", OPT_CMSOUT, '-', "Output CMS structure"},
147 {"no_content_verify", OPT_NO_CONTENT_VERIFY, '-',
148 "Do not verify signed content signatures"},
149 {"no_attr_verify", OPT_NO_ATTR_VERIFY, '-',
150 "Do not verify signed attribute signatures"},
151 {"nointern", OPT_NOINTERN, '-',
152 "Don't search certificates in message for signer"},
153 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
155 OPT_SECTION("Formatting"),
156 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
157 {"asciicrlf", OPT_ASCIICRLF, '-',
158 "Perform CRLF canonicalisation when signing"},
159 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
160 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
161 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
162 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
163 {"keyid", OPT_KEYID, '-', "Use subject key identifier"},
164 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
165 {"nocerts", OPT_NOCERTS, '-',
166 "Don't include signers certificate when signing"},
167 {"noout", OPT_NOOUT, '-',
168 "For the -cmsout operation do not output the parsed CMS structure"},
169 {"receipt_request_print", OPT_RR_PRINT, '-', "Print CMS Receipt Request" },
170 {"receipt_request_all", OPT_RR_ALL, '-',
171 "When signing, create a receipt request for all recipients"},
172 {"receipt_request_first", OPT_RR_FIRST, '-',
173 "When signing, create a receipt request for first recipient"},
174 {"rctform", OPT_RCTFORM, 'F', "Receipt file format"},
175 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
176 {"content", OPT_CONTENT, '<',
177 "Supply or override content for detached signature"},
178 {"print", OPT_PRINT, '-',
179 "For the -cmsout operation print out all fields of the CMS structure"},
180 {"certsout", OPT_CERTSOUT, '>', "Certificate output file"},
182 OPT_SECTION("Keying"),
183 {"secretkey", OPT_SECRETKEY, 's',
184 "Use specified hex-encoded key to decrypt/encrypt recipients or content"},
185 {"secretkeyid", OPT_SECRETKEYID, 's',
186 "Identity of the -secretkey for CMS \"KEKRecipientInfo\" object"},
187 {"pwri_password", OPT_PWRI_PASSWORD, 's',
188 "Specific password for recipient"},
189 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
190 {"inkey", OPT_INKEY, 's',
191 "Input private key (if not signer or recipient)"},
192 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
193 {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"},
195 OPT_SECTION("Mail header"),
196 {"econtent_type", OPT_ECONTENT_TYPE, 's', "OID for external content"},
197 {"to", OPT_TO, 's', "To address"},
198 {"from", OPT_FROM, 's', "From address"},
199 {"subject", OPT_SUBJECT, 's', "Subject"},
200 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
201 {"originator", OPT_ORIGINATOR, 's', "Originator certificate file"},
202 {"recip", OPT_RECIP, '<', "Recipient cert file for decryption"},
203 {"receipt_request_from", OPT_RR_FROM, 's',
204 "Create signed receipt request with specified email address"},
205 {"receipt_request_to", OPT_RR_TO, 's',
206 "Create signed receipt targeted to specified address"},
208 OPT_SECTION("Encryption"),
209 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
210 {"", OPT_CIPHER, '-', "Any supported cipher"},
212 OPT_SECTION("Key-wrapping"),
213 {"aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key"},
214 {"aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key"},
215 {"aes256-wrap", OPT_AES256_WRAP, '-', "Use AES256 to wrap key"},
216 # ifndef OPENSSL_NO_DES
217 {"des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key"},
219 {"wrap", OPT_WRAP, 's', "Any wrap cipher to wrap key"},
225 {"cert", 0, 0, "Recipient certs (optional; used only when encrypting)"},
229 int cms_main(int argc, char **argv)
231 ASN1_OBJECT *econtent_type = NULL;
232 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
233 CMS_ContentInfo *cms = NULL, *rcms = NULL;
234 CMS_ReceiptRequest *rr = NULL;
236 EVP_PKEY *key = NULL;
237 const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
238 const EVP_MD *sign_md = NULL;
239 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
240 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
241 STACK_OF(X509) *encerts = NULL, *other = NULL;
242 X509 *cert = NULL, *recip = NULL, *signer = NULL, *originator = NULL;
243 X509_STORE *store = NULL;
244 X509_VERIFY_PARAM *vpm = NULL;
245 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
246 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
247 char *certsoutfile = NULL;
248 int noCAfile = 0, noCApath = 0, noCAstore = 0;
249 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
250 char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *originatorfile = NULL, *recipfile = NULL;
251 char *to = NULL, *from = NULL, *subject = NULL, *prog;
252 cms_key_param *key_first = NULL, *key_param = NULL;
253 int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched = 0;
254 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
255 int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;
256 int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
257 size_t secret_keylen = 0, secret_keyidlen = 0;
258 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
259 unsigned char *secret_key = NULL, *secret_keyid = NULL;
261 const char *mime_eol = "\n";
264 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
267 prog = opt_init(argc, argv, cms_options);
268 while ((o = opt_next()) != OPT_EOF) {
273 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
276 opt_help(cms_options);
280 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
284 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
291 operation = SMIME_ENCRYPT;
294 operation = SMIME_DECRYPT;
297 operation = SMIME_SIGN;
299 case OPT_SIGN_RECEIPT:
300 operation = SMIME_SIGN_RECEIPT;
303 operation = SMIME_RESIGN;
306 operation = SMIME_VERIFY;
308 case OPT_VERIFY_RETCODE:
311 case OPT_VERIFY_RECEIPT:
312 operation = SMIME_VERIFY_RECEIPT;
316 operation = SMIME_CMSOUT;
319 operation = SMIME_DATAOUT;
321 case OPT_DATA_CREATE:
322 operation = SMIME_DATA_CREATE;
324 case OPT_DIGEST_VERIFY:
325 operation = SMIME_DIGEST_VERIFY;
327 case OPT_DIGEST_CREATE:
328 operation = SMIME_DIGEST_CREATE;
331 operation = SMIME_COMPRESS;
334 operation = SMIME_UNCOMPRESS;
337 operation = SMIME_ENCRYPTED_DECRYPT;
340 operation = SMIME_ENCRYPTED_ENCRYPT;
342 case OPT_DEBUG_DECRYPT:
343 flags |= CMS_DEBUG_DECRYPT;
349 flags |= CMS_ASCIICRLF;
352 flags |= CMS_NOINTERN;
355 flags |= CMS_NO_SIGNER_CERT_VERIFY;
358 flags |= CMS_NOCERTS;
364 flags &= ~CMS_DETACHED;
367 flags |= CMS_NOSMIMECAP;
376 flags |= CMS_USE_KEYID;
381 case OPT_NO_CONTENT_VERIFY:
382 flags |= CMS_NO_CONTENT_VERIFY;
384 case OPT_NO_ATTR_VERIFY:
385 flags |= CMS_NO_ATTR_VERIFY;
391 flags &= ~CMS_STREAM;
395 flags |= CMS_CRLFEOL;
410 if (rctformat == FORMAT_SMIME)
411 rcms = SMIME_read_CMS(rctin, NULL);
412 else if (rctformat == FORMAT_PEM)
413 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
414 else if (rctformat == FORMAT_ASN1)
415 if (!opt_format(opt_arg(),
416 OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
420 certfile = opt_arg();
444 contfile = opt_arg();
448 && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
450 sk_OPENSSL_STRING_push(rr_from, opt_arg());
454 && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
456 sk_OPENSSL_STRING_push(rr_to, opt_arg());
462 if (secret_key != NULL) {
463 BIO_printf(bio_err, "Invalid key (supplied twice) %s\n",
467 secret_key = OPENSSL_hexstr2buf(opt_arg(), <mp);
468 if (secret_key == NULL) {
469 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
472 secret_keylen = (size_t)ltmp;
474 case OPT_SECRETKEYID:
475 if (secret_keyid != NULL) {
476 BIO_printf(bio_err, "Invalid id (supplied twice) %s\n",
480 secret_keyid = OPENSSL_hexstr2buf(opt_arg(), <mp);
481 if (secret_keyid == NULL) {
482 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
485 secret_keyidlen = (size_t)ltmp;
487 case OPT_PWRI_PASSWORD:
488 pwri_pass = (unsigned char *)opt_arg();
490 case OPT_ECONTENT_TYPE:
491 if (econtent_type != NULL) {
492 BIO_printf(bio_err, "Invalid OID (supplied twice) %s\n",
496 econtent_type = OBJ_txt2obj(opt_arg(), 0);
497 if (econtent_type == NULL) {
498 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
503 e = setup_engine(opt_arg(), 0);
506 passinarg = opt_arg();
518 certsoutfile = opt_arg();
521 if (!opt_md(opt_arg(), &sign_md))
525 /* If previous -signer argument add signer to list */
526 if (signerfile != NULL) {
527 if (sksigners == NULL
528 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
530 sk_OPENSSL_STRING_push(sksigners, signerfile);
532 keyfile = signerfile;
534 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
536 sk_OPENSSL_STRING_push(skkeys, keyfile);
539 signerfile = opt_arg();
542 originatorfile = opt_arg();
545 /* If previous -inkey argument add signer to list */
546 if (keyfile != NULL) {
547 if (signerfile == NULL) {
548 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
551 if (sksigners == NULL
552 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
554 sk_OPENSSL_STRING_push(sksigners, signerfile);
557 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
559 sk_OPENSSL_STRING_push(skkeys, keyfile);
564 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
568 if (operation == SMIME_ENCRYPT) {
569 if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)
571 cert = load_cert(opt_arg(), FORMAT_PEM,
572 "recipient certificate file");
575 sk_X509_push(encerts, cert);
578 recipfile = opt_arg();
582 if (!opt_cipher(opt_unknown(), &cipher))
587 if (operation == SMIME_ENCRYPT) {
589 keyidx += sk_X509_num(encerts);
591 if (keyfile != NULL || signerfile != NULL)
594 keyidx += sk_OPENSSL_STRING_num(skkeys);
597 BIO_printf(bio_err, "No key specified\n");
600 if (key_param == NULL || key_param->idx != keyidx) {
601 cms_key_param *nparam;
602 nparam = app_malloc(sizeof(*nparam), "key param buffer");
603 nparam->idx = keyidx;
604 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
607 if (key_first == NULL)
610 key_param->next = nparam;
613 sk_OPENSSL_STRING_push(key_param->param, opt_arg());
616 if (!opt_verify(o, vpm))
625 # ifndef OPENSSL_NO_DES
626 wrap_cipher = EVP_des_ede3_wrap();
629 case OPT_AES128_WRAP:
630 wrap_cipher = EVP_aes_128_wrap();
632 case OPT_AES192_WRAP:
633 wrap_cipher = EVP_aes_192_wrap();
635 case OPT_AES256_WRAP:
636 wrap_cipher = EVP_aes_256_wrap();
639 if (!opt_cipher(opt_unknown(), &wrap_cipher))
644 argc = opt_num_rest();
647 if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {
648 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
652 if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {
653 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
656 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
657 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
661 if (flags & CMS_CADES) {
662 if (flags & CMS_NOATTR) {
663 BIO_puts(bio_err, "Incompatible options: "
664 "CAdES required signed attributes\n");
669 if (operation & SMIME_SIGNERS) {
670 if (keyfile != NULL && signerfile == NULL) {
671 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
674 /* Check to see if any final signer needs to be appended */
675 if (signerfile != NULL) {
676 if (sksigners == NULL
677 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
679 sk_OPENSSL_STRING_push(sksigners, signerfile);
680 if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
683 keyfile = signerfile;
684 sk_OPENSSL_STRING_push(skkeys, keyfile);
686 if (sksigners == NULL) {
687 BIO_printf(bio_err, "No signer certificate specified\n");
692 } else if (operation == SMIME_DECRYPT) {
693 if (recipfile == NULL && keyfile == NULL
694 && secret_key == NULL && pwri_pass == NULL) {
696 "No recipient certificate or key specified\n");
699 } else if (operation == SMIME_ENCRYPT) {
700 if (*argv == NULL && secret_key == NULL
701 && pwri_pass == NULL && encerts == NULL) {
702 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
705 } else if (!operation) {
706 BIO_printf(bio_err, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\n");
710 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
711 BIO_printf(bio_err, "Error getting password\n");
717 if (!(operation & SMIME_SIGNERS))
718 flags &= ~CMS_DETACHED;
720 if (!(operation & SMIME_OP))
721 if (flags & CMS_BINARY)
722 outformat = FORMAT_BINARY;
724 if (!(operation & SMIME_IP))
725 if (flags & CMS_BINARY)
726 informat = FORMAT_BINARY;
728 if (operation == SMIME_ENCRYPT) {
730 # ifndef OPENSSL_NO_DES
731 cipher = EVP_des_ede3_cbc();
733 BIO_printf(bio_err, "No cipher selected\n");
738 if (secret_key && !secret_keyid) {
739 BIO_printf(bio_err, "No secret key id\n");
743 if (*argv && encerts == NULL)
744 if ((encerts = sk_X509_new_null()) == NULL)
747 if ((cert = load_cert(*argv, FORMAT_PEM,
748 "recipient certificate file")) == NULL)
750 sk_X509_push(encerts, cert);
756 if (certfile != NULL) {
757 if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
758 "certificate file")) {
759 ERR_print_errors(bio_err);
764 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
765 if ((recip = load_cert(recipfile, FORMAT_PEM,
766 "recipient certificate file")) == NULL) {
767 ERR_print_errors(bio_err);
772 if (originatorfile != NULL) {
773 if ((originator = load_cert(originatorfile, FORMAT_PEM,
774 "originator certificate file")) == NULL) {
775 ERR_print_errors(bio_err);
780 if (operation == SMIME_SIGN_RECEIPT) {
781 if ((signer = load_cert(signerfile, FORMAT_PEM,
782 "receipt signer certificate file")) == NULL) {
783 ERR_print_errors(bio_err);
788 if ((operation == SMIME_DECRYPT) || (operation == SMIME_ENCRYPT)) {
791 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
793 keyfile = signerfile;
798 if (keyfile != NULL) {
799 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
804 in = bio_open_default(infile, 'r', informat);
808 if (operation & SMIME_IP) {
809 if (informat == FORMAT_SMIME) {
810 cms = SMIME_read_CMS(in, &indata);
811 } else if (informat == FORMAT_PEM) {
812 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
813 } else if (informat == FORMAT_ASN1) {
814 cms = d2i_CMS_bio(in, NULL);
816 BIO_printf(bio_err, "Bad input format for CMS file\n");
821 BIO_printf(bio_err, "Error reading S/MIME message\n");
824 if (contfile != NULL) {
826 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
827 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
831 if (certsoutfile != NULL) {
832 STACK_OF(X509) *allcerts;
833 allcerts = CMS_get1_certs(cms);
834 if (!save_certs(certsoutfile, allcerts)) {
836 "Error writing certs to %s\n", certsoutfile);
840 sk_X509_pop_free(allcerts, X509_free);
844 if (rctfile != NULL) {
845 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
846 if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
847 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
851 if (rctformat == FORMAT_SMIME) {
852 rcms = SMIME_read_CMS(rctin, NULL);
853 } else if (rctformat == FORMAT_PEM) {
854 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
855 } else if (rctformat == FORMAT_ASN1) {
856 rcms = d2i_CMS_bio(rctin, NULL);
858 BIO_printf(bio_err, "Bad input format for receipt\n");
863 BIO_printf(bio_err, "Error reading receipt\n");
868 out = bio_open_default(outfile, 'w', outformat);
872 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
873 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
874 CAstore, noCAstore)) == NULL)
876 X509_STORE_set_verify_cb(store, cms_cb);
878 X509_STORE_set1_param(store, vpm);
883 if (operation == SMIME_DATA_CREATE) {
884 cms = CMS_data_create(in, flags);
885 } else if (operation == SMIME_DIGEST_CREATE) {
886 cms = CMS_digest_create(in, sign_md, flags);
887 } else if (operation == SMIME_COMPRESS) {
888 cms = CMS_compress(in, -1, flags);
889 } else if (operation == SMIME_ENCRYPT) {
891 flags |= CMS_PARTIAL;
892 cms = CMS_encrypt(NULL, in, cipher, flags);
895 for (i = 0; i < sk_X509_num(encerts); i++) {
896 CMS_RecipientInfo *ri;
897 cms_key_param *kparam;
898 int tflags = flags | CMS_KEY_PARAM; /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */
900 X509 *x = sk_X509_value(encerts, i);
903 for (kparam = key_first; kparam; kparam = kparam->next) {
904 if (kparam->idx == i) {
908 ri = CMS_add1_recipient(cms, x, key, originator, tflags);
912 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
913 if (kparam != NULL) {
914 if (!cms_set_pkey_param(pctx, kparam->param))
918 res = EVP_PKEY_CTX_ctrl(pctx, -1, -1,
919 EVP_PKEY_CTRL_CIPHER,
920 EVP_CIPHER_nid(cipher), NULL);
921 if (res <= 0 && res != -2)
924 if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
926 EVP_CIPHER_CTX *wctx;
927 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
928 EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
932 if (secret_key != NULL) {
933 if (!CMS_add0_recipient_key(cms, NID_undef,
934 secret_key, secret_keylen,
935 secret_keyid, secret_keyidlen,
938 /* NULL these because call absorbs them */
942 if (pwri_pass != NULL) {
943 pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
944 if (pwri_tmp == NULL)
946 if (CMS_add0_recipient_password(cms,
947 -1, NID_undef, NID_undef,
948 pwri_tmp, -1, NULL) == NULL)
952 if (!(flags & CMS_STREAM)) {
953 if (!CMS_final(cms, in, NULL, flags))
956 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
957 cms = CMS_EncryptedData_encrypt(in, cipher,
958 secret_key, secret_keylen, flags);
960 } else if (operation == SMIME_SIGN_RECEIPT) {
961 CMS_ContentInfo *srcms = NULL;
962 STACK_OF(CMS_SignerInfo) *sis;
964 sis = CMS_get0_SignerInfos(cms);
967 si = sk_CMS_SignerInfo_value(sis, 0);
968 srcms = CMS_sign_receipt(si, signer, key, other, flags);
971 CMS_ContentInfo_free(cms);
973 } else if (operation & SMIME_SIGNERS) {
976 * If detached data content we enable streaming if S/MIME output
979 if (operation == SMIME_SIGN) {
981 if (flags & CMS_DETACHED) {
982 if (outformat == FORMAT_SMIME)
985 flags |= CMS_PARTIAL;
986 cms = CMS_sign(NULL, NULL, other, in, flags);
989 if (econtent_type != NULL)
990 CMS_set1_eContentType(cms, econtent_type);
993 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
996 "Signed Receipt Request Creation Error\n");
1001 flags |= CMS_REUSE_DIGEST;
1003 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
1005 cms_key_param *kparam;
1007 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
1008 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
1010 signer = load_cert(signerfile, FORMAT_PEM, "signer certificate");
1011 if (signer == NULL) {
1015 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
1020 for (kparam = key_first; kparam; kparam = kparam->next) {
1021 if (kparam->idx == i) {
1022 tflags |= CMS_KEY_PARAM;
1026 si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
1029 if (kparam != NULL) {
1031 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
1032 if (!cms_set_pkey_param(pctx, kparam->param))
1035 if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
1042 /* If not streaming or resigning finalize structure */
1043 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
1044 if (!CMS_final(cms, in, NULL, flags))
1050 BIO_printf(bio_err, "Error creating CMS structure\n");
1055 if (operation == SMIME_DECRYPT) {
1056 if (flags & CMS_DEBUG_DECRYPT)
1057 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
1059 if (secret_key != NULL) {
1060 if (!CMS_decrypt_set1_key(cms,
1061 secret_key, secret_keylen,
1062 secret_keyid, secret_keyidlen)) {
1063 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
1069 if (!CMS_decrypt_set1_pkey_and_peer(cms, key, recip, originator)) {
1070 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
1075 if (pwri_pass != NULL) {
1076 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
1077 BIO_puts(bio_err, "Error decrypting CMS using password\n");
1082 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
1083 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1086 } else if (operation == SMIME_DATAOUT) {
1087 if (!CMS_data(cms, out, flags))
1089 } else if (operation == SMIME_UNCOMPRESS) {
1090 if (!CMS_uncompress(cms, indata, out, flags))
1092 } else if (operation == SMIME_DIGEST_VERIFY) {
1093 if (CMS_digest_verify(cms, indata, out, flags) > 0) {
1094 BIO_printf(bio_err, "Verification successful\n");
1096 BIO_printf(bio_err, "Verification failure\n");
1099 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
1100 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1101 indata, out, flags))
1103 } else if (operation == SMIME_VERIFY) {
1104 if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
1105 BIO_printf(bio_err, "Verification successful\n");
1107 BIO_printf(bio_err, "Verification failure\n");
1109 ret = verify_err + 32;
1112 if (signerfile != NULL) {
1113 STACK_OF(X509) *signers;
1114 signers = CMS_get0_signers(cms);
1115 if (!save_certs(signerfile, signers)) {
1117 "Error writing signers to %s\n", signerfile);
1121 sk_X509_free(signers);
1124 receipt_request_print(cms);
1126 } else if (operation == SMIME_VERIFY_RECEIPT) {
1127 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
1128 BIO_printf(bio_err, "Verification successful\n");
1130 BIO_printf(bio_err, "Verification failure\n");
1136 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1137 } else if (outformat == FORMAT_SMIME) {
1139 BIO_printf(out, "To: %s%s", to, mime_eol);
1141 BIO_printf(out, "From: %s%s", from, mime_eol);
1143 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
1144 if (operation == SMIME_RESIGN)
1145 ret = SMIME_write_CMS(out, cms, indata, flags);
1147 ret = SMIME_write_CMS(out, cms, in, flags);
1148 } else if (outformat == FORMAT_PEM) {
1149 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1150 } else if (outformat == FORMAT_ASN1) {
1151 ret = i2d_CMS_bio_stream(out, cms, in, flags);
1153 BIO_printf(bio_err, "Bad output format for CMS file\n");
1164 ERR_print_errors(bio_err);
1165 sk_X509_pop_free(encerts, X509_free);
1166 sk_X509_pop_free(other, X509_free);
1167 X509_VERIFY_PARAM_free(vpm);
1168 sk_OPENSSL_STRING_free(sksigners);
1169 sk_OPENSSL_STRING_free(skkeys);
1170 OPENSSL_free(secret_key);
1171 OPENSSL_free(secret_keyid);
1172 OPENSSL_free(pwri_tmp);
1173 ASN1_OBJECT_free(econtent_type);
1174 CMS_ReceiptRequest_free(rr);
1175 sk_OPENSSL_STRING_free(rr_to);
1176 sk_OPENSSL_STRING_free(rr_from);
1177 for (key_param = key_first; key_param;) {
1178 cms_key_param *tparam;
1179 sk_OPENSSL_STRING_free(key_param->param);
1180 tparam = key_param->next;
1181 OPENSSL_free(key_param);
1184 X509_STORE_free(store);
1189 CMS_ContentInfo_free(cms);
1190 CMS_ContentInfo_free(rcms);
1196 OPENSSL_free(passin);
1200 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1204 if (signerfile == NULL)
1206 tmp = BIO_new_file(signerfile, "w");
1209 for (i = 0; i < sk_X509_num(signers); i++)
1210 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1215 /* Minimal callback just to output policy info (if any) */
1217 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1221 error = X509_STORE_CTX_get_error(ctx);
1225 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1226 && ((error != X509_V_OK) || (ok != 2)))
1229 policies_print(ctx);
1235 static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
1237 STACK_OF(GENERAL_NAME) *gens;
1241 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1242 gens = sk_GENERAL_NAMES_value(gns, i);
1243 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1244 gen = sk_GENERAL_NAME_value(gens, j);
1245 BIO_puts(bio_err, " ");
1246 GENERAL_NAME_print(bio_err, gen);
1247 BIO_puts(bio_err, "\n");
1253 static void receipt_request_print(CMS_ContentInfo *cms)
1255 STACK_OF(CMS_SignerInfo) *sis;
1257 CMS_ReceiptRequest *rr;
1259 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1262 sis = CMS_get0_SignerInfos(cms);
1263 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1264 si = sk_CMS_SignerInfo_value(sis, i);
1265 rv = CMS_get1_ReceiptRequest(si, &rr);
1266 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1268 BIO_puts(bio_err, " No Receipt Request\n");
1269 } else if (rv < 0) {
1270 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1271 ERR_print_errors(bio_err);
1275 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1277 BIO_puts(bio_err, " Signed Content ID:\n");
1278 idlen = ASN1_STRING_length(scid);
1279 id = (const char *)ASN1_STRING_get0_data(scid);
1280 BIO_dump_indent(bio_err, id, idlen, 4);
1281 BIO_puts(bio_err, " Receipts From");
1282 if (rlist != NULL) {
1283 BIO_puts(bio_err, " List:\n");
1284 gnames_stack_print(rlist);
1285 } else if (allorfirst == 1) {
1286 BIO_puts(bio_err, ": First Tier\n");
1287 } else if (allorfirst == 0) {
1288 BIO_puts(bio_err, ": All\n");
1290 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
1292 BIO_puts(bio_err, " Receipts To:\n");
1293 gnames_stack_print(rto);
1295 CMS_ReceiptRequest_free(rr);
1299 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1302 STACK_OF(GENERAL_NAMES) *ret;
1303 GENERAL_NAMES *gens = NULL;
1304 GENERAL_NAME *gen = NULL;
1305 ret = sk_GENERAL_NAMES_new_null();
1308 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1309 char *str = sk_OPENSSL_STRING_value(ns, i);
1310 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1313 gens = GENERAL_NAMES_new();
1316 if (!sk_GENERAL_NAME_push(gens, gen))
1319 if (!sk_GENERAL_NAMES_push(ret, gens))
1327 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1328 GENERAL_NAMES_free(gens);
1329 GENERAL_NAME_free(gen);
1333 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
1334 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
1337 STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
1338 CMS_ReceiptRequest *rr;
1339 rct_to = make_names_stack(rr_to);
1342 if (rr_from != NULL) {
1343 rct_from = make_names_stack(rr_from);
1344 if (rct_from == NULL)
1349 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1353 sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
1357 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
1358 STACK_OF(OPENSSL_STRING) *param)
1362 if (sk_OPENSSL_STRING_num(param) <= 0)
1364 for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
1365 keyopt = sk_OPENSSL_STRING_value(param, i);
1366 if (pkey_ctrl_string(pctx, keyopt) <= 0) {
1367 BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
1368 ERR_print_errors(bio_err);