apps/cms.c:682: error: USE_AFTER_FREE
  call to `sk_X509_push()` eventually accesses memory that was invalidated by call to `free()` on line 682 indirectly during the call to `sk_X509_push()`.
Showing all 24 steps of the trace


apps/cms.c:676:28: invalidation part of the trace starts here
674.   
675.           if (*argv && encerts == NULL)
676.               if ((encerts = sk_X509_new_null()) == NULL)
                                  ^
677.                   goto end;
678.           while (*argv) {

apps/cms.c:676:28: passed as argument to `sk_X509_new_null`
  674.   
  675.           if (*argv && encerts == NULL)
  676.               if ((encerts = sk_X509_new_null()) == NULL)
                                    ^
  677.                   goto end;
  678.           while (*argv) {

apps/cms.c:676:28: return from call to `sk_X509_new_null`
  674.   
  675.           if (*argv && encerts == NULL)
  676.               if ((encerts = sk_X509_new_null()) == NULL)
                                    ^
  677.                   goto end;
  678.           while (*argv) {

apps/cms.c:676:18: assigned
  674.   
  675.           if (*argv && encerts == NULL)
  676.               if ((encerts = sk_X509_new_null()) == NULL)
                          ^
  677.                   goto end;
  678.           while (*argv) {

apps/cms.c:682:13: when calling `sk_X509_push` here
  680.                                     "recipient certificate file")) == NULL)
  681.                   goto end;
  682.               sk_X509_push(encerts, cert);
                     ^
  683.               cert = NULL;
  684.               argv++;

include/openssl/x509.h:99:1: parameter `sk` of sk_X509_push
    97.   typedef struct x509_cinf_st X509_CINF;
    98.   
    99. > DEFINE_STACK_OF(X509)
    100.   
    101.   /* This is used for a table of trust checking functions */

include/openssl/x509.h:99:1: when calling `OPENSSL_sk_push` here
    97.   typedef struct x509_cinf_st X509_CINF;
    98.   
    99. > DEFINE_STACK_OF(X509)
    100.   
    101.   /* This is used for a table of trust checking functions */

crypto/stack/stack.c:321:1: parameter `st` of OPENSSL_sk_push
      319.   }
      320.   
      321. > int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)
      322.   {
      323.       if (st == NULL)

crypto/stack/stack.c:325:12: when calling `OPENSSL_sk_insert` here
      323.       if (st == NULL)
      324.           return -1;
      325.       return OPENSSL_sk_insert(st, data, st->num);
                        ^
      326.   }
      327.   

crypto/stack/stack.c:232:1: parameter `st` of OPENSSL_sk_insert
        230.   }
        231.   
        232. > int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)
        233.   {
        234.       if (st == NULL || st->num == max_nodes)

crypto/stack/stack.c:237:10: when calling `sk_reserve` here
        235.           return 0;
        236.   
        237.       if (!sk_reserve(st, 1, 0))
                        ^
        238.           return 0;
        239.   

crypto/stack/stack.c:156:1: parameter `st` of sk_reserve
          154.   
          155.   /* internal STACK storage allocation */
          156. > static int sk_reserve(OPENSSL_STACK *st, int n, int exact)
          157.   {
          158.       const void **tmpdata;

crypto/stack/stack.c:193:15: when calling `CRYPTO_realloc` here
          191.       }
          192.   
          193.       tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);
                               ^
          194.       if (tmpdata == NULL)
          195.           return 0;

crypto/mem.c:238:1: parameter `str` of CRYPTO_realloc
            236.   }
            237.   
            238. > void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
            239.   {
            240.       INCREMENT(realloc_count);

crypto/mem.c:249:9: when calling `CRYPTO_free` here
            247.   
            248.       if (num == 0) {
            249.           CRYPTO_free(str, file, line);
                           ^
            250.           return NULL;
            251.       }

crypto/mem.c:295:1: parameter `str` of CRYPTO_free
              293.   }
              294.   
              295. > void CRYPTO_free(void *str, const char *file, int line)
              296.   {
              297.       INCREMENT(free_count);

crypto/mem.c:312:5: was invalidated by call to `free()`
              310.       }
              311.   #else
              312.       free(str);
                         ^
              313.   #endif
              314.   }

apps/cms.c:676:28: use-after-lifetime part of the trace starts here
674.   
675.           if (*argv && encerts == NULL)
676.               if ((encerts = sk_X509_new_null()) == NULL)
                                  ^
677.                   goto end;
678.           while (*argv) {

apps/cms.c:676:28: passed as argument to `sk_X509_new_null`
  674.   
  675.           if (*argv && encerts == NULL)
  676.               if ((encerts = sk_X509_new_null()) == NULL)
                                    ^
  677.                   goto end;
  678.           while (*argv) {

apps/cms.c:676:28: return from call to `sk_X509_new_null`
  674.   
  675.           if (*argv && encerts == NULL)
  676.               if ((encerts = sk_X509_new_null()) == NULL)
                                    ^
  677.                   goto end;
  678.           while (*argv) {

apps/cms.c:676:18: assigned
  674.   
  675.           if (*argv && encerts == NULL)
  676.               if ((encerts = sk_X509_new_null()) == NULL)
                          ^
  677.                   goto end;
  678.           while (*argv) {

apps/cms.c:682:13: when calling `sk_X509_push` here
  680.                                     "recipient certificate file")) == NULL)
  681.                   goto end;
  682.               sk_X509_push(encerts, cert);
                     ^
  683.               cert = NULL;
  684.               argv++;

crypto/mem.c:295:1: parameter `str` of CRYPTO_free
    293.   }
    294.   
    295. > void CRYPTO_free(void *str, const char *file, int line)
    296.   {
    297.       INCREMENT(free_count);

crypto/mem.c:312:5: invalid access occurs here
    310.       }
    311.   #else
    312.       free(str);
               ^
    313.   #endif
    314.   }

void CRYPTO_free(void *str, const char *file, int line)
{
    INCREMENT(free_count);
    if (free_impl != NULL && free_impl != &CRYPTO_free) {
        free_impl(str, file, line);
        return;
    }
#ifndef OPENSSL_NO_CRYPTO_MDEBUG
    if (call_malloc_debug) {
        CRYPTO_mem_debug_free(str, 0, file, line);
        free(str);
        CRYPTO_mem_debug_free(str, 1, file, line);
    } else {
        free(str);
    }
#else
    free(str);
#endif
}
['int cms_main(int argc, char **argv)\n{\n    ASN1_OBJECT *econtent_type = NULL;\n    BIO *in = NULL, *out = NULL, *indata = NULL, *rctin = NULL;\n    CMS_ContentInfo *cms = NULL, *rcms = NULL;\n    CMS_ReceiptRequest *rr = NULL;\n    ENGINE *e = NULL;\n    EVP_PKEY *key = NULL;\n    const EVP_CIPHER *cipher = NULL, *wrap_cipher = NULL;\n    const EVP_MD *sign_md = NULL;\n    STACK_OF(OPENSSL_STRING) *rr_to = NULL, *rr_from = NULL;\n    STACK_OF(OPENSSL_STRING) *sksigners = NULL, *skkeys = NULL;\n    STACK_OF(X509) *encerts = NULL, *other = NULL;\n    X509 *cert = NULL, *recip = NULL, *signer = NULL;\n    X509_STORE *store = NULL;\n    X509_VERIFY_PARAM *vpm = NULL;\n    char *certfile = NULL, *keyfile = NULL, *contfile = NULL;\n    const char *CAfile = NULL, *CApath = NULL;\n    char *certsoutfile = NULL;\n    int noCAfile = 0, noCApath = 0;\n    char *infile = NULL, *outfile = NULL, *rctfile = NULL;\n    char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile = NULL;\n    char *to = NULL, *from = NULL, *subject = NULL, *prog;\n    cms_key_param *key_first = NULL, *key_param = NULL;\n    int flags = CMS_DETACHED, noout = 0, print = 0, keyidx = -1, vpmtouched = 0;\n    int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;\n    int operation = 0, ret = 1, rr_print = 0, rr_allorfirst = -1;\n    int verify_retcode = 0, rctformat = FORMAT_SMIME, keyform = FORMAT_PEM;\n    size_t secret_keylen = 0, secret_keyidlen = 0;\n    unsigned char *pwri_pass = NULL, *pwri_tmp = NULL;\n    unsigned char *secret_key = NULL, *secret_keyid = NULL;\n    long ltmp;\n    const char *mime_eol = "\\n";\n    OPTION_CHOICE o;\n    if ((vpm = X509_VERIFY_PARAM_new()) == NULL)\n        return 1;\n    prog = opt_init(argc, argv, cms_options);\n    while ((o = opt_next()) != OPT_EOF) {\n        switch (o) {\n        case OPT_EOF:\n        case OPT_ERR:\n opthelp:\n            BIO_printf(bio_err, "%s: Use -help for summary.\\n", prog);\n            goto end;\n        case OPT_HELP:\n            opt_help(cms_options);\n            ret = 0;\n            goto end;\n        case OPT_INFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PDS, &informat))\n                goto opthelp;\n            break;\n        case OPT_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PDS, &outformat))\n                goto opthelp;\n            break;\n        case OPT_OUT:\n            outfile = opt_arg();\n            break;\n        case OPT_ENCRYPT:\n            operation = SMIME_ENCRYPT;\n            break;\n        case OPT_DECRYPT:\n            operation = SMIME_DECRYPT;\n            break;\n        case OPT_SIGN:\n            operation = SMIME_SIGN;\n            break;\n        case OPT_SIGN_RECEIPT:\n            operation = SMIME_SIGN_RECEIPT;\n            break;\n        case OPT_RESIGN:\n            operation = SMIME_RESIGN;\n            break;\n        case OPT_VERIFY:\n            operation = SMIME_VERIFY;\n            break;\n        case OPT_VERIFY_RETCODE:\n            verify_retcode = 1;\n            break;\n        case OPT_VERIFY_RECEIPT:\n            operation = SMIME_VERIFY_RECEIPT;\n            rctfile = opt_arg();\n            break;\n        case OPT_CMSOUT:\n            operation = SMIME_CMSOUT;\n            break;\n        case OPT_DATA_OUT:\n            operation = SMIME_DATAOUT;\n            break;\n        case OPT_DATA_CREATE:\n            operation = SMIME_DATA_CREATE;\n            break;\n        case OPT_DIGEST_VERIFY:\n            operation = SMIME_DIGEST_VERIFY;\n            break;\n        case OPT_DIGEST_CREATE:\n            operation = SMIME_DIGEST_CREATE;\n            break;\n        case OPT_COMPRESS:\n            operation = SMIME_COMPRESS;\n            break;\n        case OPT_UNCOMPRESS:\n            operation = SMIME_UNCOMPRESS;\n            break;\n        case OPT_ED_DECRYPT:\n            operation = SMIME_ENCRYPTED_DECRYPT;\n            break;\n        case OPT_ED_ENCRYPT:\n            operation = SMIME_ENCRYPTED_ENCRYPT;\n            break;\n        case OPT_DEBUG_DECRYPT:\n            flags |= CMS_DEBUG_DECRYPT;\n            break;\n        case OPT_TEXT:\n            flags |= CMS_TEXT;\n            break;\n        case OPT_ASCIICRLF:\n            flags |= CMS_ASCIICRLF;\n            break;\n        case OPT_NOINTERN:\n            flags |= CMS_NOINTERN;\n            break;\n        case OPT_NOVERIFY:\n            flags |= CMS_NO_SIGNER_CERT_VERIFY;\n            break;\n        case OPT_NOCERTS:\n            flags |= CMS_NOCERTS;\n            break;\n        case OPT_NOATTR:\n            flags |= CMS_NOATTR;\n            break;\n        case OPT_NODETACH:\n            flags &= ~CMS_DETACHED;\n            break;\n        case OPT_NOSMIMECAP:\n            flags |= CMS_NOSMIMECAP;\n            break;\n        case OPT_BINARY:\n            flags |= CMS_BINARY;\n            break;\n        case OPT_KEYID:\n            flags |= CMS_USE_KEYID;\n            break;\n        case OPT_NOSIGS:\n            flags |= CMS_NOSIGS;\n            break;\n        case OPT_NO_CONTENT_VERIFY:\n            flags |= CMS_NO_CONTENT_VERIFY;\n            break;\n        case OPT_NO_ATTR_VERIFY:\n            flags |= CMS_NO_ATTR_VERIFY;\n            break;\n        case OPT_INDEF:\n            flags |= CMS_STREAM;\n            break;\n        case OPT_NOINDEF:\n            flags &= ~CMS_STREAM;\n            break;\n        case OPT_CRLFEOL:\n            mime_eol = "\\r\\n";\n            flags |= CMS_CRLFEOL;\n            break;\n        case OPT_NOOUT:\n            noout = 1;\n            break;\n        case OPT_RR_PRINT:\n            rr_print = 1;\n            break;\n        case OPT_RR_ALL:\n            rr_allorfirst = 0;\n            break;\n        case OPT_RR_FIRST:\n            rr_allorfirst = 1;\n            break;\n        case OPT_RCTFORM:\n            if (rctformat == FORMAT_SMIME)\n                rcms = SMIME_read_CMS(rctin, NULL);\n            else if (rctformat == FORMAT_PEM)\n                rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);\n            else if (rctformat == FORMAT_ASN1)\n                if (!opt_format(opt_arg(),\n                                OPT_FMT_PEMDER | OPT_FMT_SMIME, &rctformat))\n                    goto opthelp;\n            break;\n        case OPT_CERTFILE:\n            certfile = opt_arg();\n            break;\n        case OPT_CAFILE:\n            CAfile = opt_arg();\n            break;\n        case OPT_CAPATH:\n            CApath = opt_arg();\n            break;\n        case OPT_NOCAFILE:\n            noCAfile = 1;\n            break;\n        case OPT_NOCAPATH:\n            noCApath = 1;\n            break;\n        case OPT_IN:\n            infile = opt_arg();\n            break;\n        case OPT_CONTENT:\n            contfile = opt_arg();\n            break;\n        case OPT_RR_FROM:\n            if (rr_from == NULL\n                && (rr_from = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            sk_OPENSSL_STRING_push(rr_from, opt_arg());\n            break;\n        case OPT_RR_TO:\n            if (rr_to == NULL\n                && (rr_to = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            sk_OPENSSL_STRING_push(rr_to, opt_arg());\n            break;\n        case OPT_PRINT:\n            noout = print = 1;\n            break;\n        case OPT_SECRETKEY:\n            if (secret_key != NULL) {\n                BIO_printf(bio_err, "Invalid key (supplied twice) %s\\n",\n                           opt_arg());\n                goto opthelp;\n            }\n            secret_key = OPENSSL_hexstr2buf(opt_arg(), &ltmp);\n            if (secret_key == NULL) {\n                BIO_printf(bio_err, "Invalid key %s\\n", opt_arg());\n                goto end;\n            }\n            secret_keylen = (size_t)ltmp;\n            break;\n        case OPT_SECRETKEYID:\n            if (secret_keyid != NULL) {\n                BIO_printf(bio_err, "Invalid id (supplied twice) %s\\n",\n                           opt_arg());\n                goto opthelp;\n            }\n            secret_keyid = OPENSSL_hexstr2buf(opt_arg(), &ltmp);\n            if (secret_keyid == NULL) {\n                BIO_printf(bio_err, "Invalid id %s\\n", opt_arg());\n                goto opthelp;\n            }\n            secret_keyidlen = (size_t)ltmp;\n            break;\n        case OPT_PWRI_PASSWORD:\n            pwri_pass = (unsigned char *)opt_arg();\n            break;\n        case OPT_ECONTENT_TYPE:\n            if (econtent_type != NULL) {\n                BIO_printf(bio_err, "Invalid OID (supplied twice) %s\\n",\n                           opt_arg());\n                goto opthelp;\n            }\n            econtent_type = OBJ_txt2obj(opt_arg(), 0);\n            if (econtent_type == NULL) {\n                BIO_printf(bio_err, "Invalid OID %s\\n", opt_arg());\n                goto opthelp;\n            }\n            break;\n        case OPT_ENGINE:\n            e = setup_engine(opt_arg(), 0);\n            break;\n        case OPT_PASSIN:\n            passinarg = opt_arg();\n            break;\n        case OPT_TO:\n            to = opt_arg();\n            break;\n        case OPT_FROM:\n            from = opt_arg();\n            break;\n        case OPT_SUBJECT:\n            subject = opt_arg();\n            break;\n        case OPT_CERTSOUT:\n            certsoutfile = opt_arg();\n            break;\n        case OPT_MD:\n            if (!opt_md(opt_arg(), &sign_md))\n                goto end;\n            break;\n        case OPT_SIGNER:\n            if (signerfile != NULL) {\n                if (sksigners == NULL\n                    && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(sksigners, signerfile);\n                if (keyfile == NULL)\n                    keyfile = signerfile;\n                if (skkeys == NULL\n                    && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(skkeys, keyfile);\n                keyfile = NULL;\n            }\n            signerfile = opt_arg();\n            break;\n        case OPT_INKEY:\n            if (keyfile != NULL) {\n                if (signerfile == NULL) {\n                    BIO_puts(bio_err, "Illegal -inkey without -signer\\n");\n                    goto end;\n                }\n                if (sksigners == NULL\n                    && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(sksigners, signerfile);\n                signerfile = NULL;\n                if (skkeys == NULL\n                    && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                sk_OPENSSL_STRING_push(skkeys, keyfile);\n            }\n            keyfile = opt_arg();\n            break;\n        case OPT_KEYFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))\n                goto opthelp;\n            break;\n        case OPT_RECIP:\n            if (operation == SMIME_ENCRYPT) {\n                if (encerts == NULL && (encerts = sk_X509_new_null()) == NULL)\n                    goto end;\n                cert = load_cert(opt_arg(), FORMAT_PEM,\n                                 "recipient certificate file");\n                if (cert == NULL)\n                    goto end;\n                sk_X509_push(encerts, cert);\n                cert = NULL;\n            } else {\n                recipfile = opt_arg();\n            }\n            break;\n        case OPT_CIPHER:\n            if (!opt_cipher(opt_unknown(), &cipher))\n                goto end;\n            break;\n        case OPT_KEYOPT:\n            keyidx = -1;\n            if (operation == SMIME_ENCRYPT) {\n                if (encerts != NULL)\n                    keyidx += sk_X509_num(encerts);\n            } else {\n                if (keyfile != NULL || signerfile != NULL)\n                    keyidx++;\n                if (skkeys != NULL)\n                    keyidx += sk_OPENSSL_STRING_num(skkeys);\n            }\n            if (keyidx < 0) {\n                BIO_printf(bio_err, "No key specified\\n");\n                goto opthelp;\n            }\n            if (key_param == NULL || key_param->idx != keyidx) {\n                cms_key_param *nparam;\n                nparam = app_malloc(sizeof(*nparam), "key param buffer");\n                nparam->idx = keyidx;\n                if ((nparam->param = sk_OPENSSL_STRING_new_null()) == NULL)\n                    goto end;\n                nparam->next = NULL;\n                if (key_first == NULL)\n                    key_first = nparam;\n                else\n                    key_param->next = nparam;\n                key_param = nparam;\n            }\n            sk_OPENSSL_STRING_push(key_param->param, opt_arg());\n            break;\n        case OPT_V_CASES:\n            if (!opt_verify(o, vpm))\n                goto end;\n            vpmtouched++;\n            break;\n        case OPT_R_CASES:\n            if (!opt_rand(o))\n                goto end;\n            break;\n        case OPT_3DES_WRAP:\n# ifndef OPENSSL_NO_DES\n            wrap_cipher = EVP_des_ede3_wrap();\n# endif\n            break;\n        case OPT_AES128_WRAP:\n            wrap_cipher = EVP_aes_128_wrap();\n            break;\n        case OPT_AES192_WRAP:\n            wrap_cipher = EVP_aes_192_wrap();\n            break;\n        case OPT_AES256_WRAP:\n            wrap_cipher = EVP_aes_256_wrap();\n            break;\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if ((rr_allorfirst != -1 || rr_from != NULL) && rr_to == NULL) {\n        BIO_puts(bio_err, "No Signed Receipts Recipients\\n");\n        goto opthelp;\n    }\n    if (!(operation & SMIME_SIGNERS) && (rr_to != NULL || rr_from != NULL)) {\n        BIO_puts(bio_err, "Signed receipts only allowed with -sign\\n");\n        goto opthelp;\n    }\n    if (!(operation & SMIME_SIGNERS) && (skkeys != NULL || sksigners != NULL)) {\n        BIO_puts(bio_err, "Multiple signers or keys not allowed\\n");\n        goto opthelp;\n    }\n    if (operation & SMIME_SIGNERS) {\n        if (keyfile != NULL && signerfile == NULL) {\n            BIO_puts(bio_err, "Illegal -inkey without -signer\\n");\n            goto opthelp;\n        }\n        if (signerfile != NULL) {\n            if (sksigners == NULL\n                && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            sk_OPENSSL_STRING_push(sksigners, signerfile);\n            if (skkeys == NULL && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            if (keyfile == NULL)\n                keyfile = signerfile;\n            sk_OPENSSL_STRING_push(skkeys, keyfile);\n        }\n        if (sksigners == NULL) {\n            BIO_printf(bio_err, "No signer certificate specified\\n");\n            goto opthelp;\n        }\n        signerfile = NULL;\n        keyfile = NULL;\n    } else if (operation == SMIME_DECRYPT) {\n        if (recipfile == NULL && keyfile == NULL\n            && secret_key == NULL && pwri_pass == NULL) {\n            BIO_printf(bio_err,\n                       "No recipient certificate or key specified\\n");\n            goto opthelp;\n        }\n    } else if (operation == SMIME_ENCRYPT) {\n        if (*argv == NULL && secret_key == NULL\n            && pwri_pass == NULL && encerts == NULL) {\n            BIO_printf(bio_err, "No recipient(s) certificate(s) specified\\n");\n            goto opthelp;\n        }\n    } else if (!operation) {\n        goto opthelp;\n    }\n    if (!app_passwd(passinarg, NULL, &passin, NULL)) {\n        BIO_printf(bio_err, "Error getting password\\n");\n        goto end;\n    }\n    ret = 2;\n    if (!(operation & SMIME_SIGNERS))\n        flags &= ~CMS_DETACHED;\n    if (!(operation & SMIME_OP))\n        if (flags & CMS_BINARY)\n            outformat = FORMAT_BINARY;\n    if (!(operation & SMIME_IP))\n        if (flags & CMS_BINARY)\n            informat = FORMAT_BINARY;\n    if (operation == SMIME_ENCRYPT) {\n        if (!cipher) {\n# ifndef OPENSSL_NO_DES\n            cipher = EVP_des_ede3_cbc();\n# else\n            BIO_printf(bio_err, "No cipher selected\\n");\n            goto end;\n# endif\n        }\n        if (secret_key && !secret_keyid) {\n            BIO_printf(bio_err, "No secret key id\\n");\n            goto end;\n        }\n        if (*argv && encerts == NULL)\n            if ((encerts = sk_X509_new_null()) == NULL)\n                goto end;\n        while (*argv) {\n            if ((cert = load_cert(*argv, FORMAT_PEM,\n                                  "recipient certificate file")) == NULL)\n                goto end;\n            sk_X509_push(encerts, cert);\n            cert = NULL;\n            argv++;\n        }\n    }\n    if (certfile != NULL) {\n        if (!load_certs(certfile, &other, FORMAT_PEM, NULL,\n                        "certificate file")) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (recipfile != NULL && (operation == SMIME_DECRYPT)) {\n        if ((recip = load_cert(recipfile, FORMAT_PEM,\n                               "recipient certificate file")) == NULL) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (operation == SMIME_SIGN_RECEIPT) {\n        if ((signer = load_cert(signerfile, FORMAT_PEM,\n                                "receipt signer certificate file")) == NULL) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (operation == SMIME_DECRYPT) {\n        if (keyfile == NULL)\n            keyfile = recipfile;\n    } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {\n        if (keyfile == NULL)\n            keyfile = signerfile;\n    } else {\n        keyfile = NULL;\n    }\n    if (keyfile != NULL) {\n        key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n        if (key == NULL)\n            goto end;\n    }\n    in = bio_open_default(infile, \'r\', informat);\n    if (in == NULL)\n        goto end;\n    if (operation & SMIME_IP) {\n        if (informat == FORMAT_SMIME) {\n            cms = SMIME_read_CMS(in, &indata);\n        } else if (informat == FORMAT_PEM) {\n            cms = PEM_read_bio_CMS(in, NULL, NULL, NULL);\n        } else if (informat == FORMAT_ASN1) {\n            cms = d2i_CMS_bio(in, NULL);\n        } else {\n            BIO_printf(bio_err, "Bad input format for CMS file\\n");\n            goto end;\n        }\n        if (cms == NULL) {\n            BIO_printf(bio_err, "Error reading S/MIME message\\n");\n            goto end;\n        }\n        if (contfile != NULL) {\n            BIO_free(indata);\n            if ((indata = BIO_new_file(contfile, "rb")) == NULL) {\n                BIO_printf(bio_err, "Can\'t read content file %s\\n", contfile);\n                goto end;\n            }\n        }\n        if (certsoutfile != NULL) {\n            STACK_OF(X509) *allcerts;\n            allcerts = CMS_get1_certs(cms);\n            if (!save_certs(certsoutfile, allcerts)) {\n                BIO_printf(bio_err,\n                           "Error writing certs to %s\\n", certsoutfile);\n                ret = 5;\n                goto end;\n            }\n            sk_X509_pop_free(allcerts, X509_free);\n        }\n    }\n    if (rctfile != NULL) {\n        char *rctmode = (rctformat == FORMAT_ASN1) ? "rb" : "r";\n        if ((rctin = BIO_new_file(rctfile, rctmode)) == NULL) {\n            BIO_printf(bio_err, "Can\'t open receipt file %s\\n", rctfile);\n            goto end;\n        }\n        if (rctformat == FORMAT_SMIME) {\n            rcms = SMIME_read_CMS(rctin, NULL);\n        } else if (rctformat == FORMAT_PEM) {\n            rcms = PEM_read_bio_CMS(rctin, NULL, NULL, NULL);\n        } else if (rctformat == FORMAT_ASN1) {\n            rcms = d2i_CMS_bio(rctin, NULL);\n        } else {\n            BIO_printf(bio_err, "Bad input format for receipt\\n");\n            goto end;\n        }\n        if (rcms == NULL) {\n            BIO_printf(bio_err, "Error reading receipt\\n");\n            goto end;\n        }\n    }\n    out = bio_open_default(outfile, \'w\', outformat);\n    if (out == NULL)\n        goto end;\n    if ((operation == SMIME_VERIFY) || (operation == SMIME_VERIFY_RECEIPT)) {\n        if ((store = setup_verify(CAfile, CApath, noCAfile, noCApath)) == NULL)\n            goto end;\n        X509_STORE_set_verify_cb(store, cms_cb);\n        if (vpmtouched)\n            X509_STORE_set1_param(store, vpm);\n    }\n    ret = 3;\n    if (operation == SMIME_DATA_CREATE) {\n        cms = CMS_data_create(in, flags);\n    } else if (operation == SMIME_DIGEST_CREATE) {\n        cms = CMS_digest_create(in, sign_md, flags);\n    } else if (operation == SMIME_COMPRESS) {\n        cms = CMS_compress(in, -1, flags);\n    } else if (operation == SMIME_ENCRYPT) {\n        int i;\n        flags |= CMS_PARTIAL;\n        cms = CMS_encrypt(NULL, in, cipher, flags);\n        if (cms == NULL)\n            goto end;\n        for (i = 0; i < sk_X509_num(encerts); i++) {\n            CMS_RecipientInfo *ri;\n            cms_key_param *kparam;\n            int tflags = flags;\n            X509 *x = sk_X509_value(encerts, i);\n            for (kparam = key_first; kparam; kparam = kparam->next) {\n                if (kparam->idx == i) {\n                    tflags |= CMS_KEY_PARAM;\n                    break;\n                }\n            }\n            ri = CMS_add1_recipient_cert(cms, x, tflags);\n            if (ri == NULL)\n                goto end;\n            if (kparam != NULL) {\n                EVP_PKEY_CTX *pctx;\n                pctx = CMS_RecipientInfo_get0_pkey_ctx(ri);\n                if (!cms_set_pkey_param(pctx, kparam->param))\n                    goto end;\n            }\n            if (CMS_RecipientInfo_type(ri) == CMS_RECIPINFO_AGREE\n                && wrap_cipher) {\n                EVP_CIPHER_CTX *wctx;\n                wctx = CMS_RecipientInfo_kari_get0_ctx(ri);\n                EVP_EncryptInit_ex(wctx, wrap_cipher, NULL, NULL, NULL);\n            }\n        }\n        if (secret_key != NULL) {\n            if (!CMS_add0_recipient_key(cms, NID_undef,\n                                        secret_key, secret_keylen,\n                                        secret_keyid, secret_keyidlen,\n                                        NULL, NULL, NULL))\n                goto end;\n            secret_key = NULL;\n            secret_keyid = NULL;\n        }\n        if (pwri_pass != NULL) {\n            pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);\n            if (pwri_tmp == NULL)\n                goto end;\n            if (CMS_add0_recipient_password(cms,\n                                            -1, NID_undef, NID_undef,\n                                            pwri_tmp, -1, NULL) == NULL)\n                goto end;\n            pwri_tmp = NULL;\n        }\n        if (!(flags & CMS_STREAM)) {\n            if (!CMS_final(cms, in, NULL, flags))\n                goto end;\n        }\n    } else if (operation == SMIME_ENCRYPTED_ENCRYPT) {\n        cms = CMS_EncryptedData_encrypt(in, cipher,\n                                        secret_key, secret_keylen, flags);\n    } else if (operation == SMIME_SIGN_RECEIPT) {\n        CMS_ContentInfo *srcms = NULL;\n        STACK_OF(CMS_SignerInfo) *sis;\n        CMS_SignerInfo *si;\n        sis = CMS_get0_SignerInfos(cms);\n        if (sis == NULL)\n            goto end;\n        si = sk_CMS_SignerInfo_value(sis, 0);\n        srcms = CMS_sign_receipt(si, signer, key, other, flags);\n        if (srcms == NULL)\n            goto end;\n        CMS_ContentInfo_free(cms);\n        cms = srcms;\n    } else if (operation & SMIME_SIGNERS) {\n        int i;\n        if (operation == SMIME_SIGN) {\n            if (flags & CMS_DETACHED) {\n                if (outformat == FORMAT_SMIME)\n                    flags |= CMS_STREAM;\n            }\n            flags |= CMS_PARTIAL;\n            cms = CMS_sign(NULL, NULL, other, in, flags);\n            if (cms == NULL)\n                goto end;\n            if (econtent_type != NULL)\n                CMS_set1_eContentType(cms, econtent_type);\n            if (rr_to != NULL) {\n                rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);\n                if (rr == NULL) {\n                    BIO_puts(bio_err,\n                             "Signed Receipt Request Creation Error\\n");\n                    goto end;\n                }\n            }\n        } else {\n            flags |= CMS_REUSE_DIGEST;\n        }\n        for (i = 0; i < sk_OPENSSL_STRING_num(sksigners); i++) {\n            CMS_SignerInfo *si;\n            cms_key_param *kparam;\n            int tflags = flags;\n            signerfile = sk_OPENSSL_STRING_value(sksigners, i);\n            keyfile = sk_OPENSSL_STRING_value(skkeys, i);\n            signer = load_cert(signerfile, FORMAT_PEM, "signer certificate");\n            if (signer == NULL) {\n                ret = 2;\n                goto end;\n            }\n            key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n            if (key == NULL) {\n                ret = 2;\n                goto end;\n            }\n            for (kparam = key_first; kparam; kparam = kparam->next) {\n                if (kparam->idx == i) {\n                    tflags |= CMS_KEY_PARAM;\n                    break;\n                }\n            }\n            si = CMS_add1_signer(cms, signer, key, sign_md, tflags);\n            if (si == NULL)\n                goto end;\n            if (kparam != NULL) {\n                EVP_PKEY_CTX *pctx;\n                pctx = CMS_SignerInfo_get0_pkey_ctx(si);\n                if (!cms_set_pkey_param(pctx, kparam->param))\n                    goto end;\n            }\n            if (rr != NULL && !CMS_add1_ReceiptRequest(si, rr))\n                goto end;\n            X509_free(signer);\n            signer = NULL;\n            EVP_PKEY_free(key);\n            key = NULL;\n        }\n        if ((operation == SMIME_SIGN) && !(flags & CMS_STREAM)) {\n            if (!CMS_final(cms, in, NULL, flags))\n                goto end;\n        }\n    }\n    if (cms == NULL) {\n        BIO_printf(bio_err, "Error creating CMS structure\\n");\n        goto end;\n    }\n    ret = 4;\n    if (operation == SMIME_DECRYPT) {\n        if (flags & CMS_DEBUG_DECRYPT)\n            CMS_decrypt(cms, NULL, NULL, NULL, NULL, flags);\n        if (secret_key != NULL) {\n            if (!CMS_decrypt_set1_key(cms,\n                                      secret_key, secret_keylen,\n                                      secret_keyid, secret_keyidlen)) {\n                BIO_puts(bio_err, "Error decrypting CMS using secret key\\n");\n                goto end;\n            }\n        }\n        if (key != NULL) {\n            if (!CMS_decrypt_set1_pkey(cms, key, recip)) {\n                BIO_puts(bio_err, "Error decrypting CMS using private key\\n");\n                goto end;\n            }\n        }\n        if (pwri_pass != NULL) {\n            if (!CMS_decrypt_set1_password(cms, pwri_pass, -1)) {\n                BIO_puts(bio_err, "Error decrypting CMS using password\\n");\n                goto end;\n            }\n        }\n        if (!CMS_decrypt(cms, NULL, NULL, indata, out, flags)) {\n            BIO_printf(bio_err, "Error decrypting CMS structure\\n");\n            goto end;\n        }\n    } else if (operation == SMIME_DATAOUT) {\n        if (!CMS_data(cms, out, flags))\n            goto end;\n    } else if (operation == SMIME_UNCOMPRESS) {\n        if (!CMS_uncompress(cms, indata, out, flags))\n            goto end;\n    } else if (operation == SMIME_DIGEST_VERIFY) {\n        if (CMS_digest_verify(cms, indata, out, flags) > 0) {\n            BIO_printf(bio_err, "Verification successful\\n");\n        } else {\n            BIO_printf(bio_err, "Verification failure\\n");\n            goto end;\n        }\n    } else if (operation == SMIME_ENCRYPTED_DECRYPT) {\n        if (!CMS_EncryptedData_decrypt(cms, secret_key, secret_keylen,\n                                       indata, out, flags))\n            goto end;\n    } else if (operation == SMIME_VERIFY) {\n        if (CMS_verify(cms, other, store, indata, out, flags) > 0) {\n            BIO_printf(bio_err, "Verification successful\\n");\n        } else {\n            BIO_printf(bio_err, "Verification failure\\n");\n            if (verify_retcode)\n                ret = verify_err + 32;\n            goto end;\n        }\n        if (signerfile != NULL) {\n            STACK_OF(X509) *signers;\n            signers = CMS_get0_signers(cms);\n            if (!save_certs(signerfile, signers)) {\n                BIO_printf(bio_err,\n                           "Error writing signers to %s\\n", signerfile);\n                ret = 5;\n                goto end;\n            }\n            sk_X509_free(signers);\n        }\n        if (rr_print)\n            receipt_request_print(cms);\n    } else if (operation == SMIME_VERIFY_RECEIPT) {\n        if (CMS_verify_receipt(rcms, cms, other, store, flags) > 0) {\n            BIO_printf(bio_err, "Verification successful\\n");\n        } else {\n            BIO_printf(bio_err, "Verification failure\\n");\n            goto end;\n        }\n    } else {\n        if (noout) {\n            if (print)\n                CMS_ContentInfo_print_ctx(out, cms, 0, NULL);\n        } else if (outformat == FORMAT_SMIME) {\n            if (to)\n                BIO_printf(out, "To: %s%s", to, mime_eol);\n            if (from)\n                BIO_printf(out, "From: %s%s", from, mime_eol);\n            if (subject)\n                BIO_printf(out, "Subject: %s%s", subject, mime_eol);\n            if (operation == SMIME_RESIGN)\n                ret = SMIME_write_CMS(out, cms, indata, flags);\n            else\n                ret = SMIME_write_CMS(out, cms, in, flags);\n        } else if (outformat == FORMAT_PEM) {\n            ret = PEM_write_bio_CMS_stream(out, cms, in, flags);\n        } else if (outformat == FORMAT_ASN1) {\n            ret = i2d_CMS_bio_stream(out, cms, in, flags);\n        } else {\n            BIO_printf(bio_err, "Bad output format for CMS file\\n");\n            goto end;\n        }\n        if (ret <= 0) {\n            ret = 6;\n            goto end;\n        }\n    }\n    ret = 0;\n end:\n    if (ret)\n        ERR_print_errors(bio_err);\n    sk_X509_pop_free(encerts, X509_free);\n    sk_X509_pop_free(other, X509_free);\n    X509_VERIFY_PARAM_free(vpm);\n    sk_OPENSSL_STRING_free(sksigners);\n    sk_OPENSSL_STRING_free(skkeys);\n    OPENSSL_free(secret_key);\n    OPENSSL_free(secret_keyid);\n    OPENSSL_free(pwri_tmp);\n    ASN1_OBJECT_free(econtent_type);\n    CMS_ReceiptRequest_free(rr);\n    sk_OPENSSL_STRING_free(rr_to);\n    sk_OPENSSL_STRING_free(rr_from);\n    for (key_param = key_first; key_param;) {\n        cms_key_param *tparam;\n        sk_OPENSSL_STRING_free(key_param->param);\n        tparam = key_param->next;\n        OPENSSL_free(key_param);\n        key_param = tparam;\n    }\n    X509_STORE_free(store);\n    X509_free(cert);\n    X509_free(recip);\n    X509_free(signer);\n    EVP_PKEY_free(key);\n    CMS_ContentInfo_free(cms);\n    CMS_ContentInfo_free(rcms);\n    release_engine(e);\n    BIO_free(rctin);\n    BIO_free(in);\n    BIO_free(indata);\n    BIO_free_all(out);\n    OPENSSL_free(passin);\n    return ret;\n}', 'DEFINE_STACK_OF(X509)', 'int OPENSSL_sk_push(OPENSSL_STACK *st, const void *data)\n{\n    if (st == NULL)\n        return -1;\n    return OPENSSL_sk_insert(st, data, st->num);\n}', 'int OPENSSL_sk_insert(OPENSSL_STACK *st, const void *data, int loc)\n{\n    if (st == NULL || st->num == max_nodes)\n        return 0;\n    if (!sk_reserve(st, 1, 0))\n        return 0;\n    if ((loc >= st->num) || (loc < 0)) {\n        st->data[st->num] = data;\n    } else {\n        memmove(&st->data[loc + 1], &st->data[loc],\n                sizeof(st->data[0]) * (st->num - loc));\n        st->data[loc] = data;\n    }\n    st->num++;\n    st->sorted = 0;\n    return st->num;\n}', 'static int sk_reserve(OPENSSL_STACK *st, int n, int exact)\n{\n    const void **tmpdata;\n    int num_alloc;\n    if (n > max_nodes - st->num)\n        return 0;\n    num_alloc = st->num + n;\n    if (num_alloc < min_nodes)\n        num_alloc = min_nodes;\n    if (st->data == NULL) {\n        st->data = OPENSSL_zalloc(sizeof(void *) * num_alloc);\n        if (st->data == NULL)\n            return 0;\n        st->num_alloc = num_alloc;\n        return 1;\n    }\n    if (!exact) {\n        if (num_alloc <= st->num_alloc)\n            return 1;\n        num_alloc = compute_growth(num_alloc, st->num_alloc);\n        if (num_alloc == 0)\n            return 0;\n    } else if (num_alloc == st->num_alloc) {\n        return 1;\n    }\n    tmpdata = OPENSSL_realloc((void *)st->data, sizeof(void *) * num_alloc);\n    if (tmpdata == NULL)\n        return 0;\n    st->data = tmpdata;\n    st->num_alloc = num_alloc;\n    return 1;\n}', 'void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)\n{\n    INCREMENT(realloc_count);\n    if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)\n        return realloc_impl(str, num, file, line);\n    FAILTEST();\n    if (str == NULL)\n        return CRYPTO_malloc(num, file, line);\n    if (num == 0) {\n        CRYPTO_free(str, file, line);\n        return NULL;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        void *ret;\n        CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);\n        ret = realloc(str, num);\n        CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);\n        return ret;\n    }\n#else\n    (void)(file); (void)(line);\n#endif\n    return realloc(str, num);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\n    INCREMENT(free_count);\n    if (free_impl != NULL && free_impl != &CRYPTO_free) {\n        free_impl(str, file, line);\n        return;\n    }\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_free(str, 0, file, line);\n        free(str);\n        CRYPTO_mem_debug_free(str, 1, file, line);\n    } else {\n        free(str);\n    }\n#else\n    free(str);\n#endif\n}']