Commit d0801f70 authored by David Benjamin's avatar David Benjamin Committed by Brad Fitzpatrick

encoding/asn1: tags should be encoded in minimal form.

High tag number form may not be used for tag numbers that fit in low tag number
form.

Change-Id: I93edde0e1f86087047e0b3f2e55d6180b01e78bf
Reviewed-on: https://go-review.googlesource.com/18224Reviewed-by: default avatarAdam Langley <agl@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
parent 562e38c0
...@@ -461,6 +461,11 @@ func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset i ...@@ -461,6 +461,11 @@ func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset i
if err != nil { if err != nil {
return return
} }
// Tags should be encoded in minimal form.
if ret.tag < 0x1f {
err = SyntaxError{"non-minimal tag"}
return
}
} }
if offset >= len(bytes) { if offset >= len(bytes) {
err = SyntaxError{"truncated tag or length"} err = SyntaxError{"truncated tag or length"}
......
...@@ -364,7 +364,7 @@ var tagAndLengthData = []tagAndLengthTest{ ...@@ -364,7 +364,7 @@ var tagAndLengthData = []tagAndLengthTest{
{[]byte{0xa0, 0x01}, true, tagAndLength{2, 0, 1, true}}, {[]byte{0xa0, 0x01}, true, tagAndLength{2, 0, 1, true}},
{[]byte{0x02, 0x00}, true, tagAndLength{0, 2, 0, false}}, {[]byte{0x02, 0x00}, true, tagAndLength{0, 2, 0, false}},
{[]byte{0xfe, 0x00}, true, tagAndLength{3, 30, 0, true}}, {[]byte{0xfe, 0x00}, true, tagAndLength{3, 30, 0, true}},
{[]byte{0x1f, 0x01, 0x00}, true, tagAndLength{0, 1, 0, false}}, {[]byte{0x1f, 0x1f, 0x00}, true, tagAndLength{0, 31, 0, false}},
{[]byte{0x1f, 0x81, 0x00, 0x00}, true, tagAndLength{0, 128, 0, false}}, {[]byte{0x1f, 0x81, 0x00, 0x00}, true, tagAndLength{0, 128, 0, false}},
{[]byte{0x1f, 0x81, 0x80, 0x01, 0x00}, true, tagAndLength{0, 0x4001, 0, false}}, {[]byte{0x1f, 0x81, 0x80, 0x01, 0x00}, true, tagAndLength{0, 0x4001, 0, false}},
{[]byte{0x00, 0x81, 0x80}, true, tagAndLength{0, 0, 128, false}}, {[]byte{0x00, 0x81, 0x80}, true, tagAndLength{0, 0, 128, false}},
...@@ -382,6 +382,8 @@ var tagAndLengthData = []tagAndLengthTest{ ...@@ -382,6 +382,8 @@ var tagAndLengthData = []tagAndLengthTest{
{[]byte{0xa0, 0x81, 0x7f}, false, tagAndLength{}}, {[]byte{0xa0, 0x81, 0x7f}, false, tagAndLength{}},
// Tag numbers which would overflow int32 are rejected. (The value below is 2^31.) // Tag numbers which would overflow int32 are rejected. (The value below is 2^31.)
{[]byte{0x1f, 0x88, 0x80, 0x80, 0x80, 0x00, 0x00}, false, tagAndLength{}}, {[]byte{0x1f, 0x88, 0x80, 0x80, 0x80, 0x00, 0x00}, false, tagAndLength{}},
// Long tag number form may not be used for tags that fit in short form.
{[]byte{0x1f, 0x1e, 0x00}, false, tagAndLength{}},
} }
func TestParseTagAndLength(t *testing.T) { func TestParseTagAndLength(t *testing.T) {
......
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