如何从SSL_CTX获取证书?

时间:2016-08-11 12:43:21

标签: ssl x509

我使用SSL_CTX_use_certificate( SSL_CTX *ctx, X509 *x )将证书存储到SSL_CTX *ctx

它存储在SSL_CTX

ctx->cert->key->x509对象中

是否有任何API可以从SSL_CTX取回?

2 个答案:

答案 0 :(得分:0)

X509 *SSL_get_peer_certificate(const SSL *ssl); SSL_get_peer_certificate()返回指向对等方提供的X509证书的指针。

X509 *SSL_get_certificate(const SSL *ssl);该函数返回一个X.509类型指针,指向SSL结构中加载的证书。

上述定义与您在答案中提到的一样简单

X509 *SSL_get_certificate(const SSL *s)
{
    if (s->cert != NULL)
        return(s->cert->key->x509);
    else
        return(NULL);
}

以下是参考链接以获取更多信息 https://www.openssl.org/docs/manmaster/ssl/ssl.html

答案 1 :(得分:0)

旧问题,但由于我花了几个小时才能解决此问题,请在此处发布以供参考。

以下是从SSL_CTX *获取证书并检查证书的剩余有效天数(秒)的示例:

X509 *x509 = SSL_CTX_get0_certificate(ssl_ctx);
const ASN1_TIME* notAfter = X509_getm_notAfter(x509);
int remaining_days=0, remaining_seconds = 0;
int  result = ASN1_TIME_diff(&remaining_days, &remaining_seconds, NULL, notAfter);
std::cout << "remaining days: " << remaining_days << std::endl;
std::cout << "remaining seconds: " << remaining_seconds << std::endl;

它正在使用OpenSSL 1.1