public async Task <byte[]> Generate(byte[] data) { await CheckKeys(); var authRequest = _builder.GetStepData <KeyDistributionStepTwo>(data); var abonent = await _loginService.GetLogin(authRequest.AbonentId); var keyService = await _loginService.GetLogin(authRequest.KeyServiceId); var abonentPayload = _builder.GetPayload <KeyDistributionRequestPayload>(authRequest.AbonentPayload, abonent.SharedKey); var servicePayload = _builder.GetPayload <KeyDistributionRequestPayload>(authRequest.KeyServicePayload, keyService.SharedKey); if (abonentPayload.AttributeAuthorityId != _settings.Value.Name) { throw new ProtocolArgumentException("Incorrect attribute authority id"); } if (servicePayload.AttributeAuthorityId != _settings.Value.Name) { throw new ProtocolArgumentException("Incorrect attribute authority id"); } if (abonentPayload.AbonentId != servicePayload.AbonentId) { throw new ProtocolArgumentException("Incorrect abonent id"); } if (abonentPayload.KeyServiceId != servicePayload.KeyServiceId) { throw new ProtocolArgumentException("Incorrect key service id"); } if (!abonentPayload.Attributes.SequenceEqual(servicePayload.Attributes)) { throw new ProtocolArgumentException("Incorrect attributes"); } // Console.WriteLine(String.Join(" ", abonentPayload.Attributes)); // Console.WriteLine(String.Join(" ", abonent.Attributes)); if (abonentPayload.Attributes .Where(attr => !abonent.Attributes.Contains(attr)) .Any()) { throw new ProtocolArgumentException("Attribute access error"); } var secretKey = await cpabeCenter.Generate(masterKey, publicKey, new MockAttributes(abonentPayload.Attributes)); var authResponse = _builder.BuildStepThree(abonent.SharedKey, keyService.SharedKey, abonentPayload.Nonce, servicePayload.Nonce, publicKey.Value, secretKey.Value); return(authResponse); }
static async Task TestKeyDistributionBuilder() { string abonentKey = "b14ca5898a4e4133bbce2ea2315a1916"; string serviceKey = "b14ca5898a4e4132bbce2ea2315a1915"; string abonent = "device_emulator"; string keyService = "test"; string authority = "test"; string[] abonentAttributes = new string[] { "test" }; var cpabe = new MockCPAbe(); var keys = await cpabe.Setup(); var encryptor = new DataSymmetricEncryption(); var serializer = new ProtobufDataSerializer(); var builder = new KeyDistributionBuilder(serializer, encryptor); var(firstStepData, abonenNonce) = builder.BuildStepOne(abonentKey, abonent, keyService, authority, abonentAttributes); var deserializedFirstStep = builder.GetStepData <KeyDistrubutionStepOne>(firstStepData); var(secondStepData, serviceNonce) = builder.BuildStepTwo(serviceKey, abonent, keyService, authority, abonentAttributes, deserializedFirstStep.Payload); var deserializedSecondStep = builder.GetStepData <KeyDistributionStepTwo>(secondStepData); var deserializedAbonentPayload = builder.GetPayload <KeyDistributionRequestPayload>(deserializedSecondStep.AbonentPayload, abonentKey); var deserializedServicePayload = builder.GetPayload <KeyDistributionRequestPayload>(deserializedSecondStep.KeyServicePayload, serviceKey); var secretKey = await cpabe.Generate(keys.MasterKey, keys.PublicKey, new MockAttributes(deserializedAbonentPayload.Attributes)); var thirdStepData = builder.BuildStepThree(abonentKey, serviceKey, deserializedAbonentPayload.Nonce, deserializedServicePayload.Nonce, keys.PublicKey.Value, secretKey.Value); var deserializedThirdStep = builder.GetStepData <KeyDistributionStepThree>(thirdStepData); var abonentResult = builder.GetPayload <KeyDistributionAuthToAbonent>(deserializedThirdStep.AbonentPayload, abonentKey); var serviceResult = builder.GetPayload <KeyDistributionAuthToService>(deserializedThirdStep.ServicePayload, serviceKey); MockSecretKey abonentPrivateKey = new MockSecretKey(); abonentPrivateKey.Value = new byte[abonentResult.SecretKey.Length]; abonentResult.SecretKey.CopyTo(abonentPrivateKey.Value, 0); MockPublicKey authorityPublicKey = new MockPublicKey(); authorityPublicKey.Value = new byte[abonentResult.PublicKey.Length]; abonentResult.PublicKey.CopyTo(authorityPublicKey.Value, 0); var ct = await cpabe.Encrypt("TestKeyDistributionBuilder", authorityPublicKey, new MockAttributes(abonentAttributes)); string message = await cpabe.Decrypt(ct, authorityPublicKey, abonentPrivateKey); Console.WriteLine(message); }
static async Task TestMockCpabe(string plainText) { Console.WriteLine("Starting"); var cpabe = new MockCPAbe(); var keys = await cpabe.Setup(); var secret = await cpabe.Generate(keys.MasterKey, keys.PublicKey, new MockAttributes("test1 test2")); Console.WriteLine(plainText); var ct = await cpabe.Encrypt(plainText, keys.PublicKey, new MockAttributes("test1 test2")); string message = await cpabe.Decrypt(ct, keys.PublicKey, secret); Console.WriteLine(message); Console.WriteLine("Finishing"); }