]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/cms.c
Copyright year updates
[thirdparty/openssl.git] / apps / cms.c
CommitLineData
0f113f3e 1/*
da1c088f 2 * Copyright 2008-2023 The OpenSSL Project Authors. All Rights Reserved.
8931b30d 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
8931b30d
DSH
8 */
9
10/* CMS utility function */
11
12#include <stdio.h>
13#include <string.h>
14#include "apps.h"
dab2cd68 15#include "progs.h"
8931b30d 16
5c42f7aa
RS
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>
8931b30d 23
8931b30d 24static int save_certs(char *signerfile, STACK_OF(X509) *signers);
7f50d9a4 25static int cms_cb(int ok, X509_STORE_CTX *ctx);
ecf3a1fb 26static void receipt_request_print(CMS_ContentInfo *cms);
1b96cc70
DDO
27static CMS_ReceiptRequest
28*make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
29 STACK_OF(OPENSSL_STRING) *rr_from);
02498cc8 30static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
0f113f3e
MC
31 STACK_OF(OPENSSL_STRING) *param);
32
340cf875
DDO
33#define SMIME_OP 0x100
34#define SMIME_IP 0x200
35#define SMIME_SIGNERS 0x400
5c42f7aa
RS
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)
340cf875
DDO
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)
5c42f7aa 44#define SMIME_DIGEST_VERIFY (9 | SMIME_IP)
340cf875 45#define SMIME_COMPRESS (10 | SMIME_OP)
5c42f7aa 46#define SMIME_UNCOMPRESS (11 | SMIME_IP)
340cf875 47#define SMIME_ENCRYPTED_ENCRYPT (12 | SMIME_OP)
5c42f7aa 48#define SMIME_ENCRYPTED_DECRYPT (13 | SMIME_IP)
340cf875
DDO
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)
8931b30d 52
df2ee0e2 53static int verify_err = 0;
db50661f 54
02498cc8
DSH
55typedef struct cms_key_param_st cms_key_param;
56
0f113f3e
MC
57struct cms_key_param_st {
58 int idx;
59 STACK_OF(OPENSSL_STRING) *param;
60 cms_key_param *next;
61};
02498cc8 62
7e1b7485 63typedef enum OPTION_choice {
b0f96018 64 OPT_COMMON,
7e1b7485 65 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENCRYPT,
e85d19c6 66 OPT_DECRYPT, OPT_SIGN, OPT_CADES, OPT_SIGN_RECEIPT, OPT_RESIGN,
7e1b7485
RS
67 OPT_VERIFY, OPT_VERIFY_RETCODE, OPT_VERIFY_RECEIPT,
68 OPT_CMSOUT, OPT_DATA_OUT, OPT_DATA_CREATE, OPT_DIGEST_VERIFY,
07342bad 69 OPT_DIGEST, OPT_DIGEST_CREATE, OPT_COMPRESS, OPT_UNCOMPRESS,
7e1b7485
RS
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,
28aef3d9 74 OPT_NOINDEF, OPT_CRLFEOL, OPT_NOOUT, OPT_RR_PRINT,
7e1b7485 75 OPT_RR_ALL, OPT_RR_FIRST, OPT_RCTFORM, OPT_CERTFILE, OPT_CAFILE,
fd3397fc 76 OPT_CAPATH, OPT_CASTORE, OPT_NOCAPATH, OPT_NOCAFILE, OPT_NOCASTORE,
6ec351f4 77 OPT_CONTENT, OPT_PRINT, OPT_NAMEOPT,
2b6bcb70 78 OPT_SECRETKEY, OPT_SECRETKEYID, OPT_PWRI_PASSWORD, OPT_ECONTENT_TYPE,
3ee1eac2 79 OPT_PASSIN, OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP,
7e1b7485
RS
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,
71434aed 82 OPT_3DES_WRAP, OPT_WRAP, OPT_ENGINE,
3ee1eac2 83 OPT_R_ENUM,
c1669f41 84 OPT_PROV_ENUM, OPT_CONFIG,
7e1b7485 85 OPT_V_ENUM,
71434aed
DB
86 OPT_CIPHER,
87 OPT_ORIGINATOR
7e1b7485
RS
88} OPTION_CHOICE;
89
44c83ebd 90const OPTIONS cms_options[] = {
92de469f 91 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
340cf875 92 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
93
94 OPT_SECTION("General"),
7e1b7485
RS
95 {"in", OPT_IN, '<', "Input file"},
96 {"out", OPT_OUT, '>', "Output file"},
c1669f41 97 OPT_CONFIG_OPTION,
5388f986 98
340cf875 99 OPT_SECTION("Operation"),
7e1b7485
RS
100 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
101 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
102 {"sign", OPT_SIGN, '-', "Sign message"},
7e1b7485 103 {"verify", OPT_VERIFY, '-', "Verify signed message"},
340cf875
DDO
104 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
105 {"sign_receipt", OPT_SIGN_RECEIPT, '-',
106 "Generate a signed receipt for a message"},
38546024 107 {"verify_receipt", OPT_VERIFY_RECEIPT, '<',
340cf875 108 "Verify receipts; exit if receipt signatures do not verify"},
07342bad 109 {"digest", OPT_DIGEST, 's', "Sign a pre-computed digest in hex notation"},
38546024 110 {"digest_create", OPT_DIGEST_CREATE, '-',
340cf875
DDO
111 "Create a CMS \"DigestedData\" object"},
112 {"digest_verify", OPT_DIGEST_VERIFY, '-',
113 "Verify a CMS \"DigestedData\" object and output it"},
38546024 114 {"compress", OPT_COMPRESS, '-', "Create a CMS \"CompressedData\" object"},
5388f986 115 {"uncompress", OPT_UNCOMPRESS, '-',
340cf875 116 "Uncompress a CMS \"CompressedData\" object"},
38546024 117 {"EncryptedData_encrypt", OPT_ED_ENCRYPT, '-',
340cf875
DDO
118 "Create CMS \"EncryptedData\" object using symmetric key"},
119 {"EncryptedData_decrypt", OPT_ED_DECRYPT, '-',
120 "Decrypt CMS \"EncryptedData\" object using symmetric key"},
5388f986 121 {"data_create", OPT_DATA_CREATE, '-', "Create a CMS \"Data\" object"},
340cf875 122 {"data_out", OPT_DATA_OUT, '-', "Copy CMS \"Data\" object to output"},
5388f986 123 {"cmsout", OPT_CMSOUT, '-', "Output CMS structure"},
5388f986 124
340cf875
DDO
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"},
12d56b29 129 {"rctform", OPT_RCTFORM, 'F', "Receipt file format"},
340cf875
DDO
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"},
5388f986 139
340cf875
DDO
140 OPT_SECTION("Keys and passwords"),
141 {"pwri_password", OPT_PWRI_PASSWORD, 's',
142 "Specific password for recipient"},
38546024 143 {"secretkey", OPT_SECRETKEY, 's',
340cf875 144 "Use specified hex-encoded key to decrypt/encrypt recipients or content"},
38546024 145 {"secretkeyid", OPT_SECRETKEYID, 's',
340cf875 146 "Identity of the -secretkey for CMS \"KEKRecipientInfo\" object"},
5388f986
RS
147 {"inkey", OPT_INKEY, 's',
148 "Input private key (if not signer or recipient)"},
340cf875 149 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
5388f986 150 {"keyopt", OPT_KEYOPT, 's', "Set public key parameters as n:v pairs"},
340cf875
DDO
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"},
155#endif
156 OPT_PROV_OPTIONS,
157 OPT_R_OPTIONS,
5388f986 158
340cf875 159 OPT_SECTION("Encryption and decryption"),
71434aed 160 {"originator", OPT_ORIGINATOR, 's', "Originator certificate file"},
340cf875
DDO
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"},
7e1b7485
RS
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"},
7e1b7485 171 {"des3-wrap", OPT_3DES_WRAP, '-', "Use 3DES-EDE to wrap key"},
340cf875
DDO
172 {"debug_decrypt", OPT_DEBUG_DECRYPT, '-',
173 "Disable MMA protection, return error if no recipient found (see doc)"},
5388f986 174
340cf875
DDO
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"},
92de469f 194
340cf875
DDO
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"},
219
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"},
228
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" },
237
238 OPT_V_OPTIONS,
9c3bcfa0 239 {NULL}
7e1b7485 240};
8931b30d 241
1b96cc70
DDO
242static CMS_ContentInfo *load_content_info(int informat, BIO *in, int flags,
243 BIO **indata, const char *name)
c1669f41
SL
244{
245 CMS_ContentInfo *ret, *ci;
246
1b96cc70 247 ret = CMS_ContentInfo_new_ex(app_get0_libctx(), app_get0_propq());
c1669f41
SL
248 if (ret == NULL) {
249 BIO_printf(bio_err, "Error allocating CMS_contentinfo\n");
250 return NULL;
251 }
252 switch (informat) {
253 case FORMAT_SMIME:
6b83d032 254 ci = SMIME_read_CMS_ex(in, flags, indata, &ret);
c1669f41
SL
255 break;
256 case FORMAT_PEM:
257 ci = PEM_read_bio_CMS(in, &ret, NULL, NULL);
258 break;
259 case FORMAT_ASN1:
260 ci = d2i_CMS_bio(in, &ret);
261 break;
262 default:
263 BIO_printf(bio_err, "Bad input format for %s\n", name);
264 goto err;
265 }
266 if (ci == NULL) {
267 BIO_printf(bio_err, "Error reading %s Content Info\n", name);
268 goto err;
269 }
270 return ret;
1b96cc70 271 err:
c1669f41
SL
272 CMS_ContentInfo_free(ret);
273 return NULL;
274}
275
7e1b7485 276int cms_main(int argc, char **argv)
0f113f3e 277{
c1669f41 278 CONF *conf = NULL;
7e1b7485 279 ASN1_OBJECT *econtent_type = NULL;
0f113f3e 280 BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
7e1b7485 281 CMS_ContentInfo *cms = NULL, *rcms = NULL;
0f113f3e 282 CMS_ReceiptRequest *rr = NULL;
7e1b7485
RS
283 ENGINE *e = NULL;
284 EVP_PKEY *key = NULL;
606a417f
RS
285 EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;
286 EVP_MD *sign_md = NULL;
7e1b7485
RS
287 STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
288 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
56c98a7d 289 STACK_OF(X509) *encerts = sk_X509_new_null(), *other = NULL;
71434aed 290 X509 *cert = NULL, *recip = NULL, *signer = NULL, *originator = NULL;
7e1b7485 291 X509_STORE *store = NULL;
56c98a7d 292 X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
7e1b7485 293 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
fd3397fc 294 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL;
a485561b 295 char *certsoutfile = NULL, *digestname = NULL, *wrapname = NULL;
fd3397fc 296 int noCAfile = 0, noCApath = 0, noCAstore = 0;
07342bad
VS
297 char *digesthex = NULL;
298 unsigned char *digestbin = NULL;
299 long digestlen = 0;
3ee1eac2 300 char *infile = NULL, *outfile = NULL, *rctfile = NULL;
03bbd346
RS
301 char *passinarg = NULL, *passin = NULL, *signerfile = NULL;
302 char *originatorfile = NULL, *recipfile = NULL, *ciphername = NULL;
7e1b7485
RS
303 char *to = NULL, *from = NULL, *subject = NULL, *prog;
304 cms_key_param *key_first = NULL, *key_param = NULL;
5771017d
DDO
305 int flags = CMS_DETACHED, binary_files = 0;
306 int noout = 0, print = 0, keyidx = -1, vpmtouched = 0;
0f113f3e 307 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
3ee1eac2 308 int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;
d382e796 309 int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_UNDEF;
0f113f3e 310 size_t secret_keylen = 0, secret_keyidlen = 0;
7e1b7485
RS
311 unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
312 unsigned char *secret_key = NULL, *secret_keyid = NULL;
313 long ltmp;
2197494d 314 const char *mime_eol = "\n";
7e1b7485 315 OPTION_CHOICE o;
b4250010 316 OSSL_LIB_CTX *libctx = app_get0_libctx();
0f113f3e 317
56c98a7d
DDO
318 if (encerts == NULL || vpm == NULL)
319 goto end;
0f113f3e 320
2c272447 321 opt_set_unknown_name("cipher");
7e1b7485
RS
322 prog = opt_init(argc, argv, cms_options);
323 while ((o = opt_next()) != OPT_EOF) {
324 switch (o) {
325 case OPT_EOF:
326 case OPT_ERR:
327 opthelp:
328 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
329 goto end;
330 case OPT_HELP:
331 opt_help(cms_options);
332 ret = 0;
333 goto end;
334 case OPT_INFORM:
f47e5647 335 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
7e1b7485
RS
336 goto opthelp;
337 break;
338 case OPT_OUTFORM:
f47e5647 339 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
7e1b7485
RS
340 goto opthelp;
341 break;
342 case OPT_OUT:
343 outfile = opt_arg();
344 break;
340cf875 345
7e1b7485 346 case OPT_ENCRYPT:
0f113f3e 347 operation = SMIME_ENCRYPT;
7e1b7485
RS
348 break;
349 case OPT_DECRYPT:
0f113f3e 350 operation = SMIME_DECRYPT;
7e1b7485
RS
351 break;
352 case OPT_SIGN:
0f113f3e 353 operation = SMIME_SIGN;
7e1b7485 354 break;
340cf875
DDO
355 case OPT_VERIFY:
356 operation = SMIME_VERIFY;
7e1b7485
RS
357 break;
358 case OPT_RESIGN:
0f113f3e 359 operation = SMIME_RESIGN;
7e1b7485 360 break;
340cf875
DDO
361 case OPT_SIGN_RECEIPT:
362 operation = SMIME_SIGN_RECEIPT;
7e1b7485
RS
363 break;
364 case OPT_VERIFY_RECEIPT:
0f113f3e 365 operation = SMIME_VERIFY_RECEIPT;
7e1b7485
RS
366 rctfile = opt_arg();
367 break;
340cf875
DDO
368 case OPT_VERIFY_RETCODE:
369 verify_retcode = 1;
7e1b7485 370 break;
340cf875
DDO
371 case OPT_DIGEST_CREATE:
372 operation = SMIME_DIGEST_CREATE;
7e1b7485 373 break;
07342bad
VS
374 case OPT_DIGEST:
375 digesthex = opt_arg();
376 break;
7e1b7485 377 case OPT_DIGEST_VERIFY:
0f113f3e 378 operation = SMIME_DIGEST_VERIFY;
7e1b7485 379 break;
7e1b7485 380 case OPT_COMPRESS:
0f113f3e 381 operation = SMIME_COMPRESS;
7e1b7485
RS
382 break;
383 case OPT_UNCOMPRESS:
0f113f3e 384 operation = SMIME_UNCOMPRESS;
7e1b7485 385 break;
340cf875
DDO
386 case OPT_ED_ENCRYPT:
387 operation = SMIME_ENCRYPTED_ENCRYPT;
388 break;
7e1b7485 389 case OPT_ED_DECRYPT:
0f113f3e 390 operation = SMIME_ENCRYPTED_DECRYPT;
7e1b7485 391 break;
340cf875
DDO
392 case OPT_DATA_CREATE:
393 operation = SMIME_DATA_CREATE;
394 break;
395 case OPT_DATA_OUT:
396 operation = SMIME_DATA_OUT;
397 break;
398 case OPT_CMSOUT:
399 operation = SMIME_CMSOUT;
7e1b7485 400 break;
340cf875 401
7e1b7485 402 case OPT_DEBUG_DECRYPT:
0f113f3e 403 flags |= CMS_DEBUG_DECRYPT;
7e1b7485
RS
404 break;
405 case OPT_TEXT:
0f113f3e 406 flags |= CMS_TEXT;
7e1b7485
RS
407 break;
408 case OPT_ASCIICRLF:
0f113f3e 409 flags |= CMS_ASCIICRLF;
7e1b7485
RS
410 break;
411 case OPT_NOINTERN:
0f113f3e 412 flags |= CMS_NOINTERN;
7e1b7485
RS
413 break;
414 case OPT_NOVERIFY:
0f113f3e 415 flags |= CMS_NO_SIGNER_CERT_VERIFY;
7e1b7485
RS
416 break;
417 case OPT_NOCERTS:
0f113f3e 418 flags |= CMS_NOCERTS;
7e1b7485
RS
419 break;
420 case OPT_NOATTR:
0f113f3e 421 flags |= CMS_NOATTR;
7e1b7485
RS
422 break;
423 case OPT_NODETACH:
0f113f3e 424 flags &= ~CMS_DETACHED;
7e1b7485
RS
425 break;
426 case OPT_NOSMIMECAP:
0f113f3e 427 flags |= CMS_NOSMIMECAP;
7e1b7485
RS
428 break;
429 case OPT_BINARY:
0f113f3e 430 flags |= CMS_BINARY;
e85d19c6
AI
431 break;
432 case OPT_CADES:
433 flags |= CMS_CADES;
7e1b7485
RS
434 break;
435 case OPT_KEYID:
0f113f3e 436 flags |= CMS_USE_KEYID;
7e1b7485
RS
437 break;
438 case OPT_NOSIGS:
0f113f3e 439 flags |= CMS_NOSIGS;
7e1b7485
RS
440 break;
441 case OPT_NO_CONTENT_VERIFY:
0f113f3e 442 flags |= CMS_NO_CONTENT_VERIFY;
7e1b7485
RS
443 break;
444 case OPT_NO_ATTR_VERIFY:
0f113f3e 445 flags |= CMS_NO_ATTR_VERIFY;
7e1b7485
RS
446 break;
447 case OPT_INDEF:
0f113f3e 448 flags |= CMS_STREAM;
7e1b7485
RS
449 break;
450 case OPT_NOINDEF:
0f113f3e 451 flags &= ~CMS_STREAM;
7e1b7485 452 break;
7e1b7485 453 case OPT_CRLFEOL:
2197494d 454 mime_eol = "\r\n";
0f113f3e 455 flags |= CMS_CRLFEOL;
7e1b7485
RS
456 break;
457 case OPT_NOOUT:
0f113f3e 458 noout = 1;
7e1b7485
RS
459 break;
460 case OPT_RR_PRINT:
0f113f3e 461 rr_print = 1;
7e1b7485
RS
462 break;
463 case OPT_RR_ALL:
0f113f3e 464 rr_allorfirst = 0;
7e1b7485
RS
465 break;
466 case OPT_RR_FIRST:
0f113f3e 467 rr_allorfirst = 1;
7e1b7485
RS
468 break;
469 case OPT_RCTFORM:
37578dc0
TM
470 if (!opt_format(opt_arg(),
471 OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))
472 goto opthelp;
7e1b7485
RS
473 break;
474 case OPT_CERTFILE:
475 certfile = opt_arg();
476 break;
477 case OPT_CAFILE:
478 CAfile = opt_arg();
479 break;
480 case OPT_CAPATH:
481 CApath = opt_arg();
482 break;
fd3397fc
RL
483 case OPT_CASTORE:
484 CAstore = opt_arg();
485 break;
2b6bcb70
MC
486 case OPT_NOCAFILE:
487 noCAfile = 1;
488 break;
489 case OPT_NOCAPATH:
490 noCApath = 1;
491 break;
fd3397fc
RL
492 case OPT_NOCASTORE:
493 noCAstore = 1;
494 break;
7e1b7485
RS
495 case OPT_IN:
496 infile = opt_arg();
497 break;
498 case OPT_CONTENT:
499 contfile = opt_arg();
500 break;
501 case OPT_RR_FROM:
502 if (rr_from == NULL
503 && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)
504 goto end;
505 sk_OPENSSL_STRING_push(rr_from, opt_arg());
506 break;
507 case OPT_RR_TO:
508 if (rr_to == NULL
509 && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)
510 goto end;
511 sk_OPENSSL_STRING_push(rr_to, opt_arg());
512 break;
513 case OPT_PRINT:
514 noout = print = 1;
515 break;
6ec351f4
DB
516 case OPT_NAMEOPT:
517 if (!set_nameopt(opt_arg()))
518 goto opthelp;
519 break;
7e1b7485 520 case OPT_SECRETKEY:
08f6ae5b 521 if (secret_key != NULL) {
efba7787
MC
522 BIO_printf(bio_err, "Invalid key (supplied twice) %s\n",
523 opt_arg());
08f6ae5b
MC
524 goto opthelp;
525 }
14f051a0 526 secret_key = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
7e1b7485
RS
527 if (secret_key == NULL) {
528 BIO_printf(bio_err, "Invalid key %s\n", opt_arg());
529 goto end;
0f113f3e
MC
530 }
531 secret_keylen = (size_t)ltmp;
7e1b7485
RS
532 break;
533 case OPT_SECRETKEYID:
08f6ae5b 534 if (secret_keyid != NULL) {
efba7787
MC
535 BIO_printf(bio_err, "Invalid id (supplied twice) %s\n",
536 opt_arg());
08f6ae5b
MC
537 goto opthelp;
538 }
14f051a0 539 secret_keyid = OPENSSL_hexstr2buf(opt_arg(), &ltmp);
7e1b7485
RS
540 if (secret_keyid == NULL) {
541 BIO_printf(bio_err, "Invalid id %s\n", opt_arg());
542 goto opthelp;
0f113f3e
MC
543 }
544 secret_keyidlen = (size_t)ltmp;
7e1b7485
RS
545 break;
546 case OPT_PWRI_PASSWORD:
547 pwri_pass = (unsigned char *)opt_arg();
548 break;
549 case OPT_ECONTENT_TYPE:
08f6ae5b 550 if (econtent_type != NULL) {
efba7787
MC
551 BIO_printf(bio_err, "Invalid OID (supplied twice) %s\n",
552 opt_arg());
08f6ae5b
MC
553 goto opthelp;
554 }
7e1b7485
RS
555 econtent_type = OBJ_txt2obj(opt_arg(), 0);
556 if (econtent_type == NULL) {
557 BIO_printf(bio_err, "Invalid OID %s\n", opt_arg());
558 goto opthelp;
0f113f3e 559 }
7e1b7485 560 break;
7e1b7485 561 case OPT_ENGINE:
333b070e 562 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
563 break;
564 case OPT_PASSIN:
565 passinarg = opt_arg();
566 break;
567 case OPT_TO:
568 to = opt_arg();
569 break;
570 case OPT_FROM:
571 from = opt_arg();
572 break;
573 case OPT_SUBJECT:
574 subject = opt_arg();
575 break;
576 case OPT_CERTSOUT:
577 certsoutfile = opt_arg();
578 break;
579 case OPT_MD:
d0190e11 580 digestname = opt_arg();
7e1b7485
RS
581 break;
582 case OPT_SIGNER:
0f113f3e 583 /* If previous -signer argument add signer to list */
2234212c 584 if (signerfile != NULL) {
7e1b7485
RS
585 if (sksigners == NULL
586 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
587 goto end;
0f113f3e 588 sk_OPENSSL_STRING_push(sksigners, signerfile);
7e1b7485 589 if (keyfile == NULL)
0f113f3e 590 keyfile = signerfile;
7e1b7485
RS
591 if (skkeys == NULL
592 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
593 goto end;
0f113f3e
MC
594 sk_OPENSSL_STRING_push(skkeys, keyfile);
595 keyfile = NULL;
596 }
7e1b7485
RS
597 signerfile = opt_arg();
598 break;
71434aed 599 case OPT_ORIGINATOR:
1b96cc70
DDO
600 originatorfile = opt_arg();
601 break;
7e1b7485 602 case OPT_INKEY:
ceab33e2 603 /* If previous -inkey argument add signer to list */
2234212c 604 if (keyfile != NULL) {
7e1b7485 605 if (signerfile == NULL) {
0f113f3e 606 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
7e1b7485 607 goto end;
0f113f3e 608 }
7e1b7485
RS
609 if (sksigners == NULL
610 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
611 goto end;
0f113f3e
MC
612 sk_OPENSSL_STRING_push(sksigners, signerfile);
613 signerfile = NULL;
7e1b7485
RS
614 if (skkeys == NULL
615 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
616 goto end;
0f113f3e
MC
617 sk_OPENSSL_STRING_push(skkeys, keyfile);
618 }
7e1b7485
RS
619 keyfile = opt_arg();
620 break;
621 case OPT_KEYFORM:
622 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
623 goto opthelp;
624 break;
625 case OPT_RECIP:
626 if (operation == SMIME_ENCRYPT) {
d382e796
TM
627 cert = load_cert(opt_arg(), FORMAT_UNDEF,
628 "recipient certificate file");
7e1b7485
RS
629 if (cert == NULL)
630 goto end;
631 sk_X509_push(encerts, cert);
632 cert = NULL;
2234212c 633 } else {
7e1b7485 634 recipfile = opt_arg();
2234212c 635 }
7e1b7485
RS
636 break;
637 case OPT_CIPHER:
03bbd346 638 ciphername = opt_unknown();
7e1b7485
RS
639 break;
640 case OPT_KEYOPT:
641 keyidx = -1;
0f113f3e 642 if (operation == SMIME_ENCRYPT) {
56c98a7d 643 if (sk_X509_num(encerts) > 0)
0f113f3e
MC
644 keyidx += sk_X509_num(encerts);
645 } else {
2234212c 646 if (keyfile != NULL || signerfile != NULL)
0f113f3e 647 keyidx++;
2234212c 648 if (skkeys != NULL)
0f113f3e
MC
649 keyidx += sk_OPENSSL_STRING_num(skkeys);
650 }
651 if (keyidx < 0) {
652 BIO_printf(bio_err, "No key specified\n");
7e1b7485 653 goto opthelp;
0f113f3e
MC
654 }
655 if (key_param == NULL || key_param->idx != keyidx) {
656 cms_key_param *nparam;
b4faea50 657 nparam = app_malloc(sizeof(*nparam), "key param buffer");
a21db568
BB
658 if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL) {
659 OPENSSL_free(nparam);
7e1b7485 660 goto end;
a21db568
BB
661 }
662 nparam->idx = keyidx;
0f113f3e
MC
663 nparam->next = NULL;
664 if (key_first == NULL)
665 key_first = nparam;
666 else
667 key_param->next = nparam;
668 key_param = nparam;
669 }
7e1b7485
RS
670 sk_OPENSSL_STRING_push(key_param->param, opt_arg());
671 break;
672 case OPT_V_CASES:
673 if (!opt_verify(o, vpm))
674 goto end;
675 vpmtouched++;
676 break;
3ee1eac2
RS
677 case OPT_R_CASES:
678 if (!opt_rand(o))
679 goto end;
680 break;
6bd4e3f2
P
681 case OPT_PROV_CASES:
682 if (!opt_provider(o))
683 goto end;
684 break;
c1669f41
SL
685 case OPT_CONFIG:
686 conf = app_load_config_modules(opt_arg());
687 if (conf == NULL)
688 goto end;
689 break;
340cf875 690 case OPT_WRAP:
69b920bb 691 wrapname = opt_arg();
340cf875 692 break;
7e1b7485 693 case OPT_AES128_WRAP:
7e1b7485 694 case OPT_AES192_WRAP:
7e1b7485 695 case OPT_AES256_WRAP:
340cf875 696 case OPT_3DES_WRAP:
a485561b 697 wrapname = opt_flag() + 1;
7e1b7485 698 break;
7e1b7485 699 }
0f113f3e 700 }
3ad60309
DDO
701 if (!app_RAND_load())
702 goto end;
703
03bbd346
RS
704 if (digestname != NULL) {
705 if (!opt_md(digestname, &sign_md))
706 goto end;
707 }
d9f07357
DDO
708 if (!opt_cipher_any(ciphername, &cipher))
709 goto end;
a485561b 710 if (wrapname != NULL) {
a89835f7 711 if (!opt_cipher_any(wrapname, &wrap_cipher))
a485561b
RS
712 goto end;
713 }
021410ea
RS
714
715 /* Remaining args are files to process. */
7e1b7485
RS
716 argc = opt_num_rest();
717 argv = opt_rest();
0f113f3e 718
2234212c 719 if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {
0f113f3e 720 BIO_puts(bio_err, "No Signed Receipts Recipients\n");
7e1b7485 721 goto opthelp;
0f113f3e
MC
722 }
723
2234212c 724 if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {
0f113f3e 725 BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
7e1b7485 726 goto opthelp;
0f113f3e 727 }
2234212c 728 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
0f113f3e 729 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
7e1b7485 730 goto opthelp;
0f113f3e
MC
731 }
732
9e3c510b
F
733 if ((flags & CMS_CADES) != 0) {
734 if ((flags & CMS_NOATTR) != 0) {
8c89c80a 735 BIO_puts(bio_err, "Incompatible options: "
63b64f19 736 "CAdES requires signed attributes\n");
8c89c80a
F
737 goto opthelp;
738 }
9e3c510b
F
739 if (operation == SMIME_VERIFY
740 && (flags & (CMS_NO_SIGNER_CERT_VERIFY | CMS_NO_ATTR_VERIFY)) != 0) {
63b64f19 741 BIO_puts(bio_err, "Incompatible options: CAdES validation requires"
9e3c510b
F
742 " certs and signed attributes validations\n");
743 goto opthelp;
744 }
8c89c80a
F
745 }
746
0f113f3e 747 if (operation & SMIME_SIGNERS) {
2234212c 748 if (keyfile != NULL && signerfile == NULL) {
0f113f3e 749 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
7e1b7485 750 goto opthelp;
0f113f3e
MC
751 }
752 /* Check to see if any final signer needs to be appended */
2234212c
PY
753 if (signerfile != NULL) {
754 if (sksigners == NULL
7e1b7485
RS
755 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
756 goto end;
0f113f3e 757 sk_OPENSSL_STRING_push(sksigners, signerfile);
2234212c 758 if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
7e1b7485 759 goto end;
2234212c 760 if (keyfile == NULL)
0f113f3e
MC
761 keyfile = signerfile;
762 sk_OPENSSL_STRING_push(skkeys, keyfile);
763 }
2234212c 764 if (sksigners == NULL) {
0f113f3e 765 BIO_printf(bio_err, "No signer certificate specified\n");
7e1b7485 766 goto opthelp;
0f113f3e
MC
767 }
768 signerfile = NULL;
769 keyfile = NULL;
2234212c
PY
770 } else if (operation == SMIME_DECRYPT) {
771 if (recipfile == NULL && keyfile == NULL
772 && secret_key == NULL && pwri_pass == NULL) {
0f113f3e
MC
773 BIO_printf(bio_err,
774 "No recipient certificate or key specified\n");
7e1b7485 775 goto opthelp;
0f113f3e
MC
776 }
777 } else if (operation == SMIME_ENCRYPT) {
2234212c 778 if (*argv == NULL && secret_key == NULL
56c98a7d 779 && pwri_pass == NULL && sk_X509_num(encerts) <= 0) {
0f113f3e 780 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
7e1b7485 781 goto opthelp;
0f113f3e 782 }
2234212c 783 } else if (!operation) {
42151b8e 784 BIO_printf(bio_err, "No operation option (-encrypt|-decrypt|-sign|-verify|...) specified.\n");
7e1b7485 785 goto opthelp;
2234212c 786 }
7e1b7485 787
7e1b7485 788 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
0f113f3e
MC
789 BIO_printf(bio_err, "Error getting password\n");
790 goto end;
791 }
792
0f113f3e
MC
793 ret = 2;
794
5771017d
DDO
795 if ((operation & SMIME_SIGNERS) == 0) {
796 if ((flags & CMS_DETACHED) == 0)
797 BIO_printf(bio_err,
798 "Warning: -nodetach option is ignored for non-signing operation\n");
799
0f113f3e 800 flags &= ~CMS_DETACHED;
5771017d
DDO
801 }
802 if ((operation & SMIME_IP) == 0 && contfile != NULL)
803 BIO_printf(bio_err,
804 "Warning: -contfile option is ignored for the given operation\n");
8c34367e 805 if (operation != SMIME_ENCRYPT && *argv != NULL)
806 BIO_printf(bio_err,
807 "Warning: recipient certificate file parameters ignored for operation other than -encrypt\n");
0f113f3e 808
6b83d032
DDO
809 if ((flags & CMS_BINARY) != 0) {
810 if (!(operation & SMIME_OP))
bdd58d98 811 outformat = FORMAT_BINARY;
6b83d032 812 if (!(operation & SMIME_IP))
bdd58d98 813 informat = FORMAT_BINARY;
5771017d
DDO
814 if ((operation & SMIME_SIGNERS) != 0 && (flags & CMS_DETACHED) != 0)
815 binary_files = 1;
816 if ((operation & SMIME_IP) != 0 && contfile == NULL)
817 binary_files = 1;
6b83d032 818 }
0f113f3e
MC
819
820 if (operation == SMIME_ENCRYPT) {
821 if (!cipher) {
340cf875 822#ifndef OPENSSL_NO_DES
606a417f 823 cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
340cf875 824#else
0f113f3e
MC
825 BIO_printf(bio_err, "No cipher selected\n");
826 goto end;
340cf875 827#endif
0f113f3e
MC
828 }
829
830 if (secret_key && !secret_keyid) {
831 BIO_printf(bio_err, "No secret key id\n");
832 goto end;
833 }
834
8c34367e 835 for (; *argv != NULL; argv++) {
836 cert = load_cert(*argv, FORMAT_UNDEF,
837 "recipient certificate file");
838 if (cert == NULL)
839 goto end;
840 sk_X509_push(encerts, cert);
841 cert = NULL;
0f113f3e
MC
842 }
843 }
844
2234212c 845 if (certfile != NULL) {
ea51096e 846 if (!load_certs(certfile, 0, &other, NULL, "certificate file")) {
0f113f3e
MC
847 ERR_print_errors(bio_err);
848 goto end;
849 }
850 }
851
2234212c 852 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
d382e796 853 if ((recip = load_cert(recipfile, FORMAT_UNDEF,
75ebbd9a 854 "recipient certificate file")) == NULL) {
0f113f3e
MC
855 ERR_print_errors(bio_err);
856 goto end;
857 }
858 }
859
71434aed 860 if (originatorfile != NULL) {
d382e796 861 if ((originator = load_cert(originatorfile, FORMAT_UNDEF,
71434aed 862 "originator certificate file")) == NULL) {
1b96cc70
DDO
863 ERR_print_errors(bio_err);
864 goto end;
71434aed
DB
865 }
866 }
867
0f113f3e 868 if (operation == SMIME_SIGN_RECEIPT) {
d382e796 869 if ((signer = load_cert(signerfile, FORMAT_UNDEF,
75ebbd9a 870 "receipt signer certificate file")) == NULL) {
0f113f3e
MC
871 ERR_print_errors(bio_err);
872 goto end;
873 }
874 }
875
71434aed 876 if ((operation == SMIME_DECRYPT) || (operation == SMIME_ENCRYPT)) {
2234212c 877 if (keyfile == NULL)
0f113f3e
MC
878 keyfile = recipfile;
879 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
2234212c 880 if (keyfile == NULL)
0f113f3e 881 keyfile = signerfile;
2234212c 882 } else {
0f113f3e 883 keyfile = NULL;
2234212c 884 }
0f113f3e 885
2234212c 886 if (keyfile != NULL) {
50eb2a50 887 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
2234212c 888 if (key == NULL)
0f113f3e
MC
889 goto end;
890 }
891
07342bad
VS
892 if (digesthex != NULL) {
893 if (operation != SMIME_SIGN) {
894 BIO_printf(bio_err,
895 "Cannot use -digest for non-signing operation\n");
896 goto end;
897 }
898 if (infile != NULL
899 || (flags & CMS_DETACHED) == 0
900 || (flags & CMS_STREAM) != 0) {
901 BIO_printf(bio_err,
902 "Cannot use -digest when -in, -nodetach or streaming is used\n");
903 goto end;
904 }
905 digestbin = OPENSSL_hexstr2buf(digesthex, &digestlen);
906 if (digestbin == NULL) {
907 BIO_printf(bio_err,
908 "Invalid hex value after -digest\n");
909 goto end;
910 }
911 } else {
912 in = bio_open_default(infile, 'r',
913 binary_files ? FORMAT_BINARY : informat);
914 if (in == NULL)
915 goto end;
916 }
0f113f3e
MC
917
918 if (operation & SMIME_IP) {
1b96cc70 919 cms = load_content_info(informat, in, flags, &indata, "SMIME");
c1669f41 920 if (cms == NULL)
0f113f3e 921 goto end;
2234212c 922 if (contfile != NULL) {
0f113f3e 923 BIO_free(indata);
75ebbd9a 924 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
0f113f3e
MC
925 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
926 goto end;
927 }
928 }
2234212c 929 if (certsoutfile != NULL) {
0f113f3e
MC
930 STACK_OF(X509) *allcerts;
931 allcerts = CMS_get1_certs(cms);
932 if (!save_certs(certsoutfile, allcerts)) {
933 BIO_printf(bio_err,
934 "Error writing certs to %s\n", certsoutfile);
935 ret = 5;
936 goto end;
937 }
79b2a2f2 938 OSSL_STACK_OF_X509_free(allcerts);
0f113f3e
MC
939 }
940 }
941
2234212c 942 if (rctfile != NULL) {
0f113f3e 943 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
6b83d032 944
75ebbd9a 945 if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
0f113f3e
MC
946 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
947 goto end;
948 }
949
37578dc0 950 rcms = load_content_info(rctformat, rctin, 0, NULL, "receipt");
c1669f41 951 if (rcms == NULL)
0f113f3e 952 goto end;
0f113f3e
MC
953 }
954
5771017d
DDO
955 out = bio_open_default(outfile, 'w',
956 binary_files ? FORMAT_BINARY : outformat);
7e1b7485
RS
957 if (out == NULL)
958 goto end;
0f113f3e
MC
959
960 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
fd3397fc
RL
961 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
962 CAstore, noCAstore)) == NULL)
0f113f3e
MC
963 goto end;
964 X509_STORE_set_verify_cb(store, cms_cb);
7e1b7485 965 if (vpmtouched)
0f113f3e
MC
966 X509_STORE_set1_param(store, vpm);
967 }
968
969 ret = 3;
970
971 if (operation == SMIME_DATA_CREATE) {
7dc67708 972 cms = CMS_data_create_ex(in, flags, libctx, app_get0_propq());
0f113f3e 973 } else if (operation == SMIME_DIGEST_CREATE) {
7dc67708 974 cms = CMS_digest_create_ex(in, sign_md, flags, libctx, app_get0_propq());
0f113f3e
MC
975 } else if (operation == SMIME_COMPRESS) {
976 cms = CMS_compress(in, -1, flags);
977 } else if (operation == SMIME_ENCRYPT) {
978 int i;
979 flags |= CMS_PARTIAL;
7dc67708 980 cms = CMS_encrypt_ex(NULL, in, cipher, flags, libctx, app_get0_propq());
2234212c 981 if (cms == NULL)
0f113f3e
MC
982 goto end;
983 for (i = 0; i < sk_X509_num(encerts); i++) {
984 CMS_RecipientInfo *ri;
985 cms_key_param *kparam;
1b96cc70
DDO
986 int tflags = flags | CMS_KEY_PARAM;
987 /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */
71434aed 988 EVP_PKEY_CTX *pctx;
0f113f3e 989 X509 *x = sk_X509_value(encerts, i);
71434aed
DB
990 int res;
991
0f113f3e
MC
992 for (kparam = key_first; kparam; kparam = kparam->next) {
993 if (kparam->idx == i) {
0f113f3e
MC
994 break;
995 }
996 }
71434aed 997 ri = CMS_add1_recipient(cms, x, key, originator, tflags);
2234212c 998 if (ri == NULL)
0f113f3e 999 goto end;
71434aed
DB
1000
1001 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
2234212c 1002 if (kparam != NULL) {
0f113f3e
MC
1003 if (!cms_set_pkey_param(pctx, kparam->param))
1004 goto end;
1005 }
71434aed
DB
1006
1007 res = EVP_PKEY_CTX_ctrl(pctx, -1, -1,
1008 EVP_PKEY_CTRL_CIPHER,
ed576acd 1009 EVP_CIPHER_get_nid(cipher), NULL);
71434aed
DB
1010 if (res <= 0 && res != -2)
1011 goto end;
1012
0f113f3e 1013 if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
5c42f7aa 1014 && wrap_cipher != NULL) {
0f113f3e
MC
1015 EVP_CIPHER_CTX *wctx;
1016 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
98283a61
DB
1017 if (EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL) != 1)
1018 goto end;
0f113f3e
MC
1019 }
1020 }
1021
2234212c 1022 if (secret_key != NULL) {
0f113f3e
MC
1023 if (!CMS_add0_recipient_key(cms, NID_undef,
1024 secret_key, secret_keylen,
1025 secret_keyid, secret_keyidlen,
1026 NULL, NULL, NULL))
1027 goto end;
1028 /* NULL these because call absorbs them */
1029 secret_key = NULL;
1030 secret_keyid = NULL;
1031 }
2234212c 1032 if (pwri_pass != NULL) {
7644a9ae 1033 pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
2234212c 1034 if (pwri_tmp == NULL)
0f113f3e 1035 goto end;
2234212c
PY
1036 if (CMS_add0_recipient_password(cms,
1037 -1, NID_undef, NID_undef,
1038 pwri_tmp, -1, NULL) == NULL)
0f113f3e
MC
1039 goto end;
1040 pwri_tmp = NULL;
1041 }
1042 if (!(flags & CMS_STREAM)) {
1043 if (!CMS_final(cms, in, NULL, flags))
1044 goto end;
1045 }
1046 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
d8652be0 1047 cms = CMS_EncryptedData_encrypt_ex(in, cipher, secret_key,
7dc67708 1048 secret_keylen, flags, libctx, app_get0_propq());
0f113f3e
MC
1049
1050 } else if (operation == SMIME_SIGN_RECEIPT) {
1051 CMS_ContentInfo *srcms = NULL;
1052 STACK_OF(CMS_SignerInfo) *sis;
1053 CMS_SignerInfo *si;
1054 sis = CMS_get0_SignerInfos(cms);
2234212c 1055 if (sis == NULL)
0f113f3e
MC
1056 goto end;
1057 si = sk_CMS_SignerInfo_value(sis, 0);
1058 srcms = CMS_sign_receipt(si, signer, key, other, flags);
2234212c 1059 if (srcms == NULL)
0f113f3e
MC
1060 goto end;
1061 CMS_ContentInfo_free(cms);
1062 cms = srcms;
1063 } else if (operation & SMIME_SIGNERS) {
1064 int i;
1065 /*
07342bad
VS
1066 * If detached data content and not signing pre-computed digest, we
1067 * enable streaming if S/MIME output format.
0f113f3e
MC
1068 */
1069 if (operation == SMIME_SIGN) {
1070
07342bad 1071 if ((flags & CMS_DETACHED) != 0 && digestbin == NULL) {
0f113f3e
MC
1072 if (outformat == FORMAT_SMIME)
1073 flags |= CMS_STREAM;
1074 }
1075 flags |= CMS_PARTIAL;
7dc67708 1076 cms = CMS_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
2234212c 1077 if (cms == NULL)
0f113f3e 1078 goto end;
2234212c 1079 if (econtent_type != NULL)
0f113f3e
MC
1080 CMS_set1_eContentType(cms, econtent_type);
1081
1b96cc70
DDO
1082 if (rr_to != NULL
1083 && ((rr = make_receipt_request(rr_to, rr_allorfirst, rr_from))
1084 == NULL)) {
1085 BIO_puts(bio_err, "Signed Receipt Request Creation Error\n");
1086 goto end;
0f113f3e 1087 }
2234212c 1088 } else {
0f113f3e 1089 flags |= CMS_REUSE_DIGEST;
2234212c 1090 }
0f113f3e
MC
1091 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
1092 CMS_SignerInfo *si;
1093 cms_key_param *kparam;
1094 int tflags = flags;
1095 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
1096 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
1097
d382e796 1098 signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
a032ed0a
KS
1099 if (signer == NULL) {
1100 ret = 2;
0f113f3e 1101 goto end;
a032ed0a 1102 }
50eb2a50 1103 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
a032ed0a
KS
1104 if (key == NULL) {
1105 ret = 2;
0f113f3e 1106 goto end;
a032ed0a 1107 }
a10847c4 1108
0f113f3e
MC
1109 for (kparam = key_first; kparam; kparam = kparam->next) {
1110 if (kparam->idx == i) {
1111 tflags |= CMS_KEY_PARAM;
1112 break;
1113 }
1114 }
1115 si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
2234212c 1116 if (si == NULL)
0f113f3e 1117 goto end;
2234212c 1118 if (kparam != NULL) {
0f113f3e
MC
1119 EVP_PKEY_CTX *pctx;
1120 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
1121 if (!cms_set_pkey_param(pctx, kparam->param))
1122 goto end;
1123 }
2234212c 1124 if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
0f113f3e
MC
1125 goto end;
1126 X509_free(signer);
1127 signer = NULL;
1128 EVP_PKEY_free(key);
1129 key = NULL;
1130 }
1131 /* If not streaming or resigning finalize structure */
07342bad
VS
1132 if (operation == SMIME_SIGN && digestbin != NULL
1133 && (flags & CMS_STREAM) == 0) {
1134 /* Use pre-computed digest instead of content */
1135 if (!CMS_final_digest(cms, digestbin, digestlen, NULL, flags))
1136 goto end;
1137 } else if (operation == SMIME_SIGN && (flags & CMS_STREAM) == 0) {
0f113f3e
MC
1138 if (!CMS_final(cms, in, NULL, flags))
1139 goto end;
1140 }
1141 }
1142
2234212c 1143 if (cms == NULL) {
0f113f3e
MC
1144 BIO_printf(bio_err, "Error creating CMS structure\n");
1145 goto end;
1146 }
1147
1148 ret = 4;
1149 if (operation == SMIME_DECRYPT) {
1150 if (flags & CMS_DEBUG_DECRYPT)
1151 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
1152
2234212c 1153 if (secret_key != NULL) {
0f113f3e
MC
1154 if (!CMS_decrypt_set1_key(cms,
1155 secret_key, secret_keylen,
1156 secret_keyid, secret_keyidlen)) {
1157 BIO_puts(bio_err, "Error decrypting CMS using secret key\n");
1158 goto end;
1159 }
1160 }
1161
2234212c 1162 if (key != NULL) {
71434aed 1163 if (!CMS_decrypt_set1_pkey_and_peer(cms, key, recip, originator)) {
0f113f3e
MC
1164 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
1165 goto end;
1166 }
1167 }
1168
2234212c 1169 if (pwri_pass != NULL) {
0f113f3e
MC
1170 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
1171 BIO_puts(bio_err, "Error decrypting CMS using password\n");
1172 goto end;
1173 }
1174 }
1175
1176 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
1177 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1178 goto end;
1179 }
340cf875 1180 } else if (operation == SMIME_DATA_OUT) {
0f113f3e
MC
1181 if (!CMS_data(cms, out, flags))
1182 goto end;
1183 } else if (operation == SMIME_UNCOMPRESS) {
1184 if (!CMS_uncompress(cms, indata, out, flags))
1185 goto end;
1186 } else if (operation == SMIME_DIGEST_VERIFY) {
2234212c 1187 if (CMS_digest_verify(cms, indata, out, flags) > 0) {
0f113f3e 1188 BIO_printf(bio_err, "Verification successful\n");
2234212c 1189 } else {
0f113f3e
MC
1190 BIO_printf(bio_err, "Verification failure\n");
1191 goto end;
1192 }
1193 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
1194 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1195 indata, out, flags))
1196 goto end;
1197 } else if (operation == SMIME_VERIFY) {
2234212c 1198 if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
9e3c510b 1199 BIO_printf(bio_err, "%s Verification successful\n",
56c98a7d 1200 (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
2234212c 1201 } else {
56c98a7d
DDO
1202 BIO_printf(bio_err, "%s Verification failure\n",
1203 (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
0f113f3e
MC
1204 if (verify_retcode)
1205 ret = verify_err + 32;
1206 goto end;
1207 }
2234212c 1208 if (signerfile != NULL) {
340cf875
DDO
1209 STACK_OF(X509) *signers = CMS_get0_signers(cms);
1210
0f113f3e
MC
1211 if (!save_certs(signerfile, signers)) {
1212 BIO_printf(bio_err,
1213 "Error writing signers to %s\n", signerfile);
1214 ret = 5;
1215 goto end;
1216 }
1217 sk_X509_free(signers);
1218 }
1219 if (rr_print)
ecf3a1fb 1220 receipt_request_print(cms);
0f113f3e
MC
1221
1222 } else if (operation == SMIME_VERIFY_RECEIPT) {
2234212c 1223 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
0f113f3e 1224 BIO_printf(bio_err, "Verification successful\n");
2234212c 1225 } else {
0f113f3e
MC
1226 BIO_printf(bio_err, "Verification failure\n");
1227 goto end;
1228 }
1229 } else {
1230 if (noout) {
6ec351f4
DB
1231 if (print) {
1232 ASN1_PCTX *pctx = NULL;
1233 if (get_nameopt() != XN_FLAG_ONELINE) {
1234 pctx = ASN1_PCTX_new();
1235 if (pctx != NULL) { /* Print anyway if malloc failed */
1236 ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT);
1237 ASN1_PCTX_set_str_flags(pctx, get_nameopt());
1238 ASN1_PCTX_set_nm_flags(pctx, get_nameopt());
1239 }
1240 }
1241 CMS_ContentInfo_print_ctx(out, cms, 0, pctx);
1242 ASN1_PCTX_free(pctx);
1243 }
0f113f3e
MC
1244 } else if (outformat == FORMAT_SMIME) {
1245 if (to)
2197494d 1246 BIO_printf(out, "To: %s%s", to, mime_eol);
0f113f3e 1247 if (from)
2197494d 1248 BIO_printf(out, "From: %s%s", from, mime_eol);
0f113f3e 1249 if (subject)
2197494d 1250 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
0f113f3e
MC
1251 if (operation == SMIME_RESIGN)
1252 ret = SMIME_write_CMS(out, cms, indata, flags);
1253 else
1254 ret = SMIME_write_CMS(out, cms, in, flags);
2234212c 1255 } else if (outformat == FORMAT_PEM) {
0f113f3e 1256 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
2234212c 1257 } else if (outformat == FORMAT_ASN1) {
0f113f3e 1258 ret = i2d_CMS_bio_stream(out, cms, in, flags);
2234212c 1259 } else {
0f113f3e
MC
1260 BIO_printf(bio_err, "Bad output format for CMS file\n");
1261 goto end;
1262 }
1263 if (ret <= 0) {
1264 ret = 6;
1265 goto end;
1266 }
1267 }
1268 ret = 0;
1269 end:
1270 if (ret)
1271 ERR_print_errors(bio_err);
79b2a2f2
DDO
1272 OSSL_STACK_OF_X509_free(encerts);
1273 OSSL_STACK_OF_X509_free(other);
7e1b7485 1274 X509_VERIFY_PARAM_free(vpm);
25aaa98a
RS
1275 sk_OPENSSL_STRING_free(sksigners);
1276 sk_OPENSSL_STRING_free(skkeys);
b548a1f1
RS
1277 OPENSSL_free(secret_key);
1278 OPENSSL_free(secret_keyid);
1279 OPENSSL_free(pwri_tmp);
0dfb9398 1280 ASN1_OBJECT_free(econtent_type);
25aaa98a
RS
1281 CMS_ReceiptRequest_free(rr);
1282 sk_OPENSSL_STRING_free(rr_to);
1283 sk_OPENSSL_STRING_free(rr_from);
0f113f3e
MC
1284 for (key_param = key_first; key_param;) {
1285 cms_key_param *tparam;
1286 sk_OPENSSL_STRING_free(key_param->param);
1287 tparam = key_param->next;
1288 OPENSSL_free(key_param);
1289 key_param = tparam;
1290 }
1291 X509_STORE_free(store);
1292 X509_free(cert);
1293 X509_free(recip);
1294 X509_free(signer);
1295 EVP_PKEY_free(key);
606a417f
RS
1296 EVP_CIPHER_free(cipher);
1297 EVP_CIPHER_free(wrap_cipher);
1298 EVP_MD_free(sign_md);
0f113f3e
MC
1299 CMS_ContentInfo_free(cms);
1300 CMS_ContentInfo_free(rcms);
dd1abd44 1301 release_engine(e);
0f113f3e
MC
1302 BIO_free(rctin);
1303 BIO_free(in);
1304 BIO_free(indata);
1305 BIO_free_all(out);
07342bad 1306 OPENSSL_free(digestbin);
b548a1f1 1307 OPENSSL_free(passin);
c1669f41 1308 NCONF_free(conf);
26a7d938 1309 return ret;
8931b30d
DSH
1310}
1311
1312static int save_certs(char *signerfile, STACK_OF(X509) *signers)
0f113f3e
MC
1313{
1314 int i;
1315 BIO *tmp;
2234212c 1316 if (signerfile == NULL)
0f113f3e
MC
1317 return 1;
1318 tmp = BIO_new_file(signerfile, "w");
2234212c 1319 if (tmp == NULL)
0f113f3e
MC
1320 return 0;
1321 for (i = 0; i < sk_X509_num(signers); i++)
1322 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1323 BIO_free(tmp);
1324 return 1;
1325}
8931b30d
DSH
1326
1327/* Minimal callback just to output policy info (if any) */
1328
7f50d9a4 1329static int cms_cb(int ok, X509_STORE_CTX *ctx)
0f113f3e
MC
1330{
1331 int error;
8931b30d 1332
0f113f3e 1333 error = X509_STORE_CTX_get_error(ctx);
8931b30d 1334
0f113f3e 1335 verify_err = error;
db50661f 1336
0f113f3e
MC
1337 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1338 && ((error != X509_V_OK) || (ok != 2)))
1339 return ok;
8931b30d 1340
ecf3a1fb 1341 policies_print(ctx);
8931b30d 1342
0f113f3e 1343 return ok;
8931b30d 1344
0f113f3e 1345}
8931b30d 1346
ecf3a1fb 1347static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
0f113f3e
MC
1348{
1349 STACK_OF(GENERAL_NAME) *gens;
1350 GENERAL_NAME *gen;
1351 int i, j;
ecf3a1fb 1352
0f113f3e
MC
1353 for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
1354 gens = sk_GENERAL_NAMES_value(gns, i);
1355 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
1356 gen = sk_GENERAL_NAME_value(gens, j);
ecf3a1fb
RS
1357 BIO_puts(bio_err, " ");
1358 GENERAL_NAME_print(bio_err, gen);
1359 BIO_puts(bio_err, "\n");
0f113f3e
MC
1360 }
1361 }
1362 return;
1363}
f4cc56f4 1364
ecf3a1fb 1365static void receipt_request_print(CMS_ContentInfo *cms)
0f113f3e
MC
1366{
1367 STACK_OF(CMS_SignerInfo) *sis;
1368 CMS_SignerInfo *si;
1369 CMS_ReceiptRequest *rr;
1370 int allorfirst;
1371 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1372 ASN1_STRING *scid;
1373 int i, rv;
1374 sis = CMS_get0_SignerInfos(cms);
1375 for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
1376 si = sk_CMS_SignerInfo_value(sis, i);
1377 rv = CMS_get1_ReceiptRequest(si, &rr);
1378 BIO_printf(bio_err, "Signer %d:\n", i + 1);
2234212c 1379 if (rv == 0) {
0f113f3e 1380 BIO_puts(bio_err, " No Receipt Request\n");
2234212c 1381 } else if (rv < 0) {
0f113f3e
MC
1382 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1383 ERR_print_errors(bio_err);
1384 } else {
17ebf85a 1385 const char *id;
0f113f3e
MC
1386 int idlen;
1387 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1388 &rlist, &rto);
ecf3a1fb 1389 BIO_puts(bio_err, " Signed Content ID:\n");
0f113f3e 1390 idlen = ASN1_STRING_length(scid);
17ebf85a 1391 id = (const char *)ASN1_STRING_get0_data(scid);
ecf3a1fb
RS
1392 BIO_dump_indent(bio_err, id, idlen, 4);
1393 BIO_puts(bio_err, " Receipts From");
2234212c 1394 if (rlist != NULL) {
ecf3a1fb
RS
1395 BIO_puts(bio_err, " List:\n");
1396 gnames_stack_print(rlist);
2234212c 1397 } else if (allorfirst == 1) {
ecf3a1fb 1398 BIO_puts(bio_err, ": First Tier\n");
2234212c 1399 } else if (allorfirst == 0) {
ecf3a1fb 1400 BIO_puts(bio_err, ": All\n");
2234212c 1401 } else {
ecf3a1fb 1402 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
2234212c 1403 }
ecf3a1fb
RS
1404 BIO_puts(bio_err, " Receipts To:\n");
1405 gnames_stack_print(rto);
0f113f3e 1406 }
25aaa98a 1407 CMS_ReceiptRequest_free(rr);
0f113f3e
MC
1408 }
1409}
f4cc56f4 1410
c869da88 1411static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
0f113f3e
MC
1412{
1413 int i;
1414 STACK_OF(GENERAL_NAMES) *ret;
1415 GENERAL_NAMES *gens = NULL;
1416 GENERAL_NAME *gen = NULL;
1417 ret = sk_GENERAL_NAMES_new_null();
2234212c 1418 if (ret == NULL)
0f113f3e
MC
1419 goto err;
1420 for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
1421 char *str = sk_OPENSSL_STRING_value(ns, i);
1422 gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
2234212c 1423 if (gen == NULL)
0f113f3e
MC
1424 goto err;
1425 gens = GENERAL_NAMES_new();
96487cdd 1426 if (gens == NULL)
0f113f3e
MC
1427 goto err;
1428 if (!sk_GENERAL_NAME_push(gens, gen))
1429 goto err;
1430 gen = NULL;
1431 if (!sk_GENERAL_NAMES_push(ret, gens))
1432 goto err;
1433 gens = NULL;
1434 }
1435
1436 return ret;
1437
1438 err:
25aaa98a
RS
1439 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1440 GENERAL_NAMES_free(gens);
1441 GENERAL_NAME_free(gen);
0f113f3e
MC
1442 return NULL;
1443}
1444
1b96cc70
DDO
1445static CMS_ReceiptRequest
1446*make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
1447 STACK_OF(OPENSSL_STRING) *rr_from)
0f113f3e 1448{
6e4ab54b 1449 STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
1b96cc70 1450
0f113f3e 1451 rct_to = make_names_stack(rr_to);
2234212c 1452 if (rct_to == NULL)
0f113f3e 1453 goto err;
2234212c 1454 if (rr_from != NULL) {
0f113f3e 1455 rct_from = make_names_stack(rr_from);
2234212c 1456 if (rct_from == NULL)
0f113f3e 1457 goto err;
2234212c 1458 } else {
0f113f3e 1459 rct_from = NULL;
2234212c 1460 }
1b96cc70
DDO
1461 return CMS_ReceiptRequest_create0_ex(NULL, -1, rr_allorfirst, rct_from,
1462 rct_to, app_get0_libctx());
0f113f3e 1463 err:
6e4ab54b 1464 sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
0f113f3e
MC
1465 return NULL;
1466}
f5e2354c 1467
02498cc8 1468static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
0f113f3e
MC
1469 STACK_OF(OPENSSL_STRING) *param)
1470{
1471 char *keyopt;
1472 int i;
1473 if (sk_OPENSSL_STRING_num(param) <= 0)
1474 return 1;
1475 for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
1476 keyopt = sk_OPENSSL_STRING_value(param, i);
1477 if (pkey_ctrl_string(pctx, keyopt) <= 0) {
1478 BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
1479 ERR_print_errors(bio_err);
1480 return 0;
1481 }
1482 }
1483 return 1;
1484}