schannel: add support for CURLOPT_CAINFO

- Move verify_certificate functionality in schannel.c into a new
  file called schannel_verify.c. Additionally, some structure defintions
  from schannel.c have been moved to schannel.h to allow them to be
  used in schannel_verify.c.

- Make verify_certificate functionality for Schannel available on
  all versions of Windows instead of just Windows CE. verify_certificate
  will be invoked on Windows CE or when the user specifies
  CURLOPT_CAINFO and CURLOPT_SSL_VERIFYPEER.

- In verify_certificate, create a custom certificate chain engine that
  exclusively trusts the certificate store backed by the CURLOPT_CAINFO
  file.

- doc updates of --cacert/CAINFO support for schannel

- Use CERT_NAME_SEARCH_ALL_NAMES_FLAG when invoking CertGetNameString
  when available. This implements a TODO in schannel.c to improve
  handling of multiple SANs in a certificate. In particular, all SANs
  will now be searched instead of just the first name.

- Update tool_operate.c to not search for the curl-ca-bundle.crt file
  when using Schannel to maintain backward compatibility. Previously,
  any curl-ca-bundle.crt file found in that search would have been
  ignored by Schannel. But, with CAINFO support, the file found by
  that search would have been used as the certificate store and
  could cause issues for any users that have curl-ca-bundle.crt in
  the search path.

- Update url.c to not set the build time CURL_CA_BUNDLE if the selected
  SSL backend is Schannel. We allow setting CA location for schannel
  only when explicitly specified by the user via CURLOPT_CAINFO /
  --cacert.

- Add new test cases 3000 and 3001. These test cases check that the first
  and last SAN, respectively, matches the connection hostname. New test
  certificates have been added for these cases. For 3000, the certificate
  prefix is Server-localhost-firstSAN and for 3001, the certificate
  prefix is Server-localhost-secondSAN.

- Remove TODO 15.2 (Add support for custom server certificate
  validation), this commit addresses it.

Closes https://github.com/curl/curl/pull/1325
This commit is contained in:
Dan McNulty 2017-03-10 14:27:30 -06:00 committed by Jay Satiro
parent 4d660fdcb0
commit 8996300211
37 changed files with 1406 additions and 258 deletions

View file

@ -638,12 +638,19 @@ char **__crt0_glob_function(char *arg)
*/
CURLcode FindWin32CACert(struct OperationConfig *config,
curl_sslbackend backend,
const char *bundle_file)
{
CURLcode result = CURLE_OK;
/* search and set cert file only if libcurl supports SSL */
if(curlinfo->features & CURL_VERSION_SSL) {
/* Search and set cert file only if libcurl supports SSL.
*
* If Schannel (WinSSL) is the selected SSL backend then these locations
* are ignored. We allow setting CA location for schannel only when
* explicitly specified by the user via CURLOPT_CAINFO / --cacert.
*/
if((curlinfo->features & CURL_VERSION_SSL) &&
backend != CURLSSLBACKEND_SCHANNEL) {
DWORD res_len;
char buf[PATH_MAX];

View file

@ -58,6 +58,7 @@ char **__crt0_glob_function(char *arg);
#ifdef WIN32
CURLcode FindWin32CACert(struct OperationConfig *config,
curl_sslbackend backend,
const char *bundle_file);
#endif /* WIN32 */

View file

@ -228,52 +228,76 @@ static CURLcode operate_do(struct GlobalConfig *global,
if(!config->cacert &&
!config->capath &&
!config->insecure_ok) {
char *env;
env = curlx_getenv("CURL_CA_BUNDLE");
if(env) {
config->cacert = strdup(env);
if(!config->cacert) {
curl_free(env);
helpf(global->errors, "out of memory\n");
result = CURLE_OUT_OF_MEMORY;
goto quit_curl;
}
struct curl_tlssessioninfo *tls_backend_info = NULL;
/* With the addition of CAINFO support for Schannel, this search could find
* a certificate bundle that was previously ignored. To maintain backward
* compatibility, only perform this search if not using Schannel.
*/
result = curl_easy_getinfo(config->easy,
CURLINFO_TLS_SSL_PTR,
&tls_backend_info);
if(result) {
goto quit_curl;
}
else {
env = curlx_getenv("SSL_CERT_DIR");
/* Set the CA cert locations specified in the environment. For Windows if
* no environment-specified filename is found then check for CA bundle
* default filename curl-ca-bundle.crt in the user's PATH.
*
* If Schannel (WinSSL) is the selected SSL backend then these locations
* are ignored. We allow setting CA location for schannel only when
* explicitly specified by the user via CURLOPT_CAINFO / --cacert.
*/
if(tls_backend_info->backend != CURLSSLBACKEND_SCHANNEL) {
char *env;
env = curlx_getenv("CURL_CA_BUNDLE");
if(env) {
config->capath = strdup(env);
if(!config->capath) {
config->cacert = strdup(env);
if(!config->cacert) {
curl_free(env);
helpf(global->errors, "out of memory\n");
result = CURLE_OUT_OF_MEMORY;
goto quit_curl;
}
capath_from_env = true;
}
else {
env = curlx_getenv("SSL_CERT_FILE");
env = curlx_getenv("SSL_CERT_DIR");
if(env) {
config->cacert = strdup(env);
if(!config->cacert) {
config->capath = strdup(env);
if(!config->capath) {
curl_free(env);
helpf(global->errors, "out of memory\n");
result = CURLE_OUT_OF_MEMORY;
goto quit_curl;
}
capath_from_env = true;
}
else {
env = curlx_getenv("SSL_CERT_FILE");
if(env) {
config->cacert = strdup(env);
if(!config->cacert) {
curl_free(env);
helpf(global->errors, "out of memory\n");
result = CURLE_OUT_OF_MEMORY;
goto quit_curl;
}
}
}
}
}
if(env)
curl_free(env);
if(env)
curl_free(env);
#ifdef WIN32
else {
result = FindWin32CACert(config, "curl-ca-bundle.crt");
if(result)
goto quit_curl;
}
else {
result = FindWin32CACert(config, tls_backend_info->backend,
"curl-ca-bundle.crt");
if(result)
goto quit_curl;
}
#endif
}
}
if(config->postfields) {