1 .\" **************************************************************************
3 .\" * Project ___| | | | _ \| |
4 .\" * / __| | | | |_) | |
5 .\" * | (__| |_| | _ <| |___
6 .\" * \___|\___/|_| \_\_____|
8 .\" * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 .\" * This software is licensed as described in the file COPYING, which
11 .\" * you should have received as part of this distribution. The terms
12 .\" * are also available at https://curl.haxx.se/docs/copyright.html.
14 .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 .\" * copies of the Software, and permit persons to whom the Software is
16 .\" * furnished to do so, under the terms of the COPYING file.
18 .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 .\" * KIND, either express or implied.
21 .\" **************************************************************************
23 .TH CURLOPT_SSL_CTX_FUNCTION 3 "March 26, 2017" "libcurl 7.56.1" "curl_easy_setopt options"
26 CURLOPT_SSL_CTX_FUNCTION \- SSL context callback for OpenSSL, wolfSSL/CyaSSL or mbedTLS
29 #include <curl/curl.h>
31 CURLcode ssl_ctx_callback(CURL *curl, void *ssl_ctx, void *userptr);
33 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_FUNCTION,
36 This option only works for libcurl powered by OpenSSL, wolfSSL/CyaSSL or
37 mbedTLS. If libcurl was built against another SSL library this functionality is
40 Pass a pointer to your callback function, which should match the prototype
43 This callback function gets called by libcurl just before the initialization
44 of an SSL connection after having processed all other SSL related options to
45 give a last chance to an application to modify the behaviour of the SSL
46 initialization. The \fIssl_ctx\fP parameter is actually a pointer to the SSL
47 library's \fISSL_CTX\fP for OpenSSL or wolfSSL/CyaSSL, and a pointer to
48 \fImbedtls_ssl_config\fP for mbedTLS. If an error is returned from the callback
49 no attempt to establish a connection is made and the perform operation will
50 return the callback's error code. Set the \fIuserptr\fP argument with the
51 \fICURLOPT_SSL_CTX_DATA(3)\fP option.
53 This function will get called on all new connections made to a server, during
54 the SSL negotiation. The \fIssl_ctx\fP will point to a newly initialized object
55 each time, but note the pointer may be the same as from a prior call.
57 To use this properly, a non-trivial amount of knowledge of your SSL library is
58 necessary. For example, you can use this function to call library-specific
59 callbacks to add additional validation code for certificates, and even to
60 change the actual URI of a HTTPS request.
64 All TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
67 /* OpenSSL specific */
69 #include <openssl/ssl.h>
70 #include <curl/curl.h>
73 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
78 char *mypem = /* example CA cert PEM - shortened */
79 "-----BEGIN CERTIFICATE-----\\n"
80 "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\\n"
81 "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\\n"
82 "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\\n"
83 "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\\n"
84 "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\\n"
85 "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\\n"
86 "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\\n"
87 "-----END CERTIFICATE-----\\n";
89 bio=BIO_new_mem_buf(mypem, -1);
90 /* use it to read the PEM formatted certificate from memory into an
91 * X509 structure that SSL can use
93 PEM_read_bio_X509(bio, &cert, 0, NULL);
95 printf("PEM_read_bio_X509 failed...\\n");
97 /* get a pointer to the X509 certificate store (which may be empty) */
98 store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
100 /* add our certificate to this store */
101 if(X509_STORE_add_cert(store, cert)==0)
102 printf("error adding certificate\\n");
104 /* decrease reference counts */
117 rv=curl_global_init(CURL_GLOBAL_ALL);
119 rv=curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
120 rv=curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
121 rv=curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
123 /* Retrieve page using cacerts' certificate -> will succeed
124 * load the certificate by installing a function doing the necessary
125 * "modifications" to the SSL CONTEXT just before link init
127 rv=curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
128 rv=curl_easy_perform(ch);
130 printf("*** transfer succeeded ***\\n");
132 printf("*** transfer failed ***\\n");
134 curl_easy_cleanup(ch);
135 curl_global_cleanup();
140 Added in 7.11.0 for OpenSSL. Added in 7.42.0 for wolfSSL/CyaSSL. Added in
141 7.54.0 for mbedTLS. Other SSL backends not supported.
143 CURLE_OK if supported; or an error such as:
145 CURLE_NOT_BUILT_IN - Not supported by the SSL backend
149 .BR CURLOPT_SSL_CTX_DATA "(3), " CURLOPT_SSL_VERIFYPEER "(3), "