keystore.Decode() fails to check the error returned when reading the trailing SHA-1 integrity digest of a JKS keystore. If an attacker removes the final 20 bytes from a .jks file, the digest read returns an empty slice and the byte-comparison loop never executes, so Decode() returns the parsed keystore with err == nil. This defeats both tamper detection and — for certificate-only truststores — the password check itself, allowing an attacker with write access to a keystore file (but without knowledge of the keystore password) to silently inject or replace trusted CA certificates.
Affected versions: github.com/pavel-v-chernykh/keystore-go ≤ v2.1.0+incompatible (the final release on this module path). v3 and v4 releases are not impacted.
In decoder.go, function Decode, lines 264-272:
computedDigest := ksd.md.Sum(nil)
actualDigest, err := ksd.readBytes(uint32(ksd.md.Size()))
for i := 0; i < len(actualDigest); i++ {
if actualDigest[i] != computedDigest[i] {
return nil, ErrInvalidDigest
}
}
return keyStore, nil
Two defects combine here:
err returned by ksd.readBytes(20) on line 265 is assigned but never inspected.len(actualDigest) rather than len(computedDigest).readBytes (decoder.go lines 75-94) reads the requested bytes via io.ReadFull; on EOF/short read it returns (nil, ErrIo):
_, err := io.ReadFull(ksd.r, ksd.b[:blockSize])
if err != nil {
return result, ErrIo
}
When the trailing 20-byte digest is absent from the input stream, actualDigest is therefore nil, len(actualDigest) == 0, the for loop body never executes, and Decode() falls through to return keyStore, nil.
The JKS format's only integrity guarantee is this trailing SHA1(passwordBytes || "Mighty Aphrodite" || body) digest. For truststores containing only TrustedCertificateEntry values it is also the only place the password is...
3.0.0Exploitability
AV:LAC:LPR:LUI:NScope
S:UImpact
C:HI:HA:N7.1/CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:N