Add option to set CA certificate path

Adds options to set path to CA certificate bundle in PEM format.
CURL_CA_BUNDLE environment variable is used if it is set and CA certificate path is not set with --cacert option.
This commit is contained in:
Sude 2016-08-30 19:34:47 +03:00
parent 8f800c602f
commit 0b455ddedc
4 changed files with 23 additions and 0 deletions

View File

@ -76,6 +76,7 @@ class Config
std::string sLanguagePriority;
std::string sPlatformPriority;
std::string sIgnoreDLCCountRegex;
std::string sCACertPath;
std::vector<unsigned int> vLanguagePriority;
std::vector<unsigned int> vPlatformPriority;

View File

@ -143,6 +143,7 @@ int main(int argc, char *argv[])
("wishlist", bpo::value<bool>(&config.bShowWishlist)->zero_tokens()->default_value(false), "Show wishlist")
("login-api", bpo::value<bool>(&config.bLoginAPI)->zero_tokens()->default_value(false), "Login (API only)")
("login-website", bpo::value<bool>(&config.bLoginHTTP)->zero_tokens()->default_value(false), "Login (website only)")
("cacert", bpo::value<std::string>(&config.sCACertPath)->default_value(""), "Path to CA certificate bundle in PEM format")
;
// Commandline options (config file)
options_cli_cfg.add_options()
@ -428,6 +429,15 @@ int main(int argc, char *argv[])
config.sDirectory = "./"; // Directory wasn't specified, use current directory
}
// CA certificate bundle
if (config.sCACertPath.empty())
{
// Use CURL_CA_BUNDLE environment variable for CA certificate path if it is set
char *ca_bundle = getenv("CURL_CA_BUNDLE");
if (ca_bundle)
config.sCACertPath = (std::string)ca_bundle;
}
if (!unrecognized_options_cfg.empty() && (!config.bSaveConfig || !config.bResetConfig))
{
std::cerr << "Unrecognized options in " << config.sConfigFilePath << std::endl;

View File

@ -88,6 +88,9 @@ int Downloader::init()
curl_easy_setopt(curlhandle, CURLOPT_LOW_SPEED_TIME, 30);
curl_easy_setopt(curlhandle, CURLOPT_LOW_SPEED_LIMIT, 200);
if (!config.sCACertPath.empty())
curl_easy_setopt(curlhandle, CURLOPT_CAINFO, config.sCACertPath.c_str());
// Create new GOG website handle
gogWebsite = new Website(config);
bool bWebsiteIsLoggedIn = gogWebsite->IsLoggedIn();
@ -97,6 +100,8 @@ int Downloader::init()
gogAPI->curlSetOpt(CURLOPT_VERBOSE, config.bVerbose);
gogAPI->curlSetOpt(CURLOPT_SSL_VERIFYPEER, config.bVerifyPeer);
gogAPI->curlSetOpt(CURLOPT_CONNECTTIMEOUT, config.iTimeout);
if (!config.sCACertPath.empty())
gogAPI->curlSetOpt(CURLOPT_CAINFO, config.sCACertPath.c_str());
progressbar = new ProgressBar(config.bUnicode, config.bColor);

View File

@ -31,6 +31,8 @@ Website::Website(Config &conf)
curl_easy_setopt(curlhandle, CURLOPT_LOW_SPEED_TIME, 30);
curl_easy_setopt(curlhandle, CURLOPT_LOW_SPEED_LIMIT, 200);
if (!config.sCACertPath.empty())
curl_easy_setopt(curlhandle, CURLOPT_CAINFO, config.sCACertPath.c_str());
}
Website::~Website()
@ -80,6 +82,11 @@ std::string Website::getResponse(const std::string& url)
else
std::cout << "failed to get error code: " << curl_easy_strerror(result) << " (" << url << ")" << std::endl;
}
else if (result == CURLE_SSL_CACERT)
{
std::cout << "Try using CA certificate bundle from cURL: https://curl.haxx.se/ca/cacert.pem" << std::endl;
std::cout << "Use --cacert to set the path for CA certificate bundle" << std::endl;
}
}
return response;