Skip to content

Commit e34ace0

Browse files
authored
Add validation to DER parser for seq len (elastic#138683) (elastic#138697)
Add validation to org.elasticsearch.common.ssl.DerParser for sequence length that exceeds maximum signed int.
1 parent 219189f commit e34ace0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/DerParser.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ private int getLength() throws IOException {
138138
int n = derInputStream.read(bytes);
139139
if (n < num) throw new IOException("Invalid DER: length too short");
140140

141-
return new BigInteger(1, bytes).intValue();
141+
int len = new BigInteger(1, bytes).intValue();
142+
if (len < 0) {
143+
throw new IOException("Invalid DER: length larger than max-int");
144+
}
145+
146+
return len;
142147
}
143148

144149
/**

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/pki/RdnFieldExtractorTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ public void testExtractWithMalformedDerData() {
8888
assertThat(result, is(nullValue()));
8989
}
9090

91+
public void testSeqLengthOutOfSignedIntRange() {
92+
byte[] malformedBytes = {
93+
(byte) 48, // SEQUENCE
94+
(byte) 0x84, // Length byte indicating (1) long form with (2) 4 data bytes
95+
(byte) 0xFF,
96+
(byte) 0xFF,
97+
(byte) 0xFF,
98+
(byte) 0xFF };
99+
100+
String result = RdnFieldExtractor.extract(malformedBytes, OID_CN);
101+
assertThat(result, is(nullValue()));
102+
}
103+
91104
public void testExtractWithSpecialCharacters() {
92105
assertExtractions("CN=Test\\, User, OU=R\\+D, O=Elastic\\\\Co", Map.of(OID_CN, "Test, User", OID_OU, "R+D", OID_O, "Elastic\\Co"));
93106
}

0 commit comments

Comments
 (0)