3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
55 /* CMS utility function */
61 #ifndef OPENSSL_NO_CMS
63 # include <openssl/crypto.h>
64 # include <openssl/pem.h>
65 # include <openssl/err.h>
66 # include <openssl/x509_vfy.h>
67 # include <openssl/x509v3.h>
68 # include <openssl/cms.h>
71 # define PROG cms_main
72 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
73 static int cms_cb(int ok, X509_STORE_CTX *ctx);
74 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
75 static CMS_ReceiptRequest *make_receipt_request(STACK * rr_to,
79 # define SMIME_OP 0x10
80 # define SMIME_IP 0x20
81 # define SMIME_SIGNERS 0x40
82 # define SMIME_ENCRYPT (1 | SMIME_OP)
83 # define SMIME_DECRYPT (2 | SMIME_IP)
84 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
85 # define SMIME_VERIFY (4 | SMIME_IP)
86 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
87 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
88 # define SMIME_DATAOUT (7 | SMIME_IP)
89 # define SMIME_DATA_CREATE (8 | SMIME_OP)
90 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
91 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
92 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
93 # define SMIME_COMPRESS (12 | SMIME_OP)
94 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
95 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
96 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
97 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
99 int MAIN(int, char **);
101 int MAIN(int argc, char **argv)
107 const char *inmode = "r", *outmode = "w";
108 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
109 char *signerfile = NULL, *recipfile = NULL;
110 STACK *sksigners = NULL, *skkeys = NULL;
111 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
112 char *certsoutfile = NULL;
113 const EVP_CIPHER *cipher = NULL;
114 CMS_ContentInfo *cms = NULL, *rcms = NULL;
115 X509_STORE *store = NULL;
116 X509 *cert = NULL, *recip = NULL, *signer = NULL;
117 EVP_PKEY *key = NULL;
118 STACK_OF(X509) *encerts = NULL, *other = NULL;
119 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
121 int flags = CMS_DETACHED;
122 int rr_print = 0, rr_allorfirst = -1;
123 STACK *rr_to = NULL, *rr_from = NULL;
124 CMS_ReceiptRequest *rr = NULL;
125 char *to = NULL, *from = NULL, *subject = NULL;
126 char *CAfile = NULL, *CApath = NULL;
127 char *passargin = NULL, *passin = NULL;
130 const EVP_MD *sign_md = NULL;
131 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
132 int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
133 # ifndef OPENSSL_NO_ENGINE
136 unsigned char *secret_key = NULL, *secret_keyid = NULL;
137 size_t secret_keylen = 0, secret_keyidlen = 0;
139 ASN1_OBJECT *econtent_type = NULL;
141 X509_VERIFY_PARAM *vpm = NULL;
148 if (bio_err == NULL) {
149 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
150 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
153 if (!load_config(bio_err, NULL))
156 while (!badarg && *args && *args[0] == '-') {
157 if (!strcmp(*args, "-encrypt"))
158 operation = SMIME_ENCRYPT;
159 else if (!strcmp(*args, "-decrypt"))
160 operation = SMIME_DECRYPT;
161 else if (!strcmp(*args, "-sign"))
162 operation = SMIME_SIGN;
163 else if (!strcmp(*args, "-sign_receipt"))
164 operation = SMIME_SIGN_RECEIPT;
165 else if (!strcmp(*args, "-resign"))
166 operation = SMIME_RESIGN;
167 else if (!strcmp(*args, "-verify"))
168 operation = SMIME_VERIFY;
169 else if (!strcmp(*args, "-verify_receipt")) {
170 operation = SMIME_VERIFY_RECEIPT;
175 } else if (!strcmp(*args, "-cmsout"))
176 operation = SMIME_CMSOUT;
177 else if (!strcmp(*args, "-data_out"))
178 operation = SMIME_DATAOUT;
179 else if (!strcmp(*args, "-data_create"))
180 operation = SMIME_DATA_CREATE;
181 else if (!strcmp(*args, "-digest_verify"))
182 operation = SMIME_DIGEST_VERIFY;
183 else if (!strcmp(*args, "-digest_create"))
184 operation = SMIME_DIGEST_CREATE;
185 else if (!strcmp(*args, "-compress"))
186 operation = SMIME_COMPRESS;
187 else if (!strcmp(*args, "-uncompress"))
188 operation = SMIME_UNCOMPRESS;
189 else if (!strcmp(*args, "-EncryptedData_decrypt"))
190 operation = SMIME_ENCRYPTED_DECRYPT;
191 else if (!strcmp(*args, "-EncryptedData_encrypt"))
192 operation = SMIME_ENCRYPTED_ENCRYPT;
193 # ifndef OPENSSL_NO_DES
194 else if (!strcmp(*args, "-des3"))
195 cipher = EVP_des_ede3_cbc();
196 else if (!strcmp(*args, "-des"))
197 cipher = EVP_des_cbc();
199 # ifndef OPENSSL_NO_SEED
200 else if (!strcmp(*args, "-seed"))
201 cipher = EVP_seed_cbc();
203 # ifndef OPENSSL_NO_RC2
204 else if (!strcmp(*args, "-rc2-40"))
205 cipher = EVP_rc2_40_cbc();
206 else if (!strcmp(*args, "-rc2-128"))
207 cipher = EVP_rc2_cbc();
208 else if (!strcmp(*args, "-rc2-64"))
209 cipher = EVP_rc2_64_cbc();
211 # ifndef OPENSSL_NO_AES
212 else if (!strcmp(*args, "-aes128"))
213 cipher = EVP_aes_128_cbc();
214 else if (!strcmp(*args, "-aes192"))
215 cipher = EVP_aes_192_cbc();
216 else if (!strcmp(*args, "-aes256"))
217 cipher = EVP_aes_256_cbc();
219 # ifndef OPENSSL_NO_CAMELLIA
220 else if (!strcmp(*args, "-camellia128"))
221 cipher = EVP_camellia_128_cbc();
222 else if (!strcmp(*args, "-camellia192"))
223 cipher = EVP_camellia_192_cbc();
224 else if (!strcmp(*args, "-camellia256"))
225 cipher = EVP_camellia_256_cbc();
227 else if (!strcmp(*args, "-debug_decrypt"))
228 flags |= CMS_DEBUG_DECRYPT;
229 else if (!strcmp(*args, "-text"))
231 else if (!strcmp(*args, "-nointern"))
232 flags |= CMS_NOINTERN;
233 else if (!strcmp(*args, "-noverify")
234 || !strcmp(*args, "-no_signer_cert_verify"))
235 flags |= CMS_NO_SIGNER_CERT_VERIFY;
236 else if (!strcmp(*args, "-nocerts"))
237 flags |= CMS_NOCERTS;
238 else if (!strcmp(*args, "-noattr"))
240 else if (!strcmp(*args, "-nodetach"))
241 flags &= ~CMS_DETACHED;
242 else if (!strcmp(*args, "-nosmimecap"))
243 flags |= CMS_NOSMIMECAP;
244 else if (!strcmp(*args, "-binary"))
246 else if (!strcmp(*args, "-keyid"))
247 flags |= CMS_USE_KEYID;
248 else if (!strcmp(*args, "-nosigs"))
250 else if (!strcmp(*args, "-no_content_verify"))
251 flags |= CMS_NO_CONTENT_VERIFY;
252 else if (!strcmp(*args, "-no_attr_verify"))
253 flags |= CMS_NO_ATTR_VERIFY;
254 else if (!strcmp(*args, "-stream")) {
257 } else if (!strcmp(*args, "-indef")) {
260 } else if (!strcmp(*args, "-noindef"))
261 flags &= ~CMS_STREAM;
262 else if (!strcmp(*args, "-nooldmime"))
263 flags |= CMS_NOOLDMIMETYPE;
264 else if (!strcmp(*args, "-crlfeol"))
265 flags |= CMS_CRLFEOL;
266 else if (!strcmp(*args, "-receipt_request_print"))
268 else if (!strcmp(*args, "-receipt_request_all"))
270 else if (!strcmp(*args, "-receipt_request_first"))
272 else if (!strcmp(*args, "-receipt_request_from")) {
277 rr_from = sk_new_null();
278 sk_push(rr_from, *args);
279 } else if (!strcmp(*args, "-receipt_request_to")) {
284 rr_to = sk_new_null();
285 sk_push(rr_to, *args);
286 } else if (!strcmp(*args, "-secretkey")) {
291 secret_key = string_to_hex(*args, <mp);
293 BIO_printf(bio_err, "Invalid key %s\n", *args);
296 secret_keylen = (size_t)ltmp;
297 } else if (!strcmp(*args, "-secretkeyid")) {
302 secret_keyid = string_to_hex(*args, <mp);
304 BIO_printf(bio_err, "Invalid id %s\n", *args);
307 secret_keyidlen = (size_t)ltmp;
308 } else if (!strcmp(*args, "-econtent_type")) {
312 econtent_type = OBJ_txt2obj(*args, 0);
313 if (!econtent_type) {
314 BIO_printf(bio_err, "Invalid OID %s\n", *args);
317 } else if (!strcmp(*args, "-rand")) {
324 # ifndef OPENSSL_NO_ENGINE
325 else if (!strcmp(*args, "-engine")) {
331 else if (!strcmp(*args, "-passin")) {
335 } else if (!strcmp(*args, "-to")) {
339 } else if (!strcmp(*args, "-from")) {
343 } else if (!strcmp(*args, "-subject")) {
347 } else if (!strcmp(*args, "-signer")) {
350 /* If previous -signer argument add signer to list */
354 sksigners = sk_new_null();
355 sk_push(sksigners, signerfile);
357 keyfile = signerfile;
359 skkeys = sk_new_null();
360 sk_push(skkeys, keyfile);
363 signerfile = *++args;
364 } else if (!strcmp(*args, "-recip")) {
368 } else if (!strcmp(*args, "-certsout")) {
371 certsoutfile = *++args;
372 } else if (!strcmp(*args, "-md")) {
375 sign_md = EVP_get_digestbyname(*++args);
376 if (sign_md == NULL) {
377 BIO_printf(bio_err, "Unknown digest %s\n", *args);
380 } else if (!strcmp(*args, "-inkey")) {
383 /* If previous -inkey arument add signer to list */
386 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
390 sksigners = sk_new_null();
391 sk_push(sksigners, signerfile);
394 skkeys = sk_new_null();
395 sk_push(skkeys, keyfile);
398 } else if (!strcmp(*args, "-keyform")) {
401 keyform = str2fmt(*++args);
402 } else if (!strcmp(*args, "-rctform")) {
405 rctformat = str2fmt(*++args);
406 } else if (!strcmp(*args, "-certfile")) {
410 } else if (!strcmp(*args, "-CAfile")) {
414 } else if (!strcmp(*args, "-CApath")) {
418 } else if (!strcmp(*args, "-in")) {
422 } else if (!strcmp(*args, "-inform")) {
425 informat = str2fmt(*++args);
426 } else if (!strcmp(*args, "-outform")) {
429 outformat = str2fmt(*++args);
430 } else if (!strcmp(*args, "-out")) {
434 } else if (!strcmp(*args, "-content")) {
438 } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
440 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
445 if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
446 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
450 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
451 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
454 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
455 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
459 if (operation & SMIME_SIGNERS) {
460 if (keyfile && !signerfile) {
461 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
464 /* Check to see if any final signer needs to be appended */
467 sksigners = sk_new_null();
468 sk_push(sksigners, signerfile);
470 skkeys = sk_new_null();
472 keyfile = signerfile;
473 sk_push(skkeys, keyfile);
476 BIO_printf(bio_err, "No signer certificate specified\n");
484 else if (operation == SMIME_DECRYPT) {
485 if (!recipfile && !keyfile && !secret_key) {
487 "No recipient certificate or key specified\n");
490 } else if (operation == SMIME_ENCRYPT) {
491 if (!*args && !secret_key) {
492 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
496 } else if (!operation)
501 BIO_printf(bio_err, "Usage cms [options] cert.pem ...\n");
502 BIO_printf(bio_err, "where options are\n");
503 BIO_printf(bio_err, "-encrypt encrypt message\n");
504 BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
505 BIO_printf(bio_err, "-sign sign message\n");
506 BIO_printf(bio_err, "-verify verify signed message\n");
507 BIO_printf(bio_err, "-cmsout output CMS structure\n");
508 # ifndef OPENSSL_NO_DES
509 BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
510 BIO_printf(bio_err, "-des encrypt with DES\n");
512 # ifndef OPENSSL_NO_SEED
513 BIO_printf(bio_err, "-seed encrypt with SEED\n");
515 # ifndef OPENSSL_NO_RC2
516 BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
517 BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
518 BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
520 # ifndef OPENSSL_NO_AES
521 BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
523 " encrypt PEM output with cbc aes\n");
525 # ifndef OPENSSL_NO_CAMELLIA
526 BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
528 " encrypt PEM output with cbc camellia\n");
531 "-nointern don't search certificates in message for signer\n");
533 "-nosigs don't verify message signature\n");
535 "-noverify don't verify signers certificate\n");
537 "-nocerts don't include signers certificate when signing\n");
538 BIO_printf(bio_err, "-nodetach use opaque signing\n");
540 "-noattr don't include any signed attributes\n");
542 "-binary don't translate message to text\n");
543 BIO_printf(bio_err, "-certfile file other certificates file\n");
544 BIO_printf(bio_err, "-certsout file certificate output file\n");
545 BIO_printf(bio_err, "-signer file signer certificate file\n");
547 "-recip file recipient certificate file for decryption\n");
548 BIO_printf(bio_err, "-keyid use subject key identifier\n");
549 BIO_printf(bio_err, "-in file input file\n");
551 "-inform arg input format SMIME (default), PEM or DER\n");
553 "-inkey file input private key (if not signer or recipient)\n");
555 "-keyform arg input private key format (PEM or ENGINE)\n");
556 BIO_printf(bio_err, "-out file output file\n");
558 "-outform arg output format SMIME (default), PEM or DER\n");
560 "-content file supply or override content for detached signature\n");
561 BIO_printf(bio_err, "-to addr to address\n");
562 BIO_printf(bio_err, "-from ad from address\n");
563 BIO_printf(bio_err, "-subject s subject\n");
565 "-text include or delete text MIME headers\n");
567 "-CApath dir trusted certificates directory\n");
568 BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
570 "-crl_check check revocation status of signer's certificate using CRLs\n");
572 "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
573 # ifndef OPENSSL_NO_ENGINE
575 "-engine e use engine e, possibly a hardware device.\n");
577 BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
578 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR,
579 LIST_SEPARATOR_CHAR);
581 " load the file (or the files in the directory) into\n");
582 BIO_printf(bio_err, " the random number generator\n");
584 "cert.pem recipient certificate(s) for encryption\n");
587 # ifndef OPENSSL_NO_ENGINE
588 e = setup_engine(bio_err, engine, 0);
591 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
592 BIO_printf(bio_err, "Error getting password\n");
597 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
599 BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
600 app_RAND_load_files(inrand));
605 if (!(operation & SMIME_SIGNERS))
606 flags &= ~CMS_DETACHED;
608 if (operation & SMIME_OP) {
609 if (outformat == FORMAT_ASN1)
612 if (flags & CMS_BINARY)
616 if (operation & SMIME_IP) {
617 if (informat == FORMAT_ASN1)
620 if (flags & CMS_BINARY)
624 if (operation == SMIME_ENCRYPT) {
626 # ifndef OPENSSL_NO_DES
627 cipher = EVP_des_ede3_cbc();
629 BIO_printf(bio_err, "No cipher selected\n");
634 if (secret_key && !secret_keyid) {
635 BIO_printf(bio_err, "No sectre key id\n");
640 encerts = sk_X509_new_null();
642 if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
643 NULL, e, "recipient certificate file")))
645 sk_X509_push(encerts, cert);
652 if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
653 e, "certificate file"))) {
654 ERR_print_errors(bio_err);
659 if (recipfile && (operation == SMIME_DECRYPT)) {
660 if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
661 e, "recipient certificate file"))) {
662 ERR_print_errors(bio_err);
667 if (operation == SMIME_SIGN_RECEIPT) {
668 if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
669 e, "receipt signer certificate file"))) {
670 ERR_print_errors(bio_err);
675 if (operation == SMIME_DECRYPT) {
678 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
680 keyfile = signerfile;
685 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
692 if (!(in = BIO_new_file(infile, inmode))) {
693 BIO_printf(bio_err, "Can't open input file %s\n", infile);
697 in = BIO_new_fp(stdin, BIO_NOCLOSE);
699 if (operation & SMIME_IP) {
700 if (informat == FORMAT_SMIME)
701 cms = SMIME_read_CMS(in, &indata);
702 else if (informat == FORMAT_PEM)
703 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
704 else if (informat == FORMAT_ASN1)
705 cms = d2i_CMS_bio(in, NULL);
707 BIO_printf(bio_err, "Bad input format for CMS file\n");
712 BIO_printf(bio_err, "Error reading S/MIME message\n");
717 if (!(indata = BIO_new_file(contfile, "rb"))) {
718 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
723 STACK_OF(X509) *allcerts;
724 allcerts = CMS_get1_certs(cms);
725 if (!save_certs(certsoutfile, allcerts)) {
727 "Error writing certs to %s\n", certsoutfile);
731 sk_X509_pop_free(allcerts, X509_free);
736 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
737 if (!(rctin = BIO_new_file(rctfile, rctmode))) {
738 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
742 if (rctformat == FORMAT_SMIME)
743 rcms = SMIME_read_CMS(rctin, NULL);
744 else if (rctformat == FORMAT_PEM)
745 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
746 else if (rctformat == FORMAT_ASN1)
747 rcms = d2i_CMS_bio(rctin, NULL);
749 BIO_printf(bio_err, "Bad input format for receipt\n");
754 BIO_printf(bio_err, "Error reading receipt\n");
760 if (!(out = BIO_new_file(outfile, outmode))) {
761 BIO_printf(bio_err, "Can't open output file %s\n", outfile);
765 out = BIO_new_fp(stdout, BIO_NOCLOSE);
766 # ifdef OPENSSL_SYS_VMS
768 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
769 out = BIO_push(tmpbio, out);
774 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
775 if (!(store = setup_verify(bio_err, CAfile, CApath)))
777 X509_STORE_set_verify_cb_func(store, cms_cb);
779 X509_STORE_set1_param(store, vpm);
784 if (operation == SMIME_DATA_CREATE) {
785 cms = CMS_data_create(in, flags);
786 } else if (operation == SMIME_DIGEST_CREATE) {
787 cms = CMS_digest_create(in, sign_md, flags);
788 } else if (operation == SMIME_COMPRESS) {
789 cms = CMS_compress(in, -1, flags);
790 } else if (operation == SMIME_ENCRYPT) {
791 flags |= CMS_PARTIAL;
792 cms = CMS_encrypt(encerts, in, cipher, flags);
796 if (!CMS_add0_recipient_key(cms, NID_undef,
797 secret_key, secret_keylen,
798 secret_keyid, secret_keyidlen,
801 /* NULL these because call absorbs them */
805 if (!(flags & CMS_STREAM)) {
806 if (!CMS_final(cms, in, NULL, flags))
809 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
810 cms = CMS_EncryptedData_encrypt(in, cipher,
811 secret_key, secret_keylen, flags);
813 } else if (operation == SMIME_SIGN_RECEIPT) {
814 CMS_ContentInfo *srcms = NULL;
815 STACK_OF(CMS_SignerInfo) *sis;
817 sis = CMS_get0_SignerInfos(cms);
820 si = sk_CMS_SignerInfo_value(sis, 0);
821 srcms = CMS_sign_receipt(si, signer, key, other, flags);
824 CMS_ContentInfo_free(cms);
826 } else if (operation & SMIME_SIGNERS) {
829 * If detached data content we enable streaming if S/MIME output
832 if (operation == SMIME_SIGN) {
834 if (flags & CMS_DETACHED) {
835 if (outformat == FORMAT_SMIME)
838 flags |= CMS_PARTIAL;
839 cms = CMS_sign(NULL, NULL, other, in, flags);
843 CMS_set1_eContentType(cms, econtent_type);
846 rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);
849 "Signed Receipt Request Creation Error\n");
854 flags |= CMS_REUSE_DIGEST;
855 for (i = 0; i < sk_num(sksigners); i++) {
857 signerfile = sk_value(sksigners, i);
858 keyfile = sk_value(skkeys, i);
859 signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
860 e, "signer certificate");
863 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
867 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
870 if (rr && !CMS_add1_ReceiptRequest(si, rr))
877 /* If not streaming or resigning finalize structure */
878 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
879 if (!CMS_final(cms, in, NULL, flags))
885 BIO_printf(bio_err, "Error creating CMS structure\n");
890 if (operation == SMIME_DECRYPT) {
891 if (flags & CMS_DEBUG_DECRYPT)
892 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
895 if (!CMS_decrypt_set1_key(cms,
896 secret_key, secret_keylen,
897 secret_keyid, secret_keyidlen)) {
898 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
904 if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
905 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
910 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
911 BIO_printf(bio_err, "Error decrypting CMS structure\n");
914 } else if (operation == SMIME_DATAOUT) {
915 if (!CMS_data(cms, out, flags))
917 } else if (operation == SMIME_UNCOMPRESS) {
918 if (!CMS_uncompress(cms, indata, out, flags))
920 } else if (operation == SMIME_DIGEST_VERIFY) {
921 if (CMS_digest_verify(cms, indata, out, flags) > 0)
922 BIO_printf(bio_err, "Verification successful\n");
924 BIO_printf(bio_err, "Verification failure\n");
927 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
928 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
931 } else if (operation == SMIME_VERIFY) {
932 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
933 BIO_printf(bio_err, "Verification successful\n");
935 BIO_printf(bio_err, "Verification failure\n");
939 STACK_OF(X509) *signers;
940 signers = CMS_get0_signers(cms);
941 if (!save_certs(signerfile, signers)) {
943 "Error writing signers to %s\n", signerfile);
947 sk_X509_free(signers);
950 receipt_request_print(bio_err, cms);
952 } else if (operation == SMIME_VERIFY_RECEIPT) {
953 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
954 BIO_printf(bio_err, "Verification successful\n");
956 BIO_printf(bio_err, "Verification failure\n");
960 if (outformat == FORMAT_SMIME) {
962 BIO_printf(out, "To: %s\n", to);
964 BIO_printf(out, "From: %s\n", from);
966 BIO_printf(out, "Subject: %s\n", subject);
967 if (operation == SMIME_RESIGN)
968 ret = SMIME_write_CMS(out, cms, indata, flags);
970 ret = SMIME_write_CMS(out, cms, in, flags);
971 } else if (outformat == FORMAT_PEM)
972 ret = PEM_write_bio_CMS(out, cms);
973 else if (outformat == FORMAT_ASN1)
974 ret = i2d_CMS_bio(out, cms);
976 BIO_printf(bio_err, "Bad output format for CMS file\n");
987 ERR_print_errors(bio_err);
989 app_RAND_write_file(NULL, bio_err);
990 sk_X509_pop_free(encerts, X509_free);
991 sk_X509_pop_free(other, X509_free);
993 X509_VERIFY_PARAM_free(vpm);
999 OPENSSL_free(secret_key);
1001 OPENSSL_free(secret_keyid);
1003 ASN1_OBJECT_free(econtent_type);
1005 CMS_ReceiptRequest_free(rr);
1010 X509_STORE_free(store);
1015 CMS_ContentInfo_free(cms);
1016 CMS_ContentInfo_free(rcms);
1022 OPENSSL_free(passin);
1026 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1032 tmp = BIO_new_file(signerfile, "w");
1035 for (i = 0; i < sk_X509_num(signers); i++)
1036 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1041 /* Minimal callback just to output policy info (if any) */
1043 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1047 error = X509_STORE_CTX_get_error(ctx);
1049 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1050 && ((error != X509_V_OK) || (ok != 2)))
1053 policies_print(NULL, ctx);
1059 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1061 STACK_OF(GENERAL_NAME) *gens;
1064 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1065 gens = sk_GENERAL_NAMES_value(gns, i);
1066 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1067 gen = sk_GENERAL_NAME_value(gens, j);
1069 GENERAL_NAME_print(out, gen);
1070 BIO_puts(out, "\n");
1076 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1078 STACK_OF(CMS_SignerInfo) *sis;
1080 CMS_ReceiptRequest *rr;
1082 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1085 sis = CMS_get0_SignerInfos(cms);
1086 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1087 si = sk_CMS_SignerInfo_value(sis, i);
1088 rv = CMS_get1_ReceiptRequest(si, &rr);
1089 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1091 BIO_puts(bio_err, " No Receipt Request\n");
1093 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1094 ERR_print_errors(bio_err);
1098 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1100 BIO_puts(out, " Signed Content ID:\n");
1101 idlen = ASN1_STRING_length(scid);
1102 id = (char *)ASN1_STRING_data(scid);
1103 BIO_dump_indent(out, id, idlen, 4);
1104 BIO_puts(out, " Receipts From");
1106 BIO_puts(out, " List:\n");
1107 gnames_stack_print(out, rlist);
1108 } else if (allorfirst == 1)
1109 BIO_puts(out, ": First Tier\n");
1110 else if (allorfirst == 0)
1111 BIO_puts(out, ": All\n");
1113 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1114 BIO_puts(out, " Receipts To:\n");
1115 gnames_stack_print(out, rto);
1118 CMS_ReceiptRequest_free(rr);
1122 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK * ns)
1125 STACK_OF(GENERAL_NAMES) *ret;
1126 GENERAL_NAMES *gens = NULL;
1127 GENERAL_NAME *gen = NULL;
1128 ret = sk_GENERAL_NAMES_new_null();
1131 for (i = 0; i < sk_num(ns); i++) {
1134 cnf.value = sk_value(ns, i);
1135 gen = v2i_GENERAL_NAME(NULL, NULL, &cnf);
1138 gens = GENERAL_NAMES_new();
1141 if (!sk_GENERAL_NAME_push(gens, gen))
1144 if (!sk_GENERAL_NAMES_push(ret, gens))
1153 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1155 GENERAL_NAMES_free(gens);
1157 GENERAL_NAME_free(gen);
1161 static CMS_ReceiptRequest *make_receipt_request(STACK * rr_to,
1165 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1166 CMS_ReceiptRequest *rr;
1167 rct_to = make_names_stack(rr_to);
1171 rct_from = make_names_stack(rr_from);
1176 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,