]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/cms.c
CMS sign digest
[thirdparty/openssl.git] / apps / cms.c
CommitLineData
0f113f3e 1/*
a28d06f3 2 * Copyright 2008-2021 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");
0f113f3e 805
6b83d032
DDO
806 if ((flags & CMS_BINARY) != 0) {
807 if (!(operation & SMIME_OP))
bdd58d98 808 outformat = FORMAT_BINARY;
6b83d032 809 if (!(operation & SMIME_IP))
bdd58d98 810 informat = FORMAT_BINARY;
5771017d
DDO
811 if ((operation & SMIME_SIGNERS) != 0 && (flags & CMS_DETACHED) != 0)
812 binary_files = 1;
813 if ((operation & SMIME_IP) != 0 && contfile == NULL)
814 binary_files = 1;
6b83d032 815 }
0f113f3e
MC
816
817 if (operation == SMIME_ENCRYPT) {
818 if (!cipher) {
340cf875 819#ifndef OPENSSL_NO_DES
606a417f 820 cipher = (EVP_CIPHER *)EVP_des_ede3_cbc();
340cf875 821#else
0f113f3e
MC
822 BIO_printf(bio_err, "No cipher selected\n");
823 goto end;
340cf875 824#endif
0f113f3e
MC
825 }
826
827 if (secret_key && !secret_keyid) {
828 BIO_printf(bio_err, "No secret key id\n");
829 goto end;
830 }
831
56c98a7d
DDO
832 if (*argv != NULL) {
833 if (operation == SMIME_ENCRYPT) {
834 for (; *argv != NULL; argv++) {
835 cert = load_cert(*argv, FORMAT_UNDEF,
836 "recipient certificate file");
837 if (cert == NULL)
838 goto end;
839 sk_X509_push(encerts, cert);
840 cert = NULL;
841 }
842 } else {
843 BIO_printf(bio_err, "Warning: recipient certificate file parameters ignored for operation other than -encrypt\n");
844 }
0f113f3e
MC
845 }
846 }
847
2234212c 848 if (certfile != NULL) {
ea51096e 849 if (!load_certs(certfile, 0, &other, NULL, "certificate file")) {
0f113f3e
MC
850 ERR_print_errors(bio_err);
851 goto end;
852 }
853 }
854
2234212c 855 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
d382e796 856 if ((recip = load_cert(recipfile, FORMAT_UNDEF,
75ebbd9a 857 "recipient certificate file")) == NULL) {
0f113f3e
MC
858 ERR_print_errors(bio_err);
859 goto end;
860 }
861 }
862
71434aed 863 if (originatorfile != NULL) {
d382e796 864 if ((originator = load_cert(originatorfile, FORMAT_UNDEF,
71434aed 865 "originator certificate file")) == NULL) {
1b96cc70
DDO
866 ERR_print_errors(bio_err);
867 goto end;
71434aed
DB
868 }
869 }
870
0f113f3e 871 if (operation == SMIME_SIGN_RECEIPT) {
d382e796 872 if ((signer = load_cert(signerfile, FORMAT_UNDEF,
75ebbd9a 873 "receipt signer certificate file")) == NULL) {
0f113f3e
MC
874 ERR_print_errors(bio_err);
875 goto end;
876 }
877 }
878
71434aed 879 if ((operation == SMIME_DECRYPT) || (operation == SMIME_ENCRYPT)) {
2234212c 880 if (keyfile == NULL)
0f113f3e
MC
881 keyfile = recipfile;
882 } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {
2234212c 883 if (keyfile == NULL)
0f113f3e 884 keyfile = signerfile;
2234212c 885 } else {
0f113f3e 886 keyfile = NULL;
2234212c 887 }
0f113f3e 888
2234212c 889 if (keyfile != NULL) {
50eb2a50 890 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
2234212c 891 if (key == NULL)
0f113f3e
MC
892 goto end;
893 }
894
07342bad
VS
895 if (digesthex != NULL) {
896 if (operation != SMIME_SIGN) {
897 BIO_printf(bio_err,
898 "Cannot use -digest for non-signing operation\n");
899 goto end;
900 }
901 if (infile != NULL
902 || (flags & CMS_DETACHED) == 0
903 || (flags & CMS_STREAM) != 0) {
904 BIO_printf(bio_err,
905 "Cannot use -digest when -in, -nodetach or streaming is used\n");
906 goto end;
907 }
908 digestbin = OPENSSL_hexstr2buf(digesthex, &digestlen);
909 if (digestbin == NULL) {
910 BIO_printf(bio_err,
911 "Invalid hex value after -digest\n");
912 goto end;
913 }
914 } else {
915 in = bio_open_default(infile, 'r',
916 binary_files ? FORMAT_BINARY : informat);
917 if (in == NULL)
918 goto end;
919 }
0f113f3e
MC
920
921 if (operation & SMIME_IP) {
1b96cc70 922 cms = load_content_info(informat, in, flags, &indata, "SMIME");
c1669f41 923 if (cms == NULL)
0f113f3e 924 goto end;
2234212c 925 if (contfile != NULL) {
0f113f3e 926 BIO_free(indata);
75ebbd9a 927 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
0f113f3e
MC
928 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
929 goto end;
930 }
931 }
2234212c 932 if (certsoutfile != NULL) {
0f113f3e
MC
933 STACK_OF(X509) *allcerts;
934 allcerts = CMS_get1_certs(cms);
935 if (!save_certs(certsoutfile, allcerts)) {
936 BIO_printf(bio_err,
937 "Error writing certs to %s\n", certsoutfile);
938 ret = 5;
939 goto end;
940 }
79b2a2f2 941 OSSL_STACK_OF_X509_free(allcerts);
0f113f3e
MC
942 }
943 }
944
2234212c 945 if (rctfile != NULL) {
0f113f3e 946 char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
6b83d032 947
75ebbd9a 948 if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {
0f113f3e
MC
949 BIO_printf(bio_err, "Can't open receipt file %s\n", rctfile);
950 goto end;
951 }
952
37578dc0 953 rcms = load_content_info(rctformat, rctin, 0, NULL, "receipt");
c1669f41 954 if (rcms == NULL)
0f113f3e 955 goto end;
0f113f3e
MC
956 }
957
5771017d
DDO
958 out = bio_open_default(outfile, 'w',
959 binary_files ? FORMAT_BINARY : outformat);
7e1b7485
RS
960 if (out == NULL)
961 goto end;
0f113f3e
MC
962
963 if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {
fd3397fc
RL
964 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
965 CAstore, noCAstore)) == NULL)
0f113f3e
MC
966 goto end;
967 X509_STORE_set_verify_cb(store, cms_cb);
7e1b7485 968 if (vpmtouched)
0f113f3e
MC
969 X509_STORE_set1_param(store, vpm);
970 }
971
972 ret = 3;
973
974 if (operation == SMIME_DATA_CREATE) {
7dc67708 975 cms = CMS_data_create_ex(in, flags, libctx, app_get0_propq());
0f113f3e 976 } else if (operation == SMIME_DIGEST_CREATE) {
7dc67708 977 cms = CMS_digest_create_ex(in, sign_md, flags, libctx, app_get0_propq());
0f113f3e
MC
978 } else if (operation == SMIME_COMPRESS) {
979 cms = CMS_compress(in, -1, flags);
980 } else if (operation == SMIME_ENCRYPT) {
981 int i;
982 flags |= CMS_PARTIAL;
7dc67708 983 cms = CMS_encrypt_ex(NULL, in, cipher, flags, libctx, app_get0_propq());
2234212c 984 if (cms == NULL)
0f113f3e
MC
985 goto end;
986 for (i = 0; i < sk_X509_num(encerts); i++) {
987 CMS_RecipientInfo *ri;
988 cms_key_param *kparam;
1b96cc70
DDO
989 int tflags = flags | CMS_KEY_PARAM;
990 /* This flag enforces allocating the EVP_PKEY_CTX for the recipient here */
71434aed 991 EVP_PKEY_CTX *pctx;
0f113f3e 992 X509 *x = sk_X509_value(encerts, i);
71434aed
DB
993 int res;
994
0f113f3e
MC
995 for (kparam = key_first; kparam; kparam = kparam->next) {
996 if (kparam->idx == i) {
0f113f3e
MC
997 break;
998 }
999 }
71434aed 1000 ri = CMS_add1_recipient(cms, x, key, originator, tflags);
2234212c 1001 if (ri == NULL)
0f113f3e 1002 goto end;
71434aed
DB
1003
1004 pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
2234212c 1005 if (kparam != NULL) {
0f113f3e
MC
1006 if (!cms_set_pkey_param(pctx, kparam->param))
1007 goto end;
1008 }
71434aed
DB
1009
1010 res = EVP_PKEY_CTX_ctrl(pctx, -1, -1,
1011 EVP_PKEY_CTRL_CIPHER,
ed576acd 1012 EVP_CIPHER_get_nid(cipher), NULL);
71434aed
DB
1013 if (res <= 0 && res != -2)
1014 goto end;
1015
0f113f3e 1016 if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE
5c42f7aa 1017 && wrap_cipher != NULL) {
0f113f3e
MC
1018 EVP_CIPHER_CTX *wctx;
1019 wctx = CMS_RecipientInfo_kari_get0_ctx(ri);
1020 EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);
1021 }
1022 }
1023
2234212c 1024 if (secret_key != NULL) {
0f113f3e
MC
1025 if (!CMS_add0_recipient_key(cms, NID_undef,
1026 secret_key, secret_keylen,
1027 secret_keyid, secret_keyidlen,
1028 NULL, NULL, NULL))
1029 goto end;
1030 /* NULL these because call absorbs them */
1031 secret_key = NULL;
1032 secret_keyid = NULL;
1033 }
2234212c 1034 if (pwri_pass != NULL) {
7644a9ae 1035 pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);
2234212c 1036 if (pwri_tmp == NULL)
0f113f3e 1037 goto end;
2234212c
PY
1038 if (CMS_add0_recipient_password(cms,
1039 -1, NID_undef, NID_undef,
1040 pwri_tmp, -1, NULL) == NULL)
0f113f3e
MC
1041 goto end;
1042 pwri_tmp = NULL;
1043 }
1044 if (!(flags & CMS_STREAM)) {
1045 if (!CMS_final(cms, in, NULL, flags))
1046 goto end;
1047 }
1048 } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
d8652be0 1049 cms = CMS_EncryptedData_encrypt_ex(in, cipher, secret_key,
7dc67708 1050 secret_keylen, flags, libctx, app_get0_propq());
0f113f3e
MC
1051
1052 } else if (operation == SMIME_SIGN_RECEIPT) {
1053 CMS_ContentInfo *srcms = NULL;
1054 STACK_OF(CMS_SignerInfo) *sis;
1055 CMS_SignerInfo *si;
1056 sis = CMS_get0_SignerInfos(cms);
2234212c 1057 if (sis == NULL)
0f113f3e
MC
1058 goto end;
1059 si = sk_CMS_SignerInfo_value(sis, 0);
1060 srcms = CMS_sign_receipt(si, signer, key, other, flags);
2234212c 1061 if (srcms == NULL)
0f113f3e
MC
1062 goto end;
1063 CMS_ContentInfo_free(cms);
1064 cms = srcms;
1065 } else if (operation & SMIME_SIGNERS) {
1066 int i;
1067 /*
07342bad
VS
1068 * If detached data content and not signing pre-computed digest, we
1069 * enable streaming if S/MIME output format.
0f113f3e
MC
1070 */
1071 if (operation == SMIME_SIGN) {
1072
07342bad 1073 if ((flags & CMS_DETACHED) != 0 && digestbin == NULL) {
0f113f3e
MC
1074 if (outformat == FORMAT_SMIME)
1075 flags |= CMS_STREAM;
1076 }
1077 flags |= CMS_PARTIAL;
7dc67708 1078 cms = CMS_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
2234212c 1079 if (cms == NULL)
0f113f3e 1080 goto end;
2234212c 1081 if (econtent_type != NULL)
0f113f3e
MC
1082 CMS_set1_eContentType(cms, econtent_type);
1083
1b96cc70
DDO
1084 if (rr_to != NULL
1085 && ((rr = make_receipt_request(rr_to, rr_allorfirst, rr_from))
1086 == NULL)) {
1087 BIO_puts(bio_err, "Signed Receipt Request Creation Error\n");
1088 goto end;
0f113f3e 1089 }
2234212c 1090 } else {
0f113f3e 1091 flags |= CMS_REUSE_DIGEST;
2234212c 1092 }
0f113f3e
MC
1093 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
1094 CMS_SignerInfo *si;
1095 cms_key_param *kparam;
1096 int tflags = flags;
1097 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
1098 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
1099
d382e796 1100 signer = load_cert(signerfile, FORMAT_UNDEF, "signer certificate");
a032ed0a
KS
1101 if (signer == NULL) {
1102 ret = 2;
0f113f3e 1103 goto end;
a032ed0a 1104 }
50eb2a50 1105 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
a032ed0a
KS
1106 if (key == NULL) {
1107 ret = 2;
0f113f3e 1108 goto end;
a032ed0a 1109 }
a10847c4 1110
0f113f3e
MC
1111 for (kparam = key_first; kparam; kparam = kparam->next) {
1112 if (kparam->idx == i) {
1113 tflags |= CMS_KEY_PARAM;
1114 break;
1115 }
1116 }
1117 si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
2234212c 1118 if (si == NULL)
0f113f3e 1119 goto end;
2234212c 1120 if (kparam != NULL) {
0f113f3e
MC
1121 EVP_PKEY_CTX *pctx;
1122 pctx = CMS_SignerInfo_get0_pkey_ctx(si);
1123 if (!cms_set_pkey_param(pctx, kparam->param))
1124 goto end;
1125 }
2234212c 1126 if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))
0f113f3e
MC
1127 goto end;
1128 X509_free(signer);
1129 signer = NULL;
1130 EVP_PKEY_free(key);
1131 key = NULL;
1132 }
1133 /* If not streaming or resigning finalize structure */
07342bad
VS
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))
1138 goto end;
1139 } else if (operation == SMIME_SIGN && (flags & CMS_STREAM) == 0) {
0f113f3e
MC
1140 if (!CMS_final(cms, in, NULL, flags))
1141 goto end;
1142 }
1143 }
1144
2234212c 1145 if (cms == NULL) {
0f113f3e
MC
1146 BIO_printf(bio_err, "Error creating CMS structure\n");
1147 goto end;
1148 }
1149
1150 ret = 4;
1151 if (operation == SMIME_DECRYPT) {
1152 if (flags & CMS_DEBUG_DECRYPT)
1153 CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
1154
2234212c 1155 if (secret_key != NULL) {
0f113f3e
MC
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");
1160 goto end;
1161 }
1162 }
1163
2234212c 1164 if (key != NULL) {
71434aed 1165 if (!CMS_decrypt_set1_pkey_and_peer(cms, key, recip, originator)) {
0f113f3e
MC
1166 BIO_puts(bio_err, "Error decrypting CMS using private key\n");
1167 goto end;
1168 }
1169 }
1170
2234212c 1171 if (pwri_pass != NULL) {
0f113f3e
MC
1172 if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
1173 BIO_puts(bio_err, "Error decrypting CMS using password\n");
1174 goto end;
1175 }
1176 }
1177
1178 if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
1179 BIO_printf(bio_err, "Error decrypting CMS structure\n");
1180 goto end;
1181 }
340cf875 1182 } else if (operation == SMIME_DATA_OUT) {
0f113f3e
MC
1183 if (!CMS_data(cms, out, flags))
1184 goto end;
1185 } else if (operation == SMIME_UNCOMPRESS) {
1186 if (!CMS_uncompress(cms, indata, out, flags))
1187 goto end;
1188 } else if (operation == SMIME_DIGEST_VERIFY) {
2234212c 1189 if (CMS_digest_verify(cms, indata, out, flags) > 0) {
0f113f3e 1190 BIO_printf(bio_err, "Verification successful\n");
2234212c 1191 } else {
0f113f3e
MC
1192 BIO_printf(bio_err, "Verification failure\n");
1193 goto end;
1194 }
1195 } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
1196 if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
1197 indata, out, flags))
1198 goto end;
1199 } else if (operation == SMIME_VERIFY) {
2234212c 1200 if (CMS_verify(cms, other, store, indata, out, flags) > 0) {
9e3c510b 1201 BIO_printf(bio_err, "%s Verification successful\n",
56c98a7d 1202 (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
2234212c 1203 } else {
56c98a7d
DDO
1204 BIO_printf(bio_err, "%s Verification failure\n",
1205 (flags & CMS_CADES) != 0 ? "CAdES" : "CMS");
0f113f3e
MC
1206 if (verify_retcode)
1207 ret = verify_err + 32;
1208 goto end;
1209 }
2234212c 1210 if (signerfile != NULL) {
340cf875
DDO
1211 STACK_OF(X509) *signers = CMS_get0_signers(cms);
1212
0f113f3e
MC
1213 if (!save_certs(signerfile, signers)) {
1214 BIO_printf(bio_err,
1215 "Error writing signers to %s\n", signerfile);
1216 ret = 5;
1217 goto end;
1218 }
1219 sk_X509_free(signers);
1220 }
1221 if (rr_print)
ecf3a1fb 1222 receipt_request_print(cms);
0f113f3e
MC
1223
1224 } else if (operation == SMIME_VERIFY_RECEIPT) {
2234212c 1225 if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {
0f113f3e 1226 BIO_printf(bio_err, "Verification successful\n");
2234212c 1227 } else {
0f113f3e
MC
1228 BIO_printf(bio_err, "Verification failure\n");
1229 goto end;
1230 }
1231 } else {
1232 if (noout) {
6ec351f4
DB
1233 if (print) {
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());
1241 }
1242 }
1243 CMS_ContentInfo_print_ctx(out, cms, 0, pctx);
1244 ASN1_PCTX_free(pctx);
1245 }
0f113f3e
MC
1246 } else if (outformat == FORMAT_SMIME) {
1247 if (to)
2197494d 1248 BIO_printf(out, "To: %s%s", to, mime_eol);
0f113f3e 1249 if (from)
2197494d 1250 BIO_printf(out, "From: %s%s", from, mime_eol);
0f113f3e 1251 if (subject)
2197494d 1252 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
0f113f3e
MC
1253 if (operation == SMIME_RESIGN)
1254 ret = SMIME_write_CMS(out, cms, indata, flags);
1255 else
1256 ret = SMIME_write_CMS(out, cms, in, flags);
2234212c 1257 } else if (outformat == FORMAT_PEM) {
0f113f3e 1258 ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
2234212c 1259 } else if (outformat == FORMAT_ASN1) {
0f113f3e 1260 ret = i2d_CMS_bio_stream(out, cms, in, flags);
2234212c 1261 } else {
0f113f3e
MC
1262 BIO_printf(bio_err, "Bad output format for CMS file\n");
1263 goto end;
1264 }
1265 if (ret <= 0) {
1266 ret = 6;
1267 goto end;
1268 }
1269 }
1270 ret = 0;
1271 end:
1272 if (ret)
1273 ERR_print_errors(bio_err);
79b2a2f2
DDO
1274 OSSL_STACK_OF_X509_free(encerts);
1275 OSSL_STACK_OF_X509_free(other);
7e1b7485 1276 X509_VERIFY_PARAM_free(vpm);
25aaa98a
RS
1277 sk_OPENSSL_STRING_free(sksigners);
1278 sk_OPENSSL_STRING_free(skkeys);
b548a1f1
RS
1279 OPENSSL_free(secret_key);
1280 OPENSSL_free(secret_keyid);
1281 OPENSSL_free(pwri_tmp);
0dfb9398 1282 ASN1_OBJECT_free(econtent_type);
25aaa98a
RS
1283 CMS_ReceiptRequest_free(rr);
1284 sk_OPENSSL_STRING_free(rr_to);
1285 sk_OPENSSL_STRING_free(rr_from);
0f113f3e
MC
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);
1291 key_param = tparam;
1292 }
1293 X509_STORE_free(store);
1294 X509_free(cert);
1295 X509_free(recip);
1296 X509_free(signer);
1297 EVP_PKEY_free(key);
606a417f
RS
1298 EVP_CIPHER_free(cipher);
1299 EVP_CIPHER_free(wrap_cipher);
1300 EVP_MD_free(sign_md);
0f113f3e
MC
1301 CMS_ContentInfo_free(cms);
1302 CMS_ContentInfo_free(rcms);
dd1abd44 1303 release_engine(e);
0f113f3e
MC
1304 BIO_free(rctin);
1305 BIO_free(in);
1306 BIO_free(indata);
1307 BIO_free_all(out);
07342bad 1308 OPENSSL_free(digestbin);
b548a1f1 1309 OPENSSL_free(passin);
c1669f41 1310 NCONF_free(conf);
26a7d938 1311 return ret;
8931b30d
DSH
1312}
1313
1314static int save_certs(char *signerfile, STACK_OF(X509) *signers)
0f113f3e
MC
1315{
1316 int i;
1317 BIO *tmp;
2234212c 1318 if (signerfile == NULL)
0f113f3e
MC
1319 return 1;
1320 tmp = BIO_new_file(signerfile, "w");
2234212c 1321 if (tmp == NULL)
0f113f3e
MC
1322 return 0;
1323 for (i = 0; i < sk_X509_num(signers); i++)
1324 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
1325 BIO_free(tmp);
1326 return 1;
1327}
8931b30d
DSH
1328
1329/* Minimal callback just to output policy info (if any) */
1330
7f50d9a4 1331static int cms_cb(int ok, X509_STORE_CTX *ctx)
0f113f3e
MC
1332{
1333 int error;
8931b30d 1334
0f113f3e 1335 error = X509_STORE_CTX_get_error(ctx);
8931b30d 1336
0f113f3e 1337 verify_err = error;
db50661f 1338
0f113f3e
MC
1339 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
1340 && ((error != X509_V_OK) || (ok != 2)))
1341 return ok;
8931b30d 1342
ecf3a1fb 1343 policies_print(ctx);
8931b30d 1344
0f113f3e 1345 return ok;
8931b30d 1346
0f113f3e 1347}
8931b30d 1348
ecf3a1fb 1349static void gnames_stack_print(STACK_OF(GENERAL_NAMES) *gns)
0f113f3e
MC
1350{
1351 STACK_OF(GENERAL_NAME) *gens;
1352 GENERAL_NAME *gen;
1353 int i, j;
ecf3a1fb 1354
0f113f3e
MC
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);
ecf3a1fb
RS
1359 BIO_puts(bio_err, " ");
1360 GENERAL_NAME_print(bio_err, gen);
1361 BIO_puts(bio_err, "\n");
0f113f3e
MC
1362 }
1363 }
1364 return;
1365}
f4cc56f4 1366
ecf3a1fb 1367static void receipt_request_print(CMS_ContentInfo *cms)
0f113f3e
MC
1368{
1369 STACK_OF(CMS_SignerInfo) *sis;
1370 CMS_SignerInfo *si;
1371 CMS_ReceiptRequest *rr;
1372 int allorfirst;
1373 STACK_OF(GENERAL_NAMES) *rto, *rlist;
1374 ASN1_STRING *scid;
1375 int i, rv;
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);
2234212c 1381 if (rv == 0) {
0f113f3e 1382 BIO_puts(bio_err, " No Receipt Request\n");
2234212c 1383 } else if (rv < 0) {
0f113f3e
MC
1384 BIO_puts(bio_err, " Receipt Request Parse Error\n");
1385 ERR_print_errors(bio_err);
1386 } else {
17ebf85a 1387 const char *id;
0f113f3e
MC
1388 int idlen;
1389 CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
1390 &rlist, &rto);
ecf3a1fb 1391 BIO_puts(bio_err, " Signed Content ID:\n");
0f113f3e 1392 idlen = ASN1_STRING_length(scid);
17ebf85a 1393 id = (const char *)ASN1_STRING_get0_data(scid);
ecf3a1fb
RS
1394 BIO_dump_indent(bio_err, id, idlen, 4);
1395 BIO_puts(bio_err, " Receipts From");
2234212c 1396 if (rlist != NULL) {
ecf3a1fb
RS
1397 BIO_puts(bio_err, " List:\n");
1398 gnames_stack_print(rlist);
2234212c 1399 } else if (allorfirst == 1) {
ecf3a1fb 1400 BIO_puts(bio_err, ": First Tier\n");
2234212c 1401 } else if (allorfirst == 0) {
ecf3a1fb 1402 BIO_puts(bio_err, ": All\n");
2234212c 1403 } else {
ecf3a1fb 1404 BIO_printf(bio_err, " Unknown (%d)\n", allorfirst);
2234212c 1405 }
ecf3a1fb
RS
1406 BIO_puts(bio_err, " Receipts To:\n");
1407 gnames_stack_print(rto);
0f113f3e 1408 }
25aaa98a 1409 CMS_ReceiptRequest_free(rr);
0f113f3e
MC
1410 }
1411}
f4cc56f4 1412
c869da88 1413static STACK_OF(GENERAL_NAMES) *make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
0f113f3e
MC
1414{
1415 int i;
1416 STACK_OF(GENERAL_NAMES) *ret;
1417 GENERAL_NAMES *gens = NULL;
1418 GENERAL_NAME *gen = NULL;
1419 ret = sk_GENERAL_NAMES_new_null();
2234212c 1420 if (ret == NULL)
0f113f3e
MC
1421 goto err;
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);
2234212c 1425 if (gen == NULL)
0f113f3e
MC
1426 goto err;
1427 gens = GENERAL_NAMES_new();
96487cdd 1428 if (gens == NULL)
0f113f3e
MC
1429 goto err;
1430 if (!sk_GENERAL_NAME_push(gens, gen))
1431 goto err;
1432 gen = NULL;
1433 if (!sk_GENERAL_NAMES_push(ret, gens))
1434 goto err;
1435 gens = NULL;
1436 }
1437
1438 return ret;
1439
1440 err:
25aaa98a
RS
1441 sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
1442 GENERAL_NAMES_free(gens);
1443 GENERAL_NAME_free(gen);
0f113f3e
MC
1444 return NULL;
1445}
1446
1b96cc70
DDO
1447static CMS_ReceiptRequest
1448*make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
1449 STACK_OF(OPENSSL_STRING) *rr_from)
0f113f3e 1450{
6e4ab54b 1451 STACK_OF(GENERAL_NAMES) *rct_to = NULL, *rct_from = NULL;
1b96cc70 1452
0f113f3e 1453 rct_to = make_names_stack(rr_to);
2234212c 1454 if (rct_to == NULL)
0f113f3e 1455 goto err;
2234212c 1456 if (rr_from != NULL) {
0f113f3e 1457 rct_from = make_names_stack(rr_from);
2234212c 1458 if (rct_from == NULL)
0f113f3e 1459 goto err;
2234212c 1460 } else {
0f113f3e 1461 rct_from = NULL;
2234212c 1462 }
1b96cc70
DDO
1463 return CMS_ReceiptRequest_create0_ex(NULL, -1, rr_allorfirst, rct_from,
1464 rct_to, app_get0_libctx());
0f113f3e 1465 err:
6e4ab54b 1466 sk_GENERAL_NAMES_pop_free(rct_to, GENERAL_NAMES_free);
0f113f3e
MC
1467 return NULL;
1468}
f5e2354c 1469
02498cc8 1470static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
0f113f3e
MC
1471 STACK_OF(OPENSSL_STRING) *param)
1472{
1473 char *keyopt;
1474 int i;
1475 if (sk_OPENSSL_STRING_num(param) <= 0)
1476 return 1;
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);
1482 return 0;
1483 }
1484 }
1485 return 1;
1486}