Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        static async Task TestKeyDistributionService()
        {
            string     keyServiceUrl = "http://localhost:5000/api/keys";
            HttpClient client        = new HttpClient();

            string abonentKey = "b14ca5898a4e4133bbce2ea2315a1916";
            string abonent    = "device_emulator";
            string keyService = "MachineService";
            string authority  = "AttributeAuthority";

            string[] abonentAttributes = new string[] { "teapot", "iot", "science" };

            var encryptor  = new DataSymmetricEncryption();
            var serializer = new ProtobufDataSerializer();
            var builder    = new KeyDistributionBuilder(serializer, encryptor);

            var(firstRequestData, abonentNonce) = builder.BuildStepOne(abonentKey, abonent, keyService, authority, abonentAttributes);
            Console.WriteLine($"Generated nonce = {abonentNonce}");

            var requestContent = new ByteArrayContent(firstRequestData);
            var response       = await client.PostAsync(keyServiceUrl, requestContent);

            Console.WriteLine($"Http response status code = {response.StatusCode}");

            var responseData = await response.Content.ReadAsByteArrayAsync();

            var payload = builder.GetPayload <KeyDistributionAuthToAbonent>(responseData, abonentKey);

            Console.WriteLine($"Received nonce = {payload.Nonce}");

            var cpabe = new MockCPAbe();

            MockSecretKey abonentPrivateKey = new MockSecretKey();

            abonentPrivateKey.Value = new byte[payload.SecretKey.Length];
            payload.SecretKey.CopyTo(abonentPrivateKey.Value, 0);

            MockPublicKey authorityPublicKey = new MockPublicKey();

            authorityPublicKey.Value = new byte[payload.PublicKey.Length];
            payload.PublicKey.CopyTo(authorityPublicKey.Value, 0);

            var ct = await cpabe.Encrypt("TestKeyDistributionService", authorityPublicKey,
                                         new MockAttributes(abonentAttributes));

            string message = await cpabe.Decrypt(ct, authorityPublicKey, abonentPrivateKey);

            Console.WriteLine(message);
        }
Ejemplo n.º 3
0
        static void KeyServiceScenario(string prefix, int copies, int duration)
        {
            string abonent       = "test_runner";
            string keyServiceUrl = "http://localhost:5000/api/keys";
            string abonentKey    = "b14ca5898a4e4133bbce2ea2315a1916";
            string keyService    = "MachineService";
            string authority     = "AttributeAuthority";

            var testRunnerData = GetTestRunnerAttrbutePackages();

            foreach (var attrsData in testRunnerData)
            {
                var scenarionName = $"{prefix}_scenario_{attrsData.Length}";
                var stepName      = $"{prefix}_step_{attrsData.Length}";

                var step = Step.Create(stepName, async context =>
                {
                    var client     = new HttpClient();
                    var encryptor  = new DataSymmetricEncryption();
                    var serializer = new ProtobufDataSerializer();
                    var builder    = new KeyDistributionBuilder(serializer, encryptor);

                    var(firstRequestData, abonentNonce) = builder.BuildStepOne(abonentKey, abonent, keyService, authority, attrsData);
                    //Console.WriteLine($"Generated nonce = {abonentNonce}");

                    var requestContent = new ByteArrayContent(firstRequestData);
                    var response       = await client.PostAsync(keyServiceUrl, requestContent);
                    //Console.WriteLine($"Http response status code = {response.StatusCode}");

                    //var responseData = await response.Content.ReadAsByteArrayAsync();
                    //var payload = builder.GetPayload<KeyDistributionAuthToAbonent>(responseData, abonentKey);
                    //Console.WriteLine($"Received nonce = {payload.Nonce}");

                    return(Response.Ok());
                });

                var scenario = ScenarioBuilder
                               .CreateScenario(scenarionName, new[] { step })
                               .WithConcurrentCopies(copies)
                               .WithDuration(TimeSpan.FromSeconds(duration));
                NBomberRunner
                .RegisterScenarios(scenario)
                .RunTest();
            }
        }