2022-09-06 17:07:35 +02:00
|
|
|
package server
|
2022-09-06 09:30:45 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-09-06 17:07:35 +02:00
|
|
|
"fmt"
|
2022-09-06 09:30:45 +02:00
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/helper"
|
2022-09-19 14:58:26 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-09-06 09:30:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const key = "3pw0c8#6ZG8{75b5;3?fe80$2"
|
|
|
|
|
|
|
|
type Identity struct {
|
2022-10-29 20:07:35 +02:00
|
|
|
ServerName string `json:"servername,omitempty"`
|
|
|
|
ServerDesc string `json:"serverdesc,omitempty"`
|
|
|
|
ServerKp meowlib.KeyPair `json:"server_kp,omitempty"`
|
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
OwnerName string `json:"owner_name,omitempty"`
|
|
|
|
OwnerPublicKey string `json:"owner_public_key,omitempty"`
|
|
|
|
ArchiveClients []string `json:"archive_clients,omitempty"`
|
|
|
|
KnownServers []meowlib.Server `json:"known_servers,omitempty"`
|
2022-09-06 09:30:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
func CreateIdentity(ServerName string, ServerDesc string) *Identity {
|
2022-09-06 09:30:45 +02:00
|
|
|
var id Identity
|
2022-09-06 17:07:35 +02:00
|
|
|
id.ServerName = ServerName
|
|
|
|
id.ServerDesc = ServerDesc
|
|
|
|
id.ServerKp = meowlib.NewKeyPair()
|
2022-09-06 09:30:45 +02:00
|
|
|
return &id
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
func LoadIdentity(File string) (*Identity, error) {
|
2022-09-06 09:30:45 +02:00
|
|
|
var id Identity
|
2022-09-06 17:07:35 +02:00
|
|
|
indata, err := ioutil.ReadFile(File)
|
2022-09-06 09:30:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pass, err := helper.DecryptMessageWithPassword([]byte(key), string(indata))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(pass), &id)
|
|
|
|
return &id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id *Identity) Save(file string) error {
|
|
|
|
b, _ := json.Marshal(id)
|
|
|
|
armor, err := helper.EncryptMessageWithPassword([]byte(key), string(b))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(file, []byte(armor), 0644)
|
|
|
|
return err
|
|
|
|
}
|
2022-09-06 17:07:35 +02:00
|
|
|
|
2022-09-19 14:58:26 +02:00
|
|
|
//
|
|
|
|
// 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
|
2022-09-06 17:07:35 +02:00
|
|
|
}
|
2022-09-19 14:58:26 +02:00
|
|
|
return pmsg.From, pmsg.Payload, pmsg.Signature, nil
|
2022-09-06 17:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AsymDecryptMessage reads a message from a specific client contact
|
|
|
|
func (id *Identity) AsymDecryptMessage(ClientPublicKey string, Message []byte, Signature []byte) (DecryptedMsg []byte, err error) {
|
2022-09-18 21:17:28 +02:00
|
|
|
DecryptedMsg, err = meowlib.AsymDecryptAndCheck(id.ServerKp.Private, ClientPublicKey, Message, Signature)
|
2022-09-06 17:07:35 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return DecryptedMsg, err
|
|
|
|
}
|
2022-09-19 14:58:26 +02:00
|
|
|
|
|
|
|
// Decode decrypted ToServerMessage
|
2022-10-22 14:41:48 +02:00
|
|
|
func (id *Identity) DeserializeToServerMessage(toServerMessage []byte) (*meowlib.ToServerMessage, error) {
|
2022-09-19 14:58:26 +02:00
|
|
|
tsmsg := &meowlib.ToServerMessage{}
|
|
|
|
if err := proto.Unmarshal(toServerMessage, tsmsg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tsmsg, nil
|
|
|
|
}
|
|
|
|
|
2022-10-15 09:14:13 +02:00
|
|
|
// 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
|
2022-10-22 14:41:48 +02:00
|
|
|
srv_msg, err := id.DeserializeToServerMessage(srv_clear)
|
2022-10-15 09:14:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return srv_msg, nil
|
|
|
|
}
|
|
|
|
|
2022-09-19 14:58:26 +02:00
|
|
|
//
|
|
|
|
// Outbound processing chain
|
|
|
|
//
|
|
|
|
|
2022-10-15 09:14:13 +02:00
|
|
|
func (id *Identity) BuildSimpleAckResponseMessage(uuidToAck string) (fromServerMessage *meowlib.FromServerMessage, err error) {
|
2022-09-19 20:16:57 +02:00
|
|
|
var msg meowlib.FromServerMessage
|
|
|
|
msg.ServerPublicKey = id.ServerKp.Public
|
|
|
|
msg.UuidAck = uuidToAck
|
2022-10-15 09:14:13 +02:00
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2022-10-22 14:41:48 +02:00
|
|
|
func (id *Identity) SerializeFromServerMessage(msg *meowlib.FromServerMessage) (encodedFromServerMessage []byte, err error) {
|
2022-10-15 09:14:13 +02:00
|
|
|
out, err := proto.Marshal(msg)
|
2022-09-25 10:16:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
2022-09-19 20:16:57 +02:00
|
|
|
}
|
|
|
|
|
2022-10-15 09:14:13 +02:00
|
|
|
// 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)
|
2022-09-19 14:58:26 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return EncryptedMsg, Signature, err
|
|
|
|
}
|
2022-09-19 20:16:57 +02:00
|
|
|
|
2022-10-15 09:14:13 +02:00
|
|
|
func (id *Identity) PackForSending(encryptedMessage []byte, signature []byte) (fromServerMessage []byte, err error) {
|
2022-09-19 20:16:57 +02:00
|
|
|
var pmsg meowlib.PackedServerMessage
|
|
|
|
pmsg.Payload = encryptedMessage
|
|
|
|
pmsg.Signature = signature
|
|
|
|
pmsg.From = id.ServerKp.Public
|
|
|
|
out, err := proto.Marshal(&pmsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2022-10-15 09:14:13 +02:00
|
|
|
|
|
|
|
// Outbound helper function
|
2022-10-24 18:44:21 +02:00
|
|
|
func (id *Identity) ProcessOutbound(peerPublicKey string, msg *meowlib.FromServerMessage) (fromServerMessage []byte, err error) {
|
2022-10-22 14:41:48 +02:00
|
|
|
encoded_srv_fromServerMessage, err := id.SerializeFromServerMessage(msg)
|
2022-10-15 09:14:13 +02:00
|
|
|
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
|
|
|
|
}
|