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 smime_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 *rr_to, int rr_allorfirst,
79 #define SMIME_SIGNERS 0x40
80 #define SMIME_ENCRYPT (1 | SMIME_OP)
81 #define SMIME_DECRYPT (2 | SMIME_IP)
82 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
83 #define SMIME_VERIFY (4 | SMIME_IP)
84 #define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
85 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
86 #define SMIME_DATAOUT (7 | SMIME_IP)
87 #define SMIME_DATA_CREATE (8 | SMIME_OP)
88 #define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
89 #define SMIME_DIGEST_CREATE (10 | SMIME_OP)
90 #define SMIME_UNCOMPRESS (11 | SMIME_IP)
91 #define SMIME_COMPRESS (12 | SMIME_OP)
92 #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
93 #define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
94 #define SMIME_SIGN_RECEIPT (15 | SMIME_OP | SMIME_IP)
95 #define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
97 int MAIN(int, char **);
99 int MAIN(int argc, char **argv)
105 const char *inmode = "r", *outmode = "w";
106 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
107 char *signerfile = NULL, *recipfile = NULL;
108 STACK *sksigners = NULL, *skkeys = NULL;
109 char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
110 const EVP_CIPHER *cipher = NULL;
111 CMS_ContentInfo *cms = NULL, *rcms = NULL;
112 X509_STORE *store = NULL;
113 X509 *cert = NULL, *recip = NULL, *signer = NULL;
114 EVP_PKEY *key = NULL;
115 STACK_OF(X509) *encerts = NULL, *other = NULL;
116 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
118 int flags = CMS_DETACHED, noout = 0, print = 0;
119 int rr_print = 0, rr_allorfirst = -1;
120 STACK *rr_to = NULL, *rr_from = NULL;
121 CMS_ReceiptRequest *rr = NULL;
122 char *to = NULL, *from = NULL, *subject = NULL;
123 char *CAfile = NULL, *CApath = NULL;
124 char *passargin = NULL, *passin = NULL;
127 const EVP_MD *sign_md = NULL;
128 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
129 int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
130 #ifndef OPENSSL_NO_ENGINE
133 unsigned char *secret_key = NULL, *secret_keyid = NULL;
134 size_t secret_keylen = 0, secret_keyidlen = 0;
136 ASN1_OBJECT *econtent_type = NULL;
138 X509_VERIFY_PARAM *vpm = NULL;
147 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
148 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
151 if (!load_config(bio_err, NULL))
154 while (!badarg && *args && *args[0] == '-')
156 if (!strcmp (*args, "-encrypt"))
157 operation = SMIME_ENCRYPT;
158 else if (!strcmp (*args, "-decrypt"))
159 operation = SMIME_DECRYPT;
160 else if (!strcmp (*args, "-sign"))
161 operation = SMIME_SIGN;
162 else if (!strcmp (*args, "-resign"))
163 operation = SMIME_RESIGN;
164 else if (!strcmp (*args, "-verify"))
165 operation = SMIME_VERIFY;
166 else if (!strcmp(*args,"-verify_receipt"))
168 operation = SMIME_VERIFY_RECEIPT;
174 else if (!strcmp (*args, "-cmsout"))
175 operation = SMIME_CMSOUT;
176 else if (!strcmp (*args, "-data_out"))
177 operation = SMIME_DATAOUT;
178 else if (!strcmp (*args, "-data_create"))
179 operation = SMIME_DATA_CREATE;
180 else if (!strcmp (*args, "-digest_verify"))
181 operation = SMIME_DIGEST_VERIFY;
182 else if (!strcmp (*args, "-digest_create"))
183 operation = SMIME_DIGEST_CREATE;
184 else if (!strcmp (*args, "-compress"))
185 operation = SMIME_COMPRESS;
186 else if (!strcmp (*args, "-uncompress"))
187 operation = SMIME_UNCOMPRESS;
188 else if (!strcmp (*args, "-EncryptedData_decrypt"))
189 operation = SMIME_ENCRYPTED_DECRYPT;
190 else if (!strcmp (*args, "-EncryptedData_encrypt"))
191 operation = SMIME_ENCRYPTED_ENCRYPT;
192 #ifndef OPENSSL_NO_DES
193 else if (!strcmp (*args, "-des3"))
194 cipher = EVP_des_ede3_cbc();
195 else if (!strcmp (*args, "-des"))
196 cipher = EVP_des_cbc();
198 #ifndef OPENSSL_NO_SEED
199 else if (!strcmp (*args, "-seed"))
200 cipher = EVP_seed_cbc();
202 #ifndef OPENSSL_NO_RC2
203 else if (!strcmp (*args, "-rc2-40"))
204 cipher = EVP_rc2_40_cbc();
205 else if (!strcmp (*args, "-rc2-128"))
206 cipher = EVP_rc2_cbc();
207 else if (!strcmp (*args, "-rc2-64"))
208 cipher = EVP_rc2_64_cbc();
210 #ifndef OPENSSL_NO_AES
211 else if (!strcmp(*args,"-aes128"))
212 cipher = EVP_aes_128_cbc();
213 else if (!strcmp(*args,"-aes192"))
214 cipher = EVP_aes_192_cbc();
215 else if (!strcmp(*args,"-aes256"))
216 cipher = EVP_aes_256_cbc();
218 #ifndef OPENSSL_NO_CAMELLIA
219 else if (!strcmp(*args,"-camellia128"))
220 cipher = EVP_camellia_128_cbc();
221 else if (!strcmp(*args,"-camellia192"))
222 cipher = EVP_camellia_192_cbc();
223 else if (!strcmp(*args,"-camellia256"))
224 cipher = EVP_camellia_256_cbc();
226 else if (!strcmp (*args, "-text"))
228 else if (!strcmp (*args, "-nointern"))
229 flags |= CMS_NOINTERN;
230 else if (!strcmp (*args, "-noverify")
231 || !strcmp (*args, "-no_signer_cert_verify"))
232 flags |= CMS_NO_SIGNER_CERT_VERIFY;
233 else if (!strcmp (*args, "-nocerts"))
234 flags |= CMS_NOCERTS;
235 else if (!strcmp (*args, "-noattr"))
237 else if (!strcmp (*args, "-nodetach"))
238 flags &= ~CMS_DETACHED;
239 else if (!strcmp (*args, "-nosmimecap"))
240 flags |= CMS_NOSMIMECAP;
241 else if (!strcmp (*args, "-binary"))
243 else if (!strcmp (*args, "-keyid"))
244 flags |= CMS_USE_KEYID;
245 else if (!strcmp (*args, "-nosigs"))
247 else if (!strcmp (*args, "-no_content_verify"))
248 flags |= CMS_NO_CONTENT_VERIFY;
249 else if (!strcmp (*args, "-no_attr_verify"))
250 flags |= CMS_NO_ATTR_VERIFY;
251 else if (!strcmp (*args, "-stream"))
253 else if (!strcmp (*args, "-indef"))
255 else if (!strcmp (*args, "-noindef"))
256 flags &= ~CMS_STREAM;
257 else if (!strcmp (*args, "-nooldmime"))
258 flags |= CMS_NOOLDMIMETYPE;
259 else if (!strcmp (*args, "-crlfeol"))
260 flags |= CMS_CRLFEOL;
261 else if (!strcmp (*args, "-noout"))
263 else if (!strcmp (*args, "-receipt_request_print"))
265 else if (!strcmp (*args, "-receipt_request_all"))
267 else if (!strcmp (*args, "-receipt_request_first"))
269 else if (!strcmp(*args,"-receipt_request_from"))
275 rr_from = sk_new_null();
276 sk_push(rr_from, *args);
278 else if (!strcmp(*args,"-receipt_request_to"))
284 rr_to = sk_new_null();
285 sk_push(rr_to, *args);
287 else if (!strcmp (*args, "-print"))
292 else if (!strcmp(*args,"-secretkey"))
298 secret_key = string_to_hex(*args, <mp);
301 BIO_printf(bio_err, "Invalid key %s\n", *args);
304 secret_keylen = (size_t)ltmp;
306 else if (!strcmp(*args,"-secretkeyid"))
312 secret_keyid = string_to_hex(*args, <mp);
315 BIO_printf(bio_err, "Invalid id %s\n", *args);
318 secret_keyidlen = (size_t)ltmp;
320 else if (!strcmp(*args,"-econtent_type"))
325 econtent_type = OBJ_txt2obj(*args, 0);
328 BIO_printf(bio_err, "Invalid OID %s\n", *args);
332 else if (!strcmp(*args,"-rand"))
340 #ifndef OPENSSL_NO_ENGINE
341 else if (!strcmp(*args,"-engine"))
348 else if (!strcmp(*args,"-passin"))
354 else if (!strcmp (*args, "-to"))
360 else if (!strcmp (*args, "-from"))
366 else if (!strcmp (*args, "-subject"))
372 else if (!strcmp (*args, "-signer"))
376 /* If previous -signer argument add signer to list */
381 sksigners = sk_new_null();
382 sk_push(sksigners, signerfile);
384 keyfile = signerfile;
386 skkeys = sk_new_null();
387 sk_push(skkeys, keyfile);
390 signerfile = *++args;
392 else if (!strcmp (*args, "-recip"))
398 else if (!strcmp (*args, "-md"))
402 sign_md = EVP_get_digestbyname(*++args);
405 BIO_printf(bio_err, "Unknown digest %s\n",
410 else if (!strcmp (*args, "-inkey"))
414 /* If previous -inkey arument add signer to list */
419 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
423 sksigners = sk_new_null();
424 sk_push(sksigners, signerfile);
427 skkeys = sk_new_null();
428 sk_push(skkeys, keyfile);
432 else if (!strcmp (*args, "-keyform"))
436 keyform = str2fmt(*++args);
438 else if (!strcmp (*args, "-rctform"))
442 rctformat = str2fmt(*++args);
444 else if (!strcmp (*args, "-certfile"))
450 else if (!strcmp (*args, "-CAfile"))
456 else if (!strcmp (*args, "-CApath"))
462 else if (!strcmp (*args, "-in"))
468 else if (!strcmp (*args, "-inform"))
472 informat = str2fmt(*++args);
474 else if (!strcmp (*args, "-outform"))
478 outformat = str2fmt(*++args);
480 else if (!strcmp (*args, "-out"))
486 else if (!strcmp (*args, "-content"))
492 else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
494 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
499 if (((rr_allorfirst != -1) || rr_from) && !rr_to)
501 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
505 if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from))
507 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
510 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners))
512 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
516 if (operation & SMIME_SIGNERS)
518 if (keyfile && !signerfile)
520 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
523 /* Check to see if any final signer needs to be appended */
527 sksigners = sk_new_null();
528 sk_push(sksigners, signerfile);
530 skkeys = sk_new_null();
532 keyfile = signerfile;
533 sk_push(skkeys, keyfile);
537 BIO_printf(bio_err, "No signer certificate specified\n");
544 else if (operation == SMIME_DECRYPT)
546 if (!recipfile && !keyfile && !secret_key)
548 BIO_printf(bio_err, "No recipient certificate or key specified\n");
552 else if (operation == SMIME_ENCRYPT)
554 if (!*args && !secret_key)
556 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
567 BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n");
568 BIO_printf (bio_err, "where options are\n");
569 BIO_printf (bio_err, "-encrypt encrypt message\n");
570 BIO_printf (bio_err, "-decrypt decrypt encrypted message\n");
571 BIO_printf (bio_err, "-sign sign message\n");
572 BIO_printf (bio_err, "-verify verify signed message\n");
573 BIO_printf (bio_err, "-cmsout output CMS structure\n");
574 #ifndef OPENSSL_NO_DES
575 BIO_printf (bio_err, "-des3 encrypt with triple DES\n");
576 BIO_printf (bio_err, "-des encrypt with DES\n");
578 #ifndef OPENSSL_NO_SEED
579 BIO_printf (bio_err, "-seed encrypt with SEED\n");
581 #ifndef OPENSSL_NO_RC2
582 BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
583 BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n");
584 BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n");
586 #ifndef OPENSSL_NO_AES
587 BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
588 BIO_printf (bio_err, " encrypt PEM output with cbc aes\n");
590 #ifndef OPENSSL_NO_CAMELLIA
591 BIO_printf (bio_err, "-camellia128, -camellia192, -camellia256\n");
592 BIO_printf (bio_err, " encrypt PEM output with cbc camellia\n");
594 BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n");
595 BIO_printf (bio_err, "-nosigs don't verify message signature\n");
596 BIO_printf (bio_err, "-noverify don't verify signers certificate\n");
597 BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n");
598 BIO_printf (bio_err, "-nodetach use opaque signing\n");
599 BIO_printf (bio_err, "-noattr don't include any signed attributes\n");
600 BIO_printf (bio_err, "-binary don't translate message to text\n");
601 BIO_printf (bio_err, "-certfile file other certificates file\n");
602 BIO_printf (bio_err, "-signer file signer certificate file\n");
603 BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n");
604 BIO_printf (bio_err, "-skeyid use subject key identifier\n");
605 BIO_printf (bio_err, "-in file input file\n");
606 BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
607 BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n");
608 BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
609 BIO_printf (bio_err, "-out file output file\n");
610 BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
611 BIO_printf (bio_err, "-content file supply or override content for detached signature\n");
612 BIO_printf (bio_err, "-to addr to address\n");
613 BIO_printf (bio_err, "-from ad from address\n");
614 BIO_printf (bio_err, "-subject s subject\n");
615 BIO_printf (bio_err, "-text include or delete text MIME headers\n");
616 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
617 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
618 BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
619 BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
620 #ifndef OPENSSL_NO_ENGINE
621 BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n");
623 BIO_printf (bio_err, "-passin arg input file pass phrase source\n");
624 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
625 BIO_printf(bio_err, " load the file (or the files in the directory) into\n");
626 BIO_printf(bio_err, " the random number generator\n");
627 BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
631 #ifndef OPENSSL_NO_ENGINE
632 e = setup_engine(bio_err, engine, 0);
635 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
637 BIO_printf(bio_err, "Error getting password\n");
643 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
645 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
646 app_RAND_load_files(inrand));
651 if (!(operation & SMIME_SIGNERS))
652 flags &= ~CMS_DETACHED;
654 if (operation & SMIME_OP)
656 if (outformat == FORMAT_ASN1)
661 if (flags & CMS_BINARY)
665 if (operation & SMIME_IP)
667 if (informat == FORMAT_ASN1)
672 if (flags & CMS_BINARY)
676 if (operation == SMIME_ENCRYPT)
680 #ifndef OPENSSL_NO_DES
681 cipher = EVP_des_ede3_cbc();
683 BIO_printf(bio_err, "No cipher selected\n");
688 if (secret_key && !secret_keyid)
690 BIO_printf(bio_err, "No sectre key id\n");
695 encerts = sk_X509_new_null();
698 if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
699 NULL, e, "recipient certificate file")))
701 sk_X509_push(encerts, cert);
709 if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
710 e, "certificate file")))
712 ERR_print_errors(bio_err);
717 if (recipfile && (operation == SMIME_DECRYPT))
719 if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
720 e, "recipient certificate file")))
722 ERR_print_errors(bio_err);
727 if (operation == SMIME_DECRYPT)
732 else if (operation == SMIME_SIGN)
735 keyfile = signerfile;
741 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
749 if (!(in = BIO_new_file(infile, inmode)))
752 "Can't open input file %s\n", infile);
757 in = BIO_new_fp(stdin, BIO_NOCLOSE);
759 if (operation & SMIME_IP)
761 if (informat == FORMAT_SMIME)
762 cms = SMIME_read_CMS(in, &indata);
763 else if (informat == FORMAT_PEM)
764 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
765 else if (informat == FORMAT_ASN1)
766 cms = d2i_CMS_bio(in, NULL);
769 BIO_printf(bio_err, "Bad input format for CMS file\n");
775 BIO_printf(bio_err, "Error reading S/MIME message\n");
781 if (!(indata = BIO_new_file(contfile, "rb")))
783 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
791 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
792 if (!(rctin = BIO_new_file(rctfile, rctmode)))
795 "Can't open receipt file %s\n", rctfile);
799 if (rctformat == FORMAT_SMIME)
800 rcms = SMIME_read_CMS(rctin, NULL);
801 else if (rctformat == FORMAT_PEM)
802 rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
803 else if (rctformat == FORMAT_ASN1)
804 rcms = d2i_CMS_bio(rctin, NULL);
807 BIO_printf(bio_err, "Bad input format for receipt\n");
813 BIO_printf(bio_err, "Error reading receipt\n");
820 if (!(out = BIO_new_file(outfile, outmode)))
823 "Can't open output file %s\n", outfile);
829 out = BIO_new_fp(stdout, BIO_NOCLOSE);
830 #ifdef OPENSSL_SYS_VMS
832 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
833 out = BIO_push(tmpbio, out);
838 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT))
840 if (!(store = setup_verify(bio_err, CAfile, CApath)))
842 X509_STORE_set_verify_cb_func(store, smime_cb);
844 X509_STORE_set1_param(store, vpm);
850 if (operation == SMIME_DATA_CREATE)
852 cms = CMS_data_create(in, flags);
854 else if (operation == SMIME_DIGEST_CREATE)
856 cms = CMS_digest_create(in, sign_md, flags);
858 else if (operation == SMIME_COMPRESS)
860 cms = CMS_compress(in, -1, flags);
862 else if (operation == SMIME_ENCRYPT)
864 flags |= CMS_PARTIAL;
865 cms = CMS_encrypt(encerts, in, cipher, flags);
870 if (!CMS_add0_recipient_key(cms, NID_undef,
871 secret_key, secret_keylen,
872 secret_keyid, secret_keyidlen,
875 /* NULL these because call absorbs them */
879 if (!(flags & CMS_STREAM))
881 if (!CMS_final(cms, in, flags))
885 else if (operation == SMIME_ENCRYPTED_ENCRYPT)
887 cms = CMS_EncryptedData_encrypt(in, cipher,
888 secret_key, secret_keylen,
892 else if (operation & SMIME_SIGNERS)
895 /* If detached data content we enable streaming if
896 * S/MIME output format.
898 if (operation == SMIME_SIGN)
901 if (flags & CMS_DETACHED)
903 if (outformat == FORMAT_SMIME)
906 flags |= CMS_PARTIAL;
907 cms = CMS_sign(NULL, NULL, other, in, flags);
911 CMS_set1_eContentType(cms, econtent_type);
915 rr = make_receipt_request(rr_to, rr_allorfirst,
920 "Signed Receipt Request Creation Error\n");
926 flags |= CMS_REUSE_DIGEST;
927 for (i = 0; i < sk_num(sksigners); i++)
930 signerfile = sk_value(sksigners, i);
931 keyfile = sk_value(skkeys, i);
932 signer = load_cert(bio_err, signerfile,FORMAT_PEM, NULL,
933 e, "signer certificate");
936 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
940 si = CMS_add1_signer(cms, signer, key, sign_md, flags);
943 if (rr && !CMS_add1_ReceiptRequest(si, rr))
950 /* If not streaming or resigning finalize structure */
951 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM))
953 if (!CMS_final(cms, in, flags))
960 BIO_printf(bio_err, "Error creating CMS structure\n");
965 if (operation == SMIME_DECRYPT)
970 if (!CMS_decrypt_set1_key(cms,
971 secret_key, secret_keylen,
972 secret_keyid, secret_keyidlen))
975 "Error decrypting CMS using secret key\n");
982 if (!CMS_decrypt_set1_pkey(cms, key, recip))
985 "Error decrypting CMS using private key\n");
990 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags))
992 BIO_printf(bio_err, "Error decrypting CMS structure\n");
996 else if (operation == SMIME_DATAOUT)
998 if (!CMS_data(cms, out, flags))
1001 else if (operation == SMIME_UNCOMPRESS)
1003 if (!CMS_uncompress(cms, indata, out, flags))
1006 else if (operation == SMIME_DIGEST_VERIFY)
1008 if (CMS_digest_verify(cms, indata, out, flags) > 0)
1009 BIO_printf(bio_err, "Verification successful\n");
1012 BIO_printf(bio_err, "Verification failure\n");
1016 else if (operation == SMIME_ENCRYPTED_DECRYPT)
1018 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1019 indata, out, flags))
1022 else if (operation == SMIME_VERIFY)
1024 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
1025 BIO_printf(bio_err, "Verification successful\n");
1028 BIO_printf(bio_err, "Verification failure\n");
1033 STACK_OF(X509) *signers;
1034 signers = CMS_get0_signers(cms);
1035 if (!save_certs(signerfile, signers))
1038 "Error writing signers to %s\n",
1043 sk_X509_free(signers);
1046 receipt_request_print(bio_err, cms);
1049 else if (operation == SMIME_VERIFY_RECEIPT)
1051 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
1052 BIO_printf(bio_err, "Verification successful\n");
1055 BIO_printf(bio_err, "Verification failure\n");
1064 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
1066 else if (outformat == FORMAT_SMIME)
1069 BIO_printf(out, "To: %s\n", to);
1071 BIO_printf(out, "From: %s\n", from);
1073 BIO_printf(out, "Subject: %s\n", subject);
1074 if (operation == SMIME_RESIGN)
1075 ret = SMIME_write_CMS(out, cms, indata, flags);
1077 ret = SMIME_write_CMS(out, cms, in, flags);
1079 else if (outformat == FORMAT_PEM)
1080 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
1081 else if (outformat == FORMAT_ASN1)
1082 ret = i2d_CMS_bio_stream(out,cms, in, flags);
1085 BIO_printf(bio_err, "Bad output format for CMS file\n");
1097 ERR_print_errors(bio_err);
1099 app_RAND_write_file(NULL, bio_err);
1100 sk_X509_pop_free(encerts, X509_free);
1101 sk_X509_pop_free(other, X509_free);
1103 X509_VERIFY_PARAM_free(vpm);
1109 OPENSSL_free(secret_key);
1111 OPENSSL_free(secret_keyid);
1113 ASN1_OBJECT_free(econtent_type);
1115 CMS_ReceiptRequest_free(rr);
1120 X509_STORE_free(store);
1125 CMS_ContentInfo_free(cms);
1126 CMS_ContentInfo_free(rcms);
1131 if (passin) OPENSSL_free(passin);
1135 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1141 tmp = BIO_new_file(signerfile, "w");
1143 for(i = 0; i < sk_X509_num(signers); i++)
1144 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1150 /* Minimal callback just to output policy info (if any) */
1152 static int smime_cb(int ok, X509_STORE_CTX *ctx)
1156 error = X509_STORE_CTX_get_error(ctx);
1158 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1159 && ((error != X509_V_OK) || (ok != 2)))
1162 policies_print(NULL, ctx);
1168 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1170 STACK_OF(GENERAL_NAME) *gens;
1173 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++)
1175 gens = sk_GENERAL_NAMES_value(gns, i);
1176 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++)
1178 gen = sk_GENERAL_NAME_value(gens, j);
1180 GENERAL_NAME_print(out, gen);
1181 BIO_puts(out, "\n");
1187 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1189 STACK_OF(CMS_SignerInfo) *sis;
1191 CMS_ReceiptRequest *rr;
1193 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1196 sis = CMS_get0_SignerInfos(cms);
1197 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++)
1199 si = sk_CMS_SignerInfo_value(sis, i);
1200 rv = CMS_get1_ReceiptRequest(si, &rr);
1201 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1203 BIO_puts(bio_err, " No Receipt Request\n");
1206 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1207 ERR_print_errors(bio_err);
1213 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1215 BIO_puts(out, " Signed Content ID:\n");
1216 idlen = ASN1_STRING_length(scid);
1217 id = (char *)ASN1_STRING_data(scid);
1218 BIO_dump_indent(out, id, idlen, 4);
1219 BIO_puts(out, " Receipts From");
1222 BIO_puts(out, " List:\n");
1223 gnames_stack_print(out, rlist);
1225 else if (allorfirst == 1)
1226 BIO_puts(out, ": First Tier\n");
1227 else if (allorfirst == 0)
1228 BIO_puts(out, ": All\n");
1230 BIO_printf(out, " Unknown (%d)\n", allorfirst);
1231 BIO_puts(out, " Receipts To:\n");
1232 gnames_stack_print(out, rto);
1235 CMS_ReceiptRequest_free(rr);
1239 static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK *ns)
1242 STACK_OF(GENERAL_NAMES) *ret;
1243 GENERAL_NAMES *gens = NULL;
1244 GENERAL_NAME *gen = NULL;
1245 ret = sk_GENERAL_NAMES_new_null();
1248 for (i = 0; i < sk_num(ns); i++)
1250 char *str = sk_value(ns, i);
1251 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
1254 gens = GENERAL_NAMES_new();
1257 if (!sk_GENERAL_NAME_push(gens, gen))
1260 if (!sk_GENERAL_NAMES_push(ret, gens))
1269 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1271 GENERAL_NAMES_free(gens);
1273 GENERAL_NAME_free(gen);
1278 static CMS_ReceiptRequest *make_receipt_request(STACK *rr_to, int rr_allorfirst,
1281 STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1282 CMS_ReceiptRequest *rr;
1283 rct_to = make_names_stack(rr_to);
1288 rct_from = make_names_stack(rr_from);
1294 rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,