store userprivatekey in clients as crypto.key instead of armor

This commit is contained in:
Samuel Lorch 2025-03-19 14:40:16 +01:00
parent c7a0de783d
commit 118dd9624b
4 changed files with 21 additions and 67 deletions

View file

@ -8,14 +8,7 @@ import (
// EncryptMessage encrypts a message using the users public key and then signes the message using the users private key
func (c *Client) EncryptMessage(message string) (string, error) {
key, err := c.getPrivateKey(c.userPrivateKey, c.userPassword)
if err != nil {
return "", fmt.Errorf("Get Private Key: %w", err)
}
defer key.ClearPrivateParams()
encHandle, err := c.pgp.Encryption().SigningKey(key).Recipient(key).New()
encHandle, err := c.pgp.Encryption().SigningKey(c.userPrivateKey).Recipient(c.userPrivateKey).New()
if err != nil {
return "", fmt.Errorf("New Encryptor: %w", err)
}
@ -36,19 +29,12 @@ func (c *Client) EncryptMessage(message string) (string, error) {
// EncryptMessageWithPublicKey encrypts a message using the provided public key and then signes the message using the users private key
func (c *Client) EncryptMessageWithPublicKey(publickey, message string) (string, error) {
key, err := c.getPrivateKey(c.userPrivateKey, c.userPassword)
if err != nil {
return "", fmt.Errorf("Get Private Key: %w", err)
}
defer key.ClearPrivateParams()
publicKey, err := crypto.NewKeyFromArmored(publickey)
if err != nil {
return "", fmt.Errorf("Get Public Key: %w", err)
}
encHandle, err := c.pgp.Encryption().SigningKey(key).Recipient(publicKey).New()
encHandle, err := c.pgp.Encryption().SigningKey(c.userPrivateKey).Recipient(publicKey).New()
if err != nil {
return "", fmt.Errorf("New Encryptor: %w", err)
}
@ -68,29 +54,26 @@ func (c *Client) EncryptMessageWithPublicKey(publickey, message string) (string,
}
// DecryptMessage decrypts a message using the users Private Key
func (c *Client) DecryptMessage(message string) (string, error) {
message, _, err := c.DecryptMessageWithPrivateKeyAndReturnSessionKey(c.userPrivateKey, c.userPassword, message)
func (c *Client) DecryptMessage(armoredCiphertext string) (string, error) {
message, _, err := c.DecryptMessageWithPrivateKeyAndReturnSessionKey(c.userPrivateKey, armoredCiphertext)
return message, err
}
// DecryptMessageWithPrivateKey Decrypts a Message using the Provided Private Key
// Returns the Session key so that it can be saved in a cache
func (c *Client) DecryptMessageWithPrivateKeyAndReturnSessionKey(privateKey string, passphrase []byte, ciphertextArmored string) (string, *crypto.SessionKey, error) {
key, err := c.getPrivateKey(privateKey, passphrase)
if err != nil {
return "", nil, fmt.Errorf("Get Private Key: %w", err)
}
func (c *Client) DecryptMessageWithPrivateKeyAndReturnSessionKey(privateKey *crypto.Key, armoredCiphertext string) (string, *crypto.SessionKey, error) {
defer key.ClearPrivateParams()
decHandle, err := c.pgp.Decryption().DecryptionKey(key).RetrieveSessionKey().New()
decHandle, err := c.pgp.Decryption().
DecryptionKey(privateKey).
RetrieveSessionKey().
New()
if err != nil {
return "", nil, fmt.Errorf("New Decryptor: %w", err)
}
defer decHandle.ClearPrivateParams()
res, err := decHandle.Decrypt([]byte(ciphertextArmored), crypto.Armor)
res, err := decHandle.Decrypt([]byte(armoredCiphertext), crypto.Armor)
if err != nil {
return "", nil, fmt.Errorf("Decrypt: %w", err)
}
@ -98,11 +81,7 @@ func (c *Client) DecryptMessageWithPrivateKeyAndReturnSessionKey(privateKey stri
return res.String(), res.SessionKey(), nil
}
func (c *Client) getPrivateKey(privateKey string, passphrase []byte) (*crypto.Key, error) {
if c.userPrivateKey == "" {
return nil, fmt.Errorf("Client has no Private Key")
}
func GetPrivateKeyFromArmor(privateKey string, passphrase []byte) (*crypto.Key, error) {
key, err := crypto.NewKeyFromArmored(privateKey)
if err != nil {
return nil, fmt.Errorf("Key From Armored: %w", err)