/
node.go
481 lines (401 loc) · 13.6 KB
/
node.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Package node implements common node identity routines.
//
// This package is meant for interoperability with the rust compute worker.
package node
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
"time"
"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/crypto/pvss"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
"github.com/oasisprotocol/oasis-core/go/common/sgx"
"github.com/oasisprotocol/oasis-core/go/common/sgx/ias"
"github.com/oasisprotocol/oasis-core/go/common/version"
)
var (
// ErrInvalidTEEHardware is the error returned when a TEE hardware
// implementation is invalid.
ErrInvalidTEEHardware = errors.New("node: invalid TEE implementation")
// ErrRAKHashMismatch is the error returned when the TEE attestation
// does not contain the node's RAK hash.
ErrRAKHashMismatch = errors.New("node: RAK hash mismatch")
// ErrBadEnclaveIdentity is the error returned when the TEE enclave
// identity doesn't match the required values.
ErrBadEnclaveIdentity = errors.New("node: bad TEE enclave identity")
teeHashContext = []byte("oasis-core/node: TEE RAK binding")
_ prettyprint.PrettyPrinter = (*MultiSignedNode)(nil)
)
const (
// LatestNodeDescriptorVersion is the latest node descriptor version that should be used for all
// new descriptors. Using earlier versions may be rejected.
LatestNodeDescriptorVersion = 1
// Minimum and maximum descriptor versions that are allowed.
minNodeDescriptorVersion = 1
maxNodeDescriptorVersion = LatestNodeDescriptorVersion
)
// Node represents public connectivity information about an Oasis node.
type Node struct { // nolint: maligned
cbor.Versioned
// ID is the public key identifying the node.
ID signature.PublicKey `json:"id"`
// EntityID is the public key identifying the Entity controlling
// the node.
EntityID signature.PublicKey `json:"entity_id"`
// Expiration is the epoch in which this node's commitment expires.
Expiration uint64 `json:"expiration"`
// TLS contains information for connecting to this node via TLS.
TLS TLSInfo `json:"tls"`
// P2P contains information for connecting to this node via P2P.
P2P P2PInfo `json:"p2p"`
// Consensus contains information for connecting to this node as a
// consensus member.
Consensus ConsensusInfo `json:"consensus"`
// Beacon contains information for this node's participation
// in the random beacon protocol.
//
// TODO: This is optional for now, make mandatory once enough
// nodes provide this field.
Beacon *BeaconInfo `json:"beacon,omitempty"`
// Runtimes are the node's runtimes.
Runtimes []*Runtime `json:"runtimes"`
// Roles is a bitmask representing the node roles.
Roles RolesMask `json:"roles"`
}
// RolesMask is Oasis node roles bitmask.
type RolesMask uint32
const (
// RoleComputeWorker is the compute worker role.
RoleComputeWorker RolesMask = 1 << 0
// RoleStorageWorker is the storage worker role.
RoleStorageWorker RolesMask = 1 << 1
// RoleKeyManager is the the key manager role.
RoleKeyManager RolesMask = 1 << 2
// RoleValidator is the validator role.
RoleValidator RolesMask = 1 << 3
// RoleConsensusRPC is the public consensus RPC services worker role.
RoleConsensusRPC RolesMask = 1 << 4
// RoleStorageRPC is the public storage RPC services worker role.
RoleStorageRPC RolesMask = 1 << 5
// RoleReserved are all the bits of the Oasis node roles bitmask
// that are reserved and must not be used.
RoleReserved RolesMask = ((1 << 32) - 1) & ^((RoleStorageRPC << 1) - 1)
)
// Roles returns a list of available valid roles.
func Roles() (roles []RolesMask) {
return []RolesMask{
RoleComputeWorker,
RoleStorageWorker,
RoleKeyManager,
RoleValidator,
RoleConsensusRPC,
RoleStorageRPC,
}
}
// IsSingleRole returns true if RolesMask encodes a single valid role.
func (m RolesMask) IsSingleRole() bool {
// Ensures exactly one bit is set, and the set bit is a valid role.
return m != 0 && m&(m-1) == 0 && m&RoleReserved == 0
}
func (m RolesMask) String() string {
if m&RoleReserved != 0 {
return "[invalid roles]"
}
var ret []string
if m&RoleComputeWorker != 0 {
ret = append(ret, "compute")
}
if m&RoleStorageWorker != 0 {
ret = append(ret, "storage")
}
if m&RoleKeyManager != 0 {
ret = append(ret, "key-manager")
}
if m&RoleValidator != 0 {
ret = append(ret, "validator")
}
if m&RoleConsensusRPC != 0 {
ret = append(ret, "consensus-rpc")
}
if m&RoleStorageRPC != 0 {
ret = append(ret, "storage-rpc")
}
return strings.Join(ret, ",")
}
// ValidateBasic performs basic descriptor validity checks.
func (n *Node) ValidateBasic(strictVersion bool) error {
v := n.Versioned.V
switch strictVersion {
case true:
// Only the latest version is allowed.
if v != LatestNodeDescriptorVersion {
return fmt.Errorf("invalid node descriptor version (expected: %d got: %d)",
LatestNodeDescriptorVersion,
v,
)
}
case false:
// A range of versions is allowed.
if v < minNodeDescriptorVersion || v > maxNodeDescriptorVersion {
return fmt.Errorf("invalid node descriptor version (min: %d max: %d)",
minNodeDescriptorVersion,
maxNodeDescriptorVersion,
)
}
}
return nil
}
// AddRoles adds a new node role to the existing roles mask.
func (n *Node) AddRoles(r RolesMask) {
n.Roles |= r
}
// HasRoles checks if the node has the specified roles.
func (n *Node) HasRoles(r RolesMask) bool {
return n.Roles&r != 0
}
// OnlyHasRoles checks if the node only has the specified roles and no others.
func (n *Node) OnlyHasRoles(r RolesMask) bool {
return n.Roles == r
}
// IsExpired returns true if the node expiration epoch is strictly smaller
// than the passed (current) epoch.
func (n *Node) IsExpired(epoch uint64) bool {
return n.Expiration < epoch
}
// GetRuntime searches for an existing supported runtime descriptor in Runtimes and returns it.
func (n *Node) GetRuntime(id common.Namespace) *Runtime {
for _, rt := range n.Runtimes {
if !rt.ID.Equal(&id) {
continue
}
return rt
}
return nil
}
// AddOrUpdateRuntime searches for an existing supported runtime descriptor in Runtimes and returns
// it. In case a runtime descriptor for the given runtime doesn't exist yet, a new one is created
// appended to the list of supported runtimes and returned.
func (n *Node) AddOrUpdateRuntime(id common.Namespace) *Runtime {
if rt := n.GetRuntime(id); rt != nil {
return rt
}
rt := &Runtime{ID: id}
n.Runtimes = append(n.Runtimes, rt)
return rt
}
// Runtime represents the runtimes supported by a given Oasis node.
type Runtime struct {
// ID is the public key identifying the runtime.
ID common.Namespace `json:"id"`
// Version is the version of the runtime.
Version version.Version `json:"version"`
// Capabilities are the node's capabilities for a given runtime.
Capabilities Capabilities `json:"capabilities"`
// ExtraInfo is the extra per node + per runtime opaque data associated
// with the current instance.
ExtraInfo []byte `json:"extra_info"`
}
// TLSInfo contains information for connecting to this node via TLS.
type TLSInfo struct {
// PubKey is the public key used for establishing TLS connections.
PubKey signature.PublicKey `json:"pub_key"`
// NextPubKey is the public key that will be used for establishing TLS connections after
// certificate rotation (if enabled).
NextPubKey signature.PublicKey `json:"next_pub_key,omitempty"`
// Addresses is the list of addresses at which the node can be reached.
Addresses []TLSAddress `json:"addresses"`
}
// Equal compares vs another TLSInfo for equality.
func (t *TLSInfo) Equal(other *TLSInfo) bool {
if !t.PubKey.Equal(other.PubKey) {
return false
}
if !t.NextPubKey.Equal(other.NextPubKey) {
return false
}
if len(t.Addresses) != len(other.Addresses) {
return false
}
for i, ca := range t.Addresses {
if !ca.Equal(&other.Addresses[i]) {
return false
}
}
return true
}
// P2PInfo contains information for connecting to this node via P2P transport.
type P2PInfo struct {
// ID is the unique identifier of the node on the P2P transport.
ID signature.PublicKey `json:"id"`
// Addresses is the list of addresses at which the node can be reached.
Addresses []Address `json:"addresses"`
}
// ConsensusInfo contains information for connecting to this node as a
// consensus member.
type ConsensusInfo struct {
// ID is the unique identifier of the node as a consensus member.
ID signature.PublicKey `json:"id"`
// Addresses is the list of addresses at which the node can be reached.
Addresses []ConsensusAddress `json:"addresses"`
}
// BeaconInfo contains information for this node's participation in
// the random beacon protocol.
type BeaconInfo struct {
// Point is the elliptic curve point used for the PVSS algorithm.
Point pvss.Point `json:"point"`
}
// Capabilities represents a node's capabilities.
type Capabilities struct {
// TEE is the capability of a node executing batches in a TEE.
TEE *CapabilityTEE `json:"tee,omitempty"`
}
// TEEHardware is a TEE hardware implementation.
type TEEHardware uint8
// TEE Hardware implementations.
const (
// TEEHardwareInvalid is a non-TEE implementation.
TEEHardwareInvalid TEEHardware = 0
// TEEHardwareIntelSGX is an Intel SGX TEE implementation.
TEEHardwareIntelSGX TEEHardware = 1
// TEEHardwareReserved is the first reserved hardware implementation
// identifier. All equal or greater identifiers are reserved.
TEEHardwareReserved TEEHardware = TEEHardwareIntelSGX + 1
teeInvalid = "invalid"
teeIntelSGX = "intel-sgx"
)
// String returns the string representation of a TEEHardware.
func (h TEEHardware) String() string {
switch h {
case TEEHardwareInvalid:
return teeInvalid
case TEEHardwareIntelSGX:
return teeIntelSGX
default:
return "[unsupported TEEHardware]"
}
}
// FromString deserializes a string into a TEEHardware.
func (h *TEEHardware) FromString(str string) error {
switch strings.ToLower(str) {
case "", teeInvalid:
*h = TEEHardwareInvalid
case teeIntelSGX:
*h = TEEHardwareIntelSGX
default:
return ErrInvalidTEEHardware
}
return nil
}
// CapabilityTEE represents the node's TEE capability.
type CapabilityTEE struct {
// TEE hardware type.
Hardware TEEHardware `json:"hardware"`
// Runtime attestation key.
RAK signature.PublicKey `json:"rak"`
// Attestation.
Attestation []byte `json:"attestation"`
}
// RAKHash computes the expected AVR report hash bound to a given public RAK.
func RAKHash(rak signature.PublicKey) hash.Hash {
hData := make([]byte, 0, len(teeHashContext)+signature.PublicKeySize)
hData = append(hData, teeHashContext...)
hData = append(hData, rak[:]...)
return hash.NewFromBytes(hData)
}
// Verify verifies the node's TEE capabilities, at the provided timestamp.
func (c *CapabilityTEE) Verify(ts time.Time, constraints []byte) error {
rakHash := RAKHash(c.RAK)
switch c.Hardware {
case TEEHardwareIntelSGX:
var avrBundle ias.AVRBundle
if err := cbor.Unmarshal(c.Attestation, &avrBundle); err != nil {
return err
}
avr, err := avrBundle.Open(ias.IntelTrustRoots, ts)
if err != nil {
return err
}
// Extract the original ISV quote.
q, err := avr.Quote()
if err != nil {
return err
}
// Ensure that the MRENCLAVE/MRSIGNER match what is specified
// in the TEE-specific constraints field.
var cs sgx.Constraints
if err := cbor.Unmarshal(constraints, &cs); err != nil {
return fmt.Errorf("node: malformed SGX constraints: %w", err)
}
var eidValid bool
for _, eid := range cs.Enclaves {
eidMrenclave := eid.MrEnclave
eidMrsigner := eid.MrSigner
if bytes.Equal(eidMrenclave[:], q.Report.MRENCLAVE[:]) && bytes.Equal(eidMrsigner[:], q.Report.MRSIGNER[:]) {
eidValid = true
break
}
}
if !eidValid {
return ErrBadEnclaveIdentity
}
// Ensure that the ISV quote includes the hash of the node's
// RAK.
var avrRAKHash hash.Hash
_ = avrRAKHash.UnmarshalBinary(q.Report.ReportData[:hash.Size])
if !rakHash.Equal(&avrRAKHash) {
return ErrRAKHashMismatch
}
// The last 32 bytes of the quote ReportData are deliberately
// ignored.
return nil
default:
return ErrInvalidTEEHardware
}
}
// String returns a string representation of itself.
func (n *Node) String() string {
return "<Node id=" + n.ID.String() + ">"
}
// MultiSignedNode is a multi-signed blob containing a CBOR-serialized Node.
type MultiSignedNode struct {
signature.MultiSigned
}
// Open first verifies the blob signatures and then unmarshals the blob.
func (s *MultiSignedNode) Open(context signature.Context, node *Node) error {
return s.MultiSigned.Open(context, node)
}
// PrettyPrint writes a pretty-printed representation of the type
// to the given writer.
func (s MultiSignedNode) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
pt, err := s.PrettyType()
if err != nil {
fmt.Fprintf(w, "%s<error: %s>\n", prefix, err)
return
}
pt.(prettyprint.PrettyPrinter).PrettyPrint(ctx, prefix, w)
}
// PrettyType returns a representation of the type that can be used for pretty printing.
func (s MultiSignedNode) PrettyType() (interface{}, error) {
var n Node
if err := cbor.Unmarshal(s.MultiSigned.Blob, &n); err != nil {
return nil, fmt.Errorf("malformed signed blob: %w", err)
}
return signature.NewPrettyMultiSigned(s.MultiSigned, n)
}
// MultiSignNode serializes the Node and multi-signs the result.
func MultiSignNode(signers []signature.Signer, context signature.Context, node *Node) (*MultiSignedNode, error) {
multiSigned, err := signature.SignMultiSigned(signers, context, node)
if err != nil {
return nil, err
}
return &MultiSignedNode{
MultiSigned: *multiSigned,
}, nil
}