From 37f6d7b93a0ffc6adbf7aca4f2084b97e5cee91e Mon Sep 17 00:00:00 2001 From: ycc Date: Mon, 19 Sep 2022 14:58:26 +0200 Subject: [PATCH] Server inbound processing --- server/identity.go | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/server/identity.go b/server/identity.go index 22cb531..51cc565 100644 --- a/server/identity.go +++ b/server/identity.go @@ -7,6 +7,7 @@ import ( "forge.redroom.link/yves/meowlib" "github.com/ProtonMail/gopenpgp/v2/helper" + "google.golang.org/protobuf/proto" ) const key = "3pw0c8#6ZG8{75b5;3?fe80$2" @@ -51,14 +52,17 @@ func (id *Identity) Save(file string) error { return err } -// AsymEncryptMessage prepares a message to send to a specific client contact -func (id *Identity) AsymEncryptMessage(ClientPublicKey string, Message []byte) (EncryptedMsg []byte, Signature []byte, err error) { - EncryptedMsg, Signature, err = meowlib.AsymEncryptAndSign(ClientPublicKey, id.ServerKp.Private, Message) - if err != nil { - fmt.Println(err.Error()) - return nil, nil, err +// +// Inbound processing chain +// + +// Unpack a protobuf received packed message +func (id *Identity) UnpackReceived(message []byte) (fromPublicKey string, encryptedMessage []byte, signature []byte, err error) { + pmsg := &meowlib.PackedServerMessage{} + if err := proto.Unmarshal(message, pmsg); err != nil { + return "", nil, nil, err } - return EncryptedMsg, Signature, err + return pmsg.From, pmsg.Payload, pmsg.Signature, nil } // AsymDecryptMessage reads a message from a specific client contact @@ -70,3 +74,26 @@ func (id *Identity) AsymDecryptMessage(ClientPublicKey string, Message []byte, S } return DecryptedMsg, err } + +// Decode decrypted ToServerMessage +func (id *Identity) DecodeToServerMessage(toServerMessage []byte) (*meowlib.ToServerMessage, error) { + tsmsg := &meowlib.ToServerMessage{} + if err := proto.Unmarshal(toServerMessage, tsmsg); err != nil { + return nil, err + } + return tsmsg, nil +} + +// +// Outbound processing chain +// + +// AsymEncryptMessage prepares a message to send to a specific client contact +func (id *Identity) AsymEncryptMessage(ClientPublicKey string, Message []byte) (EncryptedMsg []byte, Signature []byte, err error) { + EncryptedMsg, Signature, err = meowlib.AsymEncryptAndSign(ClientPublicKey, id.ServerKp.Private, Message) + if err != nil { + fmt.Println(err.Error()) + return nil, nil, err + } + return EncryptedMsg, Signature, err +}