/// <summary> /// Each StoredData element is individually signed. However, the /// signature also must be self-contained and cover the Kind-ID and /// Resource-ID even though they are not present in the StoredData /// structure. The input to the signature algorithm is: /// resource_id || kind || storage_time || StoredDataValue || /// SignerIdentity /// </summary> /// <param name="resId"></param> /// <param name="kind"></param> /// <param name="storageTime"></param> /// <param name="storedDataValue"></param> /// <param name="identity"></param> public Signature(ResourceId resId, UInt32 kind, UInt64 storageTime, StoredDataValue value, SignerIdentity signerIdentity, ReloadConfig config) { m_ReloadConfig = config; var ascii = new ASCIIEncoding(); /* Set alogorithm and identity */ algorithm = new SignatureAndHashAlgorithm(HashAlgorithm.sha256, ReloadGlobals.SignatureAlg); identity = signerIdentity; /* Get string of stored data value */ var ms = new MemoryStream(); var bw = new BinaryWriter(ms); value.Dump(bw); value.GetUsageValue.dump(bw); ms.Position = 0; var sr = new StreamReader(ms); string strValue = sr.ReadToEnd(); sr.Close(); bw.Close(); /* Concatenate signature input */ String signaturInput = String.Format("{0}{1}{2}{3}{4}", ascii.GetString(resId.Data, 0, resId.Data.Length), kind, storageTime, strValue, identity.ToString()); signatureValue = Sign(signaturInput); }
/// <summary> /// This constructor should be used whiel message deserialization. /// </summary> /// <param name="storage_time"></param> /// <param name="lifetime"></param> /// <param name="value"></param> public StoredData(UInt64 storage_time, UInt32 lifetime, StoredDataValue value) { this.storage_time = storage_time; this.lifetime = lifetime; this.value = value; this.signature = null; length = 0; }
public override RELOAD_MessageBody FromReader(ReloadMessage rm, BinaryReader reader, long reload_msg_size) { /* try to read the packet as a FetchAns packet */ try { RELOAD_MsgCode = (RELOAD_MessageCode)(UInt16)IPAddress.NetworkToHostOrder( reader.ReadInt16()); UInt32 message_len = (UInt32)(IPAddress.HostToNetworkOrder( reader.ReadInt32())); int length = 0; if (RELOAD_MsgCode == RELOAD_MessageCode.Fetch_Answer) { long posBeforeKR = reader.BaseStream.Position; UInt32 kind_responses_length = (UInt32)IPAddress.NetworkToHostOrder( (int)reader.ReadInt32()); while (StreamUtil.ReadBytes(posBeforeKR, reader) < kind_responses_length) { FetchKindResponse kind_response = new FetchKindResponse(); UInt32 kind = (UInt32)IPAddress.NetworkToHostOrder( reader.ReadInt32()); UInt64 generation = (UInt64)IPAddress.NetworkToHostOrder( reader.ReadInt64()); // length of all StoredData contained in this FetchKindResponse long posBeforeSD = reader.BaseStream.Position; UInt32 stored_data_length = (UInt32)IPAddress.NetworkToHostOrder( reader.ReadInt32()); List<StoredData> values = new List<StoredData>(); // read StoredData while (StreamUtil.ReadBytes(posBeforeSD, reader) < stored_data_length) { // reading properties of StoredData struct UInt32 sdLength = (UInt32)(IPAddress.NetworkToHostOrder( reader.ReadInt32())); UInt64 storage_time = (UInt64)(IPAddress.NetworkToHostOrder( reader.ReadInt64())); UInt32 lifetime = (UInt32)(IPAddress.NetworkToHostOrder( reader.ReadInt32())); ReloadGlobals.DataModel data_model = myManager.GetDataModelfromKindId(kind); Boolean exists; IUsage usage; StoredDataValue stored_data_value; switch (data_model) { case ReloadGlobals.DataModel.SINGLE_VALUE: throw new NotImplementedException( "There is no Usage with Single Value atm"); case ReloadGlobals.DataModel.ARRAY: UInt32 index = (UInt32)(IPAddress.NetworkToHostOrder( reader.ReadInt32())); exists = (reader.ReadByte() == 0x00 ? false : true); usage = myManager.GetUsageFromReader(rm, reader, reload_msg_size, kind); stored_data_value = new StoredDataValue(index, usage, exists); break; case ReloadGlobals.DataModel.DICTIONARY: UInt16 keyLength = (UInt16)(IPAddress.NetworkToHostOrder( reader.ReadInt16())); /*string key = Encoding.ASCII.GetString( reader.ReadBytes(keyLength), 0, keyLength); */ string key = BitConverter.ToString(reader.ReadBytes(keyLength)).Replace("-", string.Empty); //--joscha exists = (reader.ReadByte() == 0x00 ? false : true); usage = myManager.GetUsageFromReader(rm, reader, reload_msg_size, kind); stored_data_value = new StoredDataValue(key, usage, exists); break; default: throw new NotSupportedException( String.Format("The data_model {0} is not supported", data_model)); } StoredData stored_data = new StoredData(storage_time, lifetime, stored_data_value); stored_data.Signature = new Signature(myManager.m_ReloadConfig).FromReader(reader, reload_msg_size); // TODO Process signature values.Add(stored_data); } // end read StoredData kind_response.kind = kind; kind_response.generation = generation; kind_response.values = new List<StoredData>(); kind_response.values.AddRange(values); kind_responses.Add(kind_response); } // end read FetchKindResponses } reload_msg_size = reload_msg_size - (length + 1); } catch { throw new Exception(); } return this; }
/// <summary> /// This contructor should be used by the store_data originator. /// Storeage -and lifetime will be set automaticly. /// </summary> /// <param name="value">The value to be stored</param> public StoredData(StoredDataValue value) { storage_time = (UInt64)(DateTime.UtcNow - ReloadGlobals.StartOfEpoch).TotalMilliseconds; lifetime = ReloadGlobals.DISCO_REGISTRATION_LIFETIME; this.value = value; signature = null; length = 0; }
/// <summary> /// Deserializes a StoreReq message from wire. /// </summary> /// <param name="rm"></param> /// <param name="reader"></param> /// <param name="reload_msg_size"></param> /// <returns></returns> public override RELOAD_MessageBody FromReader(ReloadMessage rm, BinaryReader reader, long reload_msg_size) { UInt32 message_len = 0; /* try to read the packet as a StoreReq packet */ try { long posBeforeMsg = reader.BaseStream.Position; RELOAD_MsgCode = (RELOAD_MessageCode)(UInt16)IPAddress.NetworkToHostOrder(reader.ReadInt16()); message_len = (UInt32)(IPAddress.HostToNetworkOrder((int)reader.ReadInt32())); Byte res_id_length = reader.ReadByte(); if (res_id_length == 0) throw new System.Exception("Resource ID length == 0!"); resourceId = new ResourceId(reader.ReadBytes(res_id_length)); replica_number = reader.ReadByte(); long posBeforeRead = reader.BaseStream.Position; UInt32 kindDataLen = (UInt32)(IPAddress.NetworkToHostOrder(reader.ReadInt32())); /* StoreKindData Receive loop */ while (StreamUtil.ReadBytes(posBeforeRead, reader) < kindDataLen) { UInt32 kindId = (UInt32)(IPAddress.HostToNetworkOrder(reader.ReadInt32())); UInt64 generation = (UInt64)(IPAddress.NetworkToHostOrder(reader.ReadInt64())); var store_kind_data = new StoreKindData(kindId, generation); long posBeforeSD = reader.BaseStream.Position; UInt32 storedDataLen = (UInt32)(IPAddress.HostToNetworkOrder(reader.ReadInt32())); if (RELOAD_MsgCode == RELOAD_MessageCode.Store_Request) { while (StreamUtil.ReadBytes(posBeforeSD, reader) < storedDataLen) { /* reading properties of StoredData struct */ UInt32 stored_data_lenght = (UInt32)(IPAddress.NetworkToHostOrder(reader.ReadInt32())); UInt64 storage_time = (UInt64)(IPAddress.NetworkToHostOrder(reader.ReadInt64())); UInt32 lifetime = (UInt32)(IPAddress.NetworkToHostOrder(reader.ReadInt32())); ReloadGlobals.DataModel data_model = myManager.GetDataModelfromKindId(store_kind_data.Kind); Boolean exists; IUsage usage; StoredDataValue stored_data_value; switch (data_model) { case ReloadGlobals.DataModel.SINGLE_VALUE: throw new NotImplementedException("There is no Usage with Single Value atm"); case ReloadGlobals.DataModel.ARRAY: UInt32 index = (UInt32)(IPAddress.NetworkToHostOrder((int)reader.ReadInt32())); exists = (reader.ReadByte() == 0x00 ? false : true); usage = myManager.GetUsageFromReader(rm, reader, reload_msg_size, store_kind_data.Kind); stored_data_value = new StoredDataValue(index, usage, exists); break; case ReloadGlobals.DataModel.DICTIONARY: UInt16 keyLength = (UInt16)(IPAddress.NetworkToHostOrder((short)reader.ReadInt16())); string key = BitConverter.ToString(reader.ReadBytes(keyLength), 0, keyLength); //key is a hex string key = key.Replace("-", ""); exists = (reader.ReadByte() == 0x00 ? false : true); usage = myManager.GetUsageFromReader(rm, reader, reload_msg_size, store_kind_data.Kind); stored_data_value = new StoredDataValue(key, usage, exists); break; default: throw new NotSupportedException(String.Format("The data_model {0} is not supported", data_model)); } StoredData stored_data = new StoredData(storage_time, lifetime, stored_data_value); stored_data.Signature = new Signature(myManager.m_ReloadConfig).FromReader(reader, reload_msg_size); // TODO Process signature store_kind_data.Add(stored_data); appendStoreKindData(store_kind_data); } } } UInt32 totalRead = StreamUtil.ReadBytes(posBeforeMsg, reader); reload_msg_size = reload_msg_size - (totalRead + 1); } catch (Exception ex) { throw ex; } return this; }