static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al)
{
    int i, chain_count;
    X509 *x;
    STACK_OF(X509) *extra_certs;
    STACK_OF(X509) *chain = NULL;
    X509_STORE *chain_store;
    int tmpal = SSL_AD_INTERNAL_ERROR;
    if (cpk == NULL || cpk->x509 == NULL)
        return 1;
    x = cpk->x509;
    if (cpk->chain != NULL)
        extra_certs = cpk->chain;
    else
        extra_certs = s->ctx->extra_certs;
    if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
        chain_store = NULL;
    else if (s->cert->chain_store)
        chain_store = s->cert->chain_store;
    else
        chain_store = s->ctx->cert_store;
    if (chain_store != NULL) {
        X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
        if (xs_ctx == NULL) {
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
            goto err;
        }
        if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
            X509_STORE_CTX_free(xs_ctx);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
            goto err;
        }
        (void)X509_verify_cert(xs_ctx);
        ERR_clear_error();
        chain = X509_STORE_CTX_get0_chain(xs_ctx);
        i = ssl_security_cert_chain(s, chain, NULL, 0);
        if (i != 1) {
#if 0
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
#endif
            X509_STORE_CTX_free(xs_ctx);
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
            goto err;
        }
        chain_count = sk_X509_num(chain);
        for (i = 0; i < chain_count; i++) {
            x = sk_X509_value(chain, i);
            if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) {
                X509_STORE_CTX_free(xs_ctx);
                goto err;
            }
        }
        X509_STORE_CTX_free(xs_ctx);
    } else {
        i = ssl_security_cert_chain(s, extra_certs, x, 0);
        if (i != 1) {
            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
            goto err;
        }
        if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal))
            goto err;
        for (i = 0; i < sk_X509_num(extra_certs); i++) {
            x = sk_X509_value(extra_certs, i);
            if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal))
                goto err;
        }
    }
    return 1;
 err:
    *al = tmpal;
    return 0;
}
['static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk, int *al)\n{\n    int i, chain_count;\n    X509 *x;\n    STACK_OF(X509) *extra_certs;\n    STACK_OF(X509) *chain = NULL;\n    X509_STORE *chain_store;\n    int tmpal = SSL_AD_INTERNAL_ERROR;\n    if (cpk == NULL || cpk->x509 == NULL)\n        return 1;\n    x = cpk->x509;\n    if (cpk->chain != NULL)\n        extra_certs = cpk->chain;\n    else\n        extra_certs = s->ctx->extra_certs;\n    if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)\n        chain_store = NULL;\n    else if (s->cert->chain_store)\n        chain_store = s->cert->chain_store;\n    else\n        chain_store = s->ctx->cert_store;\n    if (chain_store != NULL) {\n        X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();\n        if (xs_ctx == NULL) {\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);\n            goto err;\n        }\n        if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {\n            X509_STORE_CTX_free(xs_ctx);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);\n            goto err;\n        }\n        (void)X509_verify_cert(xs_ctx);\n        ERR_clear_error();\n        chain = X509_STORE_CTX_get0_chain(xs_ctx);\n        i = ssl_security_cert_chain(s, chain, NULL, 0);\n        if (i != 1) {\n#if 0\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);\n#endif\n            X509_STORE_CTX_free(xs_ctx);\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n            goto err;\n        }\n        chain_count = sk_X509_num(chain);\n        for (i = 0; i < chain_count; i++) {\n            x = sk_X509_value(chain, i);\n            if (!ssl_add_cert_to_wpacket(s, pkt, x, i, &tmpal)) {\n                X509_STORE_CTX_free(xs_ctx);\n                goto err;\n            }\n        }\n        X509_STORE_CTX_free(xs_ctx);\n    } else {\n        i = ssl_security_cert_chain(s, extra_certs, x, 0);\n        if (i != 1) {\n            SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);\n            goto err;\n        }\n        if (!ssl_add_cert_to_wpacket(s, pkt, x, 0, &tmpal))\n            goto err;\n        for (i = 0; i < sk_X509_num(extra_certs); i++) {\n            x = sk_X509_value(extra_certs, i);\n            if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1, &tmpal))\n                goto err;\n        }\n    }\n    return 1;\n err:\n    *al = tmpal;\n    return 0;\n}', 'X509_STORE_CTX *X509_STORE_CTX_new(void)\n{\n    X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));\n    if (ctx == NULL) {\n        X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);\n        return NULL;\n    }\n    return ctx;\n}', 'void *CRYPTO_zalloc(size_t num, const char *file, int line)\n{\n    void *ret = CRYPTO_malloc(num, file, line);\n    FAILTEST();\n    if (ret != NULL)\n        memset(ret, 0, num);\n    return ret;\n}', 'void *CRYPTO_malloc(size_t num, const char *file, int line)\n{\n    void *ret = NULL;\n    if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)\n        return malloc_impl(num, file, line);\n    if (num == 0)\n        return NULL;\n    FAILTEST();\n    allow_customize = 0;\n#ifndef OPENSSL_NO_CRYPTO_MDEBUG\n    if (call_malloc_debug) {\n        CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);\n        ret = malloc(num);\n        CRYPTO_mem_debug_malloc(ret, num, 1, file, line);\n    } else {\n        ret = malloc(num);\n    }\n#else\n    (void)(file); (void)(line);\n    ret = malloc(num);\n#endif\n    return ret;\n}', 'void X509_STORE_CTX_free(X509_STORE_CTX *ctx)\n{\n    if (ctx == NULL)\n        return;\n    X509_STORE_CTX_cleanup(ctx);\n    OPENSSL_free(ctx);\n}', 'void CRYPTO_free(void *str, const char *file, int line)\n{\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}']