1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_SSL_CTX_FUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_SSL_CTX_DATA (3)
9  - CURLOPT_SSL_VERIFYPEER (3)
10---
11
12# NAME
13
14CURLOPT_SSL_CTX_FUNCTION - SSL context callback for OpenSSL, wolfSSL or mbedTLS
15
16# SYNOPSIS
17
18~~~c
19#include <curl/curl.h>
20
21CURLcode ssl_ctx_callback(CURL *curl, void *ssl_ctx, void *clientp);
22
23CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_FUNCTION,
24                          ssl_ctx_callback);
25~~~
26
27# DESCRIPTION
28
29This option only works for libcurl powered by OpenSSL, wolfSSL, mbedTLS or
30BearSSL. If libcurl was built against another SSL library this functionality
31is absent.
32
33Pass a pointer to your callback function, which should match the prototype
34shown above.
35
36This callback function gets called by libcurl just before the initialization
37of an SSL connection after having processed all other SSL related options to
38give a last chance to an application to modify the behavior of the SSL
39initialization. The *ssl_ctx* parameter is actually a pointer to the SSL
40library's *SSL_CTX* for OpenSSL or wolfSSL, a pointer to
41*mbedtls_ssl_config* for mbedTLS or a pointer to
42*br_ssl_client_context* for BearSSL. If an error is returned from the
43callback no attempt to establish a connection is made and the perform
44operation returns the callback's error code. Set the *clientp* argument
45with the CURLOPT_SSL_CTX_DATA(3) option.
46
47This function gets called on all new connections made to a server, during the
48SSL negotiation. The *ssl_ctx* points to a newly initialized object each
49time, but note the pointer may be the same as from a prior call.
50
51To use this properly, a non-trivial amount of knowledge of your SSL library is
52necessary. For example, you can use this function to call library-specific
53callbacks to add additional validation code for certificates, and even to
54change the actual URI of an HTTPS request.
55
56For OpenSSL, asynchronous certificate verification via
57*SSL_set_retry_verify* is supported. (Added in 8.3.0)
58
59WARNING: The CURLOPT_SSL_CTX_FUNCTION(3) callback allows the application
60to reach in and modify SSL details in the connection without libcurl itself
61knowing anything about it, which then subsequently can lead to libcurl
62unknowingly reusing SSL connections with different properties. To remedy this
63you may set CURLOPT_FORBID_REUSE(3) from the callback function.
64
65WARNING: If you are using DNS-over-HTTPS (DoH) via CURLOPT_DOH_URL(3)
66then this callback is also called for those transfers and the curl handle is
67set to an internal handle. **This behavior is subject to change.** We
68recommend before performing your transfer set CURLOPT_PRIVATE(3) on your
69curl handle so you can identify it in the context callback. If you have a
70reason to modify DoH SSL context please let us know on the curl-library
71mailing list because we are considering removing this capability.
72
73# DEFAULT
74
75NULL
76
77# PROTOCOLS
78
79All TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
80
81# EXAMPLE
82
83~~~c
84/* OpenSSL specific */
85
86#include <openssl/ssl.h>
87#include <curl/curl.h>
88#include <stdio.h>
89
90static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
91{
92  X509_STORE *store;
93  X509 *cert = NULL;
94  BIO *bio;
95  char *mypem = parm;
96  /* get a BIO */
97  bio = BIO_new_mem_buf(mypem, -1);
98  /* use it to read the PEM formatted certificate from memory into an
99   * X509 structure that SSL can use
100   */
101  PEM_read_bio_X509(bio, &cert, 0, NULL);
102  if(!cert)
103    printf("PEM_read_bio_X509 failed...\n");
104
105  /* get a pointer to the X509 certificate store (which may be empty) */
106  store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
107
108  /* add our certificate to this store */
109  if(X509_STORE_add_cert(store, cert) == 0)
110    printf("error adding certificate\n");
111
112  /* decrease reference counts */
113  X509_free(cert);
114  BIO_free(bio);
115
116  /* all set to go */
117  return CURLE_OK;
118}
119
120int main(void)
121{
122  CURL *ch;
123  CURLcode rv;
124  char *mypem = /* example CA cert PEM - shortened */
125    "-----BEGIN CERTIFICATE-----\n"
126    "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"
127    "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"
128    "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"
129    "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"
130    "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"
131    "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"
132    "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"
133    "-----END CERTIFICATE-----\n";
134
135  curl_global_init(CURL_GLOBAL_ALL);
136  ch = curl_easy_init();
137
138  curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
139  curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
140  curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
141
142  curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
143  curl_easy_setopt(ch, CURLOPT_SSL_CTX_DATA, mypem);
144  rv = curl_easy_perform(ch);
145  if(!rv)
146    printf("*** transfer succeeded ***\n");
147  else
148    printf("*** transfer failed ***\n");
149
150  curl_easy_cleanup(ch);
151  curl_global_cleanup();
152  return rv;
153}
154~~~
155
156# AVAILABILITY
157
158Added in 7.11.0 for OpenSSL, in 7.42.0 for wolfSSL, in 7.54.0 for mbedTLS,
159in 7.83.0 in BearSSL. Other SSL backends are not supported.
160
161# RETURN VALUE
162
163CURLE_OK if supported; or an error such as:
164
165CURLE_NOT_BUILT_IN - Not supported by the SSL backend
166
167CURLE_UNKNOWN_OPTION
168