c - 使用 openssl 库验证自签名/过期证书不会返回错误

标签 c openssl

我正在尝试使用 openssl 库在 C 中编写证书验证函数。由于我正在验证的证书是自签名的并且已过期,因此我希望 X509_verify_cert() 返回错误(返回值为 1 并且 store_ctx->error 设置为 X509_V_OK)。 'openssl verify my_pem_cert_file' 输出:

error 18 at 0 depth lookup:self signed certificate
error 10 at 0 depth lookup:certificate has expired

我做错了什么?这是我的代码:

static int cert_verify_callback(int ok, X509_STORE_CTX *ctx)
{
    /* Tolerate self-signed certificate */
    if (X509_STORE_CTX_get_error(ctx) == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) {
        return 1;
    }

    /* Otherwise don't override */
    return ok;
}

int cert_validate(const char* certFileName) 
{
    BIO *pBio = NULL;
    X509 *pX509 = NULL;
    X509 *CA = NULL;
    X509_STORE *cert_store = NULL;
    X509_STORE_CTX *store_ctx = NULL;
    STACK_OF(X509) *stack_of_x509 = NULL;
    time_t check_time;
    int store_ctx_error;
    int store_ctx_error_depth;


    pBio = BIO_new( BIO_s_file_internal() );
    if(pBio == NULL)
        /* error handling */

    if(BIO_read_filename(pBio, certFileName) <= 0)
        /* error handling */

    pX509 = PEM_read_bio_X509(pBio, NULL, NULL, NULL);
    if (pX509 == NULL)
        /* error handling */

    if( (cert_store= X509_STORE_new()) == NULL)
        /* error handling */

    if( (store_ctx= X509_STORE_CTX_new()) == NULL)
        /* error handling */

    /* edit1: this was wrong: don't add the certificate being verified to the trusted cert list */
    /* if( !X509_STORE_add_cert(cert_store, pX509) ) */
        /* error handling */

    if( !X509_STORE_CTX_init(store_ctx, cert_store, CA, stack_of_x509) )
        /* error handling */

    X509_STORE_CTX_set_cert(store_ctx, pX509);

    /* edit1: this was missing: set the verify time in order to check the certificate for expiry */
    time(&check_time);
    X509_STORE_CTX_set_time(store_ctx, 0, check_time);
    X509_STORE_CTX_set_flags(store_ctx, X509_V_FLAG_USE_CHECK_TIME);

    /* edit1: add callback function for ignoring self-signed error
     * now, I'd like the validation to fail because of the expiry */
    X509_STORE_set_verify_cb_func(store_ctx, cert_verify_callback);

    switch( X509_verify_cert(store_ctx) ) {
        /* the certificate is valid */
        case 1:
            printf("The certificate is valid\n");

            break;

        /* the certificate cannot be validated */
        case -1:
        case 0:
            printf("The certificate is not valid\n");

            store_ctx_error= X509_STORE_CTX_get_error(store_ctx);
            store_ctx_error_depth= X509_STORE_CTX_get_error_depth(store_ctx);
            printf("Error %d at %d depth: %s\n", store_ctx_error, store_ctx_error_depth, X509_verify_cert_error_string(store_ctx->error));

        default:
            break;
    }

    /* free data ... */
}

在验证自签名和过期的证书时,我的函数打印: 0 深度错误 0:好的

最佳答案

函数X509_STORE_add_cert()添加对应的证书作为可信证书进行验证,所以这一行:

X509_STORE_add_cert(cert_store, pX509)

表示您的 pX509 证书可用于验证 - 但这是您要测试的证书,因此这就是自签名证书通过验证的原因。

您也没有设置验证时间 - 这就是未检测到过期证书的原因。使用 X509_STORE_CTX_set_time() 设置验证时间。

关于c - 使用 openssl 库验证自签名/过期证书不会返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13612343/

相关文章:

c - 如何使用 openssl 通过本地信任库验证 PEM 中的证书?

c - 如何在C中打印数字符号?

选择中间语言

你能具体告诉我这个程序是做什么的吗?

python - 如何从 x509 证书中提取签名

ruby-on-rails - OpenSSL 导致 Windows 上的 Rails 启动时间非常慢

delphi - 印地10 + OpenSSL : send email code stopped working on Windows 8

ssl - CURL:(35)Ubuntu 服务器的 SSL 连接错误

C - 同时接收和处理来自 unix 套接字的数据

c - 如何在C中复制矩阵?