Search in sources :

Example 1 with InvalidKeyException

use of me.retrodaredevil.solarthing.packets.security.crypto.InvalidKeyException in project solarthing by wildmountainfarms.

the class AlterManagerAction method isDocumentMadeByUs.

private boolean isDocumentMadeByUs(Instant now, ScheduledCommandData scheduledCommandData, StoredPacketGroup existingDocument) {
    LargeIntegrityPacket largeIntegrityPacket = (LargeIntegrityPacket) existingDocument.getPackets().stream().filter(p -> p instanceof LargeIntegrityPacket).findAny().orElse(null);
    if (largeIntegrityPacket == null) {
        LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "The stored document did not have a LargeIntegrity packet. Someone must be trying to stop a scheduled command!");
        return false;
    }
    String sender = largeIntegrityPacket.getSender();
    if (!commandManager.getSender().equals(sender)) {
        LOGGER.info(SolarThingConstants.SUMMARY_MARKER, "The sender of the large integrity packet we are inspecting is not us (" + commandManager.getSender() + "). It is " + sender + ". Might be a malicious actor, might be a bad setup.");
        return false;
    }
    String encryptedHash = largeIntegrityPacket.getEncryptedHash();
    String data;
    try {
        synchronized (CIPHER) {
            data = Decrypt.decrypt(CIPHER, commandManager.getKeyPair().getPublic(), encryptedHash);
        }
    } catch (InvalidKeyException e) {
        throw new RuntimeException("Should be a valid key!", e);
    } catch (DecryptException e) {
        LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "The document we are inspecting had a large integrity packet with the sender: " + sender + ", but that's us and we couldn't decrypt their payload. Likely a malicious actor", e);
        return false;
    }
    final String[] split = data.split(",", 2);
    LOGGER.debug("decrypted data: " + data);
    if (split.length != 2) {
        LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "split.length: " + split.length + " split: " + Arrays.asList(split));
        return false;
    }
    String hexMillis = split[0];
    // String message = split[1]; We don't care what the message is. We don't even care enough to check if it matches the payload's hash
    long dateMillis;
    try {
        dateMillis = Long.parseLong(hexMillis, 16);
    } catch (NumberFormatException e) {
        LOGGER.error(SolarThingConstants.SUMMARY_MARKER, "Error parsing hex date millis", e);
        return false;
    }
    if (dateMillis < scheduledCommandData.getScheduledTimeMillis()) {
        LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "The dateMillis for this is less than the command's scheduled execution time! This must be a malicious actor!");
        return false;
    }
    if (dateMillis > now.toEpochMilli()) {
        LOGGER.warn(SolarThingConstants.SUMMARY_MARKER, "The dateMillis for this is greater than now! This should never ever happen.");
        return false;
    }
    return true;
}
Also used : CommandManager(me.retrodaredevil.solarthing.commands.util.CommandManager) Packet(me.retrodaredevil.solarthing.packets.Packet) Arrays(java.util.Arrays) AlterPacketsProvider(me.retrodaredevil.solarthing.AlterPacketsProvider) LoggerFactory(org.slf4j.LoggerFactory) InstanceTargetPackets(me.retrodaredevil.solarthing.packets.instance.InstanceTargetPackets) VersionedPacket(me.retrodaredevil.solarthing.database.VersionedPacket) LargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.LargeIntegrityPacket) ActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.ActivePeriod) ImmutableStoredAlterPacket(me.retrodaredevil.solarthing.type.alter.ImmutableStoredAlterPacket) DeleteAlterPacket(me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket) InvalidKeyException(me.retrodaredevil.solarthing.packets.security.crypto.InvalidKeyException) Duration(java.time.Duration) FlagPacket(me.retrodaredevil.solarthing.type.alter.packets.FlagPacket) RequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket) DecryptException(me.retrodaredevil.solarthing.packets.security.crypto.DecryptException) Instant(java.time.Instant) IncompatibleUpdateTokenException(me.retrodaredevil.solarthing.database.exception.IncompatibleUpdateTokenException) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) ExecutionReason(me.retrodaredevil.solarthing.reason.ExecutionReason) List(java.util.List) SolarThingConstants(me.retrodaredevil.solarthing.SolarThingConstants) TimeRange(me.retrodaredevil.solarthing.util.TimeRange) TimeRangeActivePeriod(me.retrodaredevil.solarthing.type.alter.flag.TimeRangeActivePeriod) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ScheduledCommandPacket(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandPacket) OpenSource(me.retrodaredevil.solarthing.type.open.OpenSource) TargetPacketGroup(me.retrodaredevil.solarthing.packets.collection.TargetPacketGroup) Cipher(javax.crypto.Cipher) ArrayList(java.util.ArrayList) RequestFlagPacket(me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket) FlagData(me.retrodaredevil.solarthing.type.alter.flag.FlagData) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OpenSourceExecutionReason(me.retrodaredevil.solarthing.reason.OpenSourceExecutionReason) ScheduleCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ScheduleCommandPacket) ExecutorService(java.util.concurrent.ExecutorService) SimpleAction(me.retrodaredevil.action.SimpleAction) ScheduledCommandData(me.retrodaredevil.solarthing.type.alter.packets.ScheduledCommandData) Logger(org.slf4j.Logger) PacketCollectionCreator(me.retrodaredevil.solarthing.packets.collection.PacketCollectionCreator) KeyUtil(me.retrodaredevil.solarthing.packets.security.crypto.KeyUtil) SolarThingDatabase(me.retrodaredevil.solarthing.database.SolarThingDatabase) Decrypt(me.retrodaredevil.solarthing.packets.security.crypto.Decrypt) DatabaseCache(me.retrodaredevil.solarthing.database.cache.DatabaseCache) StoredAlterPacket(me.retrodaredevil.solarthing.type.alter.StoredAlterPacket) ImmutableRequestCommandPacket(me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket) StoredPacketGroup(me.retrodaredevil.solarthing.packets.collection.StoredPacketGroup) SecurityPacketReceiver(me.retrodaredevil.solarthing.program.SecurityPacketReceiver) AlterPacket(me.retrodaredevil.solarthing.type.alter.AlterPacket) UpdateConflictSolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.UpdateConflictSolarThingDatabaseException) PublicKeyLookUp(me.retrodaredevil.solarthing.packets.security.crypto.PublicKeyLookUp) Collections(java.util.Collections) PacketCollection(me.retrodaredevil.solarthing.packets.collection.PacketCollection) CommandOpenPacket(me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket) SolarThingDatabaseException(me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException) LargeIntegrityPacket(me.retrodaredevil.solarthing.packets.security.LargeIntegrityPacket) InvalidKeyException(me.retrodaredevil.solarthing.packets.security.crypto.InvalidKeyException) DecryptException(me.retrodaredevil.solarthing.packets.security.crypto.DecryptException)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 Cipher (javax.crypto.Cipher)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1 SimpleAction (me.retrodaredevil.action.SimpleAction)1 AlterPacketsProvider (me.retrodaredevil.solarthing.AlterPacketsProvider)1 SolarThingConstants (me.retrodaredevil.solarthing.SolarThingConstants)1 CommandOpenPacket (me.retrodaredevil.solarthing.commands.packets.open.CommandOpenPacket)1 DeleteAlterPacket (me.retrodaredevil.solarthing.commands.packets.open.DeleteAlterPacket)1 ImmutableRequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.ImmutableRequestCommandPacket)1 RequestCommandPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestCommandPacket)1 RequestFlagPacket (me.retrodaredevil.solarthing.commands.packets.open.RequestFlagPacket)1