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