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
, "-text"))
238 else if (!strcmp (*args
, "-nointern"))
239 flags
|= CMS_NOINTERN
;
240 else if (!strcmp (*args
, "-noverify")
241 || !strcmp (*args
, "-no_signer_cert_verify"))
242 flags
|= CMS_NO_SIGNER_CERT_VERIFY
;
243 else if (!strcmp (*args
, "-nocerts"))
244 flags
|= CMS_NOCERTS
;
245 else if (!strcmp (*args
, "-noattr"))
247 else if (!strcmp (*args
, "-nodetach"))
248 flags
&= ~CMS_DETACHED
;
249 else if (!strcmp (*args
, "-nosmimecap"))
250 flags
|= CMS_NOSMIMECAP
;
251 else if (!strcmp (*args
, "-binary"))
253 else if (!strcmp (*args
, "-keyid"))
254 flags
|= CMS_USE_KEYID
;
255 else if (!strcmp (*args
, "-nosigs"))
257 else if (!strcmp (*args
, "-no_content_verify"))
258 flags
|= CMS_NO_CONTENT_VERIFY
;
259 else if (!strcmp (*args
, "-no_attr_verify"))
260 flags
|= CMS_NO_ATTR_VERIFY
;
261 else if (!strcmp (*args
, "-stream"))
263 else if (!strcmp (*args
, "-indef"))
265 else if (!strcmp (*args
, "-noindef"))
266 flags
&= ~CMS_STREAM
;
267 else if (!strcmp (*args
, "-nooldmime"))
268 flags
|= CMS_NOOLDMIMETYPE
;
269 else if (!strcmp (*args
, "-crlfeol"))
270 flags
|= CMS_CRLFEOL
;
271 else if (!strcmp (*args
, "-noout"))
273 else if (!strcmp (*args
, "-receipt_request_print"))
275 else if (!strcmp (*args
, "-receipt_request_all"))
277 else if (!strcmp (*args
, "-receipt_request_first"))
279 else if (!strcmp(*args
,"-receipt_request_from"))
285 rr_from
= sk_OPENSSL_STRING_new_null();
286 sk_OPENSSL_STRING_push(rr_from
, *args
);
288 else if (!strcmp(*args
,"-receipt_request_to"))
294 rr_to
= sk_OPENSSL_STRING_new_null();
295 sk_OPENSSL_STRING_push(rr_to
, *args
);
297 else if (!strcmp (*args
, "-print"))
302 else if (!strcmp(*args
,"-secretkey"))
308 secret_key
= string_to_hex(*args
, <mp
);
311 BIO_printf(bio_err
, "Invalid key %s\n", *args
);
314 secret_keylen
= (size_t)ltmp
;
316 else if (!strcmp(*args
,"-secretkeyid"))
322 secret_keyid
= string_to_hex(*args
, <mp
);
325 BIO_printf(bio_err
, "Invalid id %s\n", *args
);
328 secret_keyidlen
= (size_t)ltmp
;
330 else if (!strcmp(*args
,"-pwri_password"))
335 pwri_pass
= (unsigned char *)*args
;
337 else if (!strcmp(*args
,"-econtent_type"))
342 econtent_type
= OBJ_txt2obj(*args
, 0);
345 BIO_printf(bio_err
, "Invalid OID %s\n", *args
);
349 else if (!strcmp(*args
,"-rand"))
357 #ifndef OPENSSL_NO_ENGINE
358 else if (!strcmp(*args
,"-engine"))
365 else if (!strcmp(*args
,"-passin"))
371 else if (!strcmp (*args
, "-to"))
377 else if (!strcmp (*args
, "-from"))
383 else if (!strcmp (*args
, "-subject"))
389 else if (!strcmp (*args
, "-signer"))
393 /* If previous -signer argument add signer to list */
398 sksigners
= sk_OPENSSL_STRING_new_null();
399 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
401 keyfile
= signerfile
;
403 skkeys
= sk_OPENSSL_STRING_new_null();
404 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
407 signerfile
= *++args
;
409 else if (!strcmp (*args
, "-recip"))
415 else if (!strcmp (*args
, "-certsout"))
419 certsoutfile
= *++args
;
421 else if (!strcmp (*args
, "-md"))
425 sign_md
= EVP_get_digestbyname(*++args
);
428 BIO_printf(bio_err
, "Unknown digest %s\n",
433 else if (!strcmp (*args
, "-inkey"))
437 /* If previous -inkey arument add signer to list */
442 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
446 sksigners
= sk_OPENSSL_STRING_new_null();
447 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
450 skkeys
= sk_OPENSSL_STRING_new_null();
451 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
455 else if (!strcmp (*args
, "-keyform"))
459 keyform
= str2fmt(*++args
);
461 else if (!strcmp (*args
, "-rctform"))
465 rctformat
= str2fmt(*++args
);
467 else if (!strcmp (*args
, "-certfile"))
473 else if (!strcmp (*args
, "-CAfile"))
479 else if (!strcmp (*args
, "-CApath"))
485 else if (!strcmp (*args
, "-in"))
491 else if (!strcmp (*args
, "-inform"))
495 informat
= str2fmt(*++args
);
497 else if (!strcmp (*args
, "-outform"))
501 outformat
= str2fmt(*++args
);
503 else if (!strcmp (*args
, "-out"))
509 else if (!strcmp (*args
, "-content"))
515 else if (args_verify(&args
, NULL
, &badarg
, bio_err
, &vpm
))
517 else if ((cipher
= EVP_get_cipherbyname(*args
+ 1)) == NULL
)
522 if (((rr_allorfirst
!= -1) || rr_from
) && !rr_to
)
524 BIO_puts(bio_err
, "No Signed Receipts Recipients\n");
528 if (!(operation
& SMIME_SIGNERS
) && (rr_to
|| rr_from
))
530 BIO_puts(bio_err
, "Signed receipts only allowed with -sign\n");
533 if (!(operation
& SMIME_SIGNERS
) && (skkeys
|| sksigners
))
535 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
539 if (operation
& SMIME_SIGNERS
)
541 if (keyfile
&& !signerfile
)
543 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
546 /* Check to see if any final signer needs to be appended */
550 sksigners
= sk_OPENSSL_STRING_new_null();
551 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
553 skkeys
= sk_OPENSSL_STRING_new_null();
555 keyfile
= signerfile
;
556 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
560 BIO_printf(bio_err
, "No signer certificate specified\n");
568 else if (operation
== SMIME_DECRYPT
)
570 if (!recipfile
&& !keyfile
&& !secret_key
&& !pwri_pass
)
572 BIO_printf(bio_err
, "No recipient certificate or key specified\n");
576 else if (operation
== SMIME_ENCRYPT
)
578 if (!*args
&& !secret_key
&& !pwri_pass
)
580 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
591 BIO_printf (bio_err
, "Usage cms [options] cert.pem ...\n");
592 BIO_printf (bio_err
, "where options are\n");
593 BIO_printf (bio_err
, "-encrypt encrypt message\n");
594 BIO_printf (bio_err
, "-decrypt decrypt encrypted message\n");
595 BIO_printf (bio_err
, "-sign sign message\n");
596 BIO_printf (bio_err
, "-verify verify signed message\n");
597 BIO_printf (bio_err
, "-cmsout output CMS structure\n");
598 #ifndef OPENSSL_NO_DES
599 BIO_printf (bio_err
, "-des3 encrypt with triple DES\n");
600 BIO_printf (bio_err
, "-des encrypt with DES\n");
602 #ifndef OPENSSL_NO_SEED
603 BIO_printf (bio_err
, "-seed encrypt with SEED\n");
605 #ifndef OPENSSL_NO_RC2
606 BIO_printf (bio_err
, "-rc2-40 encrypt with RC2-40 (default)\n");
607 BIO_printf (bio_err
, "-rc2-64 encrypt with RC2-64\n");
608 BIO_printf (bio_err
, "-rc2-128 encrypt with RC2-128\n");
610 #ifndef OPENSSL_NO_AES
611 BIO_printf (bio_err
, "-aes128, -aes192, -aes256\n");
612 BIO_printf (bio_err
, " encrypt PEM output with cbc aes\n");
614 #ifndef OPENSSL_NO_CAMELLIA
615 BIO_printf (bio_err
, "-camellia128, -camellia192, -camellia256\n");
616 BIO_printf (bio_err
, " encrypt PEM output with cbc camellia\n");
618 BIO_printf (bio_err
, "-nointern don't search certificates in message for signer\n");
619 BIO_printf (bio_err
, "-nosigs don't verify message signature\n");
620 BIO_printf (bio_err
, "-noverify don't verify signers certificate\n");
621 BIO_printf (bio_err
, "-nocerts don't include signers certificate when signing\n");
622 BIO_printf (bio_err
, "-nodetach use opaque signing\n");
623 BIO_printf (bio_err
, "-noattr don't include any signed attributes\n");
624 BIO_printf (bio_err
, "-binary don't translate message to text\n");
625 BIO_printf (bio_err
, "-certfile file other certificates file\n");
626 BIO_printf (bio_err
, "-certsout file certificate output file\n");
627 BIO_printf (bio_err
, "-signer file signer certificate file\n");
628 BIO_printf (bio_err
, "-recip file recipient certificate file for decryption\n");
629 BIO_printf (bio_err
, "-skeyid use subject key identifier\n");
630 BIO_printf (bio_err
, "-in file input file\n");
631 BIO_printf (bio_err
, "-inform arg input format SMIME (default), PEM or DER\n");
632 BIO_printf (bio_err
, "-inkey file input private key (if not signer or recipient)\n");
633 BIO_printf (bio_err
, "-keyform arg input private key format (PEM or ENGINE)\n");
634 BIO_printf (bio_err
, "-out file output file\n");
635 BIO_printf (bio_err
, "-outform arg output format SMIME (default), PEM or DER\n");
636 BIO_printf (bio_err
, "-content file supply or override content for detached signature\n");
637 BIO_printf (bio_err
, "-to addr to address\n");
638 BIO_printf (bio_err
, "-from ad from address\n");
639 BIO_printf (bio_err
, "-subject s subject\n");
640 BIO_printf (bio_err
, "-text include or delete text MIME headers\n");
641 BIO_printf (bio_err
, "-CApath dir trusted certificates directory\n");
642 BIO_printf (bio_err
, "-CAfile file trusted certificates file\n");
643 BIO_printf (bio_err
, "-crl_check check revocation status of signer's certificate using CRLs\n");
644 BIO_printf (bio_err
, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
645 #ifndef OPENSSL_NO_ENGINE
646 BIO_printf (bio_err
, "-engine e use engine e, possibly a hardware device.\n");
648 BIO_printf (bio_err
, "-passin arg input file pass phrase source\n");
649 BIO_printf(bio_err
, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
, LIST_SEPARATOR_CHAR
);
650 BIO_printf(bio_err
, " load the file (or the files in the directory) into\n");
651 BIO_printf(bio_err
, " the random number generator\n");
652 BIO_printf (bio_err
, "cert.pem recipient certificate(s) for encryption\n");
656 #ifndef OPENSSL_NO_ENGINE
657 e
= setup_engine(bio_err
, engine
, 0);
660 if (!app_passwd(bio_err
, passargin
, NULL
, &passin
, NULL
))
662 BIO_printf(bio_err
, "Error getting password\n");
668 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
));
670 BIO_printf(bio_err
,"%ld semi-random bytes loaded\n",
671 app_RAND_load_files(inrand
));
676 if (!(operation
& SMIME_SIGNERS
))
677 flags
&= ~CMS_DETACHED
;
679 if (operation
& SMIME_OP
)
681 if (outformat
== FORMAT_ASN1
)
686 if (flags
& CMS_BINARY
)
690 if (operation
& SMIME_IP
)
692 if (informat
== FORMAT_ASN1
)
697 if (flags
& CMS_BINARY
)
701 if (operation
== SMIME_ENCRYPT
)
705 #ifndef OPENSSL_NO_DES
706 cipher
= EVP_des_ede3_cbc();
708 BIO_printf(bio_err
, "No cipher selected\n");
713 if (secret_key
&& !secret_keyid
)
715 BIO_printf(bio_err
, "No secret key id\n");
720 encerts
= sk_X509_new_null();
723 if (!(cert
= load_cert(bio_err
,*args
,FORMAT_PEM
,
724 NULL
, e
, "recipient certificate file")))
726 sk_X509_push(encerts
, cert
);
734 if (!(other
= load_certs(bio_err
,certfile
,FORMAT_PEM
, NULL
,
735 e
, "certificate file")))
737 ERR_print_errors(bio_err
);
742 if (recipfile
&& (operation
== SMIME_DECRYPT
))
744 if (!(recip
= load_cert(bio_err
,recipfile
,FORMAT_PEM
,NULL
,
745 e
, "recipient certificate file")))
747 ERR_print_errors(bio_err
);
752 if (operation
== SMIME_SIGN_RECEIPT
)
754 if (!(signer
= load_cert(bio_err
,signerfile
,FORMAT_PEM
,NULL
,
755 e
, "receipt signer certificate file")))
757 ERR_print_errors(bio_err
);
762 if (operation
== SMIME_DECRYPT
)
767 else if ((operation
== SMIME_SIGN
) || (operation
== SMIME_SIGN_RECEIPT
))
770 keyfile
= signerfile
;
776 key
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
784 if (!(in
= BIO_new_file(infile
, inmode
)))
787 "Can't open input file %s\n", infile
);
792 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
794 if (operation
& SMIME_IP
)
796 if (informat
== FORMAT_SMIME
)
797 cms
= SMIME_read_CMS(in
, &indata
);
798 else if (informat
== FORMAT_PEM
)
799 cms
= PEM_read_bio_CMS(in
, NULL
, NULL
, NULL
);
800 else if (informat
== FORMAT_ASN1
)
801 cms
= d2i_CMS_bio(in
, NULL
);
804 BIO_printf(bio_err
, "Bad input format for CMS file\n");
810 BIO_printf(bio_err
, "Error reading S/MIME message\n");
816 if (!(indata
= BIO_new_file(contfile
, "rb")))
818 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
824 STACK_OF(X509
) *allcerts
;
825 allcerts
= CMS_get1_certs(cms
);
826 if (!save_certs(certsoutfile
, allcerts
))
829 "Error writing certs to %s\n",
834 sk_X509_pop_free(allcerts
, X509_free
);
840 char *rctmode
= (rctformat
== FORMAT_ASN1
) ? "rb" : "r";
841 if (!(rctin
= BIO_new_file(rctfile
, rctmode
)))
844 "Can't open receipt file %s\n", rctfile
);
848 if (rctformat
== FORMAT_SMIME
)
849 rcms
= SMIME_read_CMS(rctin
, NULL
);
850 else if (rctformat
== FORMAT_PEM
)
851 rcms
= PEM_read_bio_CMS(rctin
, NULL
, NULL
, NULL
);
852 else if (rctformat
== FORMAT_ASN1
)
853 rcms
= d2i_CMS_bio(rctin
, NULL
);
856 BIO_printf(bio_err
, "Bad input format for receipt\n");
862 BIO_printf(bio_err
, "Error reading receipt\n");
869 if (!(out
= BIO_new_file(outfile
, outmode
)))
872 "Can't open output file %s\n", outfile
);
878 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
879 #ifdef OPENSSL_SYS_VMS
881 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
882 out
= BIO_push(tmpbio
, out
);
887 if ((operation
== SMIME_VERIFY
) || (operation
== SMIME_VERIFY_RECEIPT
))
889 if (!(store
= setup_verify(bio_err
, CAfile
, CApath
)))
891 X509_STORE_set_verify_cb(store
, cms_cb
);
893 X509_STORE_set1_param(store
, vpm
);
899 if (operation
== SMIME_DATA_CREATE
)
901 cms
= CMS_data_create(in
, flags
);
903 else if (operation
== SMIME_DIGEST_CREATE
)
905 cms
= CMS_digest_create(in
, sign_md
, flags
);
907 else if (operation
== SMIME_COMPRESS
)
909 cms
= CMS_compress(in
, -1, flags
);
911 else if (operation
== SMIME_ENCRYPT
)
913 flags
|= CMS_PARTIAL
;
914 cms
= CMS_encrypt(encerts
, in
, cipher
, flags
);
919 if (!CMS_add0_recipient_key(cms
, NID_undef
,
920 secret_key
, secret_keylen
,
921 secret_keyid
, secret_keyidlen
,
924 /* NULL these because call absorbs them */
930 pwri_tmp
= (unsigned char *)BUF_strdup((char *)pwri_pass
);
933 if (!CMS_add0_recipient_password(cms
,
934 -1, NID_undef
, NID_undef
,
939 if (!(flags
& CMS_STREAM
))
941 if (!CMS_final(cms
, in
, NULL
, flags
))
945 else if (operation
== SMIME_ENCRYPTED_ENCRYPT
)
947 cms
= CMS_EncryptedData_encrypt(in
, cipher
,
948 secret_key
, secret_keylen
,
952 else if (operation
== SMIME_SIGN_RECEIPT
)
954 CMS_ContentInfo
*srcms
= NULL
;
955 STACK_OF(CMS_SignerInfo
) *sis
;
957 sis
= CMS_get0_SignerInfos(cms
);
960 si
= sk_CMS_SignerInfo_value(sis
, 0);
961 srcms
= CMS_sign_receipt(si
, signer
, key
, other
, flags
);
964 CMS_ContentInfo_free(cms
);
967 else if (operation
& SMIME_SIGNERS
)
970 /* If detached data content we enable streaming if
971 * S/MIME output format.
973 if (operation
== SMIME_SIGN
)
976 if (flags
& CMS_DETACHED
)
978 if (outformat
== FORMAT_SMIME
)
981 flags
|= CMS_PARTIAL
;
982 cms
= CMS_sign(NULL
, NULL
, other
, in
, flags
);
986 CMS_set1_eContentType(cms
, econtent_type
);
990 rr
= make_receipt_request(rr_to
, rr_allorfirst
,
995 "Signed Receipt Request Creation Error\n");
1001 flags
|= CMS_REUSE_DIGEST
;
1002 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++)
1005 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
1006 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
1007 signer
= load_cert(bio_err
, signerfile
,FORMAT_PEM
, NULL
,
1008 e
, "signer certificate");
1011 key
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
1012 "signing key file");
1015 si
= CMS_add1_signer(cms
, signer
, key
, sign_md
, flags
);
1018 if (rr
&& !CMS_add1_ReceiptRequest(si
, rr
))
1025 /* If not streaming or resigning finalize structure */
1026 if ((operation
== SMIME_SIGN
) && !(flags
& CMS_STREAM
))
1028 if (!CMS_final(cms
, in
, NULL
, flags
))
1035 BIO_printf(bio_err
, "Error creating CMS structure\n");
1040 if (operation
== SMIME_DECRYPT
)
1045 if (!CMS_decrypt_set1_key(cms
,
1046 secret_key
, secret_keylen
,
1047 secret_keyid
, secret_keyidlen
))
1050 "Error decrypting CMS using secret key\n");
1057 if (!CMS_decrypt_set1_pkey(cms
, key
, recip
))
1060 "Error decrypting CMS using private key\n");
1067 if (!CMS_decrypt_set1_password(cms
, pwri_pass
, -1))
1070 "Error decrypting CMS using password\n");
1075 if (!CMS_decrypt(cms
, NULL
, NULL
, indata
, out
, flags
))
1077 BIO_printf(bio_err
, "Error decrypting CMS structure\n");
1081 else if (operation
== SMIME_DATAOUT
)
1083 if (!CMS_data(cms
, out
, flags
))
1086 else if (operation
== SMIME_UNCOMPRESS
)
1088 if (!CMS_uncompress(cms
, indata
, out
, flags
))
1091 else if (operation
== SMIME_DIGEST_VERIFY
)
1093 if (CMS_digest_verify(cms
, indata
, out
, flags
) > 0)
1094 BIO_printf(bio_err
, "Verification successful\n");
1097 BIO_printf(bio_err
, "Verification failure\n");
1101 else if (operation
== SMIME_ENCRYPTED_DECRYPT
)
1103 if (!CMS_EncryptedData_decrypt(cms
, secret_key
, secret_keylen
,
1104 indata
, out
, flags
))
1107 else if (operation
== SMIME_VERIFY
)
1109 if (CMS_verify(cms
, other
, store
, indata
, out
, flags
) > 0)
1110 BIO_printf(bio_err
, "Verification successful\n");
1113 BIO_printf(bio_err
, "Verification failure\n");
1115 ret
= verify_err
+ 32;
1120 STACK_OF(X509
) *signers
;
1121 signers
= CMS_get0_signers(cms
);
1122 if (!save_certs(signerfile
, signers
))
1125 "Error writing signers to %s\n",
1130 sk_X509_free(signers
);
1133 receipt_request_print(bio_err
, cms
);
1136 else if (operation
== SMIME_VERIFY_RECEIPT
)
1138 if (CMS_verify_receipt(rcms
, cms
, other
, store
, flags
) > 0)
1139 BIO_printf(bio_err
, "Verification successful\n");
1142 BIO_printf(bio_err
, "Verification failure\n");
1151 CMS_ContentInfo_print_ctx(out
, cms
, 0, NULL
);
1153 else if (outformat
== FORMAT_SMIME
)
1156 BIO_printf(out
, "To: %s\n", to
);
1158 BIO_printf(out
, "From: %s\n", from
);
1160 BIO_printf(out
, "Subject: %s\n", subject
);
1161 if (operation
== SMIME_RESIGN
)
1162 ret
= SMIME_write_CMS(out
, cms
, indata
, flags
);
1164 ret
= SMIME_write_CMS(out
, cms
, in
, flags
);
1166 else if (outformat
== FORMAT_PEM
)
1167 ret
= PEM_write_bio_CMS_stream(out
, cms
, in
, flags
);
1168 else if (outformat
== FORMAT_ASN1
)
1169 ret
= i2d_CMS_bio_stream(out
,cms
, in
, flags
);
1172 BIO_printf(bio_err
, "Bad output format for CMS file\n");
1184 ERR_print_errors(bio_err
);
1186 app_RAND_write_file(NULL
, bio_err
);
1187 sk_X509_pop_free(encerts
, X509_free
);
1188 sk_X509_pop_free(other
, X509_free
);
1190 X509_VERIFY_PARAM_free(vpm
);
1192 sk_OPENSSL_STRING_free(sksigners
);
1194 sk_OPENSSL_STRING_free(skkeys
);
1196 OPENSSL_free(secret_key
);
1198 OPENSSL_free(secret_keyid
);
1200 OPENSSL_free(pwri_tmp
);
1202 ASN1_OBJECT_free(econtent_type
);
1204 CMS_ReceiptRequest_free(rr
);
1206 sk_OPENSSL_STRING_free(rr_to
);
1208 sk_OPENSSL_STRING_free(rr_from
);
1209 X509_STORE_free(store
);
1214 CMS_ContentInfo_free(cms
);
1215 CMS_ContentInfo_free(rcms
);
1220 if (passin
) OPENSSL_free(passin
);
1224 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
1230 tmp
= BIO_new_file(signerfile
, "w");
1232 for(i
= 0; i
< sk_X509_num(signers
); i
++)
1233 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
1239 /* Minimal callback just to output policy info (if any) */
1241 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
)
1245 error
= X509_STORE_CTX_get_error(ctx
);
1249 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
1250 && ((error
!= X509_V_OK
) || (ok
!= 2)))
1253 policies_print(NULL
, ctx
);
1259 static void gnames_stack_print(BIO
*out
, STACK_OF(GENERAL_NAMES
) *gns
)
1261 STACK_OF(GENERAL_NAME
) *gens
;
1264 for (i
= 0; i
< sk_GENERAL_NAMES_num(gns
); i
++)
1266 gens
= sk_GENERAL_NAMES_value(gns
, i
);
1267 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++)
1269 gen
= sk_GENERAL_NAME_value(gens
, j
);
1271 GENERAL_NAME_print(out
, gen
);
1272 BIO_puts(out
, "\n");
1278 static void receipt_request_print(BIO
*out
, CMS_ContentInfo
*cms
)
1280 STACK_OF(CMS_SignerInfo
) *sis
;
1282 CMS_ReceiptRequest
*rr
;
1284 STACK_OF(GENERAL_NAMES
) *rto
, *rlist
;
1287 sis
= CMS_get0_SignerInfos(cms
);
1288 for (i
= 0; i
< sk_CMS_SignerInfo_num(sis
); i
++)
1290 si
= sk_CMS_SignerInfo_value(sis
, i
);
1291 rv
= CMS_get1_ReceiptRequest(si
, &rr
);
1292 BIO_printf(bio_err
, "Signer %d:\n", i
+ 1);
1294 BIO_puts(bio_err
, " No Receipt Request\n");
1297 BIO_puts(bio_err
, " Receipt Request Parse Error\n");
1298 ERR_print_errors(bio_err
);
1304 CMS_ReceiptRequest_get0_values(rr
, &scid
, &allorfirst
,
1306 BIO_puts(out
, " Signed Content ID:\n");
1307 idlen
= ASN1_STRING_length(scid
);
1308 id
= (char *)ASN1_STRING_data(scid
);
1309 BIO_dump_indent(out
, id
, idlen
, 4);
1310 BIO_puts(out
, " Receipts From");
1313 BIO_puts(out
, " List:\n");
1314 gnames_stack_print(out
, rlist
);
1316 else if (allorfirst
== 1)
1317 BIO_puts(out
, ": First Tier\n");
1318 else if (allorfirst
== 0)
1319 BIO_puts(out
, ": All\n");
1321 BIO_printf(out
, " Unknown (%d)\n", allorfirst
);
1322 BIO_puts(out
, " Receipts To:\n");
1323 gnames_stack_print(out
, rto
);
1326 CMS_ReceiptRequest_free(rr
);
1330 static STACK_OF(GENERAL_NAMES
) *make_names_stack(STACK_OF(OPENSSL_STRING
) *ns
)
1333 STACK_OF(GENERAL_NAMES
) *ret
;
1334 GENERAL_NAMES
*gens
= NULL
;
1335 GENERAL_NAME
*gen
= NULL
;
1336 ret
= sk_GENERAL_NAMES_new_null();
1339 for (i
= 0; i
< sk_OPENSSL_STRING_num(ns
); i
++)
1341 char *str
= sk_OPENSSL_STRING_value(ns
, i
);
1342 gen
= a2i_GENERAL_NAME(NULL
, NULL
, NULL
, GEN_EMAIL
, str
, 0);
1345 gens
= GENERAL_NAMES_new();
1348 if (!sk_GENERAL_NAME_push(gens
, gen
))
1351 if (!sk_GENERAL_NAMES_push(ret
, gens
))
1360 sk_GENERAL_NAMES_pop_free(ret
, GENERAL_NAMES_free
);
1362 GENERAL_NAMES_free(gens
);
1364 GENERAL_NAME_free(gen
);
1369 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
) *rr_to
,
1371 STACK_OF(OPENSSL_STRING
) *rr_from
)
1373 STACK_OF(GENERAL_NAMES
) *rct_to
, *rct_from
;
1374 CMS_ReceiptRequest
*rr
;
1375 rct_to
= make_names_stack(rr_to
);
1380 rct_from
= make_names_stack(rr_from
);
1386 rr
= CMS_ReceiptRequest_create0(NULL
, -1, rr_allorfirst
, rct_from
,