Commit d271576a authored by Joe Shaw's avatar Joe Shaw Committed by Adam Langley

encoding/pem: refuse extra data on ending line

Previously the code didn't check for extra data after the final five
dashes of the ending line of a PEM block.

Fixes #19147
Fixes #7042

Change-Id: Idaab2390914a2bed8c2c12b14dfb6d68233fdfec
Reviewed-on: https://go-review.googlesource.com/37147Reviewed-by: default avatarAdam Langley <agl@golang.org>
parent b2a2a605
...@@ -135,20 +135,26 @@ func Decode(data []byte) (p *Block, rest []byte) { ...@@ -135,20 +135,26 @@ func Decode(data []byte) (p *Block, rest []byte) {
return decodeError(data, rest) return decodeError(data, rest)
} }
// After the "-----" of the ending line should be the same type and a // After the "-----" of the ending line, there should be the same type
// final five dashes. // and then a final five dashes.
endTrailer := rest[endTrailerIndex:] endTrailer := rest[endTrailerIndex:]
endTrailerLen := len(typeLine) + len(pemEndOfLine) endTrailerLen := len(typeLine) + len(pemEndOfLine)
if len(endTrailer) < endTrailerLen { if len(endTrailer) < endTrailerLen {
return decodeError(data, rest) return decodeError(data, rest)
} }
restOfEndLine := endTrailer[endTrailerLen:]
endTrailer = endTrailer[:endTrailerLen] endTrailer = endTrailer[:endTrailerLen]
if !bytes.HasPrefix(endTrailer, typeLine) || if !bytes.HasPrefix(endTrailer, typeLine) ||
!bytes.HasSuffix(endTrailer, pemEndOfLine) { !bytes.HasSuffix(endTrailer, pemEndOfLine) {
return decodeError(data, rest) return decodeError(data, rest)
} }
// The line must end with only whitespace.
if s, _ := getLine(restOfEndLine); len(s) != 0 {
return decodeError(data, rest)
}
base64Data := removeWhitespace(rest[:endIndex]) base64Data := removeWhitespace(rest[:endIndex])
p.Bytes = make([]byte, base64.StdEncoding.DecodedLen(len(base64Data))) p.Bytes = make([]byte, base64.StdEncoding.DecodedLen(len(base64Data)))
n, err := base64.StdEncoding.Decode(p.Bytes, base64Data) n, err := base64.StdEncoding.Decode(p.Bytes, base64Data)
......
...@@ -83,6 +83,16 @@ const pemTooFewEndingDashes = ` ...@@ -83,6 +83,16 @@ const pemTooFewEndingDashes = `
dGVzdA== dGVzdA==
-----END FOO----` -----END FOO----`
const pemTooManyEndingDashes = `
-----BEGIN FOO-----
dGVzdA==
-----END FOO------`
const pemTrailingNonWhitespace = `
-----BEGIN FOO-----
dGVzdA==
-----END FOO----- .`
const pemWrongEndingType = ` const pemWrongEndingType = `
-----BEGIN FOO----- -----BEGIN FOO-----
dGVzdA== dGVzdA==
...@@ -101,6 +111,14 @@ var badPEMTests = []struct { ...@@ -101,6 +111,14 @@ var badPEMTests = []struct {
"too few trailing dashes", "too few trailing dashes",
pemTooFewEndingDashes, pemTooFewEndingDashes,
}, },
{
"too many trailing dashes",
pemTooManyEndingDashes,
},
{
"trailing non-whitespace",
pemTrailingNonWhitespace,
},
{ {
"incorrect ending type", "incorrect ending type",
pemWrongEndingType, pemWrongEndingType,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment