Exemple #1
0
        /**
         * Encrypt a message.
         *
         * @param  paddedMessage The plaintext message bytes, optionally padded to a constant multiple.
         * @return A ciphertext message encrypted to the recipient+device tuple.
         */
        public CiphertextMessage encrypt(byte[] paddedMessage)
        {
            lock (SESSION_LOCK)
            {
                SessionRecord sessionRecord   = sessionStore.loadSession(remoteAddress);
                SessionState  sessionState    = sessionRecord.getSessionState();
                ChainKey      chainKey        = sessionState.getSenderChainKey();
                MessageKeys   messageKeys     = chainKey.getMessageKeys();
                ECPublicKey   senderEphemeral = sessionState.getSenderRatchetKey();
                uint          previousCounter = sessionState.getPreviousCounter();
                uint          sessionVersion  = sessionState.getSessionVersion();

                byte[]            ciphertextBody    = getCiphertext(sessionVersion, messageKeys, paddedMessage);
                CiphertextMessage ciphertextMessage = new WhisperMessage(sessionVersion, messageKeys.getMacKey(),
                                                                         senderEphemeral, chainKey.getIndex(),
                                                                         previousCounter, ciphertextBody,
                                                                         sessionState.getLocalIdentityKey(),
                                                                         sessionState.getRemoteIdentityKey());

                if (sessionState.hasUnacknowledgedPreKeyMessage())
                {
                    SessionState.UnacknowledgedPreKeyMessageItems items = sessionState.getUnacknowledgedPreKeyMessageItems();
                    uint localRegistrationId = sessionState.getLocalRegistrationId();

                    ciphertextMessage = new PreKeyWhisperMessage(sessionVersion, localRegistrationId, items.getPreKeyId(),
                                                                 items.getSignedPreKeyId(), items.getBaseKey(),
                                                                 sessionState.getLocalIdentityKey(),
                                                                 (WhisperMessage)ciphertextMessage);
                }

                sessionState.setSenderChainKey(chainKey.getNextChainKey());
                sessionStore.storeSession(remoteAddress, sessionRecord);
                return(ciphertextMessage);
            }
        }
        /**
         * Encrypt a message.
         *
         * @param  paddedMessage The plaintext message bytes, optionally padded to a constant multiple.
         * @return A ciphertext message encrypted to the recipient+device tuple.
         */
        public CiphertextMessage encrypt(byte[] paddedMessage)
        {
            lock (SESSION_LOCK)
            {
                SessionRecord sessionRecord   = sessionStore.LoadSession(remoteAddress);
                SessionState  sessionState    = sessionRecord.getSessionState();
                ChainKey      chainKey        = sessionState.getSenderChainKey();
                MessageKeys   messageKeys     = chainKey.getMessageKeys();
                ECPublicKey   senderEphemeral = sessionState.getSenderRatchetKey();
                uint          previousCounter = sessionState.getPreviousCounter();
                uint          sessionVersion  = sessionState.getSessionVersion();

                byte[]            ciphertextBody    = getCiphertext(messageKeys, paddedMessage);
                CiphertextMessage ciphertextMessage = new SignalMessage(sessionVersion, messageKeys.getMacKey(),
                                                                        senderEphemeral, chainKey.getIndex(),
                                                                        previousCounter, ciphertextBody,
                                                                        sessionState.getLocalIdentityKey(),
                                                                        sessionState.getRemoteIdentityKey());

                if (sessionState.hasUnacknowledgedPreKeyMessage())
                {
                    SessionState.UnacknowledgedPreKeyMessageItems items = sessionState.getUnacknowledgedPreKeyMessageItems();
                    uint localRegistrationId = sessionState.GetLocalRegistrationId();

                    ciphertextMessage = new PreKeySignalMessage(sessionVersion, localRegistrationId, items.getPreKeyId(),
                                                                items.getSignedPreKeyId(), items.getBaseKey(),
                                                                sessionState.getLocalIdentityKey(),
                                                                (SignalMessage)ciphertextMessage);
                }

                sessionState.setSenderChainKey(chainKey.getNextChainKey());

                if (!identityKeyStore.IsTrustedIdentity(remoteAddress, sessionState.getRemoteIdentityKey(), Direction.SENDING))
                {
                    throw new UntrustedIdentityException(remoteAddress.Name, sessionState.getRemoteIdentityKey());
                }

                identityKeyStore.SaveIdentity(remoteAddress, sessionState.getRemoteIdentityKey());

                sessionStore.StoreSession(remoteAddress, sessionRecord);
                return(ciphertextMessage);
            }
        }