CURLOPT_SSL_CTX_FUNCTION.3 6.6 KB

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