Skip to content

Commit 8c77019

Browse files
committed
Refactor voucher persistent state; Store vouchers in single sqlite table
Signed-off-by: Ben Krieger <[email protected]>
1 parent fe42754 commit 8c77019

File tree

12 files changed

+278
-138
lines changed

12 files changed

+278
-138
lines changed

di.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (s *DIServer[T]) diDone(ctx context.Context, msg io.Reader) (struct{}, erro
326326
return struct{}{}, fmt.Errorf("error in callback before new voucher is persisted: %w", err)
327327
}
328328
}
329-
if err := s.Vouchers.NewVoucher(ctx, ov); err != nil {
329+
if err := s.Vouchers.AddVoucher(ctx, ov); err != nil {
330330
return struct{}{}, fmt.Errorf("error storing voucher: %w", err)
331331
}
332332
if s.AfterVoucherPersist != nil {

examples/cmd/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ func resell(ctx context.Context, state *sqlite.DB) error {
509509
OwnerKeys: state,
510510
}).Resell(ctx, guid, nextOwner, nil)
511511
if err != nil {
512+
// TODO: If extended != nil, then call AddVoucher to restore state
512513
return fmt.Errorf("resale protocol: %w", err)
513514
}
514515
ovBytes, err := cbor.Marshal(extended)

fdotest/client.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,9 @@ func RunClientTestSuite(t *testing.T, conf Config) {
222222
}
223223
},
224224
},
225-
Vouchers: conf.State,
226-
OwnerKeys: conf.State,
225+
Vouchers: conf.State,
226+
OwnerKeys: conf.State,
227+
VouchersForExtension: conf.State,
227228
RvInfo: func(context.Context, fdo.Voucher) ([][]protocol.RvInstruction, error) {
228229
return [][]protocol.RvInstruction{}, nil
229230
},
@@ -453,10 +454,24 @@ func RunClientTestSuite(t *testing.T, conf Config) {
453454
if cred == nil {
454455
t.Fatal("cred not set due to previous failure")
455456
}
457+
rsaBits := 3072
458+
if conf.UnsupportedRSA3072 {
459+
rsaBits = 2048
460+
}
461+
nextOwner, _, err := to2Responder.OwnerKeys.OwnerKey(t.Context(), table.keyType, rsaBits)
462+
if err != nil {
463+
t.Fatalf("could not get owner key for voucher extension: %v", err)
464+
}
465+
ov, err := to2Responder.Resell(t.Context(), cred.GUID, nextOwner.Public(), nil)
466+
if err != nil {
467+
t.Fatalf("could not extend voucher from previous onboarding: %v", err)
468+
}
469+
if err := to2Responder.Vouchers.AddVoucher(t.Context(), ov); err != nil {
470+
t.Fatalf("could not add voucher for TO2: %v", err)
471+
}
456472

457473
ctx, cancel := context.WithTimeout(context.Background(), timeout)
458474
defer cancel()
459-
var err error
460475
cred, err = fdo.TO2(ctx, transport, nil, fdo.TO2Config{
461476
Cred: *cred,
462477
HmacSha256: hmacSha256,
@@ -485,6 +500,21 @@ func RunClientTestSuite(t *testing.T, conf Config) {
485500
if cred == nil {
486501
t.Fatal("cred not set due to previous failure")
487502
}
503+
rsaBits := 3072
504+
if conf.UnsupportedRSA3072 {
505+
rsaBits = 2048
506+
}
507+
nextOwner, _, err := to2Responder.OwnerKeys.OwnerKey(t.Context(), table.keyType, rsaBits)
508+
if err != nil {
509+
t.Fatalf("could not get owner key for voucher extension: %v", err)
510+
}
511+
ov, err := to2Responder.Resell(t.Context(), cred.GUID, nextOwner.Public(), nil)
512+
if err != nil {
513+
t.Fatalf("could not extend voucher from previous onboarding: %v", err)
514+
}
515+
if err := to2Responder.Vouchers.AddVoucher(t.Context(), ov); err != nil {
516+
t.Fatalf("could not add voucher for TO2: %v", err)
517+
}
488518

489519
ctx, cancel := context.WithTimeout(context.Background(), timeout)
490520
defer cancel()
@@ -526,8 +556,11 @@ func RunClientTestSuite(t *testing.T, conf Config) {
526556
// relying on CleanupModules to be called to clear the state before the next
527557
// usage.
528558
type to2ModuleStateMachine struct {
529-
Session fdo.TO2SessionState
530-
Vouchers fdo.OwnerVoucherPersistentState
559+
Session fdo.TO2SessionState
560+
Vouchers interface {
561+
fdo.VoucherPersistentState
562+
fdo.OwnerVoucherPersistentState
563+
}
531564
OwnerModules func(ctx context.Context, guid protocol.GUID, info string, chain []*x509.Certificate, devmod serviceinfo.Devmod, modules []string) iter.Seq2[string, serviceinfo.OwnerModule]
532565

533566
module *moduleStateMachineState

fdotest/internal/memory/memory.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"crypto/rsa"
1616
"crypto/x509"
1717
"crypto/x509/pkix"
18+
"fmt"
1819
"math/big"
1920
"time"
2021

@@ -41,8 +42,6 @@ type KeyTypeAndRsaBits struct {
4142
RsaBits int
4243
}
4344

44-
var _ fdo.RendezvousBlobPersistentState = (*State)(nil)
45-
var _ fdo.ManufacturerVoucherPersistentState = (*State)(nil)
4645
var _ fdo.OwnerVoucherPersistentState = (*State)(nil)
4746
var _ fdo.OwnerKeyPersistentState = (*State)(nil)
4847

@@ -98,14 +97,6 @@ func NewState() (*State, error) {
9897
}, nil
9998
}
10099

101-
// NewVoucher creates and stores a voucher for a newly initialized device.
102-
// Note that the voucher may have entries if the server was configured for
103-
// auto voucher extension.
104-
func (s *State) NewVoucher(_ context.Context, ov *fdo.Voucher) error {
105-
s.Vouchers[ov.Header.Val.GUID] = ov
106-
return nil
107-
}
108-
109100
// AddVoucher stores the voucher of a device owned by the service.
110101
func (s *State) AddVoucher(_ context.Context, ov *fdo.Voucher) error {
111102
s.Vouchers[ov.Header.Val.GUID] = ov
@@ -115,6 +106,9 @@ func (s *State) AddVoucher(_ context.Context, ov *fdo.Voucher) error {
115106
// ReplaceVoucher stores a new voucher, possibly deleting or marking the
116107
// previous voucher as replaced.
117108
func (s *State) ReplaceVoucher(_ context.Context, oldGUID protocol.GUID, ov *fdo.Voucher) error {
109+
if len(ov.Entries) > 0 {
110+
return fmt.Errorf("ReplaceVoucher must be called with a voucher having zero extensions")
111+
}
118112
delete(s.Vouchers, oldGUID)
119113
s.Vouchers[ov.Header.Val.GUID] = ov
120114
return nil

0 commit comments

Comments
 (0)