[BACK]Return to smime.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / openssl

Annotation of src/usr.bin/openssl/smime.c, Revision 1.4

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