Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package org.elasticsearch.xpack.core.ssl;

import org.elasticsearch.common.ssl.PemUtils;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
Expand All @@ -22,6 +23,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
Expand Down Expand Up @@ -93,6 +95,23 @@ public static Certificate[] readCertificates(List<Path> certPaths) throws Certif
return certificates.toArray(new Certificate[0]);
}

public static X509Certificate readX509Certificate(Path path) throws CertificateException, IOException {
List<Certificate> certificates = PemUtils.readCertificates(List.of(path));
if (certificates.size() != 1) {
throw new IllegalArgumentException("expected a single certificate in file [" + path.toAbsolutePath() + "] but found [" +
certificates.size() + "]");
}
final Certificate cert = certificates.get(0);
if (cert instanceof X509Certificate) {
return (X509Certificate) cert;
} else {
throw new IllegalArgumentException("the certificate in " + path.toAbsolutePath() + " is not an X.509 certificate ("
+ cert.getType()
+ " : "
+ cert.getClass() + ")");
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't exactly in scope for this PR, but a test case was using the new method so it made sense to add it now..

@SuppressWarnings("unchecked")
public static X509Certificate[] readX509Certificates(List<Path> certPaths) throws CertificateException, IOException {
Collection<X509Certificate> certificates = new ArrayList<>();
Expand Down Expand Up @@ -149,8 +168,8 @@ public static Map<Certificate, Key> readKeyPairsFromKeystore(KeyStore store, Fun
/**
* Creates a {@link KeyStore} from a PEM encoded certificate and key file
*/
public static KeyStore getKeyStoreFromPEM(Path certificatePath, Path keyPath, char[] keyPassword)
throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
public static KeyStore getKeyStoreFromPEM(Path certificatePath, Path keyPath, char[] keyPassword) throws IOException,
GeneralSecurityException {
final PrivateKey key = PemUtils.readPrivateKey(keyPath, () -> keyPassword);
final Certificate[] certificates = readCertificates(Collections.singletonList(certificatePath));
return getKeyStore(certificates, key, keyPassword);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.elasticsearch.xpack.core.ssl;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.ssl.PemUtils;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.env.Environment;
Expand All @@ -22,6 +23,7 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.security.AccessControlException;
import java.security.GeneralSecurityException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
Expand Down Expand Up @@ -120,6 +122,8 @@ private static PrivateKey readPrivateKey(String keyPath, SecureString keyPasswor
throw unreadableKeyConfigFile(accessException, KEY_FILE, key);
} catch (AccessControlException securityException) {
throw blockedKeyConfigFile(securityException, environment, KEY_FILE, key);
} catch (GeneralSecurityException e) {
throw new IllegalStateException("Error parsing Private Key from: " + keyPath, e);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The X-Pack PemUtils would throw this IllegalStateException on GeneralSecurityException but the ssl-config version throws GeneralSecurityException.
This isn't the nicest way to handle it, but my plan is to get rid of this class (PEMKeyConfig) entirely, so I opted for the smallest possible change.

}
}

Expand Down
Loading