Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for DER and P12 certs (#2411) #2413

Merged
merged 4 commits into from Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -254,6 +254,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