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>
71 static int save_certs(char *signerfile, STACK_OF(X509) *signers);
72 static int cms_cb(int ok, X509_STORE_CTX *ctx);
73 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
74 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to,
76 STACK_OF(OPENSSL_STRING) *rr_from);
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 int MAIN(int, char **);
102 int MAIN(int argc, char **argv)
108 const char *inmode = "r", *outmode = "w";
109 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
110 char *signerfile = NULL, *recipfile = NULL;
111 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
112 char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
113 char *certsoutfile = NULL;
114 const EVP_CIPHER *cipher = NULL;
115 CMS_ContentInfo *cms = NULL, *rcms = NULL;
116 X509_STORE *store = NULL;
117 X509 *cert = NULL, *recip = NULL, *signer = NULL;
118 EVP_PKEY *key = NULL;
119 STACK_OF(X509) *encerts = NULL, *other = NULL;
120 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
122 int flags = CMS_DETACHED, noout = 0, print = 0;
123 int verify_retcode = 0;
124 int rr_print = 0, rr_allorfirst = -1;
125 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
126 CMS_ReceiptRequest *rr = NULL;
127 char *to = NULL, *from = NULL, *subject = NULL;
128 char *CAfile = NULL, *CApath = NULL;
129 char *passargin = NULL, *passin = NULL;
132 const EVP_MD *sign_md = NULL;
133 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
134 int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
135 #ifndef OPENSSL_NO_ENGINE
138 unsigned char *secret_key = NULL, *secret_keyid = NULL;
139 size_t secret_keylen = 0, secret_keyidlen = 0;
141 ASN1_OBJECT *econtent_type = NULL;
143 X509_VERIFY_PARAM *vpm = NULL;
152 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
153 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
156 if (!load_config(bio_err, NULL))
159 while (!badarg && *args && *args[0] == '-')
161 if (!strcmp (*args, "-encrypt"))
162 operation = SMIME_ENCRYPT;
163 else if (!strcmp (*args, "-decrypt"))
164 operation = SMIME_DECRYPT;
165 else if (!strcmp (*args, "-sign"))
166 operation = SMIME_SIGN;
167 else if (!strcmp (*args, "-sign_receipt"))
168 operation = SMIME_SIGN_RECEIPT;
169 else if (!strcmp (*args, "-resign"))
170 operation = SMIME_RESIGN;
171 else if (!strcmp (*args, "-verify"))
172 operation = SMIME_VERIFY;
173 else if (!strcmp (*args, "-verify_retcode"))
175 else if (!strcmp(*args,"-verify_receipt"))
177 operation = SMIME_VERIFY_RECEIPT;
183 else if (!strcmp (*args, "-cmsout"))
184 operation = SMIME_CMSOUT;
185 else if (!strcmp (*args, "-data_out"))
186 operation = SMIME_DATAOUT;
187 else if (!strcmp (*args, "-data_create"))
188 operation = SMIME_DATA_CREATE;
189 else if (!strcmp (*args, "-digest_verify"))
190 operation = SMIME_DIGEST_VERIFY;
191 else if (!strcmp (*args, "-digest_create"))
192 operation = SMIME_DIGEST_CREATE;
193 else if (!strcmp (*args, "-compress"))
194 operation = SMIME_COMPRESS;
195 else if (!strcmp (*args, "-uncompress"))
196 operation = SMIME_UNCOMPRESS;
197 else if (!strcmp (*args, "-EncryptedData_decrypt"))
198 operation = SMIME_ENCRYPTED_DECRYPT;
199 else if (!strcmp (*args, "-EncryptedData_encrypt"))
200 operation = SMIME_ENCRYPTED_ENCRYPT;
201 #ifndef OPENSSL_NO_DES
202 else if (!strcmp (*args, "-des3"))
203 cipher = EVP_des_ede3_cbc();
204 else if (!strcmp (*args, "-des"))
205 cipher = EVP_des_cbc();
207 #ifndef OPENSSL_NO_SEED
208 else if (!strcmp (*args, "-seed"))
209 cipher = EVP_seed_cbc();
211 #ifndef OPENSSL_NO_RC2
212 else if (!strcmp (*args, "-rc2-40"))
213 cipher = EVP_rc2_40_cbc();
214 else if (!strcmp (*args, "-rc2-128"))
215 cipher = EVP_rc2_cbc();
216 else if (!strcmp (*args, "-rc2-64"))
217 cipher = EVP_rc2_64_cbc();
219 #ifndef OPENSSL_NO_AES
220 else if (!strcmp(*args,"-aes128"))
221 cipher = EVP_aes_128_cbc();
222 else if (!strcmp(*args,"-aes192"))
223 cipher = EVP_aes_192_cbc();
224 else if (!strcmp(*args,"-aes256"))
225 cipher = EVP_aes_256_cbc();
227 #ifndef OPENSSL_NO_CAMELLIA
228 else if (!strcmp(*args,"-camellia128"))
229 cipher = EVP_camellia_128_cbc();
230 else if (!strcmp(*args,"-camellia192"))
231 cipher = EVP_camellia_192_cbc();
232 else if (!strcmp(*args,"-camellia256"))
233 cipher = EVP_camellia_256_cbc();
235 else if (!strcmp (*args, "-debug_decrypt"))
236 flags |= CMS_DEBUG_DECRYPT;
237 else if (!strcmp (*args, "-text"))
239 else if (!strcmp (*args, "-nointern"))
240 flags |= CMS_NOINTERN;
241 else if (!strcmp (*args, "-noverify")
242 || !strcmp (*args, "-no_signer_cert_verify"))
243 flags |= CMS_NO_SIGNER_CERT_VERIFY;
244 else if (!strcmp (*args, "-nocerts"))
245 flags |= CMS_NOCERTS;
246 else if (!strcmp (*args, "-noattr"))
248 else if (!strcmp (*args, "-nodetach"))
249 flags &= ~CMS_DETACHED;
250 else if (!strcmp (*args, "-nosmimecap"))
251 flags |= CMS_NOSMIMECAP;
252 else if (!strcmp (*args, "-binary"))
254 else if (!strcmp (*args, "-keyid"))
255 flags |= CMS_USE_KEYID;
256 else if (!strcmp (*args, "-nosigs"))
258 else if (!strcmp (*args, "-no_content_verify"))
259 flags |= CMS_NO_CONTENT_VERIFY;
260 else if (!strcmp (*args, "-no_attr_verify"))
261 flags |= CMS_NO_ATTR_VERIFY;
262 else if (!strcmp (*args, "-stream"))
264 else if (!strcmp (*args, "-indef"))
266 else if (!strcmp (*args, "-noindef"))
267 flags &= ~CMS_STREAM;
268 else if (!strcmp (*args, "-nooldmime"))
269 flags |= CMS_NOOLDMIMETYPE;
270 else if (!strcmp (*args, "-crlfeol"))
271 flags |= CMS_CRLFEOL;
272 else if (!strcmp (*args, "-noout"))
274 else if (!strcmp (*args, "-receipt_request_print"))
276 else if (!strcmp (*args, "-receipt_request_all"))
278 else if (!strcmp (*args, "-receipt_request_first"))
280 else if (!strcmp(*args,"-receipt_request_from"))
286 rr_from = sk_OPENSSL_STRING_new_null();
287 sk_OPENSSL_STRING_push(rr_from, *args);
289 else if (!strcmp(*args,"-receipt_request_to"))
295 rr_to = sk_OPENSSL_STRING_new_null();
296 sk_OPENSSL_STRING_push(rr_to, *args);
298 else if (!strcmp (*args, "-print"))
303 else if (!strcmp(*args,"-secretkey"))
309 secret_key = string_to_hex(*args, <mp);
312 BIO_printf(bio_err, "Invalid key %s\n", *args);
315 secret_keylen = (size_t)ltmp;
317 else if (!strcmp(*args,"-secretkeyid"))
323 secret_keyid = string_to_hex(*args, <mp);
326 BIO_printf(bio_err, "Invalid id %s\n", *args);
329 secret_keyidlen = (size_t)ltmp;
331 else if (!strcmp(*args,"-econtent_type"))
336 econtent_type = OBJ_txt2obj(*args, 0);
339 BIO_printf(bio_err, "Invalid OID %s\n", *args);
343 else if (!strcmp(*args,"-rand"))
351 #ifndef OPENSSL_NO_ENGINE
352 else if (!strcmp(*args,"-engine"))
359 else if (!strcmp(*args,"-passin"))
365 else if (!strcmp (*args, "-to"))
371 else if (!strcmp (*args, "-from"))
377 else if (!strcmp (*args, "-subject"))
383 else if (!strcmp (*args, "-signer"))
387 /* If previous -signer argument add signer to list */
392 sksigners = sk_OPENSSL_STRING_new_null();
393 sk_OPENSSL_STRING_push(sksigners, signerfile);
395 keyfile = signerfile;
397 skkeys = sk_OPENSSL_STRING_new_null();
398 sk_OPENSSL_STRING_push(skkeys, keyfile);
401 signerfile = *++args;
403 else if (!strcmp (*args, "-recip"))
409 else if (!strcmp (*args, "-certsout"))
413 certsoutfile = *++args;
415 else if (!strcmp (*args, "-md"))
419 sign_md = EVP_get_digestbyname(*++args);
422 BIO_printf(bio_err, "Unknown digest %s\n",
427 else if (!strcmp (*args, "-inkey"))
431 /* If previous -inkey arument add signer to list */
436 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
440 sksigners = sk_OPENSSL_STRING_new_null();
441 sk_OPENSSL_STRING_push(sksigners, signerfile);
444 skkeys = sk_OPENSSL_STRING_new_null();
445 sk_OPENSSL_STRING_push(skkeys, keyfile);
449 else if (!strcmp (*args, "-keyform"))
453 keyform = str2fmt(*++args);
455 else if (!strcmp (*args, "-rctform"))
459 rctformat = str2fmt(*++args);
461 else if (!strcmp (*args, "-certfile"))
467 else if (!strcmp (*args, "-CAfile"))
473 else if (!strcmp (*args, "-CApath"))
479 else if (!strcmp (*args, "-in"))
485 else if (!strcmp (*args, "-inform"))
489 informat = str2fmt(*++args);
491 else if (!strcmp (*args, "-outform"))
495 outformat = str2fmt(*++args);
497 else if (!strcmp (*args, "-out"))
503 else if (!strcmp (*args, "-content"))
509 else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
511 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
516 if (((rr_allorfirst != -1) || rr_from) && !rr_to)
518 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
522 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from))
524 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
527 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners))
529 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
533 if (operation & SMIME_SIGNERS)
535 if (keyfile && !signerfile)
537 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
540 /* Check to see if any final signer needs to be appended */
544 sksigners = sk_OPENSSL_STRING_new_null();
545 sk_OPENSSL_STRING_push(sksigners, signerfile);
547 skkeys = sk_OPENSSL_STRING_new_null();
549 keyfile = signerfile;
550 sk_OPENSSL_STRING_push(skkeys, keyfile);
554 BIO_printf(bio_err, "No signer certificate specified\n");
562 else if (operation == SMIME_DECRYPT)
564 if (!recipfile && !keyfile && !secret_key)
566 BIO_printf(bio_err, "No recipient certificate or key specified\n");
570 else if (operation == SMIME_ENCRYPT)
572 if (!*args && !secret_key)
574 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
585 BIO_printf (bio_err, "Usage cms [options] cert.pem ...\n");
586 BIO_printf (bio_err, "where options are\n");
587 BIO_printf (bio_err, "-encrypt encrypt message\n");
588 BIO_printf (bio_err, "-decrypt decrypt encrypted message\n");
589 BIO_printf (bio_err, "-sign sign message\n");
590 BIO_printf (bio_err, "-verify verify signed message\n");
591 BIO_printf (bio_err, "-cmsout output CMS structure\n");
592 #ifndef OPENSSL_NO_DES
593 BIO_printf (bio_err, "-des3 encrypt with triple DES\n");
594 BIO_printf (bio_err, "-des encrypt with DES\n");
596 #ifndef OPENSSL_NO_SEED
597 BIO_printf (bio_err, "-seed encrypt with SEED\n");
599 #ifndef OPENSSL_NO_RC2
600 BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
601 BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n");
602 BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n");
604 #ifndef OPENSSL_NO_AES
605 BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
606 BIO_printf (bio_err, " encrypt PEM output with cbc aes\n");
608 #ifndef OPENSSL_NO_CAMELLIA
609 BIO_printf (bio_err, "-camellia128, -camellia192, -camellia256\n");
610 BIO_printf (bio_err, " encrypt PEM output with cbc camellia\n");
612 BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n");
613 BIO_printf (bio_err, "-nosigs don't verify message signature\n");
614 BIO_printf (bio_err, "-noverify don't verify signers certificate\n");
615 BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n");
616 BIO_printf (bio_err, "-nodetach use opaque signing\n");
617 BIO_printf (bio_err, "-noattr don't include any signed attributes\n");
618 BIO_printf (bio_err, "-binary don't translate message to text\n");
619 BIO_printf (bio_err, "-certfile file other certificates file\n");
620 BIO_printf (bio_err, "-certsout file certificate output file\n");
621 BIO_printf (bio_err, "-signer file signer certificate file\n");
622 BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n");
623 BIO_printf (bio_err, "-keyid use subject key identifier\n");
624 BIO_printf (bio_err, "-in file input file\n");
625 BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
626 BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n");
627 BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
628 BIO_printf (bio_err, "-out file output file\n");
629 BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
630 BIO_printf (bio_err, "-content file supply or override content for detached signature\n");
631 BIO_printf (bio_err, "-to addr to address\n");
632 BIO_printf (bio_err, "-from ad from address\n");
633 BIO_printf (bio_err, "-subject s subject\n");
634 BIO_printf (bio_err, "-text include or delete text MIME headers\n");
635 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
636 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
637 BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
638 BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
639 #ifndef OPENSSL_NO_ENGINE
640 BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n");
642 BIO_printf (bio_err, "-passin arg input file pass phrase source\n");
643 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
644 BIO_printf(bio_err, " load the file (or the files in the directory) into\n");
645 BIO_printf(bio_err, " the random number generator\n");
646 BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
650 #ifndef OPENSSL_NO_ENGINE
651 e = setup_engine(bio_err, engine, 0);
654 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
656 BIO_printf(bio_err, "Error getting password\n");
662 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
664 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
665 app_RAND_load_files(inrand));
670 if (!(operation & SMIME_SIGNERS))
671 flags &= ~CMS_DETACHED;
673 if (operation & SMIME_OP)
675 if (outformat == FORMAT_ASN1)
680 if (flags & CMS_BINARY)
684 if (operation & SMIME_IP)
686 if (informat == FORMAT_ASN1)
691 if (flags & CMS_BINARY)
695 if (operation == SMIME_ENCRYPT)
699 #ifndef OPENSSL_NO_DES
700 cipher = EVP_des_ede3_cbc();
702 BIO_printf(bio_err, "No cipher selected\n");
707 if (secret_key && !secret_keyid)
709 BIO_printf(bio_err, "No secret key id\n");
714 encerts = sk_X509_new_null();
717 if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
718 NULL, e, "recipient certificate file")))
720 sk_X509_push(encerts, cert);
728 if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
729 e, "certificate file")))
731 ERR_print_errors(bio_err);
736 if (recipfile && (operation == SMIME_DECRYPT))
738 if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
739 e, "recipient certificate file")))
741 ERR_print_errors(bio_err);
746 if (operation == SMIME_SIGN_RECEIPT)
748 if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM,NULL,
749 e, "receipt signer certificate file")))
751 ERR_print_errors(bio_err);
756 if (operation == SMIME_DECRYPT)
761 else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT))
764 keyfile = signerfile;
770 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
778 if (!(in = BIO_new_file(infile, inmode)))
781 "Can't open input file %s\n", infile);
786 in = BIO_new_fp(stdin, BIO_NOCLOSE);
788 if (operation & SMIME_IP)
790 if (informat == FORMAT_SMIME)
791 cms = SMIME_read_CMS(in, &indata);
792 else if (informat == FORMAT_PEM)
793 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
794 else if (informat == FORMAT_ASN1)
795 cms = d2i_CMS_bio(in, NULL);
798 BIO_printf(bio_err, "Bad input format for CMS file\n");
804 BIO_printf(bio_err, "Error reading S/MIME message\n");
810 if (!(indata = BIO_new_file(contfile, "rb")))
812 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
818 STACK_OF(X509) *allcerts;
819 allcerts = CMS_get1_certs(cms);
820 if (!save_certs(certsoutfile, allcerts))
823 "Error writing certs to %s\n",
828 sk_X509_pop_free(allcerts, X509_free);
834 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
835 if (!(rctin = BIO_new_file(rctfile, rctmode)))
838 "Can't open receipt file %s\n", rctfile);
842 if (rctformat == FORMAT_SMIME)
843 rcms = SMIME_read_CMS(rctin, NULL);
844 else if (rctformat == FORMAT_PEM)
845 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
846 else if (rctformat == FORMAT_ASN1)
847 rcms = d2i_CMS_bio(rctin, NULL);
850 BIO_printf(bio_err, "Bad input format for receipt\n");
856 BIO_printf(bio_err, "Error reading receipt\n");
863 if (!(out = BIO_new_file(outfile, outmode)))
866 "Can't open output file %s\n", outfile);
872 out = BIO_new_fp(stdout, BIO_NOCLOSE);
873 #ifdef OPENSSL_SYS_VMS
875 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
876 out = BIO_push(tmpbio, out);
881 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT))
883 if (!(store = setup_verify(bio_err, CAfile, CApath)))
885 X509_STORE_set_verify_cb(store, cms_cb);
887 X509_STORE_set1_param(store, vpm);
893 if (operation == SMIME_DATA_CREATE)
895 cms = CMS_data_create(in, flags);
897 else if (operation == SMIME_DIGEST_CREATE)
899 cms = CMS_digest_create(in, sign_md, flags);
901 else if (operation == SMIME_COMPRESS)
903 cms = CMS_compress(in, -1, flags);
905 else if (operation == SMIME_ENCRYPT)
907 flags |= CMS_PARTIAL;
908 cms = CMS_encrypt(encerts, in, cipher, flags);
913 if (!CMS_add0_recipient_key(cms, NID_undef,
914 secret_key, secret_keylen,
915 secret_keyid, secret_keyidlen,
918 /* NULL these because call absorbs them */
922 if (!(flags & CMS_STREAM))
924 if (!CMS_final(cms, in, NULL, flags))
928 else if (operation == SMIME_ENCRYPTED_ENCRYPT)
930 cms = CMS_EncryptedData_encrypt(in, cipher,
931 secret_key, secret_keylen,
935 else if (operation == SMIME_SIGN_RECEIPT)
937 CMS_ContentInfo *srcms = NULL;
938 STACK_OF(CMS_SignerInfo) *sis;
940 sis = CMS_get0_SignerInfos(cms);
943 si = sk_CMS_SignerInfo_value(sis, 0);
944 srcms = CMS_sign_receipt(si, signer, key, other, flags);
947 CMS_ContentInfo_free(cms);
950 else if (operation & SMIME_SIGNERS)
953 /* If detached data content we enable streaming if
954 * S/MIME output format.
956 if (operation == SMIME_SIGN)
959 if (flags & CMS_DETACHED)
961 if (outformat == FORMAT_SMIME)
964 flags |= CMS_PARTIAL;
965 cms = CMS_sign(NULL, NULL, other, in, flags);
969 CMS_set1_eContentType(cms, econtent_type);
973 rr = make_receipt_request(rr_to, rr_allorfirst,
978 "Signed Receipt Request Creation Error\n");
984 flags |= CMS_REUSE_DIGEST;
985 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++)
988 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
989 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
990 signer = load_cert(bio_err, signerfile,FORMAT_PEM, NULL,
991 e, "signer certificate");
994 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
998 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
1001 if (rr && !CMS_add1_ReceiptRequest(si, rr))
1008 /* If not streaming or resigning finalize structure */
1009 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM))
1011 if (!CMS_final(cms, in, NULL, flags))
1018 BIO_printf(bio_err, "Error creating CMS structure\n");
1023 if (operation == SMIME_DECRYPT)
1025 if (flags & CMS_DEBUG_DECRYPT)
1026 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
1030 if (!CMS_decrypt_set1_key(cms,
1031 secret_key, secret_keylen,
1032 secret_keyid, secret_keyidlen))
1035 "Error decrypting CMS using secret key\n");
1042 if (!CMS_decrypt_set1_pkey(cms, key, recip))
1045 "Error decrypting CMS using private key\n");
1050 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags))
1052 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1056 else if (operation == SMIME_DATAOUT)
1058 if (!CMS_data(cms, out, flags))
1061 else if (operation == SMIME_UNCOMPRESS)
1063 if (!CMS_uncompress(cms, indata, out, flags))
1066 else if (operation == SMIME_DIGEST_VERIFY)
1068 if (CMS_digest_verify(cms, indata, out, flags) > 0)
1069 BIO_printf(bio_err, "Verification successful\n");
1072 BIO_printf(bio_err, "Verification failure\n");
1076 else if (operation == SMIME_ENCRYPTED_DECRYPT)
1078 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1079 indata, out, flags))
1082 else if (operation == SMIME_VERIFY)
1084 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
1085 BIO_printf(bio_err, "Verification successful\n");
1088 BIO_printf(bio_err, "Verification failure\n");
1090 ret = verify_err + 32;
1095 STACK_OF(X509) *signers;
1096 signers = CMS_get0_signers(cms);
1097 if (!save_certs(signerfile, signers))
1100 "Error writing signers to %s\n",
1105 sk_X509_free(signers);
1108 receipt_request_print(bio_err, cms);
1111 else if (operation == SMIME_VERIFY_RECEIPT)
1113 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
1114 BIO_printf(bio_err, "Verification successful\n");
1117 BIO_printf(bio_err, "Verification failure\n");
1126 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1128 else if (outformat == FORMAT_SMIME)
1131 BIO_printf(out, "To: %s\n", to);
1133 BIO_printf(out, "From: %s\n", from);
1135 BIO_printf(out, "Subject: %s\n", subject);
1136 if (operation == SMIME_RESIGN)
1137 ret = SMIME_write_CMS(out, cms, indata, flags);
1139 ret = SMIME_write_CMS(out, cms, in, flags);
1141 else if (outformat == FORMAT_PEM)
1142 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1143 else if (outformat == FORMAT_ASN1)
1144 ret = i2d_CMS_bio_stream(out,cms, in, flags);
1147 BIO_printf(bio_err, "Bad output format for CMS file\n");
1159 ERR_print_errors(bio_err);
1161 app_RAND_write_file(NULL, bio_err);
1162 sk_X509_pop_free(encerts, X509_free);
1163 sk_X509_pop_free(other, X509_free);
1165 X509_VERIFY_PARAM_free(vpm);
1167 sk_OPENSSL_STRING_free(sksigners);
1169 sk_OPENSSL_STRING_free(skkeys);
1171 OPENSSL_free(secret_key);
1173 OPENSSL_free(secret_keyid);
1175 ASN1_OBJECT_free(econtent_type);
1177 CMS_ReceiptRequest_free(rr);
1179 sk_OPENSSL_STRING_free(rr_to);
1181 sk_OPENSSL_STRING_free(rr_from);
1182 X509_STORE_free(store);
1187 CMS_ContentInfo_free(cms);
1188 CMS_ContentInfo_free(rcms);
1193 if (passin) OPENSSL_free(passin);
1197 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1203 tmp = BIO_new_file(signerfile, "w");
1205 for(i = 0; i < sk_X509_num(signers); i++)
1206 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1212 /* Minimal callback just to output policy info (if any) */
1214 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1218 error = X509_STORE_CTX_get_error(ctx);
1222 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1223 && ((error != X509_V_OK) || (ok != 2)))
1226 policies_print(NULL, ctx);
1232 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1234 STACK_OF(GENERAL_NAME) *gens;
1237 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++)
1239 gens = sk_GENERAL_NAMES_value(gns, i);
1240 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++)
1242 gen = sk_GENERAL_NAME_value(gens, j);
1244 GENERAL_NAME_print(out, gen);
1245 BIO_puts(out, "\n");
1251 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1253 STACK_OF(CMS_SignerInfo) *sis;
1255 CMS_ReceiptRequest *rr;
1257 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1260 sis = CMS_get0_SignerInfos(cms);
1261 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++)
1263 si = sk_CMS_SignerInfo_value(sis, i);
1264 rv = CMS_get1_ReceiptRequest(si, &rr);
1265 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1267 BIO_puts(bio_err, " No Receipt Request\n");
1270 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1271 ERR_print_errors(bio_err);
1277 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1279 BIO_puts(out, " Signed Content ID:\n");
1280 idlen = ASN1_STRING_length(scid);
1281 id = (char *)ASN1_STRING_data(scid);
1282 BIO_dump_indent(out, id, idlen, 4);
1283 BIO_puts(out, " Receipts From");
1286 BIO_puts(out, " List:\n");
1287 gnames_stack_print(out, rlist);
1289 else if (allorfirst == 1)
1290 BIO_puts(out, ": First Tier\n");
1291 else if (allorfirst == 0)
1292 BIO_puts(out, ": All\n");
1294 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1295 BIO_puts(out, " Receipts To:\n");
1296 gnames_stack_print(out, rto);
1299 CMS_ReceiptRequest_free(rr);
1303 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1306 STACK_OF(GENERAL_NAMES) *ret;
1307 GENERAL_NAMES *gens = NULL;
1308 GENERAL_NAME *gen = NULL;
1309 ret = sk_GENERAL_NAMES_new_null();
1312 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++)
1314 char *str = sk_OPENSSL_STRING_value(ns, i);
1315 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1318 gens = GENERAL_NAMES_new();
1321 if (!sk_GENERAL_NAME_push(gens, gen))
1324 if (!sk_GENERAL_NAMES_push(ret, gens))
1333 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1335 GENERAL_NAMES_free(gens);
1337 GENERAL_NAME_free(gen);
1342 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to,
1344 STACK_OF(OPENSSL_STRING) *rr_from)
1346 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1347 CMS_ReceiptRequest *rr;
1348 rct_to = make_names_stack(rr_to);
1353 rct_from = make_names_stack(rr_from);
1359 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,