2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
54 /* CMS utility function */
60 #ifndef OPENSSL_NO_CMS
62 # include <openssl/crypto.h>
63 # include <openssl/pem.h>
64 # include <openssl/err.h>
65 # include <openssl/x509_vfy.h>
66 # include <openssl/x509v3.h>
67 # include <openssl/cms.h>
69 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
70 static int cms_cb(int ok, X509_STORE_CTX *ctx);
71 static void receipt_request_print(CMS_ContentInfo *cms);
72 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
73 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
75 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
76 STACK_OF(OPENSSL_STRING) *param);
78 # define SMIME_OP 0x10
79 # define SMIME_IP 0x20
80 # define SMIME_SIGNERS 0x40
81 # define SMIME_ENCRYPT (1 | SMIME_OP)
82 # define SMIME_DECRYPT (2 | SMIME_IP)
83 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
84 # define SMIME_VERIFY (4 | SMIME_IP)
85 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
86 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
87 # define SMIME_DATAOUT (7 | SMIME_IP)
88 # define SMIME_DATA_CREATE (8 | SMIME_OP)
89 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
90 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
91 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
92 # define SMIME_COMPRESS (12 | SMIME_OP)
93 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
94 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
95 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
96 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
100 typedef struct cms_key_param_st cms_key_param;
102 struct cms_key_param_st {
104 STACK_OF(OPENSSL_STRING) *param;
108 typedef enum OPTION_choice {
109 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
110 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENCRYPT,
111 OPT_DECRYPT, OPT_SIGN, OPT_SIGN_RECEIPT, OPT_RESIGN,
112 OPT_VERIFY, OPT_VERIFY_RETCODE, OPT_VERIFY_RECEIPT,
113 OPT_CMSOUT, OPT_DATA_OUT, OPT_DATA_CREATE, OPT_DIGEST_VERIFY,
114 OPT_DIGEST_CREATE, OPT_COMPRESS, OPT_UNCOMPRESS,
115 OPT_ED_DECRYPT, OPT_ED_ENCRYPT, OPT_DEBUG_DECRYPT, OPT_TEXT,
116 OPT_ASCIICRLF, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCERTS,
117 OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP, OPT_BINARY, OPT_KEYID,
118 OPT_NOSIGS, OPT_NO_CONTENT_VERIFY, OPT_NO_ATTR_VERIFY, OPT_INDEF,
119 OPT_NOINDEF, OPT_NOOLDMIME, OPT_CRLFEOL, OPT_NOOUT, OPT_RR_PRINT,
120 OPT_RR_ALL, OPT_RR_FIRST, OPT_RCTFORM, OPT_CERTFILE, OPT_CAFILE,
121 OPT_CAPATH, OPT_CONTENT, OPT_PRINT, OPT_SECRETKEY,
122 OPT_SECRETKEYID, OPT_PWRI_PASSWORD, OPT_ECONTENT_TYPE, OPT_RAND,
123 OPT_PASSIN, OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP,
124 OPT_CERTSOUT, OPT_MD, OPT_INKEY, OPT_KEYFORM, OPT_KEYOPT, OPT_RR_FROM,
125 OPT_RR_TO, OPT_AES128_WRAP, OPT_AES192_WRAP, OPT_AES256_WRAP,
126 OPT_3DES_WRAP, OPT_ENGINE,
131 OPTIONS cms_options[] = {
132 {OPT_HELP_STR, 1, '-', "Usage: %s [options] cert.pem...\n"},
133 {OPT_HELP_STR, 1, '-',
134 " cert.pem... recipient certs for encryption\n"},
135 {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
136 {"help", OPT_HELP, '-', "Display this summary"},
137 {"inform", OPT_INFORM, 'F', "Input format SMIME (default), PEM or DER"},
138 {"outform", OPT_OUTFORM, 'F',
139 "Output format SMIME (default), PEM or DER"},
140 {"in", OPT_IN, '<', "Input file"},
141 {"out", OPT_OUT, '>', "Output file"},
142 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
143 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
144 {"sign", OPT_SIGN, '-', "Sign message"},
145 {"sign_receipt", OPT_SIGN_RECEIPT, '-'},
146 {"resign", OPT_RESIGN, '-'},
147 {"verify", OPT_VERIFY, '-', "Verify signed message"},
148 {"verify_retcode", OPT_VERIFY_RETCODE, '-'},
149 {"verify_receipt", OPT_VERIFY_RECEIPT, '<'},
150 {"cmsout", OPT_CMSOUT, '-', "Output CMS structure"},
151 {"data_out", OPT_DATA_OUT, '-'},
152 {"data_create", OPT_DATA_CREATE, '-'},
153 {"digest_verify", OPT_DIGEST_VERIFY, '-'},
154 {"digest_create", OPT_DIGEST_CREATE, '-'},
155 {"compress", OPT_COMPRESS, '-'},
156 {"uncompress", OPT_UNCOMPRESS, '-'},
157 {"EncryptedData_decrypt", OPT_ED_DECRYPT, '-'},
158 {"EncryptedData_encrypt", OPT_ED_ENCRYPT, '-'},
159 {"debug_decrypt", OPT_DEBUG_DECRYPT, '-'},
160 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
161 {"asciicrlf", OPT_ASCIICRLF, '-'},
162 {"nointern", OPT_NOINTERN, '-',
163 "Don't search certificates in message for signer"},
164 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
165 {"nocerts", OPT_NOCERTS, '-',
166 "Don't include signers certificate when signing"},
167 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
168 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
169 {"nosmimecap", OPT_NOSMIMECAP, '-'},
170 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
171 {"keyid", OPT_KEYID, '-', "Use subject key identifier"},
172 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
173 {"no_content_verify", OPT_NO_CONTENT_VERIFY, '-'},
174 {"no_attr_verify", OPT_NO_ATTR_VERIFY, '-'},
175 {"stream", OPT_INDEF, '-'},
176 {"indef", OPT_INDEF, '-'},
177 {"noindef", OPT_NOINDEF, '-'},
178 {"nooldmime", OPT_NOOLDMIME, '-'},
179 {"crlfeol", OPT_CRLFEOL, '-'},
180 {"noout", OPT_NOOUT, '-'},
181 {"receipt_request_print", OPT_RR_PRINT, '-'},
182 {"receipt_request_all", OPT_RR_ALL, '-'},
183 {"receipt_request_first", OPT_RR_FIRST, '-'},
184 {"rctform", OPT_RCTFORM, 'F'},
185 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
186 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
187 {"CApath", OPT_CAPATH, '/', "trusted certificates directory"},
188 {"content", OPT_CONTENT, '<',
189 "Supply or override content for detached signature"},
190 {"print", OPT_PRINT, '-'},
191 {"secretkey", OPT_SECRETKEY, 's'},
192 {"secretkeyid", OPT_SECRETKEYID, 's'},
193 {"pwri_password", OPT_PWRI_PASSWORD, 's'},
194 {"econtent_type", OPT_ECONTENT_TYPE, 's'},
195 {"rand", OPT_RAND, 's',
196 "Load the file(s) into the random number generator"},
197 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
198 {"to", OPT_TO, 's', "To address"},
199 {"from", OPT_FROM, 's', "From address"},
200 {"subject", OPT_SUBJECT, 's', "Subject"},
201 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
202 {"recip", OPT_RECIP, '<', "Recipient cert file for decryption"},
203 {"certsout", OPT_CERTSOUT, '>', "Certificate output file"},
205 {"inkey", OPT_INKEY, '<',
206 "Input private key (if not signer or recipient)"},
207 {"keyform", OPT_KEYFORM, 'f', "Input private key format (PEM or ENGINE)"},
208 {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"},
209 {"receipt_request_from", OPT_RR_FROM, 's'},
210 {"receipt_request_to", OPT_RR_TO, 's'},
211 # ifndef OPENSSL_NO_AES
212 {"aes128-wrap", OPT_AES128_WRAP, '-', "Use AES128 to wrap key"},
213 {"aes192-wrap", OPT_AES192_WRAP, '-', "Use AES192 to wrap key"},
214 {"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 # ifndef OPENSSL_NO_ENGINE
220 {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
222 {"", OPT_CIPHER, '-', "Any supported cipher"},
227 int cms_main(int argc, char **argv)
229 ASN1_OBJECT *econtent_type = NULL;
230 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
231 CMS_ContentInfo *cms = NULL, *rcms = NULL;
232 CMS_ReceiptRequest *rr = NULL;
234 EVP_PKEY *key = NULL;
235 const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
236 const EVP_MD *sign_md = NULL;
237 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
238 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
239 STACK_OF(X509) *encerts = NULL, *other = NULL;
240 X509 *cert = NULL, *recip = NULL, *signer = NULL;
241 X509_STORE *store = NULL;
242 X509_VERIFY_PARAM *vpm = NULL;
243 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
244 char *CAfile = NULL, *CApath = NULL, *certsoutfile = NULL;
245 char *infile = NULL, *outfile = NULL, *rctfile = NULL, *inrand = NULL;
246 char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile =
248 char *to = NULL, *from = NULL, *subject = NULL, *prog;
249 cms_key_param *key_first = NULL, *key_param = NULL;
250 const char *inmode = "r", *outmode = "w";
251 int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched =
253 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
254 int need_rand = 0, operation = 0, ret = 1, rr_print = 0, rr_allorfirst =
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;
263 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
266 prog = opt_init(argc, argv, cms_options);
267 while ((o = opt_next()) != OPT_EOF) {
272 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
275 opt_help(cms_options);
279 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
283 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
290 operation = SMIME_ENCRYPT;
293 operation = SMIME_DECRYPT;
296 operation = SMIME_SIGN;
298 case OPT_SIGN_RECEIPT:
299 operation = SMIME_SIGN_RECEIPT;
302 operation = SMIME_RESIGN;
305 operation = SMIME_VERIFY;
307 case OPT_VERIFY_RETCODE:
310 case OPT_VERIFY_RECEIPT:
311 operation = SMIME_VERIFY_RECEIPT;
315 operation = SMIME_CMSOUT;
318 operation = SMIME_DATAOUT;
320 case OPT_DATA_CREATE:
321 operation = SMIME_DATA_CREATE;
323 case OPT_DIGEST_VERIFY:
324 operation = SMIME_DIGEST_VERIFY;
326 case OPT_DIGEST_CREATE:
327 operation = SMIME_DIGEST_CREATE;
330 operation = SMIME_COMPRESS;
333 operation = SMIME_UNCOMPRESS;
336 operation = SMIME_ENCRYPTED_DECRYPT;
339 operation = SMIME_ENCRYPTED_ENCRYPT;
341 case OPT_DEBUG_DECRYPT:
342 flags |= CMS_DEBUG_DECRYPT;
348 flags |= CMS_ASCIICRLF;
351 flags |= CMS_NOINTERN;
354 flags |= CMS_NO_SIGNER_CERT_VERIFY;
357 flags |= CMS_NOCERTS;
363 flags &= ~CMS_DETACHED;
366 flags |= CMS_NOSMIMECAP;
372 flags |= CMS_USE_KEYID;
377 case OPT_NO_CONTENT_VERIFY:
378 flags |= CMS_NO_CONTENT_VERIFY;
380 case OPT_NO_ATTR_VERIFY:
381 flags |= CMS_NO_ATTR_VERIFY;
387 flags &= ~CMS_STREAM;
390 flags |= CMS_NOOLDMIMETYPE;
393 flags |= CMS_CRLFEOL;
408 if (rctformat == FORMAT_SMIME)
409 rcms = SMIME_read_CMS(rctin, NULL);
410 else if (rctformat == FORMAT_PEM)
411 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
412 else if (rctformat == FORMAT_ASN1)
413 if (!opt_format(opt_arg(),
414 OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
418 certfile = opt_arg();
430 contfile = opt_arg();
434 && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
436 sk_OPENSSL_STRING_push(rr_from, opt_arg());
440 && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
442 sk_OPENSSL_STRING_push(rr_to, opt_arg());
448 secret_key = string_to_hex(opt_arg(), <mp);
449 if (secret_key == NULL) {
450 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
453 secret_keylen = (size_t)ltmp;
455 case OPT_SECRETKEYID:
456 secret_keyid = string_to_hex(opt_arg(), <mp);
457 if (secret_keyid == NULL) {
458 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
461 secret_keyidlen = (size_t)ltmp;
463 case OPT_PWRI_PASSWORD:
464 pwri_pass = (unsigned char *)opt_arg();
466 case OPT_ECONTENT_TYPE:
467 econtent_type = OBJ_txt2obj(opt_arg(), 0);
468 if (econtent_type == NULL) {
469 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
478 e = setup_engine(opt_arg(), 0);
481 passinarg = opt_arg();
493 certsoutfile = opt_arg();
496 if (!opt_md(opt_arg(), &sign_md))
500 /* If previous -signer argument add signer to list */
502 if (sksigners == NULL
503 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
505 sk_OPENSSL_STRING_push(sksigners, signerfile);
507 keyfile = signerfile;
509 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
511 sk_OPENSSL_STRING_push(skkeys, keyfile);
514 signerfile = opt_arg();
517 /* If previous -inkey arument add signer to list */
519 if (signerfile == NULL) {
520 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
523 if (sksigners == NULL
524 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
526 sk_OPENSSL_STRING_push(sksigners, signerfile);
529 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
531 sk_OPENSSL_STRING_push(skkeys, keyfile);
536 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
540 if (operation == SMIME_ENCRYPT) {
541 if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)
543 cert = load_cert(opt_arg(), FORMAT_PEM, NULL, e,
544 "recipient certificate file");
547 sk_X509_push(encerts, cert);
550 recipfile = opt_arg();
553 if (!opt_cipher(opt_unknown(), &cipher))
558 if (operation == SMIME_ENCRYPT) {
560 keyidx += sk_X509_num(encerts);
562 if (keyfile || signerfile)
565 keyidx += sk_OPENSSL_STRING_num(skkeys);
568 BIO_printf(bio_err, "No key specified\n");
571 if (key_param == NULL || key_param->idx != keyidx) {
572 cms_key_param *nparam;
573 nparam = OPENSSL_malloc(sizeof(cms_key_param));
575 BIO_printf(bio_err, "Out of memory\n");
578 nparam->idx = keyidx;
579 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
582 if (key_first == NULL)
585 key_param->next = nparam;
588 sk_OPENSSL_STRING_push(key_param->param, opt_arg());
591 if (!opt_verify(o, vpm))
595 # ifndef OPENSSL_NO_DES
597 wrap_cipher = EVP_des_ede3_wrap();
600 # ifndef OPENSSL_NO_AES
601 case OPT_AES128_WRAP:
602 wrap_cipher = EVP_aes_128_wrap();
604 case OPT_AES192_WRAP:
605 wrap_cipher = EVP_aes_192_wrap();
607 case OPT_AES256_WRAP:
608 wrap_cipher = EVP_aes_256_wrap();
613 argc = opt_num_rest();
616 if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
617 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
621 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
622 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
625 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
626 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
630 if (operation & SMIME_SIGNERS) {
631 if (keyfile && !signerfile) {
632 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
635 /* Check to see if any final signer needs to be appended */
638 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
640 sk_OPENSSL_STRING_push(sksigners, signerfile);
641 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
644 keyfile = signerfile;
645 sk_OPENSSL_STRING_push(skkeys, keyfile);
648 BIO_printf(bio_err, "No signer certificate specified\n");
656 else if (operation == SMIME_DECRYPT) {
657 if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
659 "No recipient certificate or key specified\n");
662 } else if (operation == SMIME_ENCRYPT) {
663 if (*argv == NULL && !secret_key && !pwri_pass && !encerts) {
664 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
668 } else if (!operation)
672 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
673 BIO_printf(bio_err, "Error getting password\n");
678 app_RAND_load_file(NULL, (inrand != NULL));
680 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
681 app_RAND_load_files(inrand));
686 if (!(operation & SMIME_SIGNERS))
687 flags &= ~CMS_DETACHED;
689 if (operation & SMIME_OP) {
690 if (outformat == FORMAT_ASN1)
693 if (flags & CMS_BINARY)
697 if (operation & SMIME_IP) {
698 if (informat == FORMAT_ASN1)
701 if (flags & CMS_BINARY)
705 if (operation == SMIME_ENCRYPT) {
707 # ifndef OPENSSL_NO_DES
708 cipher = EVP_des_ede3_cbc();
710 BIO_printf(bio_err, "No cipher selected\n");
715 if (secret_key && !secret_keyid) {
716 BIO_printf(bio_err, "No secret key id\n");
720 if (*argv && !encerts)
721 if ((encerts = sk_X509_new_null()) == NULL)
724 if (!(cert = load_cert(*argv, FORMAT_PEM,
725 NULL, e, "recipient certificate file")))
727 sk_X509_push(encerts, cert);
734 if (!(other = load_certs(certfile, FORMAT_PEM, NULL,
735 e, "certificate file"))) {
736 ERR_print_errors(bio_err);
741 if (recipfile && (operation == SMIME_DECRYPT)) {
742 if (!(recip = load_cert(recipfile, FORMAT_PEM, NULL,
743 e, "recipient certificate file"))) {
744 ERR_print_errors(bio_err);
749 if (operation == SMIME_SIGN_RECEIPT) {
750 if (!(signer = load_cert(signerfile, FORMAT_PEM, NULL,
751 e, "receipt signer certificate file"))) {
752 ERR_print_errors(bio_err);
757 if (operation == SMIME_DECRYPT) {
760 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
762 keyfile = signerfile;
767 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
772 in = bio_open_default(infile, inmode);
776 if (operation & SMIME_IP) {
777 if (informat == FORMAT_SMIME)
778 cms = SMIME_read_CMS(in, &indata);
779 else if (informat == FORMAT_PEM)
780 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
781 else if (informat == FORMAT_ASN1)
782 cms = d2i_CMS_bio(in, NULL);
784 BIO_printf(bio_err, "Bad input format for CMS file\n");
789 BIO_printf(bio_err, "Error reading S/MIME message\n");
794 if (!(indata = BIO_new_file(contfile, "rb"))) {
795 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
800 STACK_OF(X509) *allcerts;
801 allcerts = CMS_get1_certs(cms);
802 if (!save_certs(certsoutfile, allcerts)) {
804 "Error writing certs to %s\n", certsoutfile);
808 sk_X509_pop_free(allcerts, X509_free);
813 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
814 if (!(rctin = BIO_new_file(rctfile, rctmode))) {
815 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
819 if (rctformat == FORMAT_SMIME)
820 rcms = SMIME_read_CMS(rctin, NULL);
821 else if (rctformat == FORMAT_PEM)
822 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
823 else if (rctformat == FORMAT_ASN1)
824 rcms = d2i_CMS_bio(rctin, NULL);
826 BIO_printf(bio_err, "Bad input format for receipt\n");
831 BIO_printf(bio_err, "Error reading receipt\n");
836 out = bio_open_default(outfile, outmode);
840 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
841 if (!(store = setup_verify(CAfile, CApath)))
843 X509_STORE_set_verify_cb(store, cms_cb);
845 X509_STORE_set1_param(store, vpm);
850 if (operation == SMIME_DATA_CREATE) {
851 cms = CMS_data_create(in, flags);
852 } else if (operation == SMIME_DIGEST_CREATE) {
853 cms = CMS_digest_create(in, sign_md, flags);
854 } else if (operation == SMIME_COMPRESS) {
855 cms = CMS_compress(in, -1, flags);
856 } else if (operation == SMIME_ENCRYPT) {
858 flags |= CMS_PARTIAL;
859 cms = CMS_encrypt(NULL, in, cipher, flags);
862 for (i = 0; i < sk_X509_num(encerts); i++) {
863 CMS_RecipientInfo *ri;
864 cms_key_param *kparam;
866 X509 *x = sk_X509_value(encerts, i);
867 for (kparam = key_first; kparam; kparam = kparam->next) {
868 if (kparam->idx == i) {
869 tflags |= CMS_KEY_PARAM;
873 ri = CMS_add1_recipient_cert(cms, x, tflags);
878 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
879 if (!cms_set_pkey_param(pctx, kparam->param))
882 if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
884 EVP_CIPHER_CTX *wctx;
885 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
886 EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
891 if (!CMS_add0_recipient_key(cms, NID_undef,
892 secret_key, secret_keylen,
893 secret_keyid, secret_keyidlen,
896 /* NULL these because call absorbs them */
901 pwri_tmp = (unsigned char *)BUF_strdup((char *)pwri_pass);
904 if (!CMS_add0_recipient_password(cms,
905 -1, NID_undef, NID_undef,
910 if (!(flags & CMS_STREAM)) {
911 if (!CMS_final(cms, in, NULL, flags))
914 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
915 cms = CMS_EncryptedData_encrypt(in, cipher,
916 secret_key, secret_keylen, flags);
918 } else if (operation == SMIME_SIGN_RECEIPT) {
919 CMS_ContentInfo *srcms = NULL;
920 STACK_OF(CMS_SignerInfo) *sis;
922 sis = CMS_get0_SignerInfos(cms);
925 si = sk_CMS_SignerInfo_value(sis, 0);
926 srcms = CMS_sign_receipt(si, signer, key, other, flags);
929 CMS_ContentInfo_free(cms);
931 } else if (operation & SMIME_SIGNERS) {
934 * If detached data content we enable streaming if S/MIME output
937 if (operation == SMIME_SIGN) {
939 if (flags & CMS_DETACHED) {
940 if (outformat == FORMAT_SMIME)
943 flags |= CMS_PARTIAL;
944 cms = CMS_sign(NULL, NULL, other, in, flags);
948 CMS_set1_eContentType(cms, econtent_type);
951 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
954 "Signed Receipt Request Creation Error\n");
959 flags |= CMS_REUSE_DIGEST;
960 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
962 cms_key_param *kparam;
964 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
965 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
967 signer = load_cert(signerfile, FORMAT_PEM, NULL,
968 e, "signer certificate");
971 key = load_key(keyfile, keyform, 0, passin, e, "signing key file");
974 for (kparam = key_first; kparam; kparam = kparam->next) {
975 if (kparam->idx == i) {
976 tflags |= CMS_KEY_PARAM;
980 si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
985 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
986 if (!cms_set_pkey_param(pctx, kparam->param))
989 if (rr && !CMS_add1_ReceiptRequest(si, rr))
996 /* If not streaming or resigning finalize structure */
997 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
998 if (!CMS_final(cms, in, NULL, flags))
1004 BIO_printf(bio_err, "Error creating CMS structure\n");
1009 if (operation == SMIME_DECRYPT) {
1010 if (flags & CMS_DEBUG_DECRYPT)
1011 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
1014 if (!CMS_decrypt_set1_key(cms,
1015 secret_key, secret_keylen,
1016 secret_keyid, secret_keyidlen)) {
1017 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
1023 if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
1024 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
1030 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
1031 BIO_puts(bio_err, "Error decrypting CMS using password\n");
1036 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
1037 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1040 } else if (operation == SMIME_DATAOUT) {
1041 if (!CMS_data(cms, out, flags))
1043 } else if (operation == SMIME_UNCOMPRESS) {
1044 if (!CMS_uncompress(cms, indata, out, flags))
1046 } else if (operation == SMIME_DIGEST_VERIFY) {
1047 if (CMS_digest_verify(cms, indata, out, flags) > 0)
1048 BIO_printf(bio_err, "Verification successful\n");
1050 BIO_printf(bio_err, "Verification failure\n");
1053 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
1054 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1055 indata, out, flags))
1057 } else if (operation == SMIME_VERIFY) {
1058 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
1059 BIO_printf(bio_err, "Verification successful\n");
1061 BIO_printf(bio_err, "Verification failure\n");
1063 ret = verify_err + 32;
1067 STACK_OF(X509) *signers;
1068 signers = CMS_get0_signers(cms);
1069 if (!save_certs(signerfile, signers)) {
1071 "Error writing signers to %s\n", signerfile);
1075 sk_X509_free(signers);
1078 receipt_request_print(cms);
1080 } else if (operation == SMIME_VERIFY_RECEIPT) {
1081 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
1082 BIO_printf(bio_err, "Verification successful\n");
1084 BIO_printf(bio_err, "Verification failure\n");
1090 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1091 } else if (outformat == FORMAT_SMIME) {
1093 BIO_printf(out, "To: %s\n", to);
1095 BIO_printf(out, "From: %s\n", from);
1097 BIO_printf(out, "Subject: %s\n", subject);
1098 if (operation == SMIME_RESIGN)
1099 ret = SMIME_write_CMS(out, cms, indata, flags);
1101 ret = SMIME_write_CMS(out, cms, in, flags);
1102 } else if (outformat == FORMAT_PEM)
1103 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1104 else if (outformat == FORMAT_ASN1)
1105 ret = i2d_CMS_bio_stream(out, cms, in, flags);
1107 BIO_printf(bio_err, "Bad output format for CMS file\n");
1118 ERR_print_errors(bio_err);
1120 app_RAND_write_file(NULL);
1121 sk_X509_pop_free(encerts, X509_free);
1122 sk_X509_pop_free(other, X509_free);
1123 X509_VERIFY_PARAM_free(vpm);
1125 sk_OPENSSL_STRING_free(sksigners);
1127 sk_OPENSSL_STRING_free(skkeys);
1129 OPENSSL_free(secret_key);
1131 OPENSSL_free(secret_keyid);
1133 OPENSSL_free(pwri_tmp);
1134 ASN1_OBJECT_free(econtent_type);
1136 CMS_ReceiptRequest_free(rr);
1138 sk_OPENSSL_STRING_free(rr_to);
1140 sk_OPENSSL_STRING_free(rr_from);
1141 for (key_param = key_first; key_param;) {
1142 cms_key_param *tparam;
1143 sk_OPENSSL_STRING_free(key_param->param);
1144 tparam = key_param->next;
1145 OPENSSL_free(key_param);
1148 X509_STORE_free(store);
1153 CMS_ContentInfo_free(cms);
1154 CMS_ContentInfo_free(rcms);
1160 OPENSSL_free(passin);
1164 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1170 tmp = BIO_new_file(signerfile, "w");
1173 for (i = 0; i < sk_X509_num(signers); i++)
1174 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1179 /* Minimal callback just to output policy info (if any) */
1181 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1185 error = X509_STORE_CTX_get_error(ctx);
1189 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1190 && ((error != X509_V_OK) || (ok != 2)))
1193 policies_print(ctx);
1199 static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
1201 STACK_OF(GENERAL_NAME) *gens;
1205 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1206 gens = sk_GENERAL_NAMES_value(gns, i);
1207 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1208 gen = sk_GENERAL_NAME_value(gens, j);
1209 BIO_puts(bio_err, " ");
1210 GENERAL_NAME_print(bio_err, gen);
1211 BIO_puts(bio_err, "\n");
1217 static void receipt_request_print(CMS_ContentInfo *cms)
1219 STACK_OF(CMS_SignerInfo) *sis;
1221 CMS_ReceiptRequest *rr;
1223 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1226 sis = CMS_get0_SignerInfos(cms);
1227 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1228 si = sk_CMS_SignerInfo_value(sis, i);
1229 rv = CMS_get1_ReceiptRequest(si, &rr);
1230 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1232 BIO_puts(bio_err, " No Receipt Request\n");
1234 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1235 ERR_print_errors(bio_err);
1239 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1241 BIO_puts(bio_err, " Signed Content ID:\n");
1242 idlen = ASN1_STRING_length(scid);
1243 id = (char *)ASN1_STRING_data(scid);
1244 BIO_dump_indent(bio_err, id, idlen, 4);
1245 BIO_puts(bio_err, " Receipts From");
1247 BIO_puts(bio_err, " List:\n");
1248 gnames_stack_print(rlist);
1249 } else if (allorfirst == 1)
1250 BIO_puts(bio_err, ": First Tier\n");
1251 else if (allorfirst == 0)
1252 BIO_puts(bio_err, ": All\n");
1254 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
1255 BIO_puts(bio_err, " Receipts To:\n");
1256 gnames_stack_print(rto);
1259 CMS_ReceiptRequest_free(rr);
1263 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1266 STACK_OF(GENERAL_NAMES) *ret;
1267 GENERAL_NAMES *gens = NULL;
1268 GENERAL_NAME *gen = NULL;
1269 ret = sk_GENERAL_NAMES_new_null();
1272 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1273 char *str = sk_OPENSSL_STRING_value(ns, i);
1274 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1277 gens = GENERAL_NAMES_new();
1280 if (!sk_GENERAL_NAME_push(gens, gen))
1283 if (!sk_GENERAL_NAMES_push(ret, gens))
1292 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1294 GENERAL_NAMES_free(gens);
1296 GENERAL_NAME_free(gen);
1300 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING)
1301 *rr_to, int rr_allorfirst, STACK_OF(OPENSSL_STRING)
1304 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1305 CMS_ReceiptRequest *rr;
1306 rct_to = make_names_stack(rr_to);
1310 rct_from = make_names_stack(rr_from);
1315 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
1322 static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
1323 STACK_OF(OPENSSL_STRING) *param)
1327 if (sk_OPENSSL_STRING_num(param) <= 0)
1329 for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
1330 keyopt = sk_OPENSSL_STRING_value(param, i);
1331 if (pkey_ctrl_string(pctx, keyopt) <= 0) {
1332 BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
1333 ERR_print_errors(bio_err);