cmCurl.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCurl.h"
  4. #include <cm/string_view>
  5. #include <cmext/string_view>
  6. #if !defined(CMAKE_USE_SYSTEM_CURL) && !defined(_WIN32) && \
  7. !defined(__APPLE__) && !defined(CURL_CA_BUNDLE) && !defined(CURL_CA_PATH)
  8. # define CMAKE_FIND_CAFILE
  9. #endif
  10. #include "cmStringAlgorithms.h"
  11. #include "cmSystemTools.h"
  12. #if defined(_WIN32)
  13. # include <vector>
  14. # include <windows.h>
  15. # include "cmsys/Encoding.hxx"
  16. #endif
  17. // curl versions before 7.21.5 did not provide this error code
  18. #if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM < 0x071505
  19. # define CURLE_NOT_BUILT_IN 4
  20. #endif
  21. #define check_curl_result(result, errstr) \
  22. do { \
  23. if ((result) != CURLE_OK && (result) != CURLE_NOT_BUILT_IN) { \
  24. e += e.empty() ? "" : "\n"; \
  25. e += (errstr); \
  26. e += ::curl_easy_strerror(result); \
  27. } \
  28. } while (false)
  29. cm::optional<int> cmCurlParseTLSVersion(std::string const& tls_version)
  30. {
  31. cm::optional<int> v;
  32. if (tls_version == "1.0"_s) {
  33. v = CURL_SSLVERSION_TLSv1_0;
  34. } else if (tls_version == "1.1"_s) {
  35. v = CURL_SSLVERSION_TLSv1_1;
  36. } else if (tls_version == "1.2"_s) {
  37. v = CURL_SSLVERSION_TLSv1_2;
  38. } else if (tls_version == "1.3"_s) {
  39. // curl version 7.52.0 introduced TLS 1.3 support
  40. #if defined(LIBCURL_VERSION_NUM) && LIBCURL_VERSION_NUM >= 0x073400
  41. v = CURL_SSLVERSION_TLSv1_3;
  42. #else
  43. v = CURL_SSLVERSION_LAST;
  44. #endif
  45. }
  46. return v;
  47. }
  48. std::string cmCurlSetCAInfo(::CURL* curl, const std::string& cafile)
  49. {
  50. std::string e;
  51. std::string env_ca;
  52. if (!cafile.empty()) {
  53. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_CAINFO, cafile.c_str());
  54. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  55. }
  56. /* Honor the user-configurable OpenSSL environment variables. */
  57. else if (cmSystemTools::GetEnv("SSL_CERT_FILE", env_ca) &&
  58. cmSystemTools::FileExists(env_ca, true)) {
  59. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_CAINFO, env_ca.c_str());
  60. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  61. } else if (cmSystemTools::GetEnv("SSL_CERT_DIR", env_ca) &&
  62. cmSystemTools::FileIsDirectory(env_ca)) {
  63. ::CURLcode res = ::curl_easy_setopt(curl, CURLOPT_CAPATH, env_ca.c_str());
  64. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  65. }
  66. #ifdef CMAKE_FIND_CAFILE
  67. # define CMAKE_CAFILE_FEDORA "/etc/pki/tls/certs/ca-bundle.crt"
  68. else if (cmSystemTools::FileExists(CMAKE_CAFILE_FEDORA, true)) {
  69. ::CURLcode res =
  70. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_FEDORA);
  71. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  72. }
  73. # undef CMAKE_CAFILE_FEDORA
  74. else {
  75. # define CMAKE_CAFILE_COMMON "/etc/ssl/certs/ca-certificates.crt"
  76. if (cmSystemTools::FileExists(CMAKE_CAFILE_COMMON, true)) {
  77. ::CURLcode res =
  78. ::curl_easy_setopt(curl, CURLOPT_CAINFO, CMAKE_CAFILE_COMMON);
  79. check_curl_result(res, "Unable to set TLS/SSL Verify CAINFO: ");
  80. }
  81. # undef CMAKE_CAFILE_COMMON
  82. # define CMAKE_CAPATH_COMMON "/etc/ssl/certs"
  83. if (cmSystemTools::FileIsDirectory(CMAKE_CAPATH_COMMON)) {
  84. ::CURLcode res =
  85. ::curl_easy_setopt(curl, CURLOPT_CAPATH, CMAKE_CAPATH_COMMON);
  86. check_curl_result(res, "Unable to set TLS/SSL Verify CAPATH: ");
  87. }
  88. # undef CMAKE_CAPATH_COMMON
  89. }
  90. #endif
  91. return e;
  92. }
  93. std::string cmCurlSetNETRCOption(::CURL* curl, const std::string& netrc_level,
  94. const std::string& netrc_file)
  95. {
  96. std::string e;
  97. CURL_NETRC_OPTION curl_netrc_level = CURL_NETRC_LAST;
  98. ::CURLcode res;
  99. if (!netrc_level.empty()) {
  100. if (netrc_level == "OPTIONAL") {
  101. curl_netrc_level = CURL_NETRC_OPTIONAL;
  102. } else if (netrc_level == "REQUIRED") {
  103. curl_netrc_level = CURL_NETRC_REQUIRED;
  104. } else if (netrc_level == "IGNORED") {
  105. curl_netrc_level = CURL_NETRC_IGNORED;
  106. } else {
  107. e = cmStrCat("NETRC accepts OPTIONAL, IGNORED or REQUIRED but got: ",
  108. netrc_level);
  109. return e;
  110. }
  111. }
  112. if (curl_netrc_level != CURL_NETRC_LAST &&
  113. curl_netrc_level != CURL_NETRC_IGNORED) {
  114. res = ::curl_easy_setopt(curl, CURLOPT_NETRC, curl_netrc_level);
  115. check_curl_result(res, "Unable to set netrc level: ");
  116. if (!e.empty()) {
  117. return e;
  118. }
  119. // check to see if a .netrc file has been specified
  120. if (!netrc_file.empty()) {
  121. res = ::curl_easy_setopt(curl, CURLOPT_NETRC_FILE, netrc_file.c_str());
  122. check_curl_result(res, "Unable to set .netrc file path : ");
  123. }
  124. }
  125. return e;
  126. }
  127. std::string cmCurlFixFileURL(std::string url)
  128. {
  129. if (!cmHasLiteralPrefix(url, "file://")) {
  130. return url;
  131. }
  132. // libcurl 7.77 and below accidentally allowed spaces in URLs in some cases.
  133. // One such case was file:// URLs, which CMake has long accepted as a result.
  134. // Explicitly encode spaces for a URL.
  135. cmSystemTools::ReplaceString(url, " ", "%20");
  136. #if defined(_WIN32)
  137. // libcurl doesn't support file:// urls for unicode filenames on Windows.
  138. // Convert string from UTF-8 to ACP if this is a file:// URL.
  139. std::wstring wurl = cmsys::Encoding::ToWide(url);
  140. if (!wurl.empty()) {
  141. int mblen = WideCharToMultiByte(CP_ACP, 0, wurl.c_str(), -1, nullptr, 0,
  142. nullptr, nullptr);
  143. if (mblen > 0) {
  144. std::vector<char> chars(mblen);
  145. mblen = WideCharToMultiByte(CP_ACP, 0, wurl.c_str(), -1, &chars[0],
  146. mblen, nullptr, nullptr);
  147. if (mblen > 0) {
  148. url = &chars[0];
  149. }
  150. }
  151. }
  152. #endif
  153. return url;
  154. }