2018-06-01 15:53:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-01 16:19:35 +00:00
|
|
|
"bytes"
|
2018-06-01 15:53:24 +00:00
|
|
|
"fmt"
|
2018-06-01 16:19:35 +00:00
|
|
|
"io"
|
2018-06-01 15:53:24 +00:00
|
|
|
|
2023-09-30 10:31:07 +00:00
|
|
|
"github.com/ProtonMail/go-crypto/openpgp"
|
|
|
|
"github.com/ProtonMail/go-crypto/openpgp/armor"
|
2018-11-02 20:40:47 +00:00
|
|
|
|
2023-09-30 10:31:07 +00:00
|
|
|
openssl "github.com/Luzifer/go-openssl/v4"
|
2018-06-01 15:53:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type decryptMethod func(body []byte, passphrase string) ([]byte, error)
|
|
|
|
|
|
|
|
func decryptMethodFromName(name string) (decryptMethod, error) {
|
|
|
|
switch name {
|
2018-06-01 16:19:35 +00:00
|
|
|
case "gpg-symmetric":
|
|
|
|
return decryptGPGSymmetric, nil
|
|
|
|
|
2018-06-01 15:53:24 +00:00
|
|
|
case "openssl-md5":
|
2023-09-30 10:31:07 +00:00
|
|
|
return decryptOpenSSL(openssl.BytesToKeyMD5), nil
|
2018-11-02 20:40:47 +00:00
|
|
|
|
|
|
|
case "openssl-sha256":
|
2023-09-30 10:31:07 +00:00
|
|
|
return decryptOpenSSL(openssl.BytesToKeySHA256), nil
|
2018-06-01 15:53:24 +00:00
|
|
|
|
|
|
|
default:
|
2023-09-30 10:31:07 +00:00
|
|
|
return nil, fmt.Errorf("decrypt method %q not found", name)
|
2018-06-01 15:53:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:19:35 +00:00
|
|
|
func decryptGPGSymmetric(body []byte, passphrase string) ([]byte, error) {
|
|
|
|
var msgReader io.Reader
|
|
|
|
|
|
|
|
block, err := armor.Decode(bytes.NewReader(body))
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
msgReader = block.Body
|
|
|
|
case io.EOF:
|
|
|
|
msgReader = bytes.NewReader(body)
|
|
|
|
default:
|
2023-09-30 10:31:07 +00:00
|
|
|
return nil, fmt.Errorf("reading armor: %w", err)
|
2018-06-01 16:19:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 16:28:55 +00:00
|
|
|
var passwordRetry bool
|
2018-06-01 16:19:35 +00:00
|
|
|
md, err := openpgp.ReadMessage(msgReader, nil, func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
|
2018-06-01 16:28:55 +00:00
|
|
|
if passwordRetry {
|
2023-09-30 10:31:07 +00:00
|
|
|
return nil, fmt.Errorf("wrong passphrase supplied")
|
2018-06-01 16:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
passwordRetry = true
|
2018-06-01 16:19:35 +00:00
|
|
|
return []byte(passphrase), nil
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
2023-09-30 10:31:07 +00:00
|
|
|
return nil, fmt.Errorf("decrypting message: %w", err)
|
2018-06-01 16:19:35 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 10:31:07 +00:00
|
|
|
data, err := io.ReadAll(md.UnverifiedBody)
|
2023-11-04 12:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading GPG body: %w", err)
|
|
|
|
}
|
|
|
|
return data, nil
|
2018-06-01 16:19:35 +00:00
|
|
|
}
|
|
|
|
|
2023-09-30 10:31:07 +00:00
|
|
|
func decryptOpenSSL(kdf openssl.CredsGenerator) decryptMethod {
|
2018-11-02 20:40:47 +00:00
|
|
|
return func(body []byte, passphrase string) ([]byte, error) {
|
2023-09-30 10:31:07 +00:00
|
|
|
data, err := openssl.New().DecryptBytes(cfg.Password, body, kdf)
|
2023-11-04 12:12:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decrypting data: %w", err)
|
|
|
|
}
|
|
|
|
return data, nil
|
2018-11-02 20:40:47 +00:00
|
|
|
}
|
2018-06-01 15:53:24 +00:00
|
|
|
}
|