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