2 * Copyright 2008-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 #include <openssl/crypto.h>
18 #include <openssl/pem.h>
19 #include <openssl/err.h>
20 #include <openssl/x509_vfy.h>
21 #include <openssl/x509v3.h>
22 #include <openssl/cms.h>
24 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
);
25 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
);
26 static void receipt_request_print(CMS_ContentInfo
*cms
);
27 static CMS_ReceiptRequest
28 *make_receipt_request(STACK_OF(OPENSSL_STRING
) *rr_to
, int rr_allorfirst
,
29 STACK_OF(OPENSSL_STRING
) *rr_from
);
30 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
31 STACK_OF(OPENSSL_STRING
) *param
);
33 #define SMIME_OP 0x100
34 #define SMIME_IP 0x200
35 #define SMIME_SIGNERS 0x400
36 #define SMIME_ENCRYPT (1 | SMIME_OP)
37 #define SMIME_DECRYPT (2 | SMIME_IP)
38 #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
39 #define SMIME_VERIFY (4 | SMIME_IP)
40 #define SMIME_RESIGN (5 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
41 #define SMIME_SIGN_RECEIPT (6 | SMIME_IP | SMIME_OP)
42 #define SMIME_VERIFY_RECEIPT (7 | SMIME_IP)
43 #define SMIME_DIGEST_CREATE (8 | SMIME_OP)
44 #define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
45 #define SMIME_COMPRESS (10 | SMIME_OP)
46 #define SMIME_UNCOMPRESS (11 | SMIME_IP)
47 #define SMIME_ENCRYPTED_ENCRYPT (12 | SMIME_OP)
48 #define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
49 #define SMIME_DATA_CREATE (14 | SMIME_OP)
50 #define SMIME_DATA_OUT (15 | SMIME_IP)
51 #define SMIME_CMSOUT (16 | SMIME_IP | SMIME_OP)
53 static int verify_err
= 0;
55 typedef struct cms_key_param_st cms_key_param
;
57 struct cms_key_param_st
{
59 STACK_OF(OPENSSL_STRING
) *param
;
63 typedef enum OPTION_choice
{
65 OPT_INFORM
, OPT_OUTFORM
, OPT_IN
, OPT_OUT
, OPT_ENCRYPT
,
66 OPT_DECRYPT
, OPT_SIGN
, OPT_CADES
, OPT_SIGN_RECEIPT
, OPT_RESIGN
,
67 OPT_VERIFY
, OPT_VERIFY_RETCODE
, OPT_VERIFY_RECEIPT
,
68 OPT_CMSOUT
, OPT_DATA_OUT
, OPT_DATA_CREATE
, OPT_DIGEST_VERIFY
,
69 OPT_DIGEST
, OPT_DIGEST_CREATE
, OPT_COMPRESS
, OPT_UNCOMPRESS
,
70 OPT_ED_DECRYPT
, OPT_ED_ENCRYPT
, OPT_DEBUG_DECRYPT
, OPT_TEXT
,
71 OPT_ASCIICRLF
, OPT_NOINTERN
, OPT_NOVERIFY
, OPT_NOCERTS
,
72 OPT_NOATTR
, OPT_NODETACH
, OPT_NOSMIMECAP
, OPT_BINARY
, OPT_KEYID
,
73 OPT_NOSIGS
, OPT_NO_CONTENT_VERIFY
, OPT_NO_ATTR_VERIFY
, OPT_INDEF
,
74 OPT_NOINDEF
, OPT_CRLFEOL
, OPT_NOOUT
, OPT_RR_PRINT
,
75 OPT_RR_ALL
, OPT_RR_FIRST
, OPT_RCTFORM
, OPT_CERTFILE
, OPT_CAFILE
,
76 OPT_CAPATH
, OPT_CASTORE
, OPT_NOCAPATH
, OPT_NOCAFILE
, OPT_NOCASTORE
,
77 OPT_CONTENT
, OPT_PRINT
, OPT_NAMEOPT
,
78 OPT_SECRETKEY
, OPT_SECRETKEYID
, OPT_PWRI_PASSWORD
, OPT_ECONTENT_TYPE
,
79 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_WRAP
, OPT_ENGINE
,
84 OPT_PROV_ENUM
, OPT_CONFIG
,
90 const OPTIONS cms_options
[] = {
91 {OPT_HELP_STR
, 1, '-', "Usage: %s [options] [cert...]\n"},
92 {"help", OPT_HELP
, '-', "Display this summary"},
94 OPT_SECTION("General"),
95 {"in", OPT_IN
, '<', "Input file"},
96 {"out", OPT_OUT
, '>', "Output file"},
99 OPT_SECTION("Operation"),
100 {"encrypt", OPT_ENCRYPT
, '-', "Encrypt message"},
101 {"decrypt", OPT_DECRYPT
, '-', "Decrypt encrypted message"},
102 {"sign", OPT_SIGN
, '-', "Sign message"},
103 {"verify", OPT_VERIFY
, '-', "Verify signed message"},
104 {"resign", OPT_RESIGN
, '-', "Resign a signed message"},
105 {"sign_receipt", OPT_SIGN_RECEIPT
, '-',
106 "Generate a signed receipt for a message"},
107 {"verify_receipt", OPT_VERIFY_RECEIPT
, '<',
108 "Verify receipts; exit if receipt signatures do not verify"},
109 {"digest", OPT_DIGEST
, 's', "Sign a pre-computed digest in hex notation"},
110 {"digest_create", OPT_DIGEST_CREATE
, '-',
111 "Create a CMS \"DigestedData\" object"},
112 {"digest_verify", OPT_DIGEST_VERIFY
, '-',
113 "Verify a CMS \"DigestedData\" object and output it"},
114 {"compress", OPT_COMPRESS
, '-', "Create a CMS \"CompressedData\" object"},
115 {"uncompress", OPT_UNCOMPRESS
, '-',
116 "Uncompress a CMS \"CompressedData\" object"},
117 {"EncryptedData_encrypt", OPT_ED_ENCRYPT
, '-',
118 "Create CMS \"EncryptedData\" object using symmetric key"},
119 {"EncryptedData_decrypt", OPT_ED_DECRYPT
, '-',
120 "Decrypt CMS \"EncryptedData\" object using symmetric key"},
121 {"data_create", OPT_DATA_CREATE
, '-', "Create a CMS \"Data\" object"},
122 {"data_out", OPT_DATA_OUT
, '-', "Copy CMS \"Data\" object to output"},
123 {"cmsout", OPT_CMSOUT
, '-', "Output CMS structure"},
125 OPT_SECTION("File format"),
126 {"inform", OPT_INFORM
, 'c', "Input format SMIME (default), PEM or DER"},
127 {"outform", OPT_OUTFORM
, 'c',
128 "Output format SMIME (default), PEM or DER"},
129 {"rctform", OPT_RCTFORM
, 'F', "Receipt file format"},
130 {"stream", OPT_INDEF
, '-', "Enable CMS streaming"},
131 {"indef", OPT_INDEF
, '-', "Same as -stream"},
132 {"noindef", OPT_NOINDEF
, '-', "Disable CMS streaming"},
133 {"binary", OPT_BINARY
, '-',
134 "Treat input as binary: do not translate to canonical form"},
135 {"crlfeol", OPT_CRLFEOL
, '-',
136 "Use CRLF as EOL termination instead of CR only" },
137 {"asciicrlf", OPT_ASCIICRLF
, '-',
138 "Perform CRLF canonicalisation when signing"},
140 OPT_SECTION("Keys and passwords"),
141 {"pwri_password", OPT_PWRI_PASSWORD
, 's',
142 "Specific password for recipient"},
143 {"secretkey", OPT_SECRETKEY
, 's',
144 "Use specified hex-encoded key to decrypt/encrypt recipients or content"},
145 {"secretkeyid", OPT_SECRETKEYID
, 's',
146 "Identity of the -secretkey for CMS \"KEKRecipientInfo\" object"},
147 {"inkey", OPT_INKEY
, 's',
148 "Input private key (if not signer or recipient)"},
149 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
150 {"keyopt", OPT_KEYOPT
, 's', "Set public key parameters as n:v pairs"},
151 {"keyform", OPT_KEYFORM
, 'f',
152 "Input private key format (ENGINE, other values ignored)"},
153 #ifndef OPENSSL_NO_ENGINE
154 {"engine", OPT_ENGINE
, 's', "Use engine e, possibly a hardware device"},
159 OPT_SECTION("Encryption and decryption"),
160 {"originator", OPT_ORIGINATOR
, 's', "Originator certificate file"},
161 {"recip", OPT_RECIP
, '<', "Recipient cert file"},
162 {"cert...", OPT_PARAM
, '.',
163 "Recipient certs (optional; used only when encrypting)"},
164 {"", OPT_CIPHER
, '-',
165 "The encryption algorithm to use (any supported cipher)"},
166 {"wrap", OPT_WRAP
, 's',
167 "Key wrap algorithm to use when encrypting with key agreement"},
168 {"aes128-wrap", OPT_AES128_WRAP
, '-', "Use AES128 to wrap key"},
169 {"aes192-wrap", OPT_AES192_WRAP
, '-', "Use AES192 to wrap key"},
170 {"aes256-wrap", OPT_AES256_WRAP
, '-', "Use AES256 to wrap key"},
171 {"des3-wrap", OPT_3DES_WRAP
, '-', "Use 3DES-EDE to wrap key"},
172 {"debug_decrypt", OPT_DEBUG_DECRYPT
, '-',
173 "Disable MMA protection, return error if no recipient found (see doc)"},
175 OPT_SECTION("Signing"),
176 {"md", OPT_MD
, 's', "Digest algorithm to use"},
177 {"signer", OPT_SIGNER
, 's', "Signer certificate input file"},
178 {"certfile", OPT_CERTFILE
, '<', "Other certificates file"},
179 {"cades", OPT_CADES
, '-',
180 "Include signingCertificate attribute (CAdES-BES)"},
181 {"nodetach", OPT_NODETACH
, '-', "Use opaque signing"},
182 {"nocerts", OPT_NOCERTS
, '-',
183 "Don't include signer's certificate when signing"},
184 {"noattr", OPT_NOATTR
, '-', "Don't include any signed attributes"},
185 {"nosmimecap", OPT_NOSMIMECAP
, '-', "Omit the SMIMECapabilities attribute"},
186 {"receipt_request_all", OPT_RR_ALL
, '-',
187 "When signing, create a receipt request for all recipients"},
188 {"receipt_request_first", OPT_RR_FIRST
, '-',
189 "When signing, create a receipt request for first recipient"},
190 {"receipt_request_from", OPT_RR_FROM
, 's',
191 "Create signed receipt request with specified email address"},
192 {"receipt_request_to", OPT_RR_TO
, 's',
193 "Create signed receipt targeted to specified address"},
195 OPT_SECTION("Verification"),
196 {"signer", OPT_DUP
, 's', "Signer certificate(s) output file"},
197 {"content", OPT_CONTENT
, '<',
198 "Supply or override content for detached signature"},
199 {"no_content_verify", OPT_NO_CONTENT_VERIFY
, '-',
200 "Do not verify signed content signatures"},
201 {"no_attr_verify", OPT_NO_ATTR_VERIFY
, '-',
202 "Do not verify signed attribute signatures"},
203 {"nosigs", OPT_NOSIGS
, '-', "Don't verify message signature"},
204 {"noverify", OPT_NOVERIFY
, '-', "Don't verify signers certificate"},
205 {"nointern", OPT_NOINTERN
, '-',
206 "Don't search certificates in message for signer"},
207 {"cades", OPT_DUP
, '-', "Check signingCertificate (CAdES-BES)"},
208 {"verify_retcode", OPT_VERIFY_RETCODE
, '-',
209 "Exit non-zero on verification failure"},
210 {"CAfile", OPT_CAFILE
, '<', "Trusted certificates file"},
211 {"CApath", OPT_CAPATH
, '/', "Trusted certificates directory"},
212 {"CAstore", OPT_CASTORE
, ':', "Trusted certificates store URI"},
213 {"no-CAfile", OPT_NOCAFILE
, '-',
214 "Do not load the default certificates file"},
215 {"no-CApath", OPT_NOCAPATH
, '-',
216 "Do not load certificates from the default certificates directory"},
217 {"no-CAstore", OPT_NOCASTORE
, '-',
218 "Do not load certificates from the default certificates store"},
220 OPT_SECTION("Output"),
221 {"keyid", OPT_KEYID
, '-', "Use subject key identifier"},
222 {"econtent_type", OPT_ECONTENT_TYPE
, 's', "OID for external content"},
223 {"text", OPT_TEXT
, '-', "Include or delete text MIME headers"},
224 {"certsout", OPT_CERTSOUT
, '>', "Certificate output file"},
225 {"to", OPT_TO
, 's', "To address"},
226 {"from", OPT_FROM
, 's', "From address"},
227 {"subject", OPT_SUBJECT
, 's', "Subject"},
229 OPT_SECTION("Printing"),
230 {"noout", OPT_NOOUT
, '-',
231 "For the -cmsout operation do not output the parsed CMS structure"},
232 {"print", OPT_PRINT
, '-',
233 "For the -cmsout operation print out all fields of the CMS structure"},
234 {"nameopt", OPT_NAMEOPT
, 's',
235 "For the -print option specifies various strings printing options"},
236 {"receipt_request_print", OPT_RR_PRINT
, '-', "Print CMS Receipt Request" },
242 static CMS_ContentInfo
*load_content_info(int informat
, BIO
*in
, int flags
,
243 BIO
**indata
, const char *name
)
245 CMS_ContentInfo
*ret
, *ci
;
247 ret
= CMS_ContentInfo_new_ex(app_get0_libctx(), app_get0_propq());
249 BIO_printf(bio_err
, "Error allocating CMS_contentinfo\n");
254 ci
= SMIME_read_CMS_ex(in
, flags
, indata
, &ret
);
257 ci
= PEM_read_bio_CMS(in
, &ret
, NULL
, NULL
);
260 ci
= d2i_CMS_bio(in
, &ret
);
263 BIO_printf(bio_err
, "Bad input format for %s\n", name
);
267 BIO_printf(bio_err
, "Error reading %s Content Info\n", name
);
272 CMS_ContentInfo_free(ret
);
276 int cms_main(int argc
, char **argv
)
279 ASN1_OBJECT
*econtent_type
= NULL
;
280 BIO
*in
= NULL
, *out
= NULL
, *indata
= NULL
, *rctin
= NULL
;
281 CMS_ContentInfo
*cms
= NULL
, *rcms
= NULL
;
282 CMS_ReceiptRequest
*rr
= NULL
;
284 EVP_PKEY
*key
= NULL
;
285 EVP_CIPHER
*cipher
= NULL
, *wrap_cipher
= NULL
;
286 EVP_MD
*sign_md
= NULL
;
287 STACK_OF(OPENSSL_STRING
) *rr_to
= NULL
, *rr_from
= NULL
;
288 STACK_OF(OPENSSL_STRING
) *sksigners
= NULL
, *skkeys
= NULL
;
289 STACK_OF(X509
) *encerts
= sk_X509_new_null(), *other
= NULL
;
290 X509
*cert
= NULL
, *recip
= NULL
, *signer
= NULL
, *originator
= NULL
;
291 X509_STORE
*store
= NULL
;
292 X509_VERIFY_PARAM
*vpm
= X509_VERIFY_PARAM_new();
293 char *certfile
= NULL
, *keyfile
= NULL
, *contfile
= NULL
;
294 const char *CAfile
= NULL
, *CApath
= NULL
, *CAstore
= NULL
;
295 char *certsoutfile
= NULL
, *digestname
= NULL
, *wrapname
= NULL
;
296 int noCAfile
= 0, noCApath
= 0, noCAstore
= 0;
297 char *digesthex
= NULL
;
298 unsigned char *digestbin
= NULL
;
300 char *infile
= NULL
, *outfile
= NULL
, *rctfile
= NULL
;
301 char *passinarg
= NULL
, *passin
= NULL
, *signerfile
= NULL
;
302 char *originatorfile
= NULL
, *recipfile
= NULL
, *ciphername
= NULL
;
303 char *to
= NULL
, *from
= NULL
, *subject
= NULL
, *prog
;
304 cms_key_param
*key_first
= NULL
, *key_param
= NULL
;
305 int flags
= CMS_DETACHED
, binary_files
= 0;
306 int noout
= 0, print
= 0, keyidx
= -1, vpmtouched
= 0;
307 int informat
= FORMAT_SMIME
, outformat
= FORMAT_SMIME
;
308 int operation
= 0, ret
= 1, rr_print
= 0, rr_allorfirst
= -1;
309 int verify_retcode
= 0, rctformat
= FORMAT_SMIME
, keyform
= FORMAT_UNDEF
;
310 size_t secret_keylen
= 0, secret_keyidlen
= 0;
311 unsigned char *pwri_pass
= NULL
, *pwri_tmp
= NULL
;
312 unsigned char *secret_key
= NULL
, *secret_keyid
= NULL
;
314 const char *mime_eol
= "\n";
316 OSSL_LIB_CTX
*libctx
= app_get0_libctx();
318 if (encerts
== NULL
|| vpm
== NULL
)
321 opt_set_unknown_name("cipher");
322 prog
= opt_init(argc
, argv
, cms_options
);
323 while ((o
= opt_next()) != OPT_EOF
) {
328 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
331 opt_help(cms_options
);
335 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &informat
))
339 if (!opt_format(opt_arg(), OPT_FMT_PDS
, &outformat
))
347 operation
= SMIME_ENCRYPT
;
350 operation
= SMIME_DECRYPT
;
353 operation
= SMIME_SIGN
;
356 operation
= SMIME_VERIFY
;
359 operation
= SMIME_RESIGN
;
361 case OPT_SIGN_RECEIPT
:
362 operation
= SMIME_SIGN_RECEIPT
;
364 case OPT_VERIFY_RECEIPT
:
365 operation
= SMIME_VERIFY_RECEIPT
;
368 case OPT_VERIFY_RETCODE
:
371 case OPT_DIGEST_CREATE
:
372 operation
= SMIME_DIGEST_CREATE
;
375 digesthex
= opt_arg();
377 case OPT_DIGEST_VERIFY
:
378 operation
= SMIME_DIGEST_VERIFY
;
381 operation
= SMIME_COMPRESS
;
384 operation
= SMIME_UNCOMPRESS
;
387 operation
= SMIME_ENCRYPTED_ENCRYPT
;
390 operation
= SMIME_ENCRYPTED_DECRYPT
;
392 case OPT_DATA_CREATE
:
393 operation
= SMIME_DATA_CREATE
;
396 operation
= SMIME_DATA_OUT
;
399 operation
= SMIME_CMSOUT
;
402 case OPT_DEBUG_DECRYPT
:
403 flags
|= CMS_DEBUG_DECRYPT
;
409 flags
|= CMS_ASCIICRLF
;
412 flags
|= CMS_NOINTERN
;
415 flags
|= CMS_NO_SIGNER_CERT_VERIFY
;
418 flags
|= CMS_NOCERTS
;
424 flags
&= ~CMS_DETACHED
;
427 flags
|= CMS_NOSMIMECAP
;
436 flags
|= CMS_USE_KEYID
;
441 case OPT_NO_CONTENT_VERIFY
:
442 flags
|= CMS_NO_CONTENT_VERIFY
;
444 case OPT_NO_ATTR_VERIFY
:
445 flags
|= CMS_NO_ATTR_VERIFY
;
451 flags
&= ~CMS_STREAM
;
455 flags
|= CMS_CRLFEOL
;
470 if (!opt_format(opt_arg(),
471 OPT_FMT_PEMDER
| OPT_FMT_SMIME
, &rctformat
))
475 certfile
= opt_arg();
499 contfile
= opt_arg();
503 && (rr_from
= sk_OPENSSL_STRING_new_null()) == NULL
)
505 sk_OPENSSL_STRING_push(rr_from
, opt_arg());
509 && (rr_to
= sk_OPENSSL_STRING_new_null()) == NULL
)
511 sk_OPENSSL_STRING_push(rr_to
, opt_arg());
517 if (!set_nameopt(opt_arg()))
521 if (secret_key
!= NULL
) {
522 BIO_printf(bio_err
, "Invalid key (supplied twice) %s\n",
526 secret_key
= OPENSSL_hexstr2buf(opt_arg(), <mp
);
527 if (secret_key
== NULL
) {
528 BIO_printf(bio_err
, "Invalid key %s\n", opt_arg());
531 secret_keylen
= (size_t)ltmp
;
533 case OPT_SECRETKEYID
:
534 if (secret_keyid
!= NULL
) {
535 BIO_printf(bio_err
, "Invalid id (supplied twice) %s\n",
539 secret_keyid
= OPENSSL_hexstr2buf(opt_arg(), <mp
);
540 if (secret_keyid
== NULL
) {
541 BIO_printf(bio_err
, "Invalid id %s\n", opt_arg());
544 secret_keyidlen
= (size_t)ltmp
;
546 case OPT_PWRI_PASSWORD
:
547 pwri_pass
= (unsigned char *)opt_arg();
549 case OPT_ECONTENT_TYPE
:
550 if (econtent_type
!= NULL
) {
551 BIO_printf(bio_err
, "Invalid OID (supplied twice) %s\n",
555 econtent_type
= OBJ_txt2obj(opt_arg(), 0);
556 if (econtent_type
== NULL
) {
557 BIO_printf(bio_err
, "Invalid OID %s\n", opt_arg());
562 e
= setup_engine(opt_arg(), 0);
565 passinarg
= opt_arg();
577 certsoutfile
= opt_arg();
580 digestname
= opt_arg();
583 /* If previous -signer argument add signer to list */
584 if (signerfile
!= NULL
) {
585 if (sksigners
== NULL
586 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
588 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
590 keyfile
= signerfile
;
592 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
594 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
597 signerfile
= opt_arg();
600 originatorfile
= opt_arg();
603 /* If previous -inkey argument add signer to list */
604 if (keyfile
!= NULL
) {
605 if (signerfile
== NULL
) {
606 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
609 if (sksigners
== NULL
610 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
612 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
615 && (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
617 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
622 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &keyform
))
626 if (operation
== SMIME_ENCRYPT
) {
627 cert
= load_cert(opt_arg(), FORMAT_UNDEF
,
628 "recipient certificate file");
631 sk_X509_push(encerts
, cert
);
634 recipfile
= opt_arg();
638 ciphername
= opt_unknown();
642 if (operation
== SMIME_ENCRYPT
) {
643 if (sk_X509_num(encerts
) > 0)
644 keyidx
+= sk_X509_num(encerts
);
646 if (keyfile
!= NULL
|| signerfile
!= NULL
)
649 keyidx
+= sk_OPENSSL_STRING_num(skkeys
);
652 BIO_printf(bio_err
, "No key specified\n");
655 if (key_param
== NULL
|| key_param
->idx
!= keyidx
) {
656 cms_key_param
*nparam
;
657 nparam
= app_malloc(sizeof(*nparam
), "key param buffer");
658 if ((nparam
->param
= sk_OPENSSL_STRING_new_null()) == NULL
) {
659 OPENSSL_free(nparam
);
662 nparam
->idx
= keyidx
;
664 if (key_first
== NULL
)
667 key_param
->next
= nparam
;
670 sk_OPENSSL_STRING_push(key_param
->param
, opt_arg());
673 if (!opt_verify(o
, vpm
))
682 if (!opt_provider(o
))
686 conf
= app_load_config_modules(opt_arg());
691 wrapname
= opt_arg();
693 case OPT_AES128_WRAP
:
694 case OPT_AES192_WRAP
:
695 case OPT_AES256_WRAP
:
697 wrapname
= opt_flag() + 1;
701 if (!app_RAND_load())
704 if (digestname
!= NULL
) {
705 if (!opt_md(digestname
, &sign_md
))
708 if (!opt_cipher_any(ciphername
, &cipher
))
710 if (wrapname
!= NULL
) {
711 if (!opt_cipher_any(wrapname
, &wrap_cipher
))
715 /* Remaining args are files to process. */
716 argc
= opt_num_rest();
719 if ((rr_allorfirst
!= -1 || rr_from
!= NULL
) && rr_to
== NULL
) {
720 BIO_puts(bio_err
, "No Signed Receipts Recipients\n");
724 if (!(operation
& SMIME_SIGNERS
) && (rr_to
!= NULL
|| rr_from
!= NULL
)) {
725 BIO_puts(bio_err
, "Signed receipts only allowed with -sign\n");
728 if (!(operation
& SMIME_SIGNERS
) && (skkeys
!= NULL
|| sksigners
!= NULL
)) {
729 BIO_puts(bio_err
, "Multiple signers or keys not allowed\n");
733 if ((flags
& CMS_CADES
) != 0) {
734 if ((flags
& CMS_NOATTR
) != 0) {
735 BIO_puts(bio_err
, "Incompatible options: "
736 "CAdES requires signed attributes\n");
739 if (operation
== SMIME_VERIFY
740 && (flags
& (CMS_NO_SIGNER_CERT_VERIFY
| CMS_NO_ATTR_VERIFY
)) != 0) {
741 BIO_puts(bio_err
, "Incompatible options: CAdES validation requires"
742 " certs and signed attributes validations\n");
747 if (operation
& SMIME_SIGNERS
) {
748 if (keyfile
!= NULL
&& signerfile
== NULL
) {
749 BIO_puts(bio_err
, "Illegal -inkey without -signer\n");
752 /* Check to see if any final signer needs to be appended */
753 if (signerfile
!= NULL
) {
754 if (sksigners
== NULL
755 && (sksigners
= sk_OPENSSL_STRING_new_null()) == NULL
)
757 sk_OPENSSL_STRING_push(sksigners
, signerfile
);
758 if (skkeys
== NULL
&& (skkeys
= sk_OPENSSL_STRING_new_null()) == NULL
)
761 keyfile
= signerfile
;
762 sk_OPENSSL_STRING_push(skkeys
, keyfile
);
764 if (sksigners
== NULL
) {
765 BIO_printf(bio_err
, "No signer certificate specified\n");
770 } else if (operation
== SMIME_DECRYPT
) {
771 if (recipfile
== NULL
&& keyfile
== NULL
772 && secret_key
== NULL
&& pwri_pass
== NULL
) {
774 "No recipient certificate or key specified\n");
777 } else if (operation
== SMIME_ENCRYPT
) {
778 if (*argv
== NULL
&& secret_key
== NULL
779 && pwri_pass
== NULL
&& sk_X509_num(encerts
) <= 0) {
780 BIO_printf(bio_err
, "No recipient(s) certificate(s) specified\n");
783 } else if (!operation
) {
784 BIO_printf(bio_err
, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\n");
788 if (!app_passwd(passinarg
, NULL
, &passin
, NULL
)) {
789 BIO_printf(bio_err
, "Error getting password\n");
795 if ((operation
& SMIME_SIGNERS
) == 0) {
796 if ((flags
& CMS_DETACHED
) == 0)
798 "Warning: -nodetach option is ignored for non-signing operation\n");
800 flags
&= ~CMS_DETACHED
;
802 if ((operation
& SMIME_IP
) == 0 && contfile
!= NULL
)
804 "Warning: -contfile option is ignored for the given operation\n");
806 if ((flags
& CMS_BINARY
) != 0) {
807 if (!(operation
& SMIME_OP
))
808 outformat
= FORMAT_BINARY
;
809 if (!(operation
& SMIME_IP
))
810 informat
= FORMAT_BINARY
;
811 if ((operation
& SMIME_SIGNERS
) != 0 && (flags
& CMS_DETACHED
) != 0)
813 if ((operation
& SMIME_IP
) != 0 && contfile
== NULL
)
817 if (operation
== SMIME_ENCRYPT
) {
819 #ifndef OPENSSL_NO_DES
820 cipher
= (EVP_CIPHER
*)EVP_des_ede3_cbc();
822 BIO_printf(bio_err
, "No cipher selected\n");
827 if (secret_key
&& !secret_keyid
) {
828 BIO_printf(bio_err
, "No secret key id\n");
833 if (operation
== SMIME_ENCRYPT
) {
834 for (; *argv
!= NULL
; argv
++) {
835 cert
= load_cert(*argv
, FORMAT_UNDEF
,
836 "recipient certificate file");
839 sk_X509_push(encerts
, cert
);
843 BIO_printf(bio_err
, "Warning: recipient certificate file parameters ignored for operation other than -encrypt\n");
848 if (certfile
!= NULL
) {
849 if (!load_certs(certfile
, 0, &other
, NULL
, "certificate file")) {
850 ERR_print_errors(bio_err
);
855 if (recipfile
!= NULL
&& (operation
== SMIME_DECRYPT
)) {
856 if ((recip
= load_cert(recipfile
, FORMAT_UNDEF
,
857 "recipient certificate file")) == NULL
) {
858 ERR_print_errors(bio_err
);
863 if (originatorfile
!= NULL
) {
864 if ((originator
= load_cert(originatorfile
, FORMAT_UNDEF
,
865 "originator certificate file")) == NULL
) {
866 ERR_print_errors(bio_err
);
871 if (operation
== SMIME_SIGN_RECEIPT
) {
872 if ((signer
= load_cert(signerfile
, FORMAT_UNDEF
,
873 "receipt signer certificate file")) == NULL
) {
874 ERR_print_errors(bio_err
);
879 if ((operation
== SMIME_DECRYPT
) || (operation
== SMIME_ENCRYPT
)) {
882 } else if ((operation
== SMIME_SIGN
) || (operation
== SMIME_SIGN_RECEIPT
)) {
884 keyfile
= signerfile
;
889 if (keyfile
!= NULL
) {
890 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key");
895 if (digesthex
!= NULL
) {
896 if (operation
!= SMIME_SIGN
) {
898 "Cannot use -digest for non-signing operation\n");
902 || (flags
& CMS_DETACHED
) == 0
903 || (flags
& CMS_STREAM
) != 0) {
905 "Cannot use -digest when -in, -nodetach or streaming is used\n");
908 digestbin
= OPENSSL_hexstr2buf(digesthex
, &digestlen
);
909 if (digestbin
== NULL
) {
911 "Invalid hex value after -digest\n");
915 in
= bio_open_default(infile
, 'r',
916 binary_files
? FORMAT_BINARY
: informat
);
921 if (operation
& SMIME_IP
) {
922 cms
= load_content_info(informat
, in
, flags
, &indata
, "SMIME");
925 if (contfile
!= NULL
) {
927 if ((indata
= BIO_new_file(contfile
, "rb")) == NULL
) {
928 BIO_printf(bio_err
, "Can't read content file %s\n", contfile
);
932 if (certsoutfile
!= NULL
) {
933 STACK_OF(X509
) *allcerts
;
934 allcerts
= CMS_get1_certs(cms
);
935 if (!save_certs(certsoutfile
, allcerts
)) {
937 "Error writing certs to %s\n", certsoutfile
);
941 OSSL_STACK_OF_X509_free(allcerts
);
945 if (rctfile
!= NULL
) {
946 char *rctmode
= (rctformat
== FORMAT_ASN1
) ? "rb" : "r";
948 if ((rctin
= BIO_new_file(rctfile
, rctmode
)) == NULL
) {
949 BIO_printf(bio_err
, "Can't open receipt file %s\n", rctfile
);
953 rcms
= load_content_info(rctformat
, rctin
, 0, NULL
, "receipt");
958 out
= bio_open_default(outfile
, 'w',
959 binary_files
? FORMAT_BINARY
: outformat
);
963 if ((operation
== SMIME_VERIFY
) || (operation
== SMIME_VERIFY_RECEIPT
)) {
964 if ((store
= setup_verify(CAfile
, noCAfile
, CApath
, noCApath
,
965 CAstore
, noCAstore
)) == NULL
)
967 X509_STORE_set_verify_cb(store
, cms_cb
);
969 X509_STORE_set1_param(store
, vpm
);
974 if (operation
== SMIME_DATA_CREATE
) {
975 cms
= CMS_data_create_ex(in
, flags
, libctx
, app_get0_propq());
976 } else if (operation
== SMIME_DIGEST_CREATE
) {
977 cms
= CMS_digest_create_ex(in
, sign_md
, flags
, libctx
, app_get0_propq());
978 } else if (operation
== SMIME_COMPRESS
) {
979 cms
= CMS_compress(in
, -1, flags
);
980 } else if (operation
== SMIME_ENCRYPT
) {
982 flags
|= CMS_PARTIAL
;
983 cms
= CMS_encrypt_ex(NULL
, in
, cipher
, flags
, libctx
, app_get0_propq());
986 for (i
= 0; i
< sk_X509_num(encerts
); i
++) {
987 CMS_RecipientInfo
*ri
;
988 cms_key_param
*kparam
;
989 int tflags
= flags
| CMS_KEY_PARAM
;
990 /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */
992 X509
*x
= sk_X509_value(encerts
, i
);
995 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
996 if (kparam
->idx
== i
) {
1000 ri
= CMS_add1_recipient(cms
, x
, key
, originator
, tflags
);
1004 pctx
= CMS_RecipientInfo_get0_pkey_ctx(ri
);
1005 if (kparam
!= NULL
) {
1006 if (!cms_set_pkey_param(pctx
, kparam
->param
))
1010 res
= EVP_PKEY_CTX_ctrl(pctx
, -1, -1,
1011 EVP_PKEY_CTRL_CIPHER
,
1012 EVP_CIPHER_get_nid(cipher
), NULL
);
1013 if (res
<= 0 && res
!= -2)
1016 if (CMS_RecipientInfo_type(ri
) == CMS_RECIPINFO_AGREE
1017 && wrap_cipher
!= NULL
) {
1018 EVP_CIPHER_CTX
*wctx
;
1019 wctx
= CMS_RecipientInfo_kari_get0_ctx(ri
);
1020 EVP_EncryptInit_ex(wctx
, wrap_cipher
, NULL
, NULL
, NULL
);
1024 if (secret_key
!= NULL
) {
1025 if (!CMS_add0_recipient_key(cms
, NID_undef
,
1026 secret_key
, secret_keylen
,
1027 secret_keyid
, secret_keyidlen
,
1030 /* NULL these because call absorbs them */
1032 secret_keyid
= NULL
;
1034 if (pwri_pass
!= NULL
) {
1035 pwri_tmp
= (unsigned char *)OPENSSL_strdup((char *)pwri_pass
);
1036 if (pwri_tmp
== NULL
)
1038 if (CMS_add0_recipient_password(cms
,
1039 -1, NID_undef
, NID_undef
,
1040 pwri_tmp
, -1, NULL
) == NULL
)
1044 if (!(flags
& CMS_STREAM
)) {
1045 if (!CMS_final(cms
, in
, NULL
, flags
))
1048 } else if (operation
== SMIME_ENCRYPTED_ENCRYPT
) {
1049 cms
= CMS_EncryptedData_encrypt_ex(in
, cipher
, secret_key
,
1050 secret_keylen
, flags
, libctx
, app_get0_propq());
1052 } else if (operation
== SMIME_SIGN_RECEIPT
) {
1053 CMS_ContentInfo
*srcms
= NULL
;
1054 STACK_OF(CMS_SignerInfo
) *sis
;
1056 sis
= CMS_get0_SignerInfos(cms
);
1059 si
= sk_CMS_SignerInfo_value(sis
, 0);
1060 srcms
= CMS_sign_receipt(si
, signer
, key
, other
, flags
);
1063 CMS_ContentInfo_free(cms
);
1065 } else if (operation
& SMIME_SIGNERS
) {
1068 * If detached data content and not signing pre-computed digest, we
1069 * enable streaming if S/MIME output format.
1071 if (operation
== SMIME_SIGN
) {
1073 if ((flags
& CMS_DETACHED
) != 0 && digestbin
== NULL
) {
1074 if (outformat
== FORMAT_SMIME
)
1075 flags
|= CMS_STREAM
;
1077 flags
|= CMS_PARTIAL
;
1078 cms
= CMS_sign_ex(NULL
, NULL
, other
, in
, flags
, libctx
, app_get0_propq());
1081 if (econtent_type
!= NULL
)
1082 CMS_set1_eContentType(cms
, econtent_type
);
1085 && ((rr
= make_receipt_request(rr_to
, rr_allorfirst
, rr_from
))
1087 BIO_puts(bio_err
, "Signed Receipt Request Creation Error\n");
1091 flags
|= CMS_REUSE_DIGEST
;
1093 for (i
= 0; i
< sk_OPENSSL_STRING_num(sksigners
); i
++) {
1095 cms_key_param
*kparam
;
1097 signerfile
= sk_OPENSSL_STRING_value(sksigners
, i
);
1098 keyfile
= sk_OPENSSL_STRING_value(skkeys
, i
);
1100 signer
= load_cert(signerfile
, FORMAT_UNDEF
, "signer certificate");
1101 if (signer
== NULL
) {
1105 key
= load_key(keyfile
, keyform
, 0, passin
, e
, "signing key");
1111 for (kparam
= key_first
; kparam
; kparam
= kparam
->next
) {
1112 if (kparam
->idx
== i
) {
1113 tflags
|= CMS_KEY_PARAM
;
1117 si
= CMS_add1_signer(cms
, signer
, key
, sign_md
, tflags
);
1120 if (kparam
!= NULL
) {
1122 pctx
= CMS_SignerInfo_get0_pkey_ctx(si
);
1123 if (!cms_set_pkey_param(pctx
, kparam
->param
))
1126 if (rr
!= NULL
&& !CMS_add1_ReceiptRequest(si
, rr
))
1133 /* If not streaming or resigning finalize structure */
1134 if (operation
== SMIME_SIGN
&& digestbin
!= NULL
1135 && (flags
& CMS_STREAM
) == 0) {
1136 /* Use pre-computed digest instead of content */
1137 if (!CMS_final_digest(cms
, digestbin
, digestlen
, NULL
, flags
))
1139 } else if (operation
== SMIME_SIGN
&& (flags
& CMS_STREAM
) == 0) {
1140 if (!CMS_final(cms
, in
, NULL
, flags
))
1146 BIO_printf(bio_err
, "Error creating CMS structure\n");
1151 if (operation
== SMIME_DECRYPT
) {
1152 if (flags
& CMS_DEBUG_DECRYPT
)
1153 CMS_decrypt(cms
, NULL
, NULL
, NULL
, NULL
, flags
);
1155 if (secret_key
!= NULL
) {
1156 if (!CMS_decrypt_set1_key(cms
,
1157 secret_key
, secret_keylen
,
1158 secret_keyid
, secret_keyidlen
)) {
1159 BIO_puts(bio_err
, "Error decrypting CMS using secret key\n");
1165 if (!CMS_decrypt_set1_pkey_and_peer(cms
, key
, recip
, originator
)) {
1166 BIO_puts(bio_err
, "Error decrypting CMS using private key\n");
1171 if (pwri_pass
!= NULL
) {
1172 if (!CMS_decrypt_set1_password(cms
, pwri_pass
, -1)) {
1173 BIO_puts(bio_err
, "Error decrypting CMS using password\n");
1178 if (!CMS_decrypt(cms
, NULL
, NULL
, indata
, out
, flags
)) {
1179 BIO_printf(bio_err
, "Error decrypting CMS structure\n");
1182 } else if (operation
== SMIME_DATA_OUT
) {
1183 if (!CMS_data(cms
, out
, flags
))
1185 } else if (operation
== SMIME_UNCOMPRESS
) {
1186 if (!CMS_uncompress(cms
, indata
, out
, flags
))
1188 } else if (operation
== SMIME_DIGEST_VERIFY
) {
1189 if (CMS_digest_verify(cms
, indata
, out
, flags
) > 0) {
1190 BIO_printf(bio_err
, "Verification successful\n");
1192 BIO_printf(bio_err
, "Verification failure\n");
1195 } else if (operation
== SMIME_ENCRYPTED_DECRYPT
) {
1196 if (!CMS_EncryptedData_decrypt(cms
, secret_key
, secret_keylen
,
1197 indata
, out
, flags
))
1199 } else if (operation
== SMIME_VERIFY
) {
1200 if (CMS_verify(cms
, other
, store
, indata
, out
, flags
) > 0) {
1201 BIO_printf(bio_err
, "%s Verification successful\n",
1202 (flags
& CMS_CADES
) != 0 ? "CAdES" : "CMS");
1204 BIO_printf(bio_err
, "%s Verification failure\n",
1205 (flags
& CMS_CADES
) != 0 ? "CAdES" : "CMS");
1207 ret
= verify_err
+ 32;
1210 if (signerfile
!= NULL
) {
1211 STACK_OF(X509
) *signers
= CMS_get0_signers(cms
);
1213 if (!save_certs(signerfile
, signers
)) {
1215 "Error writing signers to %s\n", signerfile
);
1219 sk_X509_free(signers
);
1222 receipt_request_print(cms
);
1224 } else if (operation
== SMIME_VERIFY_RECEIPT
) {
1225 if (CMS_verify_receipt(rcms
, cms
, other
, store
, flags
) > 0) {
1226 BIO_printf(bio_err
, "Verification successful\n");
1228 BIO_printf(bio_err
, "Verification failure\n");
1234 ASN1_PCTX
*pctx
= NULL
;
1235 if (get_nameopt() != XN_FLAG_ONELINE
) {
1236 pctx
= ASN1_PCTX_new();
1237 if (pctx
!= NULL
) { /* Print anyway if malloc failed */
1238 ASN1_PCTX_set_flags(pctx
, ASN1_PCTX_FLAGS_SHOW_ABSENT
);
1239 ASN1_PCTX_set_str_flags(pctx
, get_nameopt());
1240 ASN1_PCTX_set_nm_flags(pctx
, get_nameopt());
1243 CMS_ContentInfo_print_ctx(out
, cms
, 0, pctx
);
1244 ASN1_PCTX_free(pctx
);
1246 } else if (outformat
== FORMAT_SMIME
) {
1248 BIO_printf(out
, "To: %s%s", to
, mime_eol
);
1250 BIO_printf(out
, "From: %s%s", from
, mime_eol
);
1252 BIO_printf(out
, "Subject: %s%s", subject
, mime_eol
);
1253 if (operation
== SMIME_RESIGN
)
1254 ret
= SMIME_write_CMS(out
, cms
, indata
, flags
);
1256 ret
= SMIME_write_CMS(out
, cms
, in
, flags
);
1257 } else if (outformat
== FORMAT_PEM
) {
1258 ret
= PEM_write_bio_CMS_stream(out
, cms
, in
, flags
);
1259 } else if (outformat
== FORMAT_ASN1
) {
1260 ret
= i2d_CMS_bio_stream(out
, cms
, in
, flags
);
1262 BIO_printf(bio_err
, "Bad output format for CMS file\n");
1273 ERR_print_errors(bio_err
);
1274 OSSL_STACK_OF_X509_free(encerts
);
1275 OSSL_STACK_OF_X509_free(other
);
1276 X509_VERIFY_PARAM_free(vpm
);
1277 sk_OPENSSL_STRING_free(sksigners
);
1278 sk_OPENSSL_STRING_free(skkeys
);
1279 OPENSSL_free(secret_key
);
1280 OPENSSL_free(secret_keyid
);
1281 OPENSSL_free(pwri_tmp
);
1282 ASN1_OBJECT_free(econtent_type
);
1283 CMS_ReceiptRequest_free(rr
);
1284 sk_OPENSSL_STRING_free(rr_to
);
1285 sk_OPENSSL_STRING_free(rr_from
);
1286 for (key_param
= key_first
; key_param
;) {
1287 cms_key_param
*tparam
;
1288 sk_OPENSSL_STRING_free(key_param
->param
);
1289 tparam
= key_param
->next
;
1290 OPENSSL_free(key_param
);
1293 X509_STORE_free(store
);
1298 EVP_CIPHER_free(cipher
);
1299 EVP_CIPHER_free(wrap_cipher
);
1300 EVP_MD_free(sign_md
);
1301 CMS_ContentInfo_free(cms
);
1302 CMS_ContentInfo_free(rcms
);
1308 OPENSSL_free(digestbin
);
1309 OPENSSL_free(passin
);
1314 static int save_certs(char *signerfile
, STACK_OF(X509
) *signers
)
1318 if (signerfile
== NULL
)
1320 tmp
= BIO_new_file(signerfile
, "w");
1323 for (i
= 0; i
< sk_X509_num(signers
); i
++)
1324 PEM_write_bio_X509(tmp
, sk_X509_value(signers
, i
));
1329 /* Minimal callback just to output policy info (if any) */
1331 static int cms_cb(int ok
, X509_STORE_CTX
*ctx
)
1335 error
= X509_STORE_CTX_get_error(ctx
);
1339 if ((error
!= X509_V_ERR_NO_EXPLICIT_POLICY
)
1340 && ((error
!= X509_V_OK
) || (ok
!= 2)))
1343 policies_print(ctx
);
1349 static void gnames_stack_print(STACK_OF(GENERAL_NAMES
) *gns
)
1351 STACK_OF(GENERAL_NAME
) *gens
;
1355 for (i
= 0; i
< sk_GENERAL_NAMES_num(gns
); i
++) {
1356 gens
= sk_GENERAL_NAMES_value(gns
, i
);
1357 for (j
= 0; j
< sk_GENERAL_NAME_num(gens
); j
++) {
1358 gen
= sk_GENERAL_NAME_value(gens
, j
);
1359 BIO_puts(bio_err
, " ");
1360 GENERAL_NAME_print(bio_err
, gen
);
1361 BIO_puts(bio_err
, "\n");
1367 static void receipt_request_print(CMS_ContentInfo
*cms
)
1369 STACK_OF(CMS_SignerInfo
) *sis
;
1371 CMS_ReceiptRequest
*rr
;
1373 STACK_OF(GENERAL_NAMES
) *rto
, *rlist
;
1376 sis
= CMS_get0_SignerInfos(cms
);
1377 for (i
= 0; i
< sk_CMS_SignerInfo_num(sis
); i
++) {
1378 si
= sk_CMS_SignerInfo_value(sis
, i
);
1379 rv
= CMS_get1_ReceiptRequest(si
, &rr
);
1380 BIO_printf(bio_err
, "Signer %d:\n", i
+ 1);
1382 BIO_puts(bio_err
, " No Receipt Request\n");
1383 } else if (rv
< 0) {
1384 BIO_puts(bio_err
, " Receipt Request Parse Error\n");
1385 ERR_print_errors(bio_err
);
1389 CMS_ReceiptRequest_get0_values(rr
, &scid
, &allorfirst
,
1391 BIO_puts(bio_err
, " Signed Content ID:\n");
1392 idlen
= ASN1_STRING_length(scid
);
1393 id
= (const char *)ASN1_STRING_get0_data(scid
);
1394 BIO_dump_indent(bio_err
, id
, idlen
, 4);
1395 BIO_puts(bio_err
, " Receipts From");
1396 if (rlist
!= NULL
) {
1397 BIO_puts(bio_err
, " List:\n");
1398 gnames_stack_print(rlist
);
1399 } else if (allorfirst
== 1) {
1400 BIO_puts(bio_err
, ": First Tier\n");
1401 } else if (allorfirst
== 0) {
1402 BIO_puts(bio_err
, ": All\n");
1404 BIO_printf(bio_err
, " Unknown (%d)\n", allorfirst
);
1406 BIO_puts(bio_err
, " Receipts To:\n");
1407 gnames_stack_print(rto
);
1409 CMS_ReceiptRequest_free(rr
);
1413 static STACK_OF(GENERAL_NAMES
) *make_names_stack(STACK_OF(OPENSSL_STRING
) *ns
)
1416 STACK_OF(GENERAL_NAMES
) *ret
;
1417 GENERAL_NAMES
*gens
= NULL
;
1418 GENERAL_NAME
*gen
= NULL
;
1419 ret
= sk_GENERAL_NAMES_new_null();
1422 for (i
= 0; i
< sk_OPENSSL_STRING_num(ns
); i
++) {
1423 char *str
= sk_OPENSSL_STRING_value(ns
, i
);
1424 gen
= a2i_GENERAL_NAME(NULL
, NULL
, NULL
, GEN_EMAIL
, str
, 0);
1427 gens
= GENERAL_NAMES_new();
1430 if (!sk_GENERAL_NAME_push(gens
, gen
))
1433 if (!sk_GENERAL_NAMES_push(ret
, gens
))
1441 sk_GENERAL_NAMES_pop_free(ret
, GENERAL_NAMES_free
);
1442 GENERAL_NAMES_free(gens
);
1443 GENERAL_NAME_free(gen
);
1447 static CMS_ReceiptRequest
1448 *make_receipt_request(STACK_OF(OPENSSL_STRING
) *rr_to
, int rr_allorfirst
,
1449 STACK_OF(OPENSSL_STRING
) *rr_from
)
1451 STACK_OF(GENERAL_NAMES
) *rct_to
= NULL
, *rct_from
= NULL
;
1453 rct_to
= make_names_stack(rr_to
);
1456 if (rr_from
!= NULL
) {
1457 rct_from
= make_names_stack(rr_from
);
1458 if (rct_from
== NULL
)
1463 return CMS_ReceiptRequest_create0_ex(NULL
, -1, rr_allorfirst
, rct_from
,
1464 rct_to
, app_get0_libctx());
1466 sk_GENERAL_NAMES_pop_free(rct_to
, GENERAL_NAMES_free
);
1470 static int cms_set_pkey_param(EVP_PKEY_CTX
*pctx
,
1471 STACK_OF(OPENSSL_STRING
) *param
)
1475 if (sk_OPENSSL_STRING_num(param
) <= 0)
1477 for (i
= 0; i
< sk_OPENSSL_STRING_num(param
); i
++) {
1478 keyopt
= sk_OPENSSL_STRING_value(param
, i
);
1479 if (pkey_ctrl_string(pctx
, keyopt
) <= 0) {
1480 BIO_printf(bio_err
, "parameter error \"%s\"\n", keyopt
);
1481 ERR_print_errors(bio_err
);