3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2008-2018 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
55 /* CMS utility function */
61 #ifndef OPENSSL_NO_CMS
63 # include <openssl/crypto.h>
64 # include <openssl/pem.h>
65 # include <openssl/err.h>
66 # include <openssl/x509_vfy.h>
67 # include <openssl/x509v3.h>
68 # include <openssl/cms.h>
71 # define PROG cms_main
72 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
);
73 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
);
74 static void receipt_request_print(BIO
*out
, CMS_ContentInfo
*cms
);
75 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
)
76 *rr_to
, int rr_allorfirst
, STACK_OF(OPENSSL_STRING
)
78 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
79 STACK_OF(OPENSSL_STRING
) *param
);
81 # define SMIME_OP 0x10
82 # define SMIME_IP 0x20
83 # define SMIME_SIGNERS 0x40
84 # define SMIME_ENCRYPT (1 | SMIME_OP)
85 # define SMIME_DECRYPT (2 | SMIME_IP)
86 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
87 # define SMIME_VERIFY (4 | SMIME_IP)
88 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
89 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
90 # define SMIME_DATAOUT (7 | SMIME_IP)
91 # define SMIME_DATA_CREATE (8 | SMIME_OP)
92 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
93 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
94 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
95 # define SMIME_COMPRESS (12 | SMIME_OP)
96 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
97 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
98 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
99 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
103 typedef struct cms_key_param_st cms_key_param
;
105 struct cms_key_param_st
{
107 STACK_OF(OPENSSL_STRING
) *param
;
111 int MAIN(int, char **);
113 int MAIN(int argc
, char **argv
)
119 const char *inmode
= "r", *outmode
= "w";
120 char *infile
= NULL
, *outfile
= NULL
, *rctfile
= NULL
;
121 char *signerfile
= NULL
, *recipfile
= NULL
;
122 STACK_OF(OPENSSL_STRING
) *sksigners
= NULL
, *skkeys
= NULL
;
123 char *certfile
= NULL
, *keyfile
= NULL
, *contfile
= NULL
;
124 char *certsoutfile
= NULL
;
125 const EVP_CIPHER
*cipher
= NULL
, *wrap_cipher
= NULL
;
126 CMS_ContentInfo
*cms
= NULL
, *rcms
= NULL
;
127 X509_STORE
*store
= NULL
;
128 X509
*cert
= NULL
, *recip
= NULL
, *signer
= NULL
;
129 EVP_PKEY
*key
= NULL
;
130 STACK_OF(X509
) *encerts
= NULL
, *other
= NULL
;
131 BIO
*in
= NULL
, *out
= NULL
, *indata
= NULL
, *rctin
= NULL
;
133 int flags
= CMS_DETACHED
, noout
= 0, print
= 0;
134 int verify_retcode
= 0;
135 int rr_print
= 0, rr_allorfirst
= -1;
136 STACK_OF(OPENSSL_STRING
) *rr_to
= NULL
, *rr_from
= NULL
;
137 CMS_ReceiptRequest
*rr
= NULL
;
138 char *to
= NULL
, *from
= NULL
, *subject
= NULL
;
139 char *CAfile
= NULL
, *CApath
= NULL
;
140 char *passargin
= NULL
, *passin
= NULL
;
143 const EVP_MD
*sign_md
= NULL
;
144 int informat
= FORMAT_SMIME
, outformat
= FORMAT_SMIME
;
145 int rctformat
= FORMAT_SMIME
, keyform
= FORMAT_PEM
;
147 unsigned char *secret_key
= NULL
, *secret_keyid
= NULL
;
148 unsigned char *pwri_pass
= NULL
, *pwri_tmp
= NULL
;
149 size_t secret_keylen
= 0, secret_keyidlen
= 0;
151 cms_key_param
*key_first
= NULL
, *key_param
= NULL
;
153 ASN1_OBJECT
*econtent_type
= NULL
;
155 X509_VERIFY_PARAM
*vpm
= NULL
;
162 if (bio_err
== NULL
) {
163 if ((bio_err
= BIO_new(BIO_s_file())) != NULL
)
164 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
167 if (!load_config(bio_err
, NULL
))
170 while (!badarg
&& *args
&& *args
[0] == '-') {
171 if (!strcmp(*args
, "-encrypt"))
172 operation
= SMIME_ENCRYPT
;
173 else if (!strcmp(*args
, "-decrypt"))
174 operation
= SMIME_DECRYPT
;
175 else if (!strcmp(*args
, "-sign"))
176 operation
= SMIME_SIGN
;
177 else if (!strcmp(*args
, "-sign_receipt"))
178 operation
= SMIME_SIGN_RECEIPT
;
179 else if (!strcmp(*args
, "-resign"))
180 operation
= SMIME_RESIGN
;
181 else if (!strcmp(*args
, "-verify"))
182 operation
= SMIME_VERIFY
;
183 else if (!strcmp(*args
, "-verify_retcode"))
185 else if (!strcmp(*args
, "-verify_receipt")) {
186 operation
= SMIME_VERIFY_RECEIPT
;
191 } else if (!strcmp(*args
, "-cmsout"))
192 operation
= SMIME_CMSOUT
;
193 else if (!strcmp(*args
, "-data_out"))
194 operation
= SMIME_DATAOUT
;
195 else if (!strcmp(*args
, "-data_create"))
196 operation
= SMIME_DATA_CREATE
;
197 else if (!strcmp(*args
, "-digest_verify"))
198 operation
= SMIME_DIGEST_VERIFY
;
199 else if (!strcmp(*args
, "-digest_create"))
200 operation
= SMIME_DIGEST_CREATE
;
201 else if (!strcmp(*args
, "-compress"))
202 operation
= SMIME_COMPRESS
;
203 else if (!strcmp(*args
, "-uncompress"))
204 operation
= SMIME_UNCOMPRESS
;
205 else if (!strcmp(*args
, "-EncryptedData_decrypt"))
206 operation
= SMIME_ENCRYPTED_DECRYPT
;
207 else if (!strcmp(*args
, "-EncryptedData_encrypt"))
208 operation
= SMIME_ENCRYPTED_ENCRYPT
;
209 # ifndef OPENSSL_NO_DES
210 else if (!strcmp(*args
, "-des3"))
211 cipher
= EVP_des_ede3_cbc();
212 else if (!strcmp(*args
, "-des"))
213 cipher
= EVP_des_cbc();
214 else if (!strcmp(*args
, "-des3-wrap"))
215 wrap_cipher
= EVP_des_ede3_wrap();
217 # ifndef OPENSSL_NO_SEED
218 else if (!strcmp(*args
, "-seed"))
219 cipher
= EVP_seed_cbc();
221 # ifndef OPENSSL_NO_RC2
222 else if (!strcmp(*args
, "-rc2-40"))
223 cipher
= EVP_rc2_40_cbc();
224 else if (!strcmp(*args
, "-rc2-128"))
225 cipher
= EVP_rc2_cbc();
226 else if (!strcmp(*args
, "-rc2-64"))
227 cipher
= EVP_rc2_64_cbc();
229 # ifndef OPENSSL_NO_AES
230 else if (!strcmp(*args
, "-aes128"))
231 cipher
= EVP_aes_128_cbc();
232 else if (!strcmp(*args
, "-aes192"))
233 cipher
= EVP_aes_192_cbc();
234 else if (!strcmp(*args
, "-aes256"))
235 cipher
= EVP_aes_256_cbc();
236 else if (!strcmp(*args
, "-aes128-wrap"))
237 wrap_cipher
= EVP_aes_128_wrap();
238 else if (!strcmp(*args
, "-aes192-wrap"))
239 wrap_cipher
= EVP_aes_192_wrap();
240 else if (!strcmp(*args
, "-aes256-wrap"))
241 wrap_cipher
= EVP_aes_256_wrap();
243 # ifndef OPENSSL_NO_CAMELLIA
244 else if (!strcmp(*args
, "-camellia128"))
245 cipher
= EVP_camellia_128_cbc();
246 else if (!strcmp(*args
, "-camellia192"))
247 cipher
= EVP_camellia_192_cbc();
248 else if (!strcmp(*args
, "-camellia256"))
249 cipher
= EVP_camellia_256_cbc();
251 else if (!strcmp(*args
, "-debug_decrypt"))
252 flags
|= CMS_DEBUG_DECRYPT
;
253 else if (!strcmp(*args
, "-text"))
255 else if (!strcmp(*args
, "-nointern"))
256 flags
|= CMS_NOINTERN
;
257 else if (!strcmp(*args
, "-noverify")
258 || !strcmp(*args
, "-no_signer_cert_verify"))
259 flags
|= CMS_NO_SIGNER_CERT_VERIFY
;
260 else if (!strcmp(*args
, "-nocerts"))
261 flags
|= CMS_NOCERTS
;
262 else if (!strcmp(*args
, "-noattr"))
264 else if (!strcmp(*args
, "-nodetach"))
265 flags
&= ~CMS_DETACHED
;
266 else if (!strcmp(*args
, "-nosmimecap"))
267 flags
|= CMS_NOSMIMECAP
;
268 else if (!strcmp(*args
, "-binary"))
270 else if (!strcmp(*args
, "-keyid"))
271 flags
|= CMS_USE_KEYID
;
272 else if (!strcmp(*args
, "-nosigs"))
274 else if (!strcmp(*args
, "-no_content_verify"))
275 flags
|= CMS_NO_CONTENT_VERIFY
;
276 else if (!strcmp(*args
, "-no_attr_verify"))
277 flags
|= CMS_NO_ATTR_VERIFY
;
278 else if (!strcmp(*args
, "-stream"))
280 else if (!strcmp(*args
, "-indef"))
282 else if (!strcmp(*args
, "-noindef"))
283 flags
&= ~CMS_STREAM
;
284 else if (!strcmp(*args
, "-nooldmime"))
285 flags
|= CMS_NOOLDMIMETYPE
;
286 else if (!strcmp(*args
, "-crlfeol"))
287 flags
|= CMS_CRLFEOL
;
288 else if (!strcmp(*args
, "-noout"))
290 else if (!strcmp(*args
, "-receipt_request_print"))
292 else if (!strcmp(*args
, "-receipt_request_all"))
294 else if (!strcmp(*args
, "-receipt_request_first"))
296 else if (!strcmp(*args
, "-receipt_request_from")) {
301 rr_from
= sk_OPENSSL_STRING_new_null();
302 sk_OPENSSL_STRING_push(rr_from
, *args
);
303 } else if (!strcmp(*args
, "-receipt_request_to")) {
308 rr_to
= sk_OPENSSL_STRING_new_null();
309 sk_OPENSSL_STRING_push(rr_to
, *args
);
310 } else if (!strcmp(*args
, "-print")) {
313 } else if (!strcmp(*args
, "-secretkey")) {
318 secret_key
= string_to_hex(*args
, <mp
);
320 BIO_printf(bio_err
, "Invalid key %s\n", *args
);
323 secret_keylen
= (size_t)ltmp
;
324 } else if (!strcmp(*args
, "-secretkeyid")) {
329 secret_keyid
= string_to_hex(*args
, <mp
);
331 BIO_printf(bio_err
, "Invalid id %s\n", *args
);
334 secret_keyidlen
= (size_t)ltmp
;
335 } else if (!strcmp(*args
, "-pwri_password")) {
339 pwri_pass
= (unsigned char *)*args
;
340 } else if (!strcmp(*args
, "-econtent_type")) {
344 econtent_type
= OBJ_txt2obj(*args
, 0);
345 if (!econtent_type
) {
346 BIO_printf(bio_err
, "Invalid OID %s\n", *args
);
349 } else if (!strcmp(*args
, "-rand")) {
356 # ifndef OPENSSL_NO_ENGINE
357 else if (!strcmp(*args
, "-engine")) {
363 else if (!strcmp(*args
, "-passin")) {
367 } else if (!strcmp(*args
, "-to")) {
371 } else if (!strcmp(*args
, "-from")) {
375 } else if (!strcmp(*args
, "-subject")) {
379 } else if (!strcmp(*args
, "-signer")) {
382 /* If previous -signer argument add signer to list */
386 sksigners
= sk_OPENSSL_STRING_new_null();
387 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
389 keyfile
= signerfile
;
391 skkeys
= sk_OPENSSL_STRING_new_null();
392 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
395 signerfile
= *++args
;
396 } else if (!strcmp(*args
, "-recip")) {
399 if (operation
== SMIME_ENCRYPT
) {
401 encerts
= sk_X509_new_null();
402 cert
= load_cert(bio_err
, *++args
, FORMAT_PEM
,
403 NULL
, e
, "recipient certificate file");
406 sk_X509_push(encerts
, cert
);
410 } else if (!strcmp(*args
, "-certsout")) {
413 certsoutfile
= *++args
;
414 } else if (!strcmp(*args
, "-md")) {
417 sign_md
= EVP_get_digestbyname(*++args
);
418 if (sign_md
== NULL
) {
419 BIO_printf(bio_err
, "Unknown digest %s\n", *args
);
422 } else if (!strcmp(*args
, "-inkey")) {
425 /* If previous -inkey arument add signer to list */
428 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
432 sksigners
= sk_OPENSSL_STRING_new_null();
433 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
436 skkeys
= sk_OPENSSL_STRING_new_null();
437 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
440 } else if (!strcmp(*args
, "-keyform")) {
443 keyform
= str2fmt(*++args
);
444 } else if (!strcmp(*args
, "-keyopt")) {
448 if (operation
== SMIME_ENCRYPT
) {
450 keyidx
+= sk_X509_num(encerts
);
452 if (keyfile
|| signerfile
)
455 keyidx
+= sk_OPENSSL_STRING_num(skkeys
);
458 BIO_printf(bio_err
, "No key specified\n");
461 if (key_param
== NULL
|| key_param
->idx
!= keyidx
) {
462 cms_key_param
*nparam
;
463 nparam
= OPENSSL_malloc(sizeof(cms_key_param
));
465 BIO_printf(bio_err
, "Out of memory\n");
468 nparam
->idx
= keyidx
;
469 nparam
->param
= sk_OPENSSL_STRING_new_null();
471 if (key_first
== NULL
)
474 key_param
->next
= nparam
;
477 sk_OPENSSL_STRING_push(key_param
->param
, *++args
);
478 } else if (!strcmp(*args
, "-rctform")) {
481 rctformat
= str2fmt(*++args
);
482 } else if (!strcmp(*args
, "-certfile")) {
486 } else if (!strcmp(*args
, "-CAfile")) {
490 } else if (!strcmp(*args
, "-CApath")) {
494 } else if (!strcmp(*args
, "-in")) {
498 } else if (!strcmp(*args
, "-inform")) {
501 informat
= str2fmt(*++args
);
502 } else if (!strcmp(*args
, "-outform")) {
505 outformat
= str2fmt(*++args
);
506 } else if (!strcmp(*args
, "-out")) {
510 } else if (!strcmp(*args
, "-content")) {
514 } else if (args_verify(&args
, NULL
, &badarg
, bio_err
, &vpm
))
516 else if ((cipher
= EVP_get_cipherbyname(*args
+ 1)) == NULL
)
521 if (((rr_allorfirst
!= -1) || rr_from
) && !rr_to
) {
522 BIO_puts(bio_err
, "No Signed Receipts Recipients\n");
526 if (!(operation
& SMIME_SIGNERS
) && (rr_to
|| rr_from
)) {
527 BIO_puts(bio_err
, "Signed receipts only allowed with -sign\n");
530 if (!(operation
& SMIME_SIGNERS
) && (skkeys
|| sksigners
)) {
531 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
535 if (operation
& SMIME_SIGNERS
) {
536 if (keyfile
&& !signerfile
) {
537 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
540 /* Check to see if any final signer needs to be appended */
543 sksigners
= sk_OPENSSL_STRING_new_null();
544 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
546 skkeys
= sk_OPENSSL_STRING_new_null();
548 keyfile
= signerfile
;
549 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
552 BIO_printf(bio_err
, "No signer certificate specified\n");
560 else if (operation
== SMIME_DECRYPT
) {
561 if (!recipfile
&& !keyfile
&& !secret_key
&& !pwri_pass
) {
563 "No recipient certificate or key specified\n");
566 } else if (operation
== SMIME_ENCRYPT
) {
567 if (!*args
&& !secret_key
&& !pwri_pass
&& !encerts
) {
568 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
572 } else if (!operation
)
577 BIO_printf(bio_err
, "Usage cms [options] cert.pem ...\n");
578 BIO_printf(bio_err
, "where options are\n");
579 BIO_printf(bio_err
, "-encrypt encrypt message\n");
580 BIO_printf(bio_err
, "-decrypt decrypt encrypted message\n");
581 BIO_printf(bio_err
, "-sign sign message\n");
582 BIO_printf(bio_err
, "-verify verify signed message\n");
583 BIO_printf(bio_err
, "-cmsout output CMS structure\n");
584 # ifndef OPENSSL_NO_DES
585 BIO_printf(bio_err
, "-des3 encrypt with triple DES\n");
586 BIO_printf(bio_err
, "-des encrypt with DES\n");
588 # ifndef OPENSSL_NO_SEED
589 BIO_printf(bio_err
, "-seed encrypt with SEED\n");
591 # ifndef OPENSSL_NO_RC2
592 BIO_printf(bio_err
, "-rc2-40 encrypt with RC2-40 (default)\n");
593 BIO_printf(bio_err
, "-rc2-64 encrypt with RC2-64\n");
594 BIO_printf(bio_err
, "-rc2-128 encrypt with RC2-128\n");
596 # ifndef OPENSSL_NO_AES
597 BIO_printf(bio_err
, "-aes128, -aes192, -aes256\n");
599 " encrypt PEM output with cbc aes\n");
601 # ifndef OPENSSL_NO_CAMELLIA
602 BIO_printf(bio_err
, "-camellia128, -camellia192, -camellia256\n");
604 " encrypt PEM output with cbc camellia\n");
607 "-nointern don't search certificates in message for signer\n");
609 "-nosigs don't verify message signature\n");
611 "-noverify don't verify signers certificate\n");
613 "-nocerts don't include signers certificate when signing\n");
614 BIO_printf(bio_err
, "-nodetach use opaque signing\n");
616 "-noattr don't include any signed attributes\n");
618 "-binary don't translate message to text\n");
619 BIO_printf(bio_err
, "-certfile file other certificates file\n");
620 BIO_printf(bio_err
, "-certsout file certificate output file\n");
621 BIO_printf(bio_err
, "-signer file signer certificate file\n");
623 "-recip file recipient certificate file for decryption\n");
624 BIO_printf(bio_err
, "-keyid use subject key identifier\n");
625 BIO_printf(bio_err
, "-in file input file\n");
627 "-inform arg input format SMIME (default), PEM or DER\n");
629 "-inkey file input private key (if not signer or recipient)\n");
631 "-keyform arg input private key format (PEM or ENGINE)\n");
632 BIO_printf(bio_err
, "-keyopt nm:v set public key parameters\n");
633 BIO_printf(bio_err
, "-out file output file\n");
635 "-outform arg output format SMIME (default), PEM or DER\n");
637 "-content file supply or override content for detached signature\n");
638 BIO_printf(bio_err
, "-to addr to address\n");
639 BIO_printf(bio_err
, "-from ad from address\n");
640 BIO_printf(bio_err
, "-subject s subject\n");
642 "-text include or delete text MIME headers\n");
644 "-CApath dir trusted certificates directory\n");
645 BIO_printf(bio_err
, "-CAfile file trusted certificates file\n");
647 "-no_alt_chains only ever use the first certificate chain found\n");
649 "-crl_check check revocation status of signer's certificate using CRLs\n");
651 "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
652 # ifndef OPENSSL_NO_ENGINE
654 "-engine e use engine e, possibly a hardware device.\n");
656 BIO_printf(bio_err
, "-passin arg input file pass phrase source\n");
657 BIO_printf(bio_err
, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR
,
658 LIST_SEPARATOR_CHAR
);
660 " load the file (or the files in the directory) into\n");
661 BIO_printf(bio_err
, " the random number generator\n");
663 "cert.pem recipient certificate(s) for encryption\n");
666 e
= setup_engine(bio_err
, engine
, 0);
668 if (!app_passwd(bio_err
, passargin
, NULL
, &passin
, NULL
)) {
669 BIO_printf(bio_err
, "Error getting password\n");
674 app_RAND_load_file(NULL
, bio_err
, (inrand
!= NULL
));
676 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
677 app_RAND_load_files(inrand
));
682 if (!(operation
& SMIME_SIGNERS
))
683 flags
&= ~CMS_DETACHED
;
685 if (operation
& SMIME_OP
) {
686 if (outformat
== FORMAT_ASN1
)
689 if (flags
& CMS_BINARY
)
693 if (operation
& SMIME_IP
) {
694 if (informat
== FORMAT_ASN1
)
697 if (flags
& CMS_BINARY
)
701 if (operation
== SMIME_ENCRYPT
) {
703 # ifndef OPENSSL_NO_DES
704 cipher
= EVP_des_ede3_cbc();
706 BIO_printf(bio_err
, "No cipher selected\n");
711 if (secret_key
&& !secret_keyid
) {
712 BIO_printf(bio_err
, "No secret key id\n");
716 if (*args
&& !encerts
)
717 encerts
= sk_X509_new_null();
719 if (!(cert
= load_cert(bio_err
, *args
, FORMAT_PEM
,
720 NULL
, e
, "recipient certificate file")))
722 sk_X509_push(encerts
, cert
);
729 if (!(other
= load_certs(bio_err
, certfile
, FORMAT_PEM
, NULL
,
730 e
, "certificate file"))) {
731 ERR_print_errors(bio_err
);
736 if (recipfile
&& (operation
== SMIME_DECRYPT
)) {
737 if (!(recip
= load_cert(bio_err
, recipfile
, FORMAT_PEM
, NULL
,
738 e
, "recipient certificate file"))) {
739 ERR_print_errors(bio_err
);
744 if (operation
== SMIME_SIGN_RECEIPT
) {
745 if (!(signer
= load_cert(bio_err
, signerfile
, FORMAT_PEM
, NULL
,
746 e
, "receipt signer certificate file"))) {
747 ERR_print_errors(bio_err
);
752 if (operation
== SMIME_DECRYPT
) {
755 } else if ((operation
== SMIME_SIGN
) || (operation
== SMIME_SIGN_RECEIPT
)) {
757 keyfile
= signerfile
;
762 key
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
769 if (!(in
= BIO_new_file(infile
, inmode
))) {
770 BIO_printf(bio_err
, "Can't open input file %s\n", infile
);
774 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
776 if (operation
& SMIME_IP
) {
777 if (informat
== FORMAT_SMIME
)
778 cms
= SMIME_read_CMS(in
, &indata
);
779 else if (informat
== FORMAT_PEM
)
780 cms
= PEM_read_bio_CMS(in
, NULL
, NULL
, NULL
);
781 else if (informat
== FORMAT_ASN1
)
782 cms
= d2i_CMS_bio(in
, NULL
);
784 BIO_printf(bio_err
, "Bad input format for CMS file\n");
789 BIO_printf(bio_err
, "Error reading S/MIME message\n");
794 if (!(indata
= BIO_new_file(contfile
, "rb"))) {
795 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
800 STACK_OF(X509
) *allcerts
;
801 allcerts
= CMS_get1_certs(cms
);
802 if (!save_certs(certsoutfile
, allcerts
)) {
804 "Error writing certs to %s\n", certsoutfile
);
808 sk_X509_pop_free(allcerts
, X509_free
);
813 char *rctmode
= (rctformat
== FORMAT_ASN1
) ? "rb" : "r";
814 if (!(rctin
= BIO_new_file(rctfile
, rctmode
))) {
815 BIO_printf(bio_err
, "Can't open receipt file %s\n", rctfile
);
819 if (rctformat
== FORMAT_SMIME
)
820 rcms
= SMIME_read_CMS(rctin
, NULL
);
821 else if (rctformat
== FORMAT_PEM
)
822 rcms
= PEM_read_bio_CMS(rctin
, NULL
, NULL
, NULL
);
823 else if (rctformat
== FORMAT_ASN1
)
824 rcms
= d2i_CMS_bio(rctin
, NULL
);
826 BIO_printf(bio_err
, "Bad input format for receipt\n");
831 BIO_printf(bio_err
, "Error reading receipt\n");
837 if (!(out
= BIO_new_file(outfile
, outmode
))) {
838 BIO_printf(bio_err
, "Can't open output file %s\n", outfile
);
842 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
843 # ifdef OPENSSL_SYS_VMS
845 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
846 out
= BIO_push(tmpbio
, out
);
851 if ((operation
== SMIME_VERIFY
) || (operation
== SMIME_VERIFY_RECEIPT
)) {
852 if (!(store
= setup_verify(bio_err
, CAfile
, CApath
)))
854 X509_STORE_set_verify_cb(store
, cms_cb
);
856 X509_STORE_set1_param(store
, vpm
);
861 if (operation
== SMIME_DATA_CREATE
) {
862 cms
= CMS_data_create(in
, flags
);
863 } else if (operation
== SMIME_DIGEST_CREATE
) {
864 cms
= CMS_digest_create(in
, sign_md
, flags
);
865 } else if (operation
== SMIME_COMPRESS
) {
866 cms
= CMS_compress(in
, -1, flags
);
867 } else if (operation
== SMIME_ENCRYPT
) {
869 flags
|= CMS_PARTIAL
;
870 cms
= CMS_encrypt(NULL
, in
, cipher
, flags
);
873 for (i
= 0; i
< sk_X509_num(encerts
); i
++) {
874 CMS_RecipientInfo
*ri
;
875 cms_key_param
*kparam
;
877 X509
*x
= sk_X509_value(encerts
, i
);
878 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
879 if (kparam
->idx
== i
) {
880 tflags
|= CMS_KEY_PARAM
;
884 ri
= CMS_add1_recipient_cert(cms
, x
, tflags
);
889 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
890 if (!cms_set_pkey_param(pctx
, kparam
->param
))
893 if (CMS_RecipientInfo_type(ri
) == CMS_RECIPINFO_AGREE
895 EVP_CIPHER_CTX
*wctx
;
896 wctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
897 EVP_EncryptInit_ex(wctx
, wrap_cipher
, NULL
, NULL
, NULL
);
902 if (!CMS_add0_recipient_key(cms
, NID_undef
,
903 secret_key
, secret_keylen
,
904 secret_keyid
, secret_keyidlen
,
907 /* NULL these because call absorbs them */
912 pwri_tmp
= (unsigned char *)BUF_strdup((char *)pwri_pass
);
915 if (!CMS_add0_recipient_password(cms
,
916 -1, NID_undef
, NID_undef
,
921 if (!(flags
& CMS_STREAM
)) {
922 if (!CMS_final(cms
, in
, NULL
, flags
))
925 } else if (operation
== SMIME_ENCRYPTED_ENCRYPT
) {
926 cms
= CMS_EncryptedData_encrypt(in
, cipher
,
927 secret_key
, secret_keylen
, flags
);
929 } else if (operation
== SMIME_SIGN_RECEIPT
) {
930 CMS_ContentInfo
*srcms
= NULL
;
931 STACK_OF(CMS_SignerInfo
) *sis
;
933 sis
= CMS_get0_SignerInfos(cms
);
936 si
= sk_CMS_SignerInfo_value(sis
, 0);
937 srcms
= CMS_sign_receipt(si
, signer
, key
, other
, flags
);
940 CMS_ContentInfo_free(cms
);
942 } else if (operation
& SMIME_SIGNERS
) {
945 * If detached data content we enable streaming if S/MIME output
948 if (operation
== SMIME_SIGN
) {
950 if (flags
& CMS_DETACHED
) {
951 if (outformat
== FORMAT_SMIME
)
954 flags
|= CMS_PARTIAL
;
955 cms
= CMS_sign(NULL
, NULL
, other
, in
, flags
);
959 CMS_set1_eContentType(cms
, econtent_type
);
962 rr
= make_receipt_request(rr_to
, rr_allorfirst
, rr_from
);
965 "Signed Receipt Request Creation Error\n");
970 flags
|= CMS_REUSE_DIGEST
;
971 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++) {
973 cms_key_param
*kparam
;
975 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
976 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
978 signer
= load_cert(bio_err
, signerfile
, FORMAT_PEM
, NULL
,
979 e
, "signer certificate");
984 key
= load_key(bio_err
, keyfile
, keyform
, 0, passin
, e
,
990 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
991 if (kparam
->idx
== i
) {
992 tflags
|= CMS_KEY_PARAM
;
996 si
= CMS_add1_signer(cms
, signer
, key
, sign_md
, tflags
);
1001 pctx
= CMS_SignerInfo_get0_pkey_ctx(si
);
1002 if (!cms_set_pkey_param(pctx
, kparam
->param
))
1005 if (rr
&& !CMS_add1_ReceiptRequest(si
, rr
))
1012 /* If not streaming or resigning finalize structure */
1013 if ((operation
== SMIME_SIGN
) && !(flags
& CMS_STREAM
)) {
1014 if (!CMS_final(cms
, in
, NULL
, flags
))
1020 BIO_printf(bio_err
, "Error creating CMS structure\n");
1025 if (operation
== SMIME_DECRYPT
) {
1026 if (flags
& CMS_DEBUG_DECRYPT
)
1027 CMS_decrypt(cms
, NULL
, NULL
, NULL
, NULL
, flags
);
1030 if (!CMS_decrypt_set1_key(cms
,
1031 secret_key
, secret_keylen
,
1032 secret_keyid
, secret_keyidlen
)) {
1033 BIO_puts(bio_err
, "Error decrypting CMS using secret key\n");
1039 if (!CMS_decrypt_set1_pkey(cms
, key
, recip
)) {
1040 BIO_puts(bio_err
, "Error decrypting CMS using private key\n");
1046 if (!CMS_decrypt_set1_password(cms
, pwri_pass
, -1)) {
1047 BIO_puts(bio_err
, "Error decrypting CMS using password\n");
1052 if (!CMS_decrypt(cms
, NULL
, NULL
, indata
, out
, flags
)) {
1053 BIO_printf(bio_err
, "Error decrypting CMS structure\n");
1056 } else if (operation
== SMIME_DATAOUT
) {
1057 if (!CMS_data(cms
, out
, flags
))
1059 } else if (operation
== SMIME_UNCOMPRESS
) {
1060 if (!CMS_uncompress(cms
, indata
, out
, flags
))
1062 } else if (operation
== SMIME_DIGEST_VERIFY
) {
1063 if (CMS_digest_verify(cms
, indata
, out
, flags
) > 0)
1064 BIO_printf(bio_err
, "Verification successful\n");
1066 BIO_printf(bio_err
, "Verification failure\n");
1069 } else if (operation
== SMIME_ENCRYPTED_DECRYPT
) {
1070 if (!CMS_EncryptedData_decrypt(cms
, secret_key
, secret_keylen
,
1071 indata
, out
, flags
))
1073 } else if (operation
== SMIME_VERIFY
) {
1074 if (CMS_verify(cms
, other
, store
, indata
, out
, flags
) > 0)
1075 BIO_printf(bio_err
, "Verification successful\n");
1077 BIO_printf(bio_err
, "Verification failure\n");
1079 ret
= verify_err
+ 32;
1083 STACK_OF(X509
) *signers
;
1084 signers
= CMS_get0_signers(cms
);
1085 if (!save_certs(signerfile
, signers
)) {
1087 "Error writing signers to %s\n", signerfile
);
1091 sk_X509_free(signers
);
1094 receipt_request_print(bio_err
, cms
);
1096 } else if (operation
== SMIME_VERIFY_RECEIPT
) {
1097 if (CMS_verify_receipt(rcms
, cms
, other
, store
, flags
) > 0)
1098 BIO_printf(bio_err
, "Verification successful\n");
1100 BIO_printf(bio_err
, "Verification failure\n");
1106 CMS_ContentInfo_print_ctx(out
, cms
, 0, NULL
);
1107 } else if (outformat
== FORMAT_SMIME
) {
1109 BIO_printf(out
, "To: %s\n", to
);
1111 BIO_printf(out
, "From: %s\n", from
);
1113 BIO_printf(out
, "Subject: %s\n", subject
);
1114 if (operation
== SMIME_RESIGN
)
1115 ret
= SMIME_write_CMS(out
, cms
, indata
, flags
);
1117 ret
= SMIME_write_CMS(out
, cms
, in
, flags
);
1118 } else if (outformat
== FORMAT_PEM
)
1119 ret
= PEM_write_bio_CMS_stream(out
, cms
, in
, flags
);
1120 else if (outformat
== FORMAT_ASN1
)
1121 ret
= i2d_CMS_bio_stream(out
, cms
, in
, flags
);
1123 BIO_printf(bio_err
, "Bad output format for CMS file\n");
1134 ERR_print_errors(bio_err
);
1136 app_RAND_write_file(NULL
, bio_err
);
1137 sk_X509_pop_free(encerts
, X509_free
);
1138 sk_X509_pop_free(other
, X509_free
);
1140 X509_VERIFY_PARAM_free(vpm
);
1142 sk_OPENSSL_STRING_free(sksigners
);
1144 sk_OPENSSL_STRING_free(skkeys
);
1146 OPENSSL_free(secret_key
);
1148 OPENSSL_free(secret_keyid
);
1150 OPENSSL_free(pwri_tmp
);
1152 ASN1_OBJECT_free(econtent_type
);
1154 CMS_ReceiptRequest_free(rr
);
1156 sk_OPENSSL_STRING_free(rr_to
);
1158 sk_OPENSSL_STRING_free(rr_from
);
1159 for (key_param
= key_first
; key_param
;) {
1160 cms_key_param
*tparam
;
1161 sk_OPENSSL_STRING_free(key_param
->param
);
1162 tparam
= key_param
->next
;
1163 OPENSSL_free(key_param
);
1166 X509_STORE_free(store
);
1171 CMS_ContentInfo_free(cms
);
1172 CMS_ContentInfo_free(rcms
);
1179 OPENSSL_free(passin
);
1183 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
1189 tmp
= BIO_new_file(signerfile
, "w");
1192 for (i
= 0; i
< sk_X509_num(signers
); i
++)
1193 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
1198 /* Minimal callback just to output policy info (if any) */
1200 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
)
1204 error
= X509_STORE_CTX_get_error(ctx
);
1208 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
1209 && ((error
!= X509_V_OK
) || (ok
!= 2)))
1212 policies_print(NULL
, ctx
);
1218 static void gnames_stack_print(BIO
*out
, STACK_OF(GENERAL_NAMES
) *gns
)
1220 STACK_OF(GENERAL_NAME
) *gens
;
1223 for (i
= 0; i
< sk_GENERAL_NAMES_num(gns
); i
++) {
1224 gens
= sk_GENERAL_NAMES_value(gns
, i
);
1225 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++) {
1226 gen
= sk_GENERAL_NAME_value(gens
, j
);
1228 GENERAL_NAME_print(out
, gen
);
1229 BIO_puts(out
, "\n");
1235 static void receipt_request_print(BIO
*out
, CMS_ContentInfo
*cms
)
1237 STACK_OF(CMS_SignerInfo
) *sis
;
1239 CMS_ReceiptRequest
*rr
;
1241 STACK_OF(GENERAL_NAMES
) *rto
, *rlist
;
1244 sis
= CMS_get0_SignerInfos(cms
);
1245 for (i
= 0; i
< sk_CMS_SignerInfo_num(sis
); i
++) {
1246 si
= sk_CMS_SignerInfo_value(sis
, i
);
1247 rv
= CMS_get1_ReceiptRequest(si
, &rr
);
1248 BIO_printf(bio_err
, "Signer %d:\n", i
+ 1);
1250 BIO_puts(bio_err
, " No Receipt Request\n");
1252 BIO_puts(bio_err
, " Receipt Request Parse Error\n");
1253 ERR_print_errors(bio_err
);
1257 CMS_ReceiptRequest_get0_values(rr
, &scid
, &allorfirst
,
1259 BIO_puts(out
, " Signed Content ID:\n");
1260 idlen
= ASN1_STRING_length(scid
);
1261 id
= (char *)ASN1_STRING_data(scid
);
1262 BIO_dump_indent(out
, id
, idlen
, 4);
1263 BIO_puts(out
, " Receipts From");
1265 BIO_puts(out
, " List:\n");
1266 gnames_stack_print(out
, rlist
);
1267 } else if (allorfirst
== 1)
1268 BIO_puts(out
, ": First Tier\n");
1269 else if (allorfirst
== 0)
1270 BIO_puts(out
, ": All\n");
1272 BIO_printf(out
, " Unknown (%d)\n", allorfirst
);
1273 BIO_puts(out
, " Receipts To:\n");
1274 gnames_stack_print(out
, rto
);
1277 CMS_ReceiptRequest_free(rr
);
1281 static STACK_OF(GENERAL_NAMES
) *make_names_stack(STACK_OF(OPENSSL_STRING
) *ns
)
1284 STACK_OF(GENERAL_NAMES
) *ret
;
1285 GENERAL_NAMES
*gens
= NULL
;
1286 GENERAL_NAME
*gen
= NULL
;
1287 ret
= sk_GENERAL_NAMES_new_null();
1290 for (i
= 0; i
< sk_OPENSSL_STRING_num(ns
); i
++) {
1291 char *str
= sk_OPENSSL_STRING_value(ns
, i
);
1292 gen
= a2i_GENERAL_NAME(NULL
, NULL
, NULL
, GEN_EMAIL
, str
, 0);
1295 gens
= GENERAL_NAMES_new();
1298 if (!sk_GENERAL_NAME_push(gens
, gen
))
1301 if (!sk_GENERAL_NAMES_push(ret
, gens
))
1310 sk_GENERAL_NAMES_pop_free(ret
, GENERAL_NAMES_free
);
1312 GENERAL_NAMES_free(gens
);
1314 GENERAL_NAME_free(gen
);
1318 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
)
1319 *rr_to
, int rr_allorfirst
, STACK_OF(OPENSSL_STRING
)
1322 STACK_OF(GENERAL_NAMES
) *rct_to
, *rct_from
;
1323 CMS_ReceiptRequest
*rr
;
1324 rct_to
= make_names_stack(rr_to
);
1328 rct_from
= make_names_stack(rr_from
);
1333 rr
= CMS_ReceiptRequest_create0(NULL
, -1, rr_allorfirst
, rct_from
,
1340 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
1341 STACK_OF(OPENSSL_STRING
) *param
)
1345 if (sk_OPENSSL_STRING_num(param
) <= 0)
1347 for (i
= 0; i
< sk_OPENSSL_STRING_num(param
); i
++) {
1348 keyopt
= sk_OPENSSL_STRING_value(param
, i
);
1349 if (pkey_ctrl_string(pctx
, keyopt
) <= 0) {
1350 BIO_printf(bio_err
, "parameter error \"%s\"\n", keyopt
);
1351 ERR_print_errors(bio_err
);