2 * Copyright 2008-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* CMS utility function */
16 #ifndef OPENSSL_NO_CMS
18 # include <openssl/crypto.h>
19 # include <openssl/pem.h>
20 # include <openssl/err.h>
21 # include <openssl/x509_vfy.h>
22 # include <openssl/x509v3.h>
23 # include <openssl/cms.h>
25 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
);
26 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
);
27 static void receipt_request_print(CMS_ContentInfo
*cms
);
28 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
)
29 *rr_to
, int rr_allorfirst
, STACK_OF(OPENSSL_STRING
)
31 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
32 STACK_OF(OPENSSL_STRING
) *param
);
34 # define SMIME_OP 0x10
35 # define SMIME_IP 0x20
36 # define SMIME_SIGNERS 0x40
37 # define SMIME_ENCRYPT (1 | SMIME_OP)
38 # define SMIME_DECRYPT (2 | SMIME_IP)
39 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
40 # define SMIME_VERIFY (4 | SMIME_IP)
41 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
42 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
43 # define SMIME_DATAOUT (7 | SMIME_IP)
44 # define SMIME_DATA_CREATE (8 | SMIME_OP)
45 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
46 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
47 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
48 # define SMIME_COMPRESS (12 | SMIME_OP)
49 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
50 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
51 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
52 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
54 static int verify_err
= 0;
56 typedef struct cms_key_param_st cms_key_param
;
58 struct cms_key_param_st
{
60 STACK_OF(OPENSSL_STRING
) *param
;
64 typedef enum OPTION_choice
{
65 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
66 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_ENCRYPT
,
67 OPT_DECRYPT
, OPT_SIGN
, OPT_SIGN_RECEIPT
, OPT_RESIGN
,
68 OPT_VERIFY
, OPT_VERIFY_RETCODE
, OPT_VERIFY_RECEIPT
,
69 OPT_CMSOUT
, OPT_DATA_OUT
, OPT_DATA_CREATE
, OPT_DIGEST_VERIFY
,
70 OPT_DIGEST_CREATE
, OPT_COMPRESS
, OPT_UNCOMPRESS
,
71 OPT_ED_DECRYPT
, OPT_ED_ENCRYPT
, OPT_DEBUG_DECRYPT
, OPT_TEXT
,
72 OPT_ASCIICRLF
, OPT_NOINTERN
, OPT_NOVERIFY
, OPT_NOCERTS
,
73 OPT_NOATTR
, OPT_NODETACH
, OPT_NOSMIMECAP
, OPT_BINARY
, OPT_KEYID
,
74 OPT_NOSIGS
, OPT_NO_CONTENT_VERIFY
, OPT_NO_ATTR_VERIFY
, OPT_INDEF
,
75 OPT_NOINDEF
, OPT_CRLFEOL
, OPT_NOOUT
, OPT_RR_PRINT
,
76 OPT_RR_ALL
, OPT_RR_FIRST
, OPT_RCTFORM
, OPT_CERTFILE
, OPT_CAFILE
,
77 OPT_CAPATH
, OPT_NOCAPATH
, OPT_NOCAFILE
,OPT_CONTENT
, OPT_PRINT
,
78 OPT_SECRETKEY
, OPT_SECRETKEYID
, OPT_PWRI_PASSWORD
, OPT_ECONTENT_TYPE
,
79 OPT_RAND
, OPT_PASSIN
, OPT_TO
, OPT_FROM
, OPT_SUBJECT
, OPT_SIGNER
, OPT_RECIP
,
80 OPT_CERTSOUT
, OPT_MD
, OPT_INKEY
, OPT_KEYFORM
, OPT_KEYOPT
, OPT_RR_FROM
,
81 OPT_RR_TO
, OPT_AES128_WRAP
, OPT_AES192_WRAP
, OPT_AES256_WRAP
,
82 OPT_3DES_WRAP
, OPT_ENGINE
,
87 const OPTIONS cms_options
[] = {
88 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] cert.pem...\n"},
89 {OPT_HELP_STR
, 1, '-',
90 " cert.pem... recipient certs for encryption\n"},
91 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
92 {"help", OPT_HELP
, '-', "Display this summary"},
93 {"inform", OPT_INFORM
, 'c', "Input format SMIME (default), PEM or DER"},
94 {"outform", OPT_OUTFORM
, 'c',
95 "Output format SMIME (default), PEM or DER"},
96 {"in", OPT_IN
, '<', "Input file"},
97 {"out", OPT_OUT
, '>', "Output file"},
98 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt message"},
99 {"decrypt", OPT_DECRYPT
, '-', "Decrypt encrypted message"},
100 {"sign", OPT_SIGN
, '-', "Sign message"},
101 {"sign_receipt", OPT_SIGN_RECEIPT
, '-', "Generate a signed receipt for the message"},
102 {"resign", OPT_RESIGN
, '-', "Resign a signed message"},
103 {"verify", OPT_VERIFY
, '-', "Verify signed message"},
104 {"verify_retcode", OPT_VERIFY_RETCODE
, '-'},
105 {"verify_receipt", OPT_VERIFY_RECEIPT
, '<'},
106 {"cmsout", OPT_CMSOUT
, '-', "Output CMS structure"},
107 {"data_out", OPT_DATA_OUT
, '-'},
108 {"data_create", OPT_DATA_CREATE
, '-'},
109 {"digest_verify", OPT_DIGEST_VERIFY
, '-'},
110 {"digest_create", OPT_DIGEST_CREATE
, '-'},
111 {"compress", OPT_COMPRESS
, '-'},
112 {"uncompress", OPT_UNCOMPRESS
, '-'},
113 {"EncryptedData_decrypt", OPT_ED_DECRYPT
, '-'},
114 {"EncryptedData_encrypt", OPT_ED_ENCRYPT
, '-'},
115 {"debug_decrypt", OPT_DEBUG_DECRYPT
, '-'},
116 {"text", OPT_TEXT
, '-', "Include or delete text MIME headers"},
117 {"asciicrlf", OPT_ASCIICRLF
, '-'},
118 {"nointern", OPT_NOINTERN
, '-',
119 "Don't search certificates in message for signer"},
120 {"noverify", OPT_NOVERIFY
, '-', "Don't verify signers certificate"},
121 {"nocerts", OPT_NOCERTS
, '-',
122 "Don't include signers certificate when signing"},
123 {"noattr", OPT_NOATTR
, '-', "Don't include any signed attributes"},
124 {"nodetach", OPT_NODETACH
, '-', "Use opaque signing"},
125 {"nosmimecap", OPT_NOSMIMECAP
, '-', "Omit the SMIMECapabilities attribute"},
126 {"binary", OPT_BINARY
, '-', "Don't translate message to text"},
127 {"keyid", OPT_KEYID
, '-', "Use subject key identifier"},
128 {"nosigs", OPT_NOSIGS
, '-', "Don't verify message signature"},
129 {"no_content_verify", OPT_NO_CONTENT_VERIFY
, '-'},
130 {"no_attr_verify", OPT_NO_ATTR_VERIFY
, '-'},
131 {"stream", OPT_INDEF
, '-', "Enable CMS streaming"},
132 {"indef", OPT_INDEF
, '-', "Same as -stream"},
133 {"noindef", OPT_NOINDEF
, '-', "Disable CMS streaming"},
134 {"crlfeol", OPT_CRLFEOL
, '-', "Use CRLF as EOL termination instead of CR only" },
135 {"noout", OPT_NOOUT
, '-', "For the -cmsout operation do not output the parsed CMS structure"},
136 {"receipt_request_print", OPT_RR_PRINT
, '-', "Print CMS Receipt Request" },
137 {"receipt_request_all", OPT_RR_ALL
, '-'},
138 {"receipt_request_first", OPT_RR_FIRST
, '-'},
139 {"rctform", OPT_RCTFORM
, 'F', "Receipt file format"},
140 {"certfile", OPT_CERTFILE
, '<', "Other certificates file"},
141 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
142 {"CApath", OPT_CAPATH
, '/', "trusted certificates directory"},
143 {"no-CAfile", OPT_NOCAFILE
, '-',
144 "Do not load the default certificates file"},
145 {"no-CApath", OPT_NOCAPATH
, '-',
146 "Do not load certificates from the default certificates directory"},
147 {"content", OPT_CONTENT
, '<',
148 "Supply or override content for detached signature"},
149 {"print", OPT_PRINT
, '-',
150 "For the -cmsout operation print out all fields of the CMS structure"},
151 {"secretkey", OPT_SECRETKEY
, 's'},
152 {"secretkeyid", OPT_SECRETKEYID
, 's'},
153 {"pwri_password", OPT_PWRI_PASSWORD
, 's'},
154 {"econtent_type", OPT_ECONTENT_TYPE
, 's'},
155 {"rand", OPT_RAND
, 's',
156 "Load the file(s) into the random number generator"},
157 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
158 {"to", OPT_TO
, 's', "To address"},
159 {"from", OPT_FROM
, 's', "From address"},
160 {"subject", OPT_SUBJECT
, 's', "Subject"},
161 {"signer", OPT_SIGNER
, 's', "Signer certificate file"},
162 {"recip", OPT_RECIP
, '<', "Recipient cert file for decryption"},
163 {"certsout", OPT_CERTSOUT
, '>', "Certificate output file"},
164 {"md", OPT_MD
, 's', "Digest algorithm to use when signing or resigning"},
165 {"inkey", OPT_INKEY
, 's',
166 "Input private key (if not signer or recipient)"},
167 {"keyform", OPT_KEYFORM
, 'f', "Input private key format (PEM or ENGINE)"},
168 {"keyopt", OPT_KEYOPT
, 's', "Set public key parameters as n:v pairs"},
169 {"receipt_request_from", OPT_RR_FROM
, 's'},
170 {"receipt_request_to", OPT_RR_TO
, 's'},
171 {"", OPT_CIPHER
, '-', "Any supported cipher"},
173 {"aes128-wrap", OPT_AES128_WRAP
, '-', "Use AES128 to wrap key"},
174 {"aes192-wrap", OPT_AES192_WRAP
, '-', "Use AES192 to wrap key"},
175 {"aes256-wrap", OPT_AES256_WRAP
, '-', "Use AES256 to wrap key"},
176 # ifndef OPENSSL_NO_DES
177 {"des3-wrap", OPT_3DES_WRAP
, '-', "Use 3DES-EDE to wrap key"},
179 # ifndef OPENSSL_NO_ENGINE
180 {"engine", OPT_ENGINE
, 's', "Use engine e, possibly a hardware device"},
185 int cms_main(int argc
, char **argv
)
187 ASN1_OBJECT
*econtent_type
= NULL
;
188 BIO
*in
= NULL
, *out
= NULL
, *indata
= NULL
, *rctin
= NULL
;
189 CMS_ContentInfo
*cms
= NULL
, *rcms
= NULL
;
190 CMS_ReceiptRequest
*rr
= NULL
;
192 EVP_PKEY
*key
= NULL
;
193 const EVP_CIPHER
*cipher
= NULL
, *wrap_cipher
= NULL
;
194 const EVP_MD
*sign_md
= NULL
;
195 STACK_OF(OPENSSL_STRING
) *rr_to
= NULL
, *rr_from
= NULL
;
196 STACK_OF(OPENSSL_STRING
) *sksigners
= NULL
, *skkeys
= NULL
;
197 STACK_OF(X509
) *encerts
= NULL
, *other
= NULL
;
198 X509
*cert
= NULL
, *recip
= NULL
, *signer
= NULL
;
199 X509_STORE
*store
= NULL
;
200 X509_VERIFY_PARAM
*vpm
= NULL
;
201 char *certfile
= NULL
, *keyfile
= NULL
, *contfile
= NULL
;
202 const char *CAfile
= NULL
, *CApath
= NULL
;
203 char *certsoutfile
= NULL
;
204 int noCAfile
= 0, noCApath
= 0;
205 char *infile
= NULL
, *outfile
= NULL
, *rctfile
= NULL
, *inrand
= NULL
;
206 char *passinarg
= NULL
, *passin
= NULL
, *signerfile
= NULL
, *recipfile
=
208 char *to
= NULL
, *from
= NULL
, *subject
= NULL
, *prog
;
209 cms_key_param
*key_first
= NULL
, *key_param
= NULL
;
210 int flags
= CMS_DETACHED
, noout
= 0, print
= 0, keyidx
= -1, vpmtouched
=
212 int informat
= FORMAT_SMIME
, outformat
= FORMAT_SMIME
;
213 int need_rand
= 0, operation
= 0, ret
= 1, rr_print
= 0, rr_allorfirst
=
215 int verify_retcode
= 0, rctformat
= FORMAT_SMIME
, keyform
= FORMAT_PEM
;
216 size_t secret_keylen
= 0, secret_keyidlen
= 0;
217 unsigned char *pwri_pass
= NULL
, *pwri_tmp
= NULL
;
218 unsigned char *secret_key
= NULL
, *secret_keyid
= NULL
;
220 const char *mime_eol
= "\n";
223 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
226 prog
= opt_init(argc
, argv
, cms_options
);
227 while ((o
= opt_next()) != OPT_EOF
) {
232 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
235 opt_help(cms_options
);
239 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &informat
))
243 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &outformat
))
250 operation
= SMIME_ENCRYPT
;
253 operation
= SMIME_DECRYPT
;
256 operation
= SMIME_SIGN
;
258 case OPT_SIGN_RECEIPT
:
259 operation
= SMIME_SIGN_RECEIPT
;
262 operation
= SMIME_RESIGN
;
265 operation
= SMIME_VERIFY
;
267 case OPT_VERIFY_RETCODE
:
270 case OPT_VERIFY_RECEIPT
:
271 operation
= SMIME_VERIFY_RECEIPT
;
275 operation
= SMIME_CMSOUT
;
278 operation
= SMIME_DATAOUT
;
280 case OPT_DATA_CREATE
:
281 operation
= SMIME_DATA_CREATE
;
283 case OPT_DIGEST_VERIFY
:
284 operation
= SMIME_DIGEST_VERIFY
;
286 case OPT_DIGEST_CREATE
:
287 operation
= SMIME_DIGEST_CREATE
;
290 operation
= SMIME_COMPRESS
;
293 operation
= SMIME_UNCOMPRESS
;
296 operation
= SMIME_ENCRYPTED_DECRYPT
;
299 operation
= SMIME_ENCRYPTED_ENCRYPT
;
301 case OPT_DEBUG_DECRYPT
:
302 flags
|= CMS_DEBUG_DECRYPT
;
308 flags
|= CMS_ASCIICRLF
;
311 flags
|= CMS_NOINTERN
;
314 flags
|= CMS_NO_SIGNER_CERT_VERIFY
;
317 flags
|= CMS_NOCERTS
;
323 flags
&= ~CMS_DETACHED
;
326 flags
|= CMS_NOSMIMECAP
;
332 flags
|= CMS_USE_KEYID
;
337 case OPT_NO_CONTENT_VERIFY
:
338 flags
|= CMS_NO_CONTENT_VERIFY
;
340 case OPT_NO_ATTR_VERIFY
:
341 flags
|= CMS_NO_ATTR_VERIFY
;
347 flags
&= ~CMS_STREAM
;
351 flags
|= CMS_CRLFEOL
;
366 if (rctformat
== FORMAT_SMIME
)
367 rcms
= SMIME_read_CMS(rctin
, NULL
);
368 else if (rctformat
== FORMAT_PEM
)
369 rcms
= PEM_read_bio_CMS(rctin
, NULL
, NULL
, NULL
);
370 else if (rctformat
== FORMAT_ASN1
)
371 if (!opt_format(opt_arg(),
372 OPT_FMT_PEMDER
| OPT_FMT_SMIME
, &rctformat
))
376 certfile
= opt_arg();
394 contfile
= opt_arg();
398 && (rr_from
= sk_OPENSSL_STRING_new_null()) == NULL
)
400 sk_OPENSSL_STRING_push(rr_from
, opt_arg());
404 && (rr_to
= sk_OPENSSL_STRING_new_null()) == NULL
)
406 sk_OPENSSL_STRING_push(rr_to
, opt_arg());
412 if (secret_key
!= NULL
) {
413 BIO_printf(bio_err
, "Invalid key (supplied twice) %s\n",
417 secret_key
= OPENSSL_hexstr2buf(opt_arg(), <mp
);
418 if (secret_key
== NULL
) {
419 BIO_printf(bio_err
, "Invalid key %s\n", opt_arg());
422 secret_keylen
= (size_t)ltmp
;
424 case OPT_SECRETKEYID
:
425 if (secret_keyid
!= NULL
) {
426 BIO_printf(bio_err
, "Invalid id (supplied twice) %s\n",
430 secret_keyid
= OPENSSL_hexstr2buf(opt_arg(), <mp
);
431 if (secret_keyid
== NULL
) {
432 BIO_printf(bio_err
, "Invalid id %s\n", opt_arg());
435 secret_keyidlen
= (size_t)ltmp
;
437 case OPT_PWRI_PASSWORD
:
438 pwri_pass
= (unsigned char *)opt_arg();
440 case OPT_ECONTENT_TYPE
:
441 if (econtent_type
!= NULL
) {
442 BIO_printf(bio_err
, "Invalid OID (supplied twice) %s\n",
446 econtent_type
= OBJ_txt2obj(opt_arg(), 0);
447 if (econtent_type
== NULL
) {
448 BIO_printf(bio_err
, "Invalid OID %s\n", opt_arg());
457 e
= setup_engine(opt_arg(), 0);
460 passinarg
= opt_arg();
472 certsoutfile
= opt_arg();
475 if (!opt_md(opt_arg(), &sign_md
))
479 /* If previous -signer argument add signer to list */
480 if (signerfile
!= NULL
) {
481 if (sksigners
== NULL
482 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
484 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
486 keyfile
= signerfile
;
488 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
490 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
493 signerfile
= opt_arg();
496 /* If previous -inkey argument add signer to list */
497 if (keyfile
!= NULL
) {
498 if (signerfile
== NULL
) {
499 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
502 if (sksigners
== NULL
503 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
505 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
508 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
510 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
515 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
519 if (operation
== SMIME_ENCRYPT
) {
520 if (encerts
== NULL
&& (encerts
= sk_X509_new_null()) == NULL
)
522 cert
= load_cert(opt_arg(), FORMAT_PEM
,
523 "recipient certificate file");
526 sk_X509_push(encerts
, cert
);
529 recipfile
= opt_arg();
533 if (!opt_cipher(opt_unknown(), &cipher
))
538 if (operation
== SMIME_ENCRYPT
) {
540 keyidx
+= sk_X509_num(encerts
);
542 if (keyfile
!= NULL
|| signerfile
!= NULL
)
545 keyidx
+= sk_OPENSSL_STRING_num(skkeys
);
548 BIO_printf(bio_err
, "No key specified\n");
551 if (key_param
== NULL
|| key_param
->idx
!= keyidx
) {
552 cms_key_param
*nparam
;
553 nparam
= app_malloc(sizeof(*nparam
), "key param buffer");
554 nparam
->idx
= keyidx
;
555 if ((nparam
->param
= sk_OPENSSL_STRING_new_null()) == NULL
)
558 if (key_first
== NULL
)
561 key_param
->next
= nparam
;
564 sk_OPENSSL_STRING_push(key_param
->param
, opt_arg());
567 if (!opt_verify(o
, vpm
))
572 # ifndef OPENSSL_NO_DES
573 wrap_cipher
= EVP_des_ede3_wrap();
576 case OPT_AES128_WRAP
:
577 wrap_cipher
= EVP_aes_128_wrap();
579 case OPT_AES192_WRAP
:
580 wrap_cipher
= EVP_aes_192_wrap();
582 case OPT_AES256_WRAP
:
583 wrap_cipher
= EVP_aes_256_wrap();
587 argc
= opt_num_rest();
590 if ((rr_allorfirst
!= -1 || rr_from
!= NULL
) && rr_to
== NULL
) {
591 BIO_puts(bio_err
, "No Signed Receipts Recipients\n");
595 if (!(operation
& SMIME_SIGNERS
) && (rr_to
!= NULL
|| rr_from
!= NULL
)) {
596 BIO_puts(bio_err
, "Signed receipts only allowed with -sign\n");
599 if (!(operation
& SMIME_SIGNERS
) && (skkeys
!= NULL
|| sksigners
!= NULL
)) {
600 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
604 if (operation
& SMIME_SIGNERS
) {
605 if (keyfile
!= NULL
&& signerfile
== NULL
) {
606 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
609 /* Check to see if any final signer needs to be appended */
610 if (signerfile
!= NULL
) {
611 if (sksigners
== NULL
612 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
614 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
615 if (skkeys
== NULL
&& (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
618 keyfile
= signerfile
;
619 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
621 if (sksigners
== NULL
) {
622 BIO_printf(bio_err
, "No signer certificate specified\n");
628 } else if (operation
== SMIME_DECRYPT
) {
629 if (recipfile
== NULL
&& keyfile
== NULL
630 && secret_key
== NULL
&& pwri_pass
== NULL
) {
632 "No recipient certificate or key specified\n");
635 } else if (operation
== SMIME_ENCRYPT
) {
636 if (*argv
== NULL
&& secret_key
== NULL
637 && pwri_pass
== NULL
&& encerts
== NULL
) {
638 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
642 } else if (!operation
) {
646 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
647 BIO_printf(bio_err
, "Error getting password\n");
652 app_RAND_load_file(NULL
, (inrand
!= NULL
));
654 BIO_printf(bio_err
, "%ld semi-random bytes loaded\n",
655 app_RAND_load_files(inrand
));
660 if (!(operation
& SMIME_SIGNERS
))
661 flags
&= ~CMS_DETACHED
;
663 if (!(operation
& SMIME_OP
))
664 if (flags
& CMS_BINARY
)
665 outformat
= FORMAT_BINARY
;
667 if (!(operation
& SMIME_IP
))
668 if (flags
& CMS_BINARY
)
669 informat
= FORMAT_BINARY
;
671 if (operation
== SMIME_ENCRYPT
) {
673 # ifndef OPENSSL_NO_DES
674 cipher
= EVP_des_ede3_cbc();
676 BIO_printf(bio_err
, "No cipher selected\n");
681 if (secret_key
&& !secret_keyid
) {
682 BIO_printf(bio_err
, "No secret key id\n");
686 if (*argv
&& encerts
== NULL
)
687 if ((encerts
= sk_X509_new_null()) == NULL
)
690 if ((cert
= load_cert(*argv
, FORMAT_PEM
,
691 "recipient certificate file")) == NULL
)
693 sk_X509_push(encerts
, cert
);
699 if (certfile
!= NULL
) {
700 if (!load_certs(certfile
, &other
, FORMAT_PEM
, NULL
,
701 "certificate file")) {
702 ERR_print_errors(bio_err
);
707 if (recipfile
!= NULL
&& (operation
== SMIME_DECRYPT
)) {
708 if ((recip
= load_cert(recipfile
, FORMAT_PEM
,
709 "recipient certificate file")) == NULL
) {
710 ERR_print_errors(bio_err
);
715 if (operation
== SMIME_SIGN_RECEIPT
) {
716 if ((signer
= load_cert(signerfile
, FORMAT_PEM
,
717 "receipt signer certificate file")) == NULL
) {
718 ERR_print_errors(bio_err
);
723 if (operation
== SMIME_DECRYPT
) {
726 } else if ((operation
== SMIME_SIGN
) || (operation
== SMIME_SIGN_RECEIPT
)) {
728 keyfile
= signerfile
;
733 if (keyfile
!= NULL
) {
734 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
739 in
= bio_open_default(infile
, 'r', informat
);
743 if (operation
& SMIME_IP
) {
744 if (informat
== FORMAT_SMIME
) {
745 cms
= SMIME_read_CMS(in
, &indata
);
746 } else if (informat
== FORMAT_PEM
) {
747 cms
= PEM_read_bio_CMS(in
, NULL
, NULL
, NULL
);
748 } else if (informat
== FORMAT_ASN1
) {
749 cms
= d2i_CMS_bio(in
, NULL
);
751 BIO_printf(bio_err
, "Bad input format for CMS file\n");
756 BIO_printf(bio_err
, "Error reading S/MIME message\n");
759 if (contfile
!= NULL
) {
761 if ((indata
= BIO_new_file(contfile
, "rb")) == NULL
) {
762 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
766 if (certsoutfile
!= NULL
) {
767 STACK_OF(X509
) *allcerts
;
768 allcerts
= CMS_get1_certs(cms
);
769 if (!save_certs(certsoutfile
, allcerts
)) {
771 "Error writing certs to %s\n", certsoutfile
);
775 sk_X509_pop_free(allcerts
, X509_free
);
779 if (rctfile
!= NULL
) {
780 char *rctmode
= (rctformat
== FORMAT_ASN1
) ? "rb" : "r";
781 if ((rctin
= BIO_new_file(rctfile
, rctmode
)) == NULL
) {
782 BIO_printf(bio_err
, "Can't open receipt file %s\n", rctfile
);
786 if (rctformat
== FORMAT_SMIME
) {
787 rcms
= SMIME_read_CMS(rctin
, NULL
);
788 } else if (rctformat
== FORMAT_PEM
) {
789 rcms
= PEM_read_bio_CMS(rctin
, NULL
, NULL
, NULL
);
790 } else if (rctformat
== FORMAT_ASN1
) {
791 rcms
= d2i_CMS_bio(rctin
, NULL
);
793 BIO_printf(bio_err
, "Bad input format for receipt\n");
798 BIO_printf(bio_err
, "Error reading receipt\n");
803 out
= bio_open_default(outfile
, 'w', outformat
);
807 if ((operation
== SMIME_VERIFY
) || (operation
== SMIME_VERIFY_RECEIPT
)) {
808 if ((store
= setup_verify(CAfile
, CApath
, noCAfile
, noCApath
)) == NULL
)
810 X509_STORE_set_verify_cb(store
, cms_cb
);
812 X509_STORE_set1_param(store
, vpm
);
817 if (operation
== SMIME_DATA_CREATE
) {
818 cms
= CMS_data_create(in
, flags
);
819 } else if (operation
== SMIME_DIGEST_CREATE
) {
820 cms
= CMS_digest_create(in
, sign_md
, flags
);
821 } else if (operation
== SMIME_COMPRESS
) {
822 cms
= CMS_compress(in
, -1, flags
);
823 } else if (operation
== SMIME_ENCRYPT
) {
825 flags
|= CMS_PARTIAL
;
826 cms
= CMS_encrypt(NULL
, in
, cipher
, flags
);
829 for (i
= 0; i
< sk_X509_num(encerts
); i
++) {
830 CMS_RecipientInfo
*ri
;
831 cms_key_param
*kparam
;
833 X509
*x
= sk_X509_value(encerts
, i
);
834 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
835 if (kparam
->idx
== i
) {
836 tflags
|= CMS_KEY_PARAM
;
840 ri
= CMS_add1_recipient_cert(cms
, x
, tflags
);
843 if (kparam
!= NULL
) {
845 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
846 if (!cms_set_pkey_param(pctx
, kparam
->param
))
849 if (CMS_RecipientInfo_type(ri
) == CMS_RECIPINFO_AGREE
851 EVP_CIPHER_CTX
*wctx
;
852 wctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
853 EVP_EncryptInit_ex(wctx
, wrap_cipher
, NULL
, NULL
, NULL
);
857 if (secret_key
!= NULL
) {
858 if (!CMS_add0_recipient_key(cms
, NID_undef
,
859 secret_key
, secret_keylen
,
860 secret_keyid
, secret_keyidlen
,
863 /* NULL these because call absorbs them */
867 if (pwri_pass
!= NULL
) {
868 pwri_tmp
= (unsigned char *)OPENSSL_strdup((char *)pwri_pass
);
869 if (pwri_tmp
== NULL
)
871 if (CMS_add0_recipient_password(cms
,
872 -1, NID_undef
, NID_undef
,
873 pwri_tmp
, -1, NULL
) == NULL
)
877 if (!(flags
& CMS_STREAM
)) {
878 if (!CMS_final(cms
, in
, NULL
, flags
))
881 } else if (operation
== SMIME_ENCRYPTED_ENCRYPT
) {
882 cms
= CMS_EncryptedData_encrypt(in
, cipher
,
883 secret_key
, secret_keylen
, flags
);
885 } else if (operation
== SMIME_SIGN_RECEIPT
) {
886 CMS_ContentInfo
*srcms
= NULL
;
887 STACK_OF(CMS_SignerInfo
) *sis
;
889 sis
= CMS_get0_SignerInfos(cms
);
892 si
= sk_CMS_SignerInfo_value(sis
, 0);
893 srcms
= CMS_sign_receipt(si
, signer
, key
, other
, flags
);
896 CMS_ContentInfo_free(cms
);
898 } else if (operation
& SMIME_SIGNERS
) {
901 * If detached data content we enable streaming if S/MIME output
904 if (operation
== SMIME_SIGN
) {
906 if (flags
& CMS_DETACHED
) {
907 if (outformat
== FORMAT_SMIME
)
910 flags
|= CMS_PARTIAL
;
911 cms
= CMS_sign(NULL
, NULL
, other
, in
, flags
);
914 if (econtent_type
!= NULL
)
915 CMS_set1_eContentType(cms
, econtent_type
);
918 rr
= make_receipt_request(rr_to
, rr_allorfirst
, rr_from
);
921 "Signed Receipt Request Creation Error\n");
926 flags
|= CMS_REUSE_DIGEST
;
928 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++) {
930 cms_key_param
*kparam
;
932 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
933 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
935 signer
= load_cert(signerfile
, FORMAT_PEM
, "signer certificate");
938 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
941 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
942 if (kparam
->idx
== i
) {
943 tflags
|= CMS_KEY_PARAM
;
947 si
= CMS_add1_signer(cms
, signer
, key
, sign_md
, tflags
);
950 if (kparam
!= NULL
) {
952 pctx
= CMS_SignerInfo_get0_pkey_ctx(si
);
953 if (!cms_set_pkey_param(pctx
, kparam
->param
))
956 if (rr
!= NULL
&& !CMS_add1_ReceiptRequest(si
, rr
))
963 /* If not streaming or resigning finalize structure */
964 if ((operation
== SMIME_SIGN
) && !(flags
& CMS_STREAM
)) {
965 if (!CMS_final(cms
, in
, NULL
, flags
))
971 BIO_printf(bio_err
, "Error creating CMS structure\n");
976 if (operation
== SMIME_DECRYPT
) {
977 if (flags
& CMS_DEBUG_DECRYPT
)
978 CMS_decrypt(cms
, NULL
, NULL
, NULL
, NULL
, flags
);
980 if (secret_key
!= NULL
) {
981 if (!CMS_decrypt_set1_key(cms
,
982 secret_key
, secret_keylen
,
983 secret_keyid
, secret_keyidlen
)) {
984 BIO_puts(bio_err
, "Error decrypting CMS using secret key\n");
990 if (!CMS_decrypt_set1_pkey(cms
, key
, recip
)) {
991 BIO_puts(bio_err
, "Error decrypting CMS using private key\n");
996 if (pwri_pass
!= NULL
) {
997 if (!CMS_decrypt_set1_password(cms
, pwri_pass
, -1)) {
998 BIO_puts(bio_err
, "Error decrypting CMS using password\n");
1003 if (!CMS_decrypt(cms
, NULL
, NULL
, indata
, out
, flags
)) {
1004 BIO_printf(bio_err
, "Error decrypting CMS structure\n");
1007 } else if (operation
== SMIME_DATAOUT
) {
1008 if (!CMS_data(cms
, out
, flags
))
1010 } else if (operation
== SMIME_UNCOMPRESS
) {
1011 if (!CMS_uncompress(cms
, indata
, out
, flags
))
1013 } else if (operation
== SMIME_DIGEST_VERIFY
) {
1014 if (CMS_digest_verify(cms
, indata
, out
, flags
) > 0) {
1015 BIO_printf(bio_err
, "Verification successful\n");
1017 BIO_printf(bio_err
, "Verification failure\n");
1020 } else if (operation
== SMIME_ENCRYPTED_DECRYPT
) {
1021 if (!CMS_EncryptedData_decrypt(cms
, secret_key
, secret_keylen
,
1022 indata
, out
, flags
))
1024 } else if (operation
== SMIME_VERIFY
) {
1025 if (CMS_verify(cms
, other
, store
, indata
, out
, flags
) > 0) {
1026 BIO_printf(bio_err
, "Verification successful\n");
1028 BIO_printf(bio_err
, "Verification failure\n");
1030 ret
= verify_err
+ 32;
1033 if (signerfile
!= NULL
) {
1034 STACK_OF(X509
) *signers
;
1035 signers
= CMS_get0_signers(cms
);
1036 if (!save_certs(signerfile
, signers
)) {
1038 "Error writing signers to %s\n", signerfile
);
1042 sk_X509_free(signers
);
1045 receipt_request_print(cms
);
1047 } else if (operation
== SMIME_VERIFY_RECEIPT
) {
1048 if (CMS_verify_receipt(rcms
, cms
, other
, store
, flags
) > 0) {
1049 BIO_printf(bio_err
, "Verification successful\n");
1051 BIO_printf(bio_err
, "Verification failure\n");
1057 CMS_ContentInfo_print_ctx(out
, cms
, 0, NULL
);
1058 } else if (outformat
== FORMAT_SMIME
) {
1060 BIO_printf(out
, "To: %s%s", to
, mime_eol
);
1062 BIO_printf(out
, "From: %s%s", from
, mime_eol
);
1064 BIO_printf(out
, "Subject: %s%s", subject
, mime_eol
);
1065 if (operation
== SMIME_RESIGN
)
1066 ret
= SMIME_write_CMS(out
, cms
, indata
, flags
);
1068 ret
= SMIME_write_CMS(out
, cms
, in
, flags
);
1069 } else if (outformat
== FORMAT_PEM
) {
1070 ret
= PEM_write_bio_CMS_stream(out
, cms
, in
, flags
);
1071 } else if (outformat
== FORMAT_ASN1
) {
1072 ret
= i2d_CMS_bio_stream(out
, cms
, in
, flags
);
1074 BIO_printf(bio_err
, "Bad output format for CMS file\n");
1085 ERR_print_errors(bio_err
);
1087 app_RAND_write_file(NULL
);
1088 sk_X509_pop_free(encerts
, X509_free
);
1089 sk_X509_pop_free(other
, X509_free
);
1090 X509_VERIFY_PARAM_free(vpm
);
1091 sk_OPENSSL_STRING_free(sksigners
);
1092 sk_OPENSSL_STRING_free(skkeys
);
1093 OPENSSL_free(secret_key
);
1094 OPENSSL_free(secret_keyid
);
1095 OPENSSL_free(pwri_tmp
);
1096 ASN1_OBJECT_free(econtent_type
);
1097 CMS_ReceiptRequest_free(rr
);
1098 sk_OPENSSL_STRING_free(rr_to
);
1099 sk_OPENSSL_STRING_free(rr_from
);
1100 for (key_param
= key_first
; key_param
;) {
1101 cms_key_param
*tparam
;
1102 sk_OPENSSL_STRING_free(key_param
->param
);
1103 tparam
= key_param
->next
;
1104 OPENSSL_free(key_param
);
1107 X509_STORE_free(store
);
1112 CMS_ContentInfo_free(cms
);
1113 CMS_ContentInfo_free(rcms
);
1119 OPENSSL_free(passin
);
1123 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
1127 if (signerfile
== NULL
)
1129 tmp
= BIO_new_file(signerfile
, "w");
1132 for (i
= 0; i
< sk_X509_num(signers
); i
++)
1133 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
1138 /* Minimal callback just to output policy info (if any) */
1140 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
)
1144 error
= X509_STORE_CTX_get_error(ctx
);
1148 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
1149 && ((error
!= X509_V_OK
) || (ok
!= 2)))
1152 policies_print(ctx
);
1158 static void gnames_stack_print(STACK_OF(GENERAL_NAMES
) *gns
)
1160 STACK_OF(GENERAL_NAME
) *gens
;
1164 for (i
= 0; i
< sk_GENERAL_NAMES_num(gns
); i
++) {
1165 gens
= sk_GENERAL_NAMES_value(gns
, i
);
1166 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++) {
1167 gen
= sk_GENERAL_NAME_value(gens
, j
);
1168 BIO_puts(bio_err
, " ");
1169 GENERAL_NAME_print(bio_err
, gen
);
1170 BIO_puts(bio_err
, "\n");
1176 static void receipt_request_print(CMS_ContentInfo
*cms
)
1178 STACK_OF(CMS_SignerInfo
) *sis
;
1180 CMS_ReceiptRequest
*rr
;
1182 STACK_OF(GENERAL_NAMES
) *rto
, *rlist
;
1185 sis
= CMS_get0_SignerInfos(cms
);
1186 for (i
= 0; i
< sk_CMS_SignerInfo_num(sis
); i
++) {
1187 si
= sk_CMS_SignerInfo_value(sis
, i
);
1188 rv
= CMS_get1_ReceiptRequest(si
, &rr
);
1189 BIO_printf(bio_err
, "Signer %d:\n", i
+ 1);
1191 BIO_puts(bio_err
, " No Receipt Request\n");
1192 } else if (rv
< 0) {
1193 BIO_puts(bio_err
, " Receipt Request Parse Error\n");
1194 ERR_print_errors(bio_err
);
1198 CMS_ReceiptRequest_get0_values(rr
, &scid
, &allorfirst
,
1200 BIO_puts(bio_err
, " Signed Content ID:\n");
1201 idlen
= ASN1_STRING_length(scid
);
1202 id
= (const char *)ASN1_STRING_get0_data(scid
);
1203 BIO_dump_indent(bio_err
, id
, idlen
, 4);
1204 BIO_puts(bio_err
, " Receipts From");
1205 if (rlist
!= NULL
) {
1206 BIO_puts(bio_err
, " List:\n");
1207 gnames_stack_print(rlist
);
1208 } else if (allorfirst
== 1) {
1209 BIO_puts(bio_err
, ": First Tier\n");
1210 } else if (allorfirst
== 0) {
1211 BIO_puts(bio_err
, ": All\n");
1213 BIO_printf(bio_err
, " Unknown (%d)\n", allorfirst
);
1215 BIO_puts(bio_err
, " Receipts To:\n");
1216 gnames_stack_print(rto
);
1218 CMS_ReceiptRequest_free(rr
);
1222 static STACK_OF(GENERAL_NAMES
) *make_names_stack(STACK_OF(OPENSSL_STRING
) *ns
)
1225 STACK_OF(GENERAL_NAMES
) *ret
;
1226 GENERAL_NAMES
*gens
= NULL
;
1227 GENERAL_NAME
*gen
= NULL
;
1228 ret
= sk_GENERAL_NAMES_new_null();
1231 for (i
= 0; i
< sk_OPENSSL_STRING_num(ns
); i
++) {
1232 char *str
= sk_OPENSSL_STRING_value(ns
, i
);
1233 gen
= a2i_GENERAL_NAME(NULL
, NULL
, NULL
, GEN_EMAIL
, str
, 0);
1236 gens
= GENERAL_NAMES_new();
1239 if (!sk_GENERAL_NAME_push(gens
, gen
))
1242 if (!sk_GENERAL_NAMES_push(ret
, gens
))
1250 sk_GENERAL_NAMES_pop_free(ret
, GENERAL_NAMES_free
);
1251 GENERAL_NAMES_free(gens
);
1252 GENERAL_NAME_free(gen
);
1256 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
)
1257 *rr_to
, int rr_allorfirst
, STACK_OF(OPENSSL_STRING
)
1260 STACK_OF(GENERAL_NAMES
) *rct_to
= NULL
, *rct_from
= NULL
;
1261 CMS_ReceiptRequest
*rr
;
1262 rct_to
= make_names_stack(rr_to
);
1265 if (rr_from
!= NULL
) {
1266 rct_from
= make_names_stack(rr_from
);
1267 if (rct_from
== NULL
)
1272 rr
= CMS_ReceiptRequest_create0(NULL
, -1, rr_allorfirst
, rct_from
,
1276 sk_GENERAL_NAMES_pop_free(rct_to
, GENERAL_NAMES_free
);
1280 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
1281 STACK_OF(OPENSSL_STRING
) *param
)
1285 if (sk_OPENSSL_STRING_num(param
) <= 0)
1287 for (i
= 0; i
< sk_OPENSSL_STRING_num(param
); i
++) {
1288 keyopt
= sk_OPENSSL_STRING_value(param
, i
);
1289 if (pkey_ctrl_string(pctx
, keyopt
) <= 0) {
1290 BIO_printf(bio_err
, "parameter error \"%s\"\n", keyopt
);
1291 ERR_print_errors(bio_err
);