Skip to content

Commit

Permalink
Flag to allow signing key decryption (#251)
Browse files Browse the repository at this point in the history
Adds a flag InsecureAllowDecryptionWithSigningKeys that allows to use keys for decryption 
that are marked as signing keys only.
  • Loading branch information
lubux authored Nov 25, 2024
1 parent 5e3e39d commit 5521d83
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
12 changes: 12 additions & 0 deletions openpgp/packet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ type Config struct {
// might be no other way than to tolerate the missing MDC. Setting this flag, allows this
// mode of operation. It should be considered a measure of last resort.
InsecureAllowUnauthenticatedMessages bool
// InsecureAllowDecryptionWithSigningKeys allows decryption with keys marked as signing keys in the v2 API.
// This setting is potentially insecure, but it is needed as some libraries
// ignored key flags when selecting a key for encryption.
// Not relevant for the v1 API, as all keys were allowed in decryption.
InsecureAllowDecryptionWithSigningKeys bool
// KnownNotations is a map of Notation Data names to bools, which controls
// the notation names that are allowed to be present in critical Notation Data
// signature subpackets.
Expand Down Expand Up @@ -291,6 +296,13 @@ func (c *Config) AllowUnauthenticatedMessages() bool {
return c.InsecureAllowUnauthenticatedMessages
}

func (c *Config) AllowDecryptionWithSigningKeys() bool {
if c == nil {
return false
}
return c.InsecureAllowDecryptionWithSigningKeys
}

func (c *Config) KnownNotation(notationName string) bool {
if c == nil {
return false
Expand Down
4 changes: 2 additions & 2 deletions openpgp/v2/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ func (e *Entity) DecryptionKeys(id uint64, date time.Time, config *packet.Config
for _, subkey := range e.Subkeys {
subkeySelfSig, err := subkey.LatestValidBindingSignature(date, config)
if err == nil &&
isValidEncryptionKey(subkeySelfSig, subkey.PublicKey.PubKeyAlgo) &&
(config.AllowDecryptionWithSigningKeys() || isValidEncryptionKey(subkeySelfSig, subkey.PublicKey.PubKeyAlgo)) &&
(id == 0 || subkey.PublicKey.KeyId == id) {
keys = append(keys, Key{subkey.Primary, primarySelfSignature, subkey.PublicKey, subkey.PrivateKey, subkeySelfSig})
}
}
if isValidEncryptionKey(primarySelfSignature, e.PrimaryKey.PubKeyAlgo) {
if config.AllowDecryptionWithSigningKeys() || isValidEncryptionKey(primarySelfSignature, e.PrimaryKey.PubKeyAlgo) {
keys = append(keys, Key{e, primarySelfSignature, e.PrimaryKey, e.PrivateKey, primarySelfSignature})
}
return
Expand Down
38 changes: 38 additions & 0 deletions openpgp/v2/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,3 +1023,41 @@ func testMalformedMessage(t *testing.T, keyring EntityList, message string) {
return
}
}

func TestReadMessageWithSignOnly(t *testing.T) {
config := packet.Config{
InsecureAllowDecryptionWithSigningKeys: true,
}
key, err := ReadArmoredKeyRing(strings.NewReader(rsaSignOnly))
if err != nil {
t.Error(err)
return
}
// Success
msgReader, err := armor.Decode(strings.NewReader(armoredMessageRsaSignOnly))
if err != nil {
t.Error(err)
return
}
md, err := ReadMessage(msgReader.Body, key, nil, &config)
if err != nil {
t.Error(err)
return
}
_, err = io.ReadAll(md.UnverifiedBody)
if err != nil {
t.Error(err)
return
}

// Fail
msgReader, err = armor.Decode(strings.NewReader(armoredMessageRsaSignOnly))
if err != nil {
t.Error(err)
return
}
md, err = ReadMessage(msgReader.Body, key, nil, nil)
if err == nil {
t.Fatal("Should not decrypt")
}
}
48 changes: 48 additions & 0 deletions openpgp/v2/read_write_test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,51 @@ NVniEke6hM3CNBXYPAMhQBMWhCulcoz+0lxi8L34rMN+Dsbma96psdUrn7uLaB91
xqAY9Bwizt4FWgXuLm1a4+So4V9j1TRCXd12Uc2l2RNmgDE=
=miES
-----END PGP PRIVATE KEY BLOCK-----`

const rsaSignOnly = `-----BEGIN PGP PRIVATE KEY BLOCK-----
xcLYBF9Gl+MBCACc09O3gjyO0B1ledGxGFSUpPmhhJzkxKoY1WDX8VlASCHz
bAA/BytgYBXHTe7N+N3yJ6uiN3DIQ2j5uGW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PakPw==
=STqy
-----END PGP MESSAGE-----`

0 comments on commit 5521d83

Please sign in to comment.