2022-01-15 22:19:29 +01:00
|
|
|
package meowlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
|
|
|
"github.com/ProtonMail/gopenpgp/v2/helper"
|
2022-09-18 21:17:28 +02:00
|
|
|
"github.com/pkg/errors"
|
2022-01-15 22:19:29 +01:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KeyPair struct {
|
|
|
|
Public string `json:"public,omitempty"`
|
|
|
|
Private string `json:"private,omitempty"`
|
|
|
|
Generated time.Time `json:"generated,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type KeysArray []KeyPair
|
|
|
|
|
|
|
|
func NewKeyPair() KeyPair {
|
|
|
|
var kp KeyPair
|
2022-03-01 10:37:11 +01:00
|
|
|
keys, err := crypto.GenerateKey("name", "mail", "x25519", 0)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Key generation failed")
|
|
|
|
}
|
|
|
|
kp.Generated = time.Now()
|
|
|
|
pub, err := keys.GetArmoredPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Public key extraction failed")
|
|
|
|
}
|
|
|
|
kp.Public = base64.StdEncoding.EncodeToString([]byte(pub))
|
|
|
|
priv, err := keys.Armor()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Private key extraction failed")
|
|
|
|
}
|
|
|
|
kp.Private = base64.StdEncoding.EncodeToString([]byte(priv))
|
|
|
|
return kp
|
|
|
|
}
|
|
|
|
|
2022-09-06 17:07:35 +02:00
|
|
|
func (Kp *KeyPair) GetCryptoKeyObject() *crypto.Key {
|
|
|
|
priv, err := base64.StdEncoding.DecodeString(Kp.Private)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Create key from armoured b64 failed")
|
|
|
|
}
|
|
|
|
key, err := crypto.NewKeyFromArmored(string(priv))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Create key from armoured failed")
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func AsymEncrypt(publicKey string, data []byte) ([]byte, error) {
|
|
|
|
pub, err := base64.StdEncoding.DecodeString(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message encryption b64 failed")
|
|
|
|
}
|
|
|
|
ciphertext, err := encryptMessage(string(pub), crypto.NewPlainMessage(data))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message encryption failed")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ciphertext.GetBinary(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func AsymDecrypt(PrivateKey string, data []byte) ([]byte, error) {
|
|
|
|
priv, err := base64.StdEncoding.DecodeString(PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message decryption b64 failed")
|
|
|
|
}
|
|
|
|
decrypted, err := decryptMessage(string(priv), nil, crypto.NewPGPMessage(data))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message decryption failed")
|
|
|
|
}
|
|
|
|
return decrypted.GetBinary(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func AsymEncryptArmored(PublicKey string, data []byte) ([]byte, error) {
|
2022-09-06 17:07:35 +02:00
|
|
|
pub, err := base64.StdEncoding.DecodeString(PublicKey)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message encryption b64 failed")
|
|
|
|
}
|
|
|
|
armor, err := helper.EncryptBinaryMessageArmored(string(pub), data)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message encryption failed")
|
|
|
|
}
|
|
|
|
return []byte(armor), err
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func AsymDecryptArmored(PrivateKey string, data []byte) ([]byte, error) {
|
2022-09-06 17:07:35 +02:00
|
|
|
priv, err := base64.StdEncoding.DecodeString(PrivateKey)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message decryption b64 failed")
|
|
|
|
}
|
2022-09-02 12:07:21 +02:00
|
|
|
decrypted, err := helper.DecryptBinaryMessageArmored(string(priv), nil, string(data))
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message decryption failed")
|
|
|
|
}
|
|
|
|
return []byte(decrypted), err
|
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func AsymEncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, data []byte) ([]byte, []byte, error) {
|
2022-09-06 17:07:35 +02:00
|
|
|
pub, err := base64.StdEncoding.DecodeString(PublicEncryptionKey)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message encryption and sign b64 failed")
|
|
|
|
}
|
2022-09-06 17:07:35 +02:00
|
|
|
priv, err := base64.StdEncoding.DecodeString(PrivateSignatureKey)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message encryption and sign b64 failed")
|
|
|
|
}
|
2022-09-02 12:07:21 +02:00
|
|
|
encrypted, signature, err := helper.EncryptSignBinaryDetached(string(pub), string(priv), nil, data)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message encryption and sign failed")
|
|
|
|
}
|
2022-09-02 12:07:21 +02:00
|
|
|
return []byte(encrypted), []byte(signature), err
|
2022-01-15 22:19:29 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 21:17:28 +02:00
|
|
|
func AsymDecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) {
|
2022-09-02 12:07:21 +02:00
|
|
|
pub, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message decryption and sign b64 failed")
|
|
|
|
}
|
2022-09-06 17:07:35 +02:00
|
|
|
priv, err := base64.StdEncoding.DecodeString(MyContactPublicKey)
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message decryption and sign b64 failed")
|
|
|
|
}
|
2022-09-06 17:07:35 +02:00
|
|
|
DecryptedMessage, err = helper.DecryptVerifyBinaryDetached(string(pub), string(priv), nil, data, string(Signature))
|
2022-01-15 22:19:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Msg("Message decryption and sign failed")
|
|
|
|
}
|
2022-09-02 12:07:21 +02:00
|
|
|
return DecryptedMessage, err
|
2022-01-15 22:19:29 +01:00
|
|
|
}
|
2022-09-18 21:17:28 +02:00
|
|
|
|
|
|
|
func encryptMessage(key string, message *crypto.PlainMessage) (*crypto.PGPMessage, error) {
|
|
|
|
publicKeyRing, err := createPublicKeyRing(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ciphertext, err := publicKeyRing.Encrypt(message, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to encrypt message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ciphertext, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptMessage(privateKey string, passphrase []byte, ciphertext *crypto.PGPMessage) (*crypto.PlainMessage, error) {
|
|
|
|
privateKeyObj, err := crypto.NewKeyFromArmored(privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to parse the private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKeyUnlocked, err := privateKeyObj.Unlock(passphrase)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to unlock key")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer privateKeyUnlocked.ClearPrivateParams()
|
|
|
|
|
|
|
|
privateKeyRing, err := crypto.NewKeyRing(privateKeyUnlocked)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to create the private key ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := privateKeyRing.Decrypt(ciphertext, nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to decrypt message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createPublicKeyRing(publicKey string) (*crypto.KeyRing, error) {
|
|
|
|
publicKeyObj, err := crypto.NewKeyFromArmored(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to parse public key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if publicKeyObj.IsPrivate() {
|
|
|
|
publicKeyObj, err = publicKeyObj.ToPublic()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to extract public key from private key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKeyRing, err := crypto.NewKeyRing(publicKeyObj)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "gopenpgp: unable to create new keyring")
|
|
|
|
}
|
|
|
|
|
|
|
|
return publicKeyRing, nil
|
|
|
|
}
|