#ifndef LIBCURL_CERTIINFO_HPP
#define LIBCURL_CERTIINFO_HPP
#include<stdio.h>
#include<curl/curl.h>
static size_t wrfun(void *ptr,size_t size,size_t nmemb,void *stream){
(void)stream;
(void)ptr;
return size*nmemb;
}
int test_certinfo(){
CURL *pCurl;
CURLcode res;
curl_global_init(CURL_GLOBAL_ALL);
pCurl = curl_easy_init();
if(pCurl){
curl_easy_setopt(pCurl,CURLOPT_URL,"https://www.example.com/");
curl_easy_setopt(pCurl,CURLOPT_WRITEFUNCTION,wrfun);
curl_easy_setopt(pCurl,CURLOPT_SSL_VERIFYPEER,0L);
curl_easy_setopt(pCurl,CURLOPT_SSL_VERIFYHOST,0L);
curl_easy_setopt(pCurl,CURLOPT_VERBOSE,1L);
curl_easy_setopt(pCurl,CURLOPT_CERTINFO,1L);
res = curl_easy_perform(pCurl);
if(!res){
struct curl_certinfo *pCertinfo;
res = curl_easy_getinfo(pCurl,CURLINFO_CERTINFO,&pCertinfo);
if(!res && pCertinfo){
printf("%d certs\n",pCertinfo->num_of_certs);
for(int i = 0;i<pCertinfo->num_of_certs;i++){
struct curl_slist *pSlist;
for(pSlist = pCertinfo->certinfo[i];pSlist->data != NULL;pSlist->next);
printf("%s\n",pSlist->data);
}
}
}
}
curl_easy_cleanup(pCurl);
}
#endif // LIBCURL_CERTIINFO_HPP
编译运行结果
* Trying 93.184.216.34:443...
* Connected to www.example.com (93.184.216.34) port 443 (#0)
* ALPN, offering http/1.1
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=US; ST=California; L=Los Angeles; O=Internet Corporation for Assigned Names and Numbers; OU=Technology; CN=www.example.org
* start date: Nov 28 00:00:00 2018 GMT
* expire date: Dec 2 12:00:00 2020 GMT
* issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
* SSL certificate verify result: self signed certificate in certificate chain (19), continuing anyway.
> GET / HTTP/1.1
Host: www.example.com
Accept: */*
* old SSL session ID is stale, removing
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Age: 476150
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Fri, 05 Jun 2020 12:42:23 GMT
< Etag: "3147526947"
< Expires: Fri, 12 Jun 2020 12:42:23 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
< Server: ECS (sjc/4E44)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1256
<
* Connection #0 to host www.example.com left intact
3 certs