aboutsummaryrefslogtreecommitdiff
path: root/x509util/x509util_test.go
blob: 538ef453639f670f6de4a18652e3011c63e71f0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package x509util

import (
	"bytes"
	"fmt"
	"testing"

	"github.com/system-transparency/stfe/x509util/testdata"
)

func TestNewEd25519PrivateKey(t *testing.T) {
	for _, table := range []struct {
		description string
		pem         []byte
		wantErr     bool
	}{
		{
			description: "bad block: unwanted white space",
			pem:         testdata.Ed25519PrivateKeyBadWhiteSpace,
			wantErr:     true,
		},
		{
			description: "invalid block type",
			pem:         testdata.EndEntityCertificate,
			wantErr:     true,
		},
		{
			description: "bad block: trailing data",
			pem:         testdata.DoubleEd25519PrivateKey,
			wantErr:     true,
		},
		{
			description: "bad block bytes: truncated key",
			pem:         testdata.TruncatedEd25519PrivateKey,
			wantErr:     true,
		},
		{
			description: "bad block bytes: not an ed25519 private key",
			pem:         testdata.NotEd25519PrivateKey,
			wantErr:     true,
		},
		{
			description: "ok ed25519 private key",
			pem:         testdata.EndEntityPrivateKey,
		},
	} {
		_, err := NewEd25519PrivateKey(table.pem)
		if got, want := err != nil, table.wantErr; got != want {
			t.Errorf("got error=%v but wanted %v in test %q: %v", got, want, table.description, err)
		}
	}
}

func TestNewCertificateList(t *testing.T) {
	for _, table := range []struct {
		description string
		pem         []byte
		wantErr     bool
		wantSerial  []string
	}{
		{
			description: "invalid block type",
			pem:         testdata.EndEntityPrivateKey,
			wantErr:     true,
		},
		{
			description: "bad block bytes: not a certificate",
			pem:         testdata.NotACertificate,
			wantErr:     true,
		},
		{
			description: "bad block bytes: truncated certificate",
			pem:         testdata.TruncatedCertificate,
			wantErr:     true,
		},
		{
			description: "bad block bytes: truncated certificate in list",
			pem:         append(testdata.TruncatedCertificate, testdata.IntermediateCertificate...),
			wantErr:     true,
		},
		{
			description: "bad block: unwanted white spaces",
			pem:         testdata.CertificateBadWhiteSpace,
			wantErr:     true,
		},
		{
			description: "ok certificate list: empty",
			pem:         []byte{},
			wantSerial:  nil,
		},
		{
			description: "ok certificate list: size 1",
			pem:         testdata.EndEntityCertificate,
			wantSerial:  []string{testdata.EndEntityCertificateSerial},
		},
		{
			description: "ok certificate list: size 2",
			pem:         testdata.IntermediateChain,
			wantSerial:  []string{testdata.EndEntityCertificateSerial, testdata.IntermediateCertificateSerial},
		},
		{
			description: "ok certificate list: size 3",
			pem:         testdata.RootChain,
			wantSerial: []string{
				testdata.EndEntityCertificateSerial,
				testdata.IntermediateCertificateSerial,
				testdata.RootCertificateSerial,
			},
		},
	} {
		list, err := NewCertificateList(table.pem)
		if got, want := err != nil, table.wantErr; got != want {
			t.Errorf("got error=%v but wanted %v in test %q: %v", got, want, table.description, err)
		}
		if err != nil {
			continue
		}
		if got, want := len(list), len(table.wantSerial); got != want {
			t.Errorf("got list of length %d but wanted %d in test %q", got, want, table.description)
			continue
		}
		for i, certificate := range list {
			if got, want := fmt.Sprintf("%v", certificate.SerialNumber), table.wantSerial[i]; got != want {
				t.Errorf("Got serial number %s but wanted %s on index %d and test %q", got, want, i, table.description)
			}
		}
	}
}

func TestNewCertPool(t *testing.T) {
	for i, pem := range [][]byte{
		testdata.EndEntityCertificate,
		testdata.IntermediateChain,
		testdata.RootChain,
	} {
		list, err := NewCertificateList(pem)
		if err != nil {
			t.Fatalf("must parse chain: %v", err)
		}
		pool := NewCertPool(list)
		if got, want := len(pool.Subjects()), len(list); got != want {
			t.Errorf("got pool of size %d but wanted %d in test %d", got, want, i)
			continue
		}
		for j, got := range pool.Subjects() {
			if want := list[j].RawSubject; !bytes.Equal(got, want) {
				t.Errorf("got subject[%d]=%X but wanted %X in test %d", j, got, want, i)
			}
		}
	}
}

// TODO: TestParseDerChain
func TestParseDerChain(t *testing.T) {
}

func TestParseDerList(t *testing.T) {
	for _, table := range []struct {
		description string
		list        [][]byte
		wantErr     bool
	}{
		{
			description: "invalid certificate: first byte is missing",
			list: [][]byte{
				mustMakeDerList(t, testdata.EndEntityCertificate)[0][1:],
			},
			wantErr: true,
		},
		{
			description: "valid certificate list: empty",
		},
		{
			description: "valid certificate list: size 1",
			list:        mustMakeDerList(t, testdata.EndEntityCertificate),
		},
		{
			description: "valid certificate list: size 2",
			list:        mustMakeDerList(t, testdata.IntermediateChain),
		},
		{
			description: "valid certificate list: size 3",
			list:        mustMakeDerList(t, testdata.RootChain),
		},
	} {
		list, err := ParseDerList(table.list)
		if got, want := err != nil, table.wantErr; got != want {
			t.Errorf("got error=%v but wanted %v in test %q: %v", got, want, table.description, err)
		}
		if err != nil {
			continue
		}

		if got, want := len(list), len(table.list); got != want {
			t.Errorf("got %d certifictes but wanted %d in test %q", got, want, table.description)
			continue
		}
		for i, cert := range list {
			if got, want := cert.Raw, table.list[i]; !bytes.Equal(got, want) {
				t.Errorf("got certificate bytes %X but wanted %X in test %q", got, want, table.description)
			}
		}
	}
}

// mustMakeDerList must create a list of DER-encoded certificates from PEM
func mustMakeDerList(t *testing.T, pem []byte) [][]byte {
	certs, err := NewCertificateList(pem)
	if err != nil {
		t.Fatalf("must parse pem-encoded certificates: %v", err)
	}

	list := make([][]byte, 0, len(certs))
	for _, cert := range certs {
		list = append(list, cert.Raw)
	}
	return list
}

func TestVerifyChain(t *testing.T) {
	for _, table := range []struct {
		description string
		pem         []byte
		wantErr     bool
	}{
		{
			description: "invalid chain: intermediate did not sign end-entity",
			pem:         testdata.ChainBadIntermediate,
			wantErr:     true,
		},
		{
			description: "invalid chain: root did not sign intermediate",
			pem:         testdata.ChainBadRoot,
			wantErr:     true,
		},
		{
			description: "valid chain",
			pem:         testdata.RootChain,
		},
		{
			description: "valid chain 2",
			pem:         testdata.RootChain2,
		},
	} {
		chain, err := NewCertificateList(table.pem)
		if err != nil {
			t.Fatalf("must parse chain: %v", err)
		}
		err = VerifyChain(chain)
		if got, want := err != nil, table.wantErr; got != want {
			t.Errorf("got error %v but wanted %v in test %q: %v", got, want, table.description, err)
		}
	}
}