-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
180 lines (158 loc) · 4.35 KB
/
main.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
// Copyright 2024 Edgeless Systems GmbH
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"context"
"encoding/hex"
"fmt"
"os"
"github.com/edgelesssys/contrast/tdx-measure/rtmr"
"github.com/edgelesssys/contrast/tdx-measure/tdvf"
"github.com/spf13/cobra"
)
var version = "0.0.0-dev"
func main() {
if err := execute(); err != nil {
os.Exit(1)
}
}
func execute() error {
cmd := newRootCmd()
return cmd.ExecuteContext(context.Background())
}
func newRootCmd() *cobra.Command {
root := &cobra.Command{
Version: version,
Use: "tdx-measure",
Short: "tdx-measure",
}
root.SetOut(os.Stdout)
root.InitDefaultVersionFlag()
root.AddCommand(
newMrTdCmd(),
newRtMrCmd(),
)
return root
}
// newMrTdCmd creates the tdx-measure mrtd subcommand.
func newMrTdCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "mrtd -f OVMF.fd",
Short: "calculate the MRTD for a firmware file",
Long: `Calculate the MRTD for a firmware file.
This will parse the firmware according to the TDX Virtual Firmware Design Guide and
pre-calculate the MRTD.`,
Args: cobra.NoArgs,
RunE: runMrTd,
}
cmd.Flags().StringP("firmware", "f", "OVMF.fd", "path to firmware file")
if err := cmd.MarkFlagFilename("firmware", "fd"); err != nil {
panic(err)
}
return cmd
}
func runMrTd(cmd *cobra.Command, _ []string) error {
firmwarePath, err := cmd.Flags().GetString("firmware")
if err != nil {
return fmt.Errorf("can't get firmware arg: %w", err)
}
firmware, err := os.ReadFile(firmwarePath)
if err != nil {
return fmt.Errorf("can't read firmware file: %w", err)
}
digest, err := tdvf.CalculateMrTd(firmware)
if err != nil {
return fmt.Errorf("can't calculate MRTD for firmware: %w", err)
}
hexDigest := hex.EncodeToString(digest[:])
fmt.Print(hexDigest)
return nil
}
// newRtMrCmd creates the tdx-measure rtmr subcommand.
func newRtMrCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "rtmr -f OVMF.fd -k bzImage [0|1|2|3]",
Short: "calculate the RTMR for a firmware and kernel file",
Long: `Calculate the RTMR for a firmware and kernel file.
This will parse the firmware according to the TDX Virtual Firmware Design Guide
and/or hash the kernel and pre-calculate a given RTMR.`,
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
ValidArgs: []string{"0", "1", "2", "3"},
RunE: runRtMr,
}
cmd.Flags().StringP("firmware", "f", "OVMF.fd", "path to firmware file")
if err := cmd.MarkFlagFilename("firmware", "fd"); err != nil {
panic(err)
}
cmd.Flags().StringP("kernel", "k", "bzImage", "path to kernel file")
if err := cmd.MarkFlagFilename("kernel"); err != nil {
panic(err)
}
cmd.Flags().StringP("initrd", "i", "initrd.zst", "path to initrd file")
if err := cmd.MarkFlagFilename("initrd"); err != nil {
panic(err)
}
cmd.Flags().StringP("cmdline", "c", "", "kernel command line")
return cmd
}
func runRtMr(cmd *cobra.Command, args []string) error {
var digest [48]byte
switch args[0] {
case "0":
firmwarePath, err := cmd.Flags().GetString("firmware")
if err != nil {
return err
}
firmware, err := os.ReadFile(firmwarePath)
if err != nil {
return fmt.Errorf("can't read firmware file: %w", err)
}
digest, err = rtmr.CalcRtmr0(firmware)
if err != nil {
return fmt.Errorf("can't calculate RTMR 0: %w", err)
}
case "1":
kernelPath, err := cmd.Flags().GetString("kernel")
if err != nil {
return err
}
kernel, err := os.ReadFile(kernelPath)
if err != nil {
return fmt.Errorf("can't read kernel file: %w", err)
}
initrdPath, err := cmd.Flags().GetString("initrd")
if err != nil {
return err
}
initrd, err := os.ReadFile(initrdPath)
if err != nil {
return fmt.Errorf("can't read initrd file: %w", err)
}
digest, err = rtmr.CalcRtmr1(kernel, initrd)
if err != nil {
return fmt.Errorf("can't calculate RTMR 1: %w", err)
}
case "2":
cmdLine, err := cmd.Flags().GetString("cmdline")
if err != nil {
return err
}
initrdPath, err := cmd.Flags().GetString("initrd")
if err != nil {
return err
}
initrd, err := os.ReadFile(initrdPath)
if err != nil {
return fmt.Errorf("can't read initrd file: %w", err)
}
digest, err = rtmr.CalcRtmr2(cmdLine, initrd)
if err != nil {
return fmt.Errorf("can't calculate RTMR 2: %w", err)
}
case "3":
digest = [48]byte{}
}
hexDigest := hex.EncodeToString(digest[:])
fmt.Print(hexDigest)
return nil
}