chiark / gitweb /
Import curl_7.56.1.orig.tar.gz
[curl.git] / docs / libcurl / opts / CURLOPT_SSL_CTX_FUNCTION.3
1 .\" **************************************************************************
2 .\" *                                  _   _ ____  _
3 .\" *  Project                     ___| | | |  _ \| |
4 .\" *                             / __| | | | |_) | |
5 .\" *                            | (__| |_| |  _ <| |___
6 .\" *                             \___|\___/|_| \_\_____|
7 .\" *
8 .\" * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9 .\" *
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.
13 .\" *
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.
17 .\" *
18 .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 .\" * KIND, either express or implied.
20 .\" *
21 .\" **************************************************************************
22 .\"
23 .TH CURLOPT_SSL_CTX_FUNCTION 3 "March 26, 2017" "libcurl 7.56.1" "curl_easy_setopt options"
24
25 .SH NAME
26 CURLOPT_SSL_CTX_FUNCTION \- SSL context callback for OpenSSL, wolfSSL/CyaSSL or mbedTLS
27 .SH SYNOPSIS
28 .nf
29 #include <curl/curl.h>
30
31 CURLcode ssl_ctx_callback(CURL *curl, void *ssl_ctx, void *userptr);
32
33 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_FUNCTION,
34                           ssl_ctx_callback);
35 .SH DESCRIPTION
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
38 absent.
39
40 Pass a pointer to your callback function, which should match the prototype
41 shown above.
42
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.
52
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.
56
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.
61 .SH DEFAULT
62 NULL
63 .SH PROTOCOLS
64 All TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
65 .SH EXAMPLE
66 .nf
67 /* OpenSSL specific */
68
69 #include <openssl/ssl.h>
70 #include <curl/curl.h>
71 #include <stdio.h>
72
73 static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
74 {
75   X509_STORE *store;
76   X509 *cert=NULL;
77   BIO *bio;
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";
88   /* get a BIO */
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
92    */
93   PEM_read_bio_X509(bio, &cert, 0, NULL);
94   if(cert == NULL)
95     printf("PEM_read_bio_X509 failed...\\n");
96
97   /* get a pointer to the X509 certificate store (which may be empty) */
98   store=SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
99
100   /* add our certificate to this store */
101   if(X509_STORE_add_cert(store, cert)==0)
102     printf("error adding certificate\\n");
103
104   /* decrease reference counts */
105   X509_free(cert);
106   BIO_free(bio);
107
108   /* all set to go */
109   return CURLE_OK;
110 }
111
112 int main(void)
113 {
114   CURL * ch;
115   CURLcode rv;
116
117   rv=curl_global_init(CURL_GLOBAL_ALL);
118   ch=curl_easy_init();
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/");
122
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
126    */
127   rv=curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
128   rv=curl_easy_perform(ch);
129   if(rv==CURLE_OK)
130     printf("*** transfer succeeded ***\\n");
131   else
132     printf("*** transfer failed ***\\n");
133
134   curl_easy_cleanup(ch);
135   curl_global_cleanup();
136   return rv;
137 }
138 .fi
139 .SH AVAILABILITY
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.
142 .SH RETURN VALUE
143 CURLE_OK if supported; or an error such as:
144
145 CURLE_NOT_BUILT_IN - Not supported by the SSL backend
146
147 CURLE_UNKNOWN_OPTION
148 .SH "SEE ALSO"
149 .BR CURLOPT_SSL_CTX_DATA "(3), " CURLOPT_SSL_VERIFYPEER "(3), "