Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions cryptobyte/asn1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
69 changes: 69 additions & 0 deletions cryptobyte/named_integer_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading