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

[stable28] fix: gracefully parse non-standard trusted certificates #48443

Merged
merged 1 commit into from
Sep 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/private/Security/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function __construct(string $data, string $name) {
}

$info = openssl_x509_parse($data);
if (!is_array($info)) {
// There is a non-standardized certificate format only used by OpenSSL. Replace all
// separators and try again.
$data = str_replace(
['-----BEGIN TRUSTED CERTIFICATE-----', '-----END TRUSTED CERTIFICATE-----'],
['-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----'],
$data,
);
$info = openssl_x509_parse($data);
}
if (!is_array($info)) {
throw new \Exception('Certificate could not get parsed.');
}
Expand Down
25 changes: 25 additions & 0 deletions tests/data/certificates/openSslTrustedCertificate.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-----BEGIN TRUSTED CERTIFICATE-----
MIIEIDCCAwigAwIBAgIQNE7VVyDV7exJ9C/ON9srbTANBgkqhkiG9w0BAQUFADCB
qTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMf
Q2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjE4MDYGA1UECxMvKGMpIDIw
MDYgdGhhd3RlLCBJbmMuIC0gRm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkxHzAdBgNV
BAMTFnRoYXd0ZSBQcmltYXJ5IFJvb3QgQ0EwHhcNMDYxMTE3MDAwMDAwWhcNMzYw
NzE2MjM1OTU5WjCBqTELMAkGA1UEBhMCVVMx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PB7
jVaMaDAMMAoGCCsGAQUFBwMB
-----END TRUSTED CERTIFICATE-----
9 changes: 7 additions & 2 deletions tests/lib/Security/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function setUp(): void {
$this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
}


public function testBogusData() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Certificate could not get parsed.');
Expand All @@ -54,7 +54,12 @@ public function testBogusData() {
$certificate->getIssueDate();
}


public function testOpenSslTrustedCertificateFormat(): void {
$trustedCertificate = file_get_contents(__DIR__ . '/../../data/certificates/openSslTrustedCertificate.crt');
$certificate = new Certificate($trustedCertificate, 'TrustedCertificate');
$this->assertSame('thawte, Inc.', $certificate->getOrganization());
}

public function testCertificateStartingWithFileReference() {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Certificate could not get parsed.');
Expand Down
Loading