22
22
import org .elasticsearch .test .ESTestCase ;
23
23
import org .hamcrest .Matchers ;
24
24
25
- import javax .net .ssl .X509ExtendedTrustManager ;
26
25
import java .nio .file .Files ;
27
26
import java .nio .file .NoSuchFileException ;
28
27
import java .nio .file .Path ;
37
36
import java .util .stream .Collectors ;
38
37
import java .util .stream .Stream ;
39
38
39
+ import javax .net .ssl .X509ExtendedTrustManager ;
40
+
40
41
public class PemTrustConfigTests extends ESTestCase {
41
42
42
43
public void testBuildTrustConfigFromSinglePemFile () throws Exception {
@@ -57,7 +58,7 @@ public void testBuildTrustConfigFromMultiplePemFiles() throws Exception {
57
58
58
59
public void testBadFileFormatFails () throws Exception {
59
60
final Path ca = createTempFile ("ca" , ".crt" );
60
- Files .write (ca , randomByteArrayOfLength (128 ), StandardOpenOption .APPEND );
61
+ Files .write (ca , generateRandomByteArrayOfLength (128 ), StandardOpenOption .APPEND );
61
62
final PemTrustConfig trustConfig = new PemTrustConfig (Collections .singletonList (ca ));
62
63
assertThat (trustConfig .getDependentFiles (), Matchers .containsInAnyOrder (ca ));
63
64
assertInvalidFileFormat (trustConfig , ca );
@@ -107,7 +108,7 @@ public void testTrustConfigReloadsFileContents() throws Exception {
107
108
Files .delete (ca1 );
108
109
assertFileNotFound (trustConfig , ca1 );
109
110
110
- Files .write (ca1 , randomByteArrayOfLength (128 ), StandardOpenOption .CREATE );
111
+ Files .write (ca1 , generateRandomByteArrayOfLength (128 ), StandardOpenOption .CREATE );
111
112
assertInvalidFileFormat (trustConfig , ca1 );
112
113
}
113
114
@@ -149,4 +150,24 @@ private void assertFileNotFound(PemTrustConfig trustConfig, Path file) {
149
150
assertThat (exception .getMessage (), Matchers .containsString (file .toAbsolutePath ().toString ()));
150
151
assertThat (exception .getCause (), Matchers .instanceOf (NoSuchFileException .class ));
151
152
}
153
+
154
+ private byte [] generateRandomByteArrayOfLength (int length ) {
155
+ byte [] bytes = randomByteArrayOfLength (length );
156
+ /*
157
+ * If the bytes represent DER encoded value indicating ASN.1 SEQUENCE followed by length byte if it is zero then while trying to
158
+ * parse PKCS7 block from the encoded stream, it failed parsing the content type. The DerInputStream.getSequence() method in this
159
+ * case returns an empty DerValue array but ContentType does not check the length of array before accessing the array resulting in a
160
+ * ArrayIndexOutOfBoundsException. This check ensures that when we create random stream of bytes we do not create ASN.1 SEQUENCE
161
+ * followed by zero length which fails the test intermittently.
162
+ */
163
+ while (checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound (bytes )) {
164
+ bytes = randomByteArrayOfLength (length );
165
+ }
166
+ return bytes ;
167
+ }
168
+
169
+ private static boolean checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound (byte [] bytes ) {
170
+ // Tag value indicating an ASN.1 "SEQUENCE". Reference: sun.security.util.DerValue.tag_Sequence = 0x30
171
+ return bytes [0 ] == 0x30 && bytes [1 ] == 0x00 ;
172
+ }
152
173
}
0 commit comments