int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
                      const unsigned char *in, int inl)
{
    int i, j, bl;
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
        i = ctx->cipher->do_cipher(ctx, out, in, inl);
        if (i < 0)
            return 0;
        else
            *outl = i;
        return 1;
    }
    if (inl <= 0) {
        *outl = 0;
        return inl == 0;
    }
    if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
        if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
            *outl = inl;
            return 1;
        } else {
            *outl = 0;
            return 0;
        }
    }
    i = ctx->buf_len;
    bl = ctx->cipher->block_size;
    OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
    if (i != 0) {
        if (i + inl < bl) {
            memcpy(&(ctx->buf[i]), in, inl);
            ctx->buf_len += inl;
            *outl = 0;
            return 1;
        } else {
            j = bl - i;
            memcpy(&(ctx->buf[i]), in, j);
            if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
                return 0;
            inl -= j;
            in += j;
            out += bl;
            *outl = bl;
        }
    } else
        *outl = 0;
    i = inl & (bl - 1);
    inl -= i;
    if (inl > 0) {
        if (!ctx->cipher->do_cipher(ctx, out, in, inl))
            return 0;
        *outl += inl;
    }
    if (i != 0)
        memcpy(ctx->buf, &(in[inl]), i);
    ctx->buf_len = i;
    return 1;
}
['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    char *CAfile = NULL, *CApath = NULL, *certsoutfile = NULL;\n    int noCAfile = 0, noCApath = 0;\n    char *infile = NULL, *outfile = NULL, *rctfile = NULL, *inrand = NULL;\n    char *passinarg = NULL, *passin = NULL, *signerfile = NULL, *recipfile =\n        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 =\n        0;\n    int informat = FORMAT_SMIME, outformat = FORMAT_SMIME;\n    int need_rand = 0, operation = 0, ret = 1, rr_print = 0, rr_allorfirst =\n        -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    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_PEMDER, &informat))\n                goto opthelp;\n            break;\n        case OPT_OUTFORM:\n            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &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_NOOLDMIME:\n            flags |= CMS_NOOLDMIMETYPE;\n            break;\n        case OPT_CRLFEOL:\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            secret_key = string_to_hex(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            secret_keyid = string_to_hex(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            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_RAND:\n            inrand = opt_arg();\n            need_rand = 1;\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) {\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) {\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, NULL, e,\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            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)\n                    keyidx += sk_X509_num(encerts);\n            } else {\n                if (keyfile || signerfile)\n                    keyidx++;\n                if (skkeys)\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_3DES_WRAP:\n# ifndef OPENSSL_NO_DES\n            wrap_cipher = EVP_des_ede3_wrap();\n# endif\n            break;\n# ifndef OPENSSL_NO_AES\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# else\n        case OPT_AES128_WRAP:\n        case OPT_AES192_WRAP:\n        case OPT_AES256_WRAP:\n            break;\n# endif\n        }\n    }\n    argc = opt_num_rest();\n    argv = opt_rest();\n    if (((rr_allorfirst != -1) || rr_from) && !rr_to) {\n        BIO_puts(bio_err, "No Signed Receipts Recipients\\n");\n        goto opthelp;\n    }\n    if (!(operation & SMIME_SIGNERS) && (rr_to || rr_from)) {\n        BIO_puts(bio_err, "Signed receipts only allowed with -sign\\n");\n        goto opthelp;\n    }\n    if (!(operation & SMIME_SIGNERS) && (skkeys || sksigners)) {\n        BIO_puts(bio_err, "Multiple signers or keys not allowed\\n");\n        goto opthelp;\n    }\n    if (operation & SMIME_SIGNERS) {\n        if (keyfile && !signerfile) {\n            BIO_puts(bio_err, "Illegal -inkey without -signer\\n");\n            goto opthelp;\n        }\n        if (signerfile) {\n            if (!sksigners\n                && (sksigners = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            sk_OPENSSL_STRING_push(sksigners, signerfile);\n            if (!skkeys && (skkeys = sk_OPENSSL_STRING_new_null()) == NULL)\n                goto end;\n            if (!keyfile)\n                keyfile = signerfile;\n            sk_OPENSSL_STRING_push(skkeys, keyfile);\n        }\n        if (!sksigners) {\n            BIO_printf(bio_err, "No signer certificate specified\\n");\n            goto opthelp;\n        }\n        signerfile = NULL;\n        keyfile = NULL;\n        need_rand = 1;\n    }\n    else if (operation == SMIME_DECRYPT) {\n        if (!recipfile && !keyfile && !secret_key && !pwri_pass) {\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 && !pwri_pass && !encerts) {\n            BIO_printf(bio_err, "No recipient(s) certificate(s) specified\\n");\n            goto opthelp;\n        }\n        need_rand = 1;\n    } else if (!operation)\n        goto opthelp;\n    if (!app_passwd(passinarg, NULL, &passin, NULL)) {\n        BIO_printf(bio_err, "Error getting password\\n");\n        goto end;\n    }\n    if (need_rand) {\n        app_RAND_load_file(NULL, (inrand != NULL));\n        if (inrand != NULL)\n            BIO_printf(bio_err, "%ld semi-random bytes loaded\\n",\n                       app_RAND_load_files(inrand));\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    }\n    if (!(operation & SMIME_IP)) {\n        if (flags & CMS_BINARY)\n            informat = FORMAT_BINARY;\n    }\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)\n            if ((encerts = sk_X509_new_null()) == NULL)\n                goto end;\n        while (*argv) {\n            if ((cert = load_cert(*argv, FORMAT_PEM, NULL, e,\n                                  "recipient certificate file")) == NULL)\n                goto end;\n            sk_X509_push(encerts, cert);\n            cert = NULL;\n            argv++;\n        }\n    }\n    if (certfile) {\n        if ((other = load_certs(certfile, FORMAT_PEM, NULL, e,\n                                "certificate file")) == NULL) {\n            ERR_print_errors(bio_err);\n            goto end;\n        }\n    }\n    if (recipfile && (operation == SMIME_DECRYPT)) {\n        if ((recip = load_cert(recipfile, FORMAT_PEM, NULL, e,\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, NULL, e,\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)\n            keyfile = recipfile;\n    } else if ((operation == SMIME_SIGN) || (operation == SMIME_SIGN_RECEIPT)) {\n        if (!keyfile)\n            keyfile = signerfile;\n    } else\n        keyfile = NULL;\n    if (keyfile) {\n        key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n        if (!key)\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) {\n            BIO_printf(bio_err, "Error reading S/MIME message\\n");\n            goto end;\n        }\n        if (contfile) {\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) {\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) {\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) {\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)\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)\n                goto end;\n            if (kparam) {\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) {\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) {\n            pwri_tmp = (unsigned char *)OPENSSL_strdup((char *)pwri_pass);\n            if (!pwri_tmp)\n                goto end;\n            if (!CMS_add0_recipient_password(cms,\n                                             -1, NID_undef, NID_undef,\n                                             pwri_tmp, -1, 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)\n            goto end;\n        si = sk_CMS_SignerInfo_value(sis, 0);\n        srcms = CMS_sign_receipt(si, signer, key, other, flags);\n        if (!srcms)\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)\n                goto end;\n            if (econtent_type)\n                CMS_set1_eContentType(cms, econtent_type);\n            if (rr_to) {\n                rr = make_receipt_request(rr_to, rr_allorfirst, rr_from);\n                if (!rr) {\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        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, NULL,\n                               e, "signer certificate");\n            if (!signer)\n                goto end;\n            key = load_key(keyfile, keyform, 0, passin, e, "signing key file");\n            if (!key)\n                goto end;\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)\n                goto end;\n            if (kparam) {\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 && !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) {\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) {\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) {\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) {\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) {\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\\n", to);\n            if (from)\n                BIO_printf(out, "From: %s\\n", from);\n            if (subject)\n                BIO_printf(out, "Subject: %s\\n", subject);\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    if (need_rand)\n        app_RAND_write_file(NULL);\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    BIO_free(rctin);\n    BIO_free(in);\n    BIO_free(indata);\n    BIO_free_all(out);\n    OPENSSL_free(passin);\n    return (ret);\n}', 'int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)\n{\n    BIO *cmsbio;\n    int ret = 0;\n    if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {\n        CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);\n        return 0;\n    }\n    SMIME_crlf_copy(data, cmsbio, flags);\n    (void)BIO_flush(cmsbio);\n    if (!CMS_dataFinal(cms, cmsbio)) {\n        CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);\n        goto err;\n    }\n    ret = 1;\n err:\n    do_free_upto(cmsbio, dcont);\n    return ret;\n}', 'BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)\n{\n    BIO *cmsbio, *cont;\n    if (icont)\n        cont = icont;\n    else\n        cont = cms_content_bio(cms);\n    if (!cont) {\n        CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);\n        return NULL;\n    }\n    switch (OBJ_obj2nid(cms->contentType)) {\n    case NID_pkcs7_data:\n        return cont;\n    case NID_pkcs7_signed:\n        cmsbio = cms_SignedData_init_bio(cms);\n        break;\n    case NID_pkcs7_digest:\n        cmsbio = cms_DigestedData_init_bio(cms);\n        break;\n#ifdef ZLIB\n    case NID_id_smime_ct_compressedData:\n        cmsbio = cms_CompressedData_init_bio(cms);\n        break;\n#endif\n    case NID_pkcs7_encrypted:\n        cmsbio = cms_EncryptedData_init_bio(cms);\n        break;\n    case NID_pkcs7_enveloped:\n        cmsbio = cms_EnvelopedData_init_bio(cms);\n        break;\n    default:\n        CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);\n        return NULL;\n    }\n    if (cmsbio)\n        return BIO_push(cmsbio, cont);\n    if (!icont)\n        BIO_free(cont);\n    return NULL;\n}', 'int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,\n                BIO *dcont, BIO *out, unsigned int flags)\n{\n    int r;\n    BIO *cont;\n    if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {\n        CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);\n        return 0;\n    }\n    if (!dcont && !check_content(cms))\n        return 0;\n    if (flags & CMS_DEBUG_DECRYPT)\n        cms->d.envelopedData->encryptedContentInfo->debug = 1;\n    else\n        cms->d.envelopedData->encryptedContentInfo->debug = 0;\n    if (!pk && !cert && !dcont && !out)\n        return 1;\n    if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))\n        return 0;\n    cont = CMS_dataInit(cms, dcont);\n    if (!cont)\n        return 0;\n    r = cms_copy_content(out, cont, flags);\n    do_free_upto(cont, dcont);\n    return r;\n}', 'BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)\n{\n    CMS_EncryptedContentInfo *ec;\n    STACK_OF(CMS_RecipientInfo) *rinfos;\n    CMS_RecipientInfo *ri;\n    int i, ok = 0;\n    BIO *ret;\n    ec = cms->d.envelopedData->encryptedContentInfo;\n    ret = cms_EncryptedContent_init_bio(ec);\n    if (!ret || !ec->cipher)\n        return ret;\n    rinfos = cms->d.envelopedData->recipientInfos;\n    for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {\n        ri = sk_CMS_RecipientInfo_value(rinfos, i);\n        if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {\n            CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,\n                   CMS_R_ERROR_SETTING_RECIPIENTINFO);\n            goto err;\n        }\n    }\n    cms_env_set_version(cms->d.envelopedData);\n    ok = 1;\n err:\n    ec->cipher = NULL;\n    OPENSSL_clear_free(ec->key, ec->keylen);\n    ec->key = NULL;\n    ec->keylen = 0;\n    if (ok)\n        return ret;\n    BIO_free(ret);\n    return NULL;\n}', 'BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)\n{\n    BIO *b;\n    EVP_CIPHER_CTX *ctx;\n    const EVP_CIPHER *ciph;\n    X509_ALGOR *calg = ec->contentEncryptionAlgorithm;\n    unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;\n    unsigned char *tkey = NULL;\n    size_t tkeylen = 0;\n    int ok = 0;\n    int enc, keep_key = 0;\n    enc = ec->cipher ? 1 : 0;\n    b = BIO_new(BIO_f_cipher());\n    if (b == NULL) {\n        CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    BIO_get_cipher_ctx(b, &ctx);\n    if (enc) {\n        ciph = ec->cipher;\n        if (ec->key)\n            ec->cipher = NULL;\n    } else {\n        ciph = EVP_get_cipherbyobj(calg->algorithm);\n        if (!ciph) {\n            CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);\n            goto err;\n        }\n    }\n    if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {\n        CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,\n               CMS_R_CIPHER_INITIALISATION_ERROR);\n        goto err;\n    }\n    if (enc) {\n        int ivlen;\n        calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));\n        ivlen = EVP_CIPHER_CTX_iv_length(ctx);\n        if (ivlen > 0) {\n            if (RAND_bytes(iv, ivlen) <= 0)\n                goto err;\n            piv = iv;\n        }\n    } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {\n        CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,\n               CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);\n        goto err;\n    }\n    tkeylen = EVP_CIPHER_CTX_key_length(ctx);\n    if (!enc || !ec->key) {\n        tkey = OPENSSL_malloc(tkeylen);\n        if (tkey == NULL) {\n            CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)\n            goto err;\n    }\n    if (!ec->key) {\n        ec->key = tkey;\n        ec->keylen = tkeylen;\n        tkey = NULL;\n        if (enc)\n            keep_key = 1;\n        else\n            ERR_clear_error();\n    }\n    if (ec->keylen != tkeylen) {\n        if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {\n            if (enc || ec->debug) {\n                CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,\n                       CMS_R_INVALID_KEY_LENGTH);\n                goto err;\n            } else {\n                OPENSSL_clear_free(ec->key, ec->keylen);\n                ec->key = tkey;\n                ec->keylen = tkeylen;\n                tkey = NULL;\n                ERR_clear_error();\n            }\n        }\n    }\n    if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {\n        CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,\n               CMS_R_CIPHER_INITIALISATION_ERROR);\n        goto err;\n    }\n    if (piv) {\n        calg->parameter = ASN1_TYPE_new();\n        if (calg->parameter == NULL) {\n            CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {\n            CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,\n                   CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);\n            goto err;\n        }\n    }\n    ok = 1;\n err:\n    if (!keep_key || !ok) {\n        OPENSSL_clear_free(ec->key, ec->keylen);\n        ec->key = NULL;\n    }\n    OPENSSL_clear_free(tkey, tkeylen);\n    if (ok)\n        return b;\n    BIO_free(b);\n    return NULL;\n}', 'int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)\n{\n    switch (ri->type) {\n    case CMS_RECIPINFO_TRANS:\n        return cms_RecipientInfo_ktri_encrypt(cms, ri);\n    case CMS_RECIPINFO_AGREE:\n        return cms_RecipientInfo_kari_encrypt(cms, ri);\n    case CMS_RECIPINFO_KEK:\n        return cms_RecipientInfo_kekri_encrypt(cms, ri);\n    case CMS_RECIPINFO_PASS:\n        return cms_RecipientInfo_pwri_crypt(cms, ri, 1);\n    default:\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,\n               CMS_R_UNSUPPORTED_RECIPIENT_TYPE);\n        return 0;\n    }\n}', 'int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,\n                                   CMS_RecipientInfo *ri)\n{\n    CMS_KeyAgreeRecipientInfo *kari;\n    CMS_EncryptedContentInfo *ec;\n    CMS_RecipientEncryptedKey *rek;\n    STACK_OF(CMS_RecipientEncryptedKey) *reks;\n    int i;\n    if (ri->type != CMS_RECIPINFO_AGREE) {\n        CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT);\n        return 0;\n    }\n    kari = ri->d.kari;\n    reks = kari->recipientEncryptedKeys;\n    ec = cms->d.envelopedData->encryptedContentInfo;\n    if (!cms_wrap_init(kari, ec->cipher))\n        return 0;\n    if (kari->originator->type == -1) {\n        CMS_OriginatorIdentifierOrKey *oik = kari->originator;\n        oik->type = CMS_OIK_PUBKEY;\n        oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);\n        if (!oik->d.originatorKey)\n            return 0;\n    }\n    if (!cms_env_asn1_ctrl(ri, 0))\n        return 0;\n    for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {\n        unsigned char *enckey;\n        size_t enckeylen;\n        rek = sk_CMS_RecipientEncryptedKey_value(reks, i);\n        if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)\n            return 0;\n        if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,\n                            kari, 1))\n            return 0;\n        ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);\n    }\n    return 1;\n}', 'static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,\n                          const unsigned char *in, size_t inlen,\n                          CMS_KeyAgreeRecipientInfo *kari, int enc)\n{\n    unsigned char kek[EVP_MAX_KEY_LENGTH];\n    size_t keklen;\n    int rv = 0;\n    unsigned char *out = NULL;\n    int outlen;\n    keklen = EVP_CIPHER_CTX_key_length(kari->ctx);\n    if (keklen > EVP_MAX_KEY_LENGTH)\n        return 0;\n    if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)\n        goto err;\n    if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))\n        goto err;\n    if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))\n        goto err;\n    out = OPENSSL_malloc(outlen);\n    if (out == NULL)\n        goto err;\n    if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))\n        goto err;\n    *pout = out;\n    *poutlen = (size_t)outlen;\n    rv = 1;\n err:\n    OPENSSL_cleanse(kek, keklen);\n    if (!rv)\n        OPENSSL_free(out);\n    EVP_CIPHER_CTX_reset(kari->ctx);\n    EVP_PKEY_CTX_free(kari->pctx);\n    kari->pctx = NULL;\n    return rv;\n}', 'int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n                     const unsigned char *in, int inl)\n{\n    if (ctx->encrypt)\n        return EVP_EncryptUpdate(ctx, out, outl, in, inl);\n    else\n        return EVP_DecryptUpdate(ctx, out, outl, in, inl);\n}', 'int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,\n                      const unsigned char *in, int inl)\n{\n    int i, j, bl;\n    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {\n        i = ctx->cipher->do_cipher(ctx, out, in, inl);\n        if (i < 0)\n            return 0;\n        else\n            *outl = i;\n        return 1;\n    }\n    if (inl <= 0) {\n        *outl = 0;\n        return inl == 0;\n    }\n    if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {\n        if (ctx->cipher->do_cipher(ctx, out, in, inl)) {\n            *outl = inl;\n            return 1;\n        } else {\n            *outl = 0;\n            return 0;\n        }\n    }\n    i = ctx->buf_len;\n    bl = ctx->cipher->block_size;\n    OPENSSL_assert(bl <= (int)sizeof(ctx->buf));\n    if (i != 0) {\n        if (i + inl < bl) {\n            memcpy(&(ctx->buf[i]), in, inl);\n            ctx->buf_len += inl;\n            *outl = 0;\n            return 1;\n        } else {\n            j = bl - i;\n            memcpy(&(ctx->buf[i]), in, j);\n            if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))\n                return 0;\n            inl -= j;\n            in += j;\n            out += bl;\n            *outl = bl;\n        }\n    } else\n        *outl = 0;\n    i = inl & (bl - 1);\n    inl -= i;\n    if (inl > 0) {\n        if (!ctx->cipher->do_cipher(ctx, out, in, inl))\n            return 0;\n        *outl += inl;\n    }\n    if (i != 0)\n        memcpy(ctx->buf, &(in[inl]), i);\n    ctx->buf_len = i;\n    return 1;\n}']