GHSA-j282-pjg5-j643 / CVE-2026-76101: keystore-go: Keystore integrity and password verification bypass via stripped trailing digest in Decode()
- Source:
- GitHub Security Advisories
- Published:
- Fetched:
- Updated:
- Affects:
- github.com/pavel-v-chernykh/keystore-go
https://github.com/advisories/GHSA-j282-pjg5-j643 ↗
keystore-go: Keystore integrity and password verification bypass via stripped trailing digest in Decode() ### Summary `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. ### Details **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: ```go 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: 1. The `err` returned by `ksd.readBytes(20)` on line 265 is assigned but never inspected. 2. The comparison loop on line 266 is bounded by `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)`: ```go _, 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 verified. Consequently: - An attacker who can modify a `.jks` truststore on disk — but does **not** know the keystore password — can replace or add trusted CA certificates, drop the last 20 bytes of the file, and the consuming Go process will load the forged truststore as valid. - After the digest is stripped, `Decode()` accepts **any** password, so operators who believe the password gates integrity receive no error even when the wrong password is supplied. The reference Java `JavaKeyStore` implementation reads the digest with `DataInputStream.readFully` and throws on short read; this Go port diverges from that behaviour. ### PoC ``` go mod init poc go get github.com/pavel-v-chernykh/keystore-go@v2.1.0+incompatible go run . ``` ```go package main import ( "bytes" "fmt" "os" keystore "github.com/pavel-v-chernykh/keystore-go" ) func main() { originalCert := []byte("ORIGINAL-TRUSTED-CA-CERTIFICATE") evilCert := []byte("EVIL-ATTACKER-CA Affected packages: go:github.com/pavel-v-chernykh/keystore-go, go:github.com/pavel-v-chernykh/keystore-go/v3 Patched: github.com/pavel-v-chernykh/keystore-go/v3 3.0.0