Annotation of src/usr.bin/openssl/smime.c, Revision 1.3
1.3 ! jsing 1: /* $OpenBSD: smime.c,v 1.2 2015/02/08 10:22:45 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: ENGINE *e = NULL;
89: int operation = 0;
90: int ret = 0;
91: char **args;
92: const char *inmode = "r", *outmode = "w";
93: char *infile = NULL, *outfile = NULL;
94: char *signerfile = NULL, *recipfile = NULL;
95: STACK_OF(OPENSSL_STRING) * sksigners = NULL, *skkeys = NULL;
96: char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
97: const EVP_CIPHER *cipher = NULL;
98: PKCS7 *p7 = NULL;
99: X509_STORE *store = NULL;
100: X509 *cert = NULL, *recip = NULL, *signer = NULL;
101: EVP_PKEY *key = NULL;
102: STACK_OF(X509) * encerts = NULL, *other = NULL;
103: BIO *in = NULL, *out = NULL, *indata = NULL;
104: int badarg = 0;
105: int flags = PKCS7_DETACHED;
106: char *to = NULL, *from = NULL, *subject = NULL;
107: char *CAfile = NULL, *CApath = NULL;
108: char *passargin = NULL, *passin = NULL;
109: int indef = 0;
110: const EVP_MD *sign_md = NULL;
111: int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
112: int keyform = FORMAT_PEM;
113: #ifndef OPENSSL_NO_ENGINE
114: char *engine = NULL;
115: #endif
116:
117: X509_VERIFY_PARAM *vpm = NULL;
118:
119: args = argv + 1;
120: ret = 1;
121:
122: while (!badarg && *args && *args[0] == '-') {
123: if (!strcmp(*args, "-encrypt"))
124: operation = SMIME_ENCRYPT;
125: else if (!strcmp(*args, "-decrypt"))
126: operation = SMIME_DECRYPT;
127: else if (!strcmp(*args, "-sign"))
128: operation = SMIME_SIGN;
129: else if (!strcmp(*args, "-resign"))
130: operation = SMIME_RESIGN;
131: else if (!strcmp(*args, "-verify"))
132: operation = SMIME_VERIFY;
133: else if (!strcmp(*args, "-pk7out"))
134: operation = SMIME_PK7OUT;
135: #ifndef OPENSSL_NO_DES
136: else if (!strcmp(*args, "-des3"))
137: cipher = EVP_des_ede3_cbc();
138: else if (!strcmp(*args, "-des"))
139: cipher = EVP_des_cbc();
140: #endif
141: #ifndef OPENSSL_NO_RC2
142: else if (!strcmp(*args, "-rc2-40"))
143: cipher = EVP_rc2_40_cbc();
144: else if (!strcmp(*args, "-rc2-128"))
145: cipher = EVP_rc2_cbc();
146: else if (!strcmp(*args, "-rc2-64"))
147: cipher = EVP_rc2_64_cbc();
148: #endif
149: #ifndef OPENSSL_NO_AES
150: else if (!strcmp(*args, "-aes128"))
151: cipher = EVP_aes_128_cbc();
152: else if (!strcmp(*args, "-aes192"))
153: cipher = EVP_aes_192_cbc();
154: else if (!strcmp(*args, "-aes256"))
155: cipher = EVP_aes_256_cbc();
156: #endif
157: #ifndef OPENSSL_NO_CAMELLIA
158: else if (!strcmp(*args, "-camellia128"))
159: cipher = EVP_camellia_128_cbc();
160: else if (!strcmp(*args, "-camellia192"))
161: cipher = EVP_camellia_192_cbc();
162: else if (!strcmp(*args, "-camellia256"))
163: cipher = EVP_camellia_256_cbc();
164: #endif
165: else if (!strcmp(*args, "-text"))
166: flags |= PKCS7_TEXT;
167: else if (!strcmp(*args, "-nointern"))
168: flags |= PKCS7_NOINTERN;
169: else if (!strcmp(*args, "-noverify"))
170: flags |= PKCS7_NOVERIFY;
171: else if (!strcmp(*args, "-nochain"))
172: flags |= PKCS7_NOCHAIN;
173: else if (!strcmp(*args, "-nocerts"))
174: flags |= PKCS7_NOCERTS;
175: else if (!strcmp(*args, "-noattr"))
176: flags |= PKCS7_NOATTR;
177: else if (!strcmp(*args, "-nodetach"))
178: flags &= ~PKCS7_DETACHED;
179: else if (!strcmp(*args, "-nosmimecap"))
180: flags |= PKCS7_NOSMIMECAP;
181: else if (!strcmp(*args, "-binary"))
182: flags |= PKCS7_BINARY;
183: else if (!strcmp(*args, "-nosigs"))
184: flags |= PKCS7_NOSIGS;
185: else if (!strcmp(*args, "-stream"))
186: indef = 1;
187: else if (!strcmp(*args, "-indef"))
188: indef = 1;
189: else if (!strcmp(*args, "-noindef"))
190: indef = 0;
191: else if (!strcmp(*args, "-nooldmime"))
192: flags |= PKCS7_NOOLDMIMETYPE;
193: else if (!strcmp(*args, "-crlfeol"))
194: flags |= PKCS7_CRLFEOL;
195: #ifndef OPENSSL_NO_ENGINE
196: else if (!strcmp(*args, "-engine")) {
197: if (!args[1])
198: goto argerr;
199: engine = *++args;
200: }
201: #endif
202: else if (!strcmp(*args, "-passin")) {
203: if (!args[1])
204: goto argerr;
205: passargin = *++args;
206: } else if (!strcmp(*args, "-to")) {
207: if (!args[1])
208: goto argerr;
209: to = *++args;
210: } else if (!strcmp(*args, "-from")) {
211: if (!args[1])
212: goto argerr;
213: from = *++args;
214: } else if (!strcmp(*args, "-subject")) {
215: if (!args[1])
216: goto argerr;
217: subject = *++args;
218: } else if (!strcmp(*args, "-signer")) {
219: if (!args[1])
220: goto argerr;
221: /* If previous -signer argument add signer to list */
222:
223: if (signerfile) {
224: if (!sksigners)
225: sksigners = sk_OPENSSL_STRING_new_null();
226: sk_OPENSSL_STRING_push(sksigners, signerfile);
227: if (!keyfile)
228: keyfile = signerfile;
229: if (!skkeys)
230: skkeys = sk_OPENSSL_STRING_new_null();
231: sk_OPENSSL_STRING_push(skkeys, keyfile);
232: keyfile = NULL;
233: }
234: signerfile = *++args;
235: } else if (!strcmp(*args, "-recip")) {
236: if (!args[1])
237: goto argerr;
238: recipfile = *++args;
239: } else if (!strcmp(*args, "-md")) {
240: if (!args[1])
241: goto argerr;
242: sign_md = EVP_get_digestbyname(*++args);
243: if (sign_md == NULL) {
244: BIO_printf(bio_err, "Unknown digest %s\n",
245: *args);
246: goto argerr;
247: }
248: } else if (!strcmp(*args, "-inkey")) {
249: if (!args[1])
250: goto argerr;
251: /* If previous -inkey arument add signer to list */
252: if (keyfile) {
253: if (!signerfile) {
254: BIO_puts(bio_err, "Illegal -inkey without -signer\n");
255: goto argerr;
256: }
257: if (!sksigners)
258: sksigners = sk_OPENSSL_STRING_new_null();
259: sk_OPENSSL_STRING_push(sksigners, signerfile);
260: signerfile = NULL;
261: if (!skkeys)
262: skkeys = sk_OPENSSL_STRING_new_null();
263: sk_OPENSSL_STRING_push(skkeys, keyfile);
264: }
265: keyfile = *++args;
266: } else if (!strcmp(*args, "-keyform")) {
267: if (!args[1])
268: goto argerr;
269: keyform = str2fmt(*++args);
270: } else if (!strcmp(*args, "-certfile")) {
271: if (!args[1])
272: goto argerr;
273: certfile = *++args;
274: } else if (!strcmp(*args, "-CAfile")) {
275: if (!args[1])
276: goto argerr;
277: CAfile = *++args;
278: } else if (!strcmp(*args, "-CApath")) {
279: if (!args[1])
280: goto argerr;
281: CApath = *++args;
282: } else if (!strcmp(*args, "-in")) {
283: if (!args[1])
284: goto argerr;
285: infile = *++args;
286: } else if (!strcmp(*args, "-inform")) {
287: if (!args[1])
288: goto argerr;
289: informat = str2fmt(*++args);
290: } else if (!strcmp(*args, "-outform")) {
291: if (!args[1])
292: goto argerr;
293: outformat = str2fmt(*++args);
294: } else if (!strcmp(*args, "-out")) {
295: if (!args[1])
296: goto argerr;
297: outfile = *++args;
298: } else if (!strcmp(*args, "-content")) {
299: if (!args[1])
300: goto argerr;
301: contfile = *++args;
302: } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
303: continue;
304: else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
305: badarg = 1;
306: args++;
307: }
308:
309: if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
310: BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
311: goto argerr;
312: }
313: if (operation & SMIME_SIGNERS) {
314: /* Check to see if any final signer needs to be appended */
315: if (keyfile && !signerfile) {
316: BIO_puts(bio_err, "Illegal -inkey without -signer\n");
317: goto argerr;
318: }
319: if (signerfile) {
320: if (!sksigners)
321: sksigners = sk_OPENSSL_STRING_new_null();
322: sk_OPENSSL_STRING_push(sksigners, signerfile);
323: if (!skkeys)
324: skkeys = sk_OPENSSL_STRING_new_null();
325: if (!keyfile)
326: keyfile = signerfile;
327: sk_OPENSSL_STRING_push(skkeys, keyfile);
328: }
329: if (!sksigners) {
330: BIO_printf(bio_err, "No signer certificate specified\n");
331: badarg = 1;
332: }
333: signerfile = NULL;
334: keyfile = NULL;
335: } else if (operation == SMIME_DECRYPT) {
336: if (!recipfile && !keyfile) {
337: BIO_printf(bio_err, "No recipient certificate or key specified\n");
338: badarg = 1;
339: }
340: } else if (operation == SMIME_ENCRYPT) {
341: if (!*args) {
342: BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
343: badarg = 1;
344: }
345: } else if (!operation)
346: badarg = 1;
347:
348: if (badarg) {
349: argerr:
350: BIO_printf(bio_err, "Usage smime [options] cert.pem ...\n");
351: BIO_printf(bio_err, "where options are\n");
352: BIO_printf(bio_err, "-encrypt encrypt message\n");
353: BIO_printf(bio_err, "-decrypt decrypt encrypted message\n");
354: BIO_printf(bio_err, "-sign sign message\n");
355: BIO_printf(bio_err, "-verify verify signed message\n");
356: BIO_printf(bio_err, "-pk7out output PKCS#7 structure\n");
357: #ifndef OPENSSL_NO_DES
358: BIO_printf(bio_err, "-des3 encrypt with triple DES\n");
359: BIO_printf(bio_err, "-des encrypt with DES\n");
360: #endif
361: #ifndef OPENSSL_NO_RC2
362: BIO_printf(bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
363: BIO_printf(bio_err, "-rc2-64 encrypt with RC2-64\n");
364: BIO_printf(bio_err, "-rc2-128 encrypt with RC2-128\n");
365: #endif
366: #ifndef OPENSSL_NO_AES
367: BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
368: BIO_printf(bio_err, " encrypt PEM output with cbc aes\n");
369: #endif
370: #ifndef OPENSSL_NO_CAMELLIA
371: BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
372: BIO_printf(bio_err, " encrypt PEM output with cbc camellia\n");
373: #endif
374: BIO_printf(bio_err, "-nointern don't search certificates in message for signer\n");
375: BIO_printf(bio_err, "-nosigs don't verify message signature\n");
376: BIO_printf(bio_err, "-noverify don't verify signers certificate\n");
377: BIO_printf(bio_err, "-nocerts don't include signers certificate when signing\n");
378: BIO_printf(bio_err, "-nodetach use opaque signing\n");
379: BIO_printf(bio_err, "-noattr don't include any signed attributes\n");
380: BIO_printf(bio_err, "-binary don't translate message to text\n");
381: BIO_printf(bio_err, "-certfile file other certificates file\n");
382: BIO_printf(bio_err, "-signer file signer certificate file\n");
383: BIO_printf(bio_err, "-recip file recipient certificate file for decryption\n");
384: BIO_printf(bio_err, "-in file input file\n");
385: BIO_printf(bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
386: BIO_printf(bio_err, "-inkey file input private key (if not signer or recipient)\n");
387: BIO_printf(bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
388: BIO_printf(bio_err, "-out file output file\n");
389: BIO_printf(bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
390: BIO_printf(bio_err, "-content file supply or override content for detached signature\n");
391: BIO_printf(bio_err, "-to addr to address\n");
392: BIO_printf(bio_err, "-from ad from address\n");
393: BIO_printf(bio_err, "-subject s subject\n");
394: BIO_printf(bio_err, "-text include or delete text MIME headers\n");
395: BIO_printf(bio_err, "-CApath dir trusted certificates directory\n");
396: BIO_printf(bio_err, "-CAfile file trusted certificates file\n");
397: BIO_printf(bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
398: BIO_printf(bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
399: #ifndef OPENSSL_NO_ENGINE
400: BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
401: #endif
402: BIO_printf(bio_err, "-passin arg input file pass phrase source\n");
403: BIO_printf(bio_err, "cert.pem recipient certificate(s) for encryption\n");
404: goto end;
405: }
406: #ifndef OPENSSL_NO_ENGINE
407: e = setup_engine(bio_err, engine, 0);
408: #endif
409:
410: if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
411: BIO_printf(bio_err, "Error getting password\n");
412: goto end;
413: }
414: ret = 2;
415:
416: if (!(operation & SMIME_SIGNERS))
417: flags &= ~PKCS7_DETACHED;
418:
419: if (operation & SMIME_OP) {
420: if (outformat == FORMAT_ASN1)
421: outmode = "wb";
422: } else {
423: if (flags & PKCS7_BINARY)
424: outmode = "wb";
425: }
426:
427: if (operation & SMIME_IP) {
428: if (informat == FORMAT_ASN1)
429: inmode = "rb";
430: } else {
431: if (flags & PKCS7_BINARY)
432: inmode = "rb";
433: }
434:
435: if (operation == SMIME_ENCRYPT) {
436: if (!cipher) {
437: #ifndef OPENSSL_NO_RC2
438: cipher = EVP_rc2_40_cbc();
439: #else
440: BIO_printf(bio_err, "No cipher selected\n");
441: goto end;
442: #endif
443: }
444: encerts = sk_X509_new_null();
445: while (*args) {
446: if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
1.2 doug 447: NULL, e, "recipient certificate file"))) {
1.1 jsing 448: goto end;
449: }
450: sk_X509_push(encerts, cert);
451: cert = NULL;
452: args++;
453: }
454: }
455: if (certfile) {
456: if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
457: e, "certificate file"))) {
458: ERR_print_errors(bio_err);
459: goto end;
460: }
461: }
462: if (recipfile && (operation == SMIME_DECRYPT)) {
463: if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
464: e, "recipient certificate file"))) {
465: ERR_print_errors(bio_err);
466: goto end;
467: }
468: }
469: if (operation == SMIME_DECRYPT) {
470: if (!keyfile)
471: keyfile = recipfile;
472: } else if (operation == SMIME_SIGN) {
473: if (!keyfile)
474: keyfile = signerfile;
475: } else
476: keyfile = NULL;
477:
478: if (keyfile) {
479: key = load_key(bio_err, keyfile, keyform, 0, passin, e,
480: "signing key file");
481: if (!key)
482: goto end;
483: }
484: if (infile) {
485: if (!(in = BIO_new_file(infile, inmode))) {
486: BIO_printf(bio_err,
487: "Can't open input file %s\n", infile);
488: goto end;
489: }
490: } else
491: in = BIO_new_fp(stdin, BIO_NOCLOSE);
492:
493: if (operation & SMIME_IP) {
494: if (informat == FORMAT_SMIME)
495: p7 = SMIME_read_PKCS7(in, &indata);
496: else if (informat == FORMAT_PEM)
497: p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
498: else if (informat == FORMAT_ASN1)
499: p7 = d2i_PKCS7_bio(in, NULL);
500: else {
501: BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
502: goto end;
503: }
504:
505: if (!p7) {
506: BIO_printf(bio_err, "Error reading S/MIME message\n");
507: goto end;
508: }
509: if (contfile) {
510: BIO_free(indata);
511: if (!(indata = BIO_new_file(contfile, "rb"))) {
512: BIO_printf(bio_err, "Can't read content file %s\n", contfile);
513: goto end;
514: }
515: }
516: }
517: if (outfile) {
518: if (!(out = BIO_new_file(outfile, outmode))) {
519: BIO_printf(bio_err,
520: "Can't open output file %s\n", outfile);
521: goto end;
522: }
523: } else {
524: out = BIO_new_fp(stdout, BIO_NOCLOSE);
525: }
526:
527: if (operation == SMIME_VERIFY) {
528: if (!(store = setup_verify(bio_err, CAfile, CApath)))
529: goto end;
530: X509_STORE_set_verify_cb(store, smime_cb);
531: if (vpm)
532: X509_STORE_set1_param(store, vpm);
533: }
534: ret = 3;
535:
536: if (operation == SMIME_ENCRYPT) {
537: if (indef)
538: flags |= PKCS7_STREAM;
539: p7 = PKCS7_encrypt(encerts, in, cipher, flags);
540: } else if (operation & SMIME_SIGNERS) {
541: int i;
542: /*
543: * If detached data content we only enable streaming if
544: * S/MIME output format.
545: */
546: if (operation == SMIME_SIGN) {
547: if (flags & PKCS7_DETACHED) {
548: if (outformat == FORMAT_SMIME)
549: flags |= PKCS7_STREAM;
550: } else if (indef)
551: flags |= PKCS7_STREAM;
552: flags |= PKCS7_PARTIAL;
553: p7 = PKCS7_sign(NULL, NULL, other, in, flags);
554: if (!p7)
555: goto end;
556: } else
557: flags |= PKCS7_REUSE_DIGEST;
558: for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
559: signerfile = sk_OPENSSL_STRING_value(sksigners, i);
560: keyfile = sk_OPENSSL_STRING_value(skkeys, i);
561: signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
562: e, "signer certificate");
563: if (!signer)
564: goto end;
565: key = load_key(bio_err, keyfile, keyform, 0, passin, e,
566: "signing key file");
567: if (!key)
568: goto end;
569: if (!PKCS7_sign_add_signer(p7, signer, key,
570: sign_md, flags))
571: goto end;
572: X509_free(signer);
573: signer = NULL;
574: EVP_PKEY_free(key);
575: key = NULL;
576: }
577: /* If not streaming or resigning finalize structure */
578: if ((operation == SMIME_SIGN) && !(flags & PKCS7_STREAM)) {
579: if (!PKCS7_final(p7, in, flags))
580: goto end;
581: }
582: }
583: if (!p7) {
584: BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
585: goto end;
586: }
587: ret = 4;
588: if (operation == SMIME_DECRYPT) {
589: if (!PKCS7_decrypt(p7, key, recip, out, flags)) {
590: BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
591: goto end;
592: }
593: } else if (operation == SMIME_VERIFY) {
594: STACK_OF(X509) * signers;
595: if (PKCS7_verify(p7, other, store, indata, out, flags))
596: BIO_printf(bio_err, "Verification successful\n");
597: else {
598: BIO_printf(bio_err, "Verification failure\n");
599: goto end;
600: }
601: signers = PKCS7_get0_signers(p7, other, flags);
602: if (!save_certs(signerfile, signers)) {
603: BIO_printf(bio_err, "Error writing signers to %s\n",
604: signerfile);
605: ret = 5;
606: goto end;
607: }
608: sk_X509_free(signers);
609: } else if (operation == SMIME_PK7OUT)
610: PEM_write_bio_PKCS7(out, p7);
611: else {
612: if (to)
613: BIO_printf(out, "To: %s\n", to);
614: if (from)
615: BIO_printf(out, "From: %s\n", from);
616: if (subject)
617: BIO_printf(out, "Subject: %s\n", subject);
618: if (outformat == FORMAT_SMIME) {
619: if (operation == SMIME_RESIGN)
620: SMIME_write_PKCS7(out, p7, indata, flags);
621: else
622: SMIME_write_PKCS7(out, p7, in, flags);
623: } else if (outformat == FORMAT_PEM)
624: PEM_write_bio_PKCS7_stream(out, p7, in, flags);
625: else if (outformat == FORMAT_ASN1)
626: i2d_PKCS7_bio_stream(out, p7, in, flags);
627: else {
628: BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
629: goto end;
630: }
631: }
632: ret = 0;
633: end:
634: if (ret)
635: ERR_print_errors(bio_err);
636: sk_X509_pop_free(encerts, X509_free);
637: sk_X509_pop_free(other, X509_free);
638: if (vpm)
639: X509_VERIFY_PARAM_free(vpm);
640: if (sksigners)
641: sk_OPENSSL_STRING_free(sksigners);
642: if (skkeys)
643: sk_OPENSSL_STRING_free(skkeys);
644: X509_STORE_free(store);
645: X509_free(cert);
646: X509_free(recip);
647: X509_free(signer);
648: EVP_PKEY_free(key);
649: PKCS7_free(p7);
650: BIO_free(in);
651: BIO_free(indata);
652: BIO_free_all(out);
653: free(passin);
654:
655: return (ret);
656: }
657:
658: static int
659: save_certs(char *signerfile, STACK_OF(X509) * signers)
660: {
661: int i;
662: BIO *tmp;
663: if (!signerfile)
664: return 1;
665: tmp = BIO_new_file(signerfile, "w");
666: if (!tmp)
667: return 0;
668: for (i = 0; i < sk_X509_num(signers); i++)
669: PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
670: BIO_free(tmp);
671: return 1;
672: }
673:
674:
675: /* Minimal callback just to output policy info (if any) */
676:
677: static int
678: smime_cb(int ok, X509_STORE_CTX * ctx)
679: {
680: int error;
681:
682: error = X509_STORE_CTX_get_error(ctx);
683:
684: if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
685: && ((error != X509_V_OK) || (ok != 2)))
686: return ok;
687:
688: policies_print(NULL, ctx);
689:
690: return ok;
691:
692: }