-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper functions to inspect and create key packets
- Loading branch information
Showing
7 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package constants | ||
|
||
const Version = "2.7.4" | ||
const Version = "2.7.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", "[email protected]", "x25519", 0) | ||
if err != nil { | ||
t.Fatal("Expected no error when generating key, got:", err) | ||
} | ||
|
||
keyB, err := crypto.GenerateKey("B", "[email protected]", "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()) | ||
} |