Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/asn1: error when unmarshalling SEQUENCE OF SET #27426

Open
kaxap opened this issue Aug 31, 2018 · 3 comments · May be fixed by #30059

Comments

@kaxap
Copy link

commented Aug 31, 2018

What version of Go are you using (go version)?

go1.11 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\xxx\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\dev;C:\gopath
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\xxx\AppData\Local\Temp\go-build551125774=/tmp/go-build -gno-rec
ord-gcc-switches

What did you do?

package main

import (
	"encoding/asn1"
	"fmt"
)

//` schema:
//
//	World-Schema DEFINITIONS AUTOMATIC TAGS ::=
//	BEGIN
//	SomeStruct ::= SEQUENCE
//	{
//		id      INTEGER,
//		field1  SEQUENCE OF SomeSet
//	}
//	SomeSet ::= SET
//	{
//		field2 INTEGER
//	}
//	END
//`

// data
//	{
//		"id":1,
//		"field1": [{"field2": 1}, {"field2": 2}]
//	}

// encode with http://asn1-playground.oss.com/
const encodedDer = "\x30\x0F\x80\x01\x01\xA1\x0A\x31\x03\x80\x01\x01\x31\x03\x80\x01\x02"

type SomeStruct struct {
	ID int `asn1:"tag:0"`
	SomeSetSlice []SomeSet `asn1:"tag:1,set"`
}

type SomeSet struct {
	Field2 int `asn1:"tag:0"`
}

func main() {
	var b SomeStruct
	_, err := asn1.Unmarshal([]byte(encodedDer), &b)
	if err != nil {
		panic(err)
	}
	fmt.Println(b)
}

play link:
https://play.golang.org/p/V-za5Cu1wkr

What did you expect to see?

SEQUENCE OF SET should be properly unmarshalled into a slice of structs. Documentation says that SET can be unmarshalled into a struct. So I believe a SEQUENCE OF SET is expected to be properly unmarshalled into a slice of structs.

What did you see instead?

asn1: structure error: sequence tag mismatch.

The problem might be in getUniversalTag function when it is called from parseSequenceOf. E.g.

	case reflect.Struct:
		return false, TagSequence, true, true

When struct is an element of slice there is no possibility to tag a struct as a SET (17) , it's always tagged as a SEQUENCE (16).

@kaxap kaxap changed the title encoding/asn1 error when unmarshalling SEQUENCE OF SET encoding/asn1: error when unmarshalling SEQUENCE OF SET Aug 31, 2018

@andybons andybons added this to the Unplanned milestone Sep 4, 2018

@andybons

This comment has been minimized.

Copy link
Member

commented Sep 4, 2018

@ken5scal

This comment has been minimized.

Copy link

commented Jan 3, 2019

I'm encountering a similar issue.

@gopherbot

This comment has been minimized.

Copy link

commented Feb 1, 2019

Change https://golang.org/cl/160819 mentions this issue: encoding/asn1: fix unmarshalling SEQUENCE OF SET. Fixes #27426

kaxap added a commit to kaxap/go that referenced this issue Feb 1, 2019
@kaxap kaxap referenced a pull request that will close this issue Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.