government / internal / der / der_test.go

@ bdd32d2bb7ca2a1b5b8c192fbaab691d8f1ed255 | history


package der

import (
	"bytes"
	"crypto/x509"
	"encoding/asn1"
	"strings"
	"testing"
)

func mustMarshal(t *testing.T, v any, params string) []byte {
	t.Helper()
	b, err := asn1.MarshalWithParams(v, params)
	if err != nil {
		t.Fatal(err)
	}
	return b
}

func TestLengthForms(t *testing.T) {
	// An OCTET STRING (tag 4) of each size, against encoding/asn1.
	for _, n := range []int{0, 1, 127, 128, 255, 256, 65535, 65536, 1 << 24} {
		content := bytes.Repeat([]byte{0xab}, n)
		got := TLV(0x04, content)
		want := mustMarshal(t, content, "")
		if !bytes.Equal(got, want) {
			t.Errorf("length %d: header % x, want % x", n, got[:min(len(got), 6)], want[:min(len(want), 6)])
		}
	}
}

func TestOIDContent(t *testing.T) {
	tests := [][]uint64{
		{1, 2},
		{2, 5, 29, 32, 0}, // anyPolicy
		{2, 5, 29, 33},    // policyMappings
		{1, 3, 6, 1, 5, 5, 7, 2, 2},
		{2, 999, 3}, // second arc past 40 under arc 2
		{1, 2, 128, 16383, 16384, 1 << 63},
	}
	for _, arcs := range tests {
		got, err := OIDContent(arcs...)
		if err != nil {
			t.Errorf("%v: %v", arcs, err)
			continue
		}
		oid, err := x509.OIDFromInts(arcs)
		if err != nil {
			t.Fatal(err)
		}
		want, _ := oid.MarshalBinary()
		if !bytes.Equal(got, want) {
			t.Errorf("%v: % x, want % x (x509.OID)", arcs, got, want)
		}
		if !bytes.Equal(OID(oid), TLV(TagOID, got)) {
			t.Errorf("%v: OID and OIDContent disagree", arcs)
		}
	}
}

func TestOIDMatchesASN1(t *testing.T) {
	for _, arcs := range []asn1.ObjectIdentifier{{2, 5, 29, 32, 0}, {1, 3, 6, 1, 4, 1, 311}, {2, 999, 3}} {
		oid, err := x509.ParseOID(arcs.String())
		if err != nil {
			t.Fatal(err)
		}
		if got, want := OID(oid), mustMarshal(t, arcs, ""); !bytes.Equal(got, want) {
			t.Errorf("%v: % x, want % x", arcs, got, want)
		}
	}
}

func TestOIDWithLargeArc(t *testing.T) {
	// encoding/asn1 cannot write arcs past int, so compare against
	// x509.ParseOID, which can.
	const s = "2.25.9665536045854453410170579537759926404.2.1.52.50"
	oid, err := x509.ParseOID(s)
	if err != nil {
		t.Fatal(err)
	}
	enc := OID(oid)
	var back x509.OID
	if err := back.UnmarshalBinary(enc[2:]); err != nil || back.String() != s {
		t.Errorf("round trip gave %s, %v", back, err)
	}
	if enc[0] != TagOID || int(enc[1]) != len(enc)-2 {
		t.Errorf("header % x", enc[:2])
	}
}

func TestOIDContentRejects(t *testing.T) {
	for _, arcs := range [][]uint64{{1}, {3, 1}, {0, 40}, {1, 40}} {
		if _, err := OIDContent(arcs...); err == nil {
			t.Errorf("%v accepted", arcs)
		}
	}
}

func TestUTF8String(t *testing.T) {
	for _, s := range []string{"", "hi", "héllo 😀", strings.Repeat("x", 200)} {
		if got, want := UTF8String(s), mustMarshal(t, s, "utf8"); !bytes.Equal(got, want) {
			t.Errorf("%q: % x, want % x", s, got, want)
		}
	}
}

// TestSequenceOfSequences builds the shape of PolicyMappings, a SEQUENCE OF
// SEQUENCE { issuerDomainPolicy, subjectDomainPolicy }, both ways.
func TestSequenceOfSequences(t *testing.T) {
	type mapping struct {
		Issuer, Subject asn1.ObjectIdentifier
	}
	pairs := []mapping{
		{asn1.ObjectIdentifier{1, 2, 3}, asn1.ObjectIdentifier{1, 2, 4}},
		{asn1.ObjectIdentifier{1, 2, 3}, asn1.ObjectIdentifier{1, 2, 5, 1000}},
	}
	var elems [][]byte
	for _, p := range pairs {
		i, _ := x509.ParseOID(p.Issuer.String())
		s, _ := x509.ParseOID(p.Subject.String())
		elems = append(elems, Sequence(OID(i), OID(s)))
	}
	if got, want := Sequence(elems...), mustMarshal(t, pairs, ""); !bytes.Equal(got, want) {
		t.Errorf("% x\nwant % x", got, want)
	}
	if got, want := Sequence(), mustMarshal(t, []mapping{}, ""); !bytes.Equal(got, want) {
		t.Errorf("empty: % x, want % x", got, want)
	}
}

func TestMixedSequence(t *testing.T) {
	v := struct {
		ID   asn1.ObjectIdentifier
		Text string `asn1:"utf8"`
	}{asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 2, 2}, strings.Repeat("note ", 40)}
	oid, _ := x509.ParseOID(v.ID.String())
	if got, want := Sequence(OID(oid), UTF8String(v.Text)), mustMarshal(t, v, ""); !bytes.Equal(got, want) {
		t.Errorf("% x\nwant % x", got, want)
	}
}