org.whispersystems.libsignal.DuplicateMessageException Java Examples
The following examples show how to use
org.whispersystems.libsignal.DuplicateMessageException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmsCipher.java From Silence with GNU General Public License v3.0 | 6 votes |
public IncomingTextMessage decrypt(Context context, IncomingTextMessage message) throws LegacyMessageException, InvalidMessageException, DuplicateMessageException, NoSessionException, UntrustedIdentityException { try { byte[] decoded = transportDetails.getDecodedMessage(message.getMessageBody().getBytes()); SignalMessage signalMessage = new SignalMessage(decoded); SessionCipher sessionCipher = new SessionCipher(signalProtocolStore, new SignalProtocolAddress(message.getSender(), 1)); byte[] padded = sessionCipher.decrypt(signalMessage); byte[] plaintext = transportDetails.getStrippedPaddingMessageBody(padded); if (message.isEndSession() && "TERMINATE".equals(new String(plaintext))) { signalProtocolStore.deleteSession(new SignalProtocolAddress(message.getSender(), 1)); } return message.withMessageBody(new String(plaintext)); } catch (IOException | IllegalArgumentException | NullPointerException e) { throw new InvalidMessageException(e); } }
Example #2
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 6 votes |
public void testLargeMessages() throws InvalidMessageException, LegacyMessageException, NoSessionException, DuplicateMessageException { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, GROUP_SENDER); GroupCipher bobGroupCipher = new GroupCipher(bobStore, GROUP_SENDER); SenderKeyDistributionMessage sentAliceDistributionMessage = aliceSessionBuilder.create(GROUP_SENDER); SenderKeyDistributionMessage receivedAliceDistributionMessage = new SenderKeyDistributionMessage(sentAliceDistributionMessage.serialize()); bobSessionBuilder.process(GROUP_SENDER, receivedAliceDistributionMessage); byte[] plaintext = new byte[1024 * 1024]; new Random().nextBytes(plaintext); byte[] ciphertextFromAlice = aliceGroupCipher.encrypt(plaintext); byte[] plaintextFromAlice = bobGroupCipher.decrypt(ciphertextFromAlice); assertTrue(Arrays.equals(plaintext, plaintextFromAlice)); }
Example #3
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 6 votes |
public void testBasicEncryptDecrypt() throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, GROUP_SENDER); GroupCipher bobGroupCipher = new GroupCipher(bobStore, GROUP_SENDER); SenderKeyDistributionMessage sentAliceDistributionMessage = aliceSessionBuilder.create(GROUP_SENDER); SenderKeyDistributionMessage receivedAliceDistributionMessage = new SenderKeyDistributionMessage(sentAliceDistributionMessage.serialize()); bobSessionBuilder.process(GROUP_SENDER, receivedAliceDistributionMessage); byte[] ciphertextFromAlice = aliceGroupCipher.encrypt("smert ze smert".getBytes()); byte[] plaintextFromAlice = bobGroupCipher.decrypt(ciphertextFromAlice); assertTrue(new String(plaintextFromAlice).equals("smert ze smert")); }
Example #4
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 6 votes |
public void testNoSession() throws InvalidMessageException, LegacyMessageException, NoSessionException, DuplicateMessageException { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, GROUP_SENDER); GroupCipher bobGroupCipher = new GroupCipher(bobStore, GROUP_SENDER); SenderKeyDistributionMessage sentAliceDistributionMessage = aliceSessionBuilder.create(GROUP_SENDER); SenderKeyDistributionMessage receivedAliceDistributionMessage = new SenderKeyDistributionMessage(sentAliceDistributionMessage.serialize()); // bobSessionBuilder.process(GROUP_SENDER, receivedAliceDistributionMessage); byte[] ciphertextFromAlice = aliceGroupCipher.encrypt("smert ze smert".getBytes()); try { byte[] plaintextFromAlice = bobGroupCipher.decrypt(ciphertextFromAlice); throw new AssertionError("Should be no session!"); } catch (NoSessionException e) { // good } }
Example #5
Source File: GroupCipher.java From libsignal-protocol-java with GNU General Public License v3.0 | 6 votes |
private SenderMessageKey getSenderKey(SenderKeyState senderKeyState, int iteration) throws DuplicateMessageException, InvalidMessageException { SenderChainKey senderChainKey = senderKeyState.getSenderChainKey(); if (senderChainKey.getIteration() > iteration) { if (senderKeyState.hasSenderMessageKey(iteration)) { return senderKeyState.removeSenderMessageKey(iteration); } else { throw new DuplicateMessageException("Received message with old counter: " + senderChainKey.getIteration() + " , " + iteration); } } if (iteration - senderChainKey.getIteration() > 2000) { throw new InvalidMessageException("Over 2000 messages into the future!"); } while (senderChainKey.getIteration() < iteration) { senderKeyState.addSenderMessageKey(senderChainKey.getSenderMessageKey()); senderChainKey = senderChainKey.getNext(); } senderKeyState.setSenderChainKey(senderChainKey.getNext()); return senderChainKey.getSenderMessageKey(); }
Example #6
Source File: SmsCipher.java From Silence with GNU General Public License v3.0 | 6 votes |
public IncomingEncryptedMessage decrypt(Context context, IncomingPreKeyBundleMessage message) throws InvalidVersionException, InvalidMessageException, DuplicateMessageException, UntrustedIdentityException, LegacyMessageException { try { byte[] decoded = transportDetails.getDecodedMessage(message.getMessageBody().getBytes()); PreKeySignalMessage preKeyMessage = new PreKeySignalMessage(decoded); SessionCipher sessionCipher = new SessionCipher(signalProtocolStore, new SignalProtocolAddress(message.getSender(), 1)); byte[] padded = sessionCipher.decrypt(preKeyMessage); byte[] plaintext = transportDetails.getStrippedPaddingMessageBody(padded); return new IncomingEncryptedMessage(message, new String(plaintext)); } catch (IOException | InvalidKeyException | InvalidKeyIdException e) { throw new InvalidMessageException(e); } }
Example #7
Source File: SmsDecryptJob.java From Silence with GNU General Public License v3.0 | 5 votes |
private void handleXmppExchangeMessage(MasterSecret masterSecret, long messageId, long threadId, IncomingXmppExchangeMessage message) throws NoSessionException, DuplicateMessageException, InvalidMessageException, LegacyMessageException { EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context); database.markAsXmppExchange(messageId); }
Example #8
Source File: GroupCipher.java From libsignal-protocol-java with GNU General Public License v3.0 | 5 votes |
/** * Decrypt a SenderKey group message. * * @param senderKeyMessageBytes The received ciphertext. * @param callback A callback that is triggered after decryption is complete, * but before the updated session state has been committed to the session * DB. This allows some implementations to store the committed plaintext * to a DB first, in case they are concerned with a crash happening between * the time the session state is updated but before they're able to store * the plaintext to disk. * @return Plaintext * @throws LegacyMessageException * @throws InvalidMessageException * @throws DuplicateMessageException */ public byte[] decrypt(byte[] senderKeyMessageBytes, DecryptionCallback callback) throws LegacyMessageException, InvalidMessageException, DuplicateMessageException, NoSessionException { synchronized (LOCK) { try { SenderKeyRecord record = senderKeyStore.loadSenderKey(senderKeyId); if (record.isEmpty()) { throw new NoSessionException("No sender key for: " + senderKeyId); } SenderKeyMessage senderKeyMessage = new SenderKeyMessage(senderKeyMessageBytes); SenderKeyState senderKeyState = record.getSenderKeyState(senderKeyMessage.getKeyId()); senderKeyMessage.verifySignature(senderKeyState.getSigningKeyPublic()); SenderMessageKey senderKey = getSenderKey(senderKeyState, senderKeyMessage.getIteration()); byte[] plaintext = getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText()); callback.handlePlaintext(plaintext); senderKeyStore.storeSenderKey(senderKeyId, record); return plaintext; } catch (org.whispersystems.libsignal.InvalidKeyException | InvalidKeyIdException e) { throw new InvalidMessageException(e); } } }
Example #9
Source File: SmsDecryptJob.java From Silence with GNU General Public License v3.0 | 5 votes |
private void handleSecureMessage(MasterSecret masterSecret, long messageId, long threadId, IncomingTextMessage message) throws NoSessionException, DuplicateMessageException, InvalidMessageException, LegacyMessageException, UntrustedIdentityException { EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context); SmsCipher cipher = new SmsCipher(new SilenceSignalProtocolStore(context, masterSecret, message.getSubscriptionId())); IncomingTextMessage plaintext = cipher.decrypt(context, message); database.updateMessageBody(masterSecret, messageId, plaintext.getMessageBody()); if (message.isEndSession()) SecurityEvent.broadcastSecurityUpdateEvent(context, threadId); }
Example #10
Source File: SignalServiceCipher.java From bcm-android with GNU General Public License v3.0 | 5 votes |
private byte[] decrypt(SignalServiceProtos.Envelope envelope, byte[] ciphertext) throws InvalidVersionException, InvalidMessageException, InvalidKeyException, DuplicateMessageException, InvalidKeyIdException, UntrustedIdentityException, LegacyMessageException, NoSessionException { SignalProtocolAddress sourceAddress = new SignalProtocolAddress(envelope.getSource(), envelope.getSourceDevice()); SessionCipher sessionCipher = new SessionCipher(signalProtocolStore, sourceAddress); byte[] paddedMessage; if (envelope.getType() == Type.PREKEY_BUNDLE) { paddedMessage = sessionCipher.decrypt(new PreKeySignalMessage(ciphertext)); //纠正remote register id SessionRecord sessionRecord = signalProtocolStore.loadSession(sourceAddress); if (sessionRecord.getSessionState().getRemoteRegistrationId() == 0) { sessionRecord.getSessionState().setRemoteRegistrationId(envelope.getSourceRegistration()); signalProtocolStore.storeSession(sourceAddress, sessionRecord); } } else if (envelope.getType() == Type.CIPHERTEXT) { paddedMessage = sessionCipher.decrypt(new SignalMessage(ciphertext)); } else { throw new InvalidMessageException("Unknown type: " + envelope.getType()); } PushTransportDetails transportDetails = new PushTransportDetails(sessionCipher.getSessionVersion()); return transportDetails.getStrippedPaddingMessageBody(paddedMessage); }
Example #11
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 5 votes |
public void testOutOfOrder() throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); SenderKeyName aliceName = GROUP_SENDER; GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName); GroupCipher bobGroupCipher = new GroupCipher(bobStore, aliceName); SenderKeyDistributionMessage aliceDistributionMessage = aliceSessionBuilder.create(aliceName); bobSessionBuilder.process(aliceName, aliceDistributionMessage); ArrayList<byte[]> ciphertexts = new ArrayList<>(100); for (int i=0;i<100;i++) { ciphertexts.add(aliceGroupCipher.encrypt("up the punks".getBytes())); } while (ciphertexts.size() > 0) { int index = randomInt() % ciphertexts.size(); byte[] ciphertext = ciphertexts.remove(index); byte[] plaintext = bobGroupCipher.decrypt(ciphertext); assertTrue(new String(plaintext).equals("up the punks")); } }
Example #12
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 5 votes |
public void testTooFarInFuture() throws DuplicateMessageException, InvalidMessageException, LegacyMessageException, NoSessionException { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); SenderKeyName aliceName = GROUP_SENDER; GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName); GroupCipher bobGroupCipher = new GroupCipher(bobStore, aliceName); SenderKeyDistributionMessage aliceDistributionMessage = aliceSessionBuilder.create(aliceName); bobSessionBuilder.process(aliceName, aliceDistributionMessage); for (int i=0;i<2001;i++) { aliceGroupCipher.encrypt("up the punks".getBytes()); } byte[] tooFarCiphertext = aliceGroupCipher.encrypt("notta gonna worka".getBytes()); try { bobGroupCipher.decrypt(tooFarCiphertext); throw new AssertionError("Should have failed!"); } catch (InvalidMessageException e) { // good } }
Example #13
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 5 votes |
public void testMessageKeyLimit() throws Exception { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); SenderKeyName aliceName = GROUP_SENDER; GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName); GroupCipher bobGroupCipher = new GroupCipher(bobStore, aliceName); SenderKeyDistributionMessage aliceDistributionMessage = aliceSessionBuilder.create(aliceName); bobSessionBuilder.process(aliceName, aliceDistributionMessage); List<byte[]> inflight = new LinkedList<>(); for (int i=0;i<2010;i++) { inflight.add(aliceGroupCipher.encrypt("up the punks".getBytes())); } bobGroupCipher.decrypt(inflight.get(1000)); bobGroupCipher.decrypt(inflight.get(inflight.size()-1)); try { bobGroupCipher.decrypt(inflight.get(0)); throw new AssertionError("Should have failed!"); } catch (DuplicateMessageException e) { // good } }
Example #14
Source File: MmsDownloadJob.java From Silence with GNU General Public License v3.0 | 4 votes |
private void storeRetrievedMms(MasterSecret masterSecret, String contentLocation, long messageId, long threadId, EncodedStringValue pduFrom, EncodedStringValue[] pduTo, EncodedStringValue[] pduCc, PduBody pduBody, long date, boolean isSecure, int subscriptionId) throws MmsException, NoSessionException, DuplicateMessageException, InvalidMessageException, LegacyMessageException { MmsDatabase database = DatabaseFactory.getMmsDatabase(context); SingleUseBlobProvider provider = SingleUseBlobProvider.getInstance(); String from = null; List<String> to = new LinkedList<>(); List<String> cc = new LinkedList<>(); String body = null; List<Attachment> attachments = new LinkedList<>(); if (pduFrom != null) { from = Util.toIsoString(pduFrom.getTextString()); } if (pduTo != null) { for (EncodedStringValue toValue : pduTo) { to.add(Util.toIsoString(toValue.getTextString())); } } if (pduCc != null) { for (EncodedStringValue ccValue : pduCc) { cc.add(Util.toIsoString(ccValue.getTextString())); } } if (pduBody != null) { body = PartParser.getMessageText(pduBody); PduBody media = PartParser.getSupportedMediaParts(pduBody); for (int i=0;i<media.getPartsNum();i++) { PduPart part = media.getPart(i); if (part.getData() != null) { byte[] decodedDigest = null; if (isSecure) { PduPart digestPart = pduBody.getPartByName(Util.toIsoString(part.getName()) + ".digest"); byte[] digestBytes = null; if (digestPart != null) { digestBytes = digestPart.getData(); } if (digestBytes != null) { decodedDigest = Base64.decode(digestBytes, Base64.NO_WRAP); } if (decodedDigest != null) { Log.w(TAG, "Available digest for part name " + Util.toIsoString(part.getName()) + " (content id " + Util.toIsoString(part.getContentId()) + "): " + Hex.toString(decodedDigest)); } else { Log.w(TAG, "No available digest for part name " + Util.toIsoString(part.getName()) + " (content id " + Util.toIsoString(part.getContentId()) + ")"); } } Uri uri = provider.createUri(part.getData()); attachments.add(new UriAttachment(uri, Util.toIsoString(part.getContentType()), AttachmentDatabase.TRANSFER_PROGRESS_DONE, part.getData().length, decodedDigest)); } } } IncomingMediaMessage message = new IncomingMediaMessage(from, to, cc, body, date * 1000L, attachments, subscriptionId); Pair<Long, Long> messageAndThreadId; if (isSecure) { messageAndThreadId = database.insertSecureDecryptedMessageInbox(masterSecret, message, threadId); } else { messageAndThreadId = database.insertMessageInbox(masterSecret, message, contentLocation, threadId); } database.delete(messageId); MessageNotifier.updateNotification(context, masterSecret, message.getSubscriptionId()); }
Example #15
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 4 votes |
public void testBasicRatchet() throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); SenderKeyName aliceName = GROUP_SENDER; GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName); GroupCipher bobGroupCipher = new GroupCipher(bobStore, aliceName); SenderKeyDistributionMessage sentAliceDistributionMessage = aliceSessionBuilder.create(aliceName); SenderKeyDistributionMessage receivedAliceDistributionMessage = new SenderKeyDistributionMessage(sentAliceDistributionMessage.serialize()); bobSessionBuilder.process(aliceName, receivedAliceDistributionMessage); byte[] ciphertextFromAlice = aliceGroupCipher.encrypt("smert ze smert".getBytes()); byte[] ciphertextFromAlice2 = aliceGroupCipher.encrypt("smert ze smert2".getBytes()); byte[] ciphertextFromAlice3 = aliceGroupCipher.encrypt("smert ze smert3".getBytes()); byte[] plaintextFromAlice = bobGroupCipher.decrypt(ciphertextFromAlice); try { bobGroupCipher.decrypt(ciphertextFromAlice); throw new AssertionError("Should have ratcheted forward!"); } catch (DuplicateMessageException dme) { // good } byte[] plaintextFromAlice2 = bobGroupCipher.decrypt(ciphertextFromAlice2); byte[] plaintextFromAlice3 = bobGroupCipher.decrypt(ciphertextFromAlice3); assertTrue(new String(plaintextFromAlice).equals("smert ze smert")); assertTrue(new String(plaintextFromAlice2).equals("smert ze smert2")); assertTrue(new String(plaintextFromAlice3).equals("smert ze smert3")); }
Example #16
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 3 votes |
public void testLateJoin() throws NoSessionException, InvalidMessageException, LegacyMessageException, DuplicateMessageException { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); InMemorySenderKeyStore bobStore = new InMemorySenderKeyStore(); GroupSessionBuilder aliceSessionBuilder = new GroupSessionBuilder(aliceStore); SenderKeyName aliceName = GROUP_SENDER; GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, aliceName); SenderKeyDistributionMessage aliceDistributionMessage = aliceSessionBuilder.create(aliceName); // Send off to some people. for (int i=0;i<100;i++) { aliceGroupCipher.encrypt("up the punks up the punks up the punks".getBytes()); } // Now Bob Joins. GroupSessionBuilder bobSessionBuilder = new GroupSessionBuilder(bobStore); GroupCipher bobGroupCipher = new GroupCipher(bobStore, aliceName); SenderKeyDistributionMessage distributionMessageToBob = aliceSessionBuilder.create(aliceName); bobSessionBuilder.process(aliceName, new SenderKeyDistributionMessage(distributionMessageToBob.serialize())); byte[] ciphertext = aliceGroupCipher.encrypt("welcome to the group".getBytes()); byte[] plaintext = bobGroupCipher.decrypt(ciphertext); assertEquals(new String(plaintext), "welcome to the group"); }
Example #17
Source File: GroupCipher.java From libsignal-protocol-java with GNU General Public License v3.0 | 2 votes |
/** * Decrypt a SenderKey group message. * * @param senderKeyMessageBytes The received ciphertext. * @return Plaintext * @throws LegacyMessageException * @throws InvalidMessageException * @throws DuplicateMessageException */ public byte[] decrypt(byte[] senderKeyMessageBytes) throws LegacyMessageException, DuplicateMessageException, InvalidMessageException, NoSessionException { return decrypt(senderKeyMessageBytes, new NullDecryptionCallback()); }