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);
77 #define SMIME_SIGNERS 0x40
78 #define SMIME_ENCRYPT (1 | SMIME_OP)
79 #define SMIME_DECRYPT (2 | SMIME_IP)
80 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
81 #define SMIME_VERIFY (4 | SMIME_IP)
82 #define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
83 #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
84 #define SMIME_DATAOUT (7 | SMIME_IP)
85 #define SMIME_DATA_CREATE (8 | SMIME_OP)
86 #define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
87 #define SMIME_DIGEST_CREATE (10 | SMIME_OP)
88 #define SMIME_UNCOMPRESS (11 | SMIME_IP)
89 #define SMIME_COMPRESS (12 | SMIME_OP)
90 #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
91 #define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
93 int MAIN(int, char **);
95 int MAIN(int argc, char **argv)
101 const char *inmode = "r", *outmode = "w";
102 char *infile = NULL, *outfile = NULL;
103 char *signerfile = NULL, *recipfile = NULL;
104 STACK *sksigners = NULL, *skkeys = NULL;
105 char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
106 const EVP_CIPHER *cipher = NULL;
107 CMS_ContentInfo *cms = NULL;
108 X509_STORE *store = NULL;
109 X509 *cert = NULL, *recip = NULL, *signer = NULL;
110 EVP_PKEY *key = NULL;
111 STACK_OF(X509) *encerts = NULL, *other = NULL;
112 BIO *in = NULL, *out = NULL, *indata = NULL;
114 int flags = CMS_DETACHED, noout = 0, print = 0;
116 char *to = NULL, *from = NULL, *subject = NULL;
117 char *CAfile = NULL, *CApath = NULL;
118 char *passargin = NULL, *passin = NULL;
121 const EVP_MD *sign_md = NULL;
122 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
123 int keyform = FORMAT_PEM;
124 #ifndef OPENSSL_NO_ENGINE
127 unsigned char *secret_key = NULL, *secret_keyid = NULL;
128 size_t secret_keylen = 0, secret_keyidlen = 0;
130 ASN1_OBJECT *econtent_type = NULL;
132 X509_VERIFY_PARAM *vpm = NULL;
141 if ((bio_err = BIO_new(BIO_s_file())) != NULL)
142 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
145 if (!load_config(bio_err, NULL))
148 while (!badarg && *args && *args[0] == '-')
150 if (!strcmp (*args, "-encrypt"))
151 operation = SMIME_ENCRYPT;
152 else if (!strcmp (*args, "-decrypt"))
153 operation = SMIME_DECRYPT;
154 else if (!strcmp (*args, "-sign"))
155 operation = SMIME_SIGN;
156 else if (!strcmp (*args, "-resign"))
157 operation = SMIME_RESIGN;
158 else if (!strcmp (*args, "-verify"))
159 operation = SMIME_VERIFY;
160 else if (!strcmp (*args, "-cmsout"))
161 operation = SMIME_CMSOUT;
162 else if (!strcmp (*args, "-data_out"))
163 operation = SMIME_DATAOUT;
164 else if (!strcmp (*args, "-data_create"))
165 operation = SMIME_DATA_CREATE;
166 else if (!strcmp (*args, "-digest_verify"))
167 operation = SMIME_DIGEST_VERIFY;
168 else if (!strcmp (*args, "-digest_create"))
169 operation = SMIME_DIGEST_CREATE;
170 else if (!strcmp (*args, "-compress"))
171 operation = SMIME_COMPRESS;
172 else if (!strcmp (*args, "-uncompress"))
173 operation = SMIME_UNCOMPRESS;
174 else if (!strcmp (*args, "-EncryptedData_decrypt"))
175 operation = SMIME_ENCRYPTED_DECRYPT;
176 else if (!strcmp (*args, "-EncryptedData_encrypt"))
177 operation = SMIME_ENCRYPTED_ENCRYPT;
178 #ifndef OPENSSL_NO_DES
179 else if (!strcmp (*args, "-des3"))
180 cipher = EVP_des_ede3_cbc();
181 else if (!strcmp (*args, "-des"))
182 cipher = EVP_des_cbc();
184 #ifndef OPENSSL_NO_SEED
185 else if (!strcmp (*args, "-seed"))
186 cipher = EVP_seed_cbc();
188 #ifndef OPENSSL_NO_RC2
189 else if (!strcmp (*args, "-rc2-40"))
190 cipher = EVP_rc2_40_cbc();
191 else if (!strcmp (*args, "-rc2-128"))
192 cipher = EVP_rc2_cbc();
193 else if (!strcmp (*args, "-rc2-64"))
194 cipher = EVP_rc2_64_cbc();
196 #ifndef OPENSSL_NO_AES
197 else if (!strcmp(*args,"-aes128"))
198 cipher = EVP_aes_128_cbc();
199 else if (!strcmp(*args,"-aes192"))
200 cipher = EVP_aes_192_cbc();
201 else if (!strcmp(*args,"-aes256"))
202 cipher = EVP_aes_256_cbc();
204 #ifndef OPENSSL_NO_CAMELLIA
205 else if (!strcmp(*args,"-camellia128"))
206 cipher = EVP_camellia_128_cbc();
207 else if (!strcmp(*args,"-camellia192"))
208 cipher = EVP_camellia_192_cbc();
209 else if (!strcmp(*args,"-camellia256"))
210 cipher = EVP_camellia_256_cbc();
212 else if (!strcmp (*args, "-text"))
214 else if (!strcmp (*args, "-nointern"))
215 flags |= CMS_NOINTERN;
216 else if (!strcmp (*args, "-noverify")
217 || !strcmp (*args, "-no_signer_cert_verify"))
218 flags |= CMS_NO_SIGNER_CERT_VERIFY;
219 else if (!strcmp (*args, "-nocerts"))
220 flags |= CMS_NOCERTS;
221 else if (!strcmp (*args, "-noattr"))
223 else if (!strcmp (*args, "-nodetach"))
224 flags &= ~CMS_DETACHED;
225 else if (!strcmp (*args, "-nosmimecap"))
226 flags |= CMS_NOSMIMECAP;
227 else if (!strcmp (*args, "-binary"))
229 else if (!strcmp (*args, "-keyid"))
230 flags |= CMS_USE_KEYID;
231 else if (!strcmp (*args, "-nosigs"))
233 else if (!strcmp (*args, "-no_content_verify"))
234 flags |= CMS_NO_CONTENT_VERIFY;
235 else if (!strcmp (*args, "-no_attr_verify"))
236 flags |= CMS_NO_ATTR_VERIFY;
237 else if (!strcmp (*args, "-stream"))
239 else if (!strcmp (*args, "-indef"))
241 else if (!strcmp (*args, "-noindef"))
242 flags &= ~CMS_STREAM;
243 else if (!strcmp (*args, "-nooldmime"))
244 flags |= CMS_NOOLDMIMETYPE;
245 else if (!strcmp (*args, "-crlfeol"))
246 flags |= CMS_CRLFEOL;
247 else if (!strcmp (*args, "-noout"))
249 else if (!strcmp (*args, "-receipt_request_print"))
251 else if (!strcmp (*args, "-print"))
256 else if (!strcmp(*args,"-secretkey"))
262 secret_key = string_to_hex(*args, <mp);
265 BIO_printf(bio_err, "Invalid key %s\n", *args);
268 secret_keylen = (size_t)ltmp;
270 else if (!strcmp(*args,"-secretkeyid"))
276 secret_keyid = string_to_hex(*args, <mp);
279 BIO_printf(bio_err, "Invalid id %s\n", *args);
282 secret_keyidlen = (size_t)ltmp;
284 else if (!strcmp(*args,"-econtent_type"))
289 econtent_type = OBJ_txt2obj(*args, 0);
292 BIO_printf(bio_err, "Invalid OID %s\n", *args);
296 else if (!strcmp(*args,"-rand"))
304 #ifndef OPENSSL_NO_ENGINE
305 else if (!strcmp(*args,"-engine"))
312 else if (!strcmp(*args,"-passin"))
318 else if (!strcmp (*args, "-to"))
324 else if (!strcmp (*args, "-from"))
330 else if (!strcmp (*args, "-subject"))
336 else if (!strcmp (*args, "-signer"))
340 /* If previous -signer argument add signer to list */
345 sksigners = sk_new_null();
346 sk_push(sksigners, signerfile);
348 keyfile = signerfile;
350 skkeys = sk_new_null();
351 sk_push(skkeys, keyfile);
354 signerfile = *++args;
356 else if (!strcmp (*args, "-recip"))
362 else if (!strcmp (*args, "-md"))
366 sign_md = EVP_get_digestbyname(*++args);
369 BIO_printf(bio_err, "Unknown digest %s\n",
374 else if (!strcmp (*args, "-inkey"))
378 /* If previous -inkey arument add signer to list */
383 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
387 sksigners = sk_new_null();
388 sk_push(sksigners, signerfile);
391 skkeys = sk_new_null();
392 sk_push(skkeys, keyfile);
396 else if (!strcmp (*args, "-keyform"))
400 keyform = str2fmt(*++args);
402 else if (!strcmp (*args, "-certfile"))
408 else if (!strcmp (*args, "-CAfile"))
414 else if (!strcmp (*args, "-CApath"))
420 else if (!strcmp (*args, "-in"))
426 else if (!strcmp (*args, "-inform"))
430 informat = str2fmt(*++args);
432 else if (!strcmp (*args, "-outform"))
436 outformat = str2fmt(*++args);
438 else if (!strcmp (*args, "-out"))
444 else if (!strcmp (*args, "-content"))
450 else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
452 else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
457 if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners))
459 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
463 if (operation & SMIME_SIGNERS)
465 /* Check to see if any final signer needs to be appended */
466 if (keyfile && !signerfile)
468 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
474 sksigners = sk_new_null();
475 sk_push(sksigners, signerfile);
477 skkeys = sk_new_null();
479 keyfile = signerfile;
480 sk_push(skkeys, keyfile);
484 BIO_printf(bio_err, "No signer certificate specified\n");
491 else if (operation == SMIME_DECRYPT)
493 if (!recipfile && !keyfile && !secret_key)
495 BIO_printf(bio_err, "No recipient certificate or key specified\n");
499 else if (operation == SMIME_ENCRYPT)
501 if (!*args && !secret_key)
503 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
514 BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n");
515 BIO_printf (bio_err, "where options are\n");
516 BIO_printf (bio_err, "-encrypt encrypt message\n");
517 BIO_printf (bio_err, "-decrypt decrypt encrypted message\n");
518 BIO_printf (bio_err, "-sign sign message\n");
519 BIO_printf (bio_err, "-verify verify signed message\n");
520 BIO_printf (bio_err, "-cmsout output CMS structure\n");
521 #ifndef OPENSSL_NO_DES
522 BIO_printf (bio_err, "-des3 encrypt with triple DES\n");
523 BIO_printf (bio_err, "-des encrypt with DES\n");
525 #ifndef OPENSSL_NO_SEED
526 BIO_printf (bio_err, "-seed encrypt with SEED\n");
528 #ifndef OPENSSL_NO_RC2
529 BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
530 BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n");
531 BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n");
533 #ifndef OPENSSL_NO_AES
534 BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
535 BIO_printf (bio_err, " encrypt PEM output with cbc aes\n");
537 #ifndef OPENSSL_NO_CAMELLIA
538 BIO_printf (bio_err, "-camellia128, -camellia192, -camellia256\n");
539 BIO_printf (bio_err, " encrypt PEM output with cbc camellia\n");
541 BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n");
542 BIO_printf (bio_err, "-nosigs don't verify message signature\n");
543 BIO_printf (bio_err, "-noverify don't verify signers certificate\n");
544 BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n");
545 BIO_printf (bio_err, "-nodetach use opaque signing\n");
546 BIO_printf (bio_err, "-noattr don't include any signed attributes\n");
547 BIO_printf (bio_err, "-binary don't translate message to text\n");
548 BIO_printf (bio_err, "-certfile file other certificates file\n");
549 BIO_printf (bio_err, "-signer file signer certificate file\n");
550 BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n");
551 BIO_printf (bio_err, "-skeyid use subject key identifier\n");
552 BIO_printf (bio_err, "-in file input file\n");
553 BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
554 BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n");
555 BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
556 BIO_printf (bio_err, "-out file output file\n");
557 BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
558 BIO_printf (bio_err, "-content file supply or override content for detached signature\n");
559 BIO_printf (bio_err, "-to addr to address\n");
560 BIO_printf (bio_err, "-from ad from address\n");
561 BIO_printf (bio_err, "-subject s subject\n");
562 BIO_printf (bio_err, "-text include or delete text MIME headers\n");
563 BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
564 BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
565 BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
566 BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
567 #ifndef OPENSSL_NO_ENGINE
568 BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n");
570 BIO_printf (bio_err, "-passin arg input file pass phrase source\n");
571 BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
572 BIO_printf(bio_err, " load the file (or the files in the directory) into\n");
573 BIO_printf(bio_err, " the random number generator\n");
574 BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
578 #ifndef OPENSSL_NO_ENGINE
579 e = setup_engine(bio_err, engine, 0);
582 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
584 BIO_printf(bio_err, "Error getting password\n");
590 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
592 BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
593 app_RAND_load_files(inrand));
598 if (!(operation & SMIME_SIGNERS))
599 flags &= ~CMS_DETACHED;
601 if (operation & SMIME_OP)
603 if (outformat == FORMAT_ASN1)
608 if (flags & CMS_BINARY)
612 if (operation & SMIME_IP)
614 if (informat == FORMAT_ASN1)
619 if (flags & CMS_BINARY)
623 if (operation == SMIME_ENCRYPT)
627 #ifndef OPENSSL_NO_DES
628 cipher = EVP_des_ede3_cbc();
630 BIO_printf(bio_err, "No cipher selected\n");
635 if (secret_key && !secret_keyid)
637 BIO_printf(bio_err, "No sectre key id\n");
642 encerts = sk_X509_new_null();
645 if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
646 NULL, e, "recipient certificate file")))
648 sk_X509_push(encerts, cert);
656 if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
657 e, "certificate file")))
659 ERR_print_errors(bio_err);
664 if (recipfile && (operation == SMIME_DECRYPT))
666 if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
667 e, "recipient certificate file")))
669 ERR_print_errors(bio_err);
674 if (operation == SMIME_DECRYPT)
679 else if (operation == SMIME_SIGN)
682 keyfile = signerfile;
688 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
696 if (!(in = BIO_new_file(infile, inmode)))
699 "Can't open input file %s\n", infile);
704 in = BIO_new_fp(stdin, BIO_NOCLOSE);
706 if (operation & SMIME_IP)
708 if (informat == FORMAT_SMIME)
709 cms = SMIME_read_CMS(in, &indata);
710 else if (informat == FORMAT_PEM)
711 cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
712 else if (informat == FORMAT_ASN1)
713 cms = d2i_CMS_bio(in, NULL);
716 BIO_printf(bio_err, "Bad input format for CMS file\n");
722 BIO_printf(bio_err, "Error reading S/MIME message\n");
728 if (!(indata = BIO_new_file(contfile, "rb")))
730 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
738 if (!(out = BIO_new_file(outfile, outmode)))
741 "Can't open output file %s\n", outfile);
747 out = BIO_new_fp(stdout, BIO_NOCLOSE);
748 #ifdef OPENSSL_SYS_VMS
750 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
751 out = BIO_push(tmpbio, out);
756 if (operation == SMIME_VERIFY)
758 if (!(store = setup_verify(bio_err, CAfile, CApath)))
760 X509_STORE_set_verify_cb_func(store, smime_cb);
762 X509_STORE_set1_param(store, vpm);
768 if (operation == SMIME_DATA_CREATE)
770 cms = CMS_data_create(in, flags);
772 else if (operation == SMIME_DIGEST_CREATE)
774 cms = CMS_digest_create(in, sign_md, flags);
776 else if (operation == SMIME_COMPRESS)
778 cms = CMS_compress(in, -1, flags);
780 else if (operation == SMIME_ENCRYPT)
782 flags |= CMS_PARTIAL;
783 cms = CMS_encrypt(encerts, in, cipher, flags);
788 if (!CMS_add0_recipient_key(cms, NID_undef,
789 secret_key, secret_keylen,
790 secret_keyid, secret_keyidlen,
793 /* NULL these because call absorbs them */
797 if (!(flags & CMS_STREAM))
799 if (!CMS_final(cms, in, flags))
803 else if (operation == SMIME_ENCRYPTED_ENCRYPT)
805 cms = CMS_EncryptedData_encrypt(in, cipher,
806 secret_key, secret_keylen,
810 else if (operation & SMIME_SIGNERS)
813 /* If detached data content we only enable streaming if
814 * S/MIME output format.
816 if (operation == SMIME_SIGN)
818 if (flags & CMS_DETACHED)
820 if (outformat != FORMAT_SMIME)
821 flags &= ~CMS_STREAM;
823 flags |= CMS_PARTIAL;
824 cms = CMS_sign(NULL, NULL, other, in, flags);
826 CMS_set1_eContentType(cms, econtent_type);
831 flags |= CMS_REUSE_DIGEST;
832 for (i = 0; i < sk_num(sksigners); i++)
834 signerfile = sk_value(sksigners, i);
835 keyfile = sk_value(skkeys, i);
836 signer = load_cert(bio_err, signerfile,FORMAT_PEM, NULL,
837 e, "signer certificate");
840 key = load_key(bio_err, keyfile, keyform, 0, passin, e,
844 if (!CMS_add1_signer(cms, signer, key, sign_md, flags))
851 /* If not streaming or resigning finalize structure */
852 if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM))
854 if (!CMS_final(cms, in, flags))
861 BIO_printf(bio_err, "Error creating CMS structure\n");
866 if (operation == SMIME_DECRYPT)
871 if (!CMS_decrypt_set1_key(cms,
872 secret_key, secret_keylen,
873 secret_keyid, secret_keyidlen))
876 "Error decrypting CMS using secret key\n");
883 if (!CMS_decrypt_set1_pkey(cms, key, recip))
886 "Error decrypting CMS using private key\n");
891 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags))
893 BIO_printf(bio_err, "Error decrypting CMS structure\n");
897 else if (operation == SMIME_DATAOUT)
899 if (!CMS_data(cms, out, flags))
902 else if (operation == SMIME_UNCOMPRESS)
904 if (!CMS_uncompress(cms, indata, out, flags))
907 else if (operation == SMIME_DIGEST_VERIFY)
909 if (CMS_digest_verify(cms, indata, out, flags) > 0)
910 BIO_printf(bio_err, "Verification successful\n");
913 BIO_printf(bio_err, "Verification failure\n");
917 else if (operation == SMIME_ENCRYPTED_DECRYPT)
919 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
923 else if (operation == SMIME_VERIFY)
925 if (CMS_verify(cms, other, store, indata, out, flags) > 0)
926 BIO_printf(bio_err, "Verification successful\n");
929 BIO_printf(bio_err, "Verification failure\n");
934 STACK_OF(X509) *signers;
935 signers = CMS_get0_signers(cms);
936 if (!save_certs(signerfile, signers))
939 "Error writing signers to %s\n",
944 sk_X509_free(signers);
947 receipt_request_print(bio_err, cms);
955 CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
957 else if (outformat == FORMAT_SMIME)
960 BIO_printf(out, "To: %s\n", to);
962 BIO_printf(out, "From: %s\n", from);
964 BIO_printf(out, "Subject: %s\n", subject);
965 if (operation == SMIME_RESIGN)
966 ret = SMIME_write_CMS(out, cms, indata, flags);
968 ret = SMIME_write_CMS(out, cms, in, flags);
970 else if (outformat == FORMAT_PEM)
971 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
972 else if (outformat == FORMAT_ASN1)
973 ret = i2d_CMS_bio_stream(out,cms, in, flags);
976 BIO_printf(bio_err, "Bad output format for CMS file\n");
988 ERR_print_errors(bio_err);
990 app_RAND_write_file(NULL, bio_err);
991 sk_X509_pop_free(encerts, X509_free);
992 sk_X509_pop_free(other, X509_free);
994 X509_VERIFY_PARAM_free(vpm);
1000 OPENSSL_free(secret_key);
1002 OPENSSL_free(secret_keyid);
1004 ASN1_OBJECT_free(econtent_type);
1005 X509_STORE_free(store);
1010 CMS_ContentInfo_free(cms);
1014 if (passin) OPENSSL_free(passin);
1018 static int save_certs(char *signerfile, STACK_OF(X509) *signers)
1024 tmp = BIO_new_file(signerfile, "w");
1026 for(i = 0; i < sk_X509_num(signers); i++)
1027 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1033 /* Minimal callback just to output policy info (if any) */
1035 static int smime_cb(int ok, X509_STORE_CTX *ctx)
1039 error = X509_STORE_CTX_get_error(ctx);
1041 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1042 && ((error != X509_V_OK) || (ok != 2)))
1045 policies_print(NULL, ctx);
1051 static void gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1053 STACK_OF(GENERAL_NAME) *gens;
1056 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++)
1058 gens = sk_GENERAL_NAMES_value(gns, i);
1059 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++)
1061 gen = sk_GENERAL_NAME_value(gens, j);
1063 GENERAL_NAME_print(out, gen);
1064 BIO_puts(out, "\n");
1070 static void receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1072 STACK_OF(CMS_SignerInfo) *sis;
1074 CMS_ReceiptRequest *rr;
1076 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1079 sis = CMS_get0_SignerInfos(cms);
1080 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++)
1082 si = sk_CMS_SignerInfo_value(sis, i);
1083 rv = CMS_get1_ReceiptRequest(si, &rr);
1084 BIO_printf(bio_err, "Signer %d:\n", i + 1);
1086 BIO_puts(bio_err, " No Receipt Request\n");
1089 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1090 ERR_print_errors(bio_err);
1096 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1098 BIO_puts(out, " Signed Content ID:\n");
1099 idlen = ASN1_STRING_length(scid);
1100 id = (char *)ASN1_STRING_data(scid);
1101 BIO_dump_indent(out, id, idlen, 4);
1102 BIO_puts(out, " Receipts From");
1105 BIO_puts(out, " List:\n");
1106 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);