Esempio n. 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);
            }
        }
Esempio n. 2
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 (SessionLock)
            {
                SessionRecord sessionRecord   = _sessionStore.LoadSession(_remoteAddress);
                SessionState  sessionState    = sessionRecord.GetSessionState();
                ChainKey      chainKey        = sessionState.GetSenderChainKey();
                MessageKeys   messageKeys     = chainKey.GetMessageKeys();
                IEcPublicKey  senderEphemeral = sessionState.GetSenderRatchetKey();
                uint          previousCounter = sessionState.GetPreviousCounter();
                uint          sessionVersion  = sessionState.GetSessionVersion();

                byte[]            ciphertextBody    = GetCiphertext(sessionVersion, 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());
                _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);
            }
        }