org.whispersystems.libsignal.NoSessionException Java Examples
The following examples show how to use
org.whispersystems.libsignal.NoSessionException.
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: SmsCipher.java From Silence with GNU General Public License v3.0 | 6 votes |
public OutgoingTextMessage encrypt(OutgoingTextMessage message) throws NoSessionException, UntrustedIdentityException { byte[] paddedBody = transportDetails.getPaddedMessageBody(message.getMessageBody().getBytes()); String recipientNumber = message.getRecipients().getPrimaryRecipient().getNumber(); if (!signalProtocolStore.containsSession(new SignalProtocolAddress(recipientNumber, 1))) { throw new NoSessionException("No session for: " + recipientNumber); } SessionCipher cipher = new SessionCipher(signalProtocolStore, new SignalProtocolAddress(recipientNumber, 1)); CiphertextMessage ciphertextMessage = cipher.encrypt(paddedBody); String encodedCiphertext = new String(transportDetails.getEncodedMessage(ciphertextMessage.serialize())); if (ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE) { return new OutgoingPrekeyBundleMessage(message, encodedCiphertext); } else { return message.withBody(encodedCiphertext); } }
Example #3
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 #4
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 #5
Source File: GroupCipher.java From libsignal-protocol-java with GNU General Public License v3.0 | 6 votes |
/** * Encrypt a message. * * @param paddedPlaintext The plaintext message bytes, optionally padded. * @return Ciphertext. * @throws NoSessionException */ public byte[] encrypt(byte[] paddedPlaintext) throws NoSessionException { synchronized (LOCK) { try { SenderKeyRecord record = senderKeyStore.loadSenderKey(senderKeyId); SenderKeyState senderKeyState = record.getSenderKeyState(); SenderMessageKey senderKey = senderKeyState.getSenderChainKey().getSenderMessageKey(); byte[] ciphertext = getCipherText(senderKey.getIv(), senderKey.getCipherKey(), paddedPlaintext); SenderKeyMessage senderKeyMessage = new SenderKeyMessage(senderKeyState.getKeyId(), senderKey.getIteration(), ciphertext, senderKeyState.getSigningKeyPrivate()); senderKeyState.setSenderChainKey(senderKeyState.getSenderChainKey().getNext()); senderKeyStore.storeSenderKey(senderKeyId, record); return senderKeyMessage.serialize(); } catch (InvalidKeyIdException e) { throw new NoSessionException(e); } } }
Example #6
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 #7
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 #8
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 #9
Source File: GroupCipherTest.java From libsignal-protocol-java with GNU General Public License v3.0 | 5 votes |
public void testEncryptNoSession() { InMemorySenderKeyStore aliceStore = new InMemorySenderKeyStore(); GroupCipher aliceGroupCipher = new GroupCipher(aliceStore, new SenderKeyName("coolio groupio", new SignalProtocolAddress("+10002223333", 1))); try { aliceGroupCipher.encrypt("up the punks".getBytes()); throw new AssertionError("Should have failed!"); } catch (NoSessionException nse) { // good } }
Example #10
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 #11
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 #12
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 #13
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 #14
Source File: SmsSendJob.java From Silence with GNU General Public License v3.0 | 5 votes |
private OutgoingTextMessage getAsymmetricEncrypt(MasterSecret masterSecret, OutgoingTextMessage message) throws UndeliverableMessageException, UntrustedIdentityException { try { return new SmsCipher(new SilenceSignalProtocolStore(context, masterSecret, message.getSubscriptionId())).encrypt(message); } catch (NoSessionException e) { throw new UndeliverableMessageException(e); } }
Example #15
Source File: MmsCipher.java From Silence with GNU General Public License v3.0 | 5 votes |
public SendReq encrypt(Context context, SendReq message) throws NoSessionException, RecipientFormattingException, UndeliverableMessageException, UntrustedIdentityException { EncodedStringValue[] encodedRecipient = message.getTo(); String recipientString = encodedRecipient[0].getString(); byte[] pduBytes = new PduComposer(context, message).make(); if (pduBytes == null) { throw new UndeliverableMessageException("PDU composition failed, null payload"); } if (!axolotlStore.containsSession(new SignalProtocolAddress(recipientString, 1))) { throw new NoSessionException("No session for: " + recipientString); } SessionCipher cipher = new SessionCipher(axolotlStore, new SignalProtocolAddress(recipientString, 1)); CiphertextMessage ciphertextMessage = cipher.encrypt(pduBytes); byte[] encryptedPduBytes = textTransport.getEncodedMessage(ciphertextMessage.serialize()); PduBody body = new PduBody(); PduPart part = new PduPart(); part.setContentId((System.currentTimeMillis()+"").getBytes()); part.setContentType(ContentType.TEXT_PLAIN.getBytes()); part.setName((System.currentTimeMillis()+"").getBytes()); part.setData(encryptedPduBytes); body.addPart(part); message.setSubject(new EncodedStringValue(WirePrefix.calculateEncryptedMmsSubject())); message.setBody(body); return message; }
Example #16
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 #17
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 #18
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 #19
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()); }