Annotation of src/usr.bin/openssl/smime.c, Revision 1.6
1.6 ! semarie 1: /* $OpenBSD: smime.c,v 1.5 2015/10/10 22:28:51 doug Exp $ */
1.1 jsing 2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3: * project.
4: */
5: /* ====================================================================
6: * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: *
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: *
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in
17: * the documentation and/or other materials provided with the
18: * distribution.
19: *
20: * 3. All advertising materials mentioning features or use of this
21: * software must display the following acknowledgment:
22: * "This product includes software developed by the OpenSSL Project
23: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24: *
25: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26: * endorse or promote products derived from this software without
27: * prior written permission. For written permission, please contact
28: * licensing@OpenSSL.org.
29: *
30: * 5. Products derived from this software may not be called "OpenSSL"
31: * nor may "OpenSSL" appear in their names without prior written
32: * permission of the OpenSSL Project.
33: *
34: * 6. Redistributions of any form whatsoever must retain the following
35: * acknowledgment:
36: * "This product includes software developed by the OpenSSL Project
37: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38: *
39: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50: * OF THE POSSIBILITY OF SUCH DAMAGE.
51: * ====================================================================
52: *
53: * This product includes cryptographic software written by Eric Young
54: * (eay@cryptsoft.com). This product includes software written by Tim
55: * Hudson (tjh@cryptsoft.com).
56: *
57: */
58:
59: /* S/MIME utility function */
60:
61: #include <stdio.h>
62: #include <string.h>
63:
64: #include "apps.h"
65:
66: #include <openssl/crypto.h>
67: #include <openssl/err.h>
68: #include <openssl/pem.h>
69: #include <openssl/x509_vfy.h>
70: #include <openssl/x509v3.h>
71:
72: static int save_certs(char *signerfile, STACK_OF(X509) * signers);
73: static int smime_cb(int ok, X509_STORE_CTX * ctx);
74:
75: #define SMIME_OP 0x10
76: #define SMIME_IP 0x20
77: #define SMIME_SIGNERS 0x40
78: #define SMIME_ENCRYPT (1 | SMIME_OP)
79: #define SMIME_DECRYPT (2 | SMIME_IP)
80: #define SMIME_SIGN (3 | SMIME_OP | SMIME_SIGNERS)
81: #define SMIME_VERIFY (4 | SMIME_IP)
82: #define SMIME_PK7OUT (5 | SMIME_IP | SMIME_OP)
83: #define SMIME_RESIGN (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
84:
85: int
86: smime_main(int argc, char **argv)
87: {
88: int operation = 0;
89: int ret = 0;
90: char **args;
91: const char *inmode = "r", *outmode = "w";
92: char *infile = NULL, *outfile = NULL;
93: char *signerfile = NULL, *recipfile = NULL;
94: STACK_OF(OPENSSL_STRING) * sksigners = NULL, *skkeys = NULL;
95: char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
96: const EVP_CIPHER *cipher = NULL;
97: PKCS7 *p7 = NULL;
98: X509_STORE *store = NULL;
99: X509 *cert = NULL, *recip = NULL, *signer = NULL;
100: EVP_PKEY *key = NULL;
101: STACK_OF(X509) * encerts = NULL, *other = NULL;
102: BIO *in = NULL, *out = NULL, *indata = NULL;
103: int badarg = 0;
104: int flags = PKCS7_DETACHED;
105: char *to = NULL, *from = NULL, *subject = NULL;
106: char *CAfile = NULL, *CApath = NULL;
107: char *passargin = NULL, *passin = NULL;
108: int indef = 0;
109: const EVP_MD *sign_md = NULL;
110: int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
111: int keyform = FORMAT_PEM;
112:
113: X509_VERIFY_PARAM *vpm = NULL;
1.5 doug 114:
115: if (single_execution) {
1.6 ! semarie 116: if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
1.5 doug 117: perror("pledge");
118: }
1.1 jsing 119:
120: args = argv + 1;
121: ret = 1;
122:
123: while (!badarg && *args && *args[0] == '-') {
124: if (!strcmp(*args, "-encrypt"))
125: operation = SMIME_ENCRYPT;
126: else if (!strcmp(*args, "-decrypt"))
127: operation = SMIME_DECRYPT;
128: else if (!strcmp(*args, "-sign"))
129: operation = SMIME_SIGN;
130: else if (!strcmp(*args, "-resign"))
131: operation = SMIME_RESIGN;
132: else if (!strcmp(*args, "-verify"))
133: operation = SMIME_VERIFY;
134: else if (!strcmp(*args, "-pk7out"))
135: operation = SMIME_PK7OUT;
136: #ifndef OPENSSL_NO_DES
137: else if (!strcmp(*args, "-des3"))
138: cipher = EVP_des_ede3_cbc();
139: else if (!strcmp(*args, "-des"))
140: cipher = EVP_des_cbc();
141: #endif
142: #ifndef OPENSSL_NO_RC2
143: else if (!strcmp(*args, "-rc2-40"))
144: cipher = EVP_rc2_40_cbc();
145: else if (!strcmp(*args, "-rc2-128"))
146: cipher = EVP_rc2_cbc();
147: else if (!strcmp(*args, "-rc2-64"))
148: cipher = EVP_rc2_64_cbc();
149: #endif
150: #ifndef OPENSSL_NO_AES
151: else if (!strcmp(*args, "-aes128"))
152: cipher = EVP_aes_128_cbc();
153: else if (!strcmp(*args, "-aes192"))
154: cipher = EVP_aes_192_cbc();
155: else if (!strcmp(*args, "-aes256"))
156: cipher = EVP_aes_256_cbc();
157: #endif
158: #ifndef OPENSSL_NO_CAMELLIA
159: else if (!strcmp(*args, "-camellia128"))
160: cipher = EVP_camellia_128_cbc();
161: else if (!strcmp(*args, "-camellia192"))
162: cipher = EVP_camellia_192_cbc();
163: else if (!strcmp(*args, "-camellia256"))
164: cipher = EVP_camellia_256_cbc();
165: #endif
166: else if (!strcmp(*args, "-text"))
167: flags |= PKCS7_TEXT;
168: else if (!strcmp(*args, "-nointern"))
169: flags |= PKCS7_NOINTERN;
170: else if (!strcmp(*args, "-noverify"))
171: flags |= PKCS7_NOVERIFY;
172: else if (!strcmp(*args, "-nochain"))
173: flags |= PKCS7_NOCHAIN;
174: else if (!strcmp(*args, "-nocerts"))
175: flags |= PKCS7_NOCERTS;
176: else if (!strcmp(*args, "-noattr"))
177: flags |= PKCS7_NOATTR;
178: else if (!strcmp(*args, "-nodetach"))
179: flags &= ~PKCS7_DETACHED;
180: else if (!strcmp(*args, "-nosmimecap"))
181: flags |= PKCS7_NOSMIMECAP;
182: else if (!strcmp(*args, "-binary"))
183: flags |= PKCS7_BINARY;
184: else if (!strcmp(*args, "-nosigs"))
185: flags |= PKCS7_NOSIGS;
186: else if (!strcmp(*args, "-stream"))
187: indef = 1;
188: else if (!strcmp(*args, "-indef"))
189: indef = 1;
190: else if (!strcmp(*args, "-noindef"))
191: indef = 0;
192: else if (!strcmp(*args, "-nooldmime"))
193: flags |= PKCS7_NOOLDMIMETYPE;
194: else if (!strcmp(*args, "-crlfeol"))
195: flags |= PKCS7_CRLFEOL;
196: else if (!strcmp(*args, "-passin")) {
197: if (!args[1])
198: goto argerr;
199: passargin = *++args;
200: } else if (!strcmp(*args, "-to")) {
201: if (!args[1])
202: goto argerr;
203: to = *++args;
204: } else if (!strcmp(*args, "-from")) {
205: if (!args[1])
206: goto argerr;
207: from = *++args;
208: } else if (!strcmp(*args, "-subject")) {
209: if (!args[1])
210: goto argerr;
211: subject = *++args;
212: } else if (!strcmp(*args, "-signer")) {
213: if (!args[1])
214: goto argerr;
215: /* If previous -signer argument add signer to list */
216:
217: if (signerfile) {
218: if (!sksigners)
219: sksigners = sk_OPENSSL_STRING_new_null();
220: sk_OPENSSL_STRING_push(sksigners, signerfile);
221: if (!keyfile)
222: keyfile = signerfile;
223: if (!skkeys)
224: skkeys = sk_OPENSSL_STRING_new_null();
225: sk_OPENSSL_STRING_push(skkeys, keyfile);
226: keyfile = NULL;
227: }
228: signerfile = *++args;
229: } else if (!strcmp(*args, "-recip")) {
230: if (!args[1])
231: goto argerr;
232: recipfile = *++args;
233: } else if (!strcmp(*args, "-md")) {
234: if (!args[1])
235: goto argerr;
236: sign_md = EVP_get_digestbyname(*++args);
237: if (sign_md == NULL) {
238: BIO_printf(bio_err, "Unknown digest %s\n",
239: *args);
240: goto argerr;
241: }
242: } else if (!strcmp(*args, "-inkey")) {
243: if (!args[1])
244: goto argerr;
245: /* If previous -inkey arument add signer to list */
246: if (keyfile) {
247: if (!signerfile) {
248: BIO_puts(bio_err, "Illegal -inkey without -signer\n");
249: goto argerr;
250: }
251: if (!sksigners)
252: sksigners = sk_OPENSSL_STRING_new_null();
253: sk_OPENSSL_STRING_push(sksigners, signerfile);
254: signerfile = NULL;
255: if (!skkeys)
256: skkeys = sk_OPENSSL_STRING_new_null();
257: sk_OPENSSL_STRING_push(skkeys, keyfile);
258: }
259: keyfile = *++args;
260: } else if (!strcmp(*args, "-keyform")) {
261: if (!args[1])
262: goto argerr;
263: keyform = str2fmt(*++args);
264: } else if (!strcmp(*args, "-certfile")) {
265: if (!args[1])
266: goto argerr;
267: certfile = *++args;
268: } else if (!strcmp(*args, "-CAfile")) {
269: if (!args[1])
270: goto argerr;
271: CAfile = *++args;
272: } else if (!strcmp(*args, "-CApath")) {
273: if (!args[1])
274: goto argerr;
275: CApath = *++args;
276: } else if (!strcmp(*args, "-in")) {
277: if (!args[1])
278: goto argerr;
279: infile = *++args;
280: } else if (!strcmp(*args, "-inform")) {
281: if (!args[1])
282: goto argerr;
283: informat = str2fmt(*++args);
284: } else if (!strcmp(*args, "-outform")) {
285: if (!args[1])
286: goto argerr;
287: outformat = str2fmt(*++args);
288: } else if (!strcmp(*args, "-out")) {
289: if (!args[1])
290: goto argerr;
291: outfile = *++args;
292: } else if (!strcmp(*args, "-content")) {
293: if (!args[1])
294: goto argerr;
295: contfile = *++args;
296: } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
297: continue;
298: else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
299: badarg = 1;
300: args++;
301: }
302:
303: if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
304: BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
305: goto argerr;
306: }
307: if (operation & SMIME_SIGNERS) {
308: /* Check to see if any final signer needs to be appended */
309: if (keyfile && !signerfile) {
310: BIO_puts(bio_err, "Illegal -inkey without -signer\n");
311: goto argerr;
312: }
313: if (signerfile) {
314: if (!sksigners)
315: sksigners = sk_OPENSSL_STRING_new_null();
316: sk_OPENSSL_STRING_push(sksigners, signerfile);
317: if (!skkeys)
318: skkeys = sk_OPENSSL_STRING_new_null();
319: if (!keyfile)
320: keyfile = signerfile;
321: sk_OPENSSL_STRING_push(skkeys, keyfile);
322: }
323: if (!sksigners) {
324: BIO_printf(bio_err, "No signer certificate specified\n");
325: badarg = 1;
326: }
327: signerfile = NULL;
328: keyfile = NULL;
329: } else if (operation == SMIME_DECRYPT) {
330: if (!recipfile && !keyfile) {
331: BIO_printf(bio_err, "No recipient certificate or key specified\n");
332: badarg = 1;
333: }
334: } else if (operation == SMIME_ENCRYPT) {
335: if (!*args) {
336: BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
337: badarg = 1;
338: }
339: } else if (!operation)
340: badarg = 1;
341:
342: if (badarg) {
343: argerr:
344: BIO_printf(bio_err, "Usage smime [options] cert.pem ...\n");
345: BIO_printf(bio_err, "where options are\n");
346: BIO_printf(bio_err, "-encrypt encrypt message\n");
347: BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
348: BIO_printf(bio_err, "-sign sign message\n");
349: BIO_printf(bio_err, "-verify verify signed message\n");
350: BIO_printf(bio_err, "-pk7out output PKCS#7 structure\n");
351: #ifndef OPENSSL_NO_DES
352: BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
353: BIO_printf(bio_err, "-des encrypt with DES\n");
354: #endif
355: #ifndef OPENSSL_NO_RC2
356: BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
357: BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
358: BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
359: #endif
360: #ifndef OPENSSL_NO_AES
361: BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
362: BIO_printf(bio_err, " encrypt PEM output with cbc aes\n");
363: #endif
364: #ifndef OPENSSL_NO_CAMELLIA
365: BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
366: BIO_printf(bio_err, " encrypt PEM output with cbc camellia\n");
367: #endif
368: BIO_printf(bio_err, "-nointern don't search certificates in message for signer\n");
369: BIO_printf(bio_err, "-nosigs don't verify message signature\n");
370: BIO_printf(bio_err, "-noverify don't verify signers certificate\n");
371: BIO_printf(bio_err, "-nocerts don't include signers certificate when signing\n");
372: BIO_printf(bio_err, "-nodetach use opaque signing\n");
373: BIO_printf(bio_err, "-noattr don't include any signed attributes\n");
374: BIO_printf(bio_err, "-binary don't translate message to text\n");
375: BIO_printf(bio_err, "-certfile file other certificates file\n");
376: BIO_printf(bio_err, "-signer file signer certificate file\n");
377: BIO_printf(bio_err, "-recip file recipient certificate file for decryption\n");
378: BIO_printf(bio_err, "-in file input file\n");
379: BIO_printf(bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
380: BIO_printf(bio_err, "-inkey file input private key (if not signer or recipient)\n");
1.4 bcook 381: BIO_printf(bio_err, "-keyform arg input private key format (PEM)\n");
1.1 jsing 382: BIO_printf(bio_err, "-out file output file\n");
383: BIO_printf(bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
384: BIO_printf(bio_err, "-content file supply or override content for detached signature\n");
385: BIO_printf(bio_err, "-to addr to address\n");
386: BIO_printf(bio_err, "-from ad from address\n");
387: BIO_printf(bio_err, "-subject s subject\n");
388: BIO_printf(bio_err, "-text include or delete text MIME headers\n");
389: BIO_printf(bio_err, "-CApath dir trusted certificates directory\n");
390: BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
391: BIO_printf(bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
392: BIO_printf(bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
393: BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
394: BIO_printf(bio_err, "cert.pem recipient certificate(s) for encryption\n");
395: goto end;
396: }
397:
398: if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
399: BIO_printf(bio_err, "Error getting password\n");
400: goto end;
401: }
402: ret = 2;
403:
404: if (!(operation & SMIME_SIGNERS))
405: flags &= ~PKCS7_DETACHED;
406:
407: if (operation & SMIME_OP) {
408: if (outformat == FORMAT_ASN1)
409: outmode = "wb";
410: } else {
411: if (flags & PKCS7_BINARY)
412: outmode = "wb";
413: }
414:
415: if (operation & SMIME_IP) {
416: if (informat == FORMAT_ASN1)
417: inmode = "rb";
418: } else {
419: if (flags & PKCS7_BINARY)
420: inmode = "rb";
421: }
422:
423: if (operation == SMIME_ENCRYPT) {
424: if (!cipher) {
425: #ifndef OPENSSL_NO_RC2
426: cipher = EVP_rc2_40_cbc();
427: #else
428: BIO_printf(bio_err, "No cipher selected\n");
429: goto end;
430: #endif
431: }
432: encerts = sk_X509_new_null();
433: while (*args) {
434: if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
1.4 bcook 435: NULL, "recipient certificate file"))) {
1.1 jsing 436: goto end;
437: }
438: sk_X509_push(encerts, cert);
439: cert = NULL;
440: args++;
441: }
442: }
443: if (certfile) {
444: if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
1.4 bcook 445: "certificate file"))) {
1.1 jsing 446: ERR_print_errors(bio_err);
447: goto end;
448: }
449: }
450: if (recipfile && (operation == SMIME_DECRYPT)) {
451: if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
1.4 bcook 452: "recipient certificate file"))) {
1.1 jsing 453: ERR_print_errors(bio_err);
454: goto end;
455: }
456: }
457: if (operation == SMIME_DECRYPT) {
458: if (!keyfile)
459: keyfile = recipfile;
460: } else if (operation == SMIME_SIGN) {
461: if (!keyfile)
462: keyfile = signerfile;
463: } else
464: keyfile = NULL;
465:
466: if (keyfile) {
1.4 bcook 467: key = load_key(bio_err, keyfile, keyform, 0, passin,
1.1 jsing 468: "signing key file");
469: if (!key)
470: goto end;
471: }
472: if (infile) {
473: if (!(in = BIO_new_file(infile, inmode))) {
474: BIO_printf(bio_err,
475: "Can't open input file %s\n", infile);
476: goto end;
477: }
478: } else
479: in = BIO_new_fp(stdin, BIO_NOCLOSE);
480:
481: if (operation & SMIME_IP) {
482: if (informat == FORMAT_SMIME)
483: p7 = SMIME_read_PKCS7(in, &indata);
484: else if (informat == FORMAT_PEM)
485: p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
486: else if (informat == FORMAT_ASN1)
487: p7 = d2i_PKCS7_bio(in, NULL);
488: else {
489: BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
490: goto end;
491: }
492:
493: if (!p7) {
494: BIO_printf(bio_err, "Error reading S/MIME message\n");
495: goto end;
496: }
497: if (contfile) {
498: BIO_free(indata);
499: if (!(indata = BIO_new_file(contfile, "rb"))) {
500: BIO_printf(bio_err, "Can't read content file %s\n", contfile);
501: goto end;
502: }
503: }
504: }
505: if (outfile) {
506: if (!(out = BIO_new_file(outfile, outmode))) {
507: BIO_printf(bio_err,
508: "Can't open output file %s\n", outfile);
509: goto end;
510: }
511: } else {
512: out = BIO_new_fp(stdout, BIO_NOCLOSE);
513: }
514:
515: if (operation == SMIME_VERIFY) {
516: if (!(store = setup_verify(bio_err, CAfile, CApath)))
517: goto end;
518: X509_STORE_set_verify_cb(store, smime_cb);
519: if (vpm)
520: X509_STORE_set1_param(store, vpm);
521: }
522: ret = 3;
523:
524: if (operation == SMIME_ENCRYPT) {
525: if (indef)
526: flags |= PKCS7_STREAM;
527: p7 = PKCS7_encrypt(encerts, in, cipher, flags);
528: } else if (operation & SMIME_SIGNERS) {
529: int i;
530: /*
531: * If detached data content we only enable streaming if
532: * S/MIME output format.
533: */
534: if (operation == SMIME_SIGN) {
535: if (flags & PKCS7_DETACHED) {
536: if (outformat == FORMAT_SMIME)
537: flags |= PKCS7_STREAM;
538: } else if (indef)
539: flags |= PKCS7_STREAM;
540: flags |= PKCS7_PARTIAL;
541: p7 = PKCS7_sign(NULL, NULL, other, in, flags);
542: if (!p7)
543: goto end;
544: } else
545: flags |= PKCS7_REUSE_DIGEST;
546: for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
547: signerfile = sk_OPENSSL_STRING_value(sksigners, i);
548: keyfile = sk_OPENSSL_STRING_value(skkeys, i);
549: signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
1.4 bcook 550: "signer certificate");
1.1 jsing 551: if (!signer)
552: goto end;
1.4 bcook 553: key = load_key(bio_err, keyfile, keyform, 0, passin,
1.1 jsing 554: "signing key file");
555: if (!key)
556: goto end;
557: if (!PKCS7_sign_add_signer(p7, signer, key,
558: sign_md, flags))
559: goto end;
560: X509_free(signer);
561: signer = NULL;
562: EVP_PKEY_free(key);
563: key = NULL;
564: }
565: /* If not streaming or resigning finalize structure */
566: if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
567: if (!PKCS7_final(p7, in, flags))
568: goto end;
569: }
570: }
571: if (!p7) {
572: BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
573: goto end;
574: }
575: ret = 4;
576: if (operation == SMIME_DECRYPT) {
577: if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
578: BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
579: goto end;
580: }
581: } else if (operation == SMIME_VERIFY) {
582: STACK_OF(X509) * signers;
583: if (PKCS7_verify(p7, other, store, indata, out, flags))
584: BIO_printf(bio_err, "Verification successful\n");
585: else {
586: BIO_printf(bio_err, "Verification failure\n");
587: goto end;
588: }
589: signers = PKCS7_get0_signers(p7, other, flags);
590: if (!save_certs(signerfile, signers)) {
591: BIO_printf(bio_err, "Error writing signers to %s\n",
592: signerfile);
593: ret = 5;
594: goto end;
595: }
596: sk_X509_free(signers);
597: } else if (operation == SMIME_PK7OUT)
598: PEM_write_bio_PKCS7(out, p7);
599: else {
600: if (to)
601: BIO_printf(out, "To: %s\n", to);
602: if (from)
603: BIO_printf(out, "From: %s\n", from);
604: if (subject)
605: BIO_printf(out, "Subject: %s\n", subject);
606: if (outformat == FORMAT_SMIME) {
607: if (operation == SMIME_RESIGN)
608: SMIME_write_PKCS7(out, p7, indata, flags);
609: else
610: SMIME_write_PKCS7(out, p7, in, flags);
611: } else if (outformat == FORMAT_PEM)
612: PEM_write_bio_PKCS7_stream(out, p7, in, flags);
613: else if (outformat == FORMAT_ASN1)
614: i2d_PKCS7_bio_stream(out, p7, in, flags);
615: else {
616: BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
617: goto end;
618: }
619: }
620: ret = 0;
621: end:
622: if (ret)
623: ERR_print_errors(bio_err);
624: sk_X509_pop_free(encerts, X509_free);
625: sk_X509_pop_free(other, X509_free);
626: if (vpm)
627: X509_VERIFY_PARAM_free(vpm);
628: if (sksigners)
629: sk_OPENSSL_STRING_free(sksigners);
630: if (skkeys)
631: sk_OPENSSL_STRING_free(skkeys);
632: X509_STORE_free(store);
633: X509_free(cert);
634: X509_free(recip);
635: X509_free(signer);
636: EVP_PKEY_free(key);
637: PKCS7_free(p7);
638: BIO_free(in);
639: BIO_free(indata);
640: BIO_free_all(out);
641: free(passin);
642:
643: return (ret);
644: }
645:
646: static int
647: save_certs(char *signerfile, STACK_OF(X509) * signers)
648: {
649: int i;
650: BIO *tmp;
651: if (!signerfile)
652: return 1;
653: tmp = BIO_new_file(signerfile, "w");
654: if (!tmp)
655: return 0;
656: for (i = 0; i < sk_X509_num(signers); i++)
657: PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
658: BIO_free(tmp);
659: return 1;
660: }
661:
662:
663: /* Minimal callback just to output policy info (if any) */
664:
665: static int
666: smime_cb(int ok, X509_STORE_CTX * ctx)
667: {
668: int error;
669:
670: error = X509_STORE_CTX_get_error(ctx);
671:
672: if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
673: && ((error != X509_V_OK) || (ok != 2)))
674: return ok;
675:
676: policies_print(NULL, ctx);
677:
678: return ok;
679:
680: }