1CURLOPT_SSL_CTX_DATA(3) curl_easy_setopt options CURLOPT_SSL_CTX_DATA(3)
2
3
4
6 CURLOPT_SSL_CTX_DATA - custom pointer passed to ssl_ctx callback
7
9 #include <curl/curl.h>
10
11 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_DATA, void
12 *pointer);
13
15 Data pointer to pass to the ssl context callback set by the option CUR‐
16 LOPT_SSL_CTX_FUNCTION(3), this is the pointer you'll get as third pa‐
17 rameter.
18
20 NULL
21
23 All TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
24
26 /* OpenSSL specific */
27
28 #include <openssl/ssl.h>
29 #include <curl/curl.h>
30 #include <stdio.h>
31
32 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
33 {
34 X509_STORE *store;
35 X509 *cert = NULL;
36 BIO *bio;
37 char *mypem = parm;
38 /* get a BIO */
39 bio = BIO_new_mem_buf(mypem, -1);
40 /* use it to read the PEM formatted certificate from memory into an
41 * X509 structure that SSL can use
42 */
43 PEM_read_bio_X509(bio, &cert, 0, NULL);
44 if(cert == NULL)
45 printf("PEM_read_bio_X509 failed...\n");
46
47 /* get a pointer to the X509 certificate store (which may be empty) */
48 store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
49
50 /* add our certificate to this store */
51 if(X509_STORE_add_cert(store, cert) == 0)
52 printf("error adding certificate\n");
53
54 /* decrease reference counts */
55 X509_free(cert);
56 BIO_free(bio);
57
58 /* all set to go */
59 return CURLE_OK;
60 }
61
62 int main(void)
63 {
64 CURL * ch;
65 CURLcode rv;
66 char *mypem = /* example CA cert PEM - shortened */
67 "-----BEGIN CERTIFICATE-----\n"
68 "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"
69 "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"
70 "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"
71 "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"
72 "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"
73 "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"
74 "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"
75 "-----END CERTIFICATE-----\n";
76
77 curl_global_init(CURL_GLOBAL_ALL);
78 ch = curl_easy_init();
79
80 curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
81 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
82 curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
83
84 /* Retrieve page using cacerts' certificate -> will succeed
85 * load the certificate by installing a function doing the necessary
86 * "modifications" to the SSL CONTEXT just before link init
87 */
88 curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
89 curl_easy_setopt(ch, CURLOPT_SSL_CTX_DATA, mypem);
90 rv = curl_easy_perform(ch);
91 if(!rv)
92 printf("*** transfer succeeded ***\n");
93 else
94 printf("*** transfer failed ***\n");
95
96 curl_easy_cleanup(ch);
97 curl_global_cleanup();
98 return rv;
99 }
100
102 Added in 7.11.0 for OpenSSL, in 7.42.0 for wolfSSL and in 7.54.0 for
103 mbedTLS. Other SSL backends are not supported.
104
106 CURLE_OK if supported; or an error such as:
107
108 CURLE_NOT_BUILT_IN - Not supported by the SSL backend
109
110 CURLE_UNKNOWN_OPTION
111
113 CURLOPT_SSL_CTX_FUNCTION(3), CURLOPT_SSLVERSION(3),
114
115
116
117libcurl 7.79.1 July 26, 2021 CURLOPT_SSL_CTX_DATA(3)