Skip to content

Commit a9eefe7

Browse files
committed
openpgp/packet: reject too-short decrypted session key
EncryptedKey.Decrypt reads the decrypted session key as b[0] (cipher), b[1:len(b)-2] (key) and b[len(b)-2:] (checksum) without a length check. A crafted public-key-encrypted message whose RSA, ElGamal or ECDH ciphertext decrypts to fewer than three octets makes these index and slice operations panic (index out of range / slice bounds out of range). This is reachable from ReadMessage when decrypting an attacker-supplied message, a denial of service for programs that decrypt untrusted OpenPGP messages. Reject len(b) < 3 with a StructuralError before indexing.
1 parent 5f2de1a commit a9eefe7

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

openpgp/packet/encrypted_key.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ func (e *EncryptedKey) Decrypt(priv *PrivateKey, config *Config) error {
9494
return err
9595
}
9696

97+
// The decrypted session key is one cipher-algorithm octet, the key
98+
// material, and a two-octet checksum. A decryption that yields fewer than
99+
// three octets (e.g. from a crafted ElGamal/RSA/ECDH ciphertext) would
100+
// otherwise make the indexing below panic with slice/index out of range.
101+
if len(b) < 3 {
102+
return errors.StructuralError("session key is too short")
103+
}
104+
97105
e.CipherFunc = CipherFunction(b[0])
98106
e.Key = b[1 : len(b)-2]
99107
expectedChecksum := uint16(b[len(b)-2])<<8 | uint16(b[len(b)-1])

0 commit comments

Comments
 (0)