File: signature_list_test.go

package info (click to toggle)
golang-github-foxboron-go-uefi 0.0~git20250207.69fb7db-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,140 kB
  • sloc: makefile: 29; sh: 14
file content (270 lines) | stat: -rw-r--r-- 7,242 bytes parent folder | download
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package signature

import (
	"bytes"
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"github.com/foxboron/go-uefi/efi/attributes"
	"github.com/foxboron/go-uefi/efi/util"
	"go.mozilla.org/pkcs7"
)

func ReadTestData(dir string) []string {
	var paths []string
	files, _ := os.ReadDir(dir)
	for _, file := range files {
		paths = append(paths, filepath.Join(dir, file.Name()))
	}
	return paths
}

var (
	EfivarsTestFiles         = ReadTestData("../../tests/data/signatures/efivars")
	SiglistTestFiles         = ReadTestData("../../tests/data/signatures/siglist")
	SiglistchecksumTestFiles = ReadTestData("../../tests/data/signatures/siglistchecksum")
	SigsupportTestFiles      = ReadTestData("../../tests/data/signatures/sigsupport")
)

func TestParseSignatureListVars(t *testing.T) {
	for _, path := range EfivarsTestFiles {
		attrs, data, err := attributes.ReadEfivarsFile(path)
		var pkflags attributes.Attributes
		pkflags |= attributes.EFI_VARIABLE_NON_VOLATILE
		pkflags |= attributes.EFI_VARIABLE_BOOTSERVICE_ACCESS
		pkflags |= attributes.EFI_VARIABLE_RUNTIME_ACCESS
		pkflags |= attributes.EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
		if (pkflags & attrs) != pkflags {
			t.Errorf("Incorrect bitmask")
		}

		c, err := ReadSignatureList(data)
		if err != nil {
			t.Fatal(err)
		}
		if util.CmpEFIGUID(c.SignatureType, CERT_X509_GUID) {
			// Run over and ensure we are getting the correct type
			for _, d := range c.Signatures {
				_, err := pkcs7.NewSignedData(d.Data)
				if err != nil {
					t.Fatal(err)
				}
			}
		}
	}
}

func TestParseSignatureListFile(t *testing.T) {
	for _, path := range SiglistTestFiles {
		b, _ := os.ReadFile(path)
		f := bytes.NewReader(b)
		c, err := ReadSignatureList(f)
		if err != nil {
			t.Fatal(err)
		}
		if util.CmpEFIGUID(c.SignatureType, CERT_X509_GUID) {
			for _, d := range c.Signatures {
				_, err := pkcs7.NewSignedData(d.Data)
				if err != nil {
					t.Fatal(err)
				}
			}
		}
	}
}

func TestParseSignatureListHashFile(t *testing.T) {
	for _, path := range SiglistchecksumTestFiles {
		b, _ := os.ReadFile(path)
		f := bytes.NewReader(b)
		c, err := ReadSignatureList(f)
		if err != nil {
			t.Fatal(err)
		}
		if util.CmpEFIGUID(c.SignatureType, CERT_SHA256_GUID) {
			for _, d := range c.Signatures {
				if fmt.Sprintf("%x", d.Data) != "4be2e8d5ef8113c3b9218f05f8aed1df8a6b0e24c706360d39f74a7423f00e32" {
					t.Fatal("Not correct checksum")
				}
			}
		}
	}
}

func TestParseSignatureSupport(t *testing.T) {
	for _, path := range SigsupportTestFiles {
		attrs, data, err := attributes.ReadEfivarsFile(path)
		if err != nil {
			t.Fatal(err)
		}
		var pkflags attributes.Attributes
		pkflags |= attributes.EFI_VARIABLE_BOOTSERVICE_ACCESS
		pkflags |= attributes.EFI_VARIABLE_RUNTIME_ACCESS
		if (pkflags & attrs) != pkflags {
			t.Errorf("Incorrect bitmask")
		}
		guids, err := GetSupportedSignatures(data)
		if err != nil {
			t.Fatal(err)
		}
		if guids[0] != CERT_SHA1_GUID {
			t.Errorf("Unexpected certificate, should be SHA1")
		}
		if guids[1] != CERT_SHA256_GUID {
			t.Errorf("Unexpected certificate, should be SHA256")
		}
		if guids[2] != CERT_RSA2048_GUID {
			t.Errorf("Unexpected certificate, should be RSA2048")
		}
		if guids[3] != CERT_X509_GUID {
			t.Errorf("Unexpected certificate, should be X509")
		}
	}
}

var sigdata = []SignatureData{
	SignatureData{Owner: util.EFIGUID{Data1: 0xc1095e1b, Data2: 0x8a3b, Data3: 0x4cf5, Data4: [8]uint8{0x9d, 0x4a, 0xaf, 0xc7, 0xd7, 0x5d, 0xca, 0x68}}, Data: []uint8{0x81, 0xb4, 0xd9, 0x69, 0x31, 0xbf, 0xd, 0x2, 0xfd, 0x91, 0xa6, 0x1e, 0x19, 0xd1, 0x4f, 0x1d, 0xa4, 0x52, 0xe6, 0x6d, 0xb2, 0x40, 0x8c, 0xa8, 0x60, 0x4d, 0x41, 0x1f, 0x92, 0x65, 0x9f, 0xa}},
	SignatureData{Owner: util.EFIGUID{Data1: 0xc1095e1b, Data2: 0x8a3b, Data3: 0x4cf5, Data4: [8]uint8{0x9d, 0x4a, 0xaf, 0xc7, 0xd7, 0x5d, 0xca, 0x68}}, Data: []uint8{0x82, 0xb4, 0xd9, 0x69, 0x31, 0xbf, 0xd, 0x2, 0xfd, 0x91, 0xa6, 0x1e, 0x19, 0xd1, 0x4f, 0x1d, 0xa4, 0x52, 0xe6, 0x6d, 0xb2, 0x40, 0x8c, 0xa8, 0x60, 0x4d, 0x41, 0x1f, 0x92, 0x65, 0x9f, 0xa}},
	SignatureData{Owner: util.EFIGUID{Data1: 0xc1095e1b, Data2: 0x8a3b, Data3: 0x4cf5, Data4: [8]uint8{0x9d, 0x4a, 0xaf, 0xc7, 0xd7, 0x5d, 0xca, 0x68}}, Data: []uint8{0x83, 0xb4, 0xd9, 0x69, 0x31, 0xbf, 0xd, 0x2, 0xfd, 0x91, 0xa6, 0x1e, 0x19, 0xd1, 0x4f, 0x1d, 0xa4, 0x52, 0xe6, 0x6d, 0xb2, 0x40, 0x8c, 0xa8, 0x60, 0x4d, 0x41, 0x1f, 0x92, 0x65, 0x9f, 0xa}},
}

func TestSiglist(t *testing.T) {
	sl := NewSignatureList(CERT_SHA256_GUID)
	for _, sig := range sigdata {
		sl.AppendBytes(sig.Owner, sig.Data)
	}
	if sl.ListSize != 172 {
		t.Fatal("list size incorrect")
	}
	if sl.Size != 48 {
		t.Fatal("size incorrect")
	}
	if len(sl.Signatures) != 3 {
		t.Fatal("number of signatures wrong")
	}
}

func TestSiglistRemove(t *testing.T) {
	sl := NewSignatureList(CERT_SHA256_GUID)
	for _, sig := range sigdata {
		sl.AppendBytes(sig.Owner, sig.Data)
	}
	err := sl.RemoveSignature(sigdata[0])
	if err != nil {
		t.Error(err)
	}
	if len(sl.Signatures) != 2 {
		t.Fatal("remove: number of signatures wrong")
	}
	if sl.Size != 48 {
		t.Fatal("remove: size incorrect")
	}
	if sl.ListSize != 124 {
		t.Fatal("remove: list size incorrect")
	}
}

func TestSiglistRemoveSame(t *testing.T) {
	sl := NewSignatureList(CERT_SHA256_GUID)
	for _, sig := range sigdata {
		sl.AppendBytes(sig.Owner, sig.Data)
	}
	sl.RemoveSignature(sigdata[0])
	// Try remove the same sigdata, should give error
	err := sl.RemoveSignature(sigdata[0])
	if err == nil {
		t.Error(err)
	}
}

func TestSiglistRemoveAll(t *testing.T) {
	sl := NewSignatureList(CERT_SHA256_GUID)
	for _, sig := range sigdata {
		sl.AppendBytes(sig.Owner, sig.Data)
	}
	var err error
	for _, sig := range sigdata {
		err = sl.RemoveSignature(sig)
	}
	if err != nil {
		t.Error(err)
	}
	if len(sl.Signatures) != 0 {
		t.Fatal("remove: number of signatures wrong")
	}
	if sl.Size != 0 {
		t.Fatal("remove: size incorrect")
	}
	if sl.ListSize != 28 {
		t.Fatal("remove: list size incorrect")
	}
}

func TestSiglistExists(t *testing.T) {
	sl1 := NewSignatureList(CERT_SHA256_GUID)
	for _, sig := range sigdata {
		sl1.AppendBytes(sig.Owner, sig.Data)
	}
	sl2 := NewSignatureList(CERT_SHA256_GUID)
	for _, sig := range sigdata {
		sl2.AppendBytes(sig.Owner, sig.Data)
	}
	if !sl1.ExistsInList(sl2) {
		t.Fatal("exists: not the same list")
	}
}

func TestSiglistSigDataExists(t *testing.T) {
	sl := NewSignatureList(CERT_SHA256_GUID)
	for _, sig := range sigdata {
		sl.AppendBytes(sig.Owner, sig.Data)
	}
	if ok, _ := sl.Exists(&sigdata[0]); !ok {
		t.Fatal("exists: sigdata is not in the list")
	}
}

func TestSiglists(t *testing.T) {
	for _, c := range []struct {
		name string
		f    string
	}{
		{
			name: "db",
			f:    "testdata/db",
		},
		{
			name: "dbdefault",
			f:    "testdata/dbdefault",
		},
		{
			name: "kek",
			f:    "testdata/kek",
		},
		{
			name: "kekdefault",
			f:    "testdata/kekdefault",
		},
		{
			name: "pk",
			f:    "testdata/pk",
		},
		{
			name: "pkdefault",
			f:    "testdata/pkdefault",
		},
	} {
		t.Run(c.name, func(t *testing.T) {
			_, data, err := attributes.ReadEfivarsFile(c.f)
			if err != nil {
				t.Fatalf("%v", err)
			}
			_, err = ReadSignatureDatabase(data)
			if err != nil {
				t.Fatalf("%v", err)
			}
		})
	}
}