Пример #1
0
        private void CloseOTRSession(string session_closed_message)
        {
            DebugPrint("Ending OTR session");

            _otr_event_args = new OTREventArgs();
            _otr_event_args.SetMessage(session_closed_message);
            _otr_event_args.SetOTREvent(OTR_EVENT.CLOSED);
            DoOTREvent(_otr_event_args);

            _message_state = OTR_MESSAGE_STATE.MSG_STATE_PLAINTEXT;
            _message_manager = null;
            _ake_keys_manager = null;
            _dsa_signer = null;
            _signature_manager = null;
            _smp_manager = null;
            _ake_keys = null;
            _my_unique_id = string.Empty;
            _my_buddy_unique_id = string.Empty;
            _otr_fragment_object = null;
            EndSMPSession();
        }
Пример #2
0
        private void ProcessDataMessage(OTRMessage otr_message)
        {
            DebugPrint("Received Data Message");

            if (_message_state != OTR_MESSAGE_STATE.MSG_STATE_ENCRYPTED)
            {

                OTRError("ProcessDataMessage: OTR Engine is not in the MSG_STATE_ENCRYPTED state",
                  "ProcessDataMessage: OTR Engine is not in the MSG_STATE_ENCRYPTED state",
                 null);

                return;

            }

            /*Compute new keys */

            DataExchangeKeys _data_exchange_keys = _otr_session_object.GetDataExchangeKeys(otr_message.GetRecipientKeyId(),
                _otr_message.GetSenderKeyId(), _otr_message.GetNextDHPublicKeyMpi(), _re_key_my_dh_keys);

            string _error_string = null;

            if (_data_exchange_keys == null)
            {
                if (otr_message.GetFlags() != OTRConstants.IGNORE_UNREADABLE)
                 _error_string = "You transmitted an unreadable encrypted message";

                OTRError("ProcessDataMessage: Data exchange keys are null",
                 "ProcessDataMessage: Data exchange keys are null",
                _error_string);

                return;
            }

            /* Compute hash with MAC receiving key */

            _temp_buffer = Utility.SHA1GetKeyedHash(_data_exchange_keys.GetMACKeyRecv(), otr_message.GetBytesToAuthenticate());

            /* Compare hashed MAC */

            if (Utility.IsArrayEqual(_temp_buffer, otr_message.GetAuthenticationMAC()) == false)
            {

                if (otr_message.GetFlags() != OTRConstants.IGNORE_UNREADABLE)
                 _error_string = "You transmitted an unreadable encrypted message";

                OTRError("Message MAC authentication failed", "Message MAC authentication failed", _error_string);

                return;

            }

            /* Decrypt message data */

            try
            {

                _temp_buffer = Utility.AESGetDecrypt(_data_exchange_keys.GetAESKeyRecv(),
                 otr_message.GetEncryptedData(), otr_message.GetCounterTopHalf());

                _extra_symmetric_key_temp = _data_exchange_keys.GetAESKeyExtra();

            }
            catch (Exception ex)
            {

                if (otr_message.GetFlags() != OTRConstants.IGNORE_UNREADABLE)
                    _error_string = "You transmitted an unreadable encrypted message";
                else
                    _error_string = string.Empty;

                OTRError("ProcessDataMessage:Unable to decrypte message",
                 "ProcessDataMessage:" + ex.ToString(),
                _error_string);
                _otr_fragment_object = null;

                return;

            }

            /* Process plain text */

            if (_otr_fragment_object != null)
            {
                _otr_fragment_object.ClearCombinedString();
                _otr_fragment_object = null;
            }

            ProcessPlaintext(_temp_buffer,_otr_message.GetOldMacKeys());
        }
Пример #3
0
        private void ProcessOTRFragment(string otr_fragment_string)
        {
            if (_otr_fragment_object == null)
                _otr_fragment_object = new OTRFragmentObject();

            string _string_1 = string.Empty;
            string _string_2 = string.Empty;
            string[] _parts = null;

            _string_1 = otr_fragment_string.Substring(4, otr_fragment_string.Length - 5);

            if (_current_otr_version != OTR_VERSION.VERSION_2)
            {

                if (_string_1.Substring(0, 1).Equals("|") == false)
                {
                    OTRError("ProcessOTRFragment:Fragment not properly formatted", null, null);
                    return;
                }

                _string_1 = _string_1.Substring(1, _string_1.Length - 1);

                _parts = _string_1.Split(new char[] { '|' }, 2);

                UInt32 _sender_instance_tag = Convert.ToUInt32(_parts[0].Trim());

                if (_sender_instance_tag != _message_manager.GetBuddyIntanceTag())
                {
                    OTRError("ProcessOTRFragment:Invalid sender instance tag", null, null);
                    return;
                }

                _parts = _parts[1].Split(new char[] { ',' }, 2);

                UInt32 _receiver_instance_tag = Convert.ToUInt32(_parts[0].Trim());

                if (_receiver_instance_tag != _message_manager.GetMyIntanceTag())
                {
                    OTRError("ProcessOTRFragment:Invalid receiver instance tag", null, null);
                    return;
                }

            }
            else
                _parts = _string_1.Split(new char[] { ',' }, 2);

            _parts = _parts[1].Split(new char[] { ',' }, 2);

            UInt16 _fragment_number = Convert.ToUInt16(_parts[0].Trim());

            _parts = _parts[1].Split(new char[] { ',' }, 2);

            UInt16 _total_number_fragments = Convert.ToUInt16(_parts[0].Trim());

            DebugPrint("Received fragment " + _fragment_number.ToString() + " of " + _total_number_fragments.ToString());

            if (_otr_fragment_object.IsSetFragment(_parts[1], _fragment_number, _total_number_fragments) == false)
            {
                OTRError("ProcessOTRFragment:Fragment number " + _fragment_number.ToString() + " of " + _total_number_fragments.ToString() + " was discarded", null, null);
                return;
            }

            if (_fragment_number == _total_number_fragments)
            ProcessOTRMessage(_otr_fragment_object.GetCombinedString());
        }