1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_SSL_CTX_DATA
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_SSLVERSION (3)
9  - CURLOPT_SSL_CTX_FUNCTION (3)
10---
11
12# NAME
13
14CURLOPT_SSL_CTX_DATA - pointer passed to SSL context callback
15
16# SYNOPSIS
17
18~~~c
19#include <curl/curl.h>
20
21CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_DATA, void *pointer);
22~~~
23
24# DESCRIPTION
25
26Data *pointer* to pass to the ssl context callback set by the option
27CURLOPT_SSL_CTX_FUNCTION(3), this is the pointer you get as third
28parameter.
29
30# DEFAULT
31
32NULL
33
34# PROTOCOLS
35
36All TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
37
38# EXAMPLE
39
40~~~c
41/* OpenSSL specific */
42
43#include <openssl/ssl.h>
44#include <curl/curl.h>
45#include <stdio.h>
46
47static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
48{
49  X509_STORE *store;
50  X509 *cert = NULL;
51  BIO *bio;
52  char *mypem = parm;
53  /* get a BIO */
54  bio = BIO_new_mem_buf(mypem, -1);
55  /* use it to read the PEM formatted certificate from memory into an
56   * X509 structure that SSL can use
57   */
58  PEM_read_bio_X509(bio, &cert, 0, NULL);
59  if(!cert)
60    printf("PEM_read_bio_X509 failed...\n");
61
62  /* get a pointer to the X509 certificate store (which may be empty) */
63  store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
64
65  /* add our certificate to this store */
66  if(X509_STORE_add_cert(store, cert) == 0)
67    printf("error adding certificate\n");
68
69  /* decrease reference counts */
70  X509_free(cert);
71  BIO_free(bio);
72
73  /* all set to go */
74  return CURLE_OK;
75}
76
77int main(void)
78{
79  CURL *ch;
80  CURLcode rv;
81  char *mypem = /* example CA cert PEM - shortened */
82    "-----BEGIN CERTIFICATE-----\n"
83    "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\n"
84    "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\n"
85    "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\n"
86    "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\n"
87    "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\n"
88    "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\n"
89    "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\n"
90    "-----END CERTIFICATE-----\n";
91
92  curl_global_init(CURL_GLOBAL_ALL);
93  ch = curl_easy_init();
94
95  curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
96  curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
97  curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
98
99  curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
100  curl_easy_setopt(ch, CURLOPT_SSL_CTX_DATA, mypem);
101  rv = curl_easy_perform(ch);
102  if(!rv)
103    printf("*** transfer succeeded ***\n");
104  else
105    printf("*** transfer failed ***\n");
106
107  curl_easy_cleanup(ch);
108  curl_global_cleanup();
109  return rv;
110}
111~~~
112
113# AVAILABILITY
114
115Added in 7.11.0 for OpenSSL, in 7.42.0 for wolfSSL, in 7.54.0 for mbedTLS,
116in 7.83.0 in BearSSL. Other SSL backends are not supported.
117
118# RETURN VALUE
119
120CURLE_OK if supported; or an error such as:
121
122CURLE_NOT_BUILT_IN - Not supported by the SSL backend
123
124CURLE_UNKNOWN_OPTION
125