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

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