-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
115 lines (96 loc) · 2.23 KB
/
config.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
package tpm
import (
"hash/fnv"
"net/http"
"os"
"github.com/google/go-attestation/attest"
)
type config struct {
emulated bool
commandChannel attest.CommandChannelTPM20
seed int64
cacerts []byte
header http.Header
headers map[string]string
systemfallback bool
}
func newConfig() *config {
return &config{headers: make(map[string]string)}
}
// Option is a generic option for TPM configuration
type Option func(c *config) error
// Emulated sets an emulated device in place of a real native TPM device.
// Note, the emulated device is embedded and it is unsafe.
// Should just be used for testing.
var Emulated Option = func(c *config) error {
c.emulated = true
return nil
}
// AppendCustomCAToSystemCA uses the system CA pool as a fallback, appending the custom CA
// to it.
var AppendCustomCAToSystemCA Option = func(c *config) error {
c.systemfallback = true
return nil
}
// WithCAs sets the root CAs for the request
func WithCAs(ca []byte) Option {
return func(c *config) error {
c.cacerts = ca
return nil
}
}
// WithHeader sets a specific header for the request
func WithHeader(header http.Header) Option {
return func(c *config) error {
c.header = header
return nil
}
}
// WithAdditionalHeader adds a key to the request
func WithAdditionalHeader(k, v string) Option {
return func(c *config) error {
c.headers[k] = v
return nil
}
}
// WithSeed sets a permanent seed. Used with TPM emulated device.
func WithSeed(s int64) Option {
return func(c *config) error {
c.seed = s
return nil
}
}
// WithCommandChannel overrides the TPM command channel
func WithCommandChannel(cc attest.CommandChannelTPM20) Option {
return func(c *config) error {
c.commandChannel = cc
return nil
}
}
func hashit(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func tokenize() int64 {
hostname, err := os.Hostname()
if err != nil {
return 0
}
return int64(hashit(hostname))
}
// EmulatedHostSeed generates a seed based on the hostname
var EmulatedHostSeed = func() Option {
return func(c *config) error {
c.seed = tokenize()
return nil
}
}
func (c *config) apply(opts ...Option) error {
for _, o := range opts {
if err := o(c); err != nil {
return err
}
}
return nil
}