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(STRING) *rr_to,
76 STACK_OF(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(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(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, "-text"))
237 else if (!strcmp (*args, "-nointern"))
238 flags |= CMS_NOINTERN;
239 else if (!strcmp (*args, "-noverify")
240 || !strcmp (*args, "-no_signer_cert_verify"))
241 flags |= CMS_NO_SIGNER_CERT_VERIFY;
242 else if (!strcmp (*args, "-nocerts"))
243 flags |= CMS_NOCERTS;
244 else if (!strcmp (*args, "-noattr"))
246 else if (!strcmp (*args, "-nodetach"))
247 flags &= ~CMS_DETACHED;
248 else if (!strcmp (*args, "-nosmimecap"))
249 flags |= CMS_NOSMIMECAP;
250 else if (!strcmp (*args, "-binary"))
252 else if (!strcmp (*args, "-keyid"))
253 flags |= CMS_USE_KEYID;
254 else if (!strcmp (*args, "-nosigs"))
256 else if (!strcmp (*args, "-no_content_verify"))
257 flags |= CMS_NO_CONTENT_VERIFY;
258 else if (!strcmp (*args, "-no_attr_verify"))
259 flags |= CMS_NO_ATTR_VERIFY;
260 else if (!strcmp (*args, "-stream"))
262 else if (!strcmp (*args, "-indef"))
264 else if (!strcmp (*args, "-noindef"))
265 flags &= ~CMS_STREAM;
266 else if (!strcmp (*args, "-nooldmime"))
267 flags |= CMS_NOOLDMIMETYPE;
268 else if (!strcmp (*args, "-crlfeol"))
269 flags |= CMS_CRLFEOL;
270 else if (!strcmp (*args, "-noout"))
272 else if (!strcmp (*args, "-receipt_request_print"))
274 else if (!strcmp (*args, "-receipt_request_all"))
276 else if (!strcmp (*args, "-receipt_request_first"))
278 else if (!strcmp(*args,"-receipt_request_from"))
284 rr_from = sk_STRING_new_null();
285 sk_STRING_push(rr_from, *args);
287 else if (!strcmp(*args,"-receipt_request_to"))
293 rr_to = sk_STRING_new_null();
294 sk_STRING_push(rr_to, *args);
296 else if (!strcmp (*args, "-print"))
301 else if (!strcmp(*args,"-secretkey"))
307 secret_key = string_to_hex(*args, <mp);
310 BIO_printf(bio_err, "Invalid key %s\n", *args);
313 secret_keylen = (size_t)ltmp;
315 else if (!strcmp(*args,"-secretkeyid"))
321 secret_keyid = string_to_hex(*args, <mp);
324 BIO_printf(bio_err, "Invalid id %s\n", *args);
327 secret_keyidlen = (size_t)ltmp;
329 else if (!strcmp(*args,"-econtent_type"))
334 econtent_type = OBJ_txt2obj(*args, 0);
337 BIO_printf(bio_err, "Invalid OID %s\n", *args);
341 else if (!strcmp(*args,"-rand"))
349 #ifndef OPENSSL_NO_ENGINE
350 else if (!strcmp(*args,"-engine"))
357 else if (!strcmp(*args,"-passin"))
363 else if (!strcmp (*args, "-to"))
369 else if (!strcmp (*args, "-from"))
375 else if (!strcmp (*args, "-subject"))
381 else if (!strcmp (*args, "-signer"))
385 /* If previous -signer argument add signer to list */
390 sksigners = sk_STRING_new_null();
391 sk_STRING_push(sksigners, signerfile);
393 keyfile = signerfile;
395 skkeys = sk_STRING_new_null();
396 sk_STRING_push(skkeys, keyfile);
399 signerfile = *++args;
401 else if (!strcmp (*args, "-recip"))
407 else if (!strcmp (*args, "-certsout"))
411 certsoutfile = *++args;
413 else if (!strcmp (*args, "-md"))
417 sign_md = EVP_get_digestbyname(*++args);
420 BIO_printf(bio_err, "Unknown digest %s\n",
425 else if (!strcmp (*args, "-inkey"))
429 /* If previous -inkey arument add signer to list */
434 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
438 sksigners = sk_STRING_new_null();
439 sk_STRING_push(sksigners, signerfile);
442 skkeys = sk_STRING_new_null();
443 sk_STRING_push(skkeys, keyfile);
447 else if (!strcmp (*args, "-keyform"))
451 keyform = str2fmt(*++args);
453 else if (!strcmp (*args, "-rctform"))
457 rctformat = str2fmt(*++args);
459 else if (!strcmp (*args, "-certfile"))
465 else if (!strcmp (*args, "-CAfile"))
471 else if (!strcmp (*args, "-CApath"))
477 else if (!strcmp (*args, "-in"))
483 else if (!strcmp (*args, "-inform"))
487 informat = str2fmt(*++args);
489 else if (!strcmp (*args, "-outform"))
493 outformat = str2fmt(*++args);
495 else if (!strcmp (*args, "-out"))
501 else if (!strcmp (*args, "-content"))
507 else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
509 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
514 if (((rr_allorfirst != -1) || rr_from) && !rr_to)
516 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
520 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from))
522 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
525 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners))
527 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
531 if (operation & SMIME_SIGNERS)
533 if (keyfile && !signerfile)
535 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
538 /* Check to see if any final signer needs to be appended */
542 sksigners = sk_STRING_new_null();
543 sk_STRING_push(sksigners, signerfile);
545 skkeys = sk_STRING_new_null();
547 keyfile = signerfile;
548 sk_STRING_push(skkeys, keyfile);
552 BIO_printf(bio_err, "No signer certificate specified\n");
560 else if (operation == SMIME_DECRYPT)
562 if (!recipfile && !keyfile && !secret_key)
564 BIO_printf(bio_err, "No recipient certificate or key specified\n");
568 else if (operation == SMIME_ENCRYPT)
570 if (!*args && !secret_key)
572 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
583 BIO_printf (bio_err, "Usage cms [options] cert.pem ...\n");
584 BIO_printf (bio_err, "where options are\n");
585 BIO_printf (bio_err, "-encrypt encrypt message\n");
586 BIO_printf (bio_err, "-decrypt decrypt encrypted message\n");
587 BIO_printf (bio_err, "-sign sign message\n");
588 BIO_printf (bio_err, "-verify verify signed message\n");
589 BIO_printf (bio_err, "-cmsout output CMS structure\n");
590 #ifndef OPENSSL_NO_DES
591 BIO_printf (bio_err, "-des3 encrypt with triple DES\n");
592 BIO_printf (bio_err, "-des encrypt with DES\n");
594 #ifndef OPENSSL_NO_SEED
595 BIO_printf (bio_err, "-seed encrypt with SEED\n");
597 #ifndef OPENSSL_NO_RC2
598 BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
599 BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n");
600 BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n");
602 #ifndef OPENSSL_NO_AES
603 BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
604 BIO_printf (bio_err, " encrypt PEM output with cbc aes\n");
606 #ifndef OPENSSL_NO_CAMELLIA
607 BIO_printf (bio_err, "-camellia128, -camellia192, -camellia256\n");
608 BIO_printf (bio_err, " encrypt PEM output with cbc camellia\n");
610 BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n");
611 BIO_printf (bio_err, "-nosigs don't verify message signature\n");
612 BIO_printf (bio_err, "-noverify don't verify signers certificate\n");
613 BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n");
614 BIO_printf (bio_err, "-nodetach use opaque signing\n");
615 BIO_printf (bio_err, "-noattr don't include any signed attributes\n");
616 BIO_printf (bio_err, "-binary don't translate message to text\n");
617 BIO_printf (bio_err, "-certfile file other certificates file\n");
618 BIO_printf (bio_err, "-certsout file certificate output file\n");
619 BIO_printf (bio_err, "-signer file signer certificate file\n");
620 BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n");
621 BIO_printf (bio_err, "-skeyid use subject key identifier\n");
622 BIO_printf (bio_err, "-in file input file\n");
623 BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
624 BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n");
625 BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
626 BIO_printf (bio_err, "-out file output file\n");
627 BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
628 BIO_printf (bio_err, "-content file supply or override content for detached signature\n");
629 BIO_printf (bio_err, "-to addr to address\n");
630 BIO_printf (bio_err, "-from ad from address\n");
631 BIO_printf (bio_err, "-subject s subject\n");
632 BIO_printf (bio_err, "-text include or delete text MIME headers\n");
633 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
634 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
635 BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
636 BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
637 #ifndef OPENSSL_NO_ENGINE
638 BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n");
640 BIO_printf (bio_err, "-passin arg input file pass phrase source\n");
641 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
642 BIO_printf(bio_err, " load the file (or the files in the directory) into\n");
643 BIO_printf(bio_err, " the random number generator\n");
644 BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
648 #ifndef OPENSSL_NO_ENGINE
649 e = setup_engine(bio_err, engine, 0);
652 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
654 BIO_printf(bio_err, "Error getting password\n");
660 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
662 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
663 app_RAND_load_files(inrand));
668 if (!(operation & SMIME_SIGNERS))
669 flags &= ~CMS_DETACHED;
671 if (operation & SMIME_OP)
673 if (outformat == FORMAT_ASN1)
678 if (flags & CMS_BINARY)
682 if (operation & SMIME_IP)
684 if (informat == FORMAT_ASN1)
689 if (flags & CMS_BINARY)
693 if (operation == SMIME_ENCRYPT)
697 #ifndef OPENSSL_NO_DES
698 cipher = EVP_des_ede3_cbc();
700 BIO_printf(bio_err, "No cipher selected\n");
705 if (secret_key && !secret_keyid)
707 BIO_printf(bio_err, "No secret key id\n");
712 encerts = sk_X509_new_null();
715 if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
716 NULL, e, "recipient certificate file")))
718 sk_X509_push(encerts, cert);
726 if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
727 e, "certificate file")))
729 ERR_print_errors(bio_err);
734 if (recipfile && (operation == SMIME_DECRYPT))
736 if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
737 e, "recipient certificate file")))
739 ERR_print_errors(bio_err);
744 if (operation == SMIME_SIGN_RECEIPT)
746 if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM,NULL,
747 e, "receipt signer certificate file")))
749 ERR_print_errors(bio_err);
754 if (operation == SMIME_DECRYPT)
759 else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT))
762 keyfile = signerfile;
768 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
776 if (!(in = BIO_new_file(infile, inmode)))
779 "Can't open input file %s\n", infile);
784 in = BIO_new_fp(stdin, BIO_NOCLOSE);
786 if (operation & SMIME_IP)
788 if (informat == FORMAT_SMIME)
789 cms = SMIME_read_CMS(in, &indata);
790 else if (informat == FORMAT_PEM)
791 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
792 else if (informat == FORMAT_ASN1)
793 cms = d2i_CMS_bio(in, NULL);
796 BIO_printf(bio_err, "Bad input format for CMS file\n");
802 BIO_printf(bio_err, "Error reading S/MIME message\n");
808 if (!(indata = BIO_new_file(contfile, "rb")))
810 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
816 STACK_OF(X509) *allcerts;
817 allcerts = CMS_get1_certs(cms);
818 if (!save_certs(certsoutfile, allcerts))
821 "Error writing certs to %s\n",
826 sk_X509_pop_free(allcerts, X509_free);
832 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
833 if (!(rctin = BIO_new_file(rctfile, rctmode)))
836 "Can't open receipt file %s\n", rctfile);
840 if (rctformat == FORMAT_SMIME)
841 rcms = SMIME_read_CMS(rctin, NULL);
842 else if (rctformat == FORMAT_PEM)
843 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
844 else if (rctformat == FORMAT_ASN1)
845 rcms = d2i_CMS_bio(rctin, NULL);
848 BIO_printf(bio_err, "Bad input format for receipt\n");
854 BIO_printf(bio_err, "Error reading receipt\n");
861 if (!(out = BIO_new_file(outfile, outmode)))
864 "Can't open output file %s\n", outfile);
870 out = BIO_new_fp(stdout, BIO_NOCLOSE);
871 #ifdef OPENSSL_SYS_VMS
873 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
874 out = BIO_push(tmpbio, out);
879 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT))
881 if (!(store = setup_verify(bio_err, CAfile, CApath)))
883 X509_STORE_set_verify_cb_func(store, cms_cb);
885 X509_STORE_set1_param(store, vpm);
891 if (operation == SMIME_DATA_CREATE)
893 cms = CMS_data_create(in, flags);
895 else if (operation == SMIME_DIGEST_CREATE)
897 cms = CMS_digest_create(in, sign_md, flags);
899 else if (operation == SMIME_COMPRESS)
901 cms = CMS_compress(in, -1, flags);
903 else if (operation == SMIME_ENCRYPT)
905 flags |= CMS_PARTIAL;
906 cms = CMS_encrypt(encerts, in, cipher, flags);
911 if (!CMS_add0_recipient_key(cms, NID_undef,
912 secret_key, secret_keylen,
913 secret_keyid, secret_keyidlen,
916 /* NULL these because call absorbs them */
920 if (!(flags & CMS_STREAM))
922 if (!CMS_final(cms, in, NULL, flags))
926 else if (operation == SMIME_ENCRYPTED_ENCRYPT)
928 cms = CMS_EncryptedData_encrypt(in, cipher,
929 secret_key, secret_keylen,
933 else if (operation == SMIME_SIGN_RECEIPT)
935 CMS_ContentInfo *srcms = NULL;
936 STACK_OF(CMS_SignerInfo) *sis;
938 sis = CMS_get0_SignerInfos(cms);
941 si = sk_CMS_SignerInfo_value(sis, 0);
942 srcms = CMS_sign_receipt(si, signer, key, other, flags);
945 CMS_ContentInfo_free(cms);
948 else if (operation & SMIME_SIGNERS)
951 /* If detached data content we enable streaming if
952 * S/MIME output format.
954 if (operation == SMIME_SIGN)
957 if (flags & CMS_DETACHED)
959 if (outformat == FORMAT_SMIME)
962 flags |= CMS_PARTIAL;
963 cms = CMS_sign(NULL, NULL, other, in, flags);
967 CMS_set1_eContentType(cms, econtent_type);
971 rr = make_receipt_request(rr_to, rr_allorfirst,
976 "Signed Receipt Request Creation Error\n");
982 flags |= CMS_REUSE_DIGEST;
983 for (i = 0; i < sk_STRING_num(sksigners); i++)
986 signerfile = sk_STRING_value(sksigners, i);
987 keyfile = sk_STRING_value(skkeys, i);
988 signer = load_cert(bio_err, signerfile,FORMAT_PEM, NULL,
989 e, "signer certificate");
992 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
996 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
999 if (rr && !CMS_add1_ReceiptRequest(si, rr))
1006 /* If not streaming or resigning finalize structure */
1007 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM))
1009 if (!CMS_final(cms, in, NULL, flags))
1016 BIO_printf(bio_err, "Error creating CMS structure\n");
1021 if (operation == SMIME_DECRYPT)
1026 if (!CMS_decrypt_set1_key(cms,
1027 secret_key, secret_keylen,
1028 secret_keyid, secret_keyidlen))
1031 "Error decrypting CMS using secret key\n");
1038 if (!CMS_decrypt_set1_pkey(cms, key, recip))
1041 "Error decrypting CMS using private key\n");
1046 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags))
1048 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1052 else if (operation == SMIME_DATAOUT)
1054 if (!CMS_data(cms, out, flags))
1057 else if (operation == SMIME_UNCOMPRESS)
1059 if (!CMS_uncompress(cms, indata, out, flags))
1062 else if (operation == SMIME_DIGEST_VERIFY)
1064 if (CMS_digest_verify(cms, indata, out, flags) > 0)
1065 BIO_printf(bio_err, "Verification successful\n");
1068 BIO_printf(bio_err, "Verification failure\n");
1072 else if (operation == SMIME_ENCRYPTED_DECRYPT)
1074 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1075 indata, out, flags))
1078 else if (operation == SMIME_VERIFY)
1080 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
1081 BIO_printf(bio_err, "Verification successful\n");
1084 BIO_printf(bio_err, "Verification failure\n");
1086 ret = verify_err + 32;
1091 STACK_OF(X509) *signers;
1092 signers = CMS_get0_signers(cms);
1093 if (!save_certs(signerfile, signers))
1096 "Error writing signers to %s\n",
1101 sk_X509_free(signers);
1104 receipt_request_print(bio_err, cms);
1107 else if (operation == SMIME_VERIFY_RECEIPT)
1109 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
1110 BIO_printf(bio_err, "Verification successful\n");
1113 BIO_printf(bio_err, "Verification failure\n");
1122 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1124 else if (outformat == FORMAT_SMIME)
1127 BIO_printf(out, "To: %s\n", to);
1129 BIO_printf(out, "From: %s\n", from);
1131 BIO_printf(out, "Subject: %s\n", subject);
1132 if (operation == SMIME_RESIGN)
1133 ret = SMIME_write_CMS(out, cms, indata, flags);
1135 ret = SMIME_write_CMS(out, cms, in, flags);
1137 else if (outformat == FORMAT_PEM)
1138 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1139 else if (outformat == FORMAT_ASN1)
1140 ret = i2d_CMS_bio_stream(out,cms, in, flags);
1143 BIO_printf(bio_err, "Bad output format for CMS file\n");
1155 ERR_print_errors(bio_err);
1157 app_RAND_write_file(NULL, bio_err);
1158 sk_X509_pop_free(encerts, X509_free);
1159 sk_X509_pop_free(other, X509_free);
1161 X509_VERIFY_PARAM_free(vpm);
1163 sk_STRING_free(sksigners);
1165 sk_STRING_free(skkeys);
1167 OPENSSL_free(secret_key);
1169 OPENSSL_free(secret_keyid);
1171 ASN1_OBJECT_free(econtent_type);
1173 CMS_ReceiptRequest_free(rr);
1175 sk_STRING_free(rr_to);
1177 sk_STRING_free(rr_from);
1178 X509_STORE_free(store);
1183 CMS_ContentInfo_free(cms);
1184 CMS_ContentInfo_free(rcms);
1189 if (passin) OPENSSL_free(passin);
1193 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1199 tmp = BIO_new_file(signerfile, "w");
1201 for(i = 0; i < sk_X509_num(signers); i++)
1202 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1208 /* Minimal callback just to output policy info (if any) */
1210 static int cms_cb(int ok, X509_STORE_CTX *ctx)
1214 error = X509_STORE_CTX_get_error(ctx);
1218 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1219 && ((error != X509_V_OK) || (ok != 2)))
1222 policies_print(NULL, ctx);
1228 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1230 STACK_OF(GENERAL_NAME) *gens;
1233 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++)
1235 gens = sk_GENERAL_NAMES_value(gns, i);
1236 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++)
1238 gen = sk_GENERAL_NAME_value(gens, j);
1240 GENERAL_NAME_print(out, gen);
1241 BIO_puts(out, "\n");
1247 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1249 STACK_OF(CMS_SignerInfo) *sis;
1251 CMS_ReceiptRequest *rr;
1253 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1256 sis = CMS_get0_SignerInfos(cms);
1257 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++)
1259 si = sk_CMS_SignerInfo_value(sis, i);
1260 rv = CMS_get1_ReceiptRequest(si, &rr);
1261 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1263 BIO_puts(bio_err, " No Receipt Request\n");
1266 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1267 ERR_print_errors(bio_err);
1273 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1275 BIO_puts(out, " Signed Content ID:\n");
1276 idlen = ASN1_STRING_length(scid);
1277 id = (char *)ASN1_STRING_data(scid);
1278 BIO_dump_indent(out, id, idlen, 4);
1279 BIO_puts(out, " Receipts From");
1282 BIO_puts(out, " List:\n");
1283 gnames_stack_print(out, rlist);
1285 else if (allorfirst == 1)
1286 BIO_puts(out, ": First Tier\n");
1287 else if (allorfirst == 0)
1288 BIO_puts(out, ": All\n");
1290 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1291 BIO_puts(out, " Receipts To:\n");
1292 gnames_stack_print(out, rto);
1295 CMS_ReceiptRequest_free(rr);
1299 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(STRING) *ns)
1302 STACK_OF(GENERAL_NAMES) *ret;
1303 GENERAL_NAMES *gens = NULL;
1304 GENERAL_NAME *gen = NULL;
1305 ret = sk_GENERAL_NAMES_new_null();
1308 for (i = 0; i < sk_STRING_num(ns); i++)
1310 char *str = sk_STRING_value(ns, i);
1311 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1314 gens = GENERAL_NAMES_new();
1317 if (!sk_GENERAL_NAME_push(gens, gen))
1320 if (!sk_GENERAL_NAMES_push(ret, gens))
1329 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1331 GENERAL_NAMES_free(gens);
1333 GENERAL_NAME_free(gen);
1338 static CMS_ReceiptRequest *make_receipt_request(STACK_OF(STRING) *rr_to,
1340 STACK_OF(STRING) *rr_from)
1342 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1343 CMS_ReceiptRequest *rr;
1344 rct_to = make_names_stack(rr_to);
1349 rct_from = make_names_stack(rr_from);
1355 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,