1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <curl/curl.h>
14 #include <officecfg/Office/Security.hxx>
16 // curl is built with --with-secure-transport on macOS and iOS so doesn't need these
17 // certs. Windows doesn't need them either, but let's assume everything else does
18 #if !defined(SYSTEM_OPENSSL) && !defined(_WIN32) && !defined(MACOSX) && !defined(IOS)
19 #include <com/sun/star/uno/RuntimeException.hpp>
21 #define LO_CURL_NEEDS_CA_BUNDLE
22 #include "opensslinit.hxx"
25 #include <rtl/string.hxx>
26 #include <sal/log.hxx>
28 #include <config_version.h>
30 static void InitCurl_easy(CURL
* const pCURL
)
34 #if defined(LO_CURL_NEEDS_CA_BUNDLE)
35 char const* const path
= GetCABundleFile();
38 #if defined EMSCRIPTEN
39 SAL_WARN("ucb.ucp.webdav.curl", "no OpenSSL CA certificate bundle found");
41 throw css::uno::RuntimeException(u
"no OpenSSL CA certificate bundle found"_ustr
);
46 rc
= curl_easy_setopt(pCURL
, CURLOPT_CAINFO
, path
);
47 if (rc
!= CURLE_OK
) // only if OOM?
49 throw css::uno::RuntimeException(u
"CURLOPT_CAINFO failed"_ustr
);
54 // curl: "If you have a CA cert for the server stored someplace else than
55 // in the default bundle, then the CURLOPT_CAPATH option might come handy
57 if (char const* const capath
= getenv("LO_CERTIFICATE_AUTHORITY_PATH"))
59 rc
= curl_easy_setopt(pCURL
, CURLOPT_CAPATH
, capath
);
62 throw css::uno::RuntimeException("CURLOPT_CAPATH failed");
66 if (!officecfg::Office::Security::Net::AllowInsecureProtocols::get())
68 rc
= curl_easy_setopt(pCURL
, CURLOPT_SSLVERSION
, CURL_SSLVERSION_TLSv1_2
);
69 assert(rc
== CURLE_OK
);
70 rc
= curl_easy_setopt(pCURL
, CURLOPT_PROXY_SSLVERSION
, CURL_SSLVERSION_TLSv1_2
);
71 assert(rc
== CURLE_OK
);
72 #if (LIBCURL_VERSION_MAJOR > 7) || (LIBCURL_VERSION_MAJOR == 7 && LIBCURL_VERSION_MINOR >= 85)
73 rc
= curl_easy_setopt(pCURL
, CURLOPT_PROTOCOLS_STR
, "https");
74 assert(rc
== CURLE_OK
);
75 rc
= curl_easy_setopt(pCURL
, CURLOPT_REDIR_PROTOCOLS_STR
, "https");
76 assert(rc
== CURLE_OK
);
78 rc
= curl_easy_setopt(pCURL
, CURLOPT_PROTOCOLS
, CURLPROTO_HTTPS
);
79 assert(rc
== CURLE_OK
);
80 rc
= curl_easy_setopt(pCURL
, CURLOPT_REDIR_PROTOCOLS
, CURLPROTO_HTTPS
);
81 assert(rc
== CURLE_OK
);
85 curl_version_info_data
const* const pVersion(curl_version_info(CURLVERSION_NOW
));
87 SAL_INFO("ucb.ucp.webdav.curl",
88 "curl version: " << pVersion
->version
<< " " << pVersion
->host
89 << " features: " << ::std::hex
<< pVersion
->features
<< " ssl: "
90 << pVersion
->ssl_version
<< " libz: " << pVersion
->libz_version
);
91 // Make sure a User-Agent header is always included, as at least
92 // en.wikipedia.org:80 forces back 403 "Scripts should use an informative
93 // User-Agent string with contact information, or they may be IP-blocked
94 // without notice" otherwise:
95 OString
const useragent(
96 OString::Concat("LibreOffice " LIBO_VERSION_DOTTED
" denylistedbackend/")
97 + pVersion
->version
+ " " + pVersion
->ssl_version
);
98 // looks like an explicit "User-Agent" header in CURLOPT_HTTPHEADER
99 // will override CURLOPT_USERAGENT, see Curl_http_useragent(), so no need
100 // to check anything here
101 rc
= curl_easy_setopt(pCURL
, CURLOPT_USERAGENT
, useragent
.getStr());
102 assert(rc
== CURLE_OK
);
105 #undef LO_CURL_NEEDS_CA_BUNDLE
107 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */