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_NOCAPATH, OPT_NOCAFILE,OPT_CONTENT, OPT_PRINT,
79 OPT_SECRETKEY, OPT_SECRETKEYID, OPT_PWRI_PASSWORD, OPT_ECONTENT_TYPE,
80 OPT_PASSIN, OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP,
81 OPT_CERTSOUT, OPT_MD, OPT_INKEY, OPT_KEYFORM, OPT_KEYOPT, OPT_RR_FROM,
82 OPT_RR_TO, OPT_AES128_WRAP, OPT_AES192_WRAP, OPT_AES256_WRAP,
83 OPT_3DES_WRAP, OPT_ENGINE,
89 const OPTIONS cms_options[] = {
90 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
91 {OPT_HELP_STR, 1, '-',
92 " cert.pem... recipient certs for encryption\n"},
93 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
94 {"help", OPT_HELP, '-', "Display this summary"},
95 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
96 {"outform", OPT_OUTFORM, 'c',
97 "Output format SMIME (default), PEM or DER"},
98 {"in", OPT_IN, '<', "Input file"},
99 {"out", OPT_OUT, '>', "Output file"},
100 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
101 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
102 {"sign", OPT_SIGN, '-', "Sign message"},
103 {"sign_receipt", OPT_SIGN_RECEIPT, '-', "Generate a signed receipt for the message"},
104 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
105 {"cades", OPT_CADES, '-', "Include signer certificate digest"},
106 {"verify", OPT_VERIFY, '-', "Verify signed message"},
107 {"verify_retcode", OPT_VERIFY_RETCODE, '-'},
108 {"verify_receipt", OPT_VERIFY_RECEIPT, '<'},
109 {"cmsout", OPT_CMSOUT, '-', "Output CMS structure"},
110 {"data_out", OPT_DATA_OUT, '-'},
111 {"data_create", OPT_DATA_CREATE, '-'},
112 {"digest_verify", OPT_DIGEST_VERIFY, '-'},
113 {"digest_create", OPT_DIGEST_CREATE, '-'},
114 {"compress", OPT_COMPRESS, '-'},
115 {"uncompress", OPT_UNCOMPRESS, '-'},
116 {"EncryptedData_decrypt", OPT_ED_DECRYPT, '-'},
117 {"EncryptedData_encrypt", OPT_ED_ENCRYPT, '-'},
118 {"debug_decrypt", OPT_DEBUG_DECRYPT, '-'},
119 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
120 {"asciicrlf", OPT_ASCIICRLF, '-'},
121 {"nointern", OPT_NOINTERN, '-',
122 "Don't search certificates in message for signer"},
123 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
124 {"nocerts", OPT_NOCERTS, '-',
125 "Don't include signers certificate when signing"},
126 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
127 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
128 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
129 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
130 {"keyid", OPT_KEYID, '-', "Use subject key identifier"},
131 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
132 {"no_content_verify", OPT_NO_CONTENT_VERIFY, '-'},
133 {"no_attr_verify", OPT_NO_ATTR_VERIFY, '-'},
134 {"stream", OPT_INDEF, '-', "Enable CMS streaming"},
135 {"indef", OPT_INDEF, '-', "Same as -stream"},
136 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
137 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only" },
138 {"noout", OPT_NOOUT, '-', "For the -cmsout operation do not output the parsed CMS structure"},
139 {"receipt_request_print", OPT_RR_PRINT, '-', "Print CMS Receipt Request" },
140 {"receipt_request_all", OPT_RR_ALL, '-'},
141 {"receipt_request_first", OPT_RR_FIRST, '-'},
142 {"rctform", OPT_RCTFORM, 'F', "Receipt file format"},
143 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
144 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
145 {"CApath", OPT_CAPATH, '/', "trusted certificates directory"},
146 {"no-CAfile", OPT_NOCAFILE, '-',
147 "Do not load the default certificates file"},
148 {"no-CApath", OPT_NOCAPATH, '-',
149 "Do not load certificates from the default certificates directory"},
150 {"content", OPT_CONTENT, '<',
151 "Supply or override content for detached signature"},
152 {"print", OPT_PRINT, '-',
153 "For the -cmsout operation print out all fields of the CMS structure"},
154 {"secretkey", OPT_SECRETKEY, 's'},
155 {"secretkeyid", OPT_SECRETKEYID, 's'},
156 {"pwri_password", OPT_PWRI_PASSWORD, 's'},
157 {"econtent_type", OPT_ECONTENT_TYPE, 's'},
158 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
159 {"to", OPT_TO, 's', "To address"},
160 {"from", OPT_FROM, 's', "From address"},
161 {"subject", OPT_SUBJECT, 's', "Subject"},
162 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
163 {"recip", OPT_RECIP, '<', "Recipient cert file for decryption"},
164 {"certsout", OPT_CERTSOUT, '>', "Certificate output file"},
165 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
166 {"inkey", OPT_INKEY, 's',
167 "Input private key (if not signer or recipient)"},
168 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
169 {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"},
170 {"receipt_request_from", OPT_RR_FROM, 's'},
171 {"receipt_request_to", OPT_RR_TO, 's'},
172 {"", OPT_CIPHER, '-', "Any supported cipher"},
175 {"aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key"},
176 {"aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key"},
177 {"aes256-wrap", OPT_AES256_WRAP, '-', "Use AES256 to wrap key"},
178 # ifndef OPENSSL_NO_DES
179 {"des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key"},
181 # ifndef OPENSSL_NO_ENGINE
182 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
187 int cms_main(int argc, char **argv)
189 ASN1_OBJECT *econtent_type = NULL;
190 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
191 CMS_ContentInfo *cms = NULL, *rcms = NULL;
192 CMS_ReceiptRequest *rr = NULL;
194 EVP_PKEY *key = NULL;
195 const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
196 const EVP_MD *sign_md = NULL;
197 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
198 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
199 STACK_OF(X509) *encerts = NULL, *other = NULL;
200 X509 *cert = NULL, *recip = NULL, *signer = NULL;
201 X509_STORE *store = NULL;
202 X509_VERIFY_PARAM *vpm = NULL;
203 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
204 const char *CAfile = NULL, *CApath = NULL;
205 char *certsoutfile = NULL;
206 int noCAfile = 0, noCApath = 0;
207 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
208 char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile = NULL;
209 char *to = NULL, *from = NULL, *subject = NULL, *prog;
210 cms_key_param *key_first = NULL, *key_param = NULL;
211 int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched = 0;
212 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
213 int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;
214 int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
215 size_t secret_keylen = 0, secret_keyidlen = 0;
216 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
217 unsigned char *secret_key = NULL, *secret_keyid = NULL;
219 const char *mime_eol = "\n";
222 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
225 prog = opt_init(argc, argv, cms_options);
226 while ((o = opt_next()) != OPT_EOF) {
231 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
234 opt_help(cms_options);
238 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
242 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
249 operation = SMIME_ENCRYPT;
252 operation = SMIME_DECRYPT;
255 operation = SMIME_SIGN;
257 case OPT_SIGN_RECEIPT:
258 operation = SMIME_SIGN_RECEIPT;
261 operation = SMIME_RESIGN;
264 operation = SMIME_VERIFY;
266 case OPT_VERIFY_RETCODE:
269 case OPT_VERIFY_RECEIPT:
270 operation = SMIME_VERIFY_RECEIPT;
274 operation = SMIME_CMSOUT;
277 operation = SMIME_DATAOUT;
279 case OPT_DATA_CREATE:
280 operation = SMIME_DATA_CREATE;
282 case OPT_DIGEST_VERIFY:
283 operation = SMIME_DIGEST_VERIFY;
285 case OPT_DIGEST_CREATE:
286 operation = SMIME_DIGEST_CREATE;
289 operation = SMIME_COMPRESS;
292 operation = SMIME_UNCOMPRESS;
295 operation = SMIME_ENCRYPTED_DECRYPT;
298 operation = SMIME_ENCRYPTED_ENCRYPT;
300 case OPT_DEBUG_DECRYPT:
301 flags |= CMS_DEBUG_DECRYPT;
307 flags |= CMS_ASCIICRLF;
310 flags |= CMS_NOINTERN;
313 flags |= CMS_NO_SIGNER_CERT_VERIFY;
316 flags |= CMS_NOCERTS;
322 flags &= ~CMS_DETACHED;
325 flags |= CMS_NOSMIMECAP;
334 flags |= CMS_USE_KEYID;
339 case OPT_NO_CONTENT_VERIFY:
340 flags |= CMS_NO_CONTENT_VERIFY;
342 case OPT_NO_ATTR_VERIFY:
343 flags |= CMS_NO_ATTR_VERIFY;
349 flags &= ~CMS_STREAM;
353 flags |= CMS_CRLFEOL;
368 if (rctformat == FORMAT_SMIME)
369 rcms = SMIME_read_CMS(rctin, NULL);
370 else if (rctformat == FORMAT_PEM)
371 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
372 else if (rctformat == FORMAT_ASN1)
373 if (!opt_format(opt_arg(),
374 OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
378 certfile = opt_arg();
396 contfile = opt_arg();
400 && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
402 sk_OPENSSL_STRING_push(rr_from, opt_arg());
406 && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
408 sk_OPENSSL_STRING_push(rr_to, opt_arg());
414 if (secret_key != NULL) {
415 BIO_printf(bio_err, "Invalid key (supplied twice) %s\n",
419 secret_key = OPENSSL_hexstr2buf(opt_arg(), <mp);
420 if (secret_key == NULL) {
421 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
424 secret_keylen = (size_t)ltmp;
426 case OPT_SECRETKEYID:
427 if (secret_keyid != NULL) {
428 BIO_printf(bio_err, "Invalid id (supplied twice) %s\n",
432 secret_keyid = OPENSSL_hexstr2buf(opt_arg(), <mp);
433 if (secret_keyid == NULL) {
434 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
437 secret_keyidlen = (size_t)ltmp;
439 case OPT_PWRI_PASSWORD:
440 pwri_pass = (unsigned char *)opt_arg();
442 case OPT_ECONTENT_TYPE:
443 if (econtent_type != NULL) {
444 BIO_printf(bio_err, "Invalid OID (supplied twice) %s\n",
448 econtent_type = OBJ_txt2obj(opt_arg(), 0);
449 if (econtent_type == NULL) {
450 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
455 e = setup_engine(opt_arg(), 0);
458 passinarg = opt_arg();
470 certsoutfile = opt_arg();
473 if (!opt_md(opt_arg(), &sign_md))
477 /* If previous -signer argument add signer to list */
478 if (signerfile != NULL) {
479 if (sksigners == NULL
480 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
482 sk_OPENSSL_STRING_push(sksigners, signerfile);
484 keyfile = signerfile;
486 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
488 sk_OPENSSL_STRING_push(skkeys, keyfile);
491 signerfile = opt_arg();
494 /* If previous -inkey argument add signer to list */
495 if (keyfile != NULL) {
496 if (signerfile == NULL) {
497 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
500 if (sksigners == NULL
501 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
503 sk_OPENSSL_STRING_push(sksigners, signerfile);
506 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
508 sk_OPENSSL_STRING_push(skkeys, keyfile);
513 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
517 if (operation == SMIME_ENCRYPT) {
518 if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)
520 cert = load_cert(opt_arg(), FORMAT_PEM,
521 "recipient certificate file");
524 sk_X509_push(encerts, cert);
527 recipfile = opt_arg();
531 if (!opt_cipher(opt_unknown(), &cipher))
536 if (operation == SMIME_ENCRYPT) {
538 keyidx += sk_X509_num(encerts);
540 if (keyfile != NULL || signerfile != NULL)
543 keyidx += sk_OPENSSL_STRING_num(skkeys);
546 BIO_printf(bio_err, "No key specified\n");
549 if (key_param == NULL || key_param->idx != keyidx) {
550 cms_key_param *nparam;
551 nparam = app_malloc(sizeof(*nparam), "key param buffer");
552 nparam->idx = keyidx;
553 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
556 if (key_first == NULL)
559 key_param->next = nparam;
562 sk_OPENSSL_STRING_push(key_param->param, opt_arg());
565 if (!opt_verify(o, vpm))
574 # ifndef OPENSSL_NO_DES
575 wrap_cipher = EVP_des_ede3_wrap();
578 case OPT_AES128_WRAP:
579 wrap_cipher = EVP_aes_128_wrap();
581 case OPT_AES192_WRAP:
582 wrap_cipher = EVP_aes_192_wrap();
584 case OPT_AES256_WRAP:
585 wrap_cipher = EVP_aes_256_wrap();
589 argc = opt_num_rest();
592 if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {
593 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
597 if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {
598 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
601 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
602 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
606 if (flags & CMS_CADES) {
607 if (flags & CMS_NOATTR) {
608 BIO_puts(bio_err, "Incompatible options: "
609 "CAdES required signed attributes\n");
614 if (operation & SMIME_SIGNERS) {
615 if (keyfile != NULL && signerfile == NULL) {
616 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
619 /* Check to see if any final signer needs to be appended */
620 if (signerfile != NULL) {
621 if (sksigners == NULL
622 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
624 sk_OPENSSL_STRING_push(sksigners, signerfile);
625 if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
628 keyfile = signerfile;
629 sk_OPENSSL_STRING_push(skkeys, keyfile);
631 if (sksigners == NULL) {
632 BIO_printf(bio_err, "No signer certificate specified\n");
637 } else if (operation == SMIME_DECRYPT) {
638 if (recipfile == NULL && keyfile == NULL
639 && secret_key == NULL && pwri_pass == NULL) {
641 "No recipient certificate or key specified\n");
644 } else if (operation == SMIME_ENCRYPT) {
645 if (*argv == NULL && secret_key == NULL
646 && pwri_pass == NULL && encerts == NULL) {
647 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
650 } else if (!operation) {
651 BIO_printf(bio_err, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\n");
655 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
656 BIO_printf(bio_err, "Error getting password\n");
662 if (!(operation & SMIME_SIGNERS))
663 flags &= ~CMS_DETACHED;
665 if (!(operation & SMIME_OP))
666 if (flags & CMS_BINARY)
667 outformat = FORMAT_BINARY;
669 if (!(operation & SMIME_IP))
670 if (flags & CMS_BINARY)
671 informat = FORMAT_BINARY;
673 if (operation == SMIME_ENCRYPT) {
675 # ifndef OPENSSL_NO_DES
676 cipher = EVP_des_ede3_cbc();
678 BIO_printf(bio_err, "No cipher selected\n");
683 if (secret_key && !secret_keyid) {
684 BIO_printf(bio_err, "No secret key id\n");
688 if (*argv && encerts == NULL)
689 if ((encerts = sk_X509_new_null()) == NULL)
692 if ((cert = load_cert(*argv, FORMAT_PEM,
693 "recipient certificate file")) == NULL)
695 sk_X509_push(encerts, cert);
701 if (certfile != NULL) {
702 if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
703 "certificate file")) {
704 ERR_print_errors(bio_err);
709 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
710 if ((recip = load_cert(recipfile, FORMAT_PEM,
711 "recipient certificate file")) == NULL) {
712 ERR_print_errors(bio_err);
717 if (operation == SMIME_SIGN_RECEIPT) {
718 if ((signer = load_cert(signerfile, FORMAT_PEM,
719 "receipt signer certificate file")) == NULL) {
720 ERR_print_errors(bio_err);
725 if (operation == SMIME_DECRYPT) {
728 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
730 keyfile = signerfile;
735 if (keyfile != NULL) {
736 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
741 in = bio_open_default(infile, 'r', informat);
745 if (operation & SMIME_IP) {
746 if (informat == FORMAT_SMIME) {
747 cms = SMIME_read_CMS(in, &indata);
748 } else if (informat == FORMAT_PEM) {
749 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
750 } else if (informat == FORMAT_ASN1) {
751 cms = d2i_CMS_bio(in, NULL);
753 BIO_printf(bio_err, "Bad input format for CMS file\n");
758 BIO_printf(bio_err, "Error reading S/MIME message\n");
761 if (contfile != NULL) {
763 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
764 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
768 if (certsoutfile != NULL) {
769 STACK_OF(X509) *allcerts;
770 allcerts = CMS_get1_certs(cms);
771 if (!save_certs(certsoutfile, allcerts)) {
773 "Error writing certs to %s\n", certsoutfile);
777 sk_X509_pop_free(allcerts, X509_free);
781 if (rctfile != NULL) {
782 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
783 if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
784 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
788 if (rctformat == FORMAT_SMIME) {
789 rcms = SMIME_read_CMS(rctin, NULL);
790 } else if (rctformat == FORMAT_PEM) {
791 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
792 } else if (rctformat == FORMAT_ASN1) {
793 rcms = d2i_CMS_bio(rctin, NULL);
795 BIO_printf(bio_err, "Bad input format for receipt\n");
800 BIO_printf(bio_err, "Error reading receipt\n");
805 out = bio_open_default(outfile, 'w', outformat);
809 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
810 if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)
812 X509_STORE_set_verify_cb(store, cms_cb);
814 X509_STORE_set1_param(store, vpm);
819 if (operation == SMIME_DATA_CREATE) {
820 cms = CMS_data_create(in, flags);
821 } else if (operation == SMIME_DIGEST_CREATE) {
822 cms = CMS_digest_create(in, sign_md, flags);
823 } else if (operation == SMIME_COMPRESS) {
824 cms = CMS_compress(in, -1, flags);
825 } else if (operation == SMIME_ENCRYPT) {
827 flags |= CMS_PARTIAL;
828 cms = CMS_encrypt(NULL, in, cipher, flags);
831 for (i = 0; i < sk_X509_num(encerts); i++) {
832 CMS_RecipientInfo *ri;
833 cms_key_param *kparam;
835 X509 *x = sk_X509_value(encerts, i);
836 for (kparam = key_first; kparam; kparam = kparam->next) {
837 if (kparam->idx == i) {
838 tflags |= CMS_KEY_PARAM;
842 ri = CMS_add1_recipient_cert(cms, x, tflags);
845 if (kparam != NULL) {
847 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
848 if (!cms_set_pkey_param(pctx, kparam->param))
851 if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
853 EVP_CIPHER_CTX *wctx;
854 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
855 EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
859 if (secret_key != NULL) {
860 if (!CMS_add0_recipient_key(cms, NID_undef,
861 secret_key, secret_keylen,
862 secret_keyid, secret_keyidlen,
865 /* NULL these because call absorbs them */
869 if (pwri_pass != NULL) {
870 pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
871 if (pwri_tmp == NULL)
873 if (CMS_add0_recipient_password(cms,
874 -1, NID_undef, NID_undef,
875 pwri_tmp, -1, NULL) == NULL)
879 if (!(flags & CMS_STREAM)) {
880 if (!CMS_final(cms, in, NULL, flags))
883 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
884 cms = CMS_EncryptedData_encrypt(in, cipher,
885 secret_key, secret_keylen, flags);
887 } else if (operation == SMIME_SIGN_RECEIPT) {
888 CMS_ContentInfo *srcms = NULL;
889 STACK_OF(CMS_SignerInfo) *sis;
891 sis = CMS_get0_SignerInfos(cms);
894 si = sk_CMS_SignerInfo_value(sis, 0);
895 srcms = CMS_sign_receipt(si, signer, key, other, flags);
898 CMS_ContentInfo_free(cms);
900 } else if (operation & SMIME_SIGNERS) {
903 * If detached data content we enable streaming if S/MIME output
906 if (operation == SMIME_SIGN) {
908 if (flags & CMS_DETACHED) {
909 if (outformat == FORMAT_SMIME)
912 flags |= CMS_PARTIAL;
913 cms = CMS_sign(NULL, NULL, other, in, flags);
916 if (econtent_type != NULL)
917 CMS_set1_eContentType(cms, econtent_type);
920 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
923 "Signed Receipt Request Creation Error\n");
928 flags |= CMS_REUSE_DIGEST;
930 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
932 cms_key_param *kparam;
934 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
935 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
937 signer = load_cert(signerfile, FORMAT_PEM, "signer certificate");
938 if (signer == NULL) {
942 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
947 for (kparam = key_first; kparam; kparam = kparam->next) {
948 if (kparam->idx == i) {
949 tflags |= CMS_KEY_PARAM;
953 si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
956 if (kparam != NULL) {
958 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
959 if (!cms_set_pkey_param(pctx, kparam->param))
962 if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
969 /* If not streaming or resigning finalize structure */
970 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
971 if (!CMS_final(cms, in, NULL, flags))
977 BIO_printf(bio_err, "Error creating CMS structure\n");
982 if (operation == SMIME_DECRYPT) {
983 if (flags & CMS_DEBUG_DECRYPT)
984 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
986 if (secret_key != NULL) {
987 if (!CMS_decrypt_set1_key(cms,
988 secret_key, secret_keylen,
989 secret_keyid, secret_keyidlen)) {
990 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
996 if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
997 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
1002 if (pwri_pass != NULL) {
1003 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
1004 BIO_puts(bio_err, "Error decrypting CMS using password\n");
1009 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
1010 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1013 } else if (operation == SMIME_DATAOUT) {
1014 if (!CMS_data(cms, out, flags))
1016 } else if (operation == SMIME_UNCOMPRESS) {
1017 if (!CMS_uncompress(cms, indata, out, flags))
1019 } else if (operation == SMIME_DIGEST_VERIFY) {
1020 if (CMS_digest_verify(cms, indata, out, flags) > 0) {
1021 BIO_printf(bio_err, "Verification successful\n");
1023 BIO_printf(bio_err, "Verification failure\n");
1026 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
1027 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1028 indata, out, flags))
1030 } else if (operation == SMIME_VERIFY) {
1031 if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
1032 BIO_printf(bio_err, "Verification successful\n");
1034 BIO_printf(bio_err, "Verification failure\n");
1036 ret = verify_err + 32;
1039 if (signerfile != NULL) {
1040 STACK_OF(X509) *signers;
1041 signers = CMS_get0_signers(cms);
1042 if (!save_certs(signerfile, signers)) {
1044 "Error writing signers to %s\n", signerfile);
1048 sk_X509_free(signers);
1051 receipt_request_print(cms);
1053 } else if (operation == SMIME_VERIFY_RECEIPT) {
1054 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
1055 BIO_printf(bio_err, "Verification successful\n");
1057 BIO_printf(bio_err, "Verification failure\n");
1063 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1064 } else if (outformat == FORMAT_SMIME) {
1066 BIO_printf(out, "To: %s%s", to, mime_eol);
1068 BIO_printf(out, "From: %s%s", from, mime_eol);
1070 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
1071 if (operation == SMIME_RESIGN)
1072 ret = SMIME_write_CMS(out, cms, indata, flags);
1074 ret = SMIME_write_CMS(out, cms, in, flags);
1075 } else if (outformat == FORMAT_PEM) {
1076 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1077 } else if (outformat == FORMAT_ASN1) {
1078 ret = i2d_CMS_bio_stream(out, cms, in, flags);
1080 BIO_printf(bio_err, "Bad output format for CMS file\n");
1091 ERR_print_errors(bio_err);
1092 sk_X509_pop_free(encerts, X509_free);
1093 sk_X509_pop_free(other, X509_free);
1094 X509_VERIFY_PARAM_free(vpm);
1095 sk_OPENSSL_STRING_free(sksigners);
1096 sk_OPENSSL_STRING_free(skkeys);
1097 OPENSSL_free(secret_key);
1098 OPENSSL_free(secret_keyid);
1099 OPENSSL_free(pwri_tmp);
1100 ASN1_OBJECT_free(econtent_type);
1101 CMS_ReceiptRequest_free(rr);
1102 sk_OPENSSL_STRING_free(rr_to);
1103 sk_OPENSSL_STRING_free(rr_from);
1104 for (key_param = key_first; key_param;) {
1105 cms_key_param *tparam;
1106 sk_OPENSSL_STRING_free(key_param->param);
1107 tparam = key_param->next;
1108 OPENSSL_free(key_param);
1111 X509_STORE_free(store);
1116 CMS_ContentInfo_free(cms);
1117 CMS_ContentInfo_free(rcms);
1123 OPENSSL_free(passin);
1127 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1131 if (signerfile == NULL)
1133 tmp = BIO_new_file(signerfile, "w");
1136 for (i = 0; i < sk_X509_num(signers); i++)
1137 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1142 /* Minimal callback just to output policy info (if any) */
1144 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1148 error = X509_STORE_CTX_get_error(ctx);
1152 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1153 && ((error != X509_V_OK) || (ok != 2)))
1156 policies_print(ctx);
1162 static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
1164 STACK_OF(GENERAL_NAME) *gens;
1168 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1169 gens = sk_GENERAL_NAMES_value(gns, i);
1170 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1171 gen = sk_GENERAL_NAME_value(gens, j);
1172 BIO_puts(bio_err, " ");
1173 GENERAL_NAME_print(bio_err, gen);
1174 BIO_puts(bio_err, "\n");
1180 static void receipt_request_print(CMS_ContentInfo *cms)
1182 STACK_OF(CMS_SignerInfo) *sis;
1184 CMS_ReceiptRequest *rr;
1186 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1189 sis = CMS_get0_SignerInfos(cms);
1190 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1191 si = sk_CMS_SignerInfo_value(sis, i);
1192 rv = CMS_get1_ReceiptRequest(si, &rr);
1193 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1195 BIO_puts(bio_err, " No Receipt Request\n");
1196 } else if (rv < 0) {
1197 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1198 ERR_print_errors(bio_err);
1202 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1204 BIO_puts(bio_err, " Signed Content ID:\n");
1205 idlen = ASN1_STRING_length(scid);
1206 id = (const char *)ASN1_STRING_get0_data(scid);
1207 BIO_dump_indent(bio_err, id, idlen, 4);
1208 BIO_puts(bio_err, " Receipts From");
1209 if (rlist != NULL) {
1210 BIO_puts(bio_err, " List:\n");
1211 gnames_stack_print(rlist);
1212 } else if (allorfirst == 1) {
1213 BIO_puts(bio_err, ": First Tier\n");
1214 } else if (allorfirst == 0) {
1215 BIO_puts(bio_err, ": All\n");
1217 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
1219 BIO_puts(bio_err, " Receipts To:\n");
1220 gnames_stack_print(rto);
1222 CMS_ReceiptRequest_free(rr);
1226 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1229 STACK_OF(GENERAL_NAMES) *ret;
1230 GENERAL_NAMES *gens = NULL;
1231 GENERAL_NAME *gen = NULL;
1232 ret = sk_GENERAL_NAMES_new_null();
1235 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1236 char *str = sk_OPENSSL_STRING_value(ns, i);
1237 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1240 gens = GENERAL_NAMES_new();
1243 if (!sk_GENERAL_NAME_push(gens, gen))
1246 if (!sk_GENERAL_NAMES_push(ret, gens))
1254 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1255 GENERAL_NAMES_free(gens);
1256 GENERAL_NAME_free(gen);
1260 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
1261 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
1264 STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
1265 CMS_ReceiptRequest *rr;
1266 rct_to = make_names_stack(rr_to);
1269 if (rr_from != NULL) {
1270 rct_from = make_names_stack(rr_from);
1271 if (rct_from == NULL)
1276 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1280 sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
1284 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
1285 STACK_OF(OPENSSL_STRING) *param)
1289 if (sk_OPENSSL_STRING_num(param) <= 0)
1291 for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
1292 keyopt = sk_OPENSSL_STRING_value(param, i);
1293 if (pkey_ctrl_string(pctx, keyopt) <= 0) {
1294 BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
1295 ERR_print_errors(bio_err);