Skip to content

encoding/asn1: add dynamic length integer support for base 128 integer parsing #40738

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

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 5 additions & 5 deletions src/encoding/asn1/asn1.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package asn1
import (
"errors"
"fmt"
"math"
"math/big"
"reflect"
"strconv"
Expand Down Expand Up @@ -305,9 +304,10 @@ func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error)
offset = initOffset
var ret64 int64
for shifted := 0; offset < len(bytes); shifted++ {
// 5 * 7 bits per byte == 35 bits of data
// Thus the representation is either non-minimal or too large for an int32
if shifted == 5 {
// n shifted bytes * 7 bits per byte should be less than or equal to the max number
// of bits. If greater, the representation is either non-minimal or too large for
// the platform
if shifted * 7 > strconv.IntSize {
err = StructuralError{"base 128 integer too large"}
return
}
Expand All @@ -324,7 +324,7 @@ func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error)
if b&0x80 == 0 {
ret = int(ret64)
// Ensure that the returned value fits in an int on all platforms
if ret64 > math.MaxInt32 {
if ret64 > int64(^uint(0) >> 1) {
err = StructuralError{"base 128 integer too large"}
}
return
Expand Down