CURLOPT_SSL_CTX_DATA.3 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) 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.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. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .\"
  25. .TH CURLOPT_SSL_CTX_DATA 3 "19 Jun 2014" libcurl libcurl
  26. .SH NAME
  27. CURLOPT_SSL_CTX_DATA \- pointer passed to SSL context callback
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/curl.h>
  31. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_DATA, void *pointer);
  32. .fi
  33. .SH DESCRIPTION
  34. Data \fIpointer\fP to pass to the ssl context callback set by the option
  35. \fICURLOPT_SSL_CTX_FUNCTION(3)\fP, this is the pointer you will get as third
  36. parameter.
  37. .SH DEFAULT
  38. NULL
  39. .SH PROTOCOLS
  40. All TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
  41. .SH EXAMPLE
  42. .nf
  43. /* OpenSSL specific */
  44. #include <openssl/ssl.h>
  45. #include <curl/curl.h>
  46. #include <stdio.h>
  47. static 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 == NULL)
  60. printf("PEM_read_bio_X509 failed...\\n");
  61. /* get a pointer to the X509 certificate store (which may be empty) */
  62. store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
  63. /* add our certificate to this store */
  64. if(X509_STORE_add_cert(store, cert) == 0)
  65. printf("error adding certificate\\n");
  66. /* decrease reference counts */
  67. X509_free(cert);
  68. BIO_free(bio);
  69. /* all set to go */
  70. return CURLE_OK;
  71. }
  72. int main(void)
  73. {
  74. CURL * ch;
  75. CURLcode rv;
  76. char *mypem = /* example CA cert PEM - shortened */
  77. "-----BEGIN CERTIFICATE-----\\n"
  78. "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\\n"
  79. "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\\n"
  80. "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\\n"
  81. "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\\n"
  82. "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\\n"
  83. "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\\n"
  84. "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\\n"
  85. "-----END CERTIFICATE-----\\n";
  86. curl_global_init(CURL_GLOBAL_ALL);
  87. ch = curl_easy_init();
  88. curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
  89. curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
  90. curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
  91. /* Retrieve page using cacerts' certificate -> will succeed
  92. * load the certificate by installing a function doing the necessary
  93. * "modifications" to the SSL CONTEXT just before link init
  94. */
  95. curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
  96. curl_easy_setopt(ch, CURLOPT_SSL_CTX_DATA, mypem);
  97. rv = curl_easy_perform(ch);
  98. if(!rv)
  99. printf("*** transfer succeeded ***\\n");
  100. else
  101. printf("*** transfer failed ***\\n");
  102. curl_easy_cleanup(ch);
  103. curl_global_cleanup();
  104. return rv;
  105. }
  106. .fi
  107. .SH AVAILABILITY
  108. Added in 7.11.0 for OpenSSL, in 7.42.0 for wolfSSL, in 7.54.0 for mbedTLS,
  109. in 7.83.0 in BearSSL. Other SSL backends are not supported.
  110. .SH RETURN VALUE
  111. CURLE_OK if supported; or an error such as:
  112. CURLE_NOT_BUILT_IN - Not supported by the SSL backend
  113. CURLE_UNKNOWN_OPTION
  114. .SH "SEE ALSO"
  115. .BR CURLOPT_SSL_CTX_FUNCTION "(3), " CURLOPT_SSLVERSION "(3), "