public void CreateDataEntity(Guid userId, DataEntity dataEntity) { LoadMetadata(); EntityMetadata em; List<EntityMetadata> ems = this.metadata.Entities.Where(e => e.Id == dataEntity.Id).ToList(); if (ems.Count == 1) { em = ems[0]; } else { em = new EntityMetadata(); this.metadata.Entities.Add(em); } TranslateToMetadata(userId, em, dataEntity); SaveMetadata(); File.WriteAllBytes(Path.Combine(this.entityDirPath, dataEntity.Id.ToString()), dataEntity.Payload.Content); }
public void Insert(Guid userId, DataEntity dataEntity) { if (userId == Guid.Empty) throw new ArgumentException("userId"); CheckDataEntity(dataEntity); GetDelegationKey(userId); // check that the user has a key (is still valid) byte[] signPublicKey = this.tokenStorage.FindSignPublicKey(userId); if (signPublicKey == null) { throw new InvalidOperationException("Cannot insert data as no sign key is registered for the user"); } if (!DataSigner.IsSignatureValid(dataEntity, dataEntity.Signature, signPublicKey)) { throw new InvalidOperationException("The data signature is not valid"); } // TODO: check access rights) this.dataEntityStorage.InsertDataEntity(userId, dataEntity); }
private DataEntity ReencryptDataEntityMetadata(DataEntity dataEntity, Guid userId) { byte[] delegationKey = GetDelegationKey(userId); DataEntity reencryptedEntity = new DataEntity(); IPreService proxy = CreatePreProxy(); byte[] reencryptedIV = proxy.Reencrypt(delegationKey, dataEntity.AesInfo.IV); proxy = CreatePreProxy(); byte[] reencryptedKey = proxy.Reencrypt(delegationKey, dataEntity.AesInfo.Key); reencryptedEntity.AesInfo = new AesEncryptionInfo(reencryptedKey, reencryptedIV); reencryptedEntity.Attributes = dataEntity.Attributes; reencryptedEntity.Payload = dataEntity.Payload; reencryptedEntity.Id = dataEntity.Id; return reencryptedEntity; }
private void buttonUploadNow_Click(object sender, EventArgs e) { try { if (this.listBoxUploadKeywords.Items.Count == 0) { MessageBox.Show("At least one keyword must be associated with the data before it is uploaded"); return; } if (!this.userkeysLoaded) { MessageBox.Show("You must load user keys first"); return; } byte[] fileContent = File.ReadAllBytes(this.labelUploadData.Text); AesEncryptionInfo encryptionInfo = SymmetricEncryptor.GenerateSymmetricKeyInfo(); byte[] fileCiphertext = SymmetricEncryptor.Encrypt(fileContent, encryptionInfo); IPreService preProxy = CreatePreProxy(); byte[] encSymIv = preProxy.Encrypt(this.masterPublicKey, encryptionInfo.IV); preProxy = CreatePreProxy(); byte[] encSymKey = preProxy.Encrypt(this.masterPublicKey, encryptionInfo.Key); byte[] name = SymmetricEncryptor.Encrypt(Path.GetFileName(this.labelUploadData.Text).GetBytes(), encryptionInfo); DataEntity entity = new DataEntity(); entity.Attributes = CollectAndEncryptAttributes(encryptionInfo); entity.Payload = new FilePayload(name, fileCiphertext); entity.AesInfo = new AesEncryptionInfo(encSymKey, encSymIv); entity.Id = Guid.NewGuid(); // perhaps base guid on the file path?? entity.Signature = DataSigner.Sign(entity, this.userSignKeys); IGatewayService proxy = CreateServiceProxy(); proxy.InsertData(GetUserIdentity(), entity); MessageBox.Show("Done uploading"); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); Logger.LogError("Error preparing and uploading data to server", ex); } }
private void VerifyIntegrity(DataEntity entity) { try { IGatewayService proxy = ProxyFactory.CreateProxy<IGatewayService>(); bool verified = proxy.VerifyIntegrity(this.myId, entity.Id); if (!verified) { MessageBox.Show("The integrity is NOT valid"); } else { MessageBox.Show("The integrity has been verified"); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); Logger.LogError("Error verifying integrity of entity", ex); } }
private static void CheckDataEntity(DataEntity dataEntity) { if (dataEntity == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.Attributes == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.Attributes.Count == 0) throw new ArgumentException("dataEntity"); if (dataEntity.AesInfo == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.AesInfo.Key == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.AesInfo.Key.Length == 0) throw new ArgumentException("dataEntity"); if (dataEntity.AesInfo.IV == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.AesInfo.IV.Length == 0) throw new ArgumentException("dataEntity"); if (dataEntity.Payload == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.Payload.Content == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.Payload.Content.Length == 0) throw new ArgumentException("dataEntity"); if (dataEntity.Payload.Name == null) throw new ArgumentNullException("dataEntity"); if (dataEntity.Payload.Name.Length == 0) throw new ArgumentException("dataEntity"); if (dataEntity.Id == Guid.Empty) throw new ArgumentException("dataEntity"); }
private void DeleteEntity(DataEntity entity) { try { DialogResult result = MessageBox.Show("Are you sure you want to delete: " + entity.Payload.Name.GetString(), "Confirmation",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation); if (result == DialogResult.Yes) { IGatewayService proxy = ProxyFactory.CreateProxy<IGatewayService>(); proxy.DeleteDataEntities(this.myId, rolesUserControlDownload.SelectedRoles, new List<Guid>(new[] { entity.Id })); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); Logger.LogError("Error delting entity from server", ex); } }
private void SaveEntityToFile(DataEntity entity) { try { SaveFileDialog dialog = new SaveFileDialog(); dialog.FileName = entity.Payload.Name.GetString(); DialogResult result = dialog.ShowDialog(); if (result == DialogResult.OK) { IGatewayService proxy = ProxyFactory.CreateProxy<IGatewayService>(); byte[] payloadContent = proxy.GetPayload(this.myId, entity.Id); byte[] plainText = SymmetricEncryptor.Decrypt(payloadContent, entity.AesInfo); File.WriteAllBytes(dialog.FileName, plainText); MessageBox.Show("Done saving file"); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); Logger.LogError("Error saving entity to file", ex); } }
public void InsertData(Guid userId, DataEntity dataEntity) { this.gw.Insert(userId, dataEntity); }
private void buttonUploadNow_Click(object sender, EventArgs e) { try { if (this.listBoxUploadKeywords.Items.Count == 0) { MessageBox.Show("At least one keyword must be associated with the data before it is uploaded"); return; } if (this.keyPair == null) { MessageBox.Show("You must load user keys first"); return; } if (this.rolesUserControlUploadData.SelectedRoles.Count == 0) { MessageBox.Show("You must select at least one role which should have access to the uploaded data"); return; } byte[] fileContent = File.ReadAllBytes(this.labelUploadData.Text); AesEncryptionInfo encryptionInfo = SymmetricEncryptor.GenerateSymmetricKeyInfo(); byte[] fileCiphertext = SymmetricEncryptor.Encrypt(fileContent, encryptionInfo); IPreService preProxy = CreatePreProxy(); byte[] encSymIv = preProxy.Encrypt(this.keyPair.Public, encryptionInfo.IV); preProxy = CreatePreProxy(); byte[] encSymKey = preProxy.Encrypt(this.keyPair.Public, encryptionInfo.Key); byte[] name = SymmetricEncryptor.Encrypt(Path.GetFileName(this.labelUploadData.Text).GetBytes(), encryptionInfo); DataEntity entity = new DataEntity(); entity.Attributes = CollectAndEncryptAttributes(encryptionInfo); entity.Payload = new FilePayload(name, fileCiphertext); entity.AesInfo = new AesEncryptionInfo(encSymKey, encSymIv); entity.Id = Guid.NewGuid(); entity.Signature = DataSigner.Sign(entity, this.signingKeys); IGatewayService proxy = CreateServiceProxy(); proxy.CreateDataEntities(this.myId, this.rolesUserControlUploadData.SelectedRoles, new[] { entity }); MessageBox.Show("Done uploading"); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); Logger.LogError("Error preparing and uploading data to server", ex); } }
private static void TranslateToMetadata(Guid authorId, EntityMetadata rm, DataEntity e) { rm.AesIV = ConvertBinary(e.AesInfo.IV); rm.AesKey = ConvertBinary(e.AesInfo.Key); rm.Attributes = new Collection<EntityAttribute>(e.Attributes.Select(a => new EntityAttribute(a.Id, ConvertBinary(a.Keyword))).ToList()); rm.AuthorId = authorId; rm.Id = e.Id; rm.Name = ConvertBinary(e.Payload.Name); rm.Signature = ConvertBinary(e.Signature.Value); rm.Size = e.Payload.Size; }
private void DeleteEntity(DataEntity entity) { try { DialogResult result = MessageBox.Show("Are you sure you want to delete: " + entity.Payload.Name.GetString(), "Confirmation",MessageBoxButtons.YesNo,MessageBoxIcon.Exclamation); if (result == DialogResult.Yes) { IGatewayService proxy = ProxyFactory.CreateProxy<IGatewayService>(); proxy.DeleteData(GetUserIdentity(), entity.Id); } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); Logger.LogError("Error delting entity from server", ex); } }
private static DataEntity TranslateFromMetadata(EntityMetadata em) { DataEntity e = new DataEntity(); e.AesInfo = new AesEncryptionInfo(ConvertBinary(em.AesKey), ConvertBinary(em.AesIV)); e.Attributes = new List<Attribute>(em.Attributes.Select(a => new Attribute(a.Id, ConvertBinary(a.Keyword)))); e.Id = em.Id; e.Payload = new FilePayload(ConvertBinary(em.Name), em.Size); e.Signature = new Signature(ConvertBinary(em.Signature)); return e; }
public static Signature Sign(DataEntity dataEntity, byte[] signKeyPair) { return new Signature(Sign(BitConverter.GetBytes(dataEntity.GetHashCode()), signKeyPair)); }
public static bool IsSignatureValid(DataEntity dataEntity, Signature signature, byte[] signPublicKey) { return IsSignatureValid(BitConverter.GetBytes(dataEntity.GetHashCode()), signature.Value, signPublicKey); }
private static DataEntity Translate(EntityMetadata md) { DataEntity de = new DataEntity(); de.Id = md.Id; de.Signature = new Signature(Convert.FromBase64String(md.Signature)); de.Payload = new FilePayload(Convert.FromBase64String(md.Name), md.Size); de.Attributes = new List<Attribute>(); foreach (EntityAttribute a in md.Attributes) { de.Attributes.Add(new Attribute(a.Id, Convert.FromBase64String(a.Keyword))); } de.AesInfo = new AesEncryptionInfo(Convert.FromBase64String(md.AesKey), Convert.FromBase64String(md.AesIV)); return de; }
public void InsertDataEntity(Guid userId, DataEntity dataEntity) { LoadEntityMetadata(); AuthorMetadata author; List<AuthorMetadata> authors = this.metadata.Authors.Where(a => a.AuthorId == userId).ToList(); if (authors.Count == 1) { author = authors[0]; } else { author = new AuthorMetadata(); this.metadata.Authors.Add(author); author.AuthorId = userId; } author.EntityIds.Add(new EntityId(dataEntity.Id)); foreach (Attribute attribute in dataEntity.Attributes) { AttributeMetadata am; Attribute modifiedClosureCopy = attribute; List<AttributeMetadata> ams = this.metadata.Attributes.Where(a => a.AttributeId == modifiedClosureCopy.Id).ToList(); if (ams.Count == 1) { am = ams[0]; } else { am = new AttributeMetadata(); this.metadata.Attributes.Add(am); am.AttributeId = attribute.Id; } am.EntityIds.Add(new EntityId(dataEntity.Id)); } EntityMetadata entity; List<EntityMetadata> entities = this.metadata.Entities.Where(e => e.Id == dataEntity.Id).ToList(); if (entities.Count == 1) { entity = entities[0]; } else { entity = new EntityMetadata(); this.metadata.Entities.Add(entity); } entity.Id = dataEntity.Id; entity.Signature = Convert.ToBase64String(dataEntity.Signature.Value); foreach (Attribute attribute in dataEntity.Attributes) { entity.Attributes.Add(new EntityAttribute(attribute.Id, Convert.ToBase64String(attribute.Keyword))); } entity.Name = Convert.ToBase64String(dataEntity.Payload.Name); entity.Size = dataEntity.Payload.Size; entity.AesKey = Convert.ToBase64String(dataEntity.AesInfo.Key); entity.AesIV = Convert.ToBase64String(dataEntity.AesInfo.IV); SaveEntityMetadata(); File.WriteAllBytes(Path.Combine(this.entityDirPath, dataEntity.Id.ToString()), dataEntity.Payload.Content); }