Reduce return values for mobile compatibility

This commit is contained in:
ycc 2022-12-19 20:15:53 +01:00
parent c6ec65f3fa
commit 9e56d3eb03
7 changed files with 95 additions and 74 deletions

View File

@ -100,7 +100,9 @@ func AsymDecryptArmored(PrivateKey string, data []byte) ([]byte, error) {
return []byte(decrypted), err return []byte(decrypted), err
} }
func AsymEncryptAndSign_helpers(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) ([]byte, []byte, error) { /*
func AsymEncryptAndSign_helpers(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) (*EncryptedMessage, error) {
var enc EncryptedMessage
pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey) pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey)
if err != nil { if err != nil {
log.Error().Msg("Message encryption and sign b64 failed") log.Error().Msg("Message encryption and sign b64 failed")
@ -113,7 +115,9 @@ func AsymEncryptAndSign_helpers(PublicEncryptionKey string, PrivateSignatureKey
if err != nil { if err != nil {
log.Error().Msg("Message encryption and sign failed") log.Error().Msg("Message encryption and sign failed")
} }
return []byte(encrypted), []byte(signature), err enc.data = []byte(encrypted)
enc.signature = []byte(signature)
return &enc, err
} }
func AsymDecryptAndCheck_helpers(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) { func AsymDecryptAndCheck_helpers(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) {
@ -131,7 +135,7 @@ func AsymDecryptAndCheck_helpers(MyPrivateEncryptionKey string, MyContactPublicK
} }
return DecryptedMessage, err return DecryptedMessage, err
} }
*/
func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessage, error) { func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessage, error) {
publicKeyRing, err := createPublicKeyRing(key) publicKeyRing, err := createPublicKeyRing(key)
if err != nil { if err != nil {
@ -193,35 +197,43 @@ func createPublicKeyRing(publicKey string) (*crypto.KeyRing, error) {
return publicKeyRing, nil return publicKeyRing, nil
} }
func AsymEncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) ([]byte, []byte, error) { func AsymEncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) (*EncryptedMessage, error) {
var enc EncryptedMessage
pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey) pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey)
if err != nil { if err != nil {
log.Error().Msg("Message encryption and sign b64 failed") log.Error().Msg("Message encryption and sign b64 failed")
return nil, err
} }
priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey) priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey)
if err != nil { if err != nil {
log.Error().Msg("Message encryption and sign b64 failed") log.Error().Msg("Message encryption and sign b64 failed")
return nil, err
} }
ciphertext, signature, err := encryptAndSignMessage(string(pub), string(priv), crypto.NewPlainMessage(data)) ciphertext, signature, err := encryptAndSignMessage(string(pub), string(priv), crypto.NewPlainMessage(data))
if err != nil { if err != nil {
log.Error().Msg("Message encryption failed") log.Error().Msg("Message encryption failed")
return nil, nil, err return nil, err
} }
return ciphertext.GetBinary(), signature, err enc.Data = ciphertext.GetBinary()
enc.Signature = []byte(signature)
return &enc, err
} }
func AsymDecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) { func AsymDecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) {
priv, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey) priv, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey)
if err != nil { if err != nil {
log.Error().Msg("Message decryption and sign b64 failed") log.Error().Msg("Message decryption and sign b64 failed")
return nil, err
} }
pub, err := base64.StdEncoding.DecodeString(MyContactPublicKey) pub, err := base64.StdEncoding.DecodeString(MyContactPublicKey)
if err != nil { if err != nil {
log.Error().Msg("Message decryption and sign b64 failed") log.Error().Msg("Message decryption and sign b64 failed")
return nil, err
} }
DecryptedMessage, err = decryptAndCheckMessage(string(pub), string(priv), crypto.NewPGPMessage(data), crypto.NewPGPSignature(Signature)) DecryptedMessage, err = decryptAndCheckMessage(string(pub), string(priv), crypto.NewPGPMessage(data), crypto.NewPGPSignature(Signature))
if err != nil { if err != nil {
log.Error().Msg("Message decryption and sign failed") log.Error().Msg("Message decryption and sign failed")
return nil, err
} }
return DecryptedMessage, err return DecryptedMessage, err
} }

View File

@ -83,11 +83,11 @@ func TestAsymEncryptDecrypt(t *testing.T) {
func TestAsymEncryptDecryptSigned(t *testing.T) { func TestAsymEncryptDecryptSigned(t *testing.T) {
kp := NewKeyPair() kp := NewKeyPair()
foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~" foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
encMess, sign, err := AsymEncryptAndSign(kp.Public, kp.Private, []byte(foo)) enc, err := AsymEncryptAndSign(kp.Public, kp.Private, []byte(foo))
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
decMess, err2 := AsymDecryptAndCheck(kp.Private, kp.Public, encMess, sign) decMess, err2 := AsymDecryptAndCheck(kp.Private, kp.Public, enc.Data, enc.Signature)
if err2 != nil { if err2 != nil {
log.Println(err2.Error()) log.Println(err2.Error())
} }
@ -97,11 +97,11 @@ func TestAsymEncryptDecryptSigned(t *testing.T) {
func TestAsymEncryptDecryptSigned2(t *testing.T) { func TestAsymEncryptDecryptSigned2(t *testing.T) {
kp := NewKeyPair() kp := NewKeyPair()
foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~" foo := "!#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz{|}~"
encMess, sign, err := AsymEncryptAndSign(kp.Public, kp.Private, []byte(foo)) enc, err := AsymEncryptAndSign(kp.Public, kp.Private, []byte(foo))
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
decMess, err2 := AsymDecryptAndCheck(kp.Private, kp.Public, encMess, sign) decMess, err2 := AsymDecryptAndCheck(kp.Private, kp.Public, enc.Data, enc.Signature)
if err2 != nil { if err2 != nil {
log.Println(err2.Error()) log.Println(err2.Error())
} }
@ -112,13 +112,13 @@ func TestFlutterCompat(t *testing.T) {
pub := base64.StdEncoding.EncodeToString([]byte(publicKeyECC)) pub := base64.StdEncoding.EncodeToString([]byte(publicKeyECC))
priv := base64.StdEncoding.EncodeToString([]byte(privateKeyECC)) priv := base64.StdEncoding.EncodeToString([]byte(privateKeyECC))
encMess, sign, err := AsymEncryptAndSign(pub, priv, []byte("Hello")) enc, err := AsymEncryptAndSign(pub, priv, []byte("Hello"))
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
println(base64.StdEncoding.EncodeToString(encMess)) println(base64.StdEncoding.EncodeToString(enc.Data))
println(base64.StdEncoding.EncodeToString(sign)) println(base64.StdEncoding.EncodeToString(enc.Signature))
} }
@ -163,16 +163,16 @@ func TestFlutterEncode(t *testing.T) {
fmt.Println("Err") fmt.Println("Err")
} }
encMess, sign2, err := AsymEncryptAndSign(base64.StdEncoding.EncodeToString(pub), base64.StdEncoding.EncodeToString(priv), []byte("Hello")) enc, err := AsymEncryptAndSign(base64.StdEncoding.EncodeToString(pub), base64.StdEncoding.EncodeToString(priv), []byte("Hello"))
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
} }
err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.enc", encMess, 0644) err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.enc", enc.Data, 0644)
if err != nil { if err != nil {
fmt.Println("Err") fmt.Println("Err")
} }
err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.sign", sign2, 0644) err = os.WriteFile("/home/yves/Documents/code/flutter/meowlib/gomsg.sign", enc.Signature, 0644)
if err != nil { if err != nil {
fmt.Println("Err") fmt.Println("Err")
} }

View File

@ -156,13 +156,14 @@ func (p *Peer) DeserializeUserMessage(data []byte) (*meowlib.UserMessage, error)
} }
// AsymEncryptMessage prepares a message to send to a specific peer contact // AsymEncryptMessage prepares a message to send to a specific peer contact
func (p *Peer) AsymEncryptMessage(Message []byte) (EncryptedMessage []byte, Signature []byte, Servers []*meowlib.Server, err error) { func (p *Peer) AsymEncryptMessage(Message []byte) (*meowlib.EncryptedMessage, error) {
EncryptedMessage, Signature, err = meowlib.AsymEncryptAndSign(p.Contact.EncryptionPublicKey, p.MyIdentity.Private, Message) var enc *meowlib.EncryptedMessage
enc, err := meowlib.AsymEncryptAndSign(p.Contact.EncryptionPublicKey, p.MyIdentity.Private, Message)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return nil, nil, nil, err return enc, err
} }
return EncryptedMessage, Signature, p.Contact.PullServers, err return enc, err
} }
// AsymDecryptMessage reads a message from a specific peer contact // AsymDecryptMessage reads a message from a specific peer contact
@ -203,13 +204,12 @@ func (p *Peer) ProcessOutboundUserMessage(usermessage *meowlib.UserMessage) (*me
return nil, err return nil, err
} }
// Encrypting it // Encrypting it
EncMsg, EncMsgSignature, FriendServers, err := p.AsymEncryptMessage(serializedMessage) enc, err := p.AsymEncryptMessage(serializedMessage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
fmt.Println(len(FriendServers))
// Packing it // Packing it
packedMsg := p.PackUserMessage(EncMsg, EncMsgSignature) packedMsg := p.PackUserMessage(enc.Data, enc.Signature)
return packedMsg, nil return packedMsg, nil
} }

View File

@ -44,13 +44,14 @@ func (sl *InternalServerList) AddUrls(urls []string) {
} }
// AsymEncryptMessage prepares a message to send to a specific internal server // AsymEncryptMessage prepares a message to send to a specific internal server
func (ints *InternalServer) AsymEncryptMessage(Message []byte) (EncryptedMessage []byte, Signature []byte, err error) { func (ints *InternalServer) AsymEncryptMessage(Message []byte) (*meowlib.EncryptedMessage, error) {
EncryptedMessage, Signature, err = meowlib.AsymEncryptAndSign(ints.ServerData.PublicKey, ints.Me.Private, Message) var enc *meowlib.EncryptedMessage
enc, err := meowlib.AsymEncryptAndSign(ints.ServerData.PublicKey, ints.Me.Private, Message)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return nil, nil, err return nil, err
} }
return EncryptedMessage, Signature, err return enc, err
} }
// AsymDecryptMessage reads a message from a specific internal server // AsymDecryptMessage reads a message from a specific internal server
@ -123,12 +124,12 @@ func (srv *InternalServer) ProcessOutboundMessage(toServerMessage *meowlib.ToSer
return nil, err return nil, err
} }
// Encrypting it // Encrypting it
encToServerMessage, encToServerMessageSignature, err := srv.AsymEncryptMessage(byteToServerMessage) encToServer, err := srv.AsymEncryptMessage(byteToServerMessage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Packing it // Packing it
protoPackedServerMsg, err := srv.PackServerMessage(encToServerMessage, encToServerMessageSignature) protoPackedServerMsg, err := srv.PackServerMessage(encToServer.Data, encToServer.Signature)
if err != nil { if err != nil {
return nil, err return nil, err
} }

6
crypt.go Normal file
View File

@ -0,0 +1,6 @@
package meowlib
type EncryptedMessage struct {
Data []byte
Signature []byte
}

View File

@ -91,15 +91,15 @@ func TestEndToEnd(t *testing.T) {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
// Encrypting it // Encrypting it
EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage(serializedMessage) enc, err := MyFirstFriend.AsymEncryptMessage(serializedMessage)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
fmt.Println(len(FriendServers))
// Packing it
packedMsg := MyFirstFriend.PackUserMessage(EncMsg, EncMsgSignature)
srv := FriendServers[0] // Packing it
packedMsg := MyFirstFriend.PackUserMessage(enc.Data, enc.Signature)
srv := MyFirstFriend.Contact.PullServers[0]
intS1 := client.InternalServerFromServer(srv) intS1 := client.InternalServerFromServer(srv)
// Creating Server message for transporting the user message // Creating Server message for transporting the user message
@ -108,12 +108,12 @@ func TestEndToEnd(t *testing.T) {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
// Encrypting it // Encrypting it
encToServerMessage, encToServerMessageSignature, err := intS1.AsymEncryptMessage(toServerMessage) encToServer, err := intS1.AsymEncryptMessage(toServerMessage)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
// Packing it // Packing it
protoPackedServerMsg, err := intS1.PackServerMessage(encToServerMessage, encToServerMessageSignature) protoPackedServerMsg, err := intS1.PackServerMessage(encToServer.Data, encToServer.Signature)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
@ -156,11 +156,11 @@ func TestEndToEnd(t *testing.T) {
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
srv_resp, srv_respsig, err := server1.AsymEncryptMessage(srv_from, encoded_srv_fromServerMessage) srv_resp, err := server1.AsymEncryptMessage(srv_from, encoded_srv_fromServerMessage)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
resp, err := server1.PackForSending(srv_resp, srv_respsig) resp, err := server1.PackForSending(srv_resp.Data, srv_resp.Signature)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
@ -178,7 +178,7 @@ func TestEndToEnd(t *testing.T) {
// user unpack // user unpack
// user decrypt // user decrypt
decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), EncMsgSignature) decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(enc.Data), enc.Signature)
if err2 != nil { if err2 != nil {
fmt.Println(err2.Error()) fmt.Println(err2.Error())
} }

View File

@ -3,7 +3,8 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os"
"forge.redroom.link/yves/meowlib" "forge.redroom.link/yves/meowlib"
"github.com/ProtonMail/gopenpgp/v2/helper" "github.com/ProtonMail/gopenpgp/v2/helper"
@ -33,7 +34,7 @@ func CreateIdentity(ServerName string, ServerDesc string) *Identity {
func LoadIdentity(File string) (*Identity, error) { func LoadIdentity(File string) (*Identity, error) {
var id Identity var id Identity
indata, err := ioutil.ReadFile(File) indata, err := os.ReadFile(File)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -51,7 +52,7 @@ func (id *Identity) Save(file string) error {
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile(file, []byte(armor), 0644) err = os.WriteFile(file, []byte(armor), 0644)
return err return err
} }
@ -127,13 +128,14 @@ func (id *Identity) SerializeFromServerMessage(msg *meowlib.FromServerMessage) (
} }
// AsymEncryptMessage prepares a message to send to a specific peer contact // 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) { func (id *Identity) AsymEncryptMessage(peerPublicKey string, Message []byte) (*meowlib.EncryptedMessage, error) {
EncryptedMsg, Signature, err = meowlib.AsymEncryptAndSign(peerPublicKey, id.ServerKp.Private, Message) var enc *meowlib.EncryptedMessage
enc, 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, err
} }
return EncryptedMsg, Signature, err return enc, err
} }
func (id *Identity) PackForSending(encryptedMessage []byte, signature []byte) (fromServerMessage []byte, err error) { func (id *Identity) PackForSending(encryptedMessage []byte, signature []byte) (fromServerMessage []byte, err error) {
@ -154,11 +156,11 @@ func (id *Identity) ProcessOutbound(peerPublicKey string, msg *meowlib.FromServe
if err != nil { if err != nil {
return nil, err return nil, err
} }
srv_resp, srv_respsig, err := id.AsymEncryptMessage(peerPublicKey, encoded_srv_fromServerMessage) srv_resp, err := id.AsymEncryptMessage(peerPublicKey, encoded_srv_fromServerMessage)
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp, err := id.PackForSending(srv_resp, srv_respsig) resp, err := id.PackForSending(srv_resp.Data, srv_resp.Signature)
if err != nil { if err != nil {
return nil, err return nil, err
} }