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