]> Cypherpunks repositories - gostls13.git/commitdiff
asn1: fix parsing of elements after a string in a structure.
authorAdam Langley <agl@golang.org>
Tue, 12 Jan 2010 02:53:58 +0000 (18:53 -0800)
committerAdam Langley <agl@golang.org>
Tue, 12 Jan 2010 02:53:58 +0000 (18:53 -0800)
Fixes #516.

R=rsc
CC=golang-dev, golang-dev
https://golang.org/cl/184080

src/pkg/asn1/asn1.go
src/pkg/asn1/asn1_test.go

index a422f28ad04c7287a8d50ea5ebad53026bcc4ca0..430b035f587101f224c7ac3d18a7ec06ab27e8e6 100644 (file)
@@ -609,6 +609,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
                return
        }
        innerBytes := bytes[offset : offset+t.length]
+       offset += t.length
 
        // We deal with the structures defined in this package first.
        switch fieldType {
@@ -619,13 +620,11 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
                if err1 == nil {
                        reflect.ArrayCopy(sliceValue, reflect.NewValue(newSlice).(reflect.ArrayOrSliceValue))
                }
-               offset += t.length
                err = err1
                return
        case bitStringType:
                structValue := v.(*reflect.StructValue)
                bs, err1 := parseBitString(innerBytes)
-               offset += t.length
                if err1 == nil {
                        structValue.Set(reflect.NewValue(bs).(*reflect.StructValue))
                }
@@ -634,7 +633,6 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
        case timeType:
                ptrValue := v.(*reflect.PtrValue)
                time, err1 := parseUTCTime(innerBytes)
-               offset += t.length
                if err1 == nil {
                        ptrValue.Set(reflect.NewValue(time).(*reflect.PtrValue))
                }
@@ -644,7 +642,6 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
        switch val := v.(type) {
        case *reflect.BoolValue:
                parsedBool, err1 := parseBool(innerBytes)
-               offset += t.length
                if err1 == nil {
                        val.Set(parsedBool)
                }
@@ -652,7 +649,6 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
                return
        case *reflect.IntValue:
                parsedInt, err1 := parseInt(innerBytes)
-               offset += t.length
                if err1 == nil {
                        val.Set(parsedInt)
                }
@@ -660,7 +656,6 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
                return
        case *reflect.Int64Value:
                parsedInt, err1 := parseInt64(innerBytes)
-               offset += t.length
                if err1 == nil {
                        val.Set(parsedInt)
                }
@@ -671,7 +666,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
 
                if structType.NumField() > 0 &&
                        structType.Field(0).Type == rawContentsType {
-                       bytes := bytes[initOffset : offset+t.length]
+                       bytes := bytes[initOffset:offset]
                        val.Field(0).SetValue(reflect.NewValue(RawContent(bytes)))
                }
 
@@ -686,7 +681,6 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
                                return
                        }
                }
-               offset += t.length
                // We allow extra bytes at the end of the SEQUENCE because
                // adding elements to the end has been used in X.509 as the
                // version numbers have increased.
@@ -699,7 +693,6 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
                        return
                }
                newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem())
-               offset += t.length
                if err1 == nil {
                        val.Set(newSlice)
                }
index 5071facfc07c04667b878305cc7538650dc10c70..43c746895a7506580fd706a66ed80ad2a79eaaf7 100644 (file)
@@ -263,6 +263,11 @@ type TestContextSpecificTags2 struct {
        B int
 }
 
+type TestElementsAfterString struct {
+       S    string
+       A, B int
+}
+
 var unmarshalTestData []unmarshalTest = []unmarshalTest{
        unmarshalTest{[]byte{0x02, 0x01, 0x42}, newInt(0x42)},
        unmarshalTest{[]byte{0x30, 0x08, 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d}, &TestObjectIdentifierStruct{[]int{1, 2, 840, 113549}}},
@@ -277,6 +282,7 @@ var unmarshalTestData []unmarshalTest = []unmarshalTest{
        unmarshalTest{[]byte{0x30, 0x08, 0xa1, 0x03, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02}, &TestContextSpecificTags2{1, 2}},
        unmarshalTest{[]byte{0x01, 0x01, 0x00}, newBool(false)},
        unmarshalTest{[]byte{0x01, 0x01, 0x01}, newBool(true)},
+       unmarshalTest{[]byte{0x30, 0x0b, 0x13, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x01, 0x22, 0x02, 0x01, 0x33}, &TestElementsAfterString{"foo", 0x22, 0x33}},
 }
 
 func TestUnmarshal(t *testing.T) {