golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.82k stars 17.51k forks source link

encoding/asn1: bool values not unmarshaled into interface type #68241

Open bjangelo opened 2 months ago

bjangelo commented 2 months ago

Go version

go version go1.22.3 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/bangelo/.cache/go-build'
GOENV='/home/bangelo/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/bangelo/code/go/pkg/mod'
GOOS='linux'
GOPATH='/home/bangelo/code/go'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -fmessage-length=0 -ffile-prefix-map=/tmp/go-build324096014=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Attempted to unmarshal boolean asn1 value into any type.

https://go.dev/play/p/QEFu_kJD6b4

package main

import (
    "encoding/asn1"
    "fmt"
    "log"
)

func main() {
    data, err := asn1.Marshal(true)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%v\n", data)

    var value any
    rest, err := asn1.Unmarshal(data, &value)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%d\n", len(rest))
    fmt.Printf("%T\n", value)
}

What did you see happen?

Unmarshal consumed the boolean TLV but did not populate the any type variable.

[1 1 255]
0
<nil>

What did you expect to see?

Unmarshal populate the any type variable.

[1 1 255]
0
bool
gabyhelp commented 2 months ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

bjangelo commented 2 months ago

Perhaps the parseField function is just missing a case statement for TagBoolean when dealing with any type?

diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go
index 781ab87691..72d3f84f1c 100644
--- a/src/encoding/asn1/asn1.go
+++ b/src/encoding/asn1/asn1.go
@@ -726,6 +726,8 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
                                result = innerBytes
                        case TagBMPString:
                                result, err = parseBMPString(innerBytes)
+                       case TagBoolean:
+                               result, err = parseBool(innerBytes)
                        default:
                                // If we don't know how to handle the type, we just leave Value as nil.
                        }
mauri870 commented 2 months ago

cc @golang/security per https://dev.golang.org/owners

gopherbot commented 2 months ago

Change https://go.dev/cl/595796 mentions this issue: encoding/asn1: unmarshal bool values correctly dealing with the ANY type