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

Annotation of src/usr.bin/openssl/cms.c, Revision 1.15

1.15    ! inoguchi    1: /* $OpenBSD: cms.c,v 1.14 2019/11/18 11:34:41 inoguchi Exp $ */
1.1       jsing       2: /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
                      3:  * project.
                      4:  */
                      5: /* ====================================================================
                      6:  * Copyright (c) 2008 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:
                     54: /* CMS utility function */
                     55:
                     56: #include <stdio.h>
                     57: #include <string.h>
                     58:
                     59: #include "apps.h"
                     60:
                     61: #ifndef OPENSSL_NO_CMS
                     62:
                     63: #include <openssl/crypto.h>
                     64: #include <openssl/err.h>
                     65: #include <openssl/pem.h>
                     66: #include <openssl/x509_vfy.h>
                     67: #include <openssl/x509v3.h>
1.10      jsing      68:
                     69: #include <openssl/cms.h>
1.1       jsing      70:
1.12      jsing      71: static int save_certs(char *signerfile, STACK_OF(X509) *signers);
                     72: static int cms_cb(int ok, X509_STORE_CTX *ctx);
                     73: static void receipt_request_print(BIO *out, CMS_ContentInfo *cms);
                     74: static CMS_ReceiptRequest *make_receipt_request(
                     75:     STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
                     76:     STACK_OF(OPENSSL_STRING) *rr_from);
1.14      inoguchi   77: static int cms_set_pkey_param(EVP_PKEY_CTX *pctx,
                     78:     STACK_OF(OPENSSL_STRING) *param);
1.1       jsing      79:
                     80: #define SMIME_OP       0x10
                     81: #define SMIME_IP       0x20
                     82: #define SMIME_SIGNERS  0x40
                     83: #define SMIME_ENCRYPT          (1 | SMIME_OP)
                     84: #define SMIME_DECRYPT          (2 | SMIME_IP)
                     85: #define SMIME_SIGN             (3 | SMIME_OP | SMIME_SIGNERS)
                     86: #define SMIME_VERIFY           (4 | SMIME_IP)
                     87: #define SMIME_CMSOUT           (5 | SMIME_IP | SMIME_OP)
                     88: #define SMIME_RESIGN           (6 | SMIME_IP | SMIME_OP | SMIME_SIGNERS)
                     89: #define SMIME_DATAOUT          (7 | SMIME_IP)
                     90: #define SMIME_DATA_CREATE      (8 | SMIME_OP)
                     91: #define SMIME_DIGEST_VERIFY    (9 | SMIME_IP)
                     92: #define SMIME_DIGEST_CREATE    (10 | SMIME_OP)
                     93: #define SMIME_UNCOMPRESS       (11 | SMIME_IP)
                     94: #define SMIME_COMPRESS         (12 | SMIME_OP)
                     95: #define SMIME_ENCRYPTED_DECRYPT        (13 | SMIME_IP)
                     96: #define SMIME_ENCRYPTED_ENCRYPT        (14 | SMIME_OP)
                     97: #define SMIME_SIGN_RECEIPT     (15 | SMIME_IP | SMIME_OP)
                     98: #define SMIME_VERIFY_RECEIPT   (16 | SMIME_IP)
                     99:
                    100: int verify_err = 0;
                    101:
1.15    ! inoguchi  102: struct cms_key_param {
1.14      inoguchi  103:        int idx;
                    104:        STACK_OF(OPENSSL_STRING) *param;
1.15    ! inoguchi  105:        struct cms_key_param *next;
1.14      inoguchi  106: };
                    107:
1.1       jsing     108: int
                    109: cms_main(int argc, char **argv)
                    110: {
                    111:        int operation = 0;
                    112:        int ret = 0;
                    113:        char **args;
                    114:        const char *inmode = "r", *outmode = "w";
                    115:        char *infile = NULL, *outfile = NULL, *rctfile = NULL;
                    116:        char *signerfile = NULL, *recipfile = NULL;
1.12      jsing     117:        STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;
1.1       jsing     118:        char *certfile = NULL, *keyfile = NULL, *contfile = NULL;
                    119:        char *certsoutfile = NULL;
                    120:        const EVP_CIPHER *cipher = NULL;
                    121:        CMS_ContentInfo *cms = NULL, *rcms = NULL;
                    122:        X509_STORE *store = NULL;
                    123:        X509 *cert = NULL, *recip = NULL, *signer = NULL;
                    124:        EVP_PKEY *key = NULL;
1.12      jsing     125:        STACK_OF(X509) *encerts = NULL, *other = NULL;
1.1       jsing     126:        BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;
                    127:        int badarg = 0;
                    128:        int flags = CMS_DETACHED, noout = 0, print = 0;
                    129:        int verify_retcode = 0;
                    130:        int rr_print = 0, rr_allorfirst = -1;
1.12      jsing     131:        STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;
1.1       jsing     132:        CMS_ReceiptRequest *rr = NULL;
                    133:        char *to = NULL, *from = NULL, *subject = NULL;
                    134:        char *CAfile = NULL, *CApath = NULL;
                    135:        char *passargin = NULL, *passin = NULL;
                    136:        const EVP_MD *sign_md = NULL;
                    137:        int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;
                    138:        int rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;
                    139:        unsigned char *secret_key = NULL, *secret_keyid = NULL;
                    140:        unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;
                    141:        size_t secret_keylen = 0, secret_keyidlen = 0;
                    142:
1.15    ! inoguchi  143:        struct cms_key_param *key_first = NULL, *key_param = NULL;
1.14      inoguchi  144:
1.1       jsing     145:        ASN1_OBJECT *econtent_type = NULL;
                    146:
                    147:        X509_VERIFY_PARAM *vpm = NULL;
1.4       doug      148:
                    149:        if (single_execution) {
1.6       doug      150:                if (pledge("stdio rpath wpath cpath tty", NULL) == -1) {
1.4       doug      151:                        perror("pledge");
1.6       doug      152:                        exit(1);
                    153:                }
1.4       doug      154:        }
1.1       jsing     155:
                    156:        args = argv + 1;
                    157:        ret = 1;
                    158:
                    159:        while (!badarg && *args && *args[0] == '-') {
                    160:                if (!strcmp(*args, "-encrypt"))
                    161:                        operation = SMIME_ENCRYPT;
                    162:                else if (!strcmp(*args, "-decrypt"))
                    163:                        operation = SMIME_DECRYPT;
                    164:                else if (!strcmp(*args, "-sign"))
                    165:                        operation = SMIME_SIGN;
                    166:                else if (!strcmp(*args, "-sign_receipt"))
                    167:                        operation = SMIME_SIGN_RECEIPT;
                    168:                else if (!strcmp(*args, "-resign"))
                    169:                        operation = SMIME_RESIGN;
                    170:                else if (!strcmp(*args, "-verify"))
                    171:                        operation = SMIME_VERIFY;
                    172:                else if (!strcmp(*args, "-verify_retcode"))
                    173:                        verify_retcode = 1;
                    174:                else if (!strcmp(*args, "-verify_receipt")) {
                    175:                        operation = SMIME_VERIFY_RECEIPT;
                    176:                        if (!args[1])
                    177:                                goto argerr;
                    178:                        args++;
                    179:                        rctfile = *args;
                    180:                } else if (!strcmp(*args, "-cmsout"))
                    181:                        operation = SMIME_CMSOUT;
                    182:                else if (!strcmp(*args, "-data_out"))
                    183:                        operation = SMIME_DATAOUT;
                    184:                else if (!strcmp(*args, "-data_create"))
                    185:                        operation = SMIME_DATA_CREATE;
                    186:                else if (!strcmp(*args, "-digest_verify"))
                    187:                        operation = SMIME_DIGEST_VERIFY;
                    188:                else if (!strcmp(*args, "-digest_create"))
                    189:                        operation = SMIME_DIGEST_CREATE;
                    190:                else if (!strcmp(*args, "-compress"))
                    191:                        operation = SMIME_COMPRESS;
                    192:                else if (!strcmp(*args, "-uncompress"))
                    193:                        operation = SMIME_UNCOMPRESS;
                    194:                else if (!strcmp(*args, "-EncryptedData_decrypt"))
                    195:                        operation = SMIME_ENCRYPTED_DECRYPT;
                    196:                else if (!strcmp(*args, "-EncryptedData_encrypt"))
                    197:                        operation = SMIME_ENCRYPTED_ENCRYPT;
                    198: #ifndef OPENSSL_NO_DES
                    199:                else if (!strcmp(*args, "-des3"))
                    200:                        cipher = EVP_des_ede3_cbc();
                    201:                else if (!strcmp(*args, "-des"))
                    202:                        cipher = EVP_des_cbc();
                    203: #endif
                    204: #ifndef OPENSSL_NO_RC2
                    205:                else if (!strcmp(*args, "-rc2-40"))
                    206:                        cipher = EVP_rc2_40_cbc();
                    207:                else if (!strcmp(*args, "-rc2-128"))
                    208:                        cipher = EVP_rc2_cbc();
                    209:                else if (!strcmp(*args, "-rc2-64"))
                    210:                        cipher = EVP_rc2_64_cbc();
                    211: #endif
                    212: #ifndef OPENSSL_NO_AES
                    213:                else if (!strcmp(*args, "-aes128"))
                    214:                        cipher = EVP_aes_128_cbc();
                    215:                else if (!strcmp(*args, "-aes192"))
                    216:                        cipher = EVP_aes_192_cbc();
                    217:                else if (!strcmp(*args, "-aes256"))
                    218:                        cipher = EVP_aes_256_cbc();
                    219: #endif
                    220: #ifndef OPENSSL_NO_CAMELLIA
                    221:                else if (!strcmp(*args, "-camellia128"))
                    222:                        cipher = EVP_camellia_128_cbc();
                    223:                else if (!strcmp(*args, "-camellia192"))
                    224:                        cipher = EVP_camellia_192_cbc();
                    225:                else if (!strcmp(*args, "-camellia256"))
                    226:                        cipher = EVP_camellia_256_cbc();
                    227: #endif
                    228:                else if (!strcmp(*args, "-debug_decrypt"))
                    229:                        flags |= CMS_DEBUG_DECRYPT;
                    230:                else if (!strcmp(*args, "-text"))
                    231:                        flags |= CMS_TEXT;
                    232:                else if (!strcmp(*args, "-nointern"))
                    233:                        flags |= CMS_NOINTERN;
                    234:                else if (!strcmp(*args, "-noverify") ||
                    235:                    !strcmp(*args, "-no_signer_cert_verify"))
                    236:                        flags |= CMS_NO_SIGNER_CERT_VERIFY;
                    237:                else if (!strcmp(*args, "-nocerts"))
                    238:                        flags |= CMS_NOCERTS;
                    239:                else if (!strcmp(*args, "-noattr"))
                    240:                        flags |= CMS_NOATTR;
                    241:                else if (!strcmp(*args, "-nodetach"))
                    242:                        flags &= ~CMS_DETACHED;
                    243:                else if (!strcmp(*args, "-nosmimecap"))
                    244:                        flags |= CMS_NOSMIMECAP;
                    245:                else if (!strcmp(*args, "-binary"))
                    246:                        flags |= CMS_BINARY;
                    247:                else if (!strcmp(*args, "-keyid"))
                    248:                        flags |= CMS_USE_KEYID;
                    249:                else if (!strcmp(*args, "-nosigs"))
                    250:                        flags |= CMS_NOSIGS;
                    251:                else if (!strcmp(*args, "-no_content_verify"))
                    252:                        flags |= CMS_NO_CONTENT_VERIFY;
                    253:                else if (!strcmp(*args, "-no_attr_verify"))
                    254:                        flags |= CMS_NO_ATTR_VERIFY;
                    255:                else if (!strcmp(*args, "-stream"))
                    256:                        flags |= CMS_STREAM;
                    257:                else if (!strcmp(*args, "-indef"))
                    258:                        flags |= CMS_STREAM;
                    259:                else if (!strcmp(*args, "-noindef"))
                    260:                        flags &= ~CMS_STREAM;
                    261:                else if (!strcmp(*args, "-nooldmime"))
                    262:                        flags |= CMS_NOOLDMIMETYPE;
                    263:                else if (!strcmp(*args, "-crlfeol"))
                    264:                        flags |= CMS_CRLFEOL;
                    265:                else if (!strcmp(*args, "-noout"))
                    266:                        noout = 1;
                    267:                else if (!strcmp(*args, "-receipt_request_print"))
                    268:                        rr_print = 1;
                    269:                else if (!strcmp(*args, "-receipt_request_all"))
                    270:                        rr_allorfirst = 0;
                    271:                else if (!strcmp(*args, "-receipt_request_first"))
                    272:                        rr_allorfirst = 1;
                    273:                else if (!strcmp(*args, "-receipt_request_from")) {
                    274:                        if (!args[1])
                    275:                                goto argerr;
                    276:                        args++;
                    277:                        if (!rr_from)
                    278:                                rr_from = sk_OPENSSL_STRING_new_null();
1.15    ! inoguchi  279:                        if (!sk_OPENSSL_STRING_push(rr_from, *args))
        !           280:                                goto end;
1.1       jsing     281:                } else if (!strcmp(*args, "-receipt_request_to")) {
                    282:                        if (!args[1])
                    283:                                goto argerr;
                    284:                        args++;
                    285:                        if (!rr_to)
                    286:                                rr_to = sk_OPENSSL_STRING_new_null();
1.15    ! inoguchi  287:                        if (!sk_OPENSSL_STRING_push(rr_to, *args))
        !           288:                                goto end;
1.1       jsing     289:                } else if (!strcmp(*args, "-print")) {
                    290:                        noout = 1;
                    291:                        print = 1;
                    292:                } else if (!strcmp(*args, "-secretkey")) {
                    293:                        long ltmp;
                    294:                        if (!args[1])
                    295:                                goto argerr;
                    296:                        args++;
                    297:                        secret_key = string_to_hex(*args, &ltmp);
                    298:                        if (!secret_key) {
                    299:                                BIO_printf(bio_err, "Invalid key %s\n", *args);
                    300:                                goto argerr;
                    301:                        }
                    302:                        secret_keylen = (size_t) ltmp;
                    303:                } else if (!strcmp(*args, "-secretkeyid")) {
                    304:                        long ltmp;
                    305:                        if (!args[1])
                    306:                                goto argerr;
                    307:                        args++;
                    308:                        secret_keyid = string_to_hex(*args, &ltmp);
                    309:                        if (!secret_keyid) {
                    310:                                BIO_printf(bio_err, "Invalid id %s\n", *args);
                    311:                                goto argerr;
                    312:                        }
                    313:                        secret_keyidlen = (size_t) ltmp;
                    314:                } else if (!strcmp(*args, "-pwri_password")) {
                    315:                        if (!args[1])
                    316:                                goto argerr;
                    317:                        args++;
                    318:                        pwri_pass = (unsigned char *) *args;
                    319:                } else if (!strcmp(*args, "-econtent_type")) {
                    320:                        if (!args[1])
                    321:                                goto argerr;
                    322:                        args++;
                    323:                        econtent_type = OBJ_txt2obj(*args, 0);
                    324:                        if (!econtent_type) {
                    325:                                BIO_printf(bio_err, "Invalid OID %s\n", *args);
                    326:                                goto argerr;
                    327:                        }
                    328:                }
                    329:                else if (!strcmp(*args, "-passin")) {
                    330:                        if (!args[1])
                    331:                                goto argerr;
                    332:                        passargin = *++args;
                    333:                } else if (!strcmp(*args, "-to")) {
                    334:                        if (!args[1])
                    335:                                goto argerr;
                    336:                        to = *++args;
                    337:                } else if (!strcmp(*args, "-from")) {
                    338:                        if (!args[1])
                    339:                                goto argerr;
                    340:                        from = *++args;
                    341:                } else if (!strcmp(*args, "-subject")) {
                    342:                        if (!args[1])
                    343:                                goto argerr;
                    344:                        subject = *++args;
                    345:                } else if (!strcmp(*args, "-signer")) {
                    346:                        if (!args[1])
                    347:                                goto argerr;
                    348:                        /* If previous -signer argument add signer to list */
                    349:
                    350:                        if (signerfile) {
                    351:                                if (!sksigners)
                    352:                                        sksigners =
                    353:                                            sk_OPENSSL_STRING_new_null();
1.15    ! inoguchi  354:                                if (!sk_OPENSSL_STRING_push(sksigners, signerfile))
        !           355:                                        goto end;
1.1       jsing     356:                                if (!keyfile)
                    357:                                        keyfile = signerfile;
                    358:                                if (!skkeys)
                    359:                                        skkeys = sk_OPENSSL_STRING_new_null();
1.15    ! inoguchi  360:                                if (!sk_OPENSSL_STRING_push(skkeys, keyfile))
        !           361:                                        goto end;
1.1       jsing     362:                                keyfile = NULL;
                    363:                        }
                    364:                        signerfile = *++args;
                    365:                } else if (!strcmp(*args, "-recip")) {
                    366:                        if (!args[1])
                    367:                                goto argerr;
1.14      inoguchi  368:                        if (operation == SMIME_ENCRYPT) {
                    369:                                if (encerts == NULL &&
                    370:                                    (encerts = sk_X509_new_null()) == NULL)
                    371:                                        goto end;
                    372:                                cert = load_cert(bio_err, *++args, FORMAT_PEM,
                    373:                                    NULL, "recipient certificate file");
                    374:                                if (cert == NULL)
                    375:                                        goto end;
1.15    ! inoguchi  376:                                if (!sk_X509_push(encerts, cert))
        !           377:                                        goto end;
1.14      inoguchi  378:                                cert = NULL;
                    379:                        } else {
                    380:                                recipfile = *++args;
                    381:                        }
1.1       jsing     382:                } else if (!strcmp(*args, "-certsout")) {
                    383:                        if (!args[1])
                    384:                                goto argerr;
                    385:                        certsoutfile = *++args;
                    386:                } else if (!strcmp(*args, "-md")) {
                    387:                        if (!args[1])
                    388:                                goto argerr;
                    389:                        sign_md = EVP_get_digestbyname(*++args);
                    390:                        if (sign_md == NULL) {
                    391:                                BIO_printf(bio_err, "Unknown digest %s\n",
                    392:                                    *args);
                    393:                                goto argerr;
                    394:                        }
                    395:                } else if (!strcmp(*args, "-inkey")) {
                    396:                        if (!args[1])
                    397:                                goto argerr;
                    398:                        /* If previous -inkey arument add signer to list */
                    399:                        if (keyfile) {
                    400:                                if (!signerfile) {
                    401:                                        BIO_puts(bio_err,
                    402:                                            "Illegal -inkey without -signer\n");
                    403:                                        goto argerr;
                    404:                                }
                    405:                                if (!sksigners)
                    406:                                        sksigners =
                    407:                                            sk_OPENSSL_STRING_new_null();
1.15    ! inoguchi  408:                                if (!sk_OPENSSL_STRING_push(sksigners, signerfile))
        !           409:                                        goto end;
1.1       jsing     410:                                signerfile = NULL;
                    411:                                if (!skkeys)
                    412:                                        skkeys = sk_OPENSSL_STRING_new_null();
1.15    ! inoguchi  413:                                if (!sk_OPENSSL_STRING_push(skkeys, keyfile))
        !           414:                                        goto end;
1.1       jsing     415:                        }
                    416:                        keyfile = *++args;
                    417:                } else if (!strcmp(*args, "-keyform")) {
                    418:                        if (!args[1])
                    419:                                goto argerr;
                    420:                        keyform = str2fmt(*++args);
1.14      inoguchi  421:                } else if (!strcmp (*args, "-keyopt")) {
                    422:                        int keyidx = -1;
                    423:                        if (!args[1])
                    424:                                goto argerr;
                    425:                        if (operation == SMIME_ENCRYPT) {
                    426:                                if (encerts != NULL)
                    427:                                        keyidx += sk_X509_num(encerts);
                    428:                        } else {
                    429:                                if (keyfile != NULL || signerfile != NULL)
                    430:                                        keyidx++;
                    431:                                if (skkeys != NULL)
                    432:                                        keyidx += sk_OPENSSL_STRING_num(skkeys);
                    433:                        }
                    434:                        if (keyidx < 0) {
                    435:                                BIO_printf(bio_err, "No key specified\n");
                    436:                                goto argerr;
                    437:                        }
                    438:                        if (key_param == NULL || key_param->idx != keyidx) {
1.15    ! inoguchi  439:                                struct cms_key_param *nparam;
        !           440:                                if ((nparam = malloc(sizeof(struct cms_key_param))) == NULL)
1.14      inoguchi  441:                                        goto end;
                    442:                                nparam->idx = keyidx;
                    443:                                if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)
                    444:                                        goto end;
                    445:                                nparam->next = NULL;
                    446:                                if (key_first == NULL)
                    447:                                        key_first = nparam;
                    448:                                else
                    449:                                        key_param->next = nparam;
                    450:                                key_param = nparam;
                    451:                        }
1.15    ! inoguchi  452:                        if (!sk_OPENSSL_STRING_push(key_param->param, *++args))
        !           453:                                goto end;
1.1       jsing     454:                } else if (!strcmp(*args, "-rctform")) {
                    455:                        if (!args[1])
                    456:                                goto argerr;
                    457:                        rctformat = str2fmt(*++args);
                    458:                } else if (!strcmp(*args, "-certfile")) {
                    459:                        if (!args[1])
                    460:                                goto argerr;
                    461:                        certfile = *++args;
                    462:                } else if (!strcmp(*args, "-CAfile")) {
                    463:                        if (!args[1])
                    464:                                goto argerr;
                    465:                        CAfile = *++args;
                    466:                } else if (!strcmp(*args, "-CApath")) {
                    467:                        if (!args[1])
                    468:                                goto argerr;
                    469:                        CApath = *++args;
                    470:                } else if (!strcmp(*args, "-in")) {
                    471:                        if (!args[1])
                    472:                                goto argerr;
                    473:                        infile = *++args;
                    474:                } else if (!strcmp(*args, "-inform")) {
                    475:                        if (!args[1])
                    476:                                goto argerr;
                    477:                        informat = str2fmt(*++args);
                    478:                } else if (!strcmp(*args, "-outform")) {
                    479:                        if (!args[1])
                    480:                                goto argerr;
                    481:                        outformat = str2fmt(*++args);
                    482:                } else if (!strcmp(*args, "-out")) {
                    483:                        if (!args[1])
                    484:                                goto argerr;
                    485:                        outfile = *++args;
                    486:                } else if (!strcmp(*args, "-content")) {
                    487:                        if (!args[1])
                    488:                                goto argerr;
                    489:                        contfile = *++args;
                    490:                } else if (args_verify(&args, NULL, &badarg, bio_err, &vpm))
                    491:                        continue;
                    492:                else if ((cipher = EVP_get_cipherbyname(*args + 1)) == NULL)
                    493:                        badarg = 1;
                    494:                args++;
                    495:        }
                    496:
                    497:        if (((rr_allorfirst != -1) || rr_from) && !rr_to) {
                    498:                BIO_puts(bio_err, "No Signed Receipts Recipients\n");
                    499:                goto argerr;
                    500:        }
                    501:        if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {
                    502:                BIO_puts(bio_err, "Signed receipts only allowed with -sign\n");
                    503:                goto argerr;
                    504:        }
                    505:        if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {
                    506:                BIO_puts(bio_err, "Multiple signers or keys not allowed\n");
                    507:                goto argerr;
                    508:        }
                    509:        if (operation & SMIME_SIGNERS) {
                    510:                if (keyfile && !signerfile) {
                    511:                        BIO_puts(bio_err, "Illegal -inkey without -signer\n");
                    512:                        goto argerr;
                    513:                }
                    514:                /* Check to see if any final signer needs to be appended */
                    515:                if (signerfile) {
                    516:                        if (!sksigners)
                    517:                                sksigners = sk_OPENSSL_STRING_new_null();
1.15    ! inoguchi  518:                        if (!sk_OPENSSL_STRING_push(sksigners, signerfile))
        !           519:                                goto end;
1.1       jsing     520:                        if (!skkeys)
                    521:                                skkeys = sk_OPENSSL_STRING_new_null();
                    522:                        if (!keyfile)
                    523:                                keyfile = signerfile;
1.15    ! inoguchi  524:                        if (!sk_OPENSSL_STRING_push(skkeys, keyfile))
        !           525:                                goto end;
1.1       jsing     526:                }
                    527:                if (!sksigners) {
                    528:                        BIO_printf(bio_err,
                    529:                            "No signer certificate specified\n");
                    530:                        badarg = 1;
                    531:                }
                    532:                signerfile = NULL;
                    533:                keyfile = NULL;
                    534:        } else if (operation == SMIME_DECRYPT) {
                    535:                if (!recipfile && !keyfile && !secret_key && !pwri_pass) {
                    536:                        BIO_printf(bio_err,
                    537:                            "No recipient certificate or key specified\n");
                    538:                        badarg = 1;
                    539:                }
                    540:        } else if (operation == SMIME_ENCRYPT) {
1.14      inoguchi  541:                if (!*args && !secret_key && !pwri_pass && !encerts) {
1.1       jsing     542:                        BIO_printf(bio_err,
                    543:                            "No recipient(s) certificate(s) specified\n");
                    544:                        badarg = 1;
                    545:                }
                    546:        } else if (!operation)
                    547:                badarg = 1;
                    548:
                    549:        if (badarg) {
1.13      jsing     550:  argerr:
1.1       jsing     551:                BIO_printf(bio_err, "Usage cms [options] cert.pem ...\n");
                    552:                BIO_printf(bio_err, "where options are\n");
                    553:                BIO_printf(bio_err, "-encrypt       encrypt message\n");
                    554:                BIO_printf(bio_err, "-decrypt       decrypt encrypted message\n");
                    555:                BIO_printf(bio_err, "-sign          sign message\n");
                    556:                BIO_printf(bio_err, "-verify        verify signed message\n");
                    557:                BIO_printf(bio_err, "-cmsout        output CMS structure\n");
                    558: #ifndef OPENSSL_NO_DES
                    559:                BIO_printf(bio_err, "-des3          encrypt with triple DES\n");
                    560:                BIO_printf(bio_err, "-des           encrypt with DES\n");
                    561: #endif
                    562: #ifndef OPENSSL_NO_RC2
                    563:                BIO_printf(bio_err, "-rc2-40        encrypt with RC2-40 (default)\n");
                    564:                BIO_printf(bio_err, "-rc2-64        encrypt with RC2-64\n");
                    565:                BIO_printf(bio_err, "-rc2-128       encrypt with RC2-128\n");
                    566: #endif
                    567: #ifndef OPENSSL_NO_AES
                    568:                BIO_printf(bio_err, "-aes128, -aes192, -aes256\n");
                    569:                BIO_printf(bio_err, "               encrypt PEM output with cbc aes\n");
                    570: #endif
                    571: #ifndef OPENSSL_NO_CAMELLIA
                    572:                BIO_printf(bio_err, "-camellia128, -camellia192, -camellia256\n");
                    573:                BIO_printf(bio_err, "               encrypt PEM output with cbc camellia\n");
                    574: #endif
                    575:                BIO_printf(bio_err, "-nointern      don't search certificates in message for signer\n");
                    576:                BIO_printf(bio_err, "-nosigs        don't verify message signature\n");
                    577:                BIO_printf(bio_err, "-noverify      don't verify signers certificate\n");
                    578:                BIO_printf(bio_err, "-nocerts       don't include signers certificate when signing\n");
                    579:                BIO_printf(bio_err, "-nodetach      use opaque signing\n");
                    580:                BIO_printf(bio_err, "-noattr        don't include any signed attributes\n");
                    581:                BIO_printf(bio_err, "-binary        don't translate message to text\n");
                    582:                BIO_printf(bio_err, "-certfile file other certificates file\n");
                    583:                BIO_printf(bio_err, "-certsout file certificate output file\n");
                    584:                BIO_printf(bio_err, "-signer file   signer certificate file\n");
                    585:                BIO_printf(bio_err, "-recip  file   recipient certificate file for decryption\n");
                    586:                BIO_printf(bio_err, "-keyid         use subject key identifier\n");
                    587:                BIO_printf(bio_err, "-in file       input file\n");
                    588:                BIO_printf(bio_err, "-inform arg    input format SMIME (default), PEM or DER\n");
                    589:                BIO_printf(bio_err, "-inkey file    input private key (if not signer or recipient)\n");
1.3       bcook     590:                BIO_printf(bio_err, "-keyform arg   input private key format (PEM)\n");
1.15    ! inoguchi  591:                BIO_printf(bio_err, "-keyopt nm:v   set public key parameters\n");
1.1       jsing     592:                BIO_printf(bio_err, "-out file      output file\n");
                    593:                BIO_printf(bio_err, "-outform arg   output format SMIME (default), PEM or DER\n");
                    594:                BIO_printf(bio_err, "-content file  supply or override content for detached signature\n");
                    595:                BIO_printf(bio_err, "-to addr       to address\n");
                    596:                BIO_printf(bio_err, "-from ad       from address\n");
                    597:                BIO_printf(bio_err, "-subject s     subject\n");
                    598:                BIO_printf(bio_err, "-text          include or delete text MIME headers\n");
                    599:                BIO_printf(bio_err, "-CApath dir    trusted certificates directory\n");
                    600:                BIO_printf(bio_err, "-CAfile file   trusted certificates file\n");
                    601:                BIO_printf(bio_err, "-crl_check     check revocation status of signer's certificate using CRLs\n");
                    602:                BIO_printf(bio_err, "-crl_check_all check revocation status of signer's certificate chain using CRLs\n");
                    603:                BIO_printf(bio_err, "-passin arg    input file pass phrase source\n");
                    604:                BIO_printf(bio_err, "cert.pem       recipient certificate(s) for encryption\n");
                    605:                goto end;
                    606:        }
                    607:
                    608:        if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
                    609:                BIO_printf(bio_err, "Error getting password\n");
                    610:                goto end;
                    611:        }
                    612:        ret = 2;
                    613:
                    614:        if (!(operation & SMIME_SIGNERS))
                    615:                flags &= ~CMS_DETACHED;
                    616:
                    617:        if (operation & SMIME_OP) {
                    618:                if (outformat == FORMAT_ASN1)
                    619:                        outmode = "wb";
                    620:        } else {
                    621:                if (flags & CMS_BINARY)
                    622:                        outmode = "wb";
                    623:        }
                    624:
                    625:        if (operation & SMIME_IP) {
                    626:                if (informat == FORMAT_ASN1)
                    627:                        inmode = "rb";
                    628:        } else {
                    629:                if (flags & CMS_BINARY)
                    630:                        inmode = "rb";
                    631:        }
                    632:
                    633:        if (operation == SMIME_ENCRYPT) {
                    634:                if (!cipher) {
                    635: #ifndef OPENSSL_NO_DES
                    636:                        cipher = EVP_des_ede3_cbc();
                    637: #else
                    638:                        BIO_printf(bio_err, "No cipher selected\n");
                    639:                        goto end;
                    640: #endif
                    641:                }
                    642:                if (secret_key && !secret_keyid) {
                    643:                        BIO_printf(bio_err, "No secret key id\n");
                    644:                        goto end;
                    645:                }
1.14      inoguchi  646:                if (*args && !encerts)
1.1       jsing     647:                        encerts = sk_X509_new_null();
                    648:                while (*args) {
                    649:                        if (!(cert = load_cert(bio_err, *args, FORMAT_PEM,
1.9       jsing     650:                            NULL, "recipient certificate file")))
1.1       jsing     651:                                goto end;
1.15    ! inoguchi  652:                        if (!sk_X509_push(encerts, cert))
        !           653:                                goto end;
1.1       jsing     654:                        cert = NULL;
                    655:                        args++;
                    656:                }
                    657:        }
                    658:        if (certfile) {
                    659:                if (!(other = load_certs(bio_err, certfile, FORMAT_PEM, NULL,
1.9       jsing     660:                    "certificate file"))) {
1.1       jsing     661:                        ERR_print_errors(bio_err);
                    662:                        goto end;
                    663:                }
                    664:        }
                    665:        if (recipfile && (operation == SMIME_DECRYPT)) {
                    666:                if (!(recip = load_cert(bio_err, recipfile, FORMAT_PEM, NULL,
1.9       jsing     667:                    "recipient certificate file"))) {
1.1       jsing     668:                        ERR_print_errors(bio_err);
                    669:                        goto end;
                    670:                }
                    671:        }
                    672:        if (operation == SMIME_SIGN_RECEIPT) {
                    673:                if (!(signer = load_cert(bio_err, signerfile, FORMAT_PEM, NULL,
1.9       jsing     674:                    "receipt signer certificate file"))) {
1.1       jsing     675:                        ERR_print_errors(bio_err);
                    676:                        goto end;
                    677:                }
                    678:        }
                    679:        if (operation == SMIME_DECRYPT) {
                    680:                if (!keyfile)
                    681:                        keyfile = recipfile;
                    682:        } else if ((operation == SMIME_SIGN) ||
                    683:            (operation == SMIME_SIGN_RECEIPT)) {
                    684:                if (!keyfile)
                    685:                        keyfile = signerfile;
                    686:        } else
                    687:                keyfile = NULL;
                    688:
                    689:        if (keyfile) {
1.9       jsing     690:                key = load_key(bio_err, keyfile, keyform, 0, passin,
1.1       jsing     691:                    "signing key file");
                    692:                if (!key)
                    693:                        goto end;
                    694:        }
                    695:        if (infile) {
                    696:                if (!(in = BIO_new_file(infile, inmode))) {
                    697:                        BIO_printf(bio_err,
                    698:                            "Can't open input file %s\n", infile);
                    699:                        goto end;
                    700:                }
                    701:        } else
                    702:                in = BIO_new_fp(stdin, BIO_NOCLOSE);
                    703:
                    704:        if (operation & SMIME_IP) {
                    705:                if (informat == FORMAT_SMIME)
                    706:                        cms = SMIME_read_CMS(in, &indata);
                    707:                else if (informat == FORMAT_PEM)
                    708:                        cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);
                    709:                else if (informat == FORMAT_ASN1)
                    710:                        cms = d2i_CMS_bio(in, NULL);
                    711:                else {
                    712:                        BIO_printf(bio_err, "Bad input format for CMS file\n");
                    713:                        goto end;
                    714:                }
                    715:
                    716:                if (!cms) {
                    717:                        BIO_printf(bio_err, "Error reading S/MIME message\n");
                    718:                        goto end;
                    719:                }
                    720:                if (contfile) {
                    721:                        BIO_free(indata);
                    722:                        if (!(indata = BIO_new_file(contfile, "rb"))) {
                    723:                                BIO_printf(bio_err,
                    724:                                    "Can't read content file %s\n", contfile);
                    725:                                goto end;
                    726:                        }
                    727:                }
                    728:                if (certsoutfile) {
1.12      jsing     729:                        STACK_OF(X509) *allcerts;
1.1       jsing     730:                        allcerts = CMS_get1_certs(cms);
                    731:                        if (!save_certs(certsoutfile, allcerts)) {
                    732:                                BIO_printf(bio_err,
                    733:                                    "Error writing certs to %s\n",
                    734:                                    certsoutfile);
                    735:                                ret = 5;
                    736:                                goto end;
                    737:                        }
                    738:                        sk_X509_pop_free(allcerts, X509_free);
                    739:                }
                    740:        }
                    741:        if (rctfile) {
                    742:                char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";
                    743:                if (!(rctin = BIO_new_file(rctfile, rctmode))) {
                    744:                        BIO_printf(bio_err,
                    745:                            "Can't open receipt file %s\n", rctfile);
                    746:                        goto end;
                    747:                }
                    748:                if (rctformat == FORMAT_SMIME)
                    749:                        rcms = SMIME_read_CMS(rctin, NULL);
                    750:                else if (rctformat == FORMAT_PEM)
                    751:                        rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);
                    752:                else if (rctformat == FORMAT_ASN1)
                    753:                        rcms = d2i_CMS_bio(rctin, NULL);
                    754:                else {
                    755:                        BIO_printf(bio_err, "Bad input format for receipt\n");
                    756:                        goto end;
                    757:                }
                    758:
                    759:                if (!rcms) {
                    760:                        BIO_printf(bio_err, "Error reading receipt\n");
                    761:                        goto end;
                    762:                }
                    763:        }
                    764:        if (outfile) {
                    765:                if (!(out = BIO_new_file(outfile, outmode))) {
                    766:                        BIO_printf(bio_err,
                    767:                            "Can't open output file %s\n", outfile);
                    768:                        goto end;
                    769:                }
                    770:        } else {
                    771:                out = BIO_new_fp(stdout, BIO_NOCLOSE);
                    772:        }
                    773:
                    774:        if ((operation == SMIME_VERIFY) ||
                    775:            (operation == SMIME_VERIFY_RECEIPT)) {
                    776:                if (!(store = setup_verify(bio_err, CAfile, CApath)))
                    777:                        goto end;
                    778:                X509_STORE_set_verify_cb(store, cms_cb);
                    779:                if (vpm)
                    780:                        X509_STORE_set1_param(store, vpm);
                    781:        }
                    782:        ret = 3;
                    783:
                    784:        if (operation == SMIME_DATA_CREATE) {
                    785:                cms = CMS_data_create(in, flags);
                    786:        } else if (operation == SMIME_DIGEST_CREATE) {
                    787:                cms = CMS_digest_create(in, sign_md, flags);
                    788:        } else if (operation == SMIME_COMPRESS) {
                    789:                cms = CMS_compress(in, -1, flags);
                    790:        } else if (operation == SMIME_ENCRYPT) {
1.14      inoguchi  791:                int i;
1.1       jsing     792:                flags |= CMS_PARTIAL;
1.14      inoguchi  793:                cms = CMS_encrypt(NULL, in, cipher, flags);
                    794:                if (cms == NULL)
1.1       jsing     795:                        goto end;
1.14      inoguchi  796:                for (i = 0; i < sk_X509_num(encerts); i++) {
                    797:                        CMS_RecipientInfo *ri;
1.15    ! inoguchi  798:                        struct cms_key_param *kparam;
1.14      inoguchi  799:                        int tflags = flags;
                    800:                        X509 *x = sk_X509_value(encerts, i);
                    801:                        for (kparam = key_first; kparam; kparam = kparam->next) {
                    802:                                if (kparam->idx == i) {
                    803:                                        tflags |= CMS_KEY_PARAM;
                    804:                                        break;
                    805:                                }
                    806:                        }
                    807:                        ri = CMS_add1_recipient_cert(cms, x, tflags);
                    808:                        if (ri == NULL)
                    809:                                goto end;
                    810:                        if (kparam != NULL) {
                    811:                                EVP_PKEY_CTX *pctx;
                    812:                                pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
                    813:                                if (!cms_set_pkey_param(pctx, kparam->param))
                    814:                                        goto end;
                    815:                        }
                    816:                }
                    817:
1.1       jsing     818:                if (secret_key) {
                    819:                        if (!CMS_add0_recipient_key(cms, NID_undef, secret_key,
                    820:                            secret_keylen, secret_keyid, secret_keyidlen,
                    821:                            NULL, NULL, NULL))
                    822:                                goto end;
                    823:                        /* NULL these because call absorbs them */
                    824:                        secret_key = NULL;
                    825:                        secret_keyid = NULL;
                    826:                }
                    827:                if (pwri_pass) {
                    828:                        pwri_tmp = strdup(pwri_pass);
                    829:                        if (!pwri_tmp)
                    830:                                goto end;
                    831:                        if (!CMS_add0_recipient_password(cms, -1, NID_undef,
                    832:                            NID_undef, pwri_tmp, -1, NULL))
                    833:                                goto end;
                    834:                        pwri_tmp = NULL;
                    835:                }
                    836:                if (!(flags & CMS_STREAM)) {
                    837:                        if (!CMS_final(cms, in, NULL, flags))
                    838:                                goto end;
                    839:                }
                    840:        } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {
                    841:                cms = CMS_EncryptedData_encrypt(in, cipher, secret_key,
                    842:                    secret_keylen, flags);
                    843:
                    844:        } else if (operation == SMIME_SIGN_RECEIPT) {
                    845:                CMS_ContentInfo *srcms = NULL;
1.12      jsing     846:                STACK_OF(CMS_SignerInfo) *sis;
1.1       jsing     847:                CMS_SignerInfo *si;
                    848:                sis = CMS_get0_SignerInfos(cms);
                    849:                if (!sis)
                    850:                        goto end;
                    851:                si = sk_CMS_SignerInfo_value(sis, 0);
                    852:                srcms = CMS_sign_receipt(si, signer, key, other, flags);
                    853:                if (!srcms)
                    854:                        goto end;
                    855:                CMS_ContentInfo_free(cms);
                    856:                cms = srcms;
                    857:        } else if (operation & SMIME_SIGNERS) {
                    858:                int i;
                    859:                /*
                    860:                 * If detached data content we enable streaming if S/MIME
                    861:                 * output format.
                    862:                 */
                    863:                if (operation == SMIME_SIGN) {
                    864:
                    865:                        if (flags & CMS_DETACHED) {
                    866:                                if (outformat == FORMAT_SMIME)
                    867:                                        flags |= CMS_STREAM;
                    868:                        }
                    869:                        flags |= CMS_PARTIAL;
                    870:                        cms = CMS_sign(NULL, NULL, other, in, flags);
                    871:                        if (!cms)
                    872:                                goto end;
                    873:                        if (econtent_type)
                    874:                                CMS_set1_eContentType(cms, econtent_type);
                    875:
                    876:                        if (rr_to) {
                    877:                                rr = make_receipt_request(rr_to, rr_allorfirst,
                    878:                                    rr_from);
                    879:                                if (!rr) {
                    880:                                        BIO_puts(bio_err,
                    881:                                            "Signed Receipt Request Creation Error\n");
                    882:                                        goto end;
                    883:                                }
                    884:                        }
                    885:                } else
                    886:                        flags |= CMS_REUSE_DIGEST;
                    887:                for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {
                    888:                        CMS_SignerInfo *si;
1.15    ! inoguchi  889:                        struct cms_key_param *kparam;
1.14      inoguchi  890:                        int tflags = flags;
1.1       jsing     891:                        signerfile = sk_OPENSSL_STRING_value(sksigners, i);
                    892:                        keyfile = sk_OPENSSL_STRING_value(skkeys, i);
1.14      inoguchi  893:
1.1       jsing     894:                        signer = load_cert(bio_err, signerfile, FORMAT_PEM,
1.9       jsing     895:                            NULL, "signer certificate");
1.1       jsing     896:                        if (!signer)
                    897:                                goto end;
1.9       jsing     898:                        key = load_key(bio_err, keyfile, keyform, 0, passin,
1.1       jsing     899:                            "signing key file");
                    900:                        if (!key)
                    901:                                goto end;
1.14      inoguchi  902:                        for (kparam = key_first; kparam; kparam = kparam->next) {
                    903:                                if (kparam->idx == i) {
                    904:                                        tflags |= CMS_KEY_PARAM;
                    905:                                        break;
                    906:                                }
                    907:                        }
                    908:                        si = CMS_add1_signer(cms, signer, key, sign_md, tflags);
                    909:                        if (si == NULL)
1.1       jsing     910:                                goto end;
1.14      inoguchi  911:                        if (kparam != NULL) {
                    912:                                EVP_PKEY_CTX *pctx;
                    913:                                pctx = CMS_SignerInfo_get0_pkey_ctx(si);
                    914:                                if (!cms_set_pkey_param(pctx, kparam->param))
                    915:                                        goto end;
                    916:                        }
1.1       jsing     917:                        if (rr && !CMS_add1_ReceiptRequest(si, rr))
                    918:                                goto end;
                    919:                        X509_free(signer);
                    920:                        signer = NULL;
                    921:                        EVP_PKEY_free(key);
                    922:                        key = NULL;
                    923:                }
                    924:                /* If not streaming or resigning finalize structure */
                    925:                if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {
                    926:                        if (!CMS_final(cms, in, NULL, flags))
                    927:                                goto end;
                    928:                }
                    929:        }
                    930:        if (!cms) {
                    931:                BIO_printf(bio_err, "Error creating CMS structure\n");
                    932:                goto end;
                    933:        }
                    934:        ret = 4;
                    935:        if (operation == SMIME_DECRYPT) {
                    936:                if (flags & CMS_DEBUG_DECRYPT)
                    937:                        CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);
                    938:
                    939:                if (secret_key) {
                    940:                        if (!CMS_decrypt_set1_key(cms, secret_key,
                    941:                            secret_keylen, secret_keyid, secret_keyidlen)) {
                    942:                                BIO_puts(bio_err,
                    943:                                    "Error decrypting CMS using secret key\n");
                    944:                                goto end;
                    945:                        }
                    946:                }
                    947:                if (key) {
                    948:                        if (!CMS_decrypt_set1_pkey(cms, key, recip)) {
                    949:                                BIO_puts(bio_err,
                    950:                                    "Error decrypting CMS using private key\n");
                    951:                                goto end;
                    952:                        }
                    953:                }
                    954:                if (pwri_pass) {
                    955:                        if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {
                    956:                                BIO_puts(bio_err,
                    957:                                    "Error decrypting CMS using password\n");
                    958:                                goto end;
                    959:                        }
                    960:                }
                    961:                if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {
                    962:                        BIO_printf(bio_err, "Error decrypting CMS structure\n");
                    963:                        goto end;
                    964:                }
                    965:        } else if (operation == SMIME_DATAOUT) {
                    966:                if (!CMS_data(cms, out, flags))
                    967:                        goto end;
                    968:        } else if (operation == SMIME_UNCOMPRESS) {
                    969:                if (!CMS_uncompress(cms, indata, out, flags))
                    970:                        goto end;
                    971:        } else if (operation == SMIME_DIGEST_VERIFY) {
                    972:                if (CMS_digest_verify(cms, indata, out, flags) > 0)
                    973:                        BIO_printf(bio_err, "Verification successful\n");
                    974:                else {
                    975:                        BIO_printf(bio_err, "Verification failure\n");
                    976:                        goto end;
                    977:                }
                    978:        } else if (operation == SMIME_ENCRYPTED_DECRYPT) {
                    979:                if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,
                    980:                    indata, out, flags))
                    981:                        goto end;
                    982:        } else if (operation == SMIME_VERIFY) {
                    983:                if (CMS_verify(cms, other, store, indata, out, flags) > 0)
                    984:                        BIO_printf(bio_err, "Verification successful\n");
                    985:                else {
                    986:                        BIO_printf(bio_err, "Verification failure\n");
                    987:                        if (verify_retcode)
                    988:                                ret = verify_err + 32;
                    989:                        goto end;
                    990:                }
                    991:                if (signerfile) {
1.12      jsing     992:                        STACK_OF(X509) *signers;
1.1       jsing     993:                        signers = CMS_get0_signers(cms);
                    994:                        if (!save_certs(signerfile, signers)) {
                    995:                                BIO_printf(bio_err,
                    996:                                    "Error writing signers to %s\n",
                    997:                                    signerfile);
                    998:                                ret = 5;
                    999:                                goto end;
                   1000:                        }
                   1001:                        sk_X509_free(signers);
                   1002:                }
                   1003:                if (rr_print)
                   1004:                        receipt_request_print(bio_err, cms);
                   1005:
                   1006:        } else if (operation == SMIME_VERIFY_RECEIPT) {
                   1007:                if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0)
                   1008:                        BIO_printf(bio_err, "Verification successful\n");
                   1009:                else {
                   1010:                        BIO_printf(bio_err, "Verification failure\n");
                   1011:                        goto end;
                   1012:                }
                   1013:        } else {
                   1014:                if (noout) {
                   1015:                        if (print)
                   1016:                                CMS_ContentInfo_print_ctx(out, cms, 0, NULL);
                   1017:                } else if (outformat == FORMAT_SMIME) {
                   1018:                        if (to)
                   1019:                                BIO_printf(out, "To: %s\n", to);
                   1020:                        if (from)
                   1021:                                BIO_printf(out, "From: %s\n", from);
                   1022:                        if (subject)
                   1023:                                BIO_printf(out, "Subject: %s\n", subject);
                   1024:                        if (operation == SMIME_RESIGN)
                   1025:                                ret = SMIME_write_CMS(out, cms, indata, flags);
                   1026:                        else
                   1027:                                ret = SMIME_write_CMS(out, cms, in, flags);
                   1028:                } else if (outformat == FORMAT_PEM)
                   1029:                        ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
                   1030:                else if (outformat == FORMAT_ASN1)
                   1031:                        ret = i2d_CMS_bio_stream(out, cms, in, flags);
                   1032:                else {
                   1033:                        BIO_printf(bio_err, "Bad output format for CMS file\n");
                   1034:                        goto end;
                   1035:                }
                   1036:                if (ret <= 0) {
                   1037:                        ret = 6;
                   1038:                        goto end;
                   1039:                }
                   1040:        }
                   1041:        ret = 0;
                   1042:
1.13      jsing    1043:  end:
1.1       jsing    1044:        if (ret)
                   1045:                ERR_print_errors(bio_err);
1.11      jsing    1046:
1.1       jsing    1047:        sk_X509_pop_free(encerts, X509_free);
                   1048:        sk_X509_pop_free(other, X509_free);
1.11      jsing    1049:        X509_VERIFY_PARAM_free(vpm);
                   1050:        sk_OPENSSL_STRING_free(sksigners);
                   1051:        sk_OPENSSL_STRING_free(skkeys);
1.1       jsing    1052:        free(secret_key);
                   1053:        free(secret_keyid);
                   1054:        free(pwri_tmp);
1.11      jsing    1055:        ASN1_OBJECT_free(econtent_type);
                   1056:        CMS_ReceiptRequest_free(rr);
                   1057:        sk_OPENSSL_STRING_free(rr_to);
                   1058:        sk_OPENSSL_STRING_free(rr_from);
1.14      inoguchi 1059:        for (key_param = key_first; key_param;) {
1.15    ! inoguchi 1060:                struct cms_key_param *tparam;
1.14      inoguchi 1061:                sk_OPENSSL_STRING_free(key_param->param);
                   1062:                tparam = key_param->next;
                   1063:                free(key_param);
                   1064:                key_param = tparam;
                   1065:        }
1.1       jsing    1066:        X509_STORE_free(store);
                   1067:        X509_free(cert);
                   1068:        X509_free(recip);
                   1069:        X509_free(signer);
                   1070:        EVP_PKEY_free(key);
                   1071:        CMS_ContentInfo_free(cms);
                   1072:        CMS_ContentInfo_free(rcms);
                   1073:        BIO_free(rctin);
                   1074:        BIO_free(in);
                   1075:        BIO_free(indata);
                   1076:        BIO_free_all(out);
                   1077:        free(passin);
1.11      jsing    1078:
1.1       jsing    1079:        return (ret);
                   1080: }
                   1081:
                   1082: static int
1.12      jsing    1083: save_certs(char *signerfile, STACK_OF(X509) *signers)
1.1       jsing    1084: {
                   1085:        int i;
                   1086:        BIO *tmp;
                   1087:
                   1088:        if (!signerfile)
                   1089:                return 1;
                   1090:        tmp = BIO_new_file(signerfile, "w");
                   1091:        if (!tmp)
                   1092:                return 0;
                   1093:        for (i = 0; i < sk_X509_num(signers); i++)
                   1094:                PEM_write_bio_X509(tmp, sk_X509_value(signers, i));
                   1095:        BIO_free(tmp);
                   1096:        return 1;
                   1097: }
                   1098:
                   1099: /* Minimal callback just to output policy info (if any) */
                   1100:
                   1101: static int
1.12      jsing    1102: cms_cb(int ok, X509_STORE_CTX *ctx)
1.1       jsing    1103: {
                   1104:        int error;
                   1105:
                   1106:        error = X509_STORE_CTX_get_error(ctx);
                   1107:
                   1108:        verify_err = error;
                   1109:
                   1110:        if ((error != X509_V_ERR_NO_EXPLICIT_POLICY) &&
                   1111:            ((error != X509_V_OK) || (ok != 2)))
                   1112:                return ok;
                   1113:
                   1114:        policies_print(NULL, ctx);
                   1115:
                   1116:        return ok;
                   1117: }
                   1118:
                   1119: static void
1.12      jsing    1120: gnames_stack_print(BIO *out, STACK_OF(GENERAL_NAMES) *gns)
1.1       jsing    1121: {
1.12      jsing    1122:        STACK_OF(GENERAL_NAME) *gens;
1.1       jsing    1123:        GENERAL_NAME *gen;
                   1124:        int i, j;
                   1125:
                   1126:        for (i = 0; i < sk_GENERAL_NAMES_num(gns); i++) {
                   1127:                gens = sk_GENERAL_NAMES_value(gns, i);
                   1128:                for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
                   1129:                        gen = sk_GENERAL_NAME_value(gens, j);
                   1130:                        BIO_puts(out, "    ");
                   1131:                        GENERAL_NAME_print(out, gen);
                   1132:                        BIO_puts(out, "\n");
                   1133:                }
                   1134:        }
                   1135:        return;
                   1136: }
                   1137:
                   1138: static void
1.12      jsing    1139: receipt_request_print(BIO *out, CMS_ContentInfo *cms)
1.1       jsing    1140: {
1.12      jsing    1141:        STACK_OF(CMS_SignerInfo) *sis;
1.1       jsing    1142:        CMS_SignerInfo *si;
                   1143:        CMS_ReceiptRequest *rr;
                   1144:        int allorfirst;
1.12      jsing    1145:        STACK_OF(GENERAL_NAMES) *rto, *rlist;
1.1       jsing    1146:        ASN1_STRING *scid;
                   1147:        int i, rv;
                   1148:
                   1149:        sis = CMS_get0_SignerInfos(cms);
                   1150:        for (i = 0; i < sk_CMS_SignerInfo_num(sis); i++) {
                   1151:                si = sk_CMS_SignerInfo_value(sis, i);
                   1152:                rv = CMS_get1_ReceiptRequest(si, &rr);
                   1153:                BIO_printf(bio_err, "Signer %d:\n", i + 1);
                   1154:                if (rv == 0)
                   1155:                        BIO_puts(bio_err, "  No Receipt Request\n");
                   1156:                else if (rv < 0) {
                   1157:                        BIO_puts(bio_err, "  Receipt Request Parse Error\n");
                   1158:                        ERR_print_errors(bio_err);
                   1159:                } else {
                   1160:                        char *id;
                   1161:                        int idlen;
                   1162:                        CMS_ReceiptRequest_get0_values(rr, &scid, &allorfirst,
                   1163:                            &rlist, &rto);
                   1164:                        BIO_puts(out, "  Signed Content ID:\n");
                   1165:                        idlen = ASN1_STRING_length(scid);
                   1166:                        id = (char *) ASN1_STRING_data(scid);
                   1167:                        BIO_dump_indent(out, id, idlen, 4);
                   1168:                        BIO_puts(out, "  Receipts From");
                   1169:                        if (rlist) {
                   1170:                                BIO_puts(out, " List:\n");
                   1171:                                gnames_stack_print(out, rlist);
                   1172:                        } else if (allorfirst == 1)
                   1173:                                BIO_puts(out, ": First Tier\n");
                   1174:                        else if (allorfirst == 0)
                   1175:                                BIO_puts(out, ": All\n");
                   1176:                        else
                   1177:                                BIO_printf(out, " Unknown (%d)\n", allorfirst);
                   1178:                        BIO_puts(out, "  Receipts To:\n");
                   1179:                        gnames_stack_print(out, rto);
                   1180:                }
                   1181:                if (rr)
                   1182:                        CMS_ReceiptRequest_free(rr);
                   1183:        }
                   1184: }
                   1185:
                   1186: static STACK_OF(GENERAL_NAMES) *
1.12      jsing    1187: make_names_stack(STACK_OF(OPENSSL_STRING) *ns)
1.1       jsing    1188: {
                   1189:        int i;
1.12      jsing    1190:        STACK_OF(GENERAL_NAMES) *ret;
1.1       jsing    1191:        GENERAL_NAMES *gens = NULL;
                   1192:        GENERAL_NAME *gen = NULL;
                   1193:        ret = sk_GENERAL_NAMES_new_null();
                   1194:        if (!ret)
                   1195:                goto err;
                   1196:        for (i = 0; i < sk_OPENSSL_STRING_num(ns); i++) {
                   1197:                char *str = sk_OPENSSL_STRING_value(ns, i);
                   1198:                gen = a2i_GENERAL_NAME(NULL, NULL, NULL, GEN_EMAIL, str, 0);
                   1199:                if (!gen)
                   1200:                        goto err;
                   1201:                gens = GENERAL_NAMES_new();
                   1202:                if (!gens)
                   1203:                        goto err;
                   1204:                if (!sk_GENERAL_NAME_push(gens, gen))
                   1205:                        goto err;
                   1206:                gen = NULL;
                   1207:                if (!sk_GENERAL_NAMES_push(ret, gens))
                   1208:                        goto err;
                   1209:                gens = NULL;
                   1210:        }
                   1211:
                   1212:        return ret;
                   1213:
1.13      jsing    1214:  err:
1.11      jsing    1215:        sk_GENERAL_NAMES_pop_free(ret, GENERAL_NAMES_free);
                   1216:        GENERAL_NAMES_free(gens);
                   1217:        GENERAL_NAME_free(gen);
                   1218:
1.1       jsing    1219:        return NULL;
                   1220: }
                   1221:
                   1222:
                   1223: static CMS_ReceiptRequest *
1.12      jsing    1224: make_receipt_request(STACK_OF(OPENSSL_STRING) *rr_to, int rr_allorfirst,
                   1225:     STACK_OF(OPENSSL_STRING) *rr_from)
1.1       jsing    1226: {
1.12      jsing    1227:        STACK_OF(GENERAL_NAMES) *rct_to, *rct_from;
1.1       jsing    1228:        CMS_ReceiptRequest *rr;
                   1229:
                   1230:        rct_to = make_names_stack(rr_to);
                   1231:        if (!rct_to)
                   1232:                goto err;
                   1233:        if (rr_from) {
                   1234:                rct_from = make_names_stack(rr_from);
                   1235:                if (!rct_from)
                   1236:                        goto err;
                   1237:        } else
                   1238:                rct_from = NULL;
                   1239:        rr = CMS_ReceiptRequest_create0(NULL, -1, rr_allorfirst, rct_from,
                   1240:            rct_to);
                   1241:        return rr;
                   1242:
1.13      jsing    1243:  err:
1.1       jsing    1244:        return NULL;
1.14      inoguchi 1245: }
                   1246:
                   1247: static int
                   1248: cms_set_pkey_param(EVP_PKEY_CTX *pctx, STACK_OF(OPENSSL_STRING) *param)
                   1249: {
                   1250:        char *keyopt;
                   1251:        int i;
1.15    ! inoguchi 1252:
1.14      inoguchi 1253:        if (sk_OPENSSL_STRING_num(param) <= 0)
                   1254:                return 1;
                   1255:        for (i = 0; i < sk_OPENSSL_STRING_num(param); i++) {
                   1256:                keyopt = sk_OPENSSL_STRING_value(param, i);
                   1257:                if (pkey_ctrl_string(pctx, keyopt) <= 0) {
                   1258:                        BIO_printf(bio_err, "parameter error \"%s\"\n", keyopt);
                   1259:                        ERR_print_errors(bio_err);
                   1260:                        return 0;
                   1261:                }
                   1262:        }
                   1263:        return 1;
1.1       jsing    1264: }
                   1265:
                   1266: #endif