static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest,
                        X509 *x, X509 *xca, EVP_PKEY *pkey,
                        STACK_OF(OPENSSL_STRING) *sigopts,
                        const char *serialfile, int create,
                        int days, int clrext, CONF *conf, const char *section,
                        ASN1_INTEGER *sno, int reqfile, int preserve_dates)
{
    int ret = 0;
    ASN1_INTEGER *bs = NULL;
    X509_STORE_CTX *xsc = NULL;
    EVP_PKEY *upkey;
    upkey = X509_get0_pubkey(xca);
    if (upkey == NULL) {
        BIO_printf(bio_err, "Error obtaining CA X509 public key\n");
        goto end;
    }
    EVP_PKEY_copy_parameters(upkey, pkey);
    xsc = X509_STORE_CTX_new();
    if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, x, NULL)) {
        BIO_printf(bio_err, "Error initialising X509 store\n");
        goto end;
    }
    if (sno)
        bs = sno;
    else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)
        goto end;
    X509_STORE_CTX_set_cert(xsc, x);
    X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);
    if (!reqfile && X509_verify_cert(xsc) <= 0)
        goto end;
    if (!X509_check_private_key(xca, pkey)) {
        BIO_printf(bio_err,
                   "CA certificate and CA private key do not match\n");
        goto end;
    }
    if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))
        goto end;
    if (!X509_set_serialNumber(x, bs))
        goto end;
    if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))
        goto end;
    if (clrext) {
        while (X509_get_ext_count(x) > 0)
            X509_delete_ext(x, 0);
    }
    if (conf != NULL) {
        X509V3_CTX ctx2;
        X509_set_version(x, 2);
        X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
        X509V3_set_nconf(&ctx2, conf);
        if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))
            goto end;
    }
    if (!do_X509_sign(x, pkey, digest, sigopts))
        goto end;
    ret = 1;
 end:
    X509_STORE_CTX_free(xsc);
    if (!ret)
        ERR_print_errors(bio_err);
    if (!sno)
        ASN1_INTEGER_free(bs);
    return ret;
}
['static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest,\n                        X509 *x, X509 *xca, EVP_PKEY *pkey,\n                        STACK_OF(OPENSSL_STRING) *sigopts,\n                        const char *serialfile, int create,\n                        int days, int clrext, CONF *conf, const char *section,\n                        ASN1_INTEGER *sno, int reqfile, int preserve_dates)\n{\n    int ret = 0;\n    ASN1_INTEGER *bs = NULL;\n    X509_STORE_CTX *xsc = NULL;\n    EVP_PKEY *upkey;\n    upkey = X509_get0_pubkey(xca);\n    if (upkey == NULL) {\n        BIO_printf(bio_err, "Error obtaining CA X509 public key\\n");\n        goto end;\n    }\n    EVP_PKEY_copy_parameters(upkey, pkey);\n    xsc = X509_STORE_CTX_new();\n    if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, x, NULL)) {\n        BIO_printf(bio_err, "Error initialising X509 store\\n");\n        goto end;\n    }\n    if (sno)\n        bs = sno;\n    else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL)\n        goto end;\n    X509_STORE_CTX_set_cert(xsc, x);\n    X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE);\n    if (!reqfile && X509_verify_cert(xsc) <= 0)\n        goto end;\n    if (!X509_check_private_key(xca, pkey)) {\n        BIO_printf(bio_err,\n                   "CA certificate and CA private key do not match\\n");\n        goto end;\n    }\n    if (!X509_set_issuer_name(x, X509_get_subject_name(xca)))\n        goto end;\n    if (!X509_set_serialNumber(x, bs))\n        goto end;\n    if (!preserve_dates && !set_cert_times(x, NULL, NULL, days))\n        goto end;\n    if (clrext) {\n        while (X509_get_ext_count(x) > 0)\n            X509_delete_ext(x, 0);\n    }\n    if (conf != NULL) {\n        X509V3_CTX ctx2;\n        X509_set_version(x, 2);\n        X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);\n        X509V3_set_nconf(&ctx2, conf);\n        if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x))\n            goto end;\n    }\n    if (!do_X509_sign(x, pkey, digest, sigopts))\n        goto end;\n    ret = 1;\n end:\n    X509_STORE_CTX_free(xsc);\n    if (!ret)\n        ERR_print_errors(bio_err);\n    if (!sno)\n        ASN1_INTEGER_free(bs);\n    return ret;\n}', 'EVP_PKEY *X509_get0_pubkey(const X509 *x)\n{\n    if (x == NULL)\n        return NULL;\n    return X509_PUBKEY_get0(x->cert_info.key);\n}', 'EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)\n{\n    EVP_PKEY *ret = NULL;\n    if (key == NULL || key->public_key == NULL)\n        return NULL;\n    if (key->pkey != NULL)\n        return key->pkey;\n    x509_pubkey_decode(&ret, key);\n    if (ret != NULL) {\n        X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);\n        EVP_PKEY_free(ret);\n    }\n    return NULL;\n}', 'int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)\n{\n    if (to->type == EVP_PKEY_NONE) {\n        if (EVP_PKEY_set_type(to, from->type) == 0)\n            return 0;\n    } else if (to->type != from->type) {\n        EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);\n        goto err;\n    }\n    if (EVP_PKEY_missing_parameters(from)) {\n        EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);\n        goto err;\n    }\n    if (!EVP_PKEY_missing_parameters(to)) {\n        if (EVP_PKEY_cmp_parameters(to, from) == 1)\n            return 1;\n        EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);\n        return 0;\n    }\n    if (from->ameth && from->ameth->param_copy)\n        return from->ameth->param_copy(to, from);\n err:\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    INCREMENT(malloc_count);\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_set_cert(X509_STORE_CTX *ctx, X509 *x)\n{\n    ctx->cert = x;\n}', 'void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)\n{\n    X509_VERIFY_PARAM_set_flags(ctx->param, flags);\n}', 'int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags)\n{\n    param->flags |= flags;\n    if (flags & X509_V_FLAG_POLICY_MASK)\n        param->flags |= X509_V_FLAG_POLICY_CHECK;\n    return 1;\n}', 'int X509_verify_cert(X509_STORE_CTX *ctx)\n{\n    SSL_DANE *dane = ctx->dane;\n    int ret;\n    if (ctx->cert == NULL) {\n        X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);\n        ctx->error = X509_V_ERR_INVALID_CALL;\n        return -1;\n    }\n    if (ctx->chain != NULL) {\n        X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n        ctx->error = X509_V_ERR_INVALID_CALL;\n        return -1;\n    }\n    if (((ctx->chain = sk_X509_new_null()) == NULL) ||\n        (!sk_X509_push(ctx->chain, ctx->cert))) {\n        X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);\n        ctx->error = X509_V_ERR_OUT_OF_MEM;\n        return -1;\n    }\n    X509_up_ref(ctx->cert);\n    ctx->num_untrusted = 1;\n    if (!check_key_level(ctx, ctx->cert) &&\n        !verify_cb_cert(ctx, ctx->cert, 0, X509_V_ERR_EE_KEY_TOO_SMALL))\n        return 0;\n    if (DANETLS_ENABLED(dane))\n        ret = dane_verify(ctx);\n    else\n        ret = verify_chain(ctx);\n    if (ret <= 0 && ctx->error == X509_V_OK)\n        ctx->error = X509_V_ERR_UNSPECIFIED;\n    return ret;\n}', 'DEFINE_STACK_OF(X509)', 'OPENSSL_STACK *OPENSSL_sk_new_null(void)\n{\n    return OPENSSL_sk_new_reserve(NULL, 0);\n}', '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}', '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    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}']