Search in sources :

Example 1 with SignatureFileParsingException

use of com.hedera.mirror.importer.exception.SignatureFileParsingException in project hedera-mirror-node by hashgraph.

the class CompositeSignatureFileReaderTest method testInvalidFileVersion.

@Test
void testInvalidFileVersion() {
    byte[] invalidVersionNumber = { 12 };
    StreamFileData invalidFileData = StreamFileData.from(SIGNATURE_FILENAME, invalidVersionNumber);
    SignatureFileParsingException exception = assertThrows(SignatureFileParsingException.class, () -> {
        compositeBalanceFileReader.read(invalidFileData);
    });
    assertTrue(exception.getMessage().contains("Unsupported signature file version: " + invalidVersionNumber[0]));
}
Also used : SignatureFileParsingException(com.hedera.mirror.importer.exception.SignatureFileParsingException) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) Test(org.junit.jupiter.api.Test)

Example 2 with SignatureFileParsingException

use of com.hedera.mirror.importer.exception.SignatureFileParsingException in project hedera-mirror-node by hashgraph.

the class CompositeSignatureFileReader method read.

@Override
public FileStreamSignature read(StreamFileData signatureFileData) {
    try (DataInputStream dataInputStream = new DataInputStream(signatureFileData.getInputStream())) {
        byte version = dataInputStream.readByte();
        SignatureFileReader fileReader;
        if (version == SignatureFileReaderV5.SIGNATURE_FILE_FORMAT_VERSION) {
            fileReader = signatureFileReaderV5;
        } else if (version <= SignatureFileReaderV2.SIGNATURE_TYPE_FILE_HASH) {
            // Begins with a byte of value 4
            fileReader = signatureFileReaderV2;
        } else {
            throw new SignatureFileParsingException("Unsupported signature file version: " + version);
        }
        return fileReader.read(signatureFileData);
    } catch (IOException ex) {
        throw new SignatureFileParsingException("Error reading signature file", ex);
    }
}
Also used : SignatureFileParsingException(com.hedera.mirror.importer.exception.SignatureFileParsingException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream)

Example 3 with SignatureFileParsingException

use of com.hedera.mirror.importer.exception.SignatureFileParsingException in project hedera-mirror-node by hashgraph.

the class SignatureFileReaderV2 method read.

@Override
public FileStreamSignature read(StreamFileData signatureFileData) {
    String filename = signatureFileData.getFilename();
    try (ValidatedDataInputStream vdis = new ValidatedDataInputStream(signatureFileData.getInputStream(), filename)) {
        vdis.readByte(SIGNATURE_TYPE_FILE_HASH, "hash delimiter");
        byte[] fileHash = vdis.readNBytes(DigestAlgorithm.SHA384.getSize(), "hash");
        vdis.readByte(SIGNATURE_TYPE_SIGNATURE, "signature delimiter");
        byte[] signature = vdis.readLengthAndBytes(1, SignatureType.SHA_384_WITH_RSA.getMaxLength(), false, "signature");
        if (vdis.available() != 0) {
            throw new SignatureFileParsingException("Extra data discovered in signature file " + filename);
        }
        FileStreamSignature fileStreamSignature = new FileStreamSignature();
        fileStreamSignature.setBytes(signatureFileData.getBytes());
        fileStreamSignature.setFileHash(fileHash);
        fileStreamSignature.setFileHashSignature(signature);
        fileStreamSignature.setFilename(filename);
        fileStreamSignature.setSignatureType(SignatureType.SHA_384_WITH_RSA);
        return fileStreamSignature;
    } catch (InvalidStreamFileException | IOException e) {
        throw new SignatureFileParsingException(e);
    }
}
Also used : SignatureFileParsingException(com.hedera.mirror.importer.exception.SignatureFileParsingException) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) ValidatedDataInputStream(com.hedera.mirror.importer.reader.ValidatedDataInputStream) IOException(java.io.IOException) FileStreamSignature(com.hedera.mirror.importer.domain.FileStreamSignature)

Example 4 with SignatureFileParsingException

use of com.hedera.mirror.importer.exception.SignatureFileParsingException in project hedera-mirror-node by hashgraph.

the class SignatureFileReaderV5 method read.

@Override
public FileStreamSignature read(StreamFileData signatureFileData) {
    String filename = signatureFileData.getFilename();
    try (ValidatedDataInputStream vdis = new ValidatedDataInputStream(signatureFileData.getInputStream(), filename)) {
        vdis.readByte(SIGNATURE_FILE_FORMAT_VERSION, "fileVersion");
        // Read the objectStreamSignatureVersion, which is not used
        vdis.readInt();
        HashObject fileHashObject = new HashObject(vdis, "entireFile", SHA384);
        SignatureObject fileHashSignatureObject = new SignatureObject(vdis, "entireFile");
        HashObject metadataHashObject = new HashObject(vdis, "metadata", SHA384);
        SignatureObject metadataHashSignatureObject = new SignatureObject(vdis, "metadata");
        if (vdis.available() != 0) {
            throw new SignatureFileParsingException("Extra data discovered in signature file " + filename);
        }
        FileStreamSignature fileStreamSignature = new FileStreamSignature();
        fileStreamSignature.setBytes(signatureFileData.getBytes());
        fileStreamSignature.setFileHash(fileHashObject.getHash());
        fileStreamSignature.setFileHashSignature(fileHashSignatureObject.getSignature());
        fileStreamSignature.setFilename(filename);
        fileStreamSignature.setMetadataHash(metadataHashObject.getHash());
        fileStreamSignature.setMetadataHashSignature(metadataHashSignatureObject.getSignature());
        fileStreamSignature.setSignatureType(fileHashSignatureObject.getSignatureType());
        return fileStreamSignature;
    } catch (InvalidStreamFileException | IOException e) {
        throw new SignatureFileParsingException(e);
    }
}
Also used : SignatureFileParsingException(com.hedera.mirror.importer.exception.SignatureFileParsingException) InvalidStreamFileException(com.hedera.mirror.importer.exception.InvalidStreamFileException) ValidatedDataInputStream(com.hedera.mirror.importer.reader.ValidatedDataInputStream) HashObject(com.hedera.mirror.importer.reader.HashObject) IOException(java.io.IOException) FileStreamSignature(com.hedera.mirror.importer.domain.FileStreamSignature)

Example 5 with SignatureFileParsingException

use of com.hedera.mirror.importer.exception.SignatureFileParsingException in project hedera-mirror-node by hashgraph.

the class CompositeSignatureFileReaderTest method testBlankFile.

@Test
void testBlankFile() {
    StreamFileData blankFileData = StreamFileData.from(SIGNATURE_FILENAME, new byte[0]);
    SignatureFileParsingException exception = assertThrows(SignatureFileParsingException.class, () -> {
        compositeBalanceFileReader.read(blankFileData);
    });
    assertAll(() -> assertTrue(exception.getMessage().contains("Error reading signature file")), () -> assertTrue(exception.getCause() instanceof IOException));
}
Also used : SignatureFileParsingException(com.hedera.mirror.importer.exception.SignatureFileParsingException) StreamFileData(com.hedera.mirror.importer.domain.StreamFileData) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

SignatureFileParsingException (com.hedera.mirror.importer.exception.SignatureFileParsingException)5 IOException (java.io.IOException)4 FileStreamSignature (com.hedera.mirror.importer.domain.FileStreamSignature)2 StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)2 InvalidStreamFileException (com.hedera.mirror.importer.exception.InvalidStreamFileException)2 ValidatedDataInputStream (com.hedera.mirror.importer.reader.ValidatedDataInputStream)2 Test (org.junit.jupiter.api.Test)2 HashObject (com.hedera.mirror.importer.reader.HashObject)1 DataInputStream (java.io.DataInputStream)1