Prepare release 2.7.5 with packet API (#269)

Adds the following API functions:
- API to get signature key IDs for mobile:
	func (msg *PGPMessage) GetHexSignatureKeyIDsJson() []byte

- API to get encryption key IDs for mobile:
	func (msg *PGPMessage) GetHexEncryptionKeyIDsJson() []byte

- API to get the number of key packets in a PGP message:
	func (msg *PGPSplitMessage) GetNumberOfKeyPackets() (int, error)

- API in package `helper` to encrypt a PGP message to an additional key:
	func EncryptPGPMessageToAdditionalKey(messageToModify *crypto.PGPSplitMessage, keyRing *crypto.KeyRing, additionalKey *crypto.KeyRing) error
This commit is contained in:
Lukas Burkhalter 2024-02-07 08:09:26 +01:00 committed by GitHub
parent 02a4599829
commit c6a3058e2e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 157 additions and 2 deletions

22
helper/message.go Normal file
View file

@ -0,0 +1,22 @@
package helper
import "github.com/ProtonMail/gopenpgp/v2/crypto"
// EncryptPGPMessageToAdditionalKey decrypts the session key of the input PGPSplitMessage with a private key in keyRing
// and encrypts it towards the additionalKeys by adding the additional key packets to the input PGPSplitMessage.
// If successful, new key packets are added to message.
// * messageToModify : The encrypted pgp message that should be modified
// * keyRing : The private keys to decrypt the session key in the messageToModify.
// * additionalKey : The public keys the message should be additionally encrypted to.
func EncryptPGPMessageToAdditionalKey(messageToModify *crypto.PGPSplitMessage, keyRing *crypto.KeyRing, additionalKey *crypto.KeyRing) error {
sessionKey, err := keyRing.DecryptSessionKey(messageToModify.KeyPacket)
if err != nil {
return err
}
additionalKeyPacket, err := additionalKey.EncryptSessionKey(sessionKey)
if err != nil {
return err
}
messageToModify.KeyPacket = append(messageToModify.KeyPacket, additionalKeyPacket...)
return nil
}

56
helper/message_test.go Normal file
View file

@ -0,0 +1,56 @@
package helper_test
import (
"testing"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/gopenpgp/v2/helper"
"github.com/stretchr/testify/assert"
)
func TestEncryptPGPMessageToAdditionalKey(t *testing.T) {
keyA, err := crypto.GenerateKey("A", "a@a.a", "x25519", 0)
if err != nil {
t.Fatal("Expected no error when generating key, got:", err)
}
keyB, err := crypto.GenerateKey("B", "b@b.b", "x25519", 0)
if err != nil {
t.Fatal("Expected no error when generating key, got:", err)
}
keyRingA, err := crypto.NewKeyRing(keyA)
if err != nil {
t.Fatal("Expected no error when creating keyring, got:", err)
}
keyRingB, err := crypto.NewKeyRing(keyB)
if err != nil {
t.Fatal("Expected no error when creating keyring, got:", err)
}
message := crypto.NewPlainMessageFromString("plain text")
// Encrypt towards A
ciphertext, err := keyRingA.Encrypt(message, nil)
if err != nil {
t.Fatal("Expected no error when encrypting, got:", err)
}
ciphertextSplit, err := ciphertext.SplitMessage()
if err != nil {
t.Fatal("Expected no error when splitting message, got:", err)
}
// Also encrypt the message towards B
if err := helper.EncryptPGPMessageToAdditionalKey(ciphertextSplit, keyRingA, keyRingB); err != nil {
t.Fatal("Expected no error when modifying the message, got:", err)
}
// Test decrypt with B
decrypted, err := keyRingB.Decrypt(
ciphertextSplit.GetPGPMessage(),
nil,
0,
)
if err != nil {
t.Fatal("Expected no error when decrypting, got:", err)
}
assert.Exactly(t, message.GetString(), decrypted.GetString())
}