Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8915855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:05:17+00:00 2026-06-15T05:05:17+00:00

I am trying to write a certificate validation function in C using the openssl

  • 0

I am trying to write a certificate validation function in C using the openssl library. Since the certificate I am validating is self-signed and expired, I expect the X509_verify_cert() to return error (the return value is 1 and the store_ctx->error is set to X509_V_OK instead). ‘openssl verify my_pem_cert_file‘ outputs:

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

What I am doing wrong? Here is my code:

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 ... */
}

When validating the self-signed and expired certificate, my function prints:
Error 0 at 0 depth: ok

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:05:18+00:00Added an answer on June 15, 2026 at 5:05 am

    The function X509_STORE_add_cert() adds the corresponding certificate as a trusted certificate for verification, so this line:

    X509_STORE_add_cert(cert_store, pX509)
    

    says that your pX509 certificate is trusted for verification – but that’s the certificate you want to test, so that’s why a self-signed certificate is passing verification.

    You also aren’t setting a verification time – that’s why an expired certificate is not being detected. Set the verification time with X509_STORE_CTX_set_time().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a self-signed SSL certificate using OpenSSL (the version that
I am trying write a function that generates simulated data but if the simulated
Trying to write a function to see how often an object exists and give
I'm trying to write a SSL-based async server using Boost ASIO example code from
I am trying to encrypt database columns using a certificate and a symmetric key.
I am trying to write an SSL client that sends mail using the javax.mail
I'm trying to write a reverse proxy in node.js using express, and it works
I am trying to write a WS client using Apache CXF framework. Remote WS
I'm trying write a Ruby script that checks if user credentials are valid using
So I'm trying to write a suite of tests using Selenium WebDriver in Ruby

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.