mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-15 03:11:02 +01:00
2172d6f1b9
* refactor: extract common message encryption logic * refactor: move letsencrypt logic to common * refactor: rename common package to encryption * test: add encryption tests
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
package encryption
|
|
|
|
import (
|
|
pb "github.com/golang/protobuf/proto" //nolint
|
|
log "github.com/sirupsen/logrus"
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
)
|
|
|
|
// EncryptMessage encrypts a body of the given protobuf Message
|
|
func EncryptMessage(remotePubKey wgtypes.Key, ourPrivateKey wgtypes.Key, message pb.Message) ([]byte, error) {
|
|
byteResp, err := pb.Marshal(message)
|
|
if err != nil {
|
|
log.Errorf("failed marshalling message %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
encryptedBytes, err := Encrypt(byteResp, remotePubKey, ourPrivateKey)
|
|
if err != nil {
|
|
log.Errorf("failed encrypting SyncResponse %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return encryptedBytes, nil
|
|
}
|
|
|
|
// DecryptMessage decrypts an encrypted message into given protobuf Message
|
|
func DecryptMessage(remotePubKey wgtypes.Key, ourPrivateKey wgtypes.Key, encryptedMessage []byte, message pb.Message) error {
|
|
decrypted, err := Decrypt(encryptedMessage, remotePubKey, ourPrivateKey)
|
|
if err != nil {
|
|
log.Warnf("error while decrypting Sync request message from peer %s", remotePubKey.String())
|
|
return err
|
|
}
|
|
|
|
err = pb.Unmarshal(decrypted, message)
|
|
if err != nil {
|
|
log.Warnf("error while umarshalling Sync request message from peer %s", remotePubKey.String())
|
|
return err
|
|
}
|
|
return nil
|
|
}
|