/
main.go
230 lines (196 loc) · 6.67 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
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
// Copyright (c) 2021 Fraunhofer AISEC
// Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
//
// 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 main
// Install github packages with "go get [url]"
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"encoding/json"
"flag"
// local modules
ar "github.com/Fraunhofer-AISEC/cmc/attestationreport"
"github.com/Fraunhofer-AISEC/cmc/internal"
"github.com/Fraunhofer-AISEC/cmc/provclient"
"github.com/Fraunhofer-AISEC/cmc/snpdriver"
"github.com/Fraunhofer-AISEC/cmc/swdriver"
"github.com/Fraunhofer-AISEC/cmc/tpmdriver"
"github.com/sirupsen/logrus"
)
type config struct {
Addr string `json:"addr"`
ProvServerAddr string `json:"provServerAddr"`
LocalPath string `json:"localPath"`
FetchMetadata bool `json:"fetchMetadata"`
MeasurementInterfaces []string `json:"measurementInterfaces"` // TPM, SNP
SigningInterface string `json:"signingInterface"` // TPM, SW
UseIma bool `json:"useIma"`
ImaPcr int32 `json:"imaPcr"`
KeyConfig string `json:"keyConfig,omitempty"` // RSA2048 RSA4096 EC256 EC384 EC521
Serialization string `json:"serialization"` // JSON, CBOR
Api string `json:"api"` // gRPC, CoAP
}
var log = logrus.WithField("service", "cmcd")
func loadConfig(configFile string) (*config, error) {
log.Infof("Using Config File %v", configFile)
data, err := os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to read cmcd config file %v: %v", configFile, err)
}
// Default configuration
c := &config{
KeyConfig: "EC256",
}
// Obtain custom configuration
err = json.Unmarshal(data, c)
if err != nil {
return nil, fmt.Errorf("failed to parse cmcd config: %v", err)
}
c.LocalPath = internal.GetFilePath(c.LocalPath, filepath.Dir(configFile))
// Check measurement, signing and serializer interface
for _, m := range c.MeasurementInterfaces {
if !strings.EqualFold(m, "TPM") && !strings.EqualFold(m, "SNP") {
return nil, fmt.Errorf("measurement interface of type %v not supported", m)
}
}
if !strings.EqualFold(c.SigningInterface, "TPM") && !strings.EqualFold(c.SigningInterface, "SW") {
return nil, fmt.Errorf("signing Interface of type %v not supported", c.SigningInterface)
}
if !strings.EqualFold(c.Serialization, "CBOR") && !strings.EqualFold(c.Serialization, "JSON") {
return nil, fmt.Errorf("serialization interface of type %v not supported", c.Serialization)
}
printConfig(c)
return c, nil
}
func printConfig(c *config) {
log.Info("Using the following configuration:")
log.Info("\tCMC Listen Address : ", c.Addr)
log.Info("\tProvisioning Server URL : ", c.ProvServerAddr)
log.Info("\tLocal Storage Path : ", c.LocalPath)
log.Info("\tFetch Metadata : ", c.FetchMetadata)
log.Info("\tUse IMA : ", c.UseIma)
log.Info("\tIMA PCR : ", c.ImaPcr)
log.Info("\tSerialization : ", c.Serialization)
log.Info("\tAPI : ", c.Api)
}
func main() {
logrus.SetLevel(logrus.TraceLevel)
log.Info("Starting cmcd")
configFile := flag.String("config", "", "configuration file")
metadataAddr := flag.String("addr", "", "metadata server address")
flag.Parse()
if *configFile == "" {
log.Error("Config file not specified with -config. Abort")
return
}
c, err := loadConfig(*configFile)
if err != nil {
log.Errorf("failed to load config: %v", err)
return
}
provConfig := &provclient.Config{
FetchMetadata: c.FetchMetadata,
StoreMetadata: true,
LocalPath: c.LocalPath,
RemoteAddr: *metadataAddr,
}
metadata, err := provclient.ProvisionMetadata(provConfig)
if err != nil {
log.Errorf("Failed to provision metadata: %v", err)
return
}
var tpm *tpmdriver.Tpm
var snp *snpdriver.Snp
var sw *swdriver.Sw
var jsonSerializer ar.JsonSerializer
var cborSerializer ar.CborSerializer
measurements := make([]ar.Measurement, 0)
var signer ar.Signer
var serializer ar.Serializer
if strings.EqualFold(c.Serialization, "JSON") {
log.Info("Using JSON/JWS as serialization interface")
serializer = jsonSerializer
}
if strings.EqualFold(c.Serialization, "CBOR") {
log.Info("Using CBOR/COSE as serialization interface")
serializer = cborSerializer
}
if strings.EqualFold(c.SigningInterface, "SW") {
log.Warn("Using unsafe swdriver as Signing Interface")
swConfig := swdriver.Config{
StoragePath: path.Join(c.LocalPath, "internal"),
Url: c.ProvServerAddr,
Metadata: metadata,
Serializer: serializer,
}
sw, err = swdriver.NewSwDriver(swConfig)
if err != nil {
log.Errorf("failed to create new SW driver: %v", err)
return
}
signer = sw
}
if strings.EqualFold(c.SigningInterface, "TPM") || internal.Contains("TPM", c.MeasurementInterfaces) {
tpmConfig := &tpmdriver.Config{
StoragePath: path.Join(c.LocalPath, "internal"),
ServerAddr: c.ProvServerAddr,
KeyConfig: c.KeyConfig,
Metadata: metadata,
UseIma: c.UseIma,
ImaPcr: c.ImaPcr,
Serializer: serializer,
}
tpm, err = tpmdriver.NewTpm(tpmConfig)
if err != nil {
log.Errorf("Failed to create new TPM driver: %v", err)
return
}
defer tpmdriver.CloseTpm()
}
if internal.Contains("TPM", c.MeasurementInterfaces) {
log.Info("Using TPM as Measurement Interface")
measurements = append(measurements, tpm)
}
if strings.EqualFold(c.SigningInterface, "TPM") {
log.Info("Using TPM as Signing Interface")
signer = tpm
}
if internal.Contains("SNP", c.MeasurementInterfaces) {
log.Info("Using SNP as Measurement Interface")
snpConfig := snpdriver.Config{
Url: c.ProvServerAddr,
}
snp, err = snpdriver.NewSnpDriver(snpConfig)
if err != nil {
log.Errorf("failed to create new SNP driver: %v", err)
return
}
measurements = append(measurements, snp)
}
serverConfig := &ServerConfig{
Metadata: metadata,
MeasurementInterfaces: measurements,
Signer: signer,
Serializer: serializer,
}
server, ok := servers[strings.ToLower(c.Api)]
if !ok {
log.Errorf("API '%v' is not implemented", c.Api)
return
}
server.Serve(c.Addr, serverConfig)
}