cryptobyte: handle AddASN1BigInt with -1

Fixes golang/go#38510

Change-Id: Ie52db22bf85a5f79046fad95e5bbe2788c3bec92
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/229457
Reviewed-by: Filippo Valsorda <filippo@golang.org>
This commit is contained in:
Katie Hockman
2020-04-23 19:51:18 +00:00
parent 44a6062868
commit 18b771bd64
2 changed files with 17 additions and 1 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ func (b *Builder) AddASN1BigInt(n *big.Int) {
for i := range bytes {
bytes[i] ^= 0xff
}
if bytes[0]&0x80 == 0 {
if len(bytes) == 0 || bytes[0]&0x80 == 0 {
c.add(0xff)
}
c.add(bytes...)
+16
View File
@@ -335,3 +335,19 @@ func TestReadASN1BitString(t *testing.T) {
}
}
}
func TestAddASN1BigInt(t *testing.T) {
x := big.NewInt(-1)
var b Builder
b.AddASN1BigInt(x)
got, err := b.Bytes()
if err != nil {
t.Fatalf("unexpected error adding -1: %v", err)
}
s := String(got)
var y big.Int
ok := s.ReadASN1Integer(&y)
if !ok || x.Cmp(&y) != 0 {
t.Errorf("unexpected bytes %v, want %v", &y, x)
}
}