server helper functions + refactor

This commit is contained in:
ycc 2022-10-15 09:14:13 +02:00
parent 6293e979b7
commit 2160babeae
3 changed files with 51 additions and 9 deletions

0
README.md Normal file
View File

View File

@ -125,9 +125,10 @@ func TestEndToEnd(t *testing.T) {
srv_msg, err := server1.DecodeToServerMessage(srv_clear) srv_msg, err := server1.DecodeToServerMessage(srv_clear)
// Response : Ack received message // Response : Ack received message
srv_fromServerMessage, err := server1.BuildSimpleAckResponseMessage(srv_msg.Uuid) srv_fromServerMessage, err := server1.BuildSimpleAckResponseMessage(srv_msg.Uuid)
srv_resp, srv_respsig, err := server1.AsymEncryptMessage(srv_from, srv_fromServerMessage) encoded_srv_fromServerMessage, err := server1.EncodeFromServerMessage(srv_fromServerMessage)
resp, err := server1.PackResponseMessage(srv_resp, srv_respsig) 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 // // Back to client, decoding server response //
////////////////////////////////////////////// //////////////////////////////////////////////

View File

@ -84,24 +84,48 @@ func (id *Identity) DecodeToServerMessage(toServerMessage []byte) (*meowlib.ToSe
return tsmsg, nil 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 // 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 var msg meowlib.FromServerMessage
msg.ServerPublicKey = id.ServerKp.Public msg.ServerPublicKey = id.ServerKp.Public
msg.UuidAck = uuidToAck 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 { if err != nil {
return nil, err return nil, err
} }
return out, nil return out, nil
} }
// AsymEncryptMessage prepares a message to send to a specific client contact // AsymEncryptMessage prepares a message to send to a specific peer contact
func (id *Identity) AsymEncryptMessage(ClientPublicKey string, Message []byte) (EncryptedMsg []byte, Signature []byte, err error) { func (id *Identity) AsymEncryptMessage(peerPublicKey string, Message []byte) (EncryptedMsg []byte, Signature []byte, err error) {
EncryptedMsg, Signature, err = meowlib.AsymEncryptAndSign(ClientPublicKey, id.ServerKp.Private, Message) EncryptedMsg, Signature, err = meowlib.AsymEncryptAndSign(peerPublicKey, id.ServerKp.Private, Message)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return nil, nil, err return nil, nil, err
@ -109,7 +133,7 @@ func (id *Identity) AsymEncryptMessage(ClientPublicKey string, Message []byte) (
return EncryptedMsg, Signature, err 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 var pmsg meowlib.PackedServerMessage
pmsg.Payload = encryptedMessage pmsg.Payload = encryptedMessage
pmsg.Signature = signature pmsg.Signature = signature
@ -120,3 +144,20 @@ func (id *Identity) PackResponseMessage(encryptedMessage []byte, signature []byt
} }
return out, nil 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
}