From 2160babeae62d50e5ad0cdc51adf5bac8182250f Mon Sep 17 00:00:00 2001 From: ycc Date: Sat, 15 Oct 2022 09:14:13 +0200 Subject: [PATCH] server helper functions + refactor --- README.md | 0 endtoend_test.go | 7 +++--- server/identity.go | 53 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/endtoend_test.go b/endtoend_test.go index 826e1bd..4fc2f29 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -125,9 +125,10 @@ func TestEndToEnd(t *testing.T) { srv_msg, err := server1.DecodeToServerMessage(srv_clear) // Response : Ack received message srv_fromServerMessage, err := server1.BuildSimpleAckResponseMessage(srv_msg.Uuid) - srv_resp, srv_respsig, err := server1.AsymEncryptMessage(srv_from, srv_fromServerMessage) - resp, err := server1.PackResponseMessage(srv_resp, srv_respsig) - + encoded_srv_fromServerMessage, err := server1.EncodeFromServerMessage(srv_fromServerMessage) + srv_resp, srv_respsig, err := server1.AsymEncryptMessage(srv_from, encoded_srv_fromServerMessage) + resp, err := server1.PackForSending(srv_resp, srv_respsig) + print(resp) ////////////////////////////////////////////// // Back to client, decoding server response // ////////////////////////////////////////////// diff --git a/server/identity.go b/server/identity.go index 6ab6c83..25245a4 100644 --- a/server/identity.go +++ b/server/identity.go @@ -84,24 +84,48 @@ func (id *Identity) DecodeToServerMessage(toServerMessage []byte) (*meowlib.ToSe return tsmsg, nil } +// Inbound helper function +func (id *Identity) ProcessInbound(message []byte) (*meowlib.ToServerMessage, error) { + // Unpack + srv_from, srv_encmsg, srv_signature, err := id.UnpackReceived(message) + if err != nil { + return nil, err + } + // Decrypt + srv_clear, err := id.AsymDecryptMessage(srv_from, srv_encmsg, srv_signature) + if err != nil { + return nil, err + } + // Decode msg + srv_msg, err := id.DecodeToServerMessage(srv_clear) + if err != nil { + return nil, err + } + return srv_msg, nil +} + // // Outbound processing chain // -func (id *Identity) BuildSimpleAckResponseMessage(uuidToAck string) (fromServerMessage []byte, err error) { +func (id *Identity) BuildSimpleAckResponseMessage(uuidToAck string) (fromServerMessage *meowlib.FromServerMessage, err error) { var msg meowlib.FromServerMessage msg.ServerPublicKey = id.ServerKp.Public msg.UuidAck = uuidToAck - out, err := proto.Marshal(&msg) + return &msg, nil +} + +func (id *Identity) EncodeFromServerMessage(msg *meowlib.FromServerMessage) (encodedFromServerMessage []byte, err error) { + out, err := proto.Marshal(msg) if err != nil { return nil, err } return out, nil } -// 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) +// AsymEncryptMessage prepares a message to send to a specific peer contact +func (id *Identity) AsymEncryptMessage(peerPublicKey string, Message []byte) (EncryptedMsg []byte, Signature []byte, err error) { + EncryptedMsg, Signature, err = meowlib.AsymEncryptAndSign(peerPublicKey, id.ServerKp.Private, Message) if err != nil { fmt.Println(err.Error()) return nil, nil, err @@ -109,7 +133,7 @@ func (id *Identity) AsymEncryptMessage(ClientPublicKey string, Message []byte) ( return EncryptedMsg, Signature, err } -func (id *Identity) PackResponseMessage(encryptedMessage []byte, signature []byte) (fromServerMessage []byte, err error) { +func (id *Identity) PackForSending(encryptedMessage []byte, signature []byte) (fromServerMessage []byte, err error) { var pmsg meowlib.PackedServerMessage pmsg.Payload = encryptedMessage pmsg.Signature = signature @@ -120,3 +144,20 @@ func (id *Identity) PackResponseMessage(encryptedMessage []byte, signature []byt } return out, nil } + +// Outbound helper function +func (id *Identity) ProcessOut(peerPublicKey string, msg *meowlib.FromServerMessage) (fromServerMessage []byte, err error) { + encoded_srv_fromServerMessage, err := id.EncodeFromServerMessage(msg) + if err != nil { + return nil, err + } + srv_resp, srv_respsig, err := id.AsymEncryptMessage(peerPublicKey, encoded_srv_fromServerMessage) + if err != nil { + return nil, err + } + resp, err := id.PackForSending(srv_resp, srv_respsig) + if err != nil { + return nil, err + } + return resp, nil +}