Skip to content

Commit 06a226f

Browse files
tahkapaaagl
authored andcommitted
Use boolean tag (0x01) for boolean type.
Fix for problem with ReadASN1Boolean, which uses invalid tag (Integer / 0x02) when trying to read boolean value. This is a fix for the one case mentioned in "x/crypto/cryptobyte: cannot read boolean values #26565" This is specified in ITU-T X.690, section 8.2, with DER additions specified in 11.1. Change-Id: I3c9406bd6febb6112f380224fec1d42a6cd64ed4 GitHub-Last-Rev: fd7d01c GitHub-Pull-Request: #137 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/233161 Reviewed-by: Adam Langley <agl@golang.org> Run-TryBot: Adam Langley <agl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 4b2356b commit 06a226f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

cryptobyte/asn1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,12 @@ func (b *Builder) AddASN1(tag asn1.Tag, f BuilderContinuation) {
230230

231231
// String
232232

233-
// ReadASN1Boolean decodes an ASN.1 INTEGER and converts it to a boolean
233+
// ReadASN1Boolean decodes an ASN.1 BOOLEAN and converts it to a boolean
234234
// representation into out and advances. It reports whether the read
235235
// was successful.
236236
func (s *String) ReadASN1Boolean(out *bool) bool {
237237
var bytes String
238-
if !s.ReadASN1(&bytes, asn1.INTEGER) || len(bytes) != 1 {
238+
if !s.ReadASN1(&bytes, asn1.BOOLEAN) || len(bytes) != 1 {
239239
return false
240240
}
241241

cryptobyte/asn1_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,24 @@ func TestAddASN1BigInt(t *testing.T) {
351351
t.Errorf("unexpected bytes %v, want %v", &y, x)
352352
}
353353
}
354+
355+
func TestReadASN1Boolean(t *testing.T) {
356+
testData := []struct {
357+
in []byte
358+
ok bool
359+
out bool
360+
}{
361+
{[]byte{}, false, false},
362+
{[]byte{0x01, 0x01, 0x00}, true, false},
363+
{[]byte{0x01, 0x01, 0xff}, true, true},
364+
{[]byte{0x01, 0x01, 0x01}, false, false},
365+
}
366+
for i, test := range testData {
367+
in := String(test.in)
368+
var out bool
369+
ok := in.ReadASN1Boolean(&out)
370+
if ok != test.ok || ok && (out != test.out) {
371+
t.Errorf("#%d: in.ReadASN1Boolean() = %v, want %v; out = %v, want %v", i, ok, test.ok, out, test.out)
372+
}
373+
}
374+
}

0 commit comments

Comments
 (0)