aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/asn1
diff options
context:
space:
mode:
authorAndrew Benton <andrewmbenton@gmail.com>2017-03-07 14:00:54 -0800
committerBrad Fitzpatrick <bradfitz@golang.org>2017-04-24 22:23:56 +0000
commitd9b1f9e85ee097ebc95c5904cee921ba7be4f732 (patch)
tree71a8ba44320634797a2e104ef51fde1dd995013a /src/encoding/asn1
parent8fcd69d330d53d05ba50003de189e38db12b6227 (diff)
downloadgo-d9b1f9e85ee097ebc95c5904cee921ba7be4f732.tar.gz
go-d9b1f9e85ee097ebc95c5904cee921ba7be4f732.zip
encoding/asn1: add NullBytes and NullRawValue for working with ASN.1 NULL
There were a number of places in crypto/x509 that used hardcoded representations of the ASN.1 NULL type, in both byte slice and RawValue struct forms. This change adds two new exported vars to the asn1 package for working with ASN.1 NULL in both its forms, and converts all usages from the x509 package. In addition, tests were added to exercise Marshal and Unmarshal on both vars. See #19446 for discussion. Change-Id: I63dbd0835841ccbc810bd6ec794360a84e933f1e Reviewed-on: https://go-review.googlesource.com/38660 Run-TryBot: Adam Langley <agl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
Diffstat (limited to 'src/encoding/asn1')
-rw-r--r--src/encoding/asn1/asn1.go8
-rw-r--r--src/encoding/asn1/asn1_test.go26
-rw-r--r--src/encoding/asn1/common.go1
3 files changed, 35 insertions, 0 deletions
diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go
index 65f018d014..b8e2770596 100644
--- a/src/encoding/asn1/asn1.go
+++ b/src/encoding/asn1/asn1.go
@@ -207,6 +207,14 @@ func parseBitString(bytes []byte) (ret BitString, err error) {
return
}
+// NULL
+
+// NullRawValue is a RawValue with its Tag set to the ASN.1 NULL type tag (5).
+var NullRawValue = RawValue{Tag: TagNull}
+
+// NullBytes contains bytes representing the DER-encoded ASN.1 NULL type.
+var NullBytes = []byte{TagNull, 0}
+
// OBJECT IDENTIFIER
// An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.
diff --git a/src/encoding/asn1/asn1_test.go b/src/encoding/asn1/asn1_test.go
index 2dd799f236..c9eda4069d 100644
--- a/src/encoding/asn1/asn1_test.go
+++ b/src/encoding/asn1/asn1_test.go
@@ -479,6 +479,7 @@ var unmarshalTestData = []struct {
out interface{}
}{
{[]byte{0x02, 0x01, 0x42}, newInt(0x42)},
+ {[]byte{0x05, 0x00}, &RawValue{0, 5, false, []byte{}, []byte{0x05, 0x00}}},
{[]byte{0x30, 0x08, 0x06, 0x06, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d}, &TestObjectIdentifierStruct{[]int{1, 2, 840, 113549}}},
{[]byte{0x03, 0x04, 0x06, 0x6e, 0x5d, 0xc0}, &BitString{[]byte{110, 93, 192}, 18}},
{[]byte{0x30, 0x09, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x03}, &[]int{1, 2, 3}},
@@ -1007,3 +1008,28 @@ func TestUnexportedStructField(t *testing.T) {
t.Errorf("got %v, want %v", err, want)
}
}
+
+func TestNull(t *testing.T) {
+ marshaled, err := Marshal(NullRawValue)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(NullBytes, marshaled) {
+ t.Errorf("Expected Marshal of NullRawValue to yeild %x, got %x", NullBytes, marshaled)
+ }
+
+ unmarshaled := RawValue{}
+ if _, err := Unmarshal(NullBytes, &unmarshaled); err != nil {
+ t.Fatal(err)
+ }
+
+ unmarshaled.FullBytes = NullRawValue.FullBytes
+ if len(unmarshaled.Bytes) == 0 {
+ // DeepEqual considers a nil slice and an empty slice to be different.
+ unmarshaled.Bytes = NullRawValue.Bytes
+ }
+
+ if !reflect.DeepEqual(NullRawValue, unmarshaled) {
+ t.Errorf("Expected Unmarshal of NullBytes to yield %v, got %v", NullRawValue, unmarshaled)
+ }
+}
diff --git a/src/encoding/asn1/common.go b/src/encoding/asn1/common.go
index 0695180827..cd93b27ecb 100644
--- a/src/encoding/asn1/common.go
+++ b/src/encoding/asn1/common.go
@@ -24,6 +24,7 @@ const (
TagInteger = 2
TagBitString = 3
TagOctetString = 4
+ TagNull = 5
TagOID = 6
TagEnum = 10
TagUTF8String = 12