Skip to content

Commit 5521d83

Browse files
authored
Flag to allow signing key decryption (#251)
Adds a flag InsecureAllowDecryptionWithSigningKeys that allows to use keys for decryption that are marked as signing keys only.
1 parent 5e3e39d commit 5521d83

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

openpgp/packet/config.go

+12
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ type Config struct {
139139
// might be no other way than to tolerate the missing MDC. Setting this flag, allows this
140140
// mode of operation. It should be considered a measure of last resort.
141141
InsecureAllowUnauthenticatedMessages bool
142+
// InsecureAllowDecryptionWithSigningKeys allows decryption with keys marked as signing keys in the v2 API.
143+
// This setting is potentially insecure, but it is needed as some libraries
144+
// ignored key flags when selecting a key for encryption.
145+
// Not relevant for the v1 API, as all keys were allowed in decryption.
146+
InsecureAllowDecryptionWithSigningKeys bool
142147
// KnownNotations is a map of Notation Data names to bools, which controls
143148
// the notation names that are allowed to be present in critical Notation Data
144149
// signature subpackets.
@@ -291,6 +296,13 @@ func (c *Config) AllowUnauthenticatedMessages() bool {
291296
return c.InsecureAllowUnauthenticatedMessages
292297
}
293298

299+
func (c *Config) AllowDecryptionWithSigningKeys() bool {
300+
if c == nil {
301+
return false
302+
}
303+
return c.InsecureAllowDecryptionWithSigningKeys
304+
}
305+
294306
func (c *Config) KnownNotation(notationName string) bool {
295307
if c == nil {
296308
return false

openpgp/v2/keys.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ func (e *Entity) DecryptionKeys(id uint64, date time.Time, config *packet.Config
164164
for _, subkey := range e.Subkeys {
165165
subkeySelfSig, err := subkey.LatestValidBindingSignature(date, config)
166166
if err == nil &&
167-
isValidEncryptionKey(subkeySelfSig, subkey.PublicKey.PubKeyAlgo) &&
167+
(config.AllowDecryptionWithSigningKeys() || isValidEncryptionKey(subkeySelfSig, subkey.PublicKey.PubKeyAlgo)) &&
168168
(id == 0 || subkey.PublicKey.KeyId == id) {
169169
keys = append(keys, Key{subkey.Primary, primarySelfSignature, subkey.PublicKey, subkey.PrivateKey, subkeySelfSig})
170170
}
171171
}
172-
if isValidEncryptionKey(primarySelfSignature, e.PrimaryKey.PubKeyAlgo) {
172+
if config.AllowDecryptionWithSigningKeys() || isValidEncryptionKey(primarySelfSignature, e.PrimaryKey.PubKeyAlgo) {
173173
keys = append(keys, Key{e, primarySelfSignature, e.PrimaryKey, e.PrivateKey, primarySelfSignature})
174174
}
175175
return

openpgp/v2/read_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -1023,3 +1023,41 @@ func testMalformedMessage(t *testing.T, keyring EntityList, message string) {
10231023
return
10241024
}
10251025
}
1026+
1027+
func TestReadMessageWithSignOnly(t *testing.T) {
1028+
config := packet.Config{
1029+
InsecureAllowDecryptionWithSigningKeys: true,
1030+
}
1031+
key, err := ReadArmoredKeyRing(strings.NewReader(rsaSignOnly))
1032+
if err != nil {
1033+
t.Error(err)
1034+
return
1035+
}
1036+
// Success
1037+
msgReader, err := armor.Decode(strings.NewReader(armoredMessageRsaSignOnly))
1038+
if err != nil {
1039+
t.Error(err)
1040+
return
1041+
}
1042+
md, err := ReadMessage(msgReader.Body, key, nil, &config)
1043+
if err != nil {
1044+
t.Error(err)
1045+
return
1046+
}
1047+
_, err = io.ReadAll(md.UnverifiedBody)
1048+
if err != nil {
1049+
t.Error(err)
1050+
return
1051+
}
1052+
1053+
// Fail
1054+
msgReader, err = armor.Decode(strings.NewReader(armoredMessageRsaSignOnly))
1055+
if err != nil {
1056+
t.Error(err)
1057+
return
1058+
}
1059+
md, err = ReadMessage(msgReader.Body, key, nil, nil)
1060+
if err == nil {
1061+
t.Fatal("Should not decrypt")
1062+
}
1063+
}

openpgp/v2/read_write_test_data.go

+48
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,51 @@ NVniEke6hM3CNBXYPAMhQBMWhCulcoz+0lxi8L34rMN+Dsbma96psdUrn7uLaB91
740740
xqAY9Bwizt4FWgXuLm1a4+So4V9j1TRCXd12Uc2l2RNmgDE=
741741
=miES
742742
-----END PGP PRIVATE KEY BLOCK-----`
743+
744+
const rsaSignOnly = `-----BEGIN PGP PRIVATE KEY BLOCK-----
745+
746+
xcLYBF9Gl+MBCACc09O3gjyO0B1ledGxGFSUpPmhhJzkxKoY1WDX8VlASCHz
747+
bAA/BytgYBXHTe7N+N3yJ6uiN3DIQ2j5uGWk/h5jyIOsRuzQxJ40n8AdK/71
748+
SGDCG1X5l1h9vmVTJxkQ3pcOxqRg55EEuJWKN1v7B1hIPxhaM5hgH/7s+PNn
749+
lQddckQJqYkpm9Hy6EI7f9oHrOtWJWZoCHkWZVld7+9ZVPi34ex5ofYOuvNL
750+
AIKZCc7lAiUiDJYQ+hIJRoYwLYhjIshpYoHgNeG4snlupNO32BiwDbHFDjeu
751+
eoBLQ0rxZV7B664ceCmIl+VRht9G20hfGoTjAiop5tyrN1ZeL4EaI+aTABEB
752+
AAEAB/oCKTQnftvHwrkBVlyzSN6tfXylF2551Q3n4CZGg3efI/9PCa9wF58+
753+
WApqmgsUqcNbVnDfl2T58ow05FLMxnFFNnHJq8ltfnXl+gG6c7iy94p79SQE
754+
AGCOL7xNassXrDAQZhqWkCdiLK3b6r9F8Y3URb/AYbWH2BkFkS0oWQDav+Tw
755+
lABt5vG2L5QtnShdqi8CCitcHGEKHocPHp0yAQlp3oAMq09YubgrzESDJ7Pe
756+
l93cT35NlyimAZ6IYk/gumX0/6spqcw7205KfG6P84WlMp3WmE0IUWtiOp+7
757+
rjMjDki0WeVKtuLbHBhOwKvxcILWz+0vQf3uu6aXOKQ3JlsVBADHoXa6QjrT
758+
RmKD9ch65Pkd+EZiKhe+pqqIArVj4QsVBEnaggR59SD8uXhtpyBnvOp3xpof
759+
Vut3SKWl/jmH7vKansFbHOo8xLUyVctu7lCL2/v85FcRJxfPK00MBic+z/vf
760+
mWOAY1VBoi5I9qi6o8vVHA5BJ/xw2uV9VpxfiLG0vwQAyRxHmoZl/OxaZUsm
761+
J9eDYV9xyYumkTCYvHPk9X+ehS+XeYh24z1q9a/1jEnSR3A5XE67UCLaspiA
762+
+Px7nSU1+ftJ9bC2bnRR0Upop+3UkPeCBVp4tYAhsNnPXhSWC0gCgeGU7EmW
763+
DechFg29LId35LXKgmXls9u5yDy2w978Hy0D/jbKZaxNMMwlx/XCFCoBEcXS
764+
DBzg7GHNXdillJqy215j46lfVqOCB3IiffNKjHua2l6fQc0BoiWIZnElMnIa
765+
faEBBSpOVqKhktDFacHa5xChjqXZVyw68qc0I36xkCfcwvYCpNKKkXv90r8A
766+
tRI6gpBLeMJvkL3VkmKd6AZymxFxRGjNEkJvYiA8aW5mb0Bib2IuY29tPsLA
767+
jQQQAQgAIAUCX0aX4wYLCQcIAwIEFQgKAgQWAgEAAhkBAhsDAh4BACEJEAr9
768+
x5ZY6oZmFiEEm+B7p+lshgEOwGGZCv3HlljqhmaUWgf/efmGSpOKIGQ3Kh32
769+
HUqn/4ARvUmqMtZz4xUA9P3GAPY8XwJf00jSQlAo4//3aA1eEOJFHCr2qzCk
770+
/4gIoZEScTTZp4itfL/Fer3UX+bV/VeTNgZGi+MRylSDQxLRQNpRgu+FmRAi
771+
E6fr8D8GMvEcGb0jTRgWGj1EVtfOHfDg+EyPrtw+Z8u/bErUJ+Fnxz+KOGSN
772+
SBQVAOflUYFoQhUNgZiq1s8WFD55sfes3UdBwsmHquDtYGo9dvWLJXxTEF8q
773+
QCyKHYdk25ShIlNpRUqOH3CHqY/38z7QeV7INwtZaQvoES08RlD6ZMtczYLj
774+
BZou86lozq7ISvRg1RSIWZ0ZRA==
775+
=A9Ts
776+
-----END PGP PRIVATE KEY BLOCK-----
777+
`
778+
779+
const armoredMessageRsaSignOnly = `-----BEGIN PGP MESSAGE-----
780+
781+
wcBMAwr9x5ZY6oZmAQf+Lxghg4keIFpEq8a65gFkIfW+chHTDPlfI8xnx6U9
782+
HdsICX3Oye5V0ToCVKkEWDxfN1yCfXiYalSNo7ScRZKR7C+j02/pC+FfR6AJ
783+
2cvdFoGIrLaXdjXXc/oXbsCCZA4C1DhQqpdORo2qGF0Q6Sm8659B0CfOgYSL
784+
fBfKQ5VJngUT5JG8Uek3YuXBufPNhzdmXLHyB2Y2CwKkldi2vo4YNAukDhrR
785+
2TojxdNoouhnMm+gloCE1n8huY1vw5F78/uiHen0tmHQ0dxtfk8cc1burgl/
786+
zUdJ3Sg6Eu+OC2ae5II63iB5fG+lCwZtfuepWnePDv8RDKNHCVP/LoBNpGOZ
787+
U9I6AUkZWdcsueib9ghKDDy+HbUbf2kCJWUnuyeOCKqQifDb8bsLmdQY4Wb6
788+
EBeLgD8oZHVsH3NLjPakPw==
789+
=STqy
790+
-----END PGP MESSAGE-----`

0 commit comments

Comments
 (0)