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