123 lines
3.4 KiB
Go
123 lines
3.4 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
"github.com/ProtonMail/gopenpgp/v2/helper"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
const key = "3pw0c8#6ZG8{75b5;3?fe80$2"
|
|
|
|
type Identity struct {
|
|
ServerName string `json:"servername,omitempty"`
|
|
ServerDesc string `json:"serverdesc,omitempty"`
|
|
ServerKp meowlib.KeyPair `json:"server_kp,omitempty"`
|
|
Status string `json:"status,omitempty"`
|
|
// KnownServers ServerList `json:"known_servers,omitempty"`
|
|
}
|
|
|
|
func CreateIdentity(ServerName string, ServerDesc string) *Identity {
|
|
var id Identity
|
|
id.ServerName = ServerName
|
|
id.ServerDesc = ServerDesc
|
|
id.ServerKp = meowlib.NewKeyPair()
|
|
return &id
|
|
}
|
|
|
|
func LoadIdentity(File string) (*Identity, error) {
|
|
var id Identity
|
|
indata, err := ioutil.ReadFile(File)
|
|
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
|
|
}
|
|
|
|
//
|
|
// 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 pmsg.From, pmsg.Payload, pmsg.Signature, nil
|
|
}
|
|
|
|
// AsymDecryptMessage reads a message from a specific client contact
|
|
func (id *Identity) AsymDecryptMessage(ClientPublicKey string, Message []byte, Signature []byte) (DecryptedMsg []byte, err error) {
|
|
DecryptedMsg, err = meowlib.AsymDecryptAndCheck(id.ServerKp.Private, ClientPublicKey, Message, Signature)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return nil, err
|
|
}
|
|
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
|
|
//
|
|
|
|
func (id *Identity) BuildSimpleAckResponseMessage(uuidToAck string) (fromServerMessage []byte, err error) {
|
|
var msg meowlib.FromServerMessage
|
|
msg.ServerPublicKey = id.ServerKp.Public
|
|
msg.UuidAck = uuidToAck
|
|
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)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
return nil, nil, err
|
|
}
|
|
return EncryptedMsg, Signature, err
|
|
}
|
|
|
|
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
|
|
}
|