Adding self-signed SSL certificate for libcurl

后端 未结 2 706
情歌与酒
情歌与酒 2021-02-04 12:20

I am using libcurl in my C application to communicate with an HTTPS server that I have set up. I generated a self-signed certificate on that server that I wish to use with curl.

2条回答
  •  攒了一身酷
    2021-02-04 13:04

    To add a self-signed certificate, use CURLOPT_CAINFO

    To retrieve the SSL public certificate of a site, use

    openssl s_client -connect www.site.com:443 | tee logfile
    

    The certificate is the portion marked by ----BEGIN CERTIFICATE---- and
    ---END CERTIFICATE----.

    Save that certificate into a file, and use curl in a manner like so:

    CURL* c = curl_easy_init();
    curl_easy_setopt(c, CURLOPT_URL, "https://www.site.com");
    curl_easy_setopt(c, CURLOPT_CAINFO, "/path/to/the/certificate.crt");
    curl_easy_setopt(c, CURLOPT_SSL_VERIFYPEER, 1);
    curl_easy_perform(c);
    curl_easy_cleanup(c);
    

提交回复
热议问题