Throw an exception when unable to read Certificate (#40092)

With SUN security provider, a CertificateException is thrown when
attempting to parse a Certificate from a PEM file on disk with
`sun.security.provider.X509Provider#parseX509orPKCS7Cert`

When using the BouncyCastle Security provider (as we do in fips
tests) the parsing happens in
CertificateFactory#engineGenerateCertificates which doesn't throw
an exception but returns an empty list.

In order to have a consistent behavior, this change makes it so
that we throw a CertificateException when attempting to read
a PEM file from disk and failing to do so in either Security
Provider

Resolves: #39580
This commit is contained in:
Ioannis Kakavas 2019-03-18 08:45:50 +02:00 committed by Ioannis Kakavas
parent 124de8d938
commit 3b9a884f92
2 changed files with 3 additions and 1 deletions

View File

@ -92,6 +92,9 @@ public class CertParsingUtils {
for (Path path : certPaths) {
try (InputStream input = Files.newInputStream(path)) {
certificates.addAll((Collection<Certificate>) certFactory.generateCertificates(input));
if (certificates.isEmpty()) {
throw new CertificateException("failed to parse any certificates from [" + path.toAbsolutePath() + "]");
}
}
}
return certificates.toArray(new Certificate[0]);

View File

@ -462,7 +462,6 @@ public class SSLConfigurationReloaderTests extends ESTestCase {
* truncating the certificate file that is being monitored
*/
public void testPEMTrustReloadException() throws Exception {
assumeFalse("Broken on BC-FIPS -- https://github.com/elastic/elasticsearch/issues/39580", inFipsJvm());
Path tempDir = createTempDir();
Path clientCertPath = tempDir.resolve("testclient.crt");
Files.copy(getDataPath("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.crt"), clientCertPath);