public bool VerifyMetadata(string pubkey) { if (LoadIndexMetaData() == false) { return(false); } RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(pubkey); // Integrity check if (RSA.VerifyData(GetDataToHash(), new SHA256CryptoServiceProvider(), SignedHash) == false) { return(false); } // Freshness check // TODO: change limit to startTime + Duration // Check for overflow long limit = long.MaxValue; if ((StreamFactory.NowUtc() > limit) || (StreamFactory.NowUtc() < startTime)) { return(false); } return(true); }
public new void Append(List <IKey> listOfKeys, IValue value) { StrValue strDataFilePathValue = null; byte[] hash = null; long timestamp = StreamFactory.NowUtc(); foreach (IKey key in listOfKeys) { if (logger != null) { logger.Log("Start ValueDataStream Append"); } if (strDataFilePathValue == null) { Tuple <byte[], StrValue> temp = UpdateHelper(key, value, true, timestamp); hash = temp.Item1; strDataFilePathValue = temp.Item2; } else { base.UpdateHelper(key, strDataFilePathValue, true, hash, timestamp); } if (logger != null) { logger.Log("End ValueDataStream Append"); } } }
protected void Initialize(Logger Log, FqStreamID FQSID, int num, CallerInfo Ci, LocationInfo Li, StreamFactory.StreamOp op, StreamFactory.StreamSecurityType type, CompressionType ctype, int ChunkSizeForUpload, int ThreadPoolSize) { // Logging related logger = Log; if (logger != null) { logger.Log("Start ValueDataStream Init DataStructures"); } // Naming related streamid = FQSID; stream = new MetaDataService.FQStreamID(); stream.HomeId = FQSID.HomeId; stream.AppId = FQSID.AppId; stream.StreamId = FQSID.StreamId; BaseDir = Path.GetFullPath((null != Ci.workingDir) ? Ci.workingDir : Directory.GetCurrentDirectory()); targetDir = BaseDir + "/" + streamid.ToString(); seq_num = num; targetDir = targetDir + "/" + seq_num; if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } // Ownership related caller = new MetaDataService.Principal(); caller.HomeId = streamid.HomeId; caller.AppId = Ci.appName; owner = new MetaDataService.Principal(); owner.HomeId = streamid.HomeId; owner.AppId = streamid.AppId; // Location account = new MetaDataService.AccountInfo(); account.accountName = Li.accountName; account.accountKey = Li.accountKey; account.location = "" + Li.st; // Index related index = new Dictionary <IKey, List <DataBlockInfo> >(); t_s = StreamFactory.NowUtc(); t_e = 0; IndexHash = null; isSealed = false; // Low level hasher = new SHA256CryptoServiceProvider(); // State maint streamop = op; streamtype = type; streamcompressiontype = ctype; //sync related this.StreamChunkSizeForUpload = ChunkSizeForUpload; this.StreamThreadPoolSize = ThreadPoolSize; }
public void SignMetadata(string prikey) { RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); RSA.FromXmlString(prikey); startTime = StreamFactory.NowUtc(); duration = long.MaxValue; SignedHash = RSA.SignData(GetDataToHash(), new SHA256CryptoServiceProvider()); FlushIndexMetaData(); }
public bool Close(bool retainIndex = false) { if (logger != null) { logger.Log("Start ValueDataStream Close"); } if (logger != null) { logger.Log("Start ValueDataStream File Close"); } if (fs_bw != null) { if ((streamop == StreamFactory.StreamOp.Write) && !isSealed) { fout.Flush(true); } fs_bw.Close(); fs_bw = null; } if (fs_br != null) { fs_br.Close(); fs_br = null; } if (logger != null) { logger.Log("End ValueDataStream File Close"); } if (!isClosed) { if (!isSealed && (streamop == StreamFactory.StreamOp.Write)) { t_e = StreamFactory.NowUtc(); Flush(retainIndex); Sync(); } else if (streamop == StreamFactory.StreamOp.Read) { Flush(retainIndex); } } isClosed = true; if (logger != null) { logger.Log("End ValueDataStream Close"); } return(isClosed); }
public new void Append(List <Tuple <IKey, IValue> > list) { long timestamp = StreamFactory.NowUtc(); foreach (Tuple <IKey, IValue> keyValPair in list) { if (logger != null) { logger.Log("Start ValueDataStream Append"); } UpdateHelper(keyValPair.Item1, keyValPair.Item2, true, timestamp); if (logger != null) { logger.Log("End ValueDataStream Append"); } } }
public void Append(List <IKey> listOfKeys, IValue value) { long offset = -1; long timestamp = StreamFactory.NowUtc(); foreach (IKey key in listOfKeys) { if (logger != null) { logger.Log("Start ValueDataStream Append"); } offset = UpdateHelper(key, value, true, null, timestamp, offset); if (logger != null) { logger.Log("End ValueDataStream Append"); } } }
protected long UpdateHelper(IKey key, IValue value, bool IsAppend, Byte[] valHash = null, long timestamp = -1, long offsetInStream = -1) { long offset; if (!key.GetType().Equals(typeof(KeyType))) { throw new InvalidDataException("Invalid IKey Type"); } if (!value.GetType().Equals(typeof(ValType))) { throw new InvalidDataException("Invalid IValue Type"); } if (logger != null) { logger.Log("Start ValueDataStream Tag Lookup"); } List <DataBlockInfo> offsets; // check if the entry is present (and record offset) if (index.ContainsKey(key)) { // TODO: this could just have been a simple collision // -- make sure that this is the same key // -- if different, use a secondary hash function offsets = index[key]; } else { offsets = new List <DataBlockInfo>(); index[key] = offsets; // IncrementIndexSize(offsets.Count); IncrementIndexSize(1); } if (logger != null) { logger.Log("End ValueDataStream Tag Lookup"); } if (logger != null) { logger.Log("Start ValueDataStream Construct DataBlock"); } // write <op (1B), ts, key, val_len, val> byte op; if (IsAppend) { op = (byte)WriteOp.AppendOp; } else { op = (byte)WriteOp.UpdateOp; } long ts; // Construct datablock if (timestamp == -1) { ts = StreamFactory.NowUtc(); } else { ts = timestamp; } DataBlock <KeyType, ValType> db = new DataBlock <KeyType, ValType>(); db.op = op; //db.timestamp = ts; //db.setKey(key); /* * Commenting out because the synchronizer will do this now at the chunk level * if (streamtype == StreamFactory.StreamSecurityType.Secure) * { * //if (logger != null) logger.Log("Start encrypting value"); * db.value = new ByteValue(Crypto.EncryptBytesSimple(value.GetBytes(), Crypto.KeyDer(acl_md.encKey), acl_md.IV)); * //if (logger != null) logger.Log("End encrypting value"); * } * else*/ { //db.value = new ByteValue(value.GetBytes()); db.setValue(value); } if (logger != null) { logger.Log("End ValueDataStream Construct DataBlock"); } if (offsetInStream == -1) { if (logger != null) { logger.Log("Start ValueDataStream Serialize DataBlock"); } //Byte[] buffer = db.SerializeToByteStream().ToArray(); Byte[] buffer = SerializerHelper <DataBlock <KeyType, ValType> > .SerializeToProtoStream(db).ToArray(); if (logger != null) { logger.Log("End ValueDataStream Serialize DataBlock"); } if (logger != null) { logger.Log("Start ValueDataStream WriteToDisc DataBlock"); } // fs_bw.Write(db.SerializeToJsonStream()); // get file offset; add <key_hash, offset> to index fs_bw.BaseStream.Seek(0, SeekOrigin.End); offset = fs_bw.BaseStream.Position; fs_bw.Write(buffer.Length); fs_bw.Write(buffer); fs_bw.Flush(); if (logger != null) { logger.Log("End ValueDataStream WriteToDisc DataBlock"); } } else { offset = offsetInStream; } // Construct dbinfo in index if (logger != null) { logger.Log("Start ValueDataStream Construct and Add DataBlockInfo"); } DataBlockInfo latest_tso = new DataBlockInfo(ts, offset); if (streamtype == StreamFactory.StreamSecurityType.Secure) { if (valHash != null) // passed from the dir stream and written into datablockinfo. not written for file stream { latest_tso.hashValue = valHash; } /* * else * { * if (logger != null) logger.Log("Start taking hash of encrypted value"); * latest_tso.hashValue = hasher.ComputeHash(db.value.GetBytes()); * if (logger != null) logger.Log("End taking hash of encrypted value"); * * } * latest_tso.key_version = acl_md.keyVersion; */ } // apply to index if (IsAppend) { offsets.Add(latest_tso); // IncrementIndexSize(); } else { if (offsets.Count == 0) { offsets.Add(latest_tso); // IncrementIndexSize(); } else { offsets[offsets.Count - 1] = latest_tso; } } if (logger != null) { logger.Log("End ValueDataStream Construct and Add DataBlockInfo"); } return(offset); }