Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

SSL CA File support. #5870

Merged
merged 2 commits into from
Mar 5, 2014
Merged

SSL CA File support. #5870

merged 2 commits into from
Mar 5, 2014

Conversation

icywolfy
Copy link
Contributor

Environment
Centos 6; presumably others.

Problem
Using the default Socket client adapter doesn't work.
People give up in trying to get the SSL Certificates working, and thus set ssl_verify_peer = false to get it working.
The advice "Set the ssl_ca_path option" doesn't work, and errors.

For example, By using a the /etc/ssl/certs directory here (or it's resolved location of /etc/pki/tls/certs, you end up with the Socket adapter giving the error:
"error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed"

This is an issue repeated many times on the internet, and I've run into it personally often (and admittedly, had just disabled verify_peer for a while to get development working)

Background
The underlying issue is that these systems are incompatible with the ssl stream context options capath. In that the directory contains a certificate bundle file and not (as the PHP manual states) a "correctly hashed certificate directory"

Where according to the OpenSSL documentation, the direcotry must contain files in the form hash.0 where hash is the hashed certificate subject name.

Solution
OpenSSL Documentation states that the cafile option contains "A file of trusted certificates. The file should contain multiple certificates in PEM format concatenated together."

And thus the correct answer is to set the context option cafile to '/etc/ssl/certs/ca-bundle.crt' on these systems. (I have seen on some client systems the filename being certificate-bundle.crt, so it will be highly environment specific)

If you set an non existant file, and have openssl_error_string, the result will be :

SSL error: error:02001002:system library:fopen:No such file or directory; 
SSL error: error:2006D080:BIO routines:BIO_new_file:no such file; 
SSL error: error:0B084002:x509 certificate routines:X509_load_cert_crl_file:system lib

In addition to:
stream_socket_enable_crypto(): failed to create an SSL handle
stream_socket_enable_crypto(): Unable to set verify locations '/etc/ssl/certs/ca-bundle.cret'

Workaround For < 2.2.5*
Currently, for the Socket Adapter to work one needs to have a factory to manually make use of the cafile; rather than pass in the configuration array verbatim.

$sslcafile = '/etc/ssl/certs/ca-bundle.crt';
$context =  $client->getAdapter()->getStreamContext();
if (!stream_context_set_option($context, 'ssl', 'cafile', $sslcafile))  {
   throw new AdapterException\RuntimeException('Unable to set sslcafile option');
}

@Maks3w
Copy link
Member

Maks3w commented Feb 25, 2014

Please add tests

@icywolfy
Copy link
Contributor Author

Added tests, and valid ca-bundle.crt file to minimize changes.

As there are no tests existing that test the "sslcapath" option; I just went with something that just tests that the context option was set on connect based on testConnectingViaSslEnforcesDefaultSslOptionsOnContext()

As this is may be highly system-dependent, it's hard to say whether the certificate file will parse correctly and set the context option across multiple environments (ie: Windows)

@weierophinney weierophinney added this to the 2.2.6 milestone Mar 3, 2014
@EvanDotPro EvanDotPro self-assigned this Mar 4, 2014
@weierophinney weierophinney assigned ezimuel and unassigned EvanDotPro Mar 4, 2014
@ezimuel
Copy link
Contributor

ezimuel commented Mar 5, 2014

@icywolfy thanks for this PR, I think make perfectly sense to support the CA file for SSL.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants