diff --git a/asymcrypt.go b/asymcrypt.go index 070bf37..cb07abd 100644 --- a/asymcrypt.go +++ b/asymcrypt.go @@ -100,38 +100,42 @@ func AsymDecryptArmored(PrivateKey string, data []byte) ([]byte, error) { return []byte(decrypted), err } -func AsymEncryptAndSign_helpers(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) ([]byte, []byte, error) { - pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey) - if err != nil { - log.Error().Msg("Message encryption and sign b64 failed") +/* + func AsymEncryptAndSign_helpers(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) (*EncryptedMessage, error) { + var enc EncryptedMessage + pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey) + if err != nil { + log.Error().Msg("Message encryption and sign b64 failed") + } + priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey) + if err != nil { + log.Error().Msg("Message encryption and sign b64 failed") + } + encrypted, signature, err := helper.EncryptSignBinaryDetached(string(pub), string(priv), nil, data) + if err != nil { + log.Error().Msg("Message encryption and sign failed") + } + enc.data = []byte(encrypted) + enc.signature = []byte(signature) + return &enc, err } - priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey) - if err != nil { - log.Error().Msg("Message encryption and sign b64 failed") - } - encrypted, signature, err := helper.EncryptSignBinaryDetached(string(pub), string(priv), nil, data) - if err != nil { - log.Error().Msg("Message encryption and sign failed") - } - return []byte(encrypted), []byte(signature), err -} -func AsymDecryptAndCheck_helpers(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) { - priv, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey) - if err != nil { - log.Error().Msg("Message decryption and sign b64 failed") + func AsymDecryptAndCheck_helpers(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) { + priv, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey) + if err != nil { + log.Error().Msg("Message decryption and sign b64 failed") + } + pub, err := base64.StdEncoding.DecodeString(MyContactPublicKey) + if err != nil { + log.Error().Msg("Message decryption and sign b64 failed") + } + DecryptedMessage, err = helper.DecryptVerifyBinaryDetached(string(pub), string(priv), nil, data, string(Signature)) + if err != nil { + log.Error().Msg("Message decryption and sign failed") + } + return DecryptedMessage, err } - pub, err := base64.StdEncoding.DecodeString(MyContactPublicKey) - if err != nil { - log.Error().Msg("Message decryption and sign b64 failed") - } - DecryptedMessage, err = helper.DecryptVerifyBinaryDetached(string(pub), string(priv), nil, data, string(Signature)) - if err != nil { - log.Error().Msg("Message decryption and sign failed") - } - return DecryptedMessage, err -} - +*/ func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessage, error) { publicKeyRing, err := createPublicKeyRing(key) if err != nil { @@ -193,35 +197,43 @@ func createPublicKeyRing(publicKey string) (*crypto.KeyRing, error) { 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) if err != nil { log.Error().Msg("Message encryption and sign b64 failed") + return nil, err } priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey) if err != nil { log.Error().Msg("Message encryption and sign b64 failed") + return nil, err } ciphertext, signature, err := encryptAndSignMessage(string(pub), string(priv), crypto.NewPlainMessage(data)) if err != nil { 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) { priv, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey) if err != nil { log.Error().Msg("Message decryption and sign b64 failed") + return nil, err } pub, err := base64.StdEncoding.DecodeString(MyContactPublicKey) if err != nil { 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)) if err != nil { log.Error().Msg("Message decryption and sign failed") + return nil, err } return DecryptedMessage, err } diff --git a/asymcrypt_test.go b/asymcrypt_test.go index 9d09095..e210860 100644 --- a/asymcrypt_test.go +++ b/asymcrypt_test.go @@ -83,11 +83,11 @@ func TestAsymEncryptDecrypt(t *testing.T) { func TestAsymEncryptDecryptSigned(t *testing.T) { kp := NewKeyPair() 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 { 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 { log.Println(err2.Error()) } @@ -97,11 +97,11 @@ func TestAsymEncryptDecryptSigned(t *testing.T) { func TestAsymEncryptDecryptSigned2(t *testing.T) { kp := NewKeyPair() 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 { 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 { log.Println(err2.Error()) } @@ -112,13 +112,13 @@ func TestFlutterCompat(t *testing.T) { pub := base64.StdEncoding.EncodeToString([]byte(publicKeyECC)) priv := base64.StdEncoding.EncodeToString([]byte(privateKeyECC)) - encMess, sign, err := AsymEncryptAndSign(pub, priv, []byte("Hello")) + enc, err := AsymEncryptAndSign(pub, priv, []byte("Hello")) if err != nil { log.Println(err.Error()) } - println(base64.StdEncoding.EncodeToString(encMess)) - println(base64.StdEncoding.EncodeToString(sign)) + println(base64.StdEncoding.EncodeToString(enc.Data)) + println(base64.StdEncoding.EncodeToString(enc.Signature)) } @@ -163,16 +163,16 @@ func TestFlutterEncode(t *testing.T) { 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 { 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 { 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 { fmt.Println("Err") } diff --git a/client/peer.go b/client/peer.go index 3fb50d5..81b64c4 100644 --- a/client/peer.go +++ b/client/peer.go @@ -156,13 +156,14 @@ func (p *Peer) DeserializeUserMessage(data []byte) (*meowlib.UserMessage, error) } // 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) { - EncryptedMessage, Signature, err = meowlib.AsymEncryptAndSign(p.Contact.EncryptionPublicKey, p.MyIdentity.Private, Message) +func (p *Peer) AsymEncryptMessage(Message []byte) (*meowlib.EncryptedMessage, error) { + var enc *meowlib.EncryptedMessage + enc, err := meowlib.AsymEncryptAndSign(p.Contact.EncryptionPublicKey, p.MyIdentity.Private, Message) if err != nil { 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 @@ -203,13 +204,12 @@ func (p *Peer) ProcessOutboundUserMessage(usermessage *meowlib.UserMessage) (*me return nil, err } // Encrypting it - EncMsg, EncMsgSignature, FriendServers, err := p.AsymEncryptMessage(serializedMessage) + enc, err := p.AsymEncryptMessage(serializedMessage) if err != nil { return nil, err } - fmt.Println(len(FriendServers)) // Packing it - packedMsg := p.PackUserMessage(EncMsg, EncMsgSignature) + packedMsg := p.PackUserMessage(enc.Data, enc.Signature) return packedMsg, nil } diff --git a/client/server.go b/client/server.go index 45229a2..08207be 100644 --- a/client/server.go +++ b/client/server.go @@ -44,13 +44,14 @@ func (sl *InternalServerList) AddUrls(urls []string) { } // AsymEncryptMessage prepares a message to send to a specific internal server -func (ints *InternalServer) AsymEncryptMessage(Message []byte) (EncryptedMessage []byte, Signature []byte, err error) { - EncryptedMessage, Signature, err = meowlib.AsymEncryptAndSign(ints.ServerData.PublicKey, ints.Me.Private, Message) +func (ints *InternalServer) AsymEncryptMessage(Message []byte) (*meowlib.EncryptedMessage, error) { + var enc *meowlib.EncryptedMessage + enc, err := meowlib.AsymEncryptAndSign(ints.ServerData.PublicKey, ints.Me.Private, Message) if err != nil { 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 @@ -123,12 +124,12 @@ func (srv *InternalServer) ProcessOutboundMessage(toServerMessage *meowlib.ToSer return nil, err } // Encrypting it - encToServerMessage, encToServerMessageSignature, err := srv.AsymEncryptMessage(byteToServerMessage) + encToServer, err := srv.AsymEncryptMessage(byteToServerMessage) if err != nil { return nil, err } // Packing it - protoPackedServerMsg, err := srv.PackServerMessage(encToServerMessage, encToServerMessageSignature) + protoPackedServerMsg, err := srv.PackServerMessage(encToServer.Data, encToServer.Signature) if err != nil { return nil, err } diff --git a/crypt.go b/crypt.go new file mode 100644 index 0000000..a8bbdd5 --- /dev/null +++ b/crypt.go @@ -0,0 +1,6 @@ +package meowlib + +type EncryptedMessage struct { + Data []byte + Signature []byte +} diff --git a/endtoend_test.go b/endtoend_test.go index 3a64d47..3e95fa8 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -91,15 +91,15 @@ func TestEndToEnd(t *testing.T) { fmt.Println(err.Error()) } // Encrypting it - EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage(serializedMessage) + enc, err := MyFirstFriend.AsymEncryptMessage(serializedMessage) if err != nil { 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) // Creating Server message for transporting the user message @@ -108,12 +108,12 @@ func TestEndToEnd(t *testing.T) { fmt.Println(err.Error()) } // Encrypting it - encToServerMessage, encToServerMessageSignature, err := intS1.AsymEncryptMessage(toServerMessage) + encToServer, err := intS1.AsymEncryptMessage(toServerMessage) if err != nil { fmt.Println(err.Error()) } // Packing it - protoPackedServerMsg, err := intS1.PackServerMessage(encToServerMessage, encToServerMessageSignature) + protoPackedServerMsg, err := intS1.PackServerMessage(encToServer.Data, encToServer.Signature) if err != nil { fmt.Println(err.Error()) } @@ -156,11 +156,11 @@ func TestEndToEnd(t *testing.T) { if err != nil { 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 { 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 { fmt.Println(err.Error()) } @@ -178,7 +178,7 @@ func TestEndToEnd(t *testing.T) { // user unpack // user decrypt - decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), EncMsgSignature) + decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(enc.Data), enc.Signature) if err2 != nil { fmt.Println(err2.Error()) } diff --git a/server/identity.go b/server/identity.go index 182309e..2d44960 100644 --- a/server/identity.go +++ b/server/identity.go @@ -3,7 +3,8 @@ package server import ( "encoding/json" "fmt" - "io/ioutil" + + "os" "forge.redroom.link/yves/meowlib" "github.com/ProtonMail/gopenpgp/v2/helper" @@ -33,7 +34,7 @@ func CreateIdentity(ServerName string, ServerDesc string) *Identity { func LoadIdentity(File string) (*Identity, error) { var id Identity - indata, err := ioutil.ReadFile(File) + indata, err := os.ReadFile(File) if err != nil { return nil, err } @@ -51,7 +52,7 @@ func (id *Identity) Save(file string) error { if err != nil { return err } - err = ioutil.WriteFile(file, []byte(armor), 0644) + err = os.WriteFile(file, []byte(armor), 0644) return err } @@ -127,13 +128,14 @@ func (id *Identity) SerializeFromServerMessage(msg *meowlib.FromServerMessage) ( } // 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) +func (id *Identity) AsymEncryptMessage(peerPublicKey string, Message []byte) (*meowlib.EncryptedMessage, error) { + var enc *meowlib.EncryptedMessage + enc, err := meowlib.AsymEncryptAndSign(peerPublicKey, id.ServerKp.Private, Message) if err != nil { 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) { @@ -154,11 +156,11 @@ func (id *Identity) ProcessOutbound(peerPublicKey string, msg *meowlib.FromServe if err != nil { 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 { return nil, err } - resp, err := id.PackForSending(srv_resp, srv_respsig) + resp, err := id.PackForSending(srv_resp.Data, srv_resp.Signature) if err != nil { return nil, err }