Skip to content

Commit

Permalink
Initialize NSM session on first call to Attest.
Browse files Browse the repository at this point in the history
This makes it possible to use the nitro attester on non-nitro platforms
to verify attestation documents.
  • Loading branch information
NullHypothesis committed Oct 20, 2024
1 parent 2fe9102 commit bae4f6d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
7 changes: 1 addition & 6 deletions cmd/veil/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,9 @@ func run(ctx context.Context, out io.Writer, args []string) (err error) {
}

// Initialize dependencies and start the service.
var attester enclave.Attester
var attester enclave.Attester = enclave.NewNitroAttester()
if cfg.Testing {
attester = enclave.NewNoopAttester()
} else {
attester, err = enclave.NewNitroAttester()
if err != nil {
return err
}
}
service.Run(ctx, cfg, attester, tunnel.NewNoop())
return nil
Expand Down
18 changes: 9 additions & 9 deletions internal/enclave/attester_nitro.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,8 @@ type NitroAttester struct {
}

// NewNitroAttester returns a new nitroAttester.
func NewNitroAttester() (attester Attester, err error) {
defer errs.Wrap(&err, "failed to create nitro attester")
a := new(NitroAttester)

// Open a session to the Nitro Secure Module.
if a.session, err = nsm.OpenDefaultSession(); err != nil {
return nil, err
}
return a, nil
func NewNitroAttester() Attester {
return new(NitroAttester)
}

func (*NitroAttester) Type() string {
Expand All @@ -37,6 +30,13 @@ func (*NitroAttester) Type() string {
func (a *NitroAttester) Attest(aux *AuxInfo) (_ *AttestationDoc, err error) {
defer errs.Wrap(&err, "failed to create attestation document")

if a.session == nil {
// Open a session to the Nitro Secure Module.
if a.session, err = nsm.OpenDefaultSession(); err != nil {
return nil, err
}
}

if aux == nil {
return nil, errors.New("aux info is nil")
}
Expand Down
7 changes: 2 additions & 5 deletions internal/enclave/attester_nitro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func TestNitroAttest(t *testing.T) {
if !IsEnclave() {
t.Skip("skipping test; not running in an enclave")
}
attester, err := NewNitroAttester()
require.NoError(t, err)
attester := NewNitroAttester()

cases := []struct {
name string
Expand Down Expand Up @@ -59,9 +58,7 @@ func TestNitroVerify(t *testing.T) {
t.Skip("skipping test; not running in an enclave")
}

attester, err := NewNitroAttester()
require.NoError(t, err)

attester := NewNitroAttester()
getDoc := func(t *testing.T, n *nonce.Nonce) *AttestationDoc {
doc, err := attester.Attest(&AuxInfo{Nonce: ToAuxField(n.ToSlice())})
require.NoError(t, err)
Expand Down
7 changes: 1 addition & 6 deletions internal/enclave/pcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ type pcr map[uint][]byte
func getPCRs() (_ pcr, err error) {
defer errs.Wrap(&err, "failed to get PCRs")

attester, err := NewNitroAttester()
if err != nil {
return nil, err
}

attestation, err := attester.Attest(&AuxInfo{})
attestation, err := NewNitroAttester().Attest(&AuxInfo{})
if err != nil {
return nil, err
}
Expand Down

0 comments on commit bae4f6d

Please sign in to comment.