public byte[] ProcessSMPMessage(byte[] smp_byte_array, OTR_TLV_TYPE tlv_type, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_1, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_2, ref string message) { try { if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_1) { return(ProcessSMPMessage1(smp_byte_array, ref smp_event_type_1, ref message)); } else if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_2) { return(ProcessSMPMessage2(smp_byte_array, ref smp_event_type_1, ref message)); } else if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_3) { return(ProcessSMPMessage3(smp_byte_array, ref smp_event_type_1, ref smp_event_type_2, ref message)); } else if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_4) { return(ProcessSMPMessage4(smp_byte_array, ref smp_event_type_1, ref message)); } } catch (Exception ex) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage:" + ex.ToString(); ResetData(); //TODO(mo) //Console.WriteLine("Exception {0} \n", ex.ToString()); } return(null); }
public static void EncodeTLVSMPMessage(OTR_TLV_TYPE tlv_type,UInt32 mpi_count, byte[] mpi_byte_arrays,ref byte[] out_byte_array) { if (mpi_byte_arrays == null || mpi_byte_arrays.Length < 1) throw new ArgumentException("EncodeTLVSMPMesage: The in byte array cannot be null/empty"); if (mpi_count < 1) throw new ArgumentException("EncodeTLVSMPMessage: The TLV type value must be greater than 1"); UInt16 _tlv_type = GetTLVType(tlv_type); if (_tlv_type < 2 || _tlv_type > 5) throw new ArgumentException("EncodeTLVSMPMessage: The TLV type value must be greater than 2 and less than 6"); byte[] _encoded_type = null; byte[] _encoded_length = null; byte[] _encoded_mpi_count = null; byte[] _temp_buffer = null; try { _temp_buffer = BitConverter.GetBytes(_tlv_type); EncodeOTRShort(_temp_buffer, ref _encoded_type); _temp_buffer = BitConverter.GetBytes(mpi_count); EncodeOTRInt(_temp_buffer, ref _encoded_mpi_count); _temp_buffer = BitConverter.GetBytes((UInt16)(mpi_byte_arrays.Length + _encoded_mpi_count.Length)); EncodeOTRShort(_temp_buffer, ref _encoded_length); out_byte_array = new byte[_encoded_type.Length + _encoded_length.Length + _encoded_mpi_count.Length + mpi_byte_arrays.Length]; Buffer.BlockCopy(_encoded_type, 0, out_byte_array, 0, _encoded_type.Length); Buffer.BlockCopy(_encoded_length, 0, out_byte_array, _encoded_type.Length, _encoded_length.Length); Buffer.BlockCopy(_encoded_mpi_count, 0, out_byte_array, _encoded_type.Length + _encoded_length.Length, _encoded_mpi_count.Length); Buffer.BlockCopy(mpi_byte_arrays, 0, out_byte_array, _encoded_type.Length + _encoded_length.Length + _encoded_mpi_count.Length, mpi_byte_arrays.Length); } catch (Exception ex) { throw new InvalidDataException("EncodeTLVSMPMessage:" + ex.ToString()); } }
public static void EncodeTLV(byte[] in_byte_array, OTR_TLV_TYPE tlv_type, ref byte[] out_byte_array) { if (tlv_type == OTR_TLV_TYPE.INVALID) throw new ArgumentException("EncodeTLV: The TLV type is invalid"); UInt16 _tlv_type = GetTLVType(tlv_type); if (_tlv_type < 0 || _tlv_type > 8) throw new ArgumentException("EncodeTLV: The TLV type value must be greater than 0 and less than 9"); byte[] _encoded_type = null; byte[] _encoded_length = null; byte[] _temp_buffer = null; UInt16 _tlv_value_length = 0; _temp_buffer = BitConverter.GetBytes(_tlv_type); EncodeOTRShort(_temp_buffer, ref _encoded_type); if (in_byte_array != null && in_byte_array.Length > 1) _tlv_value_length = (UInt16)in_byte_array.Length; _temp_buffer = BitConverter.GetBytes(_tlv_value_length); EncodeOTRShort(_temp_buffer, ref _encoded_length); out_byte_array = new byte[_encoded_type.Length + _encoded_length.Length + _tlv_value_length]; Buffer.BlockCopy(_encoded_type, 0, out_byte_array, 0, _encoded_type.Length); Buffer.BlockCopy(_encoded_length, 0, out_byte_array, _encoded_type.Length, _encoded_length.Length); if (_tlv_value_length > 0) { if (BitConverter.IsLittleEndian == true) Buffer.BlockCopy(in_byte_array.Reverse().ToArray(), 0, out_byte_array, _encoded_type.Length + _encoded_length.Length, in_byte_array.Length); else Buffer.BlockCopy(in_byte_array, 0, out_byte_array, _encoded_type.Length + _encoded_length.Length, in_byte_array.Length); } }
private static UInt16 GetTLVType(OTR_TLV_TYPE tlv_type) { if (tlv_type == OTR_TLV_TYPE.PADDING) return OTRConstants.TLV_TYPE_PADDING; if (tlv_type == OTR_TLV_TYPE.DISCONNECTED) return OTRConstants.TLV_TYPE_DISCONNECTED; if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_1) return OTRConstants.TLV_TYPE_SMP_MSG_1; if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_2) return OTRConstants.TLV_TYPE_SMP_MSG_2; if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_3) return OTRConstants.TLV_TYPE_SMP_MSG_3; if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_4) return OTRConstants.TLV_TYPE_SMP_MSG_4; if (tlv_type == OTR_TLV_TYPE.SMP_ABORT) return OTRConstants.TLV_TYPE_SMP_ABORT; if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_1Q) return OTRConstants.TLV_TYPE_SMP_1Q; if (tlv_type == OTR_TLV_TYPE.EXTRA_SYM_KEY) return OTRConstants.TLV_TYPE_EXTRA_SYM_KEY; return 100; }
private void ProcessTLVSMPMessage(byte[] smp_byte_data, OTR_TLV_TYPE tlv_type) { DebugPrint("Received SMP message with TLV of type " + tlv_type.ToString()); _smp_event_type_1 = OTR_SMP_EVENT.INVALID; _smp_event_type_2 = OTR_SMP_EVENT.INVALID; _smp_message = string.Empty; StartSMPSession(false); byte[] _message_byte_array = null; byte[] _encoded_smp_bytes = _smp_manager.ProcessSMPMessage(smp_byte_data, tlv_type, ref _smp_event_type_1, ref _smp_event_type_2, ref _smp_message); if (_smp_event_type_2 == OTR_SMP_EVENT.SUCCEEDED) { _otr_event_args = new OTREventArgs(); _otr_event_args.SetMessage("SMP completed succesfully"); _otr_event_args.SetOTREvent(OTR_EVENT.SMP_MESSAGE); _otr_event_args.SetSMPEvent(OTR_SMP_EVENT.SUCCEEDED); DoOTREvent(_otr_event_args); EndSMPSession(); } if ((_smp_event_type_1 == OTR_SMP_EVENT.SEND) && (_encoded_smp_bytes != null && _encoded_smp_bytes.Length > 0)) { _message_byte_array = FormatMessageWtTLV(null, _encoded_smp_bytes, null); if (_smp_max_fragement_length > 0) EncryptFragments(_message_byte_array, _smp_max_fragement_length); else EncryptMessage(_message_byte_array, false); } else if (_smp_event_type_1 == OTR_SMP_EVENT.ABORT) { _otr_event_args = new OTREventArgs(); _otr_event_args.SetMessage(_smp_message); _otr_event_args.SetOTREvent(OTR_EVENT.SMP_MESSAGE); _otr_event_args.SetSMPEvent(OTR_SMP_EVENT.ABORT); DoOTREvent(_otr_event_args); EndSMPSession(); } else if (_smp_event_type_1 == OTR_SMP_EVENT.FAILED) { _otr_event_args = new OTREventArgs(); _otr_event_args.SetMessage("Man in the middle attack suspected"); _otr_event_args.SetOTREvent(OTR_EVENT.SMP_MESSAGE); _otr_event_args.SetSMPEvent(OTR_SMP_EVENT.FAILED); DoOTREvent(_otr_event_args); EndSMPSession(); } else if (_smp_event_type_1 == OTR_SMP_EVENT.SUCCEEDED) { _otr_event_args = new OTREventArgs(); _otr_event_args.SetMessage("SMP completed succesfully"); _otr_event_args.SetOTREvent(OTR_EVENT.SMP_MESSAGE); _otr_event_args.SetSMPEvent(OTR_SMP_EVENT.SUCCEEDED); DoOTREvent(_otr_event_args); EndSMPSession(); } else { DebugPrint("ProcessTLVSMPMessage:Invalid SMP event"); EndSMPSession(); } }
public byte[] ProcessSMPMessage(byte[] smp_byte_array, OTR_TLV_TYPE tlv_type, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_1, ref OTR.Interface.OTR_SMP_EVENT smp_event_type_2, ref string message) { try { if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_1) return ProcessSMPMessage1(smp_byte_array, ref smp_event_type_1, ref message); else if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_2) return ProcessSMPMessage2(smp_byte_array, ref smp_event_type_1, ref message); else if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_3) return ProcessSMPMessage3(smp_byte_array, ref smp_event_type_1, ref smp_event_type_2, ref message); else if (tlv_type == OTR_TLV_TYPE.SMP_MESSAGE_4) return ProcessSMPMessage4(smp_byte_array, ref smp_event_type_1, ref message); } catch (Exception ex) { smp_event_type_1 = OTR.Interface.OTR_SMP_EVENT.ABORT; message = "ProcessSMPMessage:" + ex.ToString(); ResetData(); //TODO(mo) //Console.WriteLine("Exception {0} \n", ex.ToString()); } return null; }