openssl certificate date check

/*
 *  Only suitable for PEM format certificate file, and not apply
 *  for DER certificate file.
 *
 * */

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <openssl/bio.h>
#include <openssl/pem.h>

int         default_days = 9;

static int
get_cert_time(char *cert, char *date_buf, int date_buf_len)
{
    BIO         *b;
    X509        *x509cert;
    ASN1_TIME   *get_date;  //save valid time

    if (cert == NULL) {
        printf("[%s: %d] Certificate file(\"%s\") is NULL!\n", 
                __FUNCTION__, __LINE__,  cert);
        return -1;
    }

    /* Only suitable for PEM  format digital certificate file. */
    b = BIO_new_file(cert, "r");
    if (b == NULL) {
        printf("[%s: %d] bio is NULL!\n", __FUNCTION__, __LINE__);
        return -1;
    }

    x509cert = PEM_read_bio_X509(b, NULL, NULL, NULL);
    if (x509cert ==  NULL) {
        printf("[%s: %d] read bio failed!\n", __FUNCTION__, __LINE__);
        return -1;
    }

    get_date = X509_get_notBefore(x509cert);
    printf("Not Before: %s\n", get_date->data);

    //get_date = X509_get_notAfter(x509cert);
    //printf("Not After: %s\n", get_date->data);
    if (get_date->length > date_buf_len) {
        printf("[%s: %d] date length too long!\n",
                __FUNCTION__, __LINE__);
        return -1;
    }

    strncpy(date_buf, (char *)get_date->data, date_buf_len);

    X509_free(x509cert);
    return 0;
}

static int
update_cert_process(time_t current_time, char *cert_date)
{
    time_t      update_time = 0;
    struct tm   *gmt;
    char        *date_buf = NULL;
    char        *date_buf2 = NULL;
    int         result = -1;

    update_time = current_time - default_days * 24 * 60 * 60;

    printf("Before %d day: %ld\n", default_days, (long)update_time);
    gmt = gmtime(&update_time);

    date_buf = (char *)malloc(15 * sizeof(char) + 1);
    if (date_buf == NULL) {
        printf("[%s:%d] malloc failed!\n", __FUNCTION__, __LINE__);
        return -1;
    }

    /* Print current date. */
    snprintf(date_buf, 16,"%04d%02d%02d%02d%02d%02dZ", gmt->tm_year + 1900, gmt->tm_mon+1,
            gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);

    date_buf[15] = '\0';

    printf("Before %d days: %s\n", default_days, date_buf);

    /* Truncate "20", for example "2017", "17" is leaved. */
    date_buf2 = (char *)malloc(15 * sizeof(char) + 1);
    if (date_buf2 == NULL) {
        printf("[%s:%d] malloc failed!\n", __FUNCTION__, __LINE__);
        return -1;
    }

    strncpy(date_buf2, date_buf + 2, 16);

    printf("date_buf2: %s\n", date_buf2);

    result = strncmp(date_buf2, cert_date, strlen(cert_date));

    printf("date_buf2: %s,\ncert_date: %s,\nresult: %d\n",
            date_buf2, cert_date, result);
    /* 3. Judge whether to update the certificate file. */
    if (result > 0) {
        printf("It's time to updte the certificate file!\n");
    } else {
        printf("Don't update the certificate file!\n");
    }

    free(date_buf);
    free(date_buf2);
    return 0;
}

int 
main(int argc, char *const argv[])
{

    time_t      t;
    struct tm   *gmt;

    char        *date_buf = NULL;
    char        *date_buf2 = NULL;

    char        *cert_date = NULL;
    int         cert_date_len = -1;

    int         update_result = -1;

    /* 1. Obtain certificate file date. */
    cert_date_len = 13; /* Not Before: 171228063543Z.*/
    cert_date = (char *)malloc(cert_date_len * sizeof(char) + 1);

    if (get_cert_time(argv[1], cert_date, cert_date_len + 1) == -1) {
        printf("[%s: %d]: Obtain certificate date failed!\n",
                __FUNCTION__, __LINE__);
        return -1;
    }

    printf("certficate date: %s\n", cert_date);

    /* 2. Obtain current date. */
    t = time(NULL);
    printf("Current time: %ld\n", (long)t);

    gmt = gmtime(&t);

    date_buf = (char *)malloc(15 * sizeof(char) + 1);
    if (date_buf == NULL) {
        printf("[%s:%d] malloc failed!\n", __FUNCTION__, __LINE__);
        return -1;
    }

    /* Print current date. */
    snprintf(date_buf, 16,"%04d%02d%02d%02d%02d%02dZ", gmt->tm_year + 1900, gmt->tm_mon+1,
            gmt->tm_mday, gmt->tm_hour, gmt->tm_min, gmt->tm_sec);

    date_buf[15] = '\0';

    printf("Current date: %s\n", date_buf);

    /* Truncate "20", for example "2017", "17" is leaved. */
    date_buf2 = (char *)malloc(15 * sizeof(char) + 1);
    if (date_buf2 == NULL) {
        printf("[%s:%d] malloc failed!\n", __FUNCTION__, __LINE__);
        return -1;
    }

    strncpy(date_buf2, date_buf + 2, 15);

    printf("date_buf2: %s\n", date_buf2);

    update_result = strncmp(date_buf2, cert_date, strlen(cert_date));

    printf("date_buf2: %s,\ncert_date: %s,\nresult: %d\n",
            date_buf2, cert_date, update_result);

    /* 3. Judge whether to update the certificate file. */
    if (update_result > 0) {

        printf("The certificate file is valid!\n");

        if (update_cert_process(t, cert_date) == -1) {
            printf("[%s : %d] Update certificate failed!\n",
                    __FUNCTION__, __LINE__);
        } else {
            printf("Update certificate successfully!\n");
        }

    } else {
        printf("The certificate file is invalid!\n");
    }

    free(date_buf);
    free(date_buf2);
    free(cert_date);

    printf("GMT is: %s", asctime(gmt));

    return 0;
}
configure: error: openssl check failed 是一个在编译软件时出现的错误提示,意思是 OpenSSL 检查失败。OpenSSL 是一个用于进行加密通信的开放源代码工具包,它包含了 SSL 和 TLS 协议的实现,广泛应用于网络通信和安全领域。 当在编译软件时出现这个错误,通常是由于没有正确配置 OpenSSL 或者缺少必要的依赖库导致的。 为了解决这个错误,我们可以采取以下步骤: 1. 确认已经安装了 OpenSSL:可以执行命令 openssl version 来检查 OpenSSL 是否已经正确安装并且版本是正确的。如果没有安装 OpenSSL,可以尝试执行命令 sudo apt-get install openssl(或者适用于你的操作系统的相应安装命令)进行安装。 2. 确认 OpenSSL 的安装路径:有时候编译软件时可能无法自动找到 OpenSSL 的安装路径,我们需要手动指定 OpenSSL 的安装路径。可以使用命令 locate openssl 或者 find / -name "openssl" 来查找 openssl 的安装路径,然后在编译软件时使用参数 --with-openssl=/path/to/openssl 指定路径。 3. 确认所有必要的依赖库都已安装:在编译软件时可能还需要依赖其他的库,例如 libssl-dev、libcrypto-dev 等,需要确保这些依赖库都已经正确地安装到系统中。可以使用命令 sudo apt-get install libssl-dev 等来安装缺失的依赖库。 4. 如果上述步骤都没有解决问题,可能是由于 OpenSSL 版本不兼容或者编译配置有误导致的。这种情况下建议查看编译软件的文档、GitHub 项目页面或者谷歌搜索以获取更多关于如何解决 OpenSSL 配置错误的帮助。 总之,configure: error: openssl check failed 错误提示意味着 OpenSSL 检查失败,我们可以检查 OpenSSL 是否正确安装、指定 OpenSSL 的安装路径、安装缺失的依赖库等方法来解决该错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值