2 * Copyright 2008-2018 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 */
17 #ifndef OPENSSL_NO_CMS
19 # include <openssl/crypto.h>
20 # include <openssl/pem.h>
21 # include <openssl/err.h>
22 # include <openssl/x509_vfy.h>
23 # include <openssl/x509v3.h>
24 # include <openssl/cms.h>
26 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
);
27 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
);
28 static void receipt_request_print(CMS_ContentInfo
*cms
);
29 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
)
30 *rr_to
, int rr_allorfirst
, STACK_OF(OPENSSL_STRING
)
32 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
33 STACK_OF(OPENSSL_STRING
) *param
);
35 # define SMIME_OP 0x10
36 # define SMIME_IP 0x20
37 # define SMIME_SIGNERS 0x40
38 # define SMIME_ENCRYPT (1 | SMIME_OP)
39 # define SMIME_DECRYPT (2 | SMIME_IP)
40 # define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
41 # define SMIME_VERIFY (4 | SMIME_IP)
42 # define SMIME_CMSOUT (5 | SMIME_IP | SMIME_OP)
43 # define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
44 # define SMIME_DATAOUT (7 | SMIME_IP)
45 # define SMIME_DATA_CREATE (8 | SMIME_OP)
46 # define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
47 # define SMIME_DIGEST_CREATE (10 | SMIME_OP)
48 # define SMIME_UNCOMPRESS (11 | SMIME_IP)
49 # define SMIME_COMPRESS (12 | SMIME_OP)
50 # define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
51 # define SMIME_ENCRYPTED_ENCRYPT (14 | SMIME_OP)
52 # define SMIME_SIGN_RECEIPT (15 | SMIME_IP | SMIME_OP)
53 # define SMIME_VERIFY_RECEIPT (16 | SMIME_IP)
55 static int verify_err
= 0;
57 typedef struct cms_key_param_st cms_key_param
;
59 struct cms_key_param_st
{
61 STACK_OF(OPENSSL_STRING
) *param
;
65 typedef enum OPTION_choice
{
66 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
67 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_ENCRYPT
,
68 OPT_DECRYPT
, OPT_SIGN
, OPT_SIGN_RECEIPT
, OPT_RESIGN
,
69 OPT_VERIFY
, OPT_VERIFY_RETCODE
, OPT_VERIFY_RECEIPT
,
70 OPT_CMSOUT
, OPT_DATA_OUT
, OPT_DATA_CREATE
, OPT_DIGEST_VERIFY
,
71 OPT_DIGEST_CREATE
, OPT_COMPRESS
, OPT_UNCOMPRESS
,
72 OPT_ED_DECRYPT
, OPT_ED_ENCRYPT
, OPT_DEBUG_DECRYPT
, OPT_TEXT
,
73 OPT_ASCIICRLF
, OPT_NOINTERN
, OPT_NOVERIFY
, OPT_NOCERTS
,
74 OPT_NOATTR
, OPT_NODETACH
, OPT_NOSMIMECAP
, OPT_BINARY
, OPT_KEYID
,
75 OPT_NOSIGS
, OPT_NO_CONTENT_VERIFY
, OPT_NO_ATTR_VERIFY
, OPT_INDEF
,
76 OPT_NOINDEF
, OPT_CRLFEOL
, OPT_NOOUT
, OPT_RR_PRINT
,
77 OPT_RR_ALL
, OPT_RR_FIRST
, OPT_RCTFORM
, OPT_CERTFILE
, OPT_CAFILE
,
78 OPT_CAPATH
, OPT_NOCAPATH
, OPT_NOCAFILE
,OPT_CONTENT
, OPT_PRINT
,
79 OPT_SECRETKEY
, OPT_SECRETKEYID
, OPT_PWRI_PASSWORD
, OPT_ECONTENT_TYPE
,
80 OPT_PASSIN
, OPT_TO
, OPT_FROM
, OPT_SUBJECT
, OPT_SIGNER
, OPT_RECIP
,
81 OPT_CERTSOUT
, OPT_MD
, OPT_INKEY
, OPT_KEYFORM
, OPT_KEYOPT
, OPT_RR_FROM
,
82 OPT_RR_TO
, OPT_AES128_WRAP
, OPT_AES192_WRAP
, OPT_AES256_WRAP
,
83 OPT_3DES_WRAP
, OPT_ENGINE
,
89 const OPTIONS cms_options
[] = {
90 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] cert.pem...\n"},
91 {OPT_HELP_STR
, 1, '-',
92 " cert.pem... recipient certs for encryption\n"},
93 {OPT_HELP_STR
, 1, '-', "Valid options are:\n"},
94 {"help", OPT_HELP
, '-', "Display this summary"},
95 {"inform", OPT_INFORM
, 'c', "Input format SMIME (default), PEM or DER"},
96 {"outform", OPT_OUTFORM
, 'c',
97 "Output format SMIME (default), PEM or DER"},
98 {"in", OPT_IN
, '<', "Input file"},
99 {"out", OPT_OUT
, '>', "Output file"},
100 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt message"},
101 {"decrypt", OPT_DECRYPT
, '-', "Decrypt encrypted message"},
102 {"sign", OPT_SIGN
, '-', "Sign message"},
103 {"sign_receipt", OPT_SIGN_RECEIPT
, '-', "Generate a signed receipt for the message"},
104 {"resign", OPT_RESIGN
, '-', "Resign a signed message"},
105 {"verify", OPT_VERIFY
, '-', "Verify signed message"},
106 {"verify_retcode", OPT_VERIFY_RETCODE
, '-'},
107 {"verify_receipt", OPT_VERIFY_RECEIPT
, '<'},
108 {"cmsout", OPT_CMSOUT
, '-', "Output CMS structure"},
109 {"data_out", OPT_DATA_OUT
, '-'},
110 {"data_create", OPT_DATA_CREATE
, '-'},
111 {"digest_verify", OPT_DIGEST_VERIFY
, '-'},
112 {"digest_create", OPT_DIGEST_CREATE
, '-'},
113 {"compress", OPT_COMPRESS
, '-'},
114 {"uncompress", OPT_UNCOMPRESS
, '-'},
115 {"EncryptedData_decrypt", OPT_ED_DECRYPT
, '-'},
116 {"EncryptedData_encrypt", OPT_ED_ENCRYPT
, '-'},
117 {"debug_decrypt", OPT_DEBUG_DECRYPT
, '-'},
118 {"text", OPT_TEXT
, '-', "Include or delete text MIME headers"},
119 {"asciicrlf", OPT_ASCIICRLF
, '-'},
120 {"nointern", OPT_NOINTERN
, '-',
121 "Don't search certificates in message for signer"},
122 {"noverify", OPT_NOVERIFY
, '-', "Don't verify signers certificate"},
123 {"nocerts", OPT_NOCERTS
, '-',
124 "Don't include signers certificate when signing"},
125 {"noattr", OPT_NOATTR
, '-', "Don't include any signed attributes"},
126 {"nodetach", OPT_NODETACH
, '-', "Use opaque signing"},
127 {"nosmimecap", OPT_NOSMIMECAP
, '-', "Omit the SMIMECapabilities attribute"},
128 {"binary", OPT_BINARY
, '-', "Don't translate message to text"},
129 {"keyid", OPT_KEYID
, '-', "Use subject key identifier"},
130 {"nosigs", OPT_NOSIGS
, '-', "Don't verify message signature"},
131 {"no_content_verify", OPT_NO_CONTENT_VERIFY
, '-'},
132 {"no_attr_verify", OPT_NO_ATTR_VERIFY
, '-'},
133 {"stream", OPT_INDEF
, '-', "Enable CMS streaming"},
134 {"indef", OPT_INDEF
, '-', "Same as -stream"},
135 {"noindef", OPT_NOINDEF
, '-', "Disable CMS streaming"},
136 {"crlfeol", OPT_CRLFEOL
, '-', "Use CRLF as EOL termination instead of CR only" },
137 {"noout", OPT_NOOUT
, '-', "For the -cmsout operation do not output the parsed CMS structure"},
138 {"receipt_request_print", OPT_RR_PRINT
, '-', "Print CMS Receipt Request" },
139 {"receipt_request_all", OPT_RR_ALL
, '-'},
140 {"receipt_request_first", OPT_RR_FIRST
, '-'},
141 {"rctform", OPT_RCTFORM
, 'F', "Receipt file format"},
142 {"certfile", OPT_CERTFILE
, '<', "Other certificates file"},
143 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
144 {"CApath", OPT_CAPATH
, '/', "trusted certificates directory"},
145 {"no-CAfile", OPT_NOCAFILE
, '-',
146 "Do not load the default certificates file"},
147 {"no-CApath", OPT_NOCAPATH
, '-',
148 "Do not load certificates from the default certificates directory"},
149 {"content", OPT_CONTENT
, '<',
150 "Supply or override content for detached signature"},
151 {"print", OPT_PRINT
, '-',
152 "For the -cmsout operation print out all fields of the CMS structure"},
153 {"secretkey", OPT_SECRETKEY
, 's'},
154 {"secretkeyid", OPT_SECRETKEYID
, 's'},
155 {"pwri_password", OPT_PWRI_PASSWORD
, 's'},
156 {"econtent_type", OPT_ECONTENT_TYPE
, 's'},
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"},
174 {"aes128-wrap", OPT_AES128_WRAP
, '-', "Use AES128 to wrap key"},
175 {"aes192-wrap", OPT_AES192_WRAP
, '-', "Use AES192 to wrap key"},
176 {"aes256-wrap", OPT_AES256_WRAP
, '-', "Use AES256 to wrap key"},
177 # ifndef OPENSSL_NO_DES
178 {"des3-wrap", OPT_3DES_WRAP
, '-', "Use 3DES-EDE to wrap key"},
180 # ifndef OPENSSL_NO_ENGINE
181 {"engine", OPT_ENGINE
, 's', "Use engine e, possibly a hardware device"},
186 int cms_main(int argc
, char **argv
)
188 ASN1_OBJECT
*econtent_type
= NULL
;
189 BIO
*in
= NULL
, *out
= NULL
, *indata
= NULL
, *rctin
= NULL
;
190 CMS_ContentInfo
*cms
= NULL
, *rcms
= NULL
;
191 CMS_ReceiptRequest
*rr
= NULL
;
193 EVP_PKEY
*key
= NULL
;
194 const EVP_CIPHER
*cipher
= NULL
, *wrap_cipher
= NULL
;
195 const EVP_MD
*sign_md
= NULL
;
196 STACK_OF(OPENSSL_STRING
) *rr_to
= NULL
, *rr_from
= NULL
;
197 STACK_OF(OPENSSL_STRING
) *sksigners
= NULL
, *skkeys
= NULL
;
198 STACK_OF(X509
) *encerts
= NULL
, *other
= NULL
;
199 X509
*cert
= NULL
, *recip
= NULL
, *signer
= NULL
;
200 X509_STORE
*store
= NULL
;
201 X509_VERIFY_PARAM
*vpm
= NULL
;
202 char *certfile
= NULL
, *keyfile
= NULL
, *contfile
= NULL
;
203 const char *CAfile
= NULL
, *CApath
= NULL
;
204 char *certsoutfile
= NULL
;
205 int noCAfile
= 0, noCApath
= 0;
206 char *infile
= NULL
, *outfile
= NULL
, *rctfile
= NULL
;
207 char *passinarg
= NULL
, *passin
= NULL
, *signerfile
= NULL
, *recipfile
= NULL
;
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
= 0;
211 int informat
= FORMAT_SMIME
, outformat
= FORMAT_SMIME
;
212 int operation
= 0, ret
= 1, rr_print
= 0, rr_allorfirst
= -1;
213 int verify_retcode
= 0, rctformat
= FORMAT_SMIME
, keyform
= FORMAT_PEM
;
214 size_t secret_keylen
= 0, secret_keyidlen
= 0;
215 unsigned char *pwri_pass
= NULL
, *pwri_tmp
= NULL
;
216 unsigned char *secret_key
= NULL
, *secret_keyid
= NULL
;
218 const char *mime_eol
= "\n";
221 if ((vpm
= X509_VERIFY_PARAM_new()) == NULL
)
224 prog
= opt_init(argc
, argv
, cms_options
);
225 while ((o
= opt_next()) != OPT_EOF
) {
230 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
233 opt_help(cms_options
);
237 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &informat
))
241 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &outformat
))
248 operation
= SMIME_ENCRYPT
;
251 operation
= SMIME_DECRYPT
;
254 operation
= SMIME_SIGN
;
256 case OPT_SIGN_RECEIPT
:
257 operation
= SMIME_SIGN_RECEIPT
;
260 operation
= SMIME_RESIGN
;
263 operation
= SMIME_VERIFY
;
265 case OPT_VERIFY_RETCODE
:
268 case OPT_VERIFY_RECEIPT
:
269 operation
= SMIME_VERIFY_RECEIPT
;
273 operation
= SMIME_CMSOUT
;
276 operation
= SMIME_DATAOUT
;
278 case OPT_DATA_CREATE
:
279 operation
= SMIME_DATA_CREATE
;
281 case OPT_DIGEST_VERIFY
:
282 operation
= SMIME_DIGEST_VERIFY
;
284 case OPT_DIGEST_CREATE
:
285 operation
= SMIME_DIGEST_CREATE
;
288 operation
= SMIME_COMPRESS
;
291 operation
= SMIME_UNCOMPRESS
;
294 operation
= SMIME_ENCRYPTED_DECRYPT
;
297 operation
= SMIME_ENCRYPTED_ENCRYPT
;
299 case OPT_DEBUG_DECRYPT
:
300 flags
|= CMS_DEBUG_DECRYPT
;
306 flags
|= CMS_ASCIICRLF
;
309 flags
|= CMS_NOINTERN
;
312 flags
|= CMS_NO_SIGNER_CERT_VERIFY
;
315 flags
|= CMS_NOCERTS
;
321 flags
&= ~CMS_DETACHED
;
324 flags
|= CMS_NOSMIMECAP
;
330 flags
|= CMS_USE_KEYID
;
335 case OPT_NO_CONTENT_VERIFY
:
336 flags
|= CMS_NO_CONTENT_VERIFY
;
338 case OPT_NO_ATTR_VERIFY
:
339 flags
|= CMS_NO_ATTR_VERIFY
;
345 flags
&= ~CMS_STREAM
;
349 flags
|= CMS_CRLFEOL
;
364 if (rctformat
== FORMAT_SMIME
)
365 rcms
= SMIME_read_CMS(rctin
, NULL
);
366 else if (rctformat
== FORMAT_PEM
)
367 rcms
= PEM_read_bio_CMS(rctin
, NULL
, NULL
, NULL
);
368 else if (rctformat
== FORMAT_ASN1
)
369 if (!opt_format(opt_arg(),
370 OPT_FMT_PEMDER
| OPT_FMT_SMIME
, &rctformat
))
374 certfile
= opt_arg();
392 contfile
= opt_arg();
396 && (rr_from
= sk_OPENSSL_STRING_new_null()) == NULL
)
398 sk_OPENSSL_STRING_push(rr_from
, opt_arg());
402 && (rr_to
= sk_OPENSSL_STRING_new_null()) == NULL
)
404 sk_OPENSSL_STRING_push(rr_to
, opt_arg());
410 if (secret_key
!= NULL
) {
411 BIO_printf(bio_err
, "Invalid key (supplied twice) %s\n",
415 secret_key
= OPENSSL_hexstr2buf(opt_arg(), <mp
);
416 if (secret_key
== NULL
) {
417 BIO_printf(bio_err
, "Invalid key %s\n", opt_arg());
420 secret_keylen
= (size_t)ltmp
;
422 case OPT_SECRETKEYID
:
423 if (secret_keyid
!= NULL
) {
424 BIO_printf(bio_err
, "Invalid id (supplied twice) %s\n",
428 secret_keyid
= OPENSSL_hexstr2buf(opt_arg(), <mp
);
429 if (secret_keyid
== NULL
) {
430 BIO_printf(bio_err
, "Invalid id %s\n", opt_arg());
433 secret_keyidlen
= (size_t)ltmp
;
435 case OPT_PWRI_PASSWORD
:
436 pwri_pass
= (unsigned char *)opt_arg();
438 case OPT_ECONTENT_TYPE
:
439 if (econtent_type
!= NULL
) {
440 BIO_printf(bio_err
, "Invalid OID (supplied twice) %s\n",
444 econtent_type
= OBJ_txt2obj(opt_arg(), 0);
445 if (econtent_type
== NULL
) {
446 BIO_printf(bio_err
, "Invalid OID %s\n", opt_arg());
451 e
= setup_engine(opt_arg(), 0);
454 passinarg
= opt_arg();
466 certsoutfile
= opt_arg();
469 if (!opt_md(opt_arg(), &sign_md
))
473 /* If previous -signer argument add signer to list */
474 if (signerfile
!= NULL
) {
475 if (sksigners
== NULL
476 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
478 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
480 keyfile
= signerfile
;
482 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
484 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
487 signerfile
= opt_arg();
490 /* If previous -inkey argument add signer to list */
491 if (keyfile
!= NULL
) {
492 if (signerfile
== NULL
) {
493 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
496 if (sksigners
== NULL
497 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
499 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
502 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
504 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
509 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
513 if (operation
== SMIME_ENCRYPT
) {
514 if (encerts
== NULL
&& (encerts
= sk_X509_new_null()) == NULL
)
516 cert
= load_cert(opt_arg(), FORMAT_PEM
,
517 "recipient certificate file");
520 sk_X509_push(encerts
, cert
);
523 recipfile
= opt_arg();
527 if (!opt_cipher(opt_unknown(), &cipher
))
532 if (operation
== SMIME_ENCRYPT
) {
534 keyidx
+= sk_X509_num(encerts
);
536 if (keyfile
!= NULL
|| signerfile
!= NULL
)
539 keyidx
+= sk_OPENSSL_STRING_num(skkeys
);
542 BIO_printf(bio_err
, "No key specified\n");
545 if (key_param
== NULL
|| key_param
->idx
!= keyidx
) {
546 cms_key_param
*nparam
;
547 nparam
= app_malloc(sizeof(*nparam
), "key param buffer");
548 nparam
->idx
= keyidx
;
549 if ((nparam
->param
= sk_OPENSSL_STRING_new_null()) == NULL
)
552 if (key_first
== NULL
)
555 key_param
->next
= nparam
;
558 sk_OPENSSL_STRING_push(key_param
->param
, opt_arg());
561 if (!opt_verify(o
, vpm
))
570 # ifndef OPENSSL_NO_DES
571 wrap_cipher
= EVP_des_ede3_wrap();
574 case OPT_AES128_WRAP
:
575 wrap_cipher
= EVP_aes_128_wrap();
577 case OPT_AES192_WRAP
:
578 wrap_cipher
= EVP_aes_192_wrap();
580 case OPT_AES256_WRAP
:
581 wrap_cipher
= EVP_aes_256_wrap();
585 argc
= opt_num_rest();
588 if ((rr_allorfirst
!= -1 || rr_from
!= NULL
) && rr_to
== NULL
) {
589 BIO_puts(bio_err
, "No Signed Receipts Recipients\n");
593 if (!(operation
& SMIME_SIGNERS
) && (rr_to
!= NULL
|| rr_from
!= NULL
)) {
594 BIO_puts(bio_err
, "Signed receipts only allowed with -sign\n");
597 if (!(operation
& SMIME_SIGNERS
) && (skkeys
!= NULL
|| sksigners
!= NULL
)) {
598 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
602 if (operation
& SMIME_SIGNERS
) {
603 if (keyfile
!= NULL
&& signerfile
== NULL
) {
604 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
607 /* Check to see if any final signer needs to be appended */
608 if (signerfile
!= NULL
) {
609 if (sksigners
== NULL
610 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
612 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
613 if (skkeys
== NULL
&& (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
616 keyfile
= signerfile
;
617 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
619 if (sksigners
== NULL
) {
620 BIO_printf(bio_err
, "No signer certificate specified\n");
625 } else if (operation
== SMIME_DECRYPT
) {
626 if (recipfile
== NULL
&& keyfile
== NULL
627 && secret_key
== NULL
&& pwri_pass
== NULL
) {
629 "No recipient certificate or key specified\n");
632 } else if (operation
== SMIME_ENCRYPT
) {
633 if (*argv
== NULL
&& secret_key
== NULL
634 && pwri_pass
== NULL
&& encerts
== NULL
) {
635 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
638 } else if (!operation
) {
642 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
643 BIO_printf(bio_err
, "Error getting password\n");
649 if (!(operation
& SMIME_SIGNERS
))
650 flags
&= ~CMS_DETACHED
;
652 if (!(operation
& SMIME_OP
))
653 if (flags
& CMS_BINARY
)
654 outformat
= FORMAT_BINARY
;
656 if (!(operation
& SMIME_IP
))
657 if (flags
& CMS_BINARY
)
658 informat
= FORMAT_BINARY
;
660 if (operation
== SMIME_ENCRYPT
) {
662 # ifndef OPENSSL_NO_DES
663 cipher
= EVP_des_ede3_cbc();
665 BIO_printf(bio_err
, "No cipher selected\n");
670 if (secret_key
&& !secret_keyid
) {
671 BIO_printf(bio_err
, "No secret key id\n");
675 if (*argv
&& encerts
== NULL
)
676 if ((encerts
= sk_X509_new_null()) == NULL
)
679 if ((cert
= load_cert(*argv
, FORMAT_PEM
,
680 "recipient certificate file")) == NULL
)
682 sk_X509_push(encerts
, cert
);
688 if (certfile
!= NULL
) {
689 if (!load_certs(certfile
, &other
, FORMAT_PEM
, NULL
,
690 "certificate file")) {
691 ERR_print_errors(bio_err
);
696 if (recipfile
!= NULL
&& (operation
== SMIME_DECRYPT
)) {
697 if ((recip
= load_cert(recipfile
, FORMAT_PEM
,
698 "recipient certificate file")) == NULL
) {
699 ERR_print_errors(bio_err
);
704 if (operation
== SMIME_SIGN_RECEIPT
) {
705 if ((signer
= load_cert(signerfile
, FORMAT_PEM
,
706 "receipt signer certificate file")) == NULL
) {
707 ERR_print_errors(bio_err
);
712 if (operation
== SMIME_DECRYPT
) {
715 } else if ((operation
== SMIME_SIGN
) || (operation
== SMIME_SIGN_RECEIPT
)) {
717 keyfile
= signerfile
;
722 if (keyfile
!= NULL
) {
723 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
728 in
= bio_open_default(infile
, 'r', informat
);
732 if (operation
& SMIME_IP
) {
733 if (informat
== FORMAT_SMIME
) {
734 cms
= SMIME_read_CMS(in
, &indata
);
735 } else if (informat
== FORMAT_PEM
) {
736 cms
= PEM_read_bio_CMS(in
, NULL
, NULL
, NULL
);
737 } else if (informat
== FORMAT_ASN1
) {
738 cms
= d2i_CMS_bio(in
, NULL
);
740 BIO_printf(bio_err
, "Bad input format for CMS file\n");
745 BIO_printf(bio_err
, "Error reading S/MIME message\n");
748 if (contfile
!= NULL
) {
750 if ((indata
= BIO_new_file(contfile
, "rb")) == NULL
) {
751 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
755 if (certsoutfile
!= NULL
) {
756 STACK_OF(X509
) *allcerts
;
757 allcerts
= CMS_get1_certs(cms
);
758 if (!save_certs(certsoutfile
, allcerts
)) {
760 "Error writing certs to %s\n", certsoutfile
);
764 sk_X509_pop_free(allcerts
, X509_free
);
768 if (rctfile
!= NULL
) {
769 char *rctmode
= (rctformat
== FORMAT_ASN1
) ? "rb" : "r";
770 if ((rctin
= BIO_new_file(rctfile
, rctmode
)) == NULL
) {
771 BIO_printf(bio_err
, "Can't open receipt file %s\n", rctfile
);
775 if (rctformat
== FORMAT_SMIME
) {
776 rcms
= SMIME_read_CMS(rctin
, NULL
);
777 } else if (rctformat
== FORMAT_PEM
) {
778 rcms
= PEM_read_bio_CMS(rctin
, NULL
, NULL
, NULL
);
779 } else if (rctformat
== FORMAT_ASN1
) {
780 rcms
= d2i_CMS_bio(rctin
, NULL
);
782 BIO_printf(bio_err
, "Bad input format for receipt\n");
787 BIO_printf(bio_err
, "Error reading receipt\n");
792 out
= bio_open_default(outfile
, 'w', outformat
);
796 if ((operation
== SMIME_VERIFY
) || (operation
== SMIME_VERIFY_RECEIPT
)) {
797 if ((store
= setup_verify(CAfile
, CApath
, noCAfile
, noCApath
)) == NULL
)
799 X509_STORE_set_verify_cb(store
, cms_cb
);
801 X509_STORE_set1_param(store
, vpm
);
806 if (operation
== SMIME_DATA_CREATE
) {
807 cms
= CMS_data_create(in
, flags
);
808 } else if (operation
== SMIME_DIGEST_CREATE
) {
809 cms
= CMS_digest_create(in
, sign_md
, flags
);
810 } else if (operation
== SMIME_COMPRESS
) {
811 cms
= CMS_compress(in
, -1, flags
);
812 } else if (operation
== SMIME_ENCRYPT
) {
814 flags
|= CMS_PARTIAL
;
815 cms
= CMS_encrypt(NULL
, in
, cipher
, flags
);
818 for (i
= 0; i
< sk_X509_num(encerts
); i
++) {
819 CMS_RecipientInfo
*ri
;
820 cms_key_param
*kparam
;
822 X509
*x
= sk_X509_value(encerts
, i
);
823 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
824 if (kparam
->idx
== i
) {
825 tflags
|= CMS_KEY_PARAM
;
829 ri
= CMS_add1_recipient_cert(cms
, x
, tflags
);
832 if (kparam
!= NULL
) {
834 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
835 if (!cms_set_pkey_param(pctx
, kparam
->param
))
838 if (CMS_RecipientInfo_type(ri
) == CMS_RECIPINFO_AGREE
840 EVP_CIPHER_CTX
*wctx
;
841 wctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
842 EVP_EncryptInit_ex(wctx
, wrap_cipher
, NULL
, NULL
, NULL
);
846 if (secret_key
!= NULL
) {
847 if (!CMS_add0_recipient_key(cms
, NID_undef
,
848 secret_key
, secret_keylen
,
849 secret_keyid
, secret_keyidlen
,
852 /* NULL these because call absorbs them */
856 if (pwri_pass
!= NULL
) {
857 pwri_tmp
= (unsigned char *)OPENSSL_strdup((char *)pwri_pass
);
858 if (pwri_tmp
== NULL
)
860 if (CMS_add0_recipient_password(cms
,
861 -1, NID_undef
, NID_undef
,
862 pwri_tmp
, -1, NULL
) == NULL
)
866 if (!(flags
& CMS_STREAM
)) {
867 if (!CMS_final(cms
, in
, NULL
, flags
))
870 } else if (operation
== SMIME_ENCRYPTED_ENCRYPT
) {
871 cms
= CMS_EncryptedData_encrypt(in
, cipher
,
872 secret_key
, secret_keylen
, flags
);
874 } else if (operation
== SMIME_SIGN_RECEIPT
) {
875 CMS_ContentInfo
*srcms
= NULL
;
876 STACK_OF(CMS_SignerInfo
) *sis
;
878 sis
= CMS_get0_SignerInfos(cms
);
881 si
= sk_CMS_SignerInfo_value(sis
, 0);
882 srcms
= CMS_sign_receipt(si
, signer
, key
, other
, flags
);
885 CMS_ContentInfo_free(cms
);
887 } else if (operation
& SMIME_SIGNERS
) {
890 * If detached data content we enable streaming if S/MIME output
893 if (operation
== SMIME_SIGN
) {
895 if (flags
& CMS_DETACHED
) {
896 if (outformat
== FORMAT_SMIME
)
899 flags
|= CMS_PARTIAL
;
900 cms
= CMS_sign(NULL
, NULL
, other
, in
, flags
);
903 if (econtent_type
!= NULL
)
904 CMS_set1_eContentType(cms
, econtent_type
);
907 rr
= make_receipt_request(rr_to
, rr_allorfirst
, rr_from
);
910 "Signed Receipt Request Creation Error\n");
915 flags
|= CMS_REUSE_DIGEST
;
917 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++) {
919 cms_key_param
*kparam
;
921 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
922 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
924 signer
= load_cert(signerfile
, FORMAT_PEM
, "signer certificate");
925 if (signer
== NULL
) {
929 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key file");
934 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
935 if (kparam
->idx
== i
) {
936 tflags
|= CMS_KEY_PARAM
;
940 si
= CMS_add1_signer(cms
, signer
, key
, sign_md
, tflags
);
943 if (kparam
!= NULL
) {
945 pctx
= CMS_SignerInfo_get0_pkey_ctx(si
);
946 if (!cms_set_pkey_param(pctx
, kparam
->param
))
949 if (rr
!= NULL
&& !CMS_add1_ReceiptRequest(si
, rr
))
956 /* If not streaming or resigning finalize structure */
957 if ((operation
== SMIME_SIGN
) && !(flags
& CMS_STREAM
)) {
958 if (!CMS_final(cms
, in
, NULL
, flags
))
964 BIO_printf(bio_err
, "Error creating CMS structure\n");
969 if (operation
== SMIME_DECRYPT
) {
970 if (flags
& CMS_DEBUG_DECRYPT
)
971 CMS_decrypt(cms
, NULL
, NULL
, NULL
, NULL
, flags
);
973 if (secret_key
!= NULL
) {
974 if (!CMS_decrypt_set1_key(cms
,
975 secret_key
, secret_keylen
,
976 secret_keyid
, secret_keyidlen
)) {
977 BIO_puts(bio_err
, "Error decrypting CMS using secret key\n");
983 if (!CMS_decrypt_set1_pkey(cms
, key
, recip
)) {
984 BIO_puts(bio_err
, "Error decrypting CMS using private key\n");
989 if (pwri_pass
!= NULL
) {
990 if (!CMS_decrypt_set1_password(cms
, pwri_pass
, -1)) {
991 BIO_puts(bio_err
, "Error decrypting CMS using password\n");
996 if (!CMS_decrypt(cms
, NULL
, NULL
, indata
, out
, flags
)) {
997 BIO_printf(bio_err
, "Error decrypting CMS structure\n");
1000 } else if (operation
== SMIME_DATAOUT
) {
1001 if (!CMS_data(cms
, out
, flags
))
1003 } else if (operation
== SMIME_UNCOMPRESS
) {
1004 if (!CMS_uncompress(cms
, indata
, out
, flags
))
1006 } else if (operation
== SMIME_DIGEST_VERIFY
) {
1007 if (CMS_digest_verify(cms
, indata
, out
, flags
) > 0) {
1008 BIO_printf(bio_err
, "Verification successful\n");
1010 BIO_printf(bio_err
, "Verification failure\n");
1013 } else if (operation
== SMIME_ENCRYPTED_DECRYPT
) {
1014 if (!CMS_EncryptedData_decrypt(cms
, secret_key
, secret_keylen
,
1015 indata
, out
, flags
))
1017 } else if (operation
== SMIME_VERIFY
) {
1018 if (CMS_verify(cms
, other
, store
, indata
, out
, flags
) > 0) {
1019 BIO_printf(bio_err
, "Verification successful\n");
1021 BIO_printf(bio_err
, "Verification failure\n");
1023 ret
= verify_err
+ 32;
1026 if (signerfile
!= NULL
) {
1027 STACK_OF(X509
) *signers
;
1028 signers
= CMS_get0_signers(cms
);
1029 if (!save_certs(signerfile
, signers
)) {
1031 "Error writing signers to %s\n", signerfile
);
1035 sk_X509_free(signers
);
1038 receipt_request_print(cms
);
1040 } else if (operation
== SMIME_VERIFY_RECEIPT
) {
1041 if (CMS_verify_receipt(rcms
, cms
, other
, store
, flags
) > 0) {
1042 BIO_printf(bio_err
, "Verification successful\n");
1044 BIO_printf(bio_err
, "Verification failure\n");
1050 CMS_ContentInfo_print_ctx(out
, cms
, 0, NULL
);
1051 } else if (outformat
== FORMAT_SMIME
) {
1053 BIO_printf(out
, "To: %s%s", to
, mime_eol
);
1055 BIO_printf(out
, "From: %s%s", from
, mime_eol
);
1057 BIO_printf(out
, "Subject: %s%s", subject
, mime_eol
);
1058 if (operation
== SMIME_RESIGN
)
1059 ret
= SMIME_write_CMS(out
, cms
, indata
, flags
);
1061 ret
= SMIME_write_CMS(out
, cms
, in
, flags
);
1062 } else if (outformat
== FORMAT_PEM
) {
1063 ret
= PEM_write_bio_CMS_stream(out
, cms
, in
, flags
);
1064 } else if (outformat
== FORMAT_ASN1
) {
1065 ret
= i2d_CMS_bio_stream(out
, cms
, in
, flags
);
1067 BIO_printf(bio_err
, "Bad output format for CMS file\n");
1078 ERR_print_errors(bio_err
);
1079 sk_X509_pop_free(encerts
, X509_free
);
1080 sk_X509_pop_free(other
, X509_free
);
1081 X509_VERIFY_PARAM_free(vpm
);
1082 sk_OPENSSL_STRING_free(sksigners
);
1083 sk_OPENSSL_STRING_free(skkeys
);
1084 OPENSSL_free(secret_key
);
1085 OPENSSL_free(secret_keyid
);
1086 OPENSSL_free(pwri_tmp
);
1087 ASN1_OBJECT_free(econtent_type
);
1088 CMS_ReceiptRequest_free(rr
);
1089 sk_OPENSSL_STRING_free(rr_to
);
1090 sk_OPENSSL_STRING_free(rr_from
);
1091 for (key_param
= key_first
; key_param
;) {
1092 cms_key_param
*tparam
;
1093 sk_OPENSSL_STRING_free(key_param
->param
);
1094 tparam
= key_param
->next
;
1095 OPENSSL_free(key_param
);
1098 X509_STORE_free(store
);
1103 CMS_ContentInfo_free(cms
);
1104 CMS_ContentInfo_free(rcms
);
1110 OPENSSL_free(passin
);
1114 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
1118 if (signerfile
== NULL
)
1120 tmp
= BIO_new_file(signerfile
, "w");
1123 for (i
= 0; i
< sk_X509_num(signers
); i
++)
1124 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
1129 /* Minimal callback just to output policy info (if any) */
1131 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
)
1135 error
= X509_STORE_CTX_get_error(ctx
);
1139 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
1140 && ((error
!= X509_V_OK
) || (ok
!= 2)))
1143 policies_print(ctx
);
1149 static void gnames_stack_print(STACK_OF(GENERAL_NAMES
) *gns
)
1151 STACK_OF(GENERAL_NAME
) *gens
;
1155 for (i
= 0; i
< sk_GENERAL_NAMES_num(gns
); i
++) {
1156 gens
= sk_GENERAL_NAMES_value(gns
, i
);
1157 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++) {
1158 gen
= sk_GENERAL_NAME_value(gens
, j
);
1159 BIO_puts(bio_err
, " ");
1160 GENERAL_NAME_print(bio_err
, gen
);
1161 BIO_puts(bio_err
, "\n");
1167 static void receipt_request_print(CMS_ContentInfo
*cms
)
1169 STACK_OF(CMS_SignerInfo
) *sis
;
1171 CMS_ReceiptRequest
*rr
;
1173 STACK_OF(GENERAL_NAMES
) *rto
, *rlist
;
1176 sis
= CMS_get0_SignerInfos(cms
);
1177 for (i
= 0; i
< sk_CMS_SignerInfo_num(sis
); i
++) {
1178 si
= sk_CMS_SignerInfo_value(sis
, i
);
1179 rv
= CMS_get1_ReceiptRequest(si
, &rr
);
1180 BIO_printf(bio_err
, "Signer %d:\n", i
+ 1);
1182 BIO_puts(bio_err
, " No Receipt Request\n");
1183 } else if (rv
< 0) {
1184 BIO_puts(bio_err
, " Receipt Request Parse Error\n");
1185 ERR_print_errors(bio_err
);
1189 CMS_ReceiptRequest_get0_values(rr
, &scid
, &allorfirst
,
1191 BIO_puts(bio_err
, " Signed Content ID:\n");
1192 idlen
= ASN1_STRING_length(scid
);
1193 id
= (const char *)ASN1_STRING_get0_data(scid
);
1194 BIO_dump_indent(bio_err
, id
, idlen
, 4);
1195 BIO_puts(bio_err
, " Receipts From");
1196 if (rlist
!= NULL
) {
1197 BIO_puts(bio_err
, " List:\n");
1198 gnames_stack_print(rlist
);
1199 } else if (allorfirst
== 1) {
1200 BIO_puts(bio_err
, ": First Tier\n");
1201 } else if (allorfirst
== 0) {
1202 BIO_puts(bio_err
, ": All\n");
1204 BIO_printf(bio_err
, " Unknown (%d)\n", allorfirst
);
1206 BIO_puts(bio_err
, " Receipts To:\n");
1207 gnames_stack_print(rto
);
1209 CMS_ReceiptRequest_free(rr
);
1213 static STACK_OF(GENERAL_NAMES
) *make_names_stack(STACK_OF(OPENSSL_STRING
) *ns
)
1216 STACK_OF(GENERAL_NAMES
) *ret
;
1217 GENERAL_NAMES
*gens
= NULL
;
1218 GENERAL_NAME
*gen
= NULL
;
1219 ret
= sk_GENERAL_NAMES_new_null();
1222 for (i
= 0; i
< sk_OPENSSL_STRING_num(ns
); i
++) {
1223 char *str
= sk_OPENSSL_STRING_value(ns
, i
);
1224 gen
= a2i_GENERAL_NAME(NULL
, NULL
, NULL
, GEN_EMAIL
, str
, 0);
1227 gens
= GENERAL_NAMES_new();
1230 if (!sk_GENERAL_NAME_push(gens
, gen
))
1233 if (!sk_GENERAL_NAMES_push(ret
, gens
))
1241 sk_GENERAL_NAMES_pop_free(ret
, GENERAL_NAMES_free
);
1242 GENERAL_NAMES_free(gens
);
1243 GENERAL_NAME_free(gen
);
1247 static CMS_ReceiptRequest
*make_receipt_request(STACK_OF(OPENSSL_STRING
)
1248 *rr_to
, int rr_allorfirst
, STACK_OF(OPENSSL_STRING
)
1251 STACK_OF(GENERAL_NAMES
) *rct_to
= NULL
, *rct_from
= NULL
;
1252 CMS_ReceiptRequest
*rr
;
1253 rct_to
= make_names_stack(rr_to
);
1256 if (rr_from
!= NULL
) {
1257 rct_from
= make_names_stack(rr_from
);
1258 if (rct_from
== NULL
)
1263 rr
= CMS_ReceiptRequest_create0(NULL
, -1, rr_allorfirst
, rct_from
,
1267 sk_GENERAL_NAMES_pop_free(rct_to
, GENERAL_NAMES_free
);
1271 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
1272 STACK_OF(OPENSSL_STRING
) *param
)
1276 if (sk_OPENSSL_STRING_num(param
) <= 0)
1278 for (i
= 0; i
< sk_OPENSSL_STRING_num(param
); i
++) {
1279 keyopt
= sk_OPENSSL_STRING_value(param
, i
);
1280 if (pkey_ctrl_string(pctx
, keyopt
) <= 0) {
1281 BIO_printf(bio_err
, "parameter error \"%s\"\n", keyopt
);
1282 ERR_print_errors(bio_err
);