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-09-06 17: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"`
|
2022-09-06 09:30:45 +02:00
|
|
|
// KnownServers ServerList `json:"known_servers,omitempty"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
//
|
|
|
|
|
2022-09-19 20:16:57 +02:00
|
|
|
func (id *Identity) BuildSimpleAckResponseMessage(uuidToAck string) (fromServerMessage *meowlib.FromServerMessage, err error) {
|
|
|
|
var msg meowlib.FromServerMessage
|
|
|
|
msg.ServerPublicKey = id.ServerKp.Public
|
|
|
|
msg.UuidAck = uuidToAck
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
2022-09-19 14:58:26 +02:00
|
|
|
// 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
|
|
|
|
}
|
2022-09-19 20:16:57 +02:00
|
|
|
|
|
|
|
func (id *Identity) PackResponseMessage(encryptedMessage []byte, signature []byte) (fromServerMessage []byte, err error) {
|
|
|
|
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
|
|
|
|
}
|