Skip to content

Commit a74288f

Browse files
committed
Parse PEM Key files leniantly (#33173)
Allow for extra non-whitespace before the Header of PEM encoded key files. Resolves #33168
1 parent 5c24164 commit a74288f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/PemUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class PemUtils {
5858
private static final String OPENSSL_EC_FOOTER = "-----END EC PRIVATE KEY-----";
5959
private static final String OPENSSL_EC_PARAMS_HEADER = "-----BEGIN EC PARAMETERS-----";
6060
private static final String OPENSSL_EC_PARAMS_FOOTER = "-----END EC PARAMETERS-----";
61+
private static final String HEADER = "-----BEGIN";
6162

6263
private PemUtils() {
6364
throw new IllegalStateException("Utility class should not be instantiated");
@@ -74,6 +75,9 @@ private PemUtils() {
7475
public static PrivateKey readPrivateKey(Path keyPath, Supplier<char[]> passwordSupplier) {
7576
try (BufferedReader bReader = Files.newBufferedReader(keyPath, StandardCharsets.UTF_8)) {
7677
String line = bReader.readLine();
78+
while (null != line && line.startsWith(HEADER) == false){
79+
line = bReader.readLine();
80+
}
7781
if (null == line) {
7882
throw new IllegalStateException("Error parsing Private Key from: " + keyPath.toString() + ". File is empty");
7983
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/PemUtilsTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public void testReadPKCS8RsaKey() throws Exception {
3232
assertThat(privateKey, equalTo(key));
3333
}
3434

35+
public void testReadPKCS8RsaKeyWithBagAttrs() throws Exception {
36+
Key key = getKeyFromKeystore("RSA");
37+
assertThat(key, notNullValue());
38+
assertThat(key, instanceOf(PrivateKey.class));
39+
PrivateKey privateKey = PemUtils.readPrivateKey(getDataPath
40+
("/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testnode_with_bagattrs.pem"), ""::toCharArray);
41+
assertThat(privateKey, notNullValue());
42+
assertThat(privateKey, equalTo(key));
43+
}
44+
3545
public void testReadPKCS8DsaKey() throws Exception {
3646
Key key = getKeyFromKeystore("DSA");
3747
assertThat(key, notNullValue());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Bag Attributes
2+
friendlyName: testnode_rsa
3+
localKeyID: 54 69 6D 65 20 31 35 32 35 33 33 36 38 32 39 33 39 37
4+
Key Attributes: <No Attributes>
5+
-----BEGIN PRIVATE KEY-----
6+
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDesZnVBuxbT4y7
7+
KtIuYx8MUq0sGQgVbxXSBG66sWDU9Qoo1HUyra0xXCONgRMBT9RjSIpk7OOC9g8q
8+
ENNgFO179YdHVkrgJhW/tNBf+C0VAb+B79zu7SwtyH2nt9t378dmItL+sERkMiiG
9+
+BS/O+cDz44hifDiS7Eqj/mJugAhLjWSUyD+UBObxXvUsxjryKeG3vX9mRCgAcqB
10+
xH3PjI1i9DVaoobwMbwpE5eW2WXexOspuXnMmGfrrR6z/VmdHqe/C3rGdJOX+Y0c
11+
yOR+/Vuzisn+nLeo/GJx2hIif8rKiNRyAdUXfx+4DLYJBN2NUbl9aP2LP6ZC8ubf
12+
6qwhhB0XAgMBAAECggEBAKuzP6qSNfaJNTayY2/EmRHFRSP1ANiV17sgE8f6L3DC
13+
pdypQtuaMSkXo4nc9SxTwqvyKFJ8m0ZENZj3dCJmwFyNCIqmLAD7HFW9MdRs40WJ
14+
HYEv0aaeUyvRo6CHD74/r/w96XTZr0GZssmtyUFRDGNRyoJter7gIW9xprLcKHFr
15+
YTmdaAXbOm5W/K3844EBouTYzYnZYWQjB3jT/g5dIic3AtLb5YfGlpaXXb74xTOU
16+
BqY1uKonGiDCh0aXXRl2Ucyre6FWslNNy4cAAXm6/5GT6iMo7wDXQftvtyK2IszP
17+
IFcOG6xcAaJjgZ5wvM3ch0qNhQi4vL7c4Bm5JS9meoECgYEA88Ita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