/
node.go
639 lines (539 loc) · 18.1 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// 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/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 (
// ErrInvalidRole is the error returned when a node role is invalid.
ErrInvalidRole = errors.New("node: invalid role")
// ErrDuplicateRole is the error returned when a node role is duplicated.
ErrDuplicateRole = errors.New("node: duplicate role")
// 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")
// ErrConstraintViolation the error returned when the TEE attestation
// fails to conform to the optional additional constraints.
ErrConstraintViolation = errors.New("node: TEE constraint violation")
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 = 2
// 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"`
// VRF contains information for this node's participation in VRF
// based elections.
VRF *VRFInfo `json:"vrf,omitempty"`
// DeprecatedBeacon contains information for this node's
// participation in the old PVSS based random beacon protocol.
DeprecatedBeacon cbor.RawMessage `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"`
// SoftwareVersion is the node's oasis-node software version.
SoftwareVersion string `json:"software_version,omitempty"`
}
// RolesMask is Oasis node roles bitmask.
type RolesMask uint32
const (
// RoleComputeWorker is the compute worker role.
RoleComputeWorker RolesMask = 1 << 0
// roleReserved2 is the reserved role (storage role in v1 descriptors).
roleReserved2 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) | roleReserved2
// Human friendly role names.
RoleComputeWorkerName = "compute"
RoleKeyManagerName = "key-manager"
RoleValidatorName = "validator"
RoleConsensusRPCName = "consensus-rpc"
RoleStorageRPCName = "storage-rpc"
rolesMaskStringSep = ","
)
// Roles returns a list of available valid roles.
func Roles() (roles []RolesMask) {
return []RolesMask{
RoleComputeWorker,
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, RoleComputeWorkerName)
}
if m&RoleKeyManager != 0 {
ret = append(ret, RoleKeyManagerName)
}
if m&RoleValidator != 0 {
ret = append(ret, RoleValidatorName)
}
if m&RoleConsensusRPC != 0 {
ret = append(ret, RoleConsensusRPCName)
}
if m&RoleStorageRPC != 0 {
ret = append(ret, RoleStorageRPCName)
}
return strings.Join(ret, rolesMaskStringSep)
}
// MarshalText encodes a RolesMask into text form.
func (m RolesMask) MarshalText() ([]byte, error) {
return []byte(m.String()), nil
}
func checkDuplicateRole(newRole RolesMask, curRoles RolesMask) error {
if curRoles&newRole != 0 {
return fmt.Errorf("%w: '%s'", ErrDuplicateRole, newRole)
}
return nil
}
// UnmarshalText decodes a text slice into a RolesMask.
func (m *RolesMask) UnmarshalText(text []byte) error {
*m = 0
roles := strings.Split(string(text), rolesMaskStringSep)
for _, role := range roles {
switch role {
case RoleComputeWorkerName:
if err := checkDuplicateRole(RoleComputeWorker, *m); err != nil {
return err
}
*m |= RoleComputeWorker
case RoleKeyManagerName:
if err := checkDuplicateRole(RoleKeyManager, *m); err != nil {
return err
}
*m |= RoleKeyManager
case RoleValidatorName:
if err := checkDuplicateRole(RoleValidator, *m); err != nil {
return err
}
*m |= RoleValidator
case RoleConsensusRPCName:
if err := checkDuplicateRole(RoleConsensusRPC, *m); err != nil {
return err
}
*m |= RoleConsensusRPC
case RoleStorageRPCName:
if err := checkDuplicateRole(RoleStorageRPC, *m); err != nil {
return err
}
*m |= RoleStorageRPC
default:
return fmt.Errorf("%w: '%s'", ErrInvalidRole, role)
}
}
return nil
}
// UnmarshalCBOR is a custom deserializer that handles both v1 and v2 Node structures.
func (n *Node) UnmarshalCBOR(data []byte) error {
// Determine Entity structure version.
v, err := cbor.GetVersion(data)
if err != nil {
return err
}
switch v {
case 1:
// Old version had an extra supported role (the storage role).
type nv2 Node
if err := cbor.Unmarshal(data, (*nv2)(n)); err != nil {
return err
}
// Convert into new format.
n.Versioned = cbor.NewVersioned(2)
n.Roles = n.Roles & ^roleReserved2 // Clear old storage role.
return nil
case 2:
// New version, call the default unmarshaler.
type nv2 Node
return cbor.Unmarshal(data, (*nv2)(n))
default:
return fmt.Errorf("invalid node descriptor version: %d", v)
}
}
// 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,
)
}
}
// Make sure that a node has at least one valid role.
switch {
case n.Roles == 0:
return fmt.Errorf("no roles specified")
case n.HasRoles(RoleReserved):
return fmt.Errorf("invalid role specified")
}
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
}
// HasRuntime returns true iff the node supports a runtime (ignoring version).
func (n *Node) HasRuntime(id common.Namespace) bool {
for _, rt := range n.Runtimes {
if rt.ID.Equal(&id) {
return true
}
}
return false
}
// GetRuntime searches for an existing supported runtime descriptor
// in Runtimes with the specified version and returns it.
func (n *Node) GetRuntime(id common.Namespace, version version.Version) *Runtime {
for _, rt := range n.Runtimes {
if !rt.ID.Equal(&id) {
continue
}
if rt.Version != version {
continue
}
return rt
}
return nil
}
// AddOrUpdateRuntime searches for an existing supported runtime descriptor
// in Runtimes with the specified version and returns it. In case a
// runtime descriptor for the given runtime and version 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, version version.Version) *Runtime {
if rt := n.GetRuntime(id, version); rt != nil {
return rt
}
rt := &Runtime{
ID: id,
Version: version,
}
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"`
}
// VRFInfo contains information for this node's participation in
// VRF based elections.
type VRFInfo struct {
// ID is the unique identifier of the node used to generate VRF proofs.
ID signature.PublicKey `json:"id"`
}
// 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"`
}
// SGXConstraints are the Intel SGX TEE constraints.
type SGXConstraints struct {
// Enclaves is the allowed MRENCLAVE/MRSIGNER pairs.
Enclaves []sgx.EnclaveIdentity `json:"enclaves,omitempty"`
// AllowedQuoteStatuses are the allowed quote statuses for the node
// to be scheduled as a compute worker.
//
// Note: QuoteOK and QuoteSwHardeningNeeded are ALWAYS allowed, and do not need to be specified.
AllowedQuoteStatuses []ias.ISVEnclaveQuoteStatus `json:"allowed_quote_statuses,omitempty"`
}
func (constraints *SGXConstraints) quoteStatusAllowed(avr *ias.AttestationVerificationReport) bool {
status := avr.ISVEnclaveQuoteStatus
// Always allow "OK" and "SW_HARDENING_NEEDED".
if status == ias.QuoteOK || status == ias.QuoteSwHardeningNeeded {
return true
}
// Search through the constraints to see if the AVR quote status is
// explicitly allowed.
for _, v := range constraints.AllowedQuoteStatuses {
if v == status {
return true
}
}
return false
}
// 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 SGXConstraints
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
}
// Ensure that the quote status is acceptable.
if !cs.quoteStatusAllowed(avr) {
return ErrConstraintViolation
}
// 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
}