diff --git a/cryptobyte/asn1.go b/cryptobyte/asn1.go index d25979d9f5..6d839a26da 100644 --- a/cryptobyte/asn1.go +++ b/cryptobyte/asn1.go @@ -272,24 +272,31 @@ func (s *String) ReadASN1Boolean(out *bool) bool { // ReadASN1Integer reports whether the read was successful. func (s *String) ReadASN1Integer(out interface{}) bool { switch out := out.(type) { - case *int, *int8, *int16, *int32, *int64: + case *big.Int: + return s.readASN1BigInt(out) + case *[]byte: + return s.readASN1Bytes(out) + } + v := reflect.ValueOf(out) + if v.Kind() != reflect.Ptr { + panic("out is not a pointer") + } + v = v.Elem() + switch v.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: var i int64 - if !s.readASN1Int64(&i) || reflect.ValueOf(out).Elem().OverflowInt(i) { + if !s.readASN1Int64(&i) || v.OverflowInt(i) { return false } - reflect.ValueOf(out).Elem().SetInt(i) + v.SetInt(i) return true - case *uint, *uint8, *uint16, *uint32, *uint64: + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: var u uint64 - if !s.readASN1Uint64(&u) || reflect.ValueOf(out).Elem().OverflowUint(u) { + if !s.readASN1Uint64(&u) || v.OverflowUint(u) { return false } - reflect.ValueOf(out).Elem().SetUint(u) + v.SetUint(u) return true - case *big.Int: - return s.readASN1BigInt(out) - case *[]byte: - return s.readASN1Bytes(out) default: panic("out does not point to an integer type") } @@ -688,8 +695,7 @@ func (s *String) ReadOptionalASN1Integer(out interface{}, tag asn1.Tag, defaultV } if !present { switch out.(type) { - case *int, *int8, *int16, *int32, *int64, - *uint, *uint8, *uint16, *uint32, *uint64, *[]byte: + case *[]byte: reflect.ValueOf(out).Elem().Set(reflect.ValueOf(defaultValue)) case *big.Int: if defaultValue, ok := defaultValue.(*big.Int); ok { @@ -698,6 +704,16 @@ func (s *String) ReadOptionalASN1Integer(out interface{}, tag asn1.Tag, defaultV panic("out points to big.Int, but defaultValue does not") } default: + v := reflect.ValueOf(out) + if v.Kind() == reflect.Ptr { + v = v.Elem() + switch v.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + v.Set(reflect.ValueOf(defaultValue)) + return true + } + } panic("invalid integer type") } return true diff --git a/cryptobyte/named_integer_test.go b/cryptobyte/named_integer_test.go new file mode 100644 index 0000000000..9e1e602bd0 --- /dev/null +++ b/cryptobyte/named_integer_test.go @@ -0,0 +1,69 @@ +// Copyright 2026 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package cryptobyte + +import ( + "testing" + + "golang.org/x/crypto/cryptobyte/asn1" +) + +func TestReadASN1NamedInteger(t *testing.T) { + type signed int64 + type unsigned uint64 + type smallSigned int8 + type smallUnsigned uint8 + var i signed + var u unsigned + var si smallSigned + var su smallUnsigned + for _, test := range []struct { + out interface{} + value int64 + ok bool + }{ + {&i, -1234, true}, + {&u, 1234, true}, + {&si, -128, true}, + {&su, 255, true}, + {&si, 128, false}, + {&su, 256, false}, + {&u, -1, false}, + } { + b := NewBuilder(nil) + b.AddASN1Int64(test.value) + encoded, err := b.Bytes() + if err != nil { + t.Fatal(err) + } + in := String(encoded) + if ok := in.ReadASN1Integer(test.out); ok != test.ok { + t.Errorf("ReadASN1Integer(%T), value %d = %v, want %v", test.out, test.value, ok, test.ok) + } + } + if i != -1234 || u != 1234 || si != -128 || su != 255 { + t.Errorf("decoded values = %d, %d, %d, %d", i, u, si, su) + } +} + +func TestReadOptionalASN1NamedInteger(t *testing.T) { + type integer int64 + const tag = asn1.SEQUENCE + var out integer + var empty String + if !empty.ReadOptionalASN1Integer(&out, tag, integer(42)) || out != 42 { + t.Fatalf("missing optional integer = %d, want 42", out) + } + b := NewBuilder(nil) + b.AddASN1(tag, func(b *Builder) { b.AddASN1Int64(-1234) }) + encoded, err := b.Bytes() + if err != nil { + t.Fatal(err) + } + in := String(encoded) + if !in.ReadOptionalASN1Integer(&out, tag, integer(42)) || out != -1234 || !in.Empty() { + t.Errorf("present optional integer = %d, want -1234", out) + } +}