10Sstevel@tonic-gate /* smime.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*2139Sjp161948 * project.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
6*2139Sjp161948 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate /* S/MIME utility function */
600Sstevel@tonic-gate
610Sstevel@tonic-gate #include <stdio.h>
620Sstevel@tonic-gate #include <string.h>
630Sstevel@tonic-gate #include "apps.h"
640Sstevel@tonic-gate #include <openssl/crypto.h>
650Sstevel@tonic-gate #include <openssl/pem.h>
660Sstevel@tonic-gate #include <openssl/err.h>
67*2139Sjp161948 #include <openssl/x509_vfy.h>
68*2139Sjp161948 #include <openssl/x509v3.h>
690Sstevel@tonic-gate
700Sstevel@tonic-gate #undef PROG
710Sstevel@tonic-gate #define PROG smime_main
720Sstevel@tonic-gate static int save_certs(char *signerfile, STACK_OF(X509) *signers);
73*2139Sjp161948 static int smime_cb(int ok, X509_STORE_CTX *ctx);
740Sstevel@tonic-gate
750Sstevel@tonic-gate #define SMIME_OP 0x10
760Sstevel@tonic-gate #define SMIME_ENCRYPT (1 | SMIME_OP)
770Sstevel@tonic-gate #define SMIME_DECRYPT 2
780Sstevel@tonic-gate #define SMIME_SIGN (3 | SMIME_OP)
790Sstevel@tonic-gate #define SMIME_VERIFY 4
800Sstevel@tonic-gate #define SMIME_PK7OUT 5
810Sstevel@tonic-gate
820Sstevel@tonic-gate int MAIN(int, char **);
830Sstevel@tonic-gate
MAIN(int argc,char ** argv)840Sstevel@tonic-gate int MAIN(int argc, char **argv)
85*2139Sjp161948 {
860Sstevel@tonic-gate ENGINE *e = NULL;
870Sstevel@tonic-gate int operation = 0;
880Sstevel@tonic-gate int ret = 0;
890Sstevel@tonic-gate char **args;
90*2139Sjp161948 const char *inmode = "r", *outmode = "w";
910Sstevel@tonic-gate char *infile = NULL, *outfile = NULL;
920Sstevel@tonic-gate char *signerfile = NULL, *recipfile = NULL;
930Sstevel@tonic-gate char *certfile = NULL, *keyfile = NULL, *contfile=NULL;
940Sstevel@tonic-gate const EVP_CIPHER *cipher = NULL;
950Sstevel@tonic-gate PKCS7 *p7 = NULL;
960Sstevel@tonic-gate X509_STORE *store = NULL;
970Sstevel@tonic-gate X509 *cert = NULL, *recip = NULL, *signer = NULL;
980Sstevel@tonic-gate EVP_PKEY *key = NULL;
990Sstevel@tonic-gate STACK_OF(X509) *encerts = NULL, *other = NULL;
1000Sstevel@tonic-gate BIO *in = NULL, *out = NULL, *indata = NULL;
1010Sstevel@tonic-gate int badarg = 0;
102*2139Sjp161948 int flags = PKCS7_DETACHED;
1030Sstevel@tonic-gate char *to = NULL, *from = NULL, *subject = NULL;
1040Sstevel@tonic-gate char *CAfile = NULL, *CApath = NULL;
1050Sstevel@tonic-gate char *passargin = NULL, *passin = NULL;
1060Sstevel@tonic-gate char *inrand = NULL;
1070Sstevel@tonic-gate int need_rand = 0;
1080Sstevel@tonic-gate int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
1090Sstevel@tonic-gate int keyform = FORMAT_PEM;
1100Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1110Sstevel@tonic-gate char *engine=NULL;
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate
114*2139Sjp161948 X509_VERIFY_PARAM *vpm = NULL;
115*2139Sjp161948
1160Sstevel@tonic-gate args = argv + 1;
1170Sstevel@tonic-gate ret = 1;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate apps_startup();
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (bio_err == NULL)
122*2139Sjp161948 {
1230Sstevel@tonic-gate if ((bio_err = BIO_new(BIO_s_file())) != NULL)
1240Sstevel@tonic-gate BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
125*2139Sjp161948 }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if (!load_config(bio_err, NULL))
1280Sstevel@tonic-gate goto end;
1290Sstevel@tonic-gate
130*2139Sjp161948 while (!badarg && *args && *args[0] == '-')
131*2139Sjp161948 {
132*2139Sjp161948 if (!strcmp (*args, "-encrypt"))
133*2139Sjp161948 operation = SMIME_ENCRYPT;
134*2139Sjp161948 else if (!strcmp (*args, "-decrypt"))
135*2139Sjp161948 operation = SMIME_DECRYPT;
136*2139Sjp161948 else if (!strcmp (*args, "-sign"))
137*2139Sjp161948 operation = SMIME_SIGN;
138*2139Sjp161948 else if (!strcmp (*args, "-verify"))
139*2139Sjp161948 operation = SMIME_VERIFY;
140*2139Sjp161948 else if (!strcmp (*args, "-pk7out"))
141*2139Sjp161948 operation = SMIME_PK7OUT;
1420Sstevel@tonic-gate #ifndef OPENSSL_NO_DES
1430Sstevel@tonic-gate else if (!strcmp (*args, "-des3"))
1440Sstevel@tonic-gate cipher = EVP_des_ede3_cbc();
1450Sstevel@tonic-gate else if (!strcmp (*args, "-des"))
1460Sstevel@tonic-gate cipher = EVP_des_cbc();
1470Sstevel@tonic-gate #endif
1480Sstevel@tonic-gate #ifndef OPENSSL_NO_RC2
1490Sstevel@tonic-gate else if (!strcmp (*args, "-rc2-40"))
1500Sstevel@tonic-gate cipher = EVP_rc2_40_cbc();
1510Sstevel@tonic-gate else if (!strcmp (*args, "-rc2-128"))
1520Sstevel@tonic-gate cipher = EVP_rc2_cbc();
1530Sstevel@tonic-gate else if (!strcmp (*args, "-rc2-64"))
1540Sstevel@tonic-gate cipher = EVP_rc2_64_cbc();
1550Sstevel@tonic-gate #endif
1560Sstevel@tonic-gate #ifndef OPENSSL_NO_AES
1570Sstevel@tonic-gate else if (!strcmp(*args,"-aes128"))
1580Sstevel@tonic-gate cipher = EVP_aes_128_cbc();
1590Sstevel@tonic-gate else if (!strcmp(*args,"-aes192"))
1600Sstevel@tonic-gate cipher = EVP_aes_192_cbc();
1610Sstevel@tonic-gate else if (!strcmp(*args,"-aes256"))
1620Sstevel@tonic-gate cipher = EVP_aes_256_cbc();
163688Sjp161948 #endif
1640Sstevel@tonic-gate else if (!strcmp (*args, "-text"))
1650Sstevel@tonic-gate flags |= PKCS7_TEXT;
1660Sstevel@tonic-gate else if (!strcmp (*args, "-nointern"))
1670Sstevel@tonic-gate flags |= PKCS7_NOINTERN;
1680Sstevel@tonic-gate else if (!strcmp (*args, "-noverify"))
1690Sstevel@tonic-gate flags |= PKCS7_NOVERIFY;
1700Sstevel@tonic-gate else if (!strcmp (*args, "-nochain"))
1710Sstevel@tonic-gate flags |= PKCS7_NOCHAIN;
1720Sstevel@tonic-gate else if (!strcmp (*args, "-nocerts"))
1730Sstevel@tonic-gate flags |= PKCS7_NOCERTS;
1740Sstevel@tonic-gate else if (!strcmp (*args, "-noattr"))
1750Sstevel@tonic-gate flags |= PKCS7_NOATTR;
1760Sstevel@tonic-gate else if (!strcmp (*args, "-nodetach"))
1770Sstevel@tonic-gate flags &= ~PKCS7_DETACHED;
1780Sstevel@tonic-gate else if (!strcmp (*args, "-nosmimecap"))
1790Sstevel@tonic-gate flags |= PKCS7_NOSMIMECAP;
1800Sstevel@tonic-gate else if (!strcmp (*args, "-binary"))
1810Sstevel@tonic-gate flags |= PKCS7_BINARY;
1820Sstevel@tonic-gate else if (!strcmp (*args, "-nosigs"))
1830Sstevel@tonic-gate flags |= PKCS7_NOSIGS;
1840Sstevel@tonic-gate else if (!strcmp (*args, "-nooldmime"))
1850Sstevel@tonic-gate flags |= PKCS7_NOOLDMIMETYPE;
1860Sstevel@tonic-gate else if (!strcmp (*args, "-crlfeol"))
1870Sstevel@tonic-gate flags |= PKCS7_CRLFEOL;
188*2139Sjp161948 else if (!strcmp(*args,"-rand"))
189*2139Sjp161948 {
190*2139Sjp161948 if (args[1])
191*2139Sjp161948 {
1920Sstevel@tonic-gate args++;
1930Sstevel@tonic-gate inrand = *args;
194*2139Sjp161948 }
195*2139Sjp161948 else
196*2139Sjp161948 badarg = 1;
1970Sstevel@tonic-gate need_rand = 1;
198*2139Sjp161948 }
1990Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
200*2139Sjp161948 else if (!strcmp(*args,"-engine"))
201*2139Sjp161948 {
202*2139Sjp161948 if (args[1])
203*2139Sjp161948 {
2040Sstevel@tonic-gate args++;
2050Sstevel@tonic-gate engine = *args;
206*2139Sjp161948 }
207*2139Sjp161948 else badarg = 1;
208*2139Sjp161948 }
2090Sstevel@tonic-gate #endif
210*2139Sjp161948 else if (!strcmp(*args,"-passin"))
211*2139Sjp161948 {
212*2139Sjp161948 if (args[1])
213*2139Sjp161948 {
2140Sstevel@tonic-gate args++;
2150Sstevel@tonic-gate passargin = *args;
216*2139Sjp161948 }
217*2139Sjp161948 else
218*2139Sjp161948 badarg = 1;
219*2139Sjp161948 }
220*2139Sjp161948 else if (!strcmp (*args, "-to"))
221*2139Sjp161948 {
222*2139Sjp161948 if (args[1])
223*2139Sjp161948 {
2240Sstevel@tonic-gate args++;
2250Sstevel@tonic-gate to = *args;
226*2139Sjp161948 }
227*2139Sjp161948 else
228*2139Sjp161948 badarg = 1;
229*2139Sjp161948 }
230*2139Sjp161948 else if (!strcmp (*args, "-from"))
231*2139Sjp161948 {
232*2139Sjp161948 if (args[1])
233*2139Sjp161948 {
2340Sstevel@tonic-gate args++;
2350Sstevel@tonic-gate from = *args;
236*2139Sjp161948 }
237*2139Sjp161948 else badarg = 1;
238*2139Sjp161948 }
239*2139Sjp161948 else if (!strcmp (*args, "-subject"))
240*2139Sjp161948 {
241*2139Sjp161948 if (args[1])
242*2139Sjp161948 {
2430Sstevel@tonic-gate args++;
2440Sstevel@tonic-gate subject = *args;
245*2139Sjp161948 }
246*2139Sjp161948 else
247*2139Sjp161948 badarg = 1;
248*2139Sjp161948 }
249*2139Sjp161948 else if (!strcmp (*args, "-signer"))
250*2139Sjp161948 {
251*2139Sjp161948 if (args[1])
252*2139Sjp161948 {
2530Sstevel@tonic-gate args++;
2540Sstevel@tonic-gate signerfile = *args;
255*2139Sjp161948 }
256*2139Sjp161948 else
257*2139Sjp161948 badarg = 1;
258*2139Sjp161948 }
259*2139Sjp161948 else if (!strcmp (*args, "-recip"))
260*2139Sjp161948 {
261*2139Sjp161948 if (args[1])
262*2139Sjp161948 {
2630Sstevel@tonic-gate args++;
2640Sstevel@tonic-gate recipfile = *args;
265*2139Sjp161948 }
266*2139Sjp161948 else badarg = 1;
267*2139Sjp161948 }
268*2139Sjp161948 else if (!strcmp (*args, "-inkey"))
269*2139Sjp161948 {
270*2139Sjp161948 if (args[1])
271*2139Sjp161948 {
2720Sstevel@tonic-gate args++;
2730Sstevel@tonic-gate keyfile = *args;
274*2139Sjp161948 }
275*2139Sjp161948 else
276*2139Sjp161948 badarg = 1;
277*2139Sjp161948 }
278*2139Sjp161948 else if (!strcmp (*args, "-keyform"))
279*2139Sjp161948 {
280*2139Sjp161948 if (args[1])
281*2139Sjp161948 {
2820Sstevel@tonic-gate args++;
2830Sstevel@tonic-gate keyform = str2fmt(*args);
284*2139Sjp161948 }
285*2139Sjp161948 else
286*2139Sjp161948 badarg = 1;
287*2139Sjp161948 }
288*2139Sjp161948 else if (!strcmp (*args, "-certfile"))
289*2139Sjp161948 {
290*2139Sjp161948 if (args[1])
291*2139Sjp161948 {
2920Sstevel@tonic-gate args++;
2930Sstevel@tonic-gate certfile = *args;
294*2139Sjp161948 }
295*2139Sjp161948 else
296*2139Sjp161948 badarg = 1;
297*2139Sjp161948 }
298*2139Sjp161948 else if (!strcmp (*args, "-CAfile"))
299*2139Sjp161948 {
300*2139Sjp161948 if (args[1])
301*2139Sjp161948 {
3020Sstevel@tonic-gate args++;
3030Sstevel@tonic-gate CAfile = *args;
304*2139Sjp161948 }
305*2139Sjp161948 else
306*2139Sjp161948 badarg = 1;
307*2139Sjp161948 }
308*2139Sjp161948 else if (!strcmp (*args, "-CApath"))
309*2139Sjp161948 {
310*2139Sjp161948 if (args[1])
311*2139Sjp161948 {
3120Sstevel@tonic-gate args++;
3130Sstevel@tonic-gate CApath = *args;
314*2139Sjp161948 }
315*2139Sjp161948 else
316*2139Sjp161948 badarg = 1;
317*2139Sjp161948 }
318*2139Sjp161948 else if (!strcmp (*args, "-in"))
319*2139Sjp161948 {
320*2139Sjp161948 if (args[1])
321*2139Sjp161948 {
3220Sstevel@tonic-gate args++;
3230Sstevel@tonic-gate infile = *args;
324*2139Sjp161948 }
325*2139Sjp161948 else
326*2139Sjp161948 badarg = 1;
327*2139Sjp161948 }
328*2139Sjp161948 else if (!strcmp (*args, "-inform"))
329*2139Sjp161948 {
330*2139Sjp161948 if (args[1])
331*2139Sjp161948 {
3320Sstevel@tonic-gate args++;
3330Sstevel@tonic-gate informat = str2fmt(*args);
334*2139Sjp161948 }
335*2139Sjp161948 else
336*2139Sjp161948 badarg = 1;
337*2139Sjp161948 }
338*2139Sjp161948 else if (!strcmp (*args, "-outform"))
339*2139Sjp161948 {
340*2139Sjp161948 if (args[1])
341*2139Sjp161948 {
3420Sstevel@tonic-gate args++;
3430Sstevel@tonic-gate outformat = str2fmt(*args);
344*2139Sjp161948 }
345*2139Sjp161948 else
346*2139Sjp161948 badarg = 1;
347*2139Sjp161948 }
348*2139Sjp161948 else if (!strcmp (*args, "-out"))
349*2139Sjp161948 {
350*2139Sjp161948 if (args[1])
351*2139Sjp161948 {
3520Sstevel@tonic-gate args++;
3530Sstevel@tonic-gate outfile = *args;
354*2139Sjp161948 }
355*2139Sjp161948 else
356*2139Sjp161948 badarg = 1;
357*2139Sjp161948 }
358*2139Sjp161948 else if (!strcmp (*args, "-content"))
359*2139Sjp161948 {
360*2139Sjp161948 if (args[1])
361*2139Sjp161948 {
3620Sstevel@tonic-gate args++;
3630Sstevel@tonic-gate contfile = *args;
364*2139Sjp161948 }
365*2139Sjp161948 else
366*2139Sjp161948 badarg = 1;
367*2139Sjp161948 }
368*2139Sjp161948 else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
369*2139Sjp161948 continue;
370*2139Sjp161948 else
371*2139Sjp161948 badarg = 1;
3720Sstevel@tonic-gate args++;
373*2139Sjp161948 }
374*2139Sjp161948
3750Sstevel@tonic-gate
376*2139Sjp161948 if (operation == SMIME_SIGN)
377*2139Sjp161948 {
378*2139Sjp161948 if (!signerfile)
379*2139Sjp161948 {
3800Sstevel@tonic-gate BIO_printf(bio_err, "No signer certificate specified\n");
3810Sstevel@tonic-gate badarg = 1;
382*2139Sjp161948 }
3830Sstevel@tonic-gate need_rand = 1;
384*2139Sjp161948 }
385*2139Sjp161948 else if (operation == SMIME_DECRYPT)
386*2139Sjp161948 {
387*2139Sjp161948 if (!recipfile && !keyfile)
388*2139Sjp161948 {
389*2139Sjp161948 BIO_printf(bio_err, "No recipient certificate or key specified\n");
3900Sstevel@tonic-gate badarg = 1;
391*2139Sjp161948 }
3920Sstevel@tonic-gate }
393*2139Sjp161948 else if (operation == SMIME_ENCRYPT)
394*2139Sjp161948 {
395*2139Sjp161948 if (!*args)
396*2139Sjp161948 {
3970Sstevel@tonic-gate BIO_printf(bio_err, "No recipient(s) certificate(s) specified\n");
3980Sstevel@tonic-gate badarg = 1;
399*2139Sjp161948 }
4000Sstevel@tonic-gate need_rand = 1;
401*2139Sjp161948 }
402*2139Sjp161948 else if (!operation)
403*2139Sjp161948 badarg = 1;
4040Sstevel@tonic-gate
405*2139Sjp161948 if (badarg)
406*2139Sjp161948 {
4070Sstevel@tonic-gate BIO_printf (bio_err, "Usage smime [options] cert.pem ...\n");
4080Sstevel@tonic-gate BIO_printf (bio_err, "where options are\n");
4090Sstevel@tonic-gate BIO_printf (bio_err, "-encrypt encrypt message\n");
4100Sstevel@tonic-gate BIO_printf (bio_err, "-decrypt decrypt encrypted message\n");
4110Sstevel@tonic-gate BIO_printf (bio_err, "-sign sign message\n");
4120Sstevel@tonic-gate BIO_printf (bio_err, "-verify verify signed message\n");
4130Sstevel@tonic-gate BIO_printf (bio_err, "-pk7out output PKCS#7 structure\n");
4140Sstevel@tonic-gate #ifndef OPENSSL_NO_DES
4150Sstevel@tonic-gate BIO_printf (bio_err, "-des3 encrypt with triple DES\n");
4160Sstevel@tonic-gate BIO_printf (bio_err, "-des encrypt with DES\n");
4170Sstevel@tonic-gate #endif
4180Sstevel@tonic-gate #ifndef OPENSSL_NO_RC2
4190Sstevel@tonic-gate BIO_printf (bio_err, "-rc2-40 encrypt with RC2-40 (default)\n");
4200Sstevel@tonic-gate BIO_printf (bio_err, "-rc2-64 encrypt with RC2-64\n");
4210Sstevel@tonic-gate BIO_printf (bio_err, "-rc2-128 encrypt with RC2-128\n");
4220Sstevel@tonic-gate #endif
4230Sstevel@tonic-gate #ifndef OPENSSL_NO_AES
4240Sstevel@tonic-gate BIO_printf (bio_err, "-aes128, -aes192, -aes256\n");
4250Sstevel@tonic-gate BIO_printf (bio_err, " encrypt PEM output with cbc aes\n");
4260Sstevel@tonic-gate #endif
4270Sstevel@tonic-gate BIO_printf (bio_err, "-nointern don't search certificates in message for signer\n");
4280Sstevel@tonic-gate BIO_printf (bio_err, "-nosigs don't verify message signature\n");
4290Sstevel@tonic-gate BIO_printf (bio_err, "-noverify don't verify signers certificate\n");
4300Sstevel@tonic-gate BIO_printf (bio_err, "-nocerts don't include signers certificate when signing\n");
4310Sstevel@tonic-gate BIO_printf (bio_err, "-nodetach use opaque signing\n");
4320Sstevel@tonic-gate BIO_printf (bio_err, "-noattr don't include any signed attributes\n");
4330Sstevel@tonic-gate BIO_printf (bio_err, "-binary don't translate message to text\n");
4340Sstevel@tonic-gate BIO_printf (bio_err, "-certfile file other certificates file\n");
4350Sstevel@tonic-gate BIO_printf (bio_err, "-signer file signer certificate file\n");
4360Sstevel@tonic-gate BIO_printf (bio_err, "-recip file recipient certificate file for decryption\n");
4370Sstevel@tonic-gate BIO_printf (bio_err, "-in file input file\n");
4380Sstevel@tonic-gate BIO_printf (bio_err, "-inform arg input format SMIME (default), PEM or DER\n");
4390Sstevel@tonic-gate BIO_printf (bio_err, "-inkey file input private key (if not signer or recipient)\n");
4400Sstevel@tonic-gate BIO_printf (bio_err, "-keyform arg input private key format (PEM or ENGINE)\n");
4410Sstevel@tonic-gate BIO_printf (bio_err, "-out file output file\n");
4420Sstevel@tonic-gate BIO_printf (bio_err, "-outform arg output format SMIME (default), PEM or DER\n");
4430Sstevel@tonic-gate BIO_printf (bio_err, "-content file supply or override content for detached signature\n");
4440Sstevel@tonic-gate BIO_printf (bio_err, "-to addr to address\n");
4450Sstevel@tonic-gate BIO_printf (bio_err, "-from ad from address\n");
4460Sstevel@tonic-gate BIO_printf (bio_err, "-subject s subject\n");
4470Sstevel@tonic-gate BIO_printf (bio_err, "-text include or delete text MIME headers\n");
4480Sstevel@tonic-gate BIO_printf (bio_err, "-CApath dir trusted certificates directory\n");
4490Sstevel@tonic-gate BIO_printf (bio_err, "-CAfile file trusted certificates file\n");
4500Sstevel@tonic-gate BIO_printf (bio_err, "-crl_check check revocation status of signer's certificate using CRLs\n");
4510Sstevel@tonic-gate BIO_printf (bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
4520Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
4530Sstevel@tonic-gate BIO_printf (bio_err, "-engine e use engine e, possibly a hardware device.\n");
4540Sstevel@tonic-gate #endif
4550Sstevel@tonic-gate BIO_printf (bio_err, "-passin arg input file pass phrase source\n");
4560Sstevel@tonic-gate BIO_printf(bio_err, "-rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
4570Sstevel@tonic-gate BIO_printf(bio_err, " load the file (or the files in the directory) into\n");
4580Sstevel@tonic-gate BIO_printf(bio_err, " the random number generator\n");
4590Sstevel@tonic-gate BIO_printf (bio_err, "cert.pem recipient certificate(s) for encryption\n");
4600Sstevel@tonic-gate goto end;
461*2139Sjp161948 }
4620Sstevel@tonic-gate
4630Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
4640Sstevel@tonic-gate e = setup_engine(bio_err, engine, 0);
4650Sstevel@tonic-gate #endif
4660Sstevel@tonic-gate
467*2139Sjp161948 if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
468*2139Sjp161948 {
4690Sstevel@tonic-gate BIO_printf(bio_err, "Error getting password\n");
4700Sstevel@tonic-gate goto end;
471*2139Sjp161948 }
4720Sstevel@tonic-gate
473*2139Sjp161948 if (need_rand)
474*2139Sjp161948 {
4750Sstevel@tonic-gate app_RAND_load_file(NULL, bio_err, (inrand != NULL));
4760Sstevel@tonic-gate if (inrand != NULL)
4770Sstevel@tonic-gate BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
4780Sstevel@tonic-gate app_RAND_load_files(inrand));
479*2139Sjp161948 }
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate ret = 2;
4820Sstevel@tonic-gate
483*2139Sjp161948 if (operation != SMIME_SIGN)
484*2139Sjp161948 flags &= ~PKCS7_DETACHED;
4850Sstevel@tonic-gate
486*2139Sjp161948 if (operation & SMIME_OP)
487*2139Sjp161948 {
488*2139Sjp161948 if (flags & PKCS7_BINARY)
489*2139Sjp161948 inmode = "rb";
490*2139Sjp161948 if (outformat == FORMAT_ASN1)
491*2139Sjp161948 outmode = "wb";
492*2139Sjp161948 }
493*2139Sjp161948 else
494*2139Sjp161948 {
495*2139Sjp161948 if (flags & PKCS7_BINARY)
496*2139Sjp161948 outmode = "wb";
497*2139Sjp161948 if (informat == FORMAT_ASN1)
498*2139Sjp161948 inmode = "rb";
499*2139Sjp161948 }
5000Sstevel@tonic-gate
501*2139Sjp161948 if (operation == SMIME_ENCRYPT)
502*2139Sjp161948 {
503*2139Sjp161948 if (!cipher)
504*2139Sjp161948 {
5050Sstevel@tonic-gate #ifndef OPENSSL_NO_RC2
5060Sstevel@tonic-gate cipher = EVP_rc2_40_cbc();
5070Sstevel@tonic-gate #else
5080Sstevel@tonic-gate BIO_printf(bio_err, "No cipher selected\n");
5090Sstevel@tonic-gate goto end;
5100Sstevel@tonic-gate #endif
511*2139Sjp161948 }
5120Sstevel@tonic-gate encerts = sk_X509_new_null();
513*2139Sjp161948 while (*args)
514*2139Sjp161948 {
515*2139Sjp161948 if (!(cert = load_cert(bio_err,*args,FORMAT_PEM,
516*2139Sjp161948 NULL, e, "recipient certificate file")))
517*2139Sjp161948 {
5180Sstevel@tonic-gate #if 0 /* An appropriate message is already printed */
5190Sstevel@tonic-gate BIO_printf(bio_err, "Can't read recipient certificate file %s\n", *args);
5200Sstevel@tonic-gate #endif
5210Sstevel@tonic-gate goto end;
522*2139Sjp161948 }
5230Sstevel@tonic-gate sk_X509_push(encerts, cert);
5240Sstevel@tonic-gate cert = NULL;
5250Sstevel@tonic-gate args++;
526*2139Sjp161948 }
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
529*2139Sjp161948 if (signerfile && (operation == SMIME_SIGN))
530*2139Sjp161948 {
531*2139Sjp161948 if (!(signer = load_cert(bio_err,signerfile,FORMAT_PEM, NULL,
532*2139Sjp161948 e, "signer certificate")))
533*2139Sjp161948 {
5340Sstevel@tonic-gate #if 0 /* An appropri message has already been printed */
5350Sstevel@tonic-gate BIO_printf(bio_err, "Can't read signer certificate file %s\n", signerfile);
5360Sstevel@tonic-gate #endif
5370Sstevel@tonic-gate goto end;
538*2139Sjp161948 }
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate
541*2139Sjp161948 if (certfile)
542*2139Sjp161948 {
543*2139Sjp161948 if (!(other = load_certs(bio_err,certfile,FORMAT_PEM, NULL,
544*2139Sjp161948 e, "certificate file")))
545*2139Sjp161948 {
5460Sstevel@tonic-gate #if 0 /* An appropriate message has already been printed */
5470Sstevel@tonic-gate BIO_printf(bio_err, "Can't read certificate file %s\n", certfile);
5480Sstevel@tonic-gate #endif
5490Sstevel@tonic-gate ERR_print_errors(bio_err);
5500Sstevel@tonic-gate goto end;
551*2139Sjp161948 }
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
554*2139Sjp161948 if (recipfile && (operation == SMIME_DECRYPT))
555*2139Sjp161948 {
556*2139Sjp161948 if (!(recip = load_cert(bio_err,recipfile,FORMAT_PEM,NULL,
557*2139Sjp161948 e, "recipient certificate file")))
558*2139Sjp161948 {
5590Sstevel@tonic-gate #if 0 /* An appropriate message has alrady been printed */
5600Sstevel@tonic-gate BIO_printf(bio_err, "Can't read recipient certificate file %s\n", recipfile);
5610Sstevel@tonic-gate #endif
5620Sstevel@tonic-gate ERR_print_errors(bio_err);
5630Sstevel@tonic-gate goto end;
564*2139Sjp161948 }
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
567*2139Sjp161948 if (operation == SMIME_DECRYPT)
568*2139Sjp161948 {
569*2139Sjp161948 if (!keyfile)
570*2139Sjp161948 keyfile = recipfile;
571*2139Sjp161948 }
572*2139Sjp161948 else if (operation == SMIME_SIGN)
573*2139Sjp161948 {
574*2139Sjp161948 if (!keyfile)
575*2139Sjp161948 keyfile = signerfile;
576*2139Sjp161948 }
577*2139Sjp161948 else keyfile = NULL;
5780Sstevel@tonic-gate
579*2139Sjp161948 if (keyfile)
580*2139Sjp161948 {
5810Sstevel@tonic-gate key = load_key(bio_err, keyfile, keyform, 0, passin, e,
5820Sstevel@tonic-gate "signing key file");
583*2139Sjp161948 if (!key)
5840Sstevel@tonic-gate goto end;
585*2139Sjp161948 }
5860Sstevel@tonic-gate
587*2139Sjp161948 if (infile)
588*2139Sjp161948 {
589*2139Sjp161948 if (!(in = BIO_new_file(infile, inmode)))
590*2139Sjp161948 {
5910Sstevel@tonic-gate BIO_printf (bio_err,
5920Sstevel@tonic-gate "Can't open input file %s\n", infile);
5930Sstevel@tonic-gate goto end;
594*2139Sjp161948 }
5950Sstevel@tonic-gate }
596*2139Sjp161948 else
597*2139Sjp161948 in = BIO_new_fp(stdin, BIO_NOCLOSE);
5980Sstevel@tonic-gate
599*2139Sjp161948 if (outfile)
600*2139Sjp161948 {
601*2139Sjp161948 if (!(out = BIO_new_file(outfile, outmode)))
602*2139Sjp161948 {
6030Sstevel@tonic-gate BIO_printf (bio_err,
6040Sstevel@tonic-gate "Can't open output file %s\n", outfile);
6050Sstevel@tonic-gate goto end;
606*2139Sjp161948 }
6070Sstevel@tonic-gate }
608*2139Sjp161948 else
609*2139Sjp161948 {
6100Sstevel@tonic-gate out = BIO_new_fp(stdout, BIO_NOCLOSE);
6110Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
6140Sstevel@tonic-gate out = BIO_push(tmpbio, out);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate #endif
617*2139Sjp161948 }
6180Sstevel@tonic-gate
619*2139Sjp161948 if (operation == SMIME_VERIFY)
620*2139Sjp161948 {
621*2139Sjp161948 if (!(store = setup_verify(bio_err, CAfile, CApath)))
622*2139Sjp161948 goto end;
623*2139Sjp161948 X509_STORE_set_verify_cb_func(store, smime_cb);
624*2139Sjp161948 if (vpm)
625*2139Sjp161948 X509_STORE_set1_param(store, vpm);
626*2139Sjp161948 }
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate ret = 3;
6300Sstevel@tonic-gate
631*2139Sjp161948 if (operation == SMIME_ENCRYPT)
6320Sstevel@tonic-gate p7 = PKCS7_encrypt(encerts, in, cipher, flags);
633*2139Sjp161948 else if (operation == SMIME_SIGN)
634*2139Sjp161948 {
635*2139Sjp161948 /* If detached data and SMIME output enable partial
636*2139Sjp161948 * signing.
637*2139Sjp161948 */
638*2139Sjp161948 if ((flags & PKCS7_DETACHED) && (outformat == FORMAT_SMIME))
639*2139Sjp161948 flags |= PKCS7_STREAM;
6400Sstevel@tonic-gate p7 = PKCS7_sign(signer, key, other, in, flags);
641*2139Sjp161948 /* Don't need to rewind for partial signing */
642*2139Sjp161948 if (!(flags & PKCS7_STREAM) && (BIO_reset(in) != 0))
643*2139Sjp161948 {
644*2139Sjp161948 BIO_printf(bio_err, "Can't rewind input file\n");
645*2139Sjp161948 goto end;
646*2139Sjp161948 }
6470Sstevel@tonic-gate }
648*2139Sjp161948 else
649*2139Sjp161948 {
650*2139Sjp161948 if (informat == FORMAT_SMIME)
6510Sstevel@tonic-gate p7 = SMIME_read_PKCS7(in, &indata);
652*2139Sjp161948 else if (informat == FORMAT_PEM)
6530Sstevel@tonic-gate p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
654*2139Sjp161948 else if (informat == FORMAT_ASN1)
6550Sstevel@tonic-gate p7 = d2i_PKCS7_bio(in, NULL);
656*2139Sjp161948 else
657*2139Sjp161948 {
6580Sstevel@tonic-gate BIO_printf(bio_err, "Bad input format for PKCS#7 file\n");
6590Sstevel@tonic-gate goto end;
660*2139Sjp161948 }
6610Sstevel@tonic-gate
662*2139Sjp161948 if (!p7)
663*2139Sjp161948 {
6640Sstevel@tonic-gate BIO_printf(bio_err, "Error reading S/MIME message\n");
6650Sstevel@tonic-gate goto end;
666*2139Sjp161948 }
667*2139Sjp161948 if (contfile)
668*2139Sjp161948 {
6690Sstevel@tonic-gate BIO_free(indata);
670*2139Sjp161948 if (!(indata = BIO_new_file(contfile, "rb")))
671*2139Sjp161948 {
6720Sstevel@tonic-gate BIO_printf(bio_err, "Can't read content file %s\n", contfile);
6730Sstevel@tonic-gate goto end;
674*2139Sjp161948 }
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate
678*2139Sjp161948 if (!p7)
679*2139Sjp161948 {
6800Sstevel@tonic-gate BIO_printf(bio_err, "Error creating PKCS#7 structure\n");
6810Sstevel@tonic-gate goto end;
682*2139Sjp161948 }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate ret = 4;
685*2139Sjp161948 if (operation == SMIME_DECRYPT)
686*2139Sjp161948 {
687*2139Sjp161948 if (!PKCS7_decrypt(p7, key, recip, out, flags))
688*2139Sjp161948 {
6890Sstevel@tonic-gate BIO_printf(bio_err, "Error decrypting PKCS#7 structure\n");
6900Sstevel@tonic-gate goto end;
691*2139Sjp161948 }
6920Sstevel@tonic-gate }
693*2139Sjp161948 else if (operation == SMIME_VERIFY)
694*2139Sjp161948 {
6950Sstevel@tonic-gate STACK_OF(X509) *signers;
696*2139Sjp161948 if (PKCS7_verify(p7, other, store, indata, out, flags))
6970Sstevel@tonic-gate BIO_printf(bio_err, "Verification successful\n");
698*2139Sjp161948 else
699*2139Sjp161948 {
7000Sstevel@tonic-gate BIO_printf(bio_err, "Verification failure\n");
7010Sstevel@tonic-gate goto end;
702*2139Sjp161948 }
7030Sstevel@tonic-gate signers = PKCS7_get0_signers(p7, other, flags);
704*2139Sjp161948 if (!save_certs(signerfile, signers))
705*2139Sjp161948 {
7060Sstevel@tonic-gate BIO_printf(bio_err, "Error writing signers to %s\n",
7070Sstevel@tonic-gate signerfile);
7080Sstevel@tonic-gate ret = 5;
7090Sstevel@tonic-gate goto end;
710*2139Sjp161948 }
7110Sstevel@tonic-gate sk_X509_free(signers);
712*2139Sjp161948 }
713*2139Sjp161948 else if (operation == SMIME_PK7OUT)
7140Sstevel@tonic-gate PEM_write_bio_PKCS7(out, p7);
715*2139Sjp161948 else
716*2139Sjp161948 {
717*2139Sjp161948 if (to)
718*2139Sjp161948 BIO_printf(out, "To: %s\n", to);
719*2139Sjp161948 if (from)
720*2139Sjp161948 BIO_printf(out, "From: %s\n", from);
721*2139Sjp161948 if (subject)
722*2139Sjp161948 BIO_printf(out, "Subject: %s\n", subject);
723*2139Sjp161948 if (outformat == FORMAT_SMIME)
7240Sstevel@tonic-gate SMIME_write_PKCS7(out, p7, in, flags);
725*2139Sjp161948 else if (outformat == FORMAT_PEM)
7260Sstevel@tonic-gate PEM_write_bio_PKCS7(out,p7);
727*2139Sjp161948 else if (outformat == FORMAT_ASN1)
7280Sstevel@tonic-gate i2d_PKCS7_bio(out,p7);
729*2139Sjp161948 else
730*2139Sjp161948 {
7310Sstevel@tonic-gate BIO_printf(bio_err, "Bad output format for PKCS#7 file\n");
7320Sstevel@tonic-gate goto end;
733*2139Sjp161948 }
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate ret = 0;
7360Sstevel@tonic-gate end:
7370Sstevel@tonic-gate if (need_rand)
7380Sstevel@tonic-gate app_RAND_write_file(NULL, bio_err);
739*2139Sjp161948 if (ret) ERR_print_errors(bio_err);
7400Sstevel@tonic-gate sk_X509_pop_free(encerts, X509_free);
7410Sstevel@tonic-gate sk_X509_pop_free(other, X509_free);
742*2139Sjp161948 if (vpm)
743*2139Sjp161948 X509_VERIFY_PARAM_free(vpm);
7440Sstevel@tonic-gate X509_STORE_free(store);
7450Sstevel@tonic-gate X509_free(cert);
7460Sstevel@tonic-gate X509_free(recip);
7470Sstevel@tonic-gate X509_free(signer);
7480Sstevel@tonic-gate EVP_PKEY_free(key);
7490Sstevel@tonic-gate PKCS7_free(p7);
7500Sstevel@tonic-gate BIO_free(in);
7510Sstevel@tonic-gate BIO_free(indata);
7520Sstevel@tonic-gate BIO_free_all(out);
753*2139Sjp161948 if (passin) OPENSSL_free(passin);
7540Sstevel@tonic-gate return (ret);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate
save_certs(char * signerfile,STACK_OF (X509)* signers)7570Sstevel@tonic-gate static int save_certs(char *signerfile, STACK_OF(X509) *signers)
758*2139Sjp161948 {
7590Sstevel@tonic-gate int i;
7600Sstevel@tonic-gate BIO *tmp;
761*2139Sjp161948 if (!signerfile)
762*2139Sjp161948 return 1;
7630Sstevel@tonic-gate tmp = BIO_new_file(signerfile, "w");
764*2139Sjp161948 if (!tmp) return 0;
7650Sstevel@tonic-gate for(i = 0; i < sk_X509_num(signers); i++)
7660Sstevel@tonic-gate PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
7670Sstevel@tonic-gate BIO_free(tmp);
7680Sstevel@tonic-gate return 1;
769*2139Sjp161948 }
7700Sstevel@tonic-gate
771*2139Sjp161948
772*2139Sjp161948 /* Minimal callback just to output policy info (if any) */
773*2139Sjp161948
smime_cb(int ok,X509_STORE_CTX * ctx)774*2139Sjp161948 static int smime_cb(int ok, X509_STORE_CTX *ctx)
775*2139Sjp161948 {
776*2139Sjp161948 int error;
777*2139Sjp161948
778*2139Sjp161948 error = X509_STORE_CTX_get_error(ctx);
779*2139Sjp161948
780*2139Sjp161948 if ((error != X509_V_ERR_NO_EXPLICIT_POLICY)
781*2139Sjp161948 && ((error != X509_V_OK) || (ok != 2)))
782*2139Sjp161948 return ok;
783*2139Sjp161948
784*2139Sjp161948 policies_print(NULL, ctx);
785*2139Sjp161948
786*2139Sjp161948 return ok;
787*2139Sjp161948
788*2139Sjp161948 }
789