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 unsigned char *pwri_pass
= NULL
, *pwri_tmp
= NULL
;
140 size_t secret_keylen
= 0, secret_keyidlen
= 0;
142 ASN1_OBJECT
*econtent_type
= NULL
;
144 X509_VERIFY_PARAM
*vpm
= NULL
;
153 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
154 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
|BIO_FP_TEXT
);
157 if (!load_config(bio_err
, NULL
))
160 while (!badarg
&& *args
&& *args
[0] == '-')
162 if (!strcmp (*args
, "-encrypt"))
163 operation
= SMIME_ENCRYPT
;
164 else if (!strcmp (*args
, "-decrypt"))
165 operation
= SMIME_DECRYPT
;
166 else if (!strcmp (*args
, "-sign"))
167 operation
= SMIME_SIGN
;
168 else if (!strcmp (*args
, "-sign_receipt"))
169 operation
= SMIME_SIGN_RECEIPT
;
170 else if (!strcmp (*args
, "-resign"))
171 operation
= SMIME_RESIGN
;
172 else if (!strcmp (*args
, "-verify"))
173 operation
= SMIME_VERIFY
;
174 else if (!strcmp (*args
, "-verify_retcode"))
176 else if (!strcmp(*args
,"-verify_receipt"))
178 operation
= SMIME_VERIFY_RECEIPT
;
184 else if (!strcmp (*args
, "-cmsout"))
185 operation
= SMIME_CMSOUT
;
186 else if (!strcmp (*args
, "-data_out"))
187 operation
= SMIME_DATAOUT
;
188 else if (!strcmp (*args
, "-data_create"))
189 operation
= SMIME_DATA_CREATE
;
190 else if (!strcmp (*args
, "-digest_verify"))
191 operation
= SMIME_DIGEST_VERIFY
;
192 else if (!strcmp (*args
, "-digest_create"))
193 operation
= SMIME_DIGEST_CREATE
;
194 else if (!strcmp (*args
, "-compress"))
195 operation
= SMIME_COMPRESS
;
196 else if (!strcmp (*args
, "-uncompress"))
197 operation
= SMIME_UNCOMPRESS
;
198 else if (!strcmp (*args
, "-EncryptedData_decrypt"))
199 operation
= SMIME_ENCRYPTED_DECRYPT
;
200 else if (!strcmp (*args
, "-EncryptedData_encrypt"))
201 operation
= SMIME_ENCRYPTED_ENCRYPT
;
202 #ifndef OPENSSL_NO_DES
203 else if (!strcmp (*args
, "-des3"))
204 cipher
= EVP_des_ede3_cbc();
205 else if (!strcmp (*args
, "-des"))
206 cipher
= EVP_des_cbc();
208 #ifndef OPENSSL_NO_SEED
209 else if (!strcmp (*args
, "-seed"))
210 cipher
= EVP_seed_cbc();
212 #ifndef OPENSSL_NO_RC2
213 else if (!strcmp (*args
, "-rc2-40"))
214 cipher
= EVP_rc2_40_cbc();
215 else if (!strcmp (*args
, "-rc2-128"))
216 cipher
= EVP_rc2_cbc();
217 else if (!strcmp (*args
, "-rc2-64"))
218 cipher
= EVP_rc2_64_cbc();
220 #ifndef OPENSSL_NO_AES
221 else if (!strcmp(*args
,"-aes128"))
222 cipher
= EVP_aes_128_cbc();
223 else if (!strcmp(*args
,"-aes192"))
224 cipher
= EVP_aes_192_cbc();
225 else if (!strcmp(*args
,"-aes256"))
226 cipher
= EVP_aes_256_cbc();
228 #ifndef OPENSSL_NO_CAMELLIA
229 else if (!strcmp(*args
,"-camellia128"))
230 cipher
= EVP_camellia_128_cbc();
231 else if (!strcmp(*args
,"-camellia192"))
232 cipher
= EVP_camellia_192_cbc();
233 else if (!strcmp(*args
,"-camellia256"))
234 cipher
= EVP_camellia_256_cbc();
236 else if (!strcmp (*args
, "-debug_decrypt"))
237 flags
|= CMS_DEBUG_DECRYPT
;
238 else if (!strcmp (*args
, "-text"))
240 else if (!strcmp (*args
, "-nointern"))
241 flags
|= CMS_NOINTERN
;
242 else if (!strcmp (*args
, "-noverify")
243 || !strcmp (*args
, "-no_signer_cert_verify"))
244 flags
|= CMS_NO_SIGNER_CERT_VERIFY
;
245 else if (!strcmp (*args
, "-nocerts"))
246 flags
|= CMS_NOCERTS
;
247 else if (!strcmp (*args
, "-noattr"))
249 else if (!strcmp (*args
, "-nodetach"))
250 flags
&= ~CMS_DETACHED
;
251 else if (!strcmp (*args
, "-nosmimecap"))
252 flags
|= CMS_NOSMIMECAP
;
253 else if (!strcmp (*args
, "-binary"))
255 else if (!strcmp (*args
, "-keyid"))
256 flags
|= CMS_USE_KEYID
;
257 else if (!strcmp (*args
, "-nosigs"))
259 else if (!strcmp (*args
, "-no_content_verify"))
260 flags
|= CMS_NO_CONTENT_VERIFY
;
261 else if (!strcmp (*args
, "-no_attr_verify"))
262 flags
|= CMS_NO_ATTR_VERIFY
;
263 else if (!strcmp (*args
, "-stream"))
265 else if (!strcmp (*args
, "-indef"))
267 else if (!strcmp (*args
, "-noindef"))
268 flags
&= ~CMS_STREAM
;
269 else if (!strcmp (*args
, "-nooldmime"))
270 flags
|= CMS_NOOLDMIMETYPE
;
271 else if (!strcmp (*args
, "-crlfeol"))
272 flags
|= CMS_CRLFEOL
;
273 else if (!strcmp (*args
, "-noout"))
275 else if (!strcmp (*args
, "-receipt_request_print"))
277 else if (!strcmp (*args
, "-receipt_request_all"))
279 else if (!strcmp (*args
, "-receipt_request_first"))
281 else if (!strcmp(*args
,"-receipt_request_from"))
287 rr_from
= sk_OPENSSL_STRING_new_null();
288 sk_OPENSSL_STRING_push(rr_from
, *args
);
290 else if (!strcmp(*args
,"-receipt_request_to"))
296 rr_to
= sk_OPENSSL_STRING_new_null();
297 sk_OPENSSL_STRING_push(rr_to
, *args
);
299 else if (!strcmp (*args
, "-print"))
304 else if (!strcmp(*args
,"-secretkey"))
310 secret_key
= string_to_hex(*args
, <mp
);
313 BIO_printf(bio_err
, "Invalid key %s\n", *args
);
316 secret_keylen
= (size_t)ltmp
;
318 else if (!strcmp(*args
,"-secretkeyid"))
324 secret_keyid
= string_to_hex(*args
, <mp
);
327 BIO_printf(bio_err
, "Invalid id %s\n", *args
);
330 secret_keyidlen
= (size_t)ltmp
;
332 else if (!strcmp(*args
,"-pwri_password"))
337 pwri_pass
= (unsigned char *)*args
;
339 else if (!strcmp(*args
,"-econtent_type"))
344 econtent_type
= OBJ_txt2obj(*args
, 0);
347 BIO_printf(bio_err
, "Invalid OID %s\n", *args
);
351 else if (!strcmp(*args
,"-rand"))
359 #ifndef OPENSSL_NO_ENGINE
360 else if (!strcmp(*args
,"-engine"))
367 else if (!strcmp(*args
,"-passin"))
373 else if (!strcmp (*args
, "-to"))
379 else if (!strcmp (*args
, "-from"))
385 else if (!strcmp (*args
, "-subject"))
391 else if (!strcmp (*args
, "-signer"))
395 /* If previous -signer argument add signer to list */
400 sksigners
= sk_OPENSSL_STRING_new_null();
401 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
403 keyfile
= signerfile
;
405 skkeys
= sk_OPENSSL_STRING_new_null();
406 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
409 signerfile
= *++args
;
411 else if (!strcmp (*args
, "-recip"))
417 else if (!strcmp (*args
, "-certsout"))
421 certsoutfile
= *++args
;
423 else if (!strcmp (*args
, "-md"))
427 sign_md
= EVP_get_digestbyname(*++args
);
430 BIO_printf(bio_err
, "Unknown digest %s\n",
435 else if (!strcmp (*args
, "-inkey"))
439 /* If previous -inkey arument add signer to list */
444 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
448 sksigners
= sk_OPENSSL_STRING_new_null();
449 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
452 skkeys
= sk_OPENSSL_STRING_new_null();
453 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
457 else if (!strcmp (*args
, "-keyform"))
461 keyform
= str2fmt(*++args
);
463 else if (!strcmp (*args
, "-rctform"))
467 rctformat
= str2fmt(*++args
);
469 else if (!strcmp (*args
, "-certfile"))
475 else if (!strcmp (*args
, "-CAfile"))
481 else if (!strcmp (*args
, "-CApath"))
487 else if (!strcmp (*args
, "-in"))
493 else if (!strcmp (*args
, "-inform"))
497 informat
= str2fmt(*++args
);
499 else if (!strcmp (*args
, "-outform"))
503 outformat
= str2fmt(*++args
);
505 else if (!strcmp (*args
, "-out"))
511 else if (!strcmp (*args
, "-content"))
517 else if (args_verify(&args
, NULL
, &badarg
, bio_err
, &vpm
))
519 else if ((cipher
= EVP_get_cipherbyname(*args
+ 1)) == NULL
)
524 if (((rr_allorfirst
!= -1) || rr_from
) && !rr_to
)
526 BIO_puts(bio_err
, "No Signed Receipts Recipients\n");
530 if (!(operation
& SMIME_SIGNERS
) && (rr_to
|| rr_from
))
532 BIO_puts(bio_err
, "Signed receipts only allowed with -sign\n");
535 if (!(operation
& SMIME_SIGNERS
) && (skkeys
|| sksigners
))
537 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
541 if (operation
& SMIME_SIGNERS
)
543 if (keyfile
&& !signerfile
)
545 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
548 /* Check to see if any final signer needs to be appended */
552 sksigners
= sk_OPENSSL_STRING_new_null();
553 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
555 skkeys
= sk_OPENSSL_STRING_new_null();
557 keyfile
= signerfile
;
558 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
562 BIO_printf(bio_err
, "No signer certificate specified\n");
570 else if (operation
== SMIME_DECRYPT
)
572 if (!recipfile
&& !keyfile
&& !secret_key
&& !pwri_pass
)
574 BIO_printf(bio_err
, "No recipient certificate or key specified\n");
578 else if (operation
== SMIME_ENCRYPT
)
580 if (!*args
&& !secret_key
&& !pwri_pass
)
582 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
593 BIO_printf (bio_err
, "Usage cms [options] cert.pem ...\n");
594 BIO_printf (bio_err
, "where options are\n");
595 BIO_printf (bio_err
, "-encrypt encrypt message\n");
596 BIO_printf (bio_err
, "-decrypt decrypt encrypted message\n");
597 BIO_printf (bio_err
, "-sign sign message\n");
598 BIO_printf (bio_err
, "-verify verify signed message\n");
599 BIO_printf (bio_err
, "-cmsout output CMS structure\n");
600 #ifndef OPENSSL_NO_DES
601 BIO_printf (bio_err
, "-des3 encrypt with triple DES\n");
602 BIO_printf (bio_err
, "-des encrypt with DES\n");
604 #ifndef OPENSSL_NO_SEED
605 BIO_printf (bio_err
, "-seed encrypt with SEED\n");
607 #ifndef OPENSSL_NO_RC2
608 BIO_printf (bio_err
, "-rc2-40 encrypt with RC2-40 (default)\n");
609 BIO_printf (bio_err
, "-rc2-64 encrypt with RC2-64\n");
610 BIO_printf (bio_err
, "-rc2-128 encrypt with RC2-128\n");
612 #ifndef OPENSSL_NO_AES
613 BIO_printf (bio_err
, "-aes128, -aes192, -aes256\n");
614 BIO_printf (bio_err
, " encrypt PEM output with cbc aes\n");
616 #ifndef OPENSSL_NO_CAMELLIA
617 BIO_printf (bio_err
, "-camellia128, -camellia192, -camellia256\n");
618 BIO_printf (bio_err
, " encrypt PEM output with cbc camellia\n");
620 BIO_printf (bio_err
, "-nointern don't search certificates in message for signer\n");
621 BIO_printf (bio_err
, "-nosigs don't verify message signature\n");
622 BIO_printf (bio_err
, "-noverify don't verify signers certificate\n");
623 BIO_printf (bio_err
, "-nocerts don't include signers certificate when signing\n");
624 BIO_printf (bio_err
, "-nodetach use opaque signing\n");
625 BIO_printf (bio_err
, "-noattr don't include any signed attributes\n");
626 BIO_printf (bio_err
, "-binary don't translate message to text\n");
627 BIO_printf (bio_err
, "-certfile file other certificates file\n");
628 BIO_printf (bio_err
, "-certsout file certificate output file\n");
629 BIO_printf (bio_err
, "-signer file signer certificate file\n");
630 BIO_printf (bio_err
, "-recip file recipient certificate file for decryption\n");
631 BIO_printf (bio_err
, "-keyid use subject key identifier\n");
632 BIO_printf (bio_err
, "-in file input file\n");
633 BIO_printf (bio_err
, "-inform arg input format SMIME (default), PEM or DER\n");
634 BIO_printf (bio_err
, "-inkey file input private key (if not signer or recipient)\n");
635 BIO_printf (bio_err
, "-keyform arg input private key format (PEM or ENGINE)\n");
636 BIO_printf (bio_err
, "-out file output file\n");
637 BIO_printf (bio_err
, "-outform arg output format SMIME (default), PEM or DER\n");
638 BIO_printf (bio_err
, "-content file supply or override content for detached signature\n");
639 BIO_printf (bio_err
, "-to addr to address\n");
640 BIO_printf (bio_err
, "-from ad from address\n");
641 BIO_printf (bio_err
, "-subject s subject\n");
642 BIO_printf (bio_err
, "-text include or delete text MIME headers\n");
643 BIO_printf (bio_err
, "-CApath dir trusted certificates directory\n");
644 BIO_printf (bio_err
, "-CAfile file trusted certificates file\n");
645 BIO_printf (bio_err
, "-crl_check check revocation status of signer's certificate using CRLs\n");
646 BIO_printf (bio_err
, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
647 #ifndef OPENSSL_NO_ENGINE
648 BIO_printf (bio_err
, "-engine e use engine e, possibly a hardware device.\n");
650 BIO_printf (bio_err
, "-passin arg input file pass phrase source\n");
651 BIO_printf(bio_err
, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
, LIST_SEPARATOR_CHAR
);
652 BIO_printf(bio_err
, " load the file (or the files in the directory) into\n");
653 BIO_printf(bio_err
, " the random number generator\n");
654 BIO_printf (bio_err
, "cert.pem recipient certificate(s) for encryption\n");
658 #ifndef OPENSSL_NO_ENGINE
659 e
= setup_engine(bio_err
, engine
, 0);
662 if (!app_passwd(bio_err
, passargin
, NULL
, &passin
, NULL
))
664 BIO_printf(bio_err
, "Error getting password\n");
670 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
));
672 BIO_printf(bio_err
,"%ld semi-random bytes loaded\n",
673 app_RAND_load_files(inrand
));
678 if (!(operation
& SMIME_SIGNERS
))
679 flags
&= ~CMS_DETACHED
;
681 if (operation
& SMIME_OP
)
683 if (outformat
== FORMAT_ASN1
)
688 if (flags
& CMS_BINARY
)
692 if (operation
& SMIME_IP
)
694 if (informat
== FORMAT_ASN1
)
699 if (flags
& CMS_BINARY
)
703 if (operation
== SMIME_ENCRYPT
)
707 #ifndef OPENSSL_NO_DES
708 cipher
= EVP_des_ede3_cbc();
710 BIO_printf(bio_err
, "No cipher selected\n");
715 if (secret_key
&& !secret_keyid
)
717 BIO_printf(bio_err
, "No secret key id\n");
722 encerts
= sk_X509_new_null();
725 if (!(cert
= load_cert(bio_err
,*args
,FORMAT_PEM
,
726 NULL
, e
, "recipient certificate file")))
728 sk_X509_push(encerts
, cert
);
736 if (!(other
= load_certs(bio_err
,certfile
,FORMAT_PEM
, NULL
,
737 e
, "certificate file")))
739 ERR_print_errors(bio_err
);
744 if (recipfile
&& (operation
== SMIME_DECRYPT
))
746 if (!(recip
= load_cert(bio_err
,recipfile
,FORMAT_PEM
,NULL
,
747 e
, "recipient certificate file")))
749 ERR_print_errors(bio_err
);
754 if (operation
== SMIME_SIGN_RECEIPT
)
756 if (!(signer
= load_cert(bio_err
,signerfile
,FORMAT_PEM
,NULL
,
757 e
, "receipt signer certificate file")))
759 ERR_print_errors(bio_err
);
764 if (operation
== SMIME_DECRYPT
)
769 else if ((operation
== SMIME_SIGN
) || (operation
== SMIME_SIGN_RECEIPT
))
772 keyfile
= signerfile
;
778 key
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
786 if (!(in
= BIO_new_file(infile
, inmode
)))
789 "Can't open input file %s\n", infile
);
794 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
796 if (operation
& SMIME_IP
)
798 if (informat
== FORMAT_SMIME
)
799 cms
= SMIME_read_CMS(in
, &indata
);
800 else if (informat
== FORMAT_PEM
)
801 cms
= PEM_read_bio_CMS(in
, NULL
, NULL
, NULL
);
802 else if (informat
== FORMAT_ASN1
)
803 cms
= d2i_CMS_bio(in
, NULL
);
806 BIO_printf(bio_err
, "Bad input format for CMS file\n");
812 BIO_printf(bio_err
, "Error reading S/MIME message\n");
818 if (!(indata
= BIO_new_file(contfile
, "rb")))
820 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
826 STACK_OF(X509
) *allcerts
;
827 allcerts
= CMS_get1_certs(cms
);
828 if (!save_certs(certsoutfile
, allcerts
))
831 "Error writing certs to %s\n",
836 sk_X509_pop_free(allcerts
, X509_free
);
842 char *rctmode
= (rctformat
== FORMAT_ASN1
) ? "rb" : "r";
843 if (!(rctin
= BIO_new_file(rctfile
, rctmode
)))
846 "Can't open receipt file %s\n", rctfile
);
850 if (rctformat
== FORMAT_SMIME
)
851 rcms
= SMIME_read_CMS(rctin
, NULL
);
852 else if (rctformat
== FORMAT_PEM
)
853 rcms
= PEM_read_bio_CMS(rctin
, NULL
, NULL
, NULL
);
854 else if (rctformat
== FORMAT_ASN1
)
855 rcms
= d2i_CMS_bio(rctin
, NULL
);
858 BIO_printf(bio_err
, "Bad input format for receipt\n");
864 BIO_printf(bio_err
, "Error reading receipt\n");
871 if (!(out
= BIO_new_file(outfile
, outmode
)))
874 "Can't open output file %s\n", outfile
);
880 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
881 #ifdef OPENSSL_SYS_VMS
883 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
884 out
= BIO_push(tmpbio
, out
);
889 if ((operation
== SMIME_VERIFY
) || (operation
== SMIME_VERIFY_RECEIPT
))
891 if (!(store
= setup_verify(bio_err
, CAfile
, CApath
)))
893 X509_STORE_set_verify_cb(store
, cms_cb
);
895 X509_STORE_set1_param(store
, vpm
);
901 if (operation
== SMIME_DATA_CREATE
)
903 cms
= CMS_data_create(in
, flags
);
905 else if (operation
== SMIME_DIGEST_CREATE
)
907 cms
= CMS_digest_create(in
, sign_md
, flags
);
909 else if (operation
== SMIME_COMPRESS
)
911 cms
= CMS_compress(in
, -1, flags
);
913 else if (operation
== SMIME_ENCRYPT
)
915 flags
|= CMS_PARTIAL
;
916 cms
= CMS_encrypt(encerts
, in
, cipher
, flags
);
921 if (!CMS_add0_recipient_key(cms
, NID_undef
,
922 secret_key
, secret_keylen
,
923 secret_keyid
, secret_keyidlen
,
926 /* NULL these because call absorbs them */
932 pwri_tmp
= (unsigned char *)BUF_strdup((char *)pwri_pass
);
935 if (!CMS_add0_recipient_password(cms
,
936 -1, NID_undef
, NID_undef
,
941 if (!(flags
& CMS_STREAM
))
943 if (!CMS_final(cms
, in
, NULL
, flags
))
947 else if (operation
== SMIME_ENCRYPTED_ENCRYPT
)
949 cms
= CMS_EncryptedData_encrypt(in
, cipher
,
950 secret_key
, secret_keylen
,
954 else if (operation
== SMIME_SIGN_RECEIPT
)
956 CMS_ContentInfo
*srcms
= NULL
;
957 STACK_OF(CMS_SignerInfo
) *sis
;
959 sis
= CMS_get0_SignerInfos(cms
);
962 si
= sk_CMS_SignerInfo_value(sis
, 0);
963 srcms
= CMS_sign_receipt(si
, signer
, key
, other
, flags
);
966 CMS_ContentInfo_free(cms
);
969 else if (operation
& SMIME_SIGNERS
)
972 /* If detached data content we enable streaming if
973 * S/MIME output format.
975 if (operation
== SMIME_SIGN
)
978 if (flags
& CMS_DETACHED
)
980 if (outformat
== FORMAT_SMIME
)
983 flags
|= CMS_PARTIAL
;
984 cms
= CMS_sign(NULL
, NULL
, other
, in
, flags
);
988 CMS_set1_eContentType(cms
, econtent_type
);
992 rr
= make_receipt_request(rr_to
, rr_allorfirst
,
997 "Signed Receipt Request Creation Error\n");
1003 flags
|= CMS_REUSE_DIGEST
;
1004 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++)
1007 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
1008 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
1009 signer
= load_cert(bio_err
, signerfile
,FORMAT_PEM
, NULL
,
1010 e
, "signer certificate");
1013 key
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
1014 "signing key file");
1017 si
= CMS_add1_signer(cms
, signer
, key
, sign_md
, flags
);
1020 if (rr
&& !CMS_add1_ReceiptRequest(si
, rr
))
1027 /* If not streaming or resigning finalize structure */
1028 if ((operation
== SMIME_SIGN
) && !(flags
& CMS_STREAM
))
1030 if (!CMS_final(cms
, in
, NULL
, flags
))
1037 BIO_printf(bio_err
, "Error creating CMS structure\n");
1042 if (operation
== SMIME_DECRYPT
)
1044 if (flags
& CMS_DEBUG_DECRYPT
)
1045 CMS_decrypt(cms
, NULL
, NULL
, NULL
, NULL
, flags
);
1049 if (!CMS_decrypt_set1_key(cms
,
1050 secret_key
, secret_keylen
,
1051 secret_keyid
, secret_keyidlen
))
1054 "Error decrypting CMS using secret key\n");
1061 if (!CMS_decrypt_set1_pkey(cms
, key
, recip
))
1064 "Error decrypting CMS using private key\n");
1071 if (!CMS_decrypt_set1_password(cms
, pwri_pass
, -1))
1074 "Error decrypting CMS using password\n");
1079 if (!CMS_decrypt(cms
, NULL
, NULL
, indata
, out
, flags
))
1081 BIO_printf(bio_err
, "Error decrypting CMS structure\n");
1085 else if (operation
== SMIME_DATAOUT
)
1087 if (!CMS_data(cms
, out
, flags
))
1090 else if (operation
== SMIME_UNCOMPRESS
)
1092 if (!CMS_uncompress(cms
, indata
, out
, flags
))
1095 else if (operation
== SMIME_DIGEST_VERIFY
)
1097 if (CMS_digest_verify(cms
, indata
, out
, flags
) > 0)
1098 BIO_printf(bio_err
, "Verification successful\n");
1101 BIO_printf(bio_err
, "Verification failure\n");
1105 else if (operation
== SMIME_ENCRYPTED_DECRYPT
)
1107 if (!CMS_EncryptedData_decrypt(cms
, secret_key
, secret_keylen
,
1108 indata
, out
, flags
))
1111 else if (operation
== SMIME_VERIFY
)
1113 if (CMS_verify(cms
, other
, store
, indata
, out
, flags
) > 0)
1114 BIO_printf(bio_err
, "Verification successful\n");
1117 BIO_printf(bio_err
, "Verification failure\n");
1119 ret
= verify_err
+ 32;
1124 STACK_OF(X509
) *signers
;
1125 signers
= CMS_get0_signers(cms
);
1126 if (!save_certs(signerfile
, signers
))
1129 "Error writing signers to %s\n",
1134 sk_X509_free(signers
);
1137 receipt_request_print(bio_err
, cms
);
1140 else if (operation
== SMIME_VERIFY_RECEIPT
)
1142 if (CMS_verify_receipt(rcms
, cms
, other
, store
, flags
) > 0)
1143 BIO_printf(bio_err
, "Verification successful\n");
1146 BIO_printf(bio_err
, "Verification failure\n");
1155 CMS_ContentInfo_print_ctx(out
, cms
, 0, NULL
);
1157 else if (outformat
== FORMAT_SMIME
)
1160 BIO_printf(out
, "To: %s\n", to
);
1162 BIO_printf(out
, "From: %s\n", from
);
1164 BIO_printf(out
, "Subject: %s\n", subject
);
1165 if (operation
== SMIME_RESIGN
)
1166 ret
= SMIME_write_CMS(out
, cms
, indata
, flags
);
1168 ret
= SMIME_write_CMS(out
, cms
, in
, flags
);
1170 else if (outformat
== FORMAT_PEM
)
1171 ret
= PEM_write_bio_CMS_stream(out
, cms
, in
, flags
);
1172 else if (outformat
== FORMAT_ASN1
)
1173 ret
= i2d_CMS_bio_stream(out
,cms
, in
, flags
);
1176 BIO_printf(bio_err
, "Bad output format for CMS file\n");
1188 ERR_print_errors(bio_err
);
1190 app_RAND_write_file(NULL
, bio_err
);
1191 sk_X509_pop_free(encerts
, X509_free
);
1192 sk_X509_pop_free(other
, X509_free
);
1194 X509_VERIFY_PARAM_free(vpm
);
1196 sk_OPENSSL_STRING_free(sksigners
);
1198 sk_OPENSSL_STRING_free(skkeys
);
1200 OPENSSL_free(secret_key
);
1202 OPENSSL_free(secret_keyid
);
1204 OPENSSL_free(pwri_tmp
);
1206 ASN1_OBJECT_free(econtent_type
);
1208 CMS_ReceiptRequest_free(rr
);
1210 sk_OPENSSL_STRING_free(rr_to
);
1212 sk_OPENSSL_STRING_free(rr_from
);
1213 X509_STORE_free(store
);
1218 CMS_ContentInfo_free(cms
);
1219 CMS_ContentInfo_free(rcms
);
1224 if (passin
) OPENSSL_free(passin
);
1228 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
1234 tmp
= BIO_new_file(signerfile
, "w");
1236 for(i
= 0; i
< sk_X509_num(signers
); i
++)
1237 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
1243 /* Minimal callback just to output policy info (if any) */
1245 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
)
1249 error
= X509_STORE_CTX_get_error(ctx
);
1253 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
1254 && ((error
!= X509_V_OK
) || (ok
!= 2)))
1257 policies_print(NULL
, ctx
);
1263 static void gnames_stack_print(BIO
*out
, STACK_OF(GENERAL_NAMES
) *gns
)
1265 STACK_OF(GENERAL_NAME
) *gens
;
1268 for (i
= 0; i
< sk_GENERAL_NAMES_num(gns
); i
++)
1270 gens
= sk_GENERAL_NAMES_value(gns
, i
);
1271 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++)
1273 gen
= sk_GENERAL_NAME_value(gens
, j
);
1275 GENERAL_NAME_print(out
, gen
);
1276 BIO_puts(out
, "\n");
1282 static void receipt_request_print(BIO
*out
, CMS_ContentInfo
*cms
)
1284 STACK_OF(CMS_SignerInfo
) *sis
;
1286 CMS_ReceiptRequest
*rr
;
1288 STACK_OF(GENERAL_NAMES
) *rto
, *rlist
;
1291 sis
= CMS_get0_SignerInfos(cms
);
1292 for (i
= 0; i
< sk_CMS_SignerInfo_num(sis
); i
++)
1294 si
= sk_CMS_SignerInfo_value(sis
, i
);
1295 rv
= CMS_get1_ReceiptRequest(si
, &rr
);
1296 BIO_printf(bio_err
, "Signer %d:\n", i
+ 1);
1298 BIO_puts(bio_err
, " No Receipt Request\n");
1301 BIO_puts(bio_err
, " Receipt Request Parse Error\n");
1302 ERR_print_errors(bio_err
);
1308 CMS_ReceiptRequest_get0_values(rr
, &scid
, &allorfirst
,
1310 BIO_puts(out
, " Signed Content ID:\n");
1311 idlen
= ASN1_STRING_length(scid
);
1312 id
= (char *)ASN1_STRING_data(scid
);
1313 BIO_dump_indent(out
, id
, idlen
, 4);
1314 BIO_puts(out
, " Receipts From");
1317 BIO_puts(out
, " List:\n");
1318 gnames_stack_print(out
, rlist
);
1320 else if (allorfirst
== 1)
1321 BIO_puts(out
, ": First Tier\n");
1322 else if (allorfirst
== 0)
1323 BIO_puts(out
, ": All\n");
1325 BIO_printf(out
, " Unknown (%d)\n", allorfirst
);
1326 BIO_puts(out
, " Receipts To:\n");
1327 gnames_stack_print(out
, rto
);
1330 CMS_ReceiptRequest_free(rr
);
1334 static STACK_OF(GENERAL_NAMES
) *make_names_stack(STACK_OF(OPENSSL_STRING
) *ns
)
1337 STACK_OF(GENERAL_NAMES
) *ret
;
1338 GENERAL_NAMES
*gens
= NULL
;
1339 GENERAL_NAME
*gen
= NULL
;
1340 ret
= sk_GENERAL_NAMES_new_null();
1343 for (i
= 0; i
< sk_OPENSSL_STRING_num(ns
); i
++)
1345 char *str
= sk_OPENSSL_STRING_value(ns
, i
);
1346 gen
= a2i_GENERAL_NAME(NULL
, NULL
, NULL
, GEN_EMAIL
, str
, 0);
1349 gens
= GENERAL_NAMES_new();
1352 if (!sk_GENERAL_NAME_push(gens
, gen
))
1355 if (!sk_GENERAL_NAMES_push(ret
, gens
))
1364 sk_GENERAL_NAMES_pop_free(ret
, GENERAL_NAMES_free
);
1366 GENERAL_NAMES_free(gens
);
1368 GENERAL_NAME_free(gen
);
1373 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
) *rr_to
,
1375 STACK_OF(OPENSSL_STRING
) *rr_from
)
1377 STACK_OF(GENERAL_NAMES
) *rct_to
, *rct_from
;
1378 CMS_ReceiptRequest
*rr
;
1379 rct_to
= make_names_stack(rr_to
);
1384 rct_from
= make_names_stack(rr_from
);
1390 rr
= CMS_ReceiptRequest_create0(NULL
, -1, rr_allorfirst
, rct_from
,