Search in sources :

Example 16 with FileStreamSignature

use of com.hedera.mirror.importer.domain.FileStreamSignature in project hedera-mirror-node by hashgraph.

the class NodeSignatureVerifierTest method testV2FileStreamSignature.

@Test
void testV2FileStreamSignature() throws GeneralSecurityException {
    byte[] fileHash = TestUtils.generateRandomByteArray(48);
    FileStreamSignature fileStreamSignature = buildFileStreamSignature(fileHash, signHash(fileHash), null, null);
    nodeSignatureVerifier.verify(Arrays.asList(fileStreamSignature));
}
Also used : FileStreamSignature(com.hedera.mirror.importer.domain.FileStreamSignature) Test(org.junit.jupiter.api.Test)

Example 17 with FileStreamSignature

use of com.hedera.mirror.importer.domain.FileStreamSignature in project hedera-mirror-node by hashgraph.

the class NodeSignatureVerifierTest method testNoConsensusRequiredWithVerifiedSignatureFiles.

@Test
void testNoConsensusRequiredWithVerifiedSignatureFiles() throws GeneralSecurityException {
    Map<String, PublicKey> nodeAccountIDPubKeyMap = new HashMap();
    nodeAccountIDPubKeyMap.put("0.0.3", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.4", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.5", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.6", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.7", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.8", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.9", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.10", publicKey);
    when(currentAddressBook.getNodeAccountIDPubKeyMap()).thenReturn(nodeAccountIDPubKeyMap);
    when(commonDownloaderProperties.getConsensusRatio()).thenReturn(0f);
    byte[] fileHash = TestUtils.generateRandomByteArray(48);
    byte[] fileHashSignature = signHash(fileHash);
    FileStreamSignature fileStreamSignatureNode = buildFileStreamSignature(fileHash, fileHashSignature, null, null);
    // only 1 node node necessary
    nodeSignatureVerifier.verify(List.of(fileStreamSignatureNode));
}
Also used : HashMap(java.util.HashMap) PublicKey(java.security.PublicKey) FileStreamSignature(com.hedera.mirror.importer.domain.FileStreamSignature) Test(org.junit.jupiter.api.Test)

Example 18 with FileStreamSignature

use of com.hedera.mirror.importer.domain.FileStreamSignature in project hedera-mirror-node by hashgraph.

the class NodeSignatureVerifierTest method testVerifiedWithOneThirdConsensusWithMissingSignatures.

@Test
void testVerifiedWithOneThirdConsensusWithMissingSignatures() throws GeneralSecurityException {
    Map<String, PublicKey> nodeAccountIDPubKeyMap = new HashMap();
    nodeAccountIDPubKeyMap.put("0.0.3", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.4", publicKey);
    nodeAccountIDPubKeyMap.put("0.0.5", publicKey);
    when(currentAddressBook.getNodeAccountIDPubKeyMap()).thenReturn(nodeAccountIDPubKeyMap);
    byte[] fileHash = TestUtils.generateRandomByteArray(48);
    byte[] fileHashSignature = signHash(fileHash);
    FileStreamSignature fileStreamSignatureNode3 = buildFileStreamSignature(fileHash, fileHashSignature, null, null);
    // Node 4 and 5 will not verify due to missing signature, but 1/3 verified will confirm consensus reached
    FileStreamSignature fileStreamSignatureNode4 = buildFileStreamSignature(fileHash, null, null, null);
    fileStreamSignatureNode4.setNodeAccountId(new EntityId(0L, 0L, 4L, EntityType.ACCOUNT));
    FileStreamSignature fileStreamSignatureNode5 = buildFileStreamSignature(fileHash, null, null, null);
    fileStreamSignatureNode5.setNodeAccountId(new EntityId(0L, 0L, 5L, EntityType.ACCOUNT));
    nodeSignatureVerifier.verify(Arrays.asList(fileStreamSignatureNode3, fileStreamSignatureNode4, fileStreamSignatureNode5));
}
Also used : EntityId(com.hedera.mirror.common.domain.entity.EntityId) HashMap(java.util.HashMap) PublicKey(java.security.PublicKey) FileStreamSignature(com.hedera.mirror.importer.domain.FileStreamSignature) Test(org.junit.jupiter.api.Test)

Example 19 with FileStreamSignature

use of com.hedera.mirror.importer.domain.FileStreamSignature 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 20 with FileStreamSignature

use of com.hedera.mirror.importer.domain.FileStreamSignature 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)

Aggregations

FileStreamSignature (com.hedera.mirror.importer.domain.FileStreamSignature)24 Test (org.junit.jupiter.api.Test)14 SignatureVerificationException (com.hedera.mirror.importer.exception.SignatureVerificationException)12 GeneralSecurityException (java.security.GeneralSecurityException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 PublicKey (java.security.PublicKey)7 StreamFileData (com.hedera.mirror.importer.domain.StreamFileData)5 EntityId (com.hedera.mirror.common.domain.entity.EntityId)4 InvalidStreamFileException (com.hedera.mirror.importer.exception.InvalidStreamFileException)4 HashMap (java.util.HashMap)4 AddressBook (com.hedera.mirror.common.domain.addressbook.AddressBook)3 StreamFilename (com.hedera.mirror.importer.domain.StreamFilename)3 Stopwatch (com.google.common.base.Stopwatch)2 Multimap (com.google.common.collect.Multimap)2 StreamType (com.hedera.mirror.common.domain.StreamType)2 AddressBookService (com.hedera.mirror.importer.addressbook.AddressBookService)2 HashMismatchException (com.hedera.mirror.importer.exception.HashMismatchException)2 SignatureFileParsingException (com.hedera.mirror.importer.exception.SignatureFileParsingException)2 ValidatedDataInputStream (com.hedera.mirror.importer.reader.ValidatedDataInputStream)2 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)2