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
17 parameter.
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 = (char *)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 rv=curl_global_init(CURL_GLOBAL_ALL);
78 ch=curl_easy_init();
79 rv=curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
80 rv=curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
81 rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
82
83 /* Retrieve page using cacerts' certificate -> will succeed
84 * load the certificate by installing a function doing the necessary
85 * "modifications" to the SSL CONTEXT just before link init
86 */
87 rv=curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
88 rv=curl_easy_setopt(ch, CURLOPT_SSL_CTX_DATA, mypem);
89 rv=curl_easy_perform(ch);
90 if(rv==CURLE_OK)
91 printf("*** transfer succeeded ***\n");
92 else
93 printf("*** transfer failed ***\n");
94
95 curl_easy_cleanup(ch);
96 curl_global_cleanup();
97 return rv;
98 }
99
101 Added in 7.11.0 for OpenSSL, in 7.42.0 for wolfSSL and in 7.54.0 for
102 mbedTLS. Other SSL backends are not supported.
103
105 CURLE_OK if supported; or an error such as:
106
107 CURLE_NOT_BUILT_IN - Not supported by the SSL backend
108
109 CURLE_UNKNOWN_OPTION
110
112 CURLOPT_SSL_CTX_FUNCTION(3), CURLOPT_SSLVERSION(3),
113
114
115
116libcurl 7.66.0 June 02, 2019 CURLOPT_SSL_CTX_DATA(3)