CURLOPT_SSL_CTX_FUNCTION.3 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_FUNCTION 3 "19 Jun 2014" "libcurl 7.37.0" "curl_easy_setopt options"
  24. .SH NAME
  25. CURLOPT_SSL_CTX_FUNCTION \- SSL context callback for OpenSSL, wolfSSL or mbedTLS
  26. .SH SYNOPSIS
  27. .nf
  28. #include <curl/curl.h>
  29. CURLcode ssl_ctx_callback(CURL *curl, void *ssl_ctx, void *userptr);
  30. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_FUNCTION,
  31. ssl_ctx_callback);
  32. .SH DESCRIPTION
  33. This option only works for libcurl powered by OpenSSL, wolfSSL, mbedTLS or
  34. BearSSL. If libcurl was built against another SSL library this functionality
  35. is absent.
  36. Pass a pointer to your callback function, which should match the prototype
  37. shown above.
  38. This callback function gets called by libcurl just before the initialization
  39. of an SSL connection after having processed all other SSL related options to
  40. give a last chance to an application to modify the behavior of the SSL
  41. initialization. The \fIssl_ctx\fP parameter is actually a pointer to the SSL
  42. library's \fISSL_CTX\fP for OpenSSL or wolfSSL, a pointer to
  43. \fImbedtls_ssl_config\fP for mbedTLS or a pointer to
  44. \fIbr_ssl_client_context\fP for BearSSL. If an error is returned from the
  45. callback no attempt to establish a connection is made and the perform
  46. operation will return the callback's error code. Set the \fIuserptr\fP
  47. argument with the \fICURLOPT_SSL_CTX_DATA(3)\fP option.
  48. This function will get called on all new connections made to a server, during
  49. the SSL negotiation. The \fIssl_ctx\fP will point to a newly initialized object
  50. each time, but note the pointer may be the same as from a prior call.
  51. To use this properly, a non-trivial amount of knowledge of your SSL library is
  52. necessary. For example, you can use this function to call library-specific
  53. callbacks to add additional validation code for certificates, and even to
  54. change the actual URI of an HTTPS request.
  55. WARNING: The \fICURLOPT_SSL_CTX_FUNCTION(3)\fP callback allows the application
  56. to reach in and modify SSL details in the connection without libcurl itself
  57. knowing anything about it, which then subsequently can lead to libcurl
  58. unknowingly reusing SSL connections with different properties. To remedy this
  59. you may set \fICURLOPT_FORBID_REUSE(3)\fP from the callback function.
  60. WARNING: If you are using DNS-over-HTTPS (DoH) via \fICURLOPT_DOH_URL(3)\fP
  61. then the CTX callback will also be called for those transfers and the curl
  62. handle is set to an internal handle. \fBThis behavior is subject to change.\fP
  63. We recommend before performing your transfer set \fICURLOPT_PRIVATE(3)\fP on
  64. your curl handle so you can identify it in the CTX callback. If you have a
  65. reason to modify DoH SSL context please let us know on the curl-library mailing
  66. list because we are considering removing this capability.
  67. .SH DEFAULT
  68. NULL
  69. .SH PROTOCOLS
  70. All TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
  71. .SH EXAMPLE
  72. .nf
  73. /* OpenSSL specific */
  74. #include <openssl/ssl.h>
  75. #include <curl/curl.h>
  76. #include <stdio.h>
  77. static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
  78. {
  79. X509_STORE *store;
  80. X509 *cert = NULL;
  81. BIO *bio;
  82. char *mypem = parm;
  83. /* get a BIO */
  84. bio = BIO_new_mem_buf(mypem, -1);
  85. /* use it to read the PEM formatted certificate from memory into an
  86. * X509 structure that SSL can use
  87. */
  88. PEM_read_bio_X509(bio, &cert, 0, NULL);
  89. if(cert == NULL)
  90. printf("PEM_read_bio_X509 failed...\\n");
  91. /* get a pointer to the X509 certificate store (which may be empty) */
  92. store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
  93. /* add our certificate to this store */
  94. if(X509_STORE_add_cert(store, cert) == 0)
  95. printf("error adding certificate\\n");
  96. /* decrease reference counts */
  97. X509_free(cert);
  98. BIO_free(bio);
  99. /* all set to go */
  100. return CURLE_OK;
  101. }
  102. int main(void)
  103. {
  104. CURL * ch;
  105. CURLcode rv;
  106. char *mypem = /* example CA cert PEM - shortened */
  107. "-----BEGIN CERTIFICATE-----\\n"
  108. "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\\n"
  109. "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\\n"
  110. "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\\n"
  111. "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\\n"
  112. "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\\n"
  113. "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\\n"
  114. "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\\n"
  115. "-----END CERTIFICATE-----\\n";
  116. curl_global_init(CURL_GLOBAL_ALL);
  117. ch = curl_easy_init();
  118. curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
  119. curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
  120. curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
  121. /* Retrieve page using cacerts' certificate -> will succeed
  122. * load the certificate by installing a function doing the necessary
  123. * "modifications" to the SSL CONTEXT just before link init
  124. */
  125. curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
  126. curl_easy_setopt(ch, CURLOPT_SSL_CTX_DATA, mypem);
  127. rv = curl_easy_perform(ch);
  128. if(!rv)
  129. printf("*** transfer succeeded ***\\n");
  130. else
  131. printf("*** transfer failed ***\\n");
  132. curl_easy_cleanup(ch);
  133. curl_global_cleanup();
  134. return rv;
  135. }
  136. .fi
  137. .SH AVAILABILITY
  138. Added in 7.11.0 for OpenSSL, in 7.42.0 for wolfSSL, in 7.54.0 for mbedTLS,
  139. in 7.83.0 in BearSSL. Other SSL backends are not supported.
  140. .SH RETURN VALUE
  141. CURLE_OK if supported; or an error such as:
  142. CURLE_NOT_BUILT_IN - Not supported by the SSL backend
  143. CURLE_UNKNOWN_OPTION
  144. .SH "SEE ALSO"
  145. .BR CURLOPT_SSL_CTX_DATA "(3), " CURLOPT_SSL_VERIFYPEER "(3), "