Skip to content

Commit

Permalink
add support for DER and P12 certs (#2411) (#2413)
Browse files Browse the repository at this point in the history
* add support for DER and P12 certs (#2411)

https://curl.haxx.se/libcurl/c/CURLOPT_SSLCERTTYPE.html

* add test for P12/DER cert types

* dont fail test because of cleanup error

Co-authored-by: Razvan Grigore <razvan.grigore@aboutyou.com>
  • Loading branch information
Razvan Grigore and Razvan Grigore committed Mar 10, 2021
1 parent da94ef2 commit 9687c73
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Handler/CurlFactory.php
Expand Up @@ -456,6 +456,12 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void
if (!\file_exists($cert)) {
throw new \InvalidArgumentException("SSL certificate not found: {$cert}");
}
# OpenSSL (versions 0.9.3 and later) also support "P12" for PKCS#12-encoded files.
# see https://curl.se/libcurl/c/CURLOPT_SSLCERTTYPE.html
$ext = pathinfo($cert, \PATHINFO_EXTENSION);
if (preg_match('#^(der|p12)$#i', $ext)) {
$conf[\CURLOPT_SSLCERTTYPE] = strtoupper($ext);
}
$conf[\CURLOPT_SSLCERT] = $cert;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/Handler/CurlFactoryTest.php
Expand Up @@ -277,6 +277,34 @@ public function testAddsCertWithPassword()
self::assertEquals('test', $_SERVER['_curl'][\CURLOPT_SSLCERTPASSWD]);
}

public function testAddsDerCert()
{
$certFile = tempnam(sys_get_temp_dir(), "mock_test_cert");
rename($certFile, $certFile .= '.der');
try {
$f = new Handler\CurlFactory(3);
$f->create(new Psr7\Request('GET', Server::$url), ['cert' => $certFile]);
self::assertArrayHasKey(\CURLOPT_SSLCERTTYPE, $_SERVER['_curl']);
self::assertEquals('DER', $_SERVER['_curl'][\CURLOPT_SSLCERTTYPE]);
} finally {
@\unlink($certFile);
}
}

public function testAddsP12Cert()
{
$certFile = tempnam(sys_get_temp_dir(), "mock_test_cert");
rename($certFile, $certFile .= '.p12');
try {
$f = new Handler\CurlFactory(3);
$f->create(new Psr7\Request('GET', Server::$url), ['cert' => $certFile]);
self::assertArrayHasKey(\CURLOPT_SSLCERTTYPE, $_SERVER['_curl']);
self::assertEquals('P12', $_SERVER['_curl'][\CURLOPT_SSLCERTTYPE]);
} finally {
@\unlink($certFile);
}
}

public function testValidatesProgress()
{
$f = new Handler\CurlFactory(3);
Expand Down

0 comments on commit 9687c73

Please sign in to comment.