static int check_chain_extensions(X509_STORE_CTX *ctx)
{
    int i, ok = 0, must_be_ca, plen = 0;
    X509 *x;
    int (*cb) (int xok, X509_STORE_CTX *xctx);
    int proxy_path_length = 0;
    int purpose;
    int allow_proxy_certs;
    cb = ctx->verify_cb;
    must_be_ca = -1;
    if (ctx->parent) {
        allow_proxy_certs = 0;
        purpose = X509_PURPOSE_CRL_SIGN;
    } else {
        allow_proxy_certs =
            ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
        if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
            allow_proxy_certs = 1;
        purpose = ctx->param->purpose;
    }
    for (i = 0; i == 0 || i < ctx->num_untrusted; i++) {
        int ret;
        x = sk_X509_value(ctx->chain, i);
        if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
            && (x->ex_flags & EXFLAG_CRITICAL)) {
            ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
            ctx->error_depth = i;
            ctx->current_cert = x;
            ok = cb(0, ctx);
            if (!ok)
                goto end;
        }
        if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
            ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
            ctx->error_depth = i;
            ctx->current_cert = x;
            ok = cb(0, ctx);
            if (!ok)
                goto end;
        }
        ret = X509_check_ca(x);
        switch (must_be_ca) {
        case -1:
            if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
                && (ret != 1) && (ret != 0)) {
                ret = 0;
                ctx->error = X509_V_ERR_INVALID_CA;
            } else
                ret = 1;
            break;
        case 0:
            if (ret != 0) {
                ret = 0;
                ctx->error = X509_V_ERR_INVALID_NON_CA;
            } else
                ret = 1;
            break;
        default:
            if ((ret == 0)
                || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
                    && (ret != 1))) {
                ret = 0;
                ctx->error = X509_V_ERR_INVALID_CA;
            } else
                ret = 1;
            break;
        }
        if (ret == 0) {
            ctx->error_depth = i;
            ctx->current_cert = x;
            ok = cb(0, ctx);
            if (!ok)
                goto end;
        }
        if (ctx->param->purpose > 0) {
            ret = X509_check_purpose(x, purpose, must_be_ca > 0);
            if ((ret == 0)
                || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
                    && (ret != 1))) {
                ctx->error = X509_V_ERR_INVALID_PURPOSE;
                ctx->error_depth = i;
                ctx->current_cert = x;
                ok = cb(0, ctx);
                if (!ok)
                    goto end;
            }
        }
        if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
            && (x->ex_pathlen != -1)
            && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
            ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
            ctx->error_depth = i;
            ctx->current_cert = x;
            ok = cb(0, ctx);
            if (!ok)
                goto end;
        }
        if (!(x->ex_flags & EXFLAG_SI))
            plen++;
        if (x->ex_flags & EXFLAG_PROXY) {
            if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
                ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
                ctx->error_depth = i;
                ctx->current_cert = x;
                ok = cb(0, ctx);
                if (!ok)
                    goto end;
            }
            proxy_path_length++;
            must_be_ca = 0;
        } else
            must_be_ca = 1;
    }
    ok = 1;
 end:
    return ok;
}
['static int check_chain_extensions(X509_STORE_CTX *ctx)\n{\n    int i, ok = 0, must_be_ca, plen = 0;\n    X509 *x;\n    int (*cb) (int xok, X509_STORE_CTX *xctx);\n    int proxy_path_length = 0;\n    int purpose;\n    int allow_proxy_certs;\n    cb = ctx->verify_cb;\n    must_be_ca = -1;\n    if (ctx->parent) {\n        allow_proxy_certs = 0;\n        purpose = X509_PURPOSE_CRL_SIGN;\n    } else {\n        allow_proxy_certs =\n            ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);\n        if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))\n            allow_proxy_certs = 1;\n        purpose = ctx->param->purpose;\n    }\n    for (i = 0; i == 0 || i < ctx->num_untrusted; i++) {\n        int ret;\n        x = sk_X509_value(ctx->chain, i);\n        if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)\n            && (x->ex_flags & EXFLAG_CRITICAL)) {\n            ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;\n            ctx->error_depth = i;\n            ctx->current_cert = x;\n            ok = cb(0, ctx);\n            if (!ok)\n                goto end;\n        }\n        if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {\n            ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;\n            ctx->error_depth = i;\n            ctx->current_cert = x;\n            ok = cb(0, ctx);\n            if (!ok)\n                goto end;\n        }\n        ret = X509_check_ca(x);\n        switch (must_be_ca) {\n        case -1:\n            if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n                && (ret != 1) && (ret != 0)) {\n                ret = 0;\n                ctx->error = X509_V_ERR_INVALID_CA;\n            } else\n                ret = 1;\n            break;\n        case 0:\n            if (ret != 0) {\n                ret = 0;\n                ctx->error = X509_V_ERR_INVALID_NON_CA;\n            } else\n                ret = 1;\n            break;\n        default:\n            if ((ret == 0)\n                || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n                    && (ret != 1))) {\n                ret = 0;\n                ctx->error = X509_V_ERR_INVALID_CA;\n            } else\n                ret = 1;\n            break;\n        }\n        if (ret == 0) {\n            ctx->error_depth = i;\n            ctx->current_cert = x;\n            ok = cb(0, ctx);\n            if (!ok)\n                goto end;\n        }\n        if (ctx->param->purpose > 0) {\n            ret = X509_check_purpose(x, purpose, must_be_ca > 0);\n            if ((ret == 0)\n                || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)\n                    && (ret != 1))) {\n                ctx->error = X509_V_ERR_INVALID_PURPOSE;\n                ctx->error_depth = i;\n                ctx->current_cert = x;\n                ok = cb(0, ctx);\n                if (!ok)\n                    goto end;\n            }\n        }\n        if ((i > 1) && !(x->ex_flags & EXFLAG_SI)\n            && (x->ex_pathlen != -1)\n            && (plen > (x->ex_pathlen + proxy_path_length + 1))) {\n            ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;\n            ctx->error_depth = i;\n            ctx->current_cert = x;\n            ok = cb(0, ctx);\n            if (!ok)\n                goto end;\n        }\n        if (!(x->ex_flags & EXFLAG_SI))\n            plen++;\n        if (x->ex_flags & EXFLAG_PROXY) {\n            if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {\n                ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;\n                ctx->error_depth = i;\n                ctx->current_cert = x;\n                ok = cb(0, ctx);\n                if (!ok)\n                    goto end;\n            }\n            proxy_path_length++;\n            must_be_ca = 0;\n        } else\n            must_be_ca = 1;\n    }\n    ok = 1;\n end:\n    return ok;\n}', 'DEFINE_STACK_OF(X509)', 'void *sk_value(const _STACK *st, int i)\n{\n    if (!st || (i < 0) || (i >= st->num))\n        return NULL;\n    return st->data[i];\n}', 'int X509_check_ca(X509 *x)\n{\n    if (!(x->ex_flags & EXFLAG_SET)) {\n        CRYPTO_w_lock(CRYPTO_LOCK_X509);\n        x509v3_cache_extensions(x);\n        CRYPTO_w_unlock(CRYPTO_LOCK_X509);\n    }\n    return check_ca(x);\n}']