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