CURLOPT_SSL_CTX_DATA.3 4.2 KB

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