Exemplo n.º 1
0
        public IActionResult EncryptMessage(InteractionModel interactModel)
        {
            //Generate url's file
            string filePublicKey = parameters.Value.FilesOutput + parameters.Value.PubKeyFile + interactModel.userNameDestination + interactModel.userNameOrigin;
            //Initialize models and classes
            SendMessageModel      sendFirstMessage = new SendMessageModel();
            RSAEncryption         rsaEncrypt       = new RSAEncryption();
            RSASigning            rsaSigning       = new RSASigning(interactModel.userNameDestination + interactModel.userNameOrigin);
            AESEncryption         aesEncryption    = new AESEncryption(parameters.Value.KeyAESSize);
            ResponseSignData      responseSign     = new ResponseSignData();
            ResponseSignData      responseSignId   = new ResponseSignData();
            ResponseEncryptAES    responseAES      = new ResponseEncryptAES();
            ResponseEncryptAESKey responseAESKey   = new ResponseEncryptAESKey();

            // Looking for partner RSA public key
            if (rsaEncrypt.KeysPartnerExists(interactModel.userNameDestination + interactModel.userNameOrigin, filePublicKey))
            {
                FileWriter.WriteOnEvents(EventLevel.Info, "Llaves RSA para cifrado encontradas.");
                FileWriter.WriteOnEvents(EventLevel.Info, "Iniciando firmado de mensaje.");
                //Sign data with RSA Private Key
                responseSign = rsaSigning.signData(interactModel.mensaje);
                if (responseSign.result)
                {
                    //Encrypt Message
                    if (aesEncryption.generateProperties())
                    {
                        responseAES = aesEncryption.EncryptMessage(interactModel.mensaje);
                        if (!responseAES.result)
                        {
                            FileWriter.WriteOnEvents(EventLevel.Error, "Error en el proceso de cifrado de mensaje, verifique los eventos previos.");
                            return(BadRequest(sendFirstMessage));
                        }
                    }
                }
                else
                {
                    FileWriter.WriteOnEvents(EventLevel.Error, "Falla en intento de firma de mensaje, verificar logs anteriores.");
                    return(BadRequest(sendFirstMessage));
                }
            }
            else
            {
                FileWriter.WriteOnEvents(EventLevel.Error,
                                         "Imposible cifrar mensaje, llaves RSA para origen:" +
                                         interactModel.userNameOrigin + "\tdestino:" + interactModel.userNameDestination + " no encontradas");
                return(BadRequest(sendFirstMessage));
            }
            //Encrypt AES Key
            if (responseAES.privateKey != null)
            {
                FileWriter.WriteOnEvents(EventLevel.Info, "Iniciando proceso de cifrado llaves AES con RSA");
                responseAESKey = rsaEncrypt.EncryptAESKey(responseAES.privateKey, filePublicKey);
            }
            else
            {
                FileWriter.WriteOnEvents(EventLevel.Error, "Error en cifrado llave AES con RSA, no existe la llave de AES.");
                return(BadRequest(sendFirstMessage));
            }
            //Generate de sign for server identification
            //responseSignId = rsaSigning.signData(interactModel.userNameOrigin+interactModel.userNameDestination);
            //if(!responseSignId.result){
            //     FileWriter.WriteOnEvents(EventLevel.Error,"Falla en intento de firma de identificacion contra servidor, verificar logs anteriores.");

            //     return BadRequest(sendFirstMessage);
            // }
            //Call the server service and send the data model
            //ServerRequest server = new ServerRequest(parameters.Value.EndpointServer,parameters.Value.SendFirstMessage,parameters.Value.GetRequest);

            sendFirstMessage.encryptedMessage    = responseAES.encryptedData;
            sendFirstMessage.encryptSignature    = responseSign.signData;
            sendFirstMessage.encryptedKey        = responseAESKey.encryptedKey;
            sendFirstMessage.idSignature         = responseSignId.signData;
            sendFirstMessage.initVector          = responseAES.InitVector;
            sendFirstMessage.userNameOrigin      = interactModel.userNameOrigin;
            sendFirstMessage.userNameDestination = interactModel.userNameDestination;

            FileWriter.WriteOnEvents(EventLevel.Info, "Solicitud de envio de llave exitoso.");
            return(Ok(sendFirstMessage));
        }