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_ENGINE,
90 const OPTIONS cms_options[] = {
91 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
92 {OPT_HELP_STR, 1, '-',
93 " cert.pem... recipient certs for encryption\n"},
94 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
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 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
102 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
103 {"sign", OPT_SIGN, '-', "Sign message"},
104 {"sign_receipt", OPT_SIGN_RECEIPT, '-', "Generate a signed receipt for the message"},
105 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
106 {"cades", OPT_CADES, '-', "Include signer certificate digest"},
107 {"verify", OPT_VERIFY, '-', "Verify signed message"},
108 {"verify_retcode", OPT_VERIFY_RETCODE, '-',
109 "Exit non-zero on verification failure"},
110 {"verify_receipt", OPT_VERIFY_RECEIPT, '<',
111 "Verify receipts; exit if receipt signatures do not verify"},
112 {"cmsout", OPT_CMSOUT, '-', "Output CMS structure"},
113 {"data_out", OPT_DATA_OUT, '-', "Copy CMS \"Data\" object to output"},
114 {"data_create", OPT_DATA_CREATE, '-', "Create a CMS \"Data\" object"},
115 {"digest_verify", OPT_DIGEST_VERIFY, '-',
116 "Verify a CMS \"DigestedData\" object and output it"},
117 {"digest_create", OPT_DIGEST_CREATE, '-',
118 "Create a CMS \"DigestedData\" object"},
119 {"compress", OPT_COMPRESS, '-', "Create a CMS \"CompressedData\" object"},
120 {"uncompress", OPT_UNCOMPRESS, '-', "Uncompress a CMS \"CompressedData\" object"},
121 {"EncryptedData_decrypt", OPT_ED_DECRYPT, '-',
122 "Decrypt CMS \"EncryptedData\" object using symmetric key"},
123 {"EncryptedData_encrypt", OPT_ED_ENCRYPT, '-',
124 "Create CMS \"EncryptedData\" object using symmetric key"},
125 {"debug_decrypt", OPT_DEBUG_DECRYPT, '-',
126 "Disable MMA protection and return an error if no recipient found"
127 " (see documentation)"},
128 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
129 {"asciicrlf", OPT_ASCIICRLF, '-',
130 "Perform CRLF canonicalisation when signing"},
131 {"nointern", OPT_NOINTERN, '-',
132 "Don't search certificates in message for signer"},
133 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
134 {"nocerts", OPT_NOCERTS, '-',
135 "Don't include signers certificate when signing"},
136 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
137 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
138 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
139 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
140 {"keyid", OPT_KEYID, '-', "Use subject key identifier"},
141 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
142 {"no_content_verify", OPT_NO_CONTENT_VERIFY, '-',
143 "Do not verify signed content signatures"},
144 {"no_attr_verify", OPT_NO_ATTR_VERIFY, '-',
145 "Do not verify signed attribute signatures"},
146 {"stream", OPT_INDEF, '-', "Enable CMS streaming"},
147 {"indef", OPT_INDEF, '-', "Same as -stream"},
148 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
149 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only" },
150 {"noout", OPT_NOOUT, '-', "For the -cmsout operation do not output the parsed CMS structure"},
151 {"receipt_request_print", OPT_RR_PRINT, '-', "Print CMS Receipt Request" },
152 {"receipt_request_all", OPT_RR_ALL, '-',
153 "When signing, create a receipt request for all recipients"},
154 {"receipt_request_first", OPT_RR_FIRST, '-',
155 "When signing, create a receipt request for first recipient"},
156 {"rctform", OPT_RCTFORM, 'F', "Receipt file format"},
157 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
158 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
159 {"CApath", OPT_CAPATH, '/', "trusted certificates directory"},
160 {"CAstore", OPT_CASTORE, ':', "trusted certificates store URI"},
161 {"no-CAfile", OPT_NOCAFILE, '-',
162 "Do not load the default certificates file"},
163 {"no-CApath", OPT_NOCAPATH, '-',
164 "Do not load certificates from the default certificates directory"},
165 {"no-CAstore", OPT_NOCASTORE, '-',
166 "Do not load certificates from the default certificates store"},
167 {"content", OPT_CONTENT, '<',
168 "Supply or override content for detached signature"},
169 {"print", OPT_PRINT, '-',
170 "For the -cmsout operation print out all fields of the CMS structure"},
171 {"secretkey", OPT_SECRETKEY, 's',
172 "Use specified hex-encoded key to decrypt/encrypt recipients or content"},
173 {"secretkeyid", OPT_SECRETKEYID, 's',
174 "Identity of the -secretkey for CMS \"KEKRecipientInfo\" object"},
175 {"pwri_password", OPT_PWRI_PASSWORD, 's',
176 "Specific password for recipient"},
177 {"econtent_type", OPT_ECONTENT_TYPE, 's', "OID for external content"},
178 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
179 {"to", OPT_TO, 's', "To address"},
180 {"from", OPT_FROM, 's', "From address"},
181 {"subject", OPT_SUBJECT, 's', "Subject"},
182 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
183 {"recip", OPT_RECIP, '<', "Recipient cert file for decryption"},
184 {"certsout", OPT_CERTSOUT, '>', "Certificate output file"},
185 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
186 {"inkey", OPT_INKEY, 's',
187 "Input private key (if not signer or recipient)"},
188 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
189 {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"},
190 {"receipt_request_from", OPT_RR_FROM, 's',
191 "Create signed receipt request with specified email address"},
192 {"receipt_request_to", OPT_RR_TO, 's',
193 "Create signed receipt targeted to specified address"},
194 {"", OPT_CIPHER, '-', "Any supported cipher"},
197 {"aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key"},
198 {"aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key"},
199 {"aes256-wrap", OPT_AES256_WRAP, '-', "Use AES256 to wrap key"},
200 # ifndef OPENSSL_NO_DES
201 {"des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key"},
203 # ifndef OPENSSL_NO_ENGINE
204 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
209 int cms_main(int argc, char **argv)
211 ASN1_OBJECT *econtent_type = NULL;
212 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
213 CMS_ContentInfo *cms = NULL, *rcms = NULL;
214 CMS_ReceiptRequest *rr = NULL;
216 EVP_PKEY *key = NULL;
217 const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
218 const EVP_MD *sign_md = NULL;
219 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
220 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
221 STACK_OF(X509) *encerts = NULL, *other = NULL;
222 X509 *cert = NULL, *recip = NULL, *signer = NULL;
223 X509_STORE *store = NULL;
224 X509_VERIFY_PARAM *vpm = NULL;
225 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
226 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
227 char *certsoutfile = NULL;
228 int noCAfile = 0, noCApath = 0, noCAstore = 0;
229 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
230 char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile = NULL;
231 char *to = NULL, *from = NULL, *subject = NULL, *prog;
232 cms_key_param *key_first = NULL, *key_param = NULL;
233 int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched = 0;
234 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
235 int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;
236 int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
237 size_t secret_keylen = 0, secret_keyidlen = 0;
238 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
239 unsigned char *secret_key = NULL, *secret_keyid = NULL;
241 const char *mime_eol = "\n";
244 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
247 prog = opt_init(argc, argv, cms_options);
248 while ((o = opt_next()) != OPT_EOF) {
253 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
256 opt_help(cms_options);
260 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
264 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
271 operation = SMIME_ENCRYPT;
274 operation = SMIME_DECRYPT;
277 operation = SMIME_SIGN;
279 case OPT_SIGN_RECEIPT:
280 operation = SMIME_SIGN_RECEIPT;
283 operation = SMIME_RESIGN;
286 operation = SMIME_VERIFY;
288 case OPT_VERIFY_RETCODE:
291 case OPT_VERIFY_RECEIPT:
292 operation = SMIME_VERIFY_RECEIPT;
296 operation = SMIME_CMSOUT;
299 operation = SMIME_DATAOUT;
301 case OPT_DATA_CREATE:
302 operation = SMIME_DATA_CREATE;
304 case OPT_DIGEST_VERIFY:
305 operation = SMIME_DIGEST_VERIFY;
307 case OPT_DIGEST_CREATE:
308 operation = SMIME_DIGEST_CREATE;
311 operation = SMIME_COMPRESS;
314 operation = SMIME_UNCOMPRESS;
317 operation = SMIME_ENCRYPTED_DECRYPT;
320 operation = SMIME_ENCRYPTED_ENCRYPT;
322 case OPT_DEBUG_DECRYPT:
323 flags |= CMS_DEBUG_DECRYPT;
329 flags |= CMS_ASCIICRLF;
332 flags |= CMS_NOINTERN;
335 flags |= CMS_NO_SIGNER_CERT_VERIFY;
338 flags |= CMS_NOCERTS;
344 flags &= ~CMS_DETACHED;
347 flags |= CMS_NOSMIMECAP;
356 flags |= CMS_USE_KEYID;
361 case OPT_NO_CONTENT_VERIFY:
362 flags |= CMS_NO_CONTENT_VERIFY;
364 case OPT_NO_ATTR_VERIFY:
365 flags |= CMS_NO_ATTR_VERIFY;
371 flags &= ~CMS_STREAM;
375 flags |= CMS_CRLFEOL;
390 if (rctformat == FORMAT_SMIME)
391 rcms = SMIME_read_CMS(rctin, NULL);
392 else if (rctformat == FORMAT_PEM)
393 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
394 else if (rctformat == FORMAT_ASN1)
395 if (!opt_format(opt_arg(),
396 OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
400 certfile = opt_arg();
424 contfile = opt_arg();
428 && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
430 sk_OPENSSL_STRING_push(rr_from, opt_arg());
434 && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
436 sk_OPENSSL_STRING_push(rr_to, opt_arg());
442 if (secret_key != NULL) {
443 BIO_printf(bio_err, "Invalid key (supplied twice) %s\n",
447 secret_key = OPENSSL_hexstr2buf(opt_arg(), <mp);
448 if (secret_key == NULL) {
449 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
452 secret_keylen = (size_t)ltmp;
454 case OPT_SECRETKEYID:
455 if (secret_keyid != NULL) {
456 BIO_printf(bio_err, "Invalid id (supplied twice) %s\n",
460 secret_keyid = OPENSSL_hexstr2buf(opt_arg(), <mp);
461 if (secret_keyid == NULL) {
462 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
465 secret_keyidlen = (size_t)ltmp;
467 case OPT_PWRI_PASSWORD:
468 pwri_pass = (unsigned char *)opt_arg();
470 case OPT_ECONTENT_TYPE:
471 if (econtent_type != NULL) {
472 BIO_printf(bio_err, "Invalid OID (supplied twice) %s\n",
476 econtent_type = OBJ_txt2obj(opt_arg(), 0);
477 if (econtent_type == NULL) {
478 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
483 e = setup_engine(opt_arg(), 0);
486 passinarg = opt_arg();
498 certsoutfile = opt_arg();
501 if (!opt_md(opt_arg(), &sign_md))
505 /* If previous -signer argument add signer to list */
506 if (signerfile != NULL) {
507 if (sksigners == NULL
508 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
510 sk_OPENSSL_STRING_push(sksigners, signerfile);
512 keyfile = signerfile;
514 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
516 sk_OPENSSL_STRING_push(skkeys, keyfile);
519 signerfile = opt_arg();
522 /* If previous -inkey argument add signer to list */
523 if (keyfile != NULL) {
524 if (signerfile == NULL) {
525 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
528 if (sksigners == NULL
529 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
531 sk_OPENSSL_STRING_push(sksigners, signerfile);
534 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
536 sk_OPENSSL_STRING_push(skkeys, keyfile);
541 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
545 if (operation == SMIME_ENCRYPT) {
546 if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)
548 cert = load_cert(opt_arg(), FORMAT_PEM,
549 "recipient certificate file");
552 sk_X509_push(encerts, cert);
555 recipfile = opt_arg();
559 if (!opt_cipher(opt_unknown(), &cipher))
564 if (operation == SMIME_ENCRYPT) {
566 keyidx += sk_X509_num(encerts);
568 if (keyfile != NULL || signerfile != NULL)
571 keyidx += sk_OPENSSL_STRING_num(skkeys);
574 BIO_printf(bio_err, "No key specified\n");
577 if (key_param == NULL || key_param->idx != keyidx) {
578 cms_key_param *nparam;
579 nparam = app_malloc(sizeof(*nparam), "key param buffer");
580 nparam->idx = keyidx;
581 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
584 if (key_first == NULL)
587 key_param->next = nparam;
590 sk_OPENSSL_STRING_push(key_param->param, opt_arg());
593 if (!opt_verify(o, vpm))
602 # ifndef OPENSSL_NO_DES
603 wrap_cipher = EVP_des_ede3_wrap();
606 case OPT_AES128_WRAP:
607 wrap_cipher = EVP_aes_128_wrap();
609 case OPT_AES192_WRAP:
610 wrap_cipher = EVP_aes_192_wrap();
612 case OPT_AES256_WRAP:
613 wrap_cipher = EVP_aes_256_wrap();
617 argc = opt_num_rest();
620 if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {
621 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
625 if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {
626 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
629 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
630 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
634 if (flags & CMS_CADES) {
635 if (flags & CMS_NOATTR) {
636 BIO_puts(bio_err, "Incompatible options: "
637 "CAdES required signed attributes\n");
642 if (operation & SMIME_SIGNERS) {
643 if (keyfile != NULL && signerfile == NULL) {
644 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
647 /* Check to see if any final signer needs to be appended */
648 if (signerfile != NULL) {
649 if (sksigners == NULL
650 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
652 sk_OPENSSL_STRING_push(sksigners, signerfile);
653 if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
656 keyfile = signerfile;
657 sk_OPENSSL_STRING_push(skkeys, keyfile);
659 if (sksigners == NULL) {
660 BIO_printf(bio_err, "No signer certificate specified\n");
665 } else if (operation == SMIME_DECRYPT) {
666 if (recipfile == NULL && keyfile == NULL
667 && secret_key == NULL && pwri_pass == NULL) {
669 "No recipient certificate or key specified\n");
672 } else if (operation == SMIME_ENCRYPT) {
673 if (*argv == NULL && secret_key == NULL
674 && pwri_pass == NULL && encerts == NULL) {
675 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
678 } else if (!operation) {
679 BIO_printf(bio_err, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\n");
683 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
684 BIO_printf(bio_err, "Error getting password\n");
690 if (!(operation & SMIME_SIGNERS))
691 flags &= ~CMS_DETACHED;
693 if (!(operation & SMIME_OP))
694 if (flags & CMS_BINARY)
695 outformat = FORMAT_BINARY;
697 if (!(operation & SMIME_IP))
698 if (flags & CMS_BINARY)
699 informat = FORMAT_BINARY;
701 if (operation == SMIME_ENCRYPT) {
703 # ifndef OPENSSL_NO_DES
704 cipher = EVP_des_ede3_cbc();
706 BIO_printf(bio_err, "No cipher selected\n");
711 if (secret_key && !secret_keyid) {
712 BIO_printf(bio_err, "No secret key id\n");
716 if (*argv && encerts == NULL)
717 if ((encerts = sk_X509_new_null()) == NULL)
720 if ((cert = load_cert(*argv, FORMAT_PEM,
721 "recipient certificate file")) == NULL)
723 sk_X509_push(encerts, cert);
729 if (certfile != NULL) {
730 if (!load_certs(certfile, &other, FORMAT_PEM, NULL,
731 "certificate file")) {
732 ERR_print_errors(bio_err);
737 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
738 if ((recip = load_cert(recipfile, FORMAT_PEM,
739 "recipient certificate file")) == NULL) {
740 ERR_print_errors(bio_err);
745 if (operation == SMIME_SIGN_RECEIPT) {
746 if ((signer = load_cert(signerfile, FORMAT_PEM,
747 "receipt signer certificate file")) == NULL) {
748 ERR_print_errors(bio_err);
753 if (operation == SMIME_DECRYPT) {
756 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
758 keyfile = signerfile;
763 if (keyfile != NULL) {
764 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
769 in = bio_open_default(infile, 'r', informat);
773 if (operation & SMIME_IP) {
774 if (informat == FORMAT_SMIME) {
775 cms = SMIME_read_CMS(in, &indata);
776 } else if (informat == FORMAT_PEM) {
777 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
778 } else if (informat == FORMAT_ASN1) {
779 cms = d2i_CMS_bio(in, NULL);
781 BIO_printf(bio_err, "Bad input format for CMS file\n");
786 BIO_printf(bio_err, "Error reading S/MIME message\n");
789 if (contfile != NULL) {
791 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
792 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
796 if (certsoutfile != NULL) {
797 STACK_OF(X509) *allcerts;
798 allcerts = CMS_get1_certs(cms);
799 if (!save_certs(certsoutfile, allcerts)) {
801 "Error writing certs to %s\n", certsoutfile);
805 sk_X509_pop_free(allcerts, X509_free);
809 if (rctfile != NULL) {
810 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
811 if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
812 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
816 if (rctformat == FORMAT_SMIME) {
817 rcms = SMIME_read_CMS(rctin, NULL);
818 } else if (rctformat == FORMAT_PEM) {
819 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
820 } else if (rctformat == FORMAT_ASN1) {
821 rcms = d2i_CMS_bio(rctin, NULL);
823 BIO_printf(bio_err, "Bad input format for receipt\n");
828 BIO_printf(bio_err, "Error reading receipt\n");
833 out = bio_open_default(outfile, 'w', outformat);
837 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
838 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
839 CAstore, noCAstore)) == NULL)
841 X509_STORE_set_verify_cb(store, cms_cb);
843 X509_STORE_set1_param(store, vpm);
848 if (operation == SMIME_DATA_CREATE) {
849 cms = CMS_data_create(in, flags);
850 } else if (operation == SMIME_DIGEST_CREATE) {
851 cms = CMS_digest_create(in, sign_md, flags);
852 } else if (operation == SMIME_COMPRESS) {
853 cms = CMS_compress(in, -1, flags);
854 } else if (operation == SMIME_ENCRYPT) {
856 flags |= CMS_PARTIAL;
857 cms = CMS_encrypt(NULL, in, cipher, flags);
860 for (i = 0; i < sk_X509_num(encerts); i++) {
861 CMS_RecipientInfo *ri;
862 cms_key_param *kparam;
864 X509 *x = sk_X509_value(encerts, i);
865 for (kparam = key_first; kparam; kparam = kparam->next) {
866 if (kparam->idx == i) {
867 tflags |= CMS_KEY_PARAM;
871 ri = CMS_add1_recipient_cert(cms, x, tflags);
874 if (kparam != NULL) {
876 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
877 if (!cms_set_pkey_param(pctx, kparam->param))
880 if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
882 EVP_CIPHER_CTX *wctx;
883 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
884 EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
888 if (secret_key != NULL) {
889 if (!CMS_add0_recipient_key(cms, NID_undef,
890 secret_key, secret_keylen,
891 secret_keyid, secret_keyidlen,
894 /* NULL these because call absorbs them */
898 if (pwri_pass != NULL) {
899 pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
900 if (pwri_tmp == NULL)
902 if (CMS_add0_recipient_password(cms,
903 -1, NID_undef, NID_undef,
904 pwri_tmp, -1, NULL) == NULL)
908 if (!(flags & CMS_STREAM)) {
909 if (!CMS_final(cms, in, NULL, flags))
912 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
913 cms = CMS_EncryptedData_encrypt(in, cipher,
914 secret_key, secret_keylen, flags);
916 } else if (operation == SMIME_SIGN_RECEIPT) {
917 CMS_ContentInfo *srcms = NULL;
918 STACK_OF(CMS_SignerInfo) *sis;
920 sis = CMS_get0_SignerInfos(cms);
923 si = sk_CMS_SignerInfo_value(sis, 0);
924 srcms = CMS_sign_receipt(si, signer, key, other, flags);
927 CMS_ContentInfo_free(cms);
929 } else if (operation & SMIME_SIGNERS) {
932 * If detached data content we enable streaming if S/MIME output
935 if (operation == SMIME_SIGN) {
937 if (flags & CMS_DETACHED) {
938 if (outformat == FORMAT_SMIME)
941 flags |= CMS_PARTIAL;
942 cms = CMS_sign(NULL, NULL, other, in, flags);
945 if (econtent_type != NULL)
946 CMS_set1_eContentType(cms, econtent_type);
949 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
952 "Signed Receipt Request Creation Error\n");
957 flags |= CMS_REUSE_DIGEST;
959 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
961 cms_key_param *kparam;
963 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
964 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
966 signer = load_cert(signerfile, FORMAT_PEM, "signer certificate");
967 if (signer == NULL) {
971 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
976 for (kparam = key_first; kparam; kparam = kparam->next) {
977 if (kparam->idx == i) {
978 tflags |= CMS_KEY_PARAM;
982 si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
985 if (kparam != NULL) {
987 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
988 if (!cms_set_pkey_param(pctx, kparam->param))
991 if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
998 /* If not streaming or resigning finalize structure */
999 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
1000 if (!CMS_final(cms, in, NULL, flags))
1006 BIO_printf(bio_err, "Error creating CMS structure\n");
1011 if (operation == SMIME_DECRYPT) {
1012 if (flags & CMS_DEBUG_DECRYPT)
1013 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
1015 if (secret_key != NULL) {
1016 if (!CMS_decrypt_set1_key(cms,
1017 secret_key, secret_keylen,
1018 secret_keyid, secret_keyidlen)) {
1019 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
1025 if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
1026 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
1031 if (pwri_pass != NULL) {
1032 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
1033 BIO_puts(bio_err, "Error decrypting CMS using password\n");
1038 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
1039 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1042 } else if (operation == SMIME_DATAOUT) {
1043 if (!CMS_data(cms, out, flags))
1045 } else if (operation == SMIME_UNCOMPRESS) {
1046 if (!CMS_uncompress(cms, indata, out, flags))
1048 } else if (operation == SMIME_DIGEST_VERIFY) {
1049 if (CMS_digest_verify(cms, indata, out, flags) > 0) {
1050 BIO_printf(bio_err, "Verification successful\n");
1052 BIO_printf(bio_err, "Verification failure\n");
1055 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
1056 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1057 indata, out, flags))
1059 } else if (operation == SMIME_VERIFY) {
1060 if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
1061 BIO_printf(bio_err, "Verification successful\n");
1063 BIO_printf(bio_err, "Verification failure\n");
1065 ret = verify_err + 32;
1068 if (signerfile != NULL) {
1069 STACK_OF(X509) *signers;
1070 signers = CMS_get0_signers(cms);
1071 if (!save_certs(signerfile, signers)) {
1073 "Error writing signers to %s\n", signerfile);
1077 sk_X509_free(signers);
1080 receipt_request_print(cms);
1082 } else if (operation == SMIME_VERIFY_RECEIPT) {
1083 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
1084 BIO_printf(bio_err, "Verification successful\n");
1086 BIO_printf(bio_err, "Verification failure\n");
1092 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1093 } else if (outformat == FORMAT_SMIME) {
1095 BIO_printf(out, "To: %s%s", to, mime_eol);
1097 BIO_printf(out, "From: %s%s", from, mime_eol);
1099 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
1100 if (operation == SMIME_RESIGN)
1101 ret = SMIME_write_CMS(out, cms, indata, flags);
1103 ret = SMIME_write_CMS(out, cms, in, flags);
1104 } else if (outformat == FORMAT_PEM) {
1105 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1106 } else if (outformat == FORMAT_ASN1) {
1107 ret = i2d_CMS_bio_stream(out, cms, in, flags);
1109 BIO_printf(bio_err, "Bad output format for CMS file\n");
1120 ERR_print_errors(bio_err);
1121 sk_X509_pop_free(encerts, X509_free);
1122 sk_X509_pop_free(other, X509_free);
1123 X509_VERIFY_PARAM_free(vpm);
1124 sk_OPENSSL_STRING_free(sksigners);
1125 sk_OPENSSL_STRING_free(skkeys);
1126 OPENSSL_free(secret_key);
1127 OPENSSL_free(secret_keyid);
1128 OPENSSL_free(pwri_tmp);
1129 ASN1_OBJECT_free(econtent_type);
1130 CMS_ReceiptRequest_free(rr);
1131 sk_OPENSSL_STRING_free(rr_to);
1132 sk_OPENSSL_STRING_free(rr_from);
1133 for (key_param = key_first; key_param;) {
1134 cms_key_param *tparam;
1135 sk_OPENSSL_STRING_free(key_param->param);
1136 tparam = key_param->next;
1137 OPENSSL_free(key_param);
1140 X509_STORE_free(store);
1145 CMS_ContentInfo_free(cms);
1146 CMS_ContentInfo_free(rcms);
1152 OPENSSL_free(passin);
1156 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1160 if (signerfile == NULL)
1162 tmp = BIO_new_file(signerfile, "w");
1165 for (i = 0; i < sk_X509_num(signers); i++)
1166 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1171 /* Minimal callback just to output policy info (if any) */
1173 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1177 error = X509_STORE_CTX_get_error(ctx);
1181 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1182 && ((error != X509_V_OK) || (ok != 2)))
1185 policies_print(ctx);
1191 static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
1193 STACK_OF(GENERAL_NAME) *gens;
1197 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1198 gens = sk_GENERAL_NAMES_value(gns, i);
1199 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1200 gen = sk_GENERAL_NAME_value(gens, j);
1201 BIO_puts(bio_err, " ");
1202 GENERAL_NAME_print(bio_err, gen);
1203 BIO_puts(bio_err, "\n");
1209 static void receipt_request_print(CMS_ContentInfo *cms)
1211 STACK_OF(CMS_SignerInfo) *sis;
1213 CMS_ReceiptRequest *rr;
1215 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1218 sis = CMS_get0_SignerInfos(cms);
1219 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1220 si = sk_CMS_SignerInfo_value(sis, i);
1221 rv = CMS_get1_ReceiptRequest(si, &rr);
1222 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1224 BIO_puts(bio_err, " No Receipt Request\n");
1225 } else if (rv < 0) {
1226 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1227 ERR_print_errors(bio_err);
1231 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1233 BIO_puts(bio_err, " Signed Content ID:\n");
1234 idlen = ASN1_STRING_length(scid);
1235 id = (const char *)ASN1_STRING_get0_data(scid);
1236 BIO_dump_indent(bio_err, id, idlen, 4);
1237 BIO_puts(bio_err, " Receipts From");
1238 if (rlist != NULL) {
1239 BIO_puts(bio_err, " List:\n");
1240 gnames_stack_print(rlist);
1241 } else if (allorfirst == 1) {
1242 BIO_puts(bio_err, ": First Tier\n");
1243 } else if (allorfirst == 0) {
1244 BIO_puts(bio_err, ": All\n");
1246 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
1248 BIO_puts(bio_err, " Receipts To:\n");
1249 gnames_stack_print(rto);
1251 CMS_ReceiptRequest_free(rr);
1255 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1258 STACK_OF(GENERAL_NAMES) *ret;
1259 GENERAL_NAMES *gens = NULL;
1260 GENERAL_NAME *gen = NULL;
1261 ret = sk_GENERAL_NAMES_new_null();
1264 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1265 char *str = sk_OPENSSL_STRING_value(ns, i);
1266 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1269 gens = GENERAL_NAMES_new();
1272 if (!sk_GENERAL_NAME_push(gens, gen))
1275 if (!sk_GENERAL_NAMES_push(ret, gens))
1283 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1284 GENERAL_NAMES_free(gens);
1285 GENERAL_NAME_free(gen);
1289 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
1290 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
1293 STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
1294 CMS_ReceiptRequest *rr;
1295 rct_to = make_names_stack(rr_to);
1298 if (rr_from != NULL) {
1299 rct_from = make_names_stack(rr_from);
1300 if (rct_from == NULL)
1305 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1309 sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
1313 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
1314 STACK_OF(OPENSSL_STRING) *param)
1318 if (sk_OPENSSL_STRING_num(param) <= 0)
1320 for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
1321 keyopt = sk_OPENSSL_STRING_value(param, i);
1322 if (pkey_ctrl_string(pctx, keyopt) <= 0) {
1323 BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
1324 ERR_print_errors(bio_err);