We, as developers, craft bespoke apps that connect to remote servers, over encrypted connections. Those connections are authenticated using API keys, username and passwords or public certificate and private keys. Sometimes, those connections work out of the box, sometimes we’ll spend 6 hours figuring out what’s wrong.
The easiest way to establish a plain text connection from the terminal to a server, is to use telnet
then issue a HTTP command like
GET / and you’ll get Google’s homepage, which will be a redirect to a HTTPS version of the same page.
To establish a SSL connection, you can use the
openssl Swiss knife.
and then issue the same HTTP command
GET / and you’ll get the HTTPS version of the Google homepage.
OpenSSL can be used to open a connection that requires certificate authentication too, just supply those as CLI options. This way you can test that the Apple Push Notification connectivity, by using your developer certificate and private key. Just make sure that they are converted to the PEM file format first.
After you’ve figured out which developer certificate and which private key are still active and match your remote server, you can establish the same connection from PHP.
In most of the cases, after running this script, the output will be
However, if you’re unlucky, you’ll get the following output
After you double check several times, you reach the conclusion that you’re using the same certificate, key and CA certificate as above. You’ll start wondering if there is a difference when the connection is made using
openssl s_client and PHP’s
One of the ways to get a glimpse into what each program is doing, is to use DTrace if you’re lucky enough to use one of the OSes where’s available (like MacOS X, FreeBSD or SmartOS) or to use its poor friend strace.
After a careful check of those file’s output, you’ll notice that OpenSSL and PHP are using two different CA stores. Open SSL’s
/usr/lib/ssl/certs/, versus PHP’s
/usr/share/ca-certificates/mozilla/. You can spot this by checking what happens before getting the
stream_socket_client(): SSL operation failed with code 1. error message:
After figuring this little difference, it’s pretty easy to fix the PHP code. Change the
stream_context_create to include the new CA path:
Tip o’ the hat to Ben Hearsum’s Python and SSL Certificate Verification article for suggesting to use strace to figure out why do we get different results, while using the same certificates.