use of me.retrodaredevil.solarthing.packets.security.crypto.DecryptException 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;
}
Aggregations