Skip to content

Commit

Permalink
Set the certificate's hash upon init.
Browse files Browse the repository at this point in the history
  • Loading branch information
NullHypothesis committed Oct 14, 2024
1 parent 8c5717c commit 30e6e8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
37 changes: 15 additions & 22 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"net/url"
"os"
"slices"
"strings"
"sync"
"syscall"
"testing"
Expand Down Expand Up @@ -277,51 +276,45 @@ func TestHashes(t *testing.T) {
body,
)
}
doGet = func() (*http.Response, error) {
doGet = func(_ io.Reader) (*http.Response, error) {
return testutil.Client.Get(intSrv("/enclave/hashes"))
}
)
hashes.SetAppHash(util.AddrOf([sha256.Size]byte{1}))

cases := []struct {
name string
method string
reqFunc func(io.Reader) (*http.Response, error)
toMarshal any
wantCode int
wantHashes *attestation.Hashes
}{
{
name: "get empty hashes",
method: http.MethodGet,
reqFunc: doGet,
wantCode: http.StatusOK,
wantHashes: new(attestation.Hashes),
},
{
name: "post application hash",
method: http.MethodPost,
reqFunc: doPost,
toMarshal: hashes,
wantCode: http.StatusOK,
},
{
name: "get populated hashes",
method: http.MethodGet,
reqFunc: doGet,
wantCode: http.StatusOK,
wantHashes: hashes,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
var b []byte
var resp *http.Response
var err error
if c.method == http.MethodGet {
resp, err = doGet()
} else {
b, err = json.Marshal(c.toMarshal)
require.NoError(t, err)
resp, err = doPost(bytes.NewReader(b))
}
// Either POST or GET the hashes.
reqBody, err := json.Marshal(c.toMarshal)
require.NoError(t, err)
resp, err := c.reqFunc(bytes.NewReader(reqBody))
require.NoError(t, err)
require.Equal(t, c.wantCode, resp.StatusCode)

Expand All @@ -334,13 +327,13 @@ func TestHashes(t *testing.T) {
gotBody, err := io.ReadAll(resp.Body)
require.NoError(t, err)
defer resp.Body.Close()
wantBody, err := json.Marshal(c.wantHashes)
require.NoError(t, err)
var gotHashes attestation.Hashes
require.NoError(t, json.Unmarshal(gotBody, &gotHashes))

require.Equal(t,
strings.TrimSpace(string(wantBody)),
strings.TrimSpace(string(gotBody)),
)
// Make sure that the application hashes match.
require.Equal(t, c.wantHashes.AppKeyHash, gotHashes.AppKeyHash)
// Make sure that the TLS certificate hash is set.
require.NotEmpty(t, gotHashes.TlsKeyHash)
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"crypto/sha256"
"crypto/tls"
"errors"
"log"
Expand Down Expand Up @@ -41,6 +42,7 @@ func Run(

// Initialize hashes for the attestation document.
hashes := new(attestation.Hashes)
hashes.SetTLSHash(util.AddrOf(sha256.Sum256(cert)))

// Initialize Web servers.
intSrv := newIntSrv(config, keys, hashes, appReady)
Expand Down

0 comments on commit 30e6e8c

Please sign in to comment.