CURLOPT_SSL_CTX_FUNCTION.3 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. .\" generated by cd2nroff 0.1 from CURLOPT_SSL_CTX_FUNCTION.md
  2. .TH CURLOPT_SSL_CTX_FUNCTION 3 "2025-01-17" libcurl
  3. .SH NAME
  4. CURLOPT_SSL_CTX_FUNCTION \- SSL context callback
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLcode ssl_ctx_callback(CURL *curl, void *ssl_ctx, void *clientp);
  9. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SSL_CTX_FUNCTION,
  10. ssl_ctx_callback);
  11. .fi
  12. .SH DESCRIPTION
  13. Pass a pointer to your callback function, which should match the prototype
  14. shown above.
  15. This callback function gets called by libcurl just before the initialization
  16. of an SSL connection after having processed all other SSL related options to
  17. give a last chance to an application to modify the behavior of the SSL
  18. initialization. The \fIssl_ctx\fP parameter is a pointer to the SSL library\(aqs
  19. \fISSL_CTX\fP for OpenSSL or wolfSSL, a pointer to \fImbedtls_ssl_config\fP for
  20. mbedTLS or a pointer to \fIbr_ssl_client_context\fP for BearSSL. If an error is
  21. returned from the callback no attempt to establish a connection is made and
  22. the perform operation returns the callback\(aqs error code. Set the \fIclientp\fP
  23. argument passed in to this callback with the \fICURLOPT_SSL_CTX_DATA(3)\fP option.
  24. This function gets called for all new connections made to a server, during the
  25. SSL negotiation. While \fIssl_ctx\fP points to a newly initialized object each
  26. time, the pointer may still be the same as in a prior call.
  27. To use this callback, a non\-trivial amount of knowledge of your SSL library is
  28. necessary. For example, you can use this function to call library\-specific
  29. callbacks to add additional validation code for certificates, and even to
  30. change the actual URI of an HTTPS request.
  31. For OpenSSL, asynchronous certificate verification via \fISSL_set_retry_verify\fP
  32. is supported. (Added in 8.3.0)
  33. The \fICURLOPT_SSL_CTX_FUNCTION(3)\fP callback allows the application to reach in
  34. and modify SSL details in the connection without libcurl itself knowing
  35. anything about it, which then subsequently can lead to libcurl unknowingly
  36. reusing SSL connections with different properties. To remedy this you may set
  37. \fICURLOPT_FORBID_REUSE(3)\fP from the callback function.
  38. If you are using DNS\-over\-HTTPS (DoH) via \fICURLOPT_DOH_URL(3)\fP then this
  39. callback is also called for those transfers and the curl handle is set to an
  40. internal handle. \fBThis behavior is subject to change.\fP We recommend setting
  41. \fICURLOPT_PRIVATE(3)\fP on your curl handle so you can identify it correctly in the
  42. context callback. If you have a reason to modify DoH SSL context please let us
  43. know on the curl\-library mailing list because we are considering removing this
  44. capability.
  45. libcurl does not guarantee the lifetime of the passed in object once this
  46. callback function has returned. Your application must not assume that it can
  47. keep using the SSL context or data derived from it once this function is
  48. completed.
  49. For libcurl builds using TLS backends that support CA caching and
  50. \fICURLOPT_CA_CACHE_TIMEOUT(3)\fP is not set to zero, multiple calls to this
  51. callback may be done with the same CA store in memory.
  52. .SH DEFAULT
  53. NULL
  54. .SH PROTOCOLS
  55. This functionality affects all TLS based protocols: HTTPS, FTPS, IMAPS, POP3S, SMTPS etc.
  56. This option works only with the following TLS backends:
  57. BearSSL, OpenSSL, mbedTLS and wolfSSL
  58. .SH EXAMPLE
  59. .nf
  60. /* OpenSSL specific */
  61. #include <openssl/ssl.h>
  62. #include <curl/curl.h>
  63. #include <stdio.h>
  64. static CURLcode sslctx_function(CURL *curl, void *sslctx, void *parm)
  65. {
  66. X509_STORE *store;
  67. X509 *cert = NULL;
  68. BIO *bio;
  69. char *mypem = parm;
  70. /* get a BIO */
  71. bio = BIO_new_mem_buf(mypem, -1);
  72. /* use it to read the PEM formatted certificate from memory into an
  73. * X509 structure that SSL can use
  74. */
  75. PEM_read_bio_X509(bio, &cert, 0, NULL);
  76. if(!cert)
  77. printf("PEM_read_bio_X509 failed...\\n");
  78. /* get a pointer to the X509 certificate store (which may be empty) */
  79. store = SSL_CTX_get_cert_store((SSL_CTX *)sslctx);
  80. /* add our certificate to this store */
  81. if(X509_STORE_add_cert(store, cert) == 0)
  82. printf("error adding certificate\\n");
  83. /* decrease reference counts */
  84. X509_free(cert);
  85. BIO_free(bio);
  86. /* all set to go */
  87. return CURLE_OK;
  88. }
  89. int main(void)
  90. {
  91. CURL *ch;
  92. CURLcode rv;
  93. char *mypem = /* example CA cert PEM - shortened */
  94. "-----BEGIN CERTIFICATE-----\\n"
  95. "MIIHPTCCBSWgAwIBAgIBADANBgkqhkiG9w0BAQQFADB5MRAwDgYDVQQKEwdSb290\\n"
  96. "IENBMR4wHAYDVQQLExVodHRwOi8vd3d3LmNhY2VydC5vcmcxIjAgBgNVBAMTGUNB\\n"
  97. "IENlcnQgU2lnbmluZyBBdXRob3JpdHkxITAfBgkqhkiG9w0BCQEWEnN1cHBvcnRA\\n"
  98. "Y2FjZXJ0Lm9yZzAeFw0wMzAzMzAxMjI5NDlaFw0zMzAzMjkxMjI5NDlaMHkxEDAO\\n"
  99. "GCSNe9FINSkYQKyTYOGWhlC0elnYjyELn8+CkcY7v2vcB5G5l1YjqrZslMZIBjzk\\n"
  100. "zk6q5PYvCdxTby78dOs6Y5nCpqyJvKeyRKANihDjbPIky/qbn3BHLt4Ui9SyIAmW\\n"
  101. "omTxJBzcoTWcFbLUvFUufQb1nA5V9FrWk9p2rSVzTMVD\\n"
  102. "-----END CERTIFICATE-----\\n";
  103. curl_global_init(CURL_GLOBAL_ALL);
  104. ch = curl_easy_init();
  105. curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
  106. curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
  107. curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
  108. curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, *sslctx_function);
  109. curl_easy_setopt(ch, CURLOPT_SSL_CTX_DATA, mypem);
  110. rv = curl_easy_perform(ch);
  111. if(!rv)
  112. printf("*** transfer succeeded ***\\n");
  113. else
  114. printf("*** transfer failed ***\\n");
  115. curl_easy_cleanup(ch);
  116. curl_global_cleanup();
  117. return rv;
  118. }
  119. .fi
  120. .SH AVAILABILITY
  121. Added in curl 7.10.6
  122. .SH RETURN VALUE
  123. CURLE_OK if supported; or an error such as:
  124. CURLE_NOT_BUILT_IN \- Not supported by the SSL backend
  125. .SH SEE ALSO
  126. .BR CURLOPT_CAINFO (3),
  127. .BR CURLOPT_CAINFO_BLOB (3),
  128. .BR CURLOPT_CA_CACHE_TIMEOUT (3),
  129. .BR CURLOPT_SSL_CTX_DATA (3),
  130. .BR CURLOPT_SSL_VERIFYHOST (3),
  131. .BR CURLOPT_SSL_VERIFYPEER (3)