-
Notifications
You must be signed in to change notification settings - Fork 80
/
sbctl.go
222 lines (196 loc) · 4.74 KB
/
sbctl.go
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
package sbctl
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/foxboron/goefi/efi/attributes"
)
// Functions that doesn't fit anywhere else
// Veryvery simple check
func GetESP() string {
if _, err := os.Stat("/efi"); !os.IsNotExist(err) {
return "/efi"
}
out, err := exec.Command("lsblk", "-o", "PARTTYPE,MOUNTPOINT").Output()
if err != nil {
log.Fatal(err)
}
data := string(out)
for _, lines := range strings.Split(data, "\n") {
if len(lines) < 1 {
continue
}
l := strings.Split(lines, " ")
if len(l) != 2 {
continue
}
if l[0] == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" {
return l[1]
}
}
return ""
}
func VerifyESP() {
// Cache files we have looked at.
checked := make(map[string]bool)
espPath := GetESP()
files := ReadFileDatabase(DBPath)
msg.Printf("Verifying file database and EFI images in %s...", espPath)
for _, file := range files {
normalized := strings.Join(strings.Split(file.OutputFile, "/")[2:], "/")
checked[normalized] = true
if VerifyFile(DBCert, file.OutputFile) {
msg2.Printf("%s is signed\n", file.OutputFile)
} else {
warning2.Printf("%s is not signed\n", file.OutputFile)
}
}
err := filepath.Walk(espPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if fi, _ := os.Stat(path); fi.IsDir() {
return nil
}
// Don't check files we have checked
normalized := strings.Join(strings.Split(path, "/")[2:], "/")
if ok := checked[normalized]; ok {
return nil
}
r, _ := os.Open(path)
defer r.Close()
// We are looking for MS-DOS executables.
// They contain "MZ" as the two first bytes
var header [2]byte
if _, err = io.ReadFull(r, header[:]); err != nil {
return nil
}
if !bytes.Equal(header[:], []byte{0x4d, 0x5a}) {
return nil
}
if VerifyFile(DBCert, path) {
msg2.Printf("%s is signed\n", path)
} else {
warning2.Printf("%s is not signed\n", path)
}
return nil
})
if err != nil {
log.Println(err)
}
}
func Sign(file, output string, enroll bool) {
file, err := filepath.Abs(file)
if err != nil {
log.Fatal(err)
}
if output == "" {
output = file
}
files := ReadFileDatabase(DBPath)
if entry, ok := files[file]; ok {
SignFile(DBKey, DBCert, entry.File, entry.OutputFile)
} else {
SignFile(DBKey, DBCert, file, output)
}
if enroll {
files[file] = &SigningEntry{File: file, OutputFile: output}
WriteFileDatabase(DBPath, files)
}
}
func ListFiles() {
files := ReadFileDatabase(DBPath)
for path, s := range files {
msg.Printf("File: %s", path)
if path != s.OutputFile {
msg2.Printf("Output: %s", s.OutputFile)
}
}
}
func CheckStatus() {
if _, err := os.Stat("/sys/firmware/efi/efivars"); os.IsNotExist(err) {
warning.Println("System is not booted with UEFI!")
os.Exit(1)
}
if sm, err := attributes.ReadEfivars("SetupMode"); err == nil {
if sm.Data[0] == 1 {
warning.Println("Setup Mode: Enabled")
} else {
msg.Println("Setup Mode: Disabled")
}
}
if sb, err := attributes.ReadEfivars("SecureBoot"); err == nil {
if sb.Data[0] == 1 {
msg.Println("Secure Boot: Enabled")
} else {
warning.Println("Secure Boot: Disabled")
}
}
}
func CreateKeys() {
if !CheckIfKeysInitialized(KeysPath) {
msg.Printf("Creating secure boot keys...")
InitializeSecureBootKeys(DatabasePath)
} else {
msg.Printf("Secure boot keys has been created")
}
}
func SyncKeys() {
synced := SBKeySync(KeysPath)
if !synced {
err.Println("Couldn't sync keys")
os.Exit(1)
} else {
msg.Println("Synced keys!")
}
}
func CombineFiles(microcode, initramfs string) *os.File {
tmpFile, e := ioutil.TempFile("/var/tmp", "initramfs-")
if e != nil {
err.Println("Cannot create temporary file", e)
}
one, _ := os.Open(microcode)
defer one.Close()
two, _ := os.Open(initramfs)
defer two.Close()
_, e = io.Copy(tmpFile, one)
if e != nil {
log.Fatalln("failed to append microcode file to output:", err)
}
_, e = io.Copy(tmpFile, two)
if e != nil {
log.Fatalln("failed to append initramfs file to output:", err)
}
return tmpFile
}
func CreateBundle(bundle Bundle) {
if bundle.IntelMicrocode != "" {
tmpFile := CombineFiles(bundle.IntelMicrocode, bundle.Initramfs)
defer os.Remove(tmpFile.Name())
bundle.Initramfs = tmpFile.Name()
}
if bundle.AMDMicrocode != "" {
tmpFile := CombineFiles(bundle.AMDMicrocode, bundle.Initramfs)
defer os.Remove(tmpFile.Name())
bundle.Initramfs = tmpFile.Name()
}
GenerateBundle(&bundle)
}
func GenerateAllBundles() {
msg.Println("Generating EFI bundles....")
bundles := ReadBundleDatabase(BundleDBPath)
for _, bundle := range bundles {
CreateBundle(*bundle)
}
}
func ListBundles() {
bundles := ReadBundleDatabase(BundleDBPath)
for key, bundle := range bundles {
FormatBundle(key, bundle)
}
}