-
Notifications
You must be signed in to change notification settings - Fork 7
/
create.go
194 lines (178 loc) · 6.36 KB
/
create.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
// Copyright 2023 The Armored Witness authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package cmd
import (
"crypto/sha256"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/coreos/go-semver/semver"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/transparency-dev/armored-witness-common/release/firmware/ftlog"
"golang.org/x/exp/maps"
"golang.org/x/mod/sumdb/note"
)
// createCmd represents the create command
var (
createCmd = &cobra.Command{
Use: "create",
Short: "Create a new manifest file",
Long: `This command is used to create a new firmware manifest file.\n\nThe manifest describes important details about the firmware. `,
Run: create,
}
// knownFirmwareTypes is the set of possible values for the firmware_type flag.
knownFirmwareTypes = map[string]struct{}{
ftlog.ComponentApplet: {},
ftlog.ComponentBoot: {},
ftlog.ComponentOS: {},
ftlog.ComponentRecovery: {},
}
)
func init() {
rootCmd.AddCommand(createCmd)
createCmd.Flags().Int("schema_version", 0, "The schema version of the manifest to output.")
createCmd.Flags().String("git_tag", "", "The semantic version of the Trusted Applet release.")
createCmd.Flags().String("git_commit_fingerprint", "", "Hex-encoded SHA-1 commit hash of the git repository when checked out at the specified git_tag.")
createCmd.Flags().String("firmware_file", "", "Path of the firmware ELF file. ")
createCmd.Flags().String("tamago_version", "", "The version of the Tamago (https://github.com/usbarmory/tamago) used to compile the Trusted Applet.")
createCmd.Flags().StringArray("build_env", []string{}, "Environment variables required to reproduce this build.")
createCmd.Flags().String("output_file", "", "The file to write the manifest to. If this is not set, then only print the manifest to stdout.")
createCmd.Flags().String("firmware_type", "", fmt.Sprintf("One of %v ", maps.Keys(knownFirmwareTypes)))
createCmd.Flags().String("hab_target", "", "The devices the --hab_signature is targeting.")
createCmd.Flags().String("hab_signature_file", "", "The HAB signature for the firmware file.")
createCmd.Flags().Bool("raw", false, "If set, the command only emits the raw manifest JSON, it will not sign and encapsulate into a note")
createCmd.Flags().String("private_key_file", "", "The file containing a Note-formatted signer string, used to sign the manifest")
}
func create(cmd *cobra.Command, args []string) {
schemaVersion := requireFlagInt(cmd.Flags(), "schema_version")
gitTag := requireFlagString(cmd.Flags(), "git_tag")
gitCommitFingerprint := requireFlagString(cmd.Flags(), "git_commit_fingerprint")
firmwareFile := requireFlagString(cmd.Flags(), "firmware_file")
tamagoVersion := requireFlagString(cmd.Flags(), "tamago_version")
buildEnvs, err := cmd.Flags().GetStringArray("build_env")
if err != nil {
log.Fatalf("Failed to get []string from build_env")
}
firmwareType := requireFlagString(cmd.Flags(), "firmware_type")
if _, ok := knownFirmwareTypes[firmwareType]; !ok {
log.Fatalf("firmware_type must be one of %v", maps.Keys(knownFirmwareTypes))
}
raw, err := cmd.Flags().GetBool("raw")
if err != nil {
log.Fatal(err)
}
firmwareBytes, err := os.ReadFile(firmwareFile)
if err != nil {
log.Fatalf("Failed to read firmware_file %q: %v", firmwareFile, err)
}
digestBytes := sha256.Sum256(firmwareBytes)
gitSemVer, err := semver.NewVersion(strings.TrimPrefix(gitTag, "v"))
if err != nil {
log.Fatalf("Failed to parse git_tag: %v", err)
}
tamagoSemVer, err := semver.NewVersion(strings.TrimPrefix(tamagoVersion, "v"))
if err != nil {
log.Fatalf("Failed to parse tamago_version: %v", err)
}
r := ftlog.FirmwareRelease{
SchemaVersion: schemaVersion,
Component: firmwareType,
Git: ftlog.Git{
TagName: *gitSemVer,
CommitFingerprint: gitCommitFingerprint,
},
Build: ftlog.Build{
TamagoVersion: *tamagoSemVer,
Envs: buildEnvs,
},
Output: ftlog.Output{
FirmwareDigestSha256: digestBytes[:],
},
}
if firmwareType == ftlog.ComponentBoot || firmwareType == ftlog.ComponentRecovery {
if habTarget := optionalFlagString(cmd.Flags(), "hab_target"); habTarget != "" {
habSigFile := requireFlagString(cmd.Flags(), "hab_signature_file")
habSig, err := os.ReadFile(habSigFile)
if err != nil {
log.Fatalf("Failed to read HAB signature file %q: %v", habSigFile, err)
}
habSigDigest := sha256.Sum256(habSig)
r.HAB = &ftlog.HAB{
Target: habTarget,
SignatureDigestSha256: habSigDigest[:],
}
}
}
b, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Fatal(err)
}
// Note requires the msg ends in a newline.
b = append(b, byte('\n'))
if !raw {
keyFile := requireFlagString(cmd.Flags(), "private_key_file")
signer, err := os.ReadFile(keyFile)
if err != nil {
log.Fatalf("Failed to read private key file: %v", err)
}
b, err = sign(string(signer), b)
if err != nil {
log.Fatalf("Failed to sign manifest: %v", err)
}
}
fmt.Print(string(b))
outputFile, _ := cmd.Flags().GetString("output_file")
if outputFile == "" {
return
}
if err := os.WriteFile(outputFile, b, 0664); err != nil {
log.Fatal(err)
}
}
func requireFlagString(f *pflag.FlagSet, name string) string {
v, err := f.GetString(name)
if err != nil {
log.Fatalf("Getting flag %v: %v", name, err)
}
if v == "" {
log.Fatalf("Flag %v must be specified", name)
}
return v
}
func requireFlagInt(f *pflag.FlagSet, name string) int {
v, err := f.GetInt(name)
if err != nil {
log.Fatalf("Getting flag %v: %v", name, err)
}
return v
}
func optionalFlagString(f *pflag.FlagSet, name string) string {
v, err := f.GetString(name)
if err != nil {
log.Fatalf("Getting flag %v: %v", name, err)
}
return v
}
func sign(sec string, b []byte) ([]byte, error) {
signer, err := note.NewSigner(sec)
if err != nil {
return nil, err
}
t := string(b)
return note.Sign(¬e.Note{Text: t}, signer)
}