]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/smime.c
Process digest option after loading providers
[thirdparty/openssl.git] / apps / smime.c
CommitLineData
0f113f3e 1/*
4333b89f 2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
5a9a4b29 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
5a9a4b29
DSH
8 */
9
10/* S/MIME utility function */
11
12#include <stdio.h>
13#include <string.h>
36217a94 14#include "apps.h"
dab2cd68 15#include "progs.h"
11afb40c 16#include <openssl/crypto.h>
5a9a4b29
DSH
17#include <openssl/pem.h>
18#include <openssl/err.h>
5d7c222d
DSH
19#include <openssl/x509_vfy.h>
20#include <openssl/x509v3.h>
5a9a4b29 21
55ec5861 22static int save_certs(char *signerfile, STACK_OF(X509) *signers);
5d7c222d 23static int smime_cb(int ok, X509_STORE_CTX *ctx);
5a9a4b29 24
0f113f3e
MC
25#define SMIME_OP 0x10
26#define SMIME_IP 0x20
27#define SMIME_SIGNERS 0x40
28#define SMIME_ENCRYPT (1 | SMIME_OP)
29#define SMIME_DECRYPT (2 | SMIME_IP)
30#define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
31#define SMIME_VERIFY (4 | SMIME_IP)
32#define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
33#define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
5a9a4b29 34
7e1b7485
RS
35typedef enum OPTION_choice {
36 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
37 OPT_ENCRYPT, OPT_DECRYPT, OPT_SIGN, OPT_RESIGN, OPT_VERIFY,
38 OPT_PK7OUT, OPT_TEXT, OPT_NOINTERN, OPT_NOVERIFY, OPT_NOCHAIN,
39 OPT_NOCERTS, OPT_NOATTR, OPT_NODETACH, OPT_NOSMIMECAP,
40 OPT_BINARY, OPT_NOSIGS, OPT_STREAM, OPT_INDEF, OPT_NOINDEF,
3ee1eac2 41 OPT_CRLFEOL, OPT_ENGINE, OPT_PASSIN,
7e1b7485
RS
42 OPT_TO, OPT_FROM, OPT_SUBJECT, OPT_SIGNER, OPT_RECIP, OPT_MD,
43 OPT_CIPHER, OPT_INKEY, OPT_KEYFORM, OPT_CERTFILE, OPT_CAFILE,
fd3397fc 44 OPT_CAPATH, OPT_CASTORE, OPT_NOCAFILE, OPT_NOCAPATH, OPT_NOCASTORE,
90a1f2d7 45 OPT_R_ENUM, OPT_PROV_ENUM, OPT_CONFIG,
7e1b7485 46 OPT_V_ENUM,
fd3397fc 47 OPT_IN, OPT_INFORM, OPT_OUT,
2b6bcb70 48 OPT_OUTFORM, OPT_CONTENT
7e1b7485
RS
49} OPTION_CHOICE;
50
44c83ebd 51const OPTIONS smime_options[] = {
92de469f 52 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [cert...]\n"},
5388f986
RS
53
54 OPT_SECTION("General"),
7e1b7485 55 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
56 {"in", OPT_IN, '<', "Input file"},
57 {"inform", OPT_INFORM, 'c', "Input format SMIME (default), PEM or DER"},
58 {"out", OPT_OUT, '>', "Output file"},
59 {"outform", OPT_OUTFORM, 'c',
60 "Output format SMIME (default), PEM or DER"},
61 {"inkey", OPT_INKEY, 's',
62 "Input private key (if not signer or recipient)"},
6d382c74 63 {"keyform", OPT_KEYFORM, 'f', "Input private key format (ENGINE, other values ignored)"},
5388f986
RS
64#ifndef OPENSSL_NO_ENGINE
65 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
66#endif
67 {"stream", OPT_STREAM, '-', "Enable CMS streaming" },
68 {"indef", OPT_INDEF, '-', "Same as -stream" },
69 {"noindef", OPT_NOINDEF, '-', "Disable CMS streaming"},
90a1f2d7 70 OPT_CONFIG_OPTION,
5388f986
RS
71
72 OPT_SECTION("Action"),
7e1b7485
RS
73 {"encrypt", OPT_ENCRYPT, '-', "Encrypt message"},
74 {"decrypt", OPT_DECRYPT, '-', "Decrypt encrypted message"},
75 {"sign", OPT_SIGN, '-', "Sign message"},
5388f986 76 {"resign", OPT_RESIGN, '-', "Resign a signed message"},
7e1b7485 77 {"verify", OPT_VERIFY, '-', "Verify signed message"},
5388f986
RS
78
79 OPT_SECTION("Signing/Encryption"),
80 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
81 {"md", OPT_MD, 's', "Digest algorithm to use when signing or resigning"},
82 {"", OPT_CIPHER, '-', "Any supported cipher"},
7e1b7485
RS
83 {"pk7out", OPT_PK7OUT, '-', "Output PKCS#7 structure"},
84 {"nointern", OPT_NOINTERN, '-',
85 "Don't search certificates in message for signer"},
7e1b7485
RS
86 {"nodetach", OPT_NODETACH, '-', "Use opaque signing"},
87 {"noattr", OPT_NOATTR, '-', "Don't include any signed attributes"},
88 {"binary", OPT_BINARY, '-', "Don't translate message to text"},
f47e5647 89 {"signer", OPT_SIGNER, 's', "Signer certificate file"},
7e1b7485
RS
90 {"content", OPT_CONTENT, '<',
91 "Supply or override content for detached signature"},
5388f986
RS
92 {"nocerts", OPT_NOCERTS, '-',
93 "Don't include signers certificate when signing"},
94
95 OPT_SECTION("Verification/Decryption"),
96 {"nosigs", OPT_NOSIGS, '-', "Don't verify message signature"},
97 {"noverify", OPT_NOVERIFY, '-', "Don't verify signers certificate"},
98
99 {"certfile", OPT_CERTFILE, '<', "Other certificates file"},
100 {"recip", OPT_RECIP, '<', "Recipient certificate file for decryption"},
101
102 OPT_SECTION("Email"),
7e1b7485
RS
103 {"to", OPT_TO, 's', "To address"},
104 {"from", OPT_FROM, 's', "From address"},
105 {"subject", OPT_SUBJECT, 's', "Subject"},
106 {"text", OPT_TEXT, '-', "Include or delete text MIME headers"},
5388f986
RS
107 {"nosmimecap", OPT_NOSMIMECAP, '-', "Omit the SMIMECapabilities attribute"},
108
109 OPT_SECTION("Certificate chain"),
7e1b7485
RS
110 {"CApath", OPT_CAPATH, '/', "Trusted certificates directory"},
111 {"CAfile", OPT_CAFILE, '<', "Trusted certificates file"},
fd3397fc 112 {"CAstore", OPT_CASTORE, ':', "Trusted certificates store URI"},
2b6bcb70
MC
113 {"no-CAfile", OPT_NOCAFILE, '-',
114 "Do not load the default certificates file"},
115 {"no-CApath", OPT_NOCAPATH, '-',
116 "Do not load certificates from the default certificates directory"},
fd3397fc
RL
117 {"no-CAstore", OPT_NOCASTORE, '-',
118 "Do not load certificates from the default certificates store"},
609b0852 119 {"nochain", OPT_NOCHAIN, '-',
12d56b29 120 "set PKCS7_NOCHAIN so certificates contained in the message are not used as untrusted CAs" },
12d56b29 121 {"crlfeol", OPT_CRLFEOL, '-', "Use CRLF as EOL termination instead of CR only"},
5388f986 122
3ee1eac2 123 OPT_R_OPTIONS,
7e1b7485 124 OPT_V_OPTIONS,
6bd4e3f2 125 OPT_PROV_OPTIONS,
92de469f
RS
126
127 OPT_PARAMETERS(),
128 {"cert", 0, 0, "Recipient certs, used when encrypting"},
7e1b7485
RS
129 {NULL}
130};
667ac4ec 131
7e1b7485 132int smime_main(int argc, char **argv)
0f113f3e 133{
90a1f2d7 134 CONF *conf = NULL;
7e1b7485 135 BIO *in = NULL, *out = NULL, *indata = NULL;
0f113f3e 136 EVP_PKEY *key = NULL;
7e1b7485
RS
137 PKCS7 *p7 = NULL;
138 STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
0f113f3e 139 STACK_OF(X509) *encerts = NULL, *other = NULL;
7e1b7485
RS
140 X509 *cert = NULL, *recip = NULL, *signer = NULL;
141 X509_STORE *store = NULL;
142 X509_VERIFY_PARAM *vpm = NULL;
143 const EVP_CIPHER *cipher = NULL;
0f113f3e 144 const EVP_MD *sign_md = NULL;
fd3397fc 145 const char *CAfile = NULL, *CApath = NULL, *CAstore = NULL, *prog = NULL;
3ee1eac2
RS
146 char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
147 char *infile = NULL, *outfile = NULL, *signerfile = NULL, *recipfile = NULL;
d0190e11
RS
148 char *passinarg = NULL, *passin = NULL, *to = NULL, *from = NULL;
149 char *subject = NULL, *digestname = NULL;
7e1b7485 150 OPTION_CHOICE o;
fd3397fc 151 int noCApath = 0, noCAfile = 0, noCAstore = 0;
3ee1eac2 152 int flags = PKCS7_DETACHED, operation = 0, ret = 0, indef = 0;
7e1b7485
RS
153 int informat = FORMAT_SMIME, outformat = FORMAT_SMIME, keyform =
154 FORMAT_PEM;
155 int vpmtouched = 0, rv = 0;
7e1b7485 156 ENGINE *e = NULL;
2197494d 157 const char *mime_eol = "\n";
b4250010 158 OSSL_LIB_CTX *libctx = app_get0_libctx();
5a9a4b29 159
7e1b7485
RS
160 if ((vpm = X509_VERIFY_PARAM_new()) == NULL)
161 return 1;
0f113f3e 162
7e1b7485
RS
163 prog = opt_init(argc, argv, smime_options);
164 while ((o = opt_next()) != OPT_EOF) {
165 switch (o) {
166 case OPT_EOF:
167 case OPT_ERR:
168 opthelp:
169 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
170 goto end;
171 case OPT_HELP:
172 opt_help(smime_options);
173 ret = 0;
174 goto end;
175 case OPT_INFORM:
f47e5647 176 if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))
7e1b7485
RS
177 goto opthelp;
178 break;
179 case OPT_IN:
180 infile = opt_arg();
181 break;
182 case OPT_OUTFORM:
f47e5647 183 if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))
7e1b7485
RS
184 goto opthelp;
185 break;
186 case OPT_OUT:
187 outfile = opt_arg();
188 break;
189 case OPT_ENCRYPT:
0f113f3e 190 operation = SMIME_ENCRYPT;
7e1b7485
RS
191 break;
192 case OPT_DECRYPT:
0f113f3e 193 operation = SMIME_DECRYPT;
7e1b7485
RS
194 break;
195 case OPT_SIGN:
0f113f3e 196 operation = SMIME_SIGN;
7e1b7485
RS
197 break;
198 case OPT_RESIGN:
0f113f3e 199 operation = SMIME_RESIGN;
7e1b7485
RS
200 break;
201 case OPT_VERIFY:
0f113f3e 202 operation = SMIME_VERIFY;
7e1b7485
RS
203 break;
204 case OPT_PK7OUT:
0f113f3e 205 operation = SMIME_PK7OUT;
7e1b7485
RS
206 break;
207 case OPT_TEXT:
0f113f3e 208 flags |= PKCS7_TEXT;
7e1b7485
RS
209 break;
210 case OPT_NOINTERN:
0f113f3e 211 flags |= PKCS7_NOINTERN;
7e1b7485
RS
212 break;
213 case OPT_NOVERIFY:
0f113f3e 214 flags |= PKCS7_NOVERIFY;
7e1b7485
RS
215 break;
216 case OPT_NOCHAIN:
0f113f3e 217 flags |= PKCS7_NOCHAIN;
7e1b7485
RS
218 break;
219 case OPT_NOCERTS:
0f113f3e 220 flags |= PKCS7_NOCERTS;
7e1b7485
RS
221 break;
222 case OPT_NOATTR:
0f113f3e 223 flags |= PKCS7_NOATTR;
7e1b7485
RS
224 break;
225 case OPT_NODETACH:
0f113f3e 226 flags &= ~PKCS7_DETACHED;
7e1b7485
RS
227 break;
228 case OPT_NOSMIMECAP:
0f113f3e 229 flags |= PKCS7_NOSMIMECAP;
7e1b7485
RS
230 break;
231 case OPT_BINARY:
0f113f3e 232 flags |= PKCS7_BINARY;
7e1b7485
RS
233 break;
234 case OPT_NOSIGS:
0f113f3e 235 flags |= PKCS7_NOSIGS;
7e1b7485
RS
236 break;
237 case OPT_STREAM:
238 case OPT_INDEF:
0f113f3e 239 indef = 1;
7e1b7485
RS
240 break;
241 case OPT_NOINDEF:
0f113f3e 242 indef = 0;
7e1b7485 243 break;
7e1b7485 244 case OPT_CRLFEOL:
0f113f3e 245 flags |= PKCS7_CRLFEOL;
2197494d 246 mime_eol = "\r\n";
7e1b7485 247 break;
3ee1eac2
RS
248 case OPT_R_CASES:
249 if (!opt_rand(o))
250 goto end;
7e1b7485 251 break;
6bd4e3f2
P
252 case OPT_PROV_CASES:
253 if (!opt_provider(o))
254 goto end;
255 break;
90a1f2d7
SL
256 case OPT_CONFIG:
257 conf = app_load_config_modules(opt_arg());
258 if (conf == NULL)
259 goto end;
260 break;
7e1b7485 261 case OPT_ENGINE:
333b070e 262 e = setup_engine(opt_arg(), 0);
7e1b7485
RS
263 break;
264 case OPT_PASSIN:
265 passinarg = opt_arg();
266 break;
267 case OPT_TO:
268 to = opt_arg();
269 break;
270 case OPT_FROM:
271 from = opt_arg();
272 break;
273 case OPT_SUBJECT:
274 subject = opt_arg();
275 break;
276 case OPT_SIGNER:
0f113f3e 277 /* If previous -signer argument add signer to list */
2234212c 278 if (signerfile != NULL) {
7e1b7485
RS
279 if (sksigners == NULL
280 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
281 goto end;
0f113f3e 282 sk_OPENSSL_STRING_push(sksigners, signerfile);
7e1b7485 283 if (keyfile == NULL)
0f113f3e 284 keyfile = signerfile;
7e1b7485
RS
285 if (skkeys == NULL
286 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
287 goto end;
0f113f3e
MC
288 sk_OPENSSL_STRING_push(skkeys, keyfile);
289 keyfile = NULL;
290 }
7e1b7485
RS
291 signerfile = opt_arg();
292 break;
293 case OPT_RECIP:
294 recipfile = opt_arg();
295 break;
296 case OPT_MD:
d0190e11 297 digestname = opt_arg();
7e1b7485
RS
298 break;
299 case OPT_CIPHER:
300 if (!opt_cipher(opt_unknown(), &cipher))
301 goto opthelp;
302 break;
303 case OPT_INKEY:
ceab33e2 304 /* If previous -inkey argument add signer to list */
2234212c 305 if (keyfile != NULL) {
7e1b7485
RS
306 if (signerfile == NULL) {
307 BIO_printf(bio_err,
308 "%s: Must have -signer before -inkey\n", prog);
309 goto opthelp;
0f113f3e 310 }
7e1b7485
RS
311 if (sksigners == NULL
312 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
313 goto end;
0f113f3e
MC
314 sk_OPENSSL_STRING_push(sksigners, signerfile);
315 signerfile = NULL;
7e1b7485
RS
316 if (skkeys == NULL
317 && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
318 goto end;
0f113f3e
MC
319 sk_OPENSSL_STRING_push(skkeys, keyfile);
320 }
7e1b7485
RS
321 keyfile = opt_arg();
322 break;
323 case OPT_KEYFORM:
324 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
325 goto opthelp;
326 break;
327 case OPT_CERTFILE:
328 certfile = opt_arg();
329 break;
330 case OPT_CAFILE:
331 CAfile = opt_arg();
332 break;
333 case OPT_CAPATH:
334 CApath = opt_arg();
335 break;
fd3397fc
RL
336 case OPT_CASTORE:
337 CAstore = opt_arg();
338 break;
2b6bcb70
MC
339 case OPT_NOCAFILE:
340 noCAfile = 1;
341 break;
342 case OPT_NOCAPATH:
343 noCApath = 1;
344 break;
fd3397fc
RL
345 case OPT_NOCASTORE:
346 noCAstore = 1;
347 break;
7e1b7485
RS
348 case OPT_CONTENT:
349 contfile = opt_arg();
350 break;
351 case OPT_V_CASES:
352 if (!opt_verify(o, vpm))
353 goto opthelp;
354 vpmtouched++;
355 break;
356 }
0f113f3e 357 }
021410ea
RS
358
359 /* Extra arguments are files with recipient keys. */
7e1b7485
RS
360 argc = opt_num_rest();
361 argv = opt_rest();
0f113f3e 362
51e5df0e 363 app_RAND_load();
d0190e11
RS
364 if (digestname != NULL) {
365 if (!opt_md(digestname, &sign_md))
366 goto opthelp;
367 }
2234212c 368 if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {
0f113f3e 369 BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
7e1b7485 370 goto opthelp;
0f113f3e 371 }
021410ea
RS
372 if (!operation) {
373 BIO_puts(bio_err,
374 "No operation (-encrypt|-sign|...) specified\n");
375 goto opthelp;
376 }
0f113f3e
MC
377
378 if (operation & SMIME_SIGNERS) {
379 /* Check to see if any final signer needs to be appended */
380 if (keyfile && !signerfile) {
381 BIO_puts(bio_err, "Illegal -inkey without -signer\n");
7e1b7485 382 goto opthelp;
0f113f3e 383 }
2234212c
PY
384 if (signerfile != NULL) {
385 if (sksigners == NULL
7e1b7485
RS
386 && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)
387 goto end;
0f113f3e 388 sk_OPENSSL_STRING_push(sksigners, signerfile);
7e1b7485
RS
389 if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)
390 goto end;
0f113f3e
MC
391 if (!keyfile)
392 keyfile = signerfile;
393 sk_OPENSSL_STRING_push(skkeys, keyfile);
394 }
2234212c 395 if (sksigners == NULL) {
0f113f3e 396 BIO_printf(bio_err, "No signer certificate specified\n");
7e1b7485 397 goto opthelp;
0f113f3e
MC
398 }
399 signerfile = NULL;
400 keyfile = NULL;
0f113f3e 401 } else if (operation == SMIME_DECRYPT) {
2234212c 402 if (recipfile == NULL && keyfile == NULL) {
0f113f3e
MC
403 BIO_printf(bio_err,
404 "No recipient certificate or key specified\n");
7e1b7485 405 goto opthelp;
0f113f3e
MC
406 }
407 } else if (operation == SMIME_ENCRYPT) {
7e1b7485 408 if (argc == 0) {
0f113f3e 409 BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
7e1b7485 410 goto opthelp;
0f113f3e 411 }
2234212c 412 }
7e1b7485 413
7e1b7485 414 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
0f113f3e
MC
415 BIO_printf(bio_err, "Error getting password\n");
416 goto end;
417 }
418
0f113f3e
MC
419 ret = 2;
420
421 if (!(operation & SMIME_SIGNERS))
422 flags &= ~PKCS7_DETACHED;
423
bdd58d98 424 if (!(operation & SMIME_OP)) {
0f113f3e 425 if (flags & PKCS7_BINARY)
bdd58d98 426 outformat = FORMAT_BINARY;
0f113f3e
MC
427 }
428
bdd58d98 429 if (!(operation & SMIME_IP)) {
0f113f3e 430 if (flags & PKCS7_BINARY)
bdd58d98 431 informat = FORMAT_BINARY;
0f113f3e
MC
432 }
433
434 if (operation == SMIME_ENCRYPT) {
2234212c 435 if (cipher == NULL) {
0f113f3e
MC
436#ifndef OPENSSL_NO_DES
437 cipher = EVP_des_ede3_cbc();
63da21c0 438#else
0f113f3e
MC
439 BIO_printf(bio_err, "No cipher selected\n");
440 goto end;
63da21c0 441#endif
0f113f3e
MC
442 }
443 encerts = sk_X509_new_null();
2234212c 444 if (encerts == NULL)
7e1b7485 445 goto end;
2234212c 446 while (*argv != NULL) {
22dddfb9 447 cert = load_cert(*argv, "recipient certificate file");
7e1b7485 448 if (cert == NULL)
0f113f3e 449 goto end;
0f113f3e
MC
450 sk_X509_push(encerts, cert);
451 cert = NULL;
7e1b7485 452 argv++;
0f113f3e
MC
453 }
454 }
455
2234212c 456 if (certfile != NULL) {
b3c5aadf 457 if (!load_certs(certfile, &other, NULL, "certificates")) {
0f113f3e
MC
458 ERR_print_errors(bio_err);
459 goto end;
460 }
461 }
462
2234212c 463 if (recipfile != NULL && (operation == SMIME_DECRYPT)) {
22dddfb9 464 if ((recip = load_cert(recipfile,
a773b52a 465 "recipient certificate file")) == NULL) {
0f113f3e
MC
466 ERR_print_errors(bio_err);
467 goto end;
468 }
469 }
470
471 if (operation == SMIME_DECRYPT) {
2234212c 472 if (keyfile == NULL)
0f113f3e
MC
473 keyfile = recipfile;
474 } else if (operation == SMIME_SIGN) {
2234212c 475 if (keyfile == NULL)
0f113f3e 476 keyfile = signerfile;
2234212c 477 } else {
0f113f3e 478 keyfile = NULL;
2234212c 479 }
0f113f3e 480
2234212c 481 if (keyfile != NULL) {
50eb2a50 482 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
2234212c 483 if (key == NULL)
0f113f3e
MC
484 goto end;
485 }
486
bdd58d98 487 in = bio_open_default(infile, 'r', informat);
7e1b7485
RS
488 if (in == NULL)
489 goto end;
0f113f3e
MC
490
491 if (operation & SMIME_IP) {
90a1f2d7
SL
492 PKCS7 *p7_in = NULL;
493
7dc67708 494 p7 = PKCS7_new_ex(libctx, app_get0_propq());
90a1f2d7
SL
495 if (p7 == NULL) {
496 BIO_printf(bio_err, "Error allocating PKCS7 object\n");
497 goto end;
498 }
2234212c 499 if (informat == FORMAT_SMIME) {
90a1f2d7 500 p7_in = SMIME_read_PKCS7_ex(in, &indata, &p7);
2234212c 501 } else if (informat == FORMAT_PEM) {
90a1f2d7 502 p7_in = PEM_read_bio_PKCS7(in, &p7, NULL, NULL);
2234212c 503 } else if (informat == FORMAT_ASN1) {
90a1f2d7 504 p7_in = d2i_PKCS7_bio(in, &p7);
2234212c 505 } else {
0f113f3e
MC
506 BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
507 goto end;
508 }
509
90a1f2d7 510 if (p7_in == NULL) {
0f113f3e
MC
511 BIO_printf(bio_err, "Error reading S/MIME message\n");
512 goto end;
513 }
2234212c 514 if (contfile != NULL) {
0f113f3e 515 BIO_free(indata);
75ebbd9a 516 if ((indata = BIO_new_file(contfile, "rb")) == NULL) {
0f113f3e
MC
517 BIO_printf(bio_err, "Can't read content file %s\n", contfile);
518 goto end;
519 }
520 }
521 }
522
bdd58d98 523 out = bio_open_default(outfile, 'w', outformat);
7e1b7485
RS
524 if (out == NULL)
525 goto end;
0f113f3e
MC
526
527 if (operation == SMIME_VERIFY) {
fd3397fc
RL
528 if ((store = setup_verify(CAfile, noCAfile, CApath, noCApath,
529 CAstore, noCAstore)) == NULL)
0f113f3e
MC
530 goto end;
531 X509_STORE_set_verify_cb(store, smime_cb);
7e1b7485 532 if (vpmtouched)
0f113f3e
MC
533 X509_STORE_set1_param(store, vpm);
534 }
535
536 ret = 3;
537
538 if (operation == SMIME_ENCRYPT) {
539 if (indef)
540 flags |= PKCS7_STREAM;
7dc67708 541 p7 = PKCS7_encrypt_ex(encerts, in, cipher, flags, libctx, app_get0_propq());
0f113f3e
MC
542 } else if (operation & SMIME_SIGNERS) {
543 int i;
544 /*
545 * If detached data content we only enable streaming if S/MIME output
546 * format.
547 */
548 if (operation == SMIME_SIGN) {
549 if (flags & PKCS7_DETACHED) {
550 if (outformat == FORMAT_SMIME)
551 flags |= PKCS7_STREAM;
2234212c 552 } else if (indef) {
0f113f3e 553 flags |= PKCS7_STREAM;
2234212c 554 }
0f113f3e 555 flags |= PKCS7_PARTIAL;
7dc67708 556 p7 = PKCS7_sign_ex(NULL, NULL, other, in, flags, libctx, app_get0_propq());
2234212c 557 if (p7 == NULL)
0f113f3e
MC
558 goto end;
559 if (flags & PKCS7_NOCERTS) {
560 for (i = 0; i < sk_X509_num(other); i++) {
561 X509 *x = sk_X509_value(other, i);
562 PKCS7_add_certificate(p7, x);
563 }
564 }
2234212c 565 } else {
0f113f3e 566 flags |= PKCS7_REUSE_DIGEST;
2234212c 567 }
0f113f3e
MC
568 for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
569 signerfile = sk_OPENSSL_STRING_value(sksigners, i);
570 keyfile = sk_OPENSSL_STRING_value(skkeys, i);
22dddfb9 571 signer = load_cert(signerfile, "signer certificate");
2234212c 572 if (signer == NULL)
0f113f3e 573 goto end;
50eb2a50 574 key = load_key(keyfile, keyform, 0, passin, e, "signing key");
2234212c 575 if (key == NULL)
0f113f3e 576 goto end;
a10847c4 577
0f113f3e
MC
578 if (!PKCS7_sign_add_signer(p7, signer, key, sign_md, flags))
579 goto end;
580 X509_free(signer);
581 signer = NULL;
582 EVP_PKEY_free(key);
583 key = NULL;
584 }
585 /* If not streaming or resigning finalize structure */
586 if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
587 if (!PKCS7_final(p7, in, flags))
588 goto end;
589 }
590 }
591
2234212c 592 if (p7 == NULL) {
0f113f3e
MC
593 BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
594 goto end;
595 }
596
597 ret = 4;
598 if (operation == SMIME_DECRYPT) {
599 if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
600 BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
601 goto end;
602 }
603 } else if (operation == SMIME_VERIFY) {
604 STACK_OF(X509) *signers;
605 if (PKCS7_verify(p7, other, store, indata, out, flags))
606 BIO_printf(bio_err, "Verification successful\n");
607 else {
608 BIO_printf(bio_err, "Verification failure\n");
609 goto end;
610 }
611 signers = PKCS7_get0_signers(p7, other, flags);
612 if (!save_certs(signerfile, signers)) {
613 BIO_printf(bio_err, "Error writing signers to %s\n", signerfile);
614 ret = 5;
615 goto end;
616 }
617 sk_X509_free(signers);
2234212c 618 } else if (operation == SMIME_PK7OUT) {
0f113f3e 619 PEM_write_bio_PKCS7(out, p7);
2234212c 620 } else {
0f113f3e 621 if (to)
2197494d 622 BIO_printf(out, "To: %s%s", to, mime_eol);
0f113f3e 623 if (from)
2197494d 624 BIO_printf(out, "From: %s%s", from, mime_eol);
0f113f3e 625 if (subject)
2197494d 626 BIO_printf(out, "Subject: %s%s", subject, mime_eol);
0f113f3e
MC
627 if (outformat == FORMAT_SMIME) {
628 if (operation == SMIME_RESIGN)
7e1b7485 629 rv = SMIME_write_PKCS7(out, p7, indata, flags);
0f113f3e 630 else
7e1b7485 631 rv = SMIME_write_PKCS7(out, p7, in, flags);
2234212c 632 } else if (outformat == FORMAT_PEM) {
7e1b7485 633 rv = PEM_write_bio_PKCS7_stream(out, p7, in, flags);
2234212c 634 } else if (outformat == FORMAT_ASN1) {
7e1b7485 635 rv = i2d_PKCS7_bio_stream(out, p7, in, flags);
2234212c 636 } else {
0f113f3e
MC
637 BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
638 goto end;
639 }
7e1b7485
RS
640 if (rv == 0) {
641 BIO_printf(bio_err, "Error writing output\n");
642 ret = 3;
643 goto end;
644 }
0f113f3e
MC
645 }
646 ret = 0;
647 end:
0f113f3e
MC
648 if (ret)
649 ERR_print_errors(bio_err);
650 sk_X509_pop_free(encerts, X509_free);
651 sk_X509_pop_free(other, X509_free);
222561fe 652 X509_VERIFY_PARAM_free(vpm);
25aaa98a
RS
653 sk_OPENSSL_STRING_free(sksigners);
654 sk_OPENSSL_STRING_free(skkeys);
0f113f3e
MC
655 X509_STORE_free(store);
656 X509_free(cert);
657 X509_free(recip);
658 X509_free(signer);
659 EVP_PKEY_free(key);
660 PKCS7_free(p7);
dd1abd44 661 release_engine(e);
0f113f3e
MC
662 BIO_free(in);
663 BIO_free(indata);
664 BIO_free_all(out);
b548a1f1 665 OPENSSL_free(passin);
90a1f2d7 666 NCONF_free(conf);
26a7d938 667 return ret;
5a9a4b29
DSH
668}
669
a91451ef 670static int save_certs(char *signerfile, STACK_OF(X509) *signers)
0f113f3e
MC
671{
672 int i;
673 BIO *tmp;
2234212c
PY
674
675 if (signerfile == NULL)
0f113f3e
MC
676 return 1;
677 tmp = BIO_new_file(signerfile, "w");
2234212c 678 if (tmp == NULL)
0f113f3e
MC
679 return 0;
680 for (i = 0; i < sk_X509_num(signers); i++)
681 PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
682 BIO_free(tmp);
683 return 1;
684}
5d7c222d 685
5d7c222d
DSH
686/* Minimal callback just to output policy info (if any) */
687
688static int smime_cb(int ok, X509_STORE_CTX *ctx)
0f113f3e
MC
689{
690 int error;
5d7c222d 691
0f113f3e 692 error = X509_STORE_CTX_get_error(ctx);
5d7c222d 693
0f113f3e
MC
694 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
695 && ((error != X509_V_OK) || (ok != 2)))
696 return ok;
5d7c222d 697
ecf3a1fb 698 policies_print(ctx);
5d7c222d 699
0f113f3e 700 return ok;
0f113f3e 701}