The basic unit of stored data is a single StoredData structure. RELOAD base -12 p.80 --mine
예제 #1
0
 public void Add(StoredData sd) {
     if (sd.Value.GetUsageValue == null)
         throw new ArgumentNullException("StoredData to add is null!");
     values.Add(sd);
 }
예제 #2
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;
        }
예제 #3
0
    /// <summary>
    /// This method validates if an ACP applies.
    /// </summary>
    /// <param name="resId">The requested resId</param>
    /// <param name="resId">The KindId</param>
    /// <param name="data">The StoredData</param>
    /// <returns>True, if ACP applies</returns>
    public Boolean ValuePermitted(ResourceId resId, UInt32 kindId, StoredData data) {
      //var nodeid = ReloadGlobals.retrieveNodeIDfromCertificate( signerCert, ref rfc822Name);


      return true;

    }
예제 #4
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;
        }
예제 #5
0
    public bool validateDataSignature(ResourceId resId, uint kind, StoredData sd) {
      //FetchAns fetch_answer = (FetchAns)(reloadMsg.reload_message_body);

      var ascii = new ASCIIEncoding();
      /* Set alogorithm and identity */
      SignatureAndHashAlgorithm algorithm = new SignatureAndHashAlgorithm(HashAlgorithm.sha256, ReloadGlobals.SignatureAlg);
      /* Covert Idenity to string */
      String identity = sd.Signature.Identity.ToString();
      /* Get string of stored data value */
      var ms = new MemoryStream();
      var bw = new BinaryWriter(ms);
      sd.Value.Dump(bw);
      sd.Value.GetUsageValue.dump(bw);
      ms.Position = 0;
      var sr = new StreamReader(ms);
      string strValue = sr.ReadToEnd();
      sr.Close();
      bw.Close();
      /* Concatenate signature input */
      String strSignaturInput = String.Format("{0}{1}{2}{3}{4}",
        ascii.GetString(resId.Data, 0, resId.Data.Length), kind, sd.StoreageTime,
        strValue, identity);

      byte[] signatureInput = ascii.GetBytes(strSignaturInput);
      byte[] sigVal = sd.Signature.SignaureValue;

      GenericCertificate gencert = GetPKC(sd.Signature.Identity);
      byte[] bcert = gencert.Certificate; //TODO: TEST
      X509Certificate2 signerCert = new X509Certificate2(bcert);

      if (!Utils.X509Utils.VerifyCertificate(signerCert, m_ReloadConfig.RootCertificate))
      {
        m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_ERROR,
          String.Format("validateDataSignatures: NodeID {0}, Certificate" +
          "validation failed (CA Issuer {1})",
          null, signerCert.Issuer));
        //return false;
      }
      var cert = new X509Certificate2(bcert);

      switch (sd.Signature.Algorithm.signature) {
        case SignatureAlgorithm.rsa:
          var cryptoIPT = (RSACryptoServiceProvider)cert.PublicKey.Key;

          switch (sd.Signature.Algorithm.hash) {
            case HashAlgorithm.sha256:
              var sha256 = new SHA256CryptoServiceProvider();

              if (!cryptoIPT.VerifyData(signatureInput, sha256, sigVal)) {
                throw new InvalidOperationException("Invalid signature");
                return false;
              }
              else {
                m_ReloadConfig.Logger(ReloadGlobals.TRACEFLAGS.T_FORWARDING, "DATA SIGNATURE VALID!!");
                return true;
              }
              break;
            default:
              throw new NotImplementedException("AccessController:" +
                "hash algoritm not implemented!");
          }

          break;
        case SignatureAlgorithm.dsa:
          throw new NotImplementedException("AccessController:" +
            "DSA encryption not implemented!");
        default:
          throw new NotImplementedException("AccessController:" +
            "encryption not implemented!");
      }
    }