xref: /freebsd/crypto/openssl/apps/smime.c (revision c1803d783676ddc1f7655e0a58c00f35ec8c4f45)
1f579bf8eSKris Kennaway /* smime.c */
2f579bf8eSKris Kennaway /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3f579bf8eSKris Kennaway  * project 1999.
4f579bf8eSKris Kennaway  */
5f579bf8eSKris Kennaway /* ====================================================================
6f579bf8eSKris Kennaway  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7f579bf8eSKris Kennaway  *
8f579bf8eSKris Kennaway  * Redistribution and use in source and binary forms, with or without
9f579bf8eSKris Kennaway  * modification, are permitted provided that the following conditions
10f579bf8eSKris Kennaway  * are met:
11f579bf8eSKris Kennaway  *
12f579bf8eSKris Kennaway  * 1. Redistributions of source code must retain the above copyright
13f579bf8eSKris Kennaway  *    notice, this list of conditions and the following disclaimer.
14f579bf8eSKris Kennaway  *
15f579bf8eSKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
16f579bf8eSKris Kennaway  *    notice, this list of conditions and the following disclaimer in
17f579bf8eSKris Kennaway  *    the documentation and/or other materials provided with the
18f579bf8eSKris Kennaway  *    distribution.
19f579bf8eSKris Kennaway  *
20f579bf8eSKris Kennaway  * 3. All advertising materials mentioning features or use of this
21f579bf8eSKris Kennaway  *    software must display the following acknowledgment:
22f579bf8eSKris Kennaway  *    "This product includes software developed by the OpenSSL Project
23f579bf8eSKris Kennaway  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24f579bf8eSKris Kennaway  *
25f579bf8eSKris Kennaway  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26f579bf8eSKris Kennaway  *    endorse or promote products derived from this software without
27f579bf8eSKris Kennaway  *    prior written permission. For written permission, please contact
28f579bf8eSKris Kennaway  *    licensing@OpenSSL.org.
29f579bf8eSKris Kennaway  *
30f579bf8eSKris Kennaway  * 5. Products derived from this software may not be called "OpenSSL"
31f579bf8eSKris Kennaway  *    nor may "OpenSSL" appear in their names without prior written
32f579bf8eSKris Kennaway  *    permission of the OpenSSL Project.
33f579bf8eSKris Kennaway  *
34f579bf8eSKris Kennaway  * 6. Redistributions of any form whatsoever must retain the following
35f579bf8eSKris Kennaway  *    acknowledgment:
36f579bf8eSKris Kennaway  *    "This product includes software developed by the OpenSSL Project
37f579bf8eSKris Kennaway  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38f579bf8eSKris Kennaway  *
39f579bf8eSKris Kennaway  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40f579bf8eSKris Kennaway  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41f579bf8eSKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42f579bf8eSKris Kennaway  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43f579bf8eSKris Kennaway  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44f579bf8eSKris Kennaway  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45f579bf8eSKris Kennaway  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46f579bf8eSKris Kennaway  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f579bf8eSKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48f579bf8eSKris Kennaway  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49f579bf8eSKris Kennaway  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50f579bf8eSKris Kennaway  * OF THE POSSIBILITY OF SUCH DAMAGE.
51f579bf8eSKris Kennaway  * ====================================================================
52f579bf8eSKris Kennaway  *
53f579bf8eSKris Kennaway  * This product includes cryptographic software written by Eric Young
54f579bf8eSKris Kennaway  * (eay@cryptsoft.com).  This product includes software written by Tim
55f579bf8eSKris Kennaway  * Hudson (tjh@cryptsoft.com).
56f579bf8eSKris Kennaway  *
57f579bf8eSKris Kennaway  */
58f579bf8eSKris Kennaway 
59f579bf8eSKris Kennaway /* S/MIME utility function */
60f579bf8eSKris Kennaway 
61f579bf8eSKris Kennaway #include <stdio.h>
62f579bf8eSKris Kennaway #include <string.h>
63f579bf8eSKris Kennaway #include "apps.h"
64f579bf8eSKris Kennaway #include <openssl/crypto.h>
65f579bf8eSKris Kennaway #include <openssl/pem.h>
66f579bf8eSKris Kennaway #include <openssl/err.h>
67f579bf8eSKris Kennaway 
68f579bf8eSKris Kennaway #undef PROG
69f579bf8eSKris Kennaway #define PROG smime_main
70f579bf8eSKris Kennaway static X509_STORE *setup_verify(char *CAfile, char *CApath);
71f579bf8eSKris Kennaway static int save_certs(char *signerfile, STACK_OF(X509) *signers);
72f579bf8eSKris Kennaway 
73f579bf8eSKris Kennaway #define SMIME_OP	0x10
74f579bf8eSKris Kennaway #define SMIME_ENCRYPT	(1 | SMIME_OP)
75f579bf8eSKris Kennaway #define SMIME_DECRYPT	2
76f579bf8eSKris Kennaway #define SMIME_SIGN	(3 | SMIME_OP)
77f579bf8eSKris Kennaway #define SMIME_VERIFY	4
78f579bf8eSKris Kennaway #define SMIME_PK7OUT	5
79f579bf8eSKris Kennaway 
80f579bf8eSKris Kennaway int MAIN(int, char **);
81f579bf8eSKris Kennaway 
82f579bf8eSKris Kennaway int MAIN(int argc, char **argv)
83f579bf8eSKris Kennaway {
84f579bf8eSKris Kennaway 	int operation = 0;
85f579bf8eSKris Kennaway 	int ret = 0;
86f579bf8eSKris Kennaway 	char **args;
87f579bf8eSKris Kennaway 	char *inmode = "r", *outmode = "w";
88f579bf8eSKris Kennaway 	char *infile = NULL, *outfile = NULL;
89f579bf8eSKris Kennaway 	char *signerfile = NULL, *recipfile = NULL;
90ddd58736SKris Kennaway 	char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
91f579bf8eSKris Kennaway 	EVP_CIPHER *cipher = NULL;
92f579bf8eSKris Kennaway 	PKCS7 *p7 = NULL;
93f579bf8eSKris Kennaway 	X509_STORE *store = NULL;
94f579bf8eSKris Kennaway 	X509 *cert = NULL, *recip = NULL, *signer = NULL;
95f579bf8eSKris Kennaway 	EVP_PKEY *key = NULL;
96f579bf8eSKris Kennaway 	STACK_OF(X509) *encerts = NULL, *other = NULL;
97f579bf8eSKris Kennaway 	BIO *in = NULL, *out = NULL, *indata = NULL;
98f579bf8eSKris Kennaway 	int badarg = 0;
99f579bf8eSKris Kennaway 	int flags = PKCS7_DETACHED;
100f579bf8eSKris Kennaway 	char *to = NULL, *from = NULL, *subject = NULL;
101f579bf8eSKris Kennaway 	char *CAfile = NULL, *CApath = NULL;
102f579bf8eSKris Kennaway 	char *passargin = NULL, *passin = NULL;
103f579bf8eSKris Kennaway 	char *inrand = NULL;
104f579bf8eSKris Kennaway 	int need_rand = 0;
105ddd58736SKris Kennaway 	int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
106f579bf8eSKris Kennaway 	args = argv + 1;
107f579bf8eSKris Kennaway 
108f579bf8eSKris Kennaway 	ret = 1;
109f579bf8eSKris Kennaway 
110f579bf8eSKris Kennaway 	while (!badarg && *args && *args[0] == '-') {
111f579bf8eSKris Kennaway 		if (!strcmp (*args, "-encrypt")) operation = SMIME_ENCRYPT;
112f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-decrypt")) operation = SMIME_DECRYPT;
113f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-sign")) operation = SMIME_SIGN;
114f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-verify")) operation = SMIME_VERIFY;
115f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-pk7out")) operation = SMIME_PK7OUT;
116f579bf8eSKris Kennaway #ifndef NO_DES
117f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-des3"))
118f579bf8eSKris Kennaway 				cipher = EVP_des_ede3_cbc();
119f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-des"))
120f579bf8eSKris Kennaway 				cipher = EVP_des_cbc();
121f579bf8eSKris Kennaway #endif
122f579bf8eSKris Kennaway #ifndef NO_RC2
123f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-rc2-40"))
124f579bf8eSKris Kennaway 				cipher = EVP_rc2_40_cbc();
125f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-rc2-128"))
126f579bf8eSKris Kennaway 				cipher = EVP_rc2_cbc();
127f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-rc2-64"))
128f579bf8eSKris Kennaway 				cipher = EVP_rc2_64_cbc();
129f579bf8eSKris Kennaway #endif
130f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-text"))
131f579bf8eSKris Kennaway 				flags |= PKCS7_TEXT;
132f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-nointern"))
133f579bf8eSKris Kennaway 				flags |= PKCS7_NOINTERN;
134f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-noverify"))
135f579bf8eSKris Kennaway 				flags |= PKCS7_NOVERIFY;
136f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-nochain"))
137f579bf8eSKris Kennaway 				flags |= PKCS7_NOCHAIN;
138f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-nocerts"))
139f579bf8eSKris Kennaway 				flags |= PKCS7_NOCERTS;
140f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-noattr"))
141f579bf8eSKris Kennaway 				flags |= PKCS7_NOATTR;
142f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-nodetach"))
143f579bf8eSKris Kennaway 				flags &= ~PKCS7_DETACHED;
144ddd58736SKris Kennaway 		else if (!strcmp (*args, "-nosmimecap"))
145ddd58736SKris Kennaway 				flags |= PKCS7_NOSMIMECAP;
146f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-binary"))
147f579bf8eSKris Kennaway 				flags |= PKCS7_BINARY;
148f579bf8eSKris Kennaway 		else if (!strcmp (*args, "-nosigs"))
149f579bf8eSKris Kennaway 				flags |= PKCS7_NOSIGS;
150f579bf8eSKris Kennaway 		else if (!strcmp(*args,"-rand")) {
151f579bf8eSKris Kennaway 			if (args[1]) {
152f579bf8eSKris Kennaway 				args++;
153f579bf8eSKris Kennaway 				inrand = *args;
154f579bf8eSKris Kennaway 			} else badarg = 1;
155f579bf8eSKris Kennaway 			need_rand = 1;
156f579bf8eSKris Kennaway 		} else if (!strcmp(*args,"-passin")) {
157f579bf8eSKris Kennaway 			if (args[1]) {
158f579bf8eSKris Kennaway 				args++;
159f579bf8eSKris Kennaway 				passargin = *args;
160f579bf8eSKris Kennaway 			} else badarg = 1;
161f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-to")) {
162f579bf8eSKris Kennaway 			if (args[1]) {
163f579bf8eSKris Kennaway 				args++;
164f579bf8eSKris Kennaway 				to = *args;
165f579bf8eSKris Kennaway 			} else badarg = 1;
166f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-from")) {
167f579bf8eSKris Kennaway 			if (args[1]) {
168f579bf8eSKris Kennaway 				args++;
169f579bf8eSKris Kennaway 				from = *args;
170f579bf8eSKris Kennaway 			} else badarg = 1;
171f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-subject")) {
172f579bf8eSKris Kennaway 			if (args[1]) {
173f579bf8eSKris Kennaway 				args++;
174f579bf8eSKris Kennaway 				subject = *args;
175f579bf8eSKris Kennaway 			} else badarg = 1;
176f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-signer")) {
177f579bf8eSKris Kennaway 			if (args[1]) {
178f579bf8eSKris Kennaway 				args++;
179f579bf8eSKris Kennaway 				signerfile = *args;
180f579bf8eSKris Kennaway 			} else badarg = 1;
181f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-recip")) {
182f579bf8eSKris Kennaway 			if (args[1]) {
183f579bf8eSKris Kennaway 				args++;
184f579bf8eSKris Kennaway 				recipfile = *args;
185f579bf8eSKris Kennaway 			} else badarg = 1;
186f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-inkey")) {
187f579bf8eSKris Kennaway 			if (args[1]) {
188f579bf8eSKris Kennaway 				args++;
189f579bf8eSKris Kennaway 				keyfile = *args;
190f579bf8eSKris Kennaway 			} else badarg = 1;
191f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-certfile")) {
192f579bf8eSKris Kennaway 			if (args[1]) {
193f579bf8eSKris Kennaway 				args++;
194f579bf8eSKris Kennaway 				certfile = *args;
195f579bf8eSKris Kennaway 			} else badarg = 1;
196f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-CAfile")) {
197f579bf8eSKris Kennaway 			if (args[1]) {
198f579bf8eSKris Kennaway 				args++;
199f579bf8eSKris Kennaway 				CAfile = *args;
200f579bf8eSKris Kennaway 			} else badarg = 1;
201f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-CApath")) {
202f579bf8eSKris Kennaway 			if (args[1]) {
203f579bf8eSKris Kennaway 				args++;
204f579bf8eSKris Kennaway 				CApath = *args;
205f579bf8eSKris Kennaway 			} else badarg = 1;
206f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-in")) {
207f579bf8eSKris Kennaway 			if (args[1]) {
208f579bf8eSKris Kennaway 				args++;
209f579bf8eSKris Kennaway 				infile = *args;
210f579bf8eSKris Kennaway 			} else badarg = 1;
211ddd58736SKris Kennaway 		} else if (!strcmp (*args, "-inform")) {
212ddd58736SKris Kennaway 			if (args[1]) {
213ddd58736SKris Kennaway 				args++;
214ddd58736SKris Kennaway 				informat = str2fmt(*args);
215ddd58736SKris Kennaway 			} else badarg = 1;
216ddd58736SKris Kennaway 		} else if (!strcmp (*args, "-outform")) {
217ddd58736SKris Kennaway 			if (args[1]) {
218ddd58736SKris Kennaway 				args++;
219ddd58736SKris Kennaway 				outformat = str2fmt(*args);
220ddd58736SKris Kennaway 			} else badarg = 1;
221f579bf8eSKris Kennaway 		} else if (!strcmp (*args, "-out")) {
222f579bf8eSKris Kennaway 			if (args[1]) {
223f579bf8eSKris Kennaway 				args++;
224f579bf8eSKris Kennaway 				outfile = *args;
225f579bf8eSKris Kennaway 			} else badarg = 1;
226ddd58736SKris Kennaway 		} else if (!strcmp (*args, "-content")) {
227ddd58736SKris Kennaway 			if (args[1]) {
228ddd58736SKris Kennaway 				args++;
229ddd58736SKris Kennaway 				contfile = *args;
230ddd58736SKris Kennaway 			} else badarg = 1;
231f579bf8eSKris Kennaway 		} else badarg = 1;
232f579bf8eSKris Kennaway 		args++;
233f579bf8eSKris Kennaway 	}
234f579bf8eSKris Kennaway 
235f579bf8eSKris Kennaway 	if(operation == SMIME_SIGN) {
236f579bf8eSKris Kennaway 		if(!signerfile) {
237f579bf8eSKris Kennaway 			BIO_printf(bio_err, "No signer certificate specified\n");
238f579bf8eSKris Kennaway 			badarg = 1;
239f579bf8eSKris Kennaway 		}
240f579bf8eSKris Kennaway 		need_rand = 1;
241f579bf8eSKris Kennaway 	} else if(operation == SMIME_DECRYPT) {
242f579bf8eSKris Kennaway 		if(!recipfile) {
243f579bf8eSKris Kennaway 			BIO_printf(bio_err, "No recipient certificate and key specified\n");
244f579bf8eSKris Kennaway 			badarg = 1;
245f579bf8eSKris Kennaway 		}
246f579bf8eSKris Kennaway 	} else if(operation == SMIME_ENCRYPT) {
247f579bf8eSKris Kennaway 		if(!*args) {
248f579bf8eSKris Kennaway 			BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
249f579bf8eSKris Kennaway 			badarg = 1;
250f579bf8eSKris Kennaway 		}
251f579bf8eSKris Kennaway 		need_rand = 1;
252f579bf8eSKris Kennaway 	} else if(!operation) badarg = 1;
253f579bf8eSKris Kennaway 
254f579bf8eSKris Kennaway 	if (badarg) {
255f579bf8eSKris Kennaway 		BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n");
256f579bf8eSKris Kennaway 		BIO_printf (bio_err, "where options are\n");
257f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-encrypt       encrypt message\n");
258f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-decrypt       decrypt encrypted message\n");
259f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-sign          sign message\n");
260f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-verify        verify signed message\n");
261f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-pk7out        output PKCS#7 structure\n");
262f579bf8eSKris Kennaway #ifndef NO_DES
263f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-des3          encrypt with triple DES\n");
264f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-des           encrypt with DES\n");
265f579bf8eSKris Kennaway #endif
266f579bf8eSKris Kennaway #ifndef NO_RC2
267f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-rc2-40        encrypt with RC2-40 (default)\n");
268f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-rc2-64        encrypt with RC2-64\n");
269f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-rc2-128       encrypt with RC2-128\n");
270f579bf8eSKris Kennaway #endif
271f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-nointern      don't search certificates in message for signer\n");
272f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-nosigs        don't verify message signature\n");
273f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-noverify      don't verify signers certificate\n");
274f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-nocerts       don't include signers certificate when signing\n");
275f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-nodetach      use opaque signing\n");
276f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-noattr        don't include any signed attributes\n");
277f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-binary        don't translate message to text\n");
278f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-certfile file other certificates file\n");
279f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-signer file   signer certificate file\n");
280f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-recip  file   recipient certificate file for decryption\n");
281f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-in file       input file\n");
282ddd58736SKris Kennaway 		BIO_printf (bio_err, "-inform arg    input format SMIME (default), PEM or DER\n");
283f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-inkey file    input private key (if not signer or recipient)\n");
284f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-out file      output file\n");
285ddd58736SKris Kennaway 		BIO_printf (bio_err, "-outform arg   output format SMIME (default), PEM or DER\n");
286ddd58736SKris Kennaway 		BIO_printf (bio_err, "-content file  supply or override content for detached signature\n");
287f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-to addr       to address\n");
288f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-from ad       from address\n");
289f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-subject s     subject\n");
290f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-text          include or delete text MIME headers\n");
291f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-CApath dir    trusted certificates directory\n");
292f579bf8eSKris Kennaway 		BIO_printf (bio_err, "-CAfile file   trusted certificates file\n");
29326d191b4SKris Kennaway 		BIO_printf (bio_err, "-passin arg    input file pass phrase source\n");
294f579bf8eSKris Kennaway 		BIO_printf(bio_err,  "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
295f579bf8eSKris Kennaway 		BIO_printf(bio_err,  "               load the file (or the files in the directory) into\n");
296f579bf8eSKris Kennaway 		BIO_printf(bio_err,  "               the random number generator\n");
297f579bf8eSKris Kennaway 		BIO_printf (bio_err, "cert.pem       recipient certificate(s) for encryption\n");
298f579bf8eSKris Kennaway 		goto end;
299f579bf8eSKris Kennaway 	}
300f579bf8eSKris Kennaway 
301f579bf8eSKris Kennaway 	if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
302f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Error getting password\n");
303f579bf8eSKris Kennaway 		goto end;
304f579bf8eSKris Kennaway 	}
305f579bf8eSKris Kennaway 
306f579bf8eSKris Kennaway 	if (need_rand) {
307f579bf8eSKris Kennaway 		app_RAND_load_file(NULL, bio_err, (inrand != NULL));
308f579bf8eSKris Kennaway 		if (inrand != NULL)
309f579bf8eSKris Kennaway 			BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
310f579bf8eSKris Kennaway 				app_RAND_load_files(inrand));
311f579bf8eSKris Kennaway 	}
312f579bf8eSKris Kennaway 
313f579bf8eSKris Kennaway 	ret = 2;
314f579bf8eSKris Kennaway 
315f579bf8eSKris Kennaway 	if(operation != SMIME_SIGN) flags &= ~PKCS7_DETACHED;
316f579bf8eSKris Kennaway 
317ddd58736SKris Kennaway 	if(operation & SMIME_OP) {
318ddd58736SKris Kennaway 		if(flags & PKCS7_BINARY) inmode = "rb";
319ddd58736SKris Kennaway 		if(outformat == FORMAT_ASN1) outmode = "wb";
320ddd58736SKris Kennaway 	} else {
321ddd58736SKris Kennaway 		if(flags & PKCS7_BINARY) outmode = "wb";
322ddd58736SKris Kennaway 		if(informat == FORMAT_ASN1) inmode = "rb";
323f579bf8eSKris Kennaway 	}
324f579bf8eSKris Kennaway 
325f579bf8eSKris Kennaway 	if(operation == SMIME_ENCRYPT) {
326f579bf8eSKris Kennaway 		if (!cipher) {
327f579bf8eSKris Kennaway #ifndef NO_RC2
328f579bf8eSKris Kennaway 			cipher = EVP_rc2_40_cbc();
329f579bf8eSKris Kennaway #else
330f579bf8eSKris Kennaway 			BIO_printf(bio_err, "No cipher selected\n");
331f579bf8eSKris Kennaway 			goto end;
332f579bf8eSKris Kennaway #endif
333f579bf8eSKris Kennaway 		}
334f579bf8eSKris Kennaway 		encerts = sk_X509_new_null();
335f579bf8eSKris Kennaway 		while (*args) {
336ddd58736SKris Kennaway 			if(!(cert = load_cert(bio_err,*args,FORMAT_PEM))) {
337f579bf8eSKris Kennaway 				BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args);
338f579bf8eSKris Kennaway 				goto end;
339f579bf8eSKris Kennaway 			}
340f579bf8eSKris Kennaway 			sk_X509_push(encerts, cert);
341f579bf8eSKris Kennaway 			cert = NULL;
342f579bf8eSKris Kennaway 			args++;
343f579bf8eSKris Kennaway 		}
344f579bf8eSKris Kennaway 	}
345f579bf8eSKris Kennaway 
346f579bf8eSKris Kennaway 	if(signerfile && (operation == SMIME_SIGN)) {
347ddd58736SKris Kennaway 		if(!(signer = load_cert(bio_err,signerfile,FORMAT_PEM))) {
348f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile);
349f579bf8eSKris Kennaway 			goto end;
350f579bf8eSKris Kennaway 		}
351f579bf8eSKris Kennaway 	}
352f579bf8eSKris Kennaway 
353f579bf8eSKris Kennaway 	if(certfile) {
354ddd58736SKris Kennaway 		if(!(other = load_certs(bio_err,certfile,FORMAT_PEM))) {
355f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Can't read certificate file %s\n", certfile);
356f579bf8eSKris Kennaway 			ERR_print_errors(bio_err);
357f579bf8eSKris Kennaway 			goto end;
358f579bf8eSKris Kennaway 		}
359f579bf8eSKris Kennaway 	}
360f579bf8eSKris Kennaway 
361f579bf8eSKris Kennaway 	if(recipfile && (operation == SMIME_DECRYPT)) {
362ddd58736SKris Kennaway 		if(!(recip = load_cert(bio_err,recipfile,FORMAT_PEM))) {
363f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile);
364f579bf8eSKris Kennaway 			ERR_print_errors(bio_err);
365f579bf8eSKris Kennaway 			goto end;
366f579bf8eSKris Kennaway 		}
367f579bf8eSKris Kennaway 	}
368f579bf8eSKris Kennaway 
369f579bf8eSKris Kennaway 	if(operation == SMIME_DECRYPT) {
370f579bf8eSKris Kennaway 		if(!keyfile) keyfile = recipfile;
371f579bf8eSKris Kennaway 	} else if(operation == SMIME_SIGN) {
372f579bf8eSKris Kennaway 		if(!keyfile) keyfile = signerfile;
373f579bf8eSKris Kennaway 	} else keyfile = NULL;
374f579bf8eSKris Kennaway 
375f579bf8eSKris Kennaway 	if(keyfile) {
376ddd58736SKris Kennaway 		if(!(key = load_key(bio_err,keyfile, FORMAT_PEM, passin))) {
377f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Can't read recipient certificate file %s\n", keyfile);
378f579bf8eSKris Kennaway 			ERR_print_errors(bio_err);
379f579bf8eSKris Kennaway 			goto end;
380f579bf8eSKris Kennaway 		}
381f579bf8eSKris Kennaway 	}
382f579bf8eSKris Kennaway 
383f579bf8eSKris Kennaway 	if (infile) {
384f579bf8eSKris Kennaway 		if (!(in = BIO_new_file(infile, inmode))) {
385f579bf8eSKris Kennaway 			BIO_printf (bio_err,
386f579bf8eSKris Kennaway 				 "Can't open input file %s\n", infile);
387f579bf8eSKris Kennaway 			goto end;
388f579bf8eSKris Kennaway 		}
389f579bf8eSKris Kennaway 	} else in = BIO_new_fp(stdin, BIO_NOCLOSE);
390f579bf8eSKris Kennaway 
391f579bf8eSKris Kennaway 	if (outfile) {
392f579bf8eSKris Kennaway 		if (!(out = BIO_new_file(outfile, outmode))) {
393f579bf8eSKris Kennaway 			BIO_printf (bio_err,
394f579bf8eSKris Kennaway 				 "Can't open output file %s\n", outfile);
395f579bf8eSKris Kennaway 			goto end;
396f579bf8eSKris Kennaway 		}
397ddd58736SKris Kennaway 	} else {
398ddd58736SKris Kennaway 		out = BIO_new_fp(stdout, BIO_NOCLOSE);
399ddd58736SKris Kennaway #ifdef VMS
400ddd58736SKris Kennaway 		{
401ddd58736SKris Kennaway 		    BIO *tmpbio = BIO_new(BIO_f_linebuffer());
402ddd58736SKris Kennaway 		    out = BIO_push(tmpbio, out);
403ddd58736SKris Kennaway 		}
404ddd58736SKris Kennaway #endif
405ddd58736SKris Kennaway 	}
406f579bf8eSKris Kennaway 
407f579bf8eSKris Kennaway 	if(operation == SMIME_VERIFY) {
408f579bf8eSKris Kennaway 		if(!(store = setup_verify(CAfile, CApath))) goto end;
409f579bf8eSKris Kennaway 	}
410f579bf8eSKris Kennaway 
411f579bf8eSKris Kennaway 	ret = 3;
412f579bf8eSKris Kennaway 
413f579bf8eSKris Kennaway 	if(operation == SMIME_ENCRYPT) {
414f579bf8eSKris Kennaway 		p7 = PKCS7_encrypt(encerts, in, cipher, flags);
415f579bf8eSKris Kennaway 	} else if(operation == SMIME_SIGN) {
416f579bf8eSKris Kennaway 		p7 = PKCS7_sign(signer, key, other, in, flags);
417c1803d78SJacques Vidrine 		if (BIO_reset(in) != 0 && (flags & PKCS7_DETACHED)) {
418c1803d78SJacques Vidrine 		  BIO_printf(bio_err, "Can't rewind input file\n");
419c1803d78SJacques Vidrine 		  goto end;
420c1803d78SJacques Vidrine 		}
421f579bf8eSKris Kennaway 	} else {
422ddd58736SKris Kennaway 		if(informat == FORMAT_SMIME)
423ddd58736SKris Kennaway 			p7 = SMIME_read_PKCS7(in, &indata);
424ddd58736SKris Kennaway 		else if(informat == FORMAT_PEM)
425ddd58736SKris Kennaway 			p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
426ddd58736SKris Kennaway 		else if(informat == FORMAT_ASN1)
427ddd58736SKris Kennaway 			p7 = d2i_PKCS7_bio(in, NULL);
428ddd58736SKris Kennaway 		else {
429ddd58736SKris Kennaway 			BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
430ddd58736SKris Kennaway 			goto end;
431ddd58736SKris Kennaway 		}
432ddd58736SKris Kennaway 
433ddd58736SKris Kennaway 		if(!p7) {
434f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error reading S/MIME message\n");
435f579bf8eSKris Kennaway 			goto end;
436f579bf8eSKris Kennaway 		}
437ddd58736SKris Kennaway 		if(contfile) {
438ddd58736SKris Kennaway 			BIO_free(indata);
439ddd58736SKris Kennaway 			if(!(indata = BIO_new_file(contfile, "rb"))) {
440ddd58736SKris Kennaway 				BIO_printf(bio_err, "Can't read content file %s\n", contfile);
441ddd58736SKris Kennaway 				goto end;
442ddd58736SKris Kennaway 			}
443ddd58736SKris Kennaway 		}
444f579bf8eSKris Kennaway 	}
445f579bf8eSKris Kennaway 
446f579bf8eSKris Kennaway 	if(!p7) {
447f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
448f579bf8eSKris Kennaway 		goto end;
449f579bf8eSKris Kennaway 	}
450f579bf8eSKris Kennaway 
451f579bf8eSKris Kennaway 	ret = 4;
452f579bf8eSKris Kennaway 	if(operation == SMIME_DECRYPT) {
453f579bf8eSKris Kennaway 		if(!PKCS7_decrypt(p7, key, recip, out, flags)) {
454f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
455f579bf8eSKris Kennaway 			goto end;
456f579bf8eSKris Kennaway 		}
457f579bf8eSKris Kennaway 	} else if(operation == SMIME_VERIFY) {
458f579bf8eSKris Kennaway 		STACK_OF(X509) *signers;
459f579bf8eSKris Kennaway 		if(PKCS7_verify(p7, other, store, indata, out, flags)) {
460c1803d78SJacques Vidrine 			BIO_printf(bio_err, "Verification successful\n");
461f579bf8eSKris Kennaway 		} else {
462c1803d78SJacques Vidrine 			BIO_printf(bio_err, "Verification failure\n");
463f579bf8eSKris Kennaway 			goto end;
464f579bf8eSKris Kennaway 		}
465f579bf8eSKris Kennaway 		signers = PKCS7_get0_signers(p7, other, flags);
466f579bf8eSKris Kennaway 		if(!save_certs(signerfile, signers)) {
467f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error writing signers to %s\n",
468f579bf8eSKris Kennaway 								signerfile);
469f579bf8eSKris Kennaway 			ret = 5;
470f579bf8eSKris Kennaway 			goto end;
471f579bf8eSKris Kennaway 		}
472f579bf8eSKris Kennaway 		sk_X509_free(signers);
473f579bf8eSKris Kennaway 	} else if(operation == SMIME_PK7OUT) {
474f579bf8eSKris Kennaway 		PEM_write_bio_PKCS7(out, p7);
475f579bf8eSKris Kennaway 	} else {
476f579bf8eSKris Kennaway 		if(to) BIO_printf(out, "To: %s\n", to);
477f579bf8eSKris Kennaway 		if(from) BIO_printf(out, "From: %s\n", from);
478f579bf8eSKris Kennaway 		if(subject) BIO_printf(out, "Subject: %s\n", subject);
479ddd58736SKris Kennaway 		if(outformat == FORMAT_SMIME)
480f579bf8eSKris Kennaway 			SMIME_write_PKCS7(out, p7, in, flags);
481ddd58736SKris Kennaway 		else if(outformat == FORMAT_PEM)
482ddd58736SKris Kennaway 			PEM_write_bio_PKCS7(out,p7);
483ddd58736SKris Kennaway 		else if(outformat == FORMAT_ASN1)
484ddd58736SKris Kennaway 			i2d_PKCS7_bio(out,p7);
485ddd58736SKris Kennaway 		else {
486ddd58736SKris Kennaway 			BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
487ddd58736SKris Kennaway 			goto end;
488ddd58736SKris Kennaway 		}
489f579bf8eSKris Kennaway 	}
490f579bf8eSKris Kennaway 	ret = 0;
491f579bf8eSKris Kennaway end:
492f579bf8eSKris Kennaway 	if (need_rand)
493f579bf8eSKris Kennaway 		app_RAND_write_file(NULL, bio_err);
494f579bf8eSKris Kennaway 	if(ret) ERR_print_errors(bio_err);
495f579bf8eSKris Kennaway 	sk_X509_pop_free(encerts, X509_free);
496f579bf8eSKris Kennaway 	sk_X509_pop_free(other, X509_free);
497f579bf8eSKris Kennaway 	X509_STORE_free(store);
498f579bf8eSKris Kennaway 	X509_free(cert);
499f579bf8eSKris Kennaway 	X509_free(recip);
500f579bf8eSKris Kennaway 	X509_free(signer);
501f579bf8eSKris Kennaway 	EVP_PKEY_free(key);
502f579bf8eSKris Kennaway 	PKCS7_free(p7);
503f579bf8eSKris Kennaway 	BIO_free(in);
504f579bf8eSKris Kennaway 	BIO_free(indata);
505ddd58736SKris Kennaway 	BIO_free_all(out);
506ddd58736SKris Kennaway 	if(passin) OPENSSL_free(passin);
507f579bf8eSKris Kennaway 	return (ret);
508f579bf8eSKris Kennaway }
509f579bf8eSKris Kennaway 
510f579bf8eSKris Kennaway static X509_STORE *setup_verify(char *CAfile, char *CApath)
511f579bf8eSKris Kennaway {
512f579bf8eSKris Kennaway 	X509_STORE *store;
513f579bf8eSKris Kennaway 	X509_LOOKUP *lookup;
514f579bf8eSKris Kennaway 	if(!(store = X509_STORE_new())) goto end;
515f579bf8eSKris Kennaway 	lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
516f579bf8eSKris Kennaway 	if (lookup == NULL) goto end;
517f579bf8eSKris Kennaway 	if (CAfile) {
518f579bf8eSKris Kennaway 		if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
519f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error loading file %s\n", CAfile);
520f579bf8eSKris Kennaway 			goto end;
521f579bf8eSKris Kennaway 		}
522f579bf8eSKris Kennaway 	} else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
523f579bf8eSKris Kennaway 
524f579bf8eSKris Kennaway 	lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
525f579bf8eSKris Kennaway 	if (lookup == NULL) goto end;
526f579bf8eSKris Kennaway 	if (CApath) {
527f579bf8eSKris Kennaway 		if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
528f579bf8eSKris Kennaway 			BIO_printf(bio_err, "Error loading directory %s\n", CApath);
529f579bf8eSKris Kennaway 			goto end;
530f579bf8eSKris Kennaway 		}
531f579bf8eSKris Kennaway 	} else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
532f579bf8eSKris Kennaway 
533f579bf8eSKris Kennaway 	ERR_clear_error();
534f579bf8eSKris Kennaway 	return store;
535f579bf8eSKris Kennaway 	end:
536f579bf8eSKris Kennaway 	X509_STORE_free(store);
537f579bf8eSKris Kennaway 	return NULL;
538f579bf8eSKris Kennaway }
539f579bf8eSKris Kennaway 
540f579bf8eSKris Kennaway static int save_certs(char *signerfile, STACK_OF(X509) *signers)
541f579bf8eSKris Kennaway {
542f579bf8eSKris Kennaway 	int i;
543f579bf8eSKris Kennaway 	BIO *tmp;
544f579bf8eSKris Kennaway 	if(!signerfile) return 1;
545f579bf8eSKris Kennaway 	tmp = BIO_new_file(signerfile, "w");
546f579bf8eSKris Kennaway 	if(!tmp) return 0;
547f579bf8eSKris Kennaway 	for(i = 0; i < sk_X509_num(signers); i++)
548f579bf8eSKris Kennaway 		PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
549f579bf8eSKris Kennaway 	BIO_free(tmp);
550f579bf8eSKris Kennaway 	return 1;
551f579bf8eSKris Kennaway }
552f579bf8eSKris Kennaway 
553