Skip to content

Fix for PemTrustConfigTests.testTrustConfigReloadsFileContents failure #43539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2019
Merged
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 @@ -22,7 +22,6 @@
import org.elasticsearch.test.ESTestCase;
import org.hamcrest.Matchers;

import javax.net.ssl.X509ExtendedTrustManager;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
Expand All @@ -37,6 +36,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.net.ssl.X509ExtendedTrustManager;

public class PemTrustConfigTests extends ESTestCase {

public void testBuildTrustConfigFromSinglePemFile() throws Exception {
Expand All @@ -57,7 +58,7 @@ public void testBuildTrustConfigFromMultiplePemFiles() throws Exception {

public void testBadFileFormatFails() throws Exception {
final Path ca = createTempFile("ca", ".crt");
Files.write(ca, randomByteArrayOfLength(128), StandardOpenOption.APPEND);
Files.write(ca, generateRandomByteArrayOfLength(128), StandardOpenOption.APPEND);
final PemTrustConfig trustConfig = new PemTrustConfig(Collections.singletonList(ca));
assertThat(trustConfig.getDependentFiles(), Matchers.containsInAnyOrder(ca));
assertInvalidFileFormat(trustConfig, ca);
Expand Down Expand Up @@ -106,7 +107,7 @@ public void testTrustConfigReloadsFileContents() throws Exception {
Files.delete(ca1);
assertFileNotFound(trustConfig, ca1);

Files.write(ca1, randomByteArrayOfLength(128), StandardOpenOption.CREATE);
Files.write(ca1, generateRandomByteArrayOfLength(128), StandardOpenOption.CREATE);
assertInvalidFileFormat(trustConfig, ca1);
}

Expand Down Expand Up @@ -148,4 +149,24 @@ private void assertFileNotFound(PemTrustConfig trustConfig, Path file) {
assertThat(exception.getMessage(), Matchers.containsString(file.toAbsolutePath().toString()));
assertThat(exception.getCause(), Matchers.instanceOf(NoSuchFileException.class));
}

private byte[] generateRandomByteArrayOfLength(int length) {
byte[] bytes = randomByteArrayOfLength(length);
/*
* If the bytes represent DER encoded value indicating ASN.1 SEQUENCE followed by length byte if it is zero then while trying to
* parse PKCS7 block from the encoded stream, it failed parsing the content type. The DerInputStream.getSequence() method in this
* case returns an empty DerValue array but ContentType does not check the length of array before accessing the array resulting in a
* ArrayIndexOutOfBoundsException. This check ensures that when we create random stream of bytes we do not create ASN.1 SEQUENCE
* followed by zero length which fails the test intermittently.
*/
while(checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound(bytes)) {
bytes = randomByteArrayOfLength(length);
}
return bytes;
}

private static boolean checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound(byte[] bytes) {
// Tag value indicating an ASN.1 "SEQUENCE". Reference: sun.security.util.DerValue.tag_Sequence = 0x30
return bytes[0] == 0x30 && bytes[1] == 0x00;
}
}