Esempio n. 1
0
        //stuff that controller shouldn't know
        // private bool verifyMessage(string message)
        // {
        //     JsonSerializerOptions serializerOptions = new JsonSerializerOptions();
        //     serializerOptions.Converters.Add(new BigIntegerConverter());
        //     Signature signature = JsonSerializer.Deserialize<Signature>(message, serializerOptions);
        //
        //     return Verifying.verifyMessage(signature, message, EncryptionVariables.PukForClients,
        //         EncryptionVariables.EllipticCurveForClient);
        // }

        // https://weblog.west-wind.com/posts/2017/sep/14/accepting-raw-request-body-content-in-aspnet-core-api-controllers
        // public static async Task<string> GetRawBodyStringAsync(this HttpRequest request, Encoding encoding = null)
        // {
        //     if (encoding == null)
        //         encoding = Encoding.UTF8;
        //
        //     using (StreamReader reader = new StreamReader(request.Body, encoding))
        //         return await reader.ReadToEndAsync();
        // }


        private string loginClientToLoginServer(string message)
        {
            JsonSerializerOptions serializerOptions = new JsonSerializerOptions();

            serializerOptions.Converters.Add(new BigIntegerConverter());

            LoginContainer     loginContainer = JsonSerializer.Deserialize <LoginContainer>(message, serializerOptions);
            EllipticCurvePoint sharedKey      = KeyGeneration.calculateMasterKey(EncryptionVariables.PrkcForClient,
                                                                                 loginContainer.clientPuk, EncryptionVariables.EllipticCurveForClient);
            ECIESUnprocessResult unprocessResult = ECIESProvider.unprocessMessage(sharedKey.x.ToString(),
                                                                                  loginContainer.loginPackage.Tag, loginContainer.loginPackage.EncryptedMessage);

            if (unprocessResult.Status != MyEnum.Successful)
            {
                return(JsonSerializer.Serialize(new LoginResultContainer()
                {
                    LoginResultECIESProcessed = ECIESProvider.processMessage(sharedKey.x.ToString(),
                                                                             JsonSerializer.Serialize(new LoginResult()
                    {
                        Status = 401
                    }))
                }));
            }
            else
            {
                LoginPackage loginPackage =
                    JsonSerializer.Deserialize <LoginPackage>(unprocessResult.DeprocessedMessage);
                LoginResult loginResult = _login.login(loginPackage);

                return(JsonSerializer.Serialize(new LoginResultContainer()
                {
                    LoginResultECIESProcessed = ECIESProvider.processMessage(sharedKey.x.ToString(),
                                                                             JsonSerializer.Serialize(loginResult))
                }));
            }
        }
Esempio n. 2
0
        public async Task <MyEnum> sendLoginRequest(Credentials identity)
        {
            JsonSerializerOptions serializerOptions = new JsonSerializerOptions();

            serializerOptions.Converters.Add(new BigIntegerConverter());

            EllipticCurvePoint niosSharedKey = KeyGeneration.calculateMasterKey(
                ClientVariables.Prk,
                ClientVariables.NiosKey,
                ClientVariables.EllipticCurve);

            string      message     = JsonSerializer.Serialize(identity);
            NIOSPackage NIOSPackage = new()
            {
                clientPUk            = ClientVariables.Puk,
                encryptedCredentials = ECIESProvider.processMessage(niosSharedKey.x.ToString(), message)
            };

            LoginPackage loginPackage = new()
            {
                Cpr         = identity.Cpr,
                niosPackage = NIOSPackage
            };

            LoginContainer loginContainer = new()
            {
                clientPuk    = ClientVariables.Puk,
                loginPackage = ECIESProvider.processMessage(ClientVariables.SharedKey.x.ToString(),
                                                            JsonSerializer.Serialize(loginPackage, serializerOptions))
            };

            string loginContainerString             = JsonSerializer.Serialize(loginContainer, serializerOptions);
            HttpResponseMessage responseLoginResult =
                await new HttpRequester().PostMessage(loginContainerString, "/login");


            string loginResultString             = responseLoginResult.Content.ReadAsStringAsync().Result;
            LoginResultContainer resultContainer = JsonSerializer.Deserialize <LoginResultContainer>(loginResultString, serializerOptions);

            ECIESUnprocessResult unprocessResult = ECIESProvider.unprocessMessage(
                ClientVariables.SharedKey.x.ToString(),
                resultContainer.LoginResultECIESProcessed.Tag,
                resultContainer.LoginResultECIESProcessed.EncryptedMessage);

            if (unprocessResult.Status != MyEnum.Successful)
            {
                return(MyEnum.Unauthorized);
            }
            LoginResult result = JsonSerializer.Deserialize <LoginResult>(unprocessResult.DeprocessedMessage, serializerOptions);

            if (result.Status != 200)
            {
                return(MyEnum.Fail);
            }
            else
            {
                return(MyEnum.Successful);
            }
        }
    }
}