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