[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.16

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