-
Notifications
You must be signed in to change notification settings - Fork 107
/
keymanager.go
325 lines (281 loc) · 8.92 KB
/
keymanager.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
package keymanager
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/tendermint/tendermint/abci/types"
"golang.org/x/crypto/sha3"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/node"
"github.com/oasisprotocol/oasis-core/go/consensus/api/transaction"
tmapi "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
keymanagerState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/keymanager/state"
registryapp "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry"
registryState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/registry/state"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
"github.com/oasisprotocol/oasis-core/go/keymanager/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
)
var emptyHashSha3 = sha3.Sum256(nil)
type keymanagerApplication struct {
state tmapi.ApplicationState
}
func (app *keymanagerApplication) Name() string {
return AppName
}
func (app *keymanagerApplication) ID() uint8 {
return AppID
}
func (app *keymanagerApplication) Methods() []transaction.MethodName {
return api.Methods
}
func (app *keymanagerApplication) Blessed() bool {
return false
}
func (app *keymanagerApplication) Dependencies() []string {
return []string{registryapp.AppName}
}
func (app *keymanagerApplication) OnRegister(state tmapi.ApplicationState, md tmapi.MessageDispatcher) {
app.state = state
}
func (app *keymanagerApplication) OnCleanup() {}
func (app *keymanagerApplication) BeginBlock(ctx *tmapi.Context, request types.RequestBeginBlock) error {
if changed, epoch := app.state.EpochChanged(ctx); changed {
return app.onEpochChange(ctx, epoch)
}
return nil
}
func (app *keymanagerApplication) ExecuteMessage(ctx *tmapi.Context, kind, msg interface{}) (interface{}, error) {
return nil, fmt.Errorf("keymanager: unexpected message")
}
func (app *keymanagerApplication) ExecuteTx(ctx *tmapi.Context, tx *transaction.Transaction) error {
state := keymanagerState.NewMutableState(ctx.State())
ctx.SetPriority(AppPriority)
switch tx.Method {
case api.MethodUpdatePolicy:
var sigPol api.SignedPolicySGX
if err := cbor.Unmarshal(tx.Body, &sigPol); err != nil {
return err
}
return app.updatePolicy(ctx, state, &sigPol)
default:
return fmt.Errorf("keymanager: invalid method: %s", tx.Method)
}
}
func (app *keymanagerApplication) EndBlock(ctx *tmapi.Context, request types.RequestEndBlock) (types.ResponseEndBlock, error) {
return types.ResponseEndBlock{}, nil
}
func (app *keymanagerApplication) onEpochChange(ctx *tmapi.Context, epoch beacon.EpochTime) error {
// Query the runtime and node lists.
regState := registryState.NewMutableState(ctx.State())
runtimes, _ := regState.Runtimes(ctx)
nodes, _ := regState.Nodes(ctx)
registry.SortNodeList(nodes)
params, err := regState.ConsensusParameters(ctx)
if err != nil {
return fmt.Errorf("failed to get consensus parameters: %w", err)
}
var stakeAcc *stakingState.StakeAccumulatorCache
if !params.DebugBypassStake {
stakeAcc, err = stakingState.NewStakeAccumulatorCache(ctx)
if err != nil {
return fmt.Errorf("failed to create stake accumulator cache: %w", err)
}
defer stakeAcc.Discard()
}
// Recalculate all the key manager statuses.
//
// Note: This assumes that once a runtime is registered, it never expires.
var toEmit []*api.Status
state := keymanagerState.NewMutableState(ctx.State())
for _, rt := range runtimes {
if rt.Kind != registry.KindKeyManager {
continue
}
// Suspend the runtime in case the registering entity no longer has enough stake to cover
// the entity and runtime deposits.
if !params.DebugBypassStake && rt.GovernanceModel != registry.GovernanceConsensus {
acctAddr := rt.StakingAddress()
if acctAddr == nil {
// This should never happen.
ctx.Logger().Error("unknown runtime governance model",
"rt_id", rt.ID,
"gov_model", rt.GovernanceModel,
)
return fmt.Errorf("unknown runtime governance model on runtime %s: %s", rt.ID, rt.GovernanceModel)
}
if err = stakeAcc.CheckStakeClaims(*acctAddr); err != nil {
ctx.Logger().Debug("insufficient stake for key manager runtime operation",
"err", err,
"entity", rt.EntityID,
"account", *acctAddr,
)
// Suspend runtime.
if err := regState.SuspendRuntime(ctx, rt.ID); err != nil {
return err
}
continue
}
}
var forceEmit bool
oldStatus, err := state.Status(ctx, rt.ID)
switch err {
case nil:
case api.ErrNoSuchStatus:
// This must be a new key manager runtime.
forceEmit = true
oldStatus = &api.Status{
ID: rt.ID,
}
default:
// This is fatal, as it suggests state corruption.
ctx.Logger().Error("failed to query key manager status",
"id", rt.ID,
"err", err,
)
return fmt.Errorf("failed to query key manager status: %w", err)
}
newStatus := app.generateStatus(ctx, rt, oldStatus, nodes, params)
if forceEmit || !bytes.Equal(cbor.Marshal(oldStatus), cbor.Marshal(newStatus)) {
ctx.Logger().Debug("status updated",
"id", newStatus.ID,
"is_initialized", newStatus.IsInitialized,
"is_secure", newStatus.IsSecure,
"checksum", hex.EncodeToString(newStatus.Checksum),
"nodes", newStatus.Nodes,
)
// Set, enqueue for emit.
if err := state.SetStatus(ctx, newStatus); err != nil {
return fmt.Errorf("failed to set key manager status: %w", err)
}
toEmit = append(toEmit, newStatus)
}
}
// Note: It may be a good idea to sweep statuses that don't have runtimes,
// but as runtime registrations last forever, so this shouldn't be possible.
// Emit the update event if required.
if len(toEmit) > 0 {
ctx.EmitEvent(tmapi.NewEventBuilder(app.Name()).TypedAttribute(&api.StatusUpdateEvent{
Statuses: toEmit,
}))
}
return nil
}
func (app *keymanagerApplication) generateStatus(
ctx *tmapi.Context,
kmrt *registry.Runtime,
oldStatus *api.Status,
nodes []*node.Node,
params *registry.ConsensusParameters,
) *api.Status {
status := &api.Status{
ID: kmrt.ID,
IsInitialized: oldStatus.IsInitialized,
IsSecure: oldStatus.IsSecure,
Checksum: oldStatus.Checksum,
Policy: oldStatus.Policy,
}
var rawPolicy []byte
if status.Policy != nil {
rawPolicy = cbor.Marshal(status.Policy)
}
policyHash := sha3.Sum256(rawPolicy)
for _, n := range nodes {
if !n.HasRoles(node.RoleKeyManager) {
continue
}
var nodeRt *node.Runtime
for _, rt := range n.Runtimes {
if rt.ID.Equal(&kmrt.ID) {
nodeRt = rt
break
}
}
if nodeRt == nil {
continue
}
var teeOk bool
if nodeRt.Capabilities.TEE == nil {
teeOk = kmrt.TEEHardware == node.TEEHardwareInvalid
} else {
teeOk = kmrt.TEEHardware == nodeRt.Capabilities.TEE.Hardware
}
if !teeOk {
ctx.Logger().Error("TEE hardware mismatch",
"id", kmrt.ID,
"node_id", n.ID,
)
continue
}
initResponse, err := api.VerifyExtraInfo(ctx.Logger(), n.ID, kmrt, nodeRt, ctx.Now(), uint64(ctx.BlockHeight()), params)
if err != nil {
ctx.Logger().Error("failed to validate ExtraInfo",
"err", err,
"id", kmrt.ID,
"node_id", n.ID,
)
continue
}
var nodePolicyHash [api.ChecksumSize]byte
switch len(initResponse.PolicyChecksum) {
case 0:
nodePolicyHash = emptyHashSha3
case api.ChecksumSize:
copy(nodePolicyHash[:], initResponse.PolicyChecksum)
default:
ctx.Logger().Error("failed to parse policy checksum",
"err", err,
"id", kmrt.ID,
"node_id", n.ID,
)
continue
}
if policyHash != nodePolicyHash {
ctx.Logger().Error("Policy checksum mismatch for runtime",
"id", kmrt.ID,
"node_id", n.ID,
)
continue
}
if status.IsInitialized {
// Already initialized. Check to see if it should be added to
// the node list.
if initResponse.IsSecure != status.IsSecure {
ctx.Logger().Error("Security status mismatch for runtime",
"id", kmrt.ID,
"node_id", n.ID,
)
continue
}
if !bytes.Equal(initResponse.Checksum, status.Checksum) {
ctx.Logger().Error("Checksum mismatch for runtime",
"id", kmrt.ID,
"node_id", n.ID,
)
continue
}
} else {
// Not initialized. The first node gets to be the source
// of truth, every other node will sync off it.
// Allow false -> true transitions, but not the reverse, so that
// it is possible to set the security status in the genesis block.
if initResponse.IsSecure != status.IsSecure && !initResponse.IsSecure {
ctx.Logger().Error("Security status mismatch for runtime",
"id", kmrt.ID,
"node_id", n.ID,
)
continue
}
status.IsSecure = initResponse.IsSecure
status.IsInitialized = true
status.Checksum = initResponse.Checksum
}
status.Nodes = append(status.Nodes, n.ID)
}
return status
}
// New constructs a new keymanager application instance.
func New() tmapi.Application {
return &keymanagerApplication{}
}