Skip to content

Commit

Permalink
refactor: simplify verification function
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Jan 15, 2025
1 parent c00ff1f commit 3a75e5f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func main() {
panic(err)
}

enclaveMeasurements, err = attestation.VerifyAttestation(attDocJSON)
enclaveMeasurements, err = attestation.VerifyAttestationJSON(attDocJSON)
if err != nil {
log.Fatalf("Failed to parse enclave attestation doc: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *Client) CheckAttestation() error {
}
}`

att, err := attestation.VerifyAttestation([]byte(respBody))
att, err := attestation.VerifyAttestationJSON([]byte(respBody))
if err != nil {
return err
}
Expand Down
20 changes: 13 additions & 7 deletions pkg/attestation/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@ type Document struct {
Body string `json:"body"`
}

// VerifyAttestation validates the attestation document and returns the inner measurement
func VerifyAttestation(attestationDocJSON []byte) (*Measurement, error) {
var d Document
if err := json.Unmarshal(attestationDocJSON, &d); err != nil {
return nil, err
}

// Verify checks the attestation document against its trust root and returns the inner measurements
func (d *Document) Verify() (*Measurement, error) {
switch d.Format {
case AWSNitroEnclaveV1:
return verifyNitroAttestation(d.Body)
default:
return nil, ErrUnsupportedAttestationFormat
}
}

// VerifyAttestationJSON verifies an attestation document in JSON format and returns the inner measurements
func VerifyAttestationJSON(j []byte) (*Measurement, error) {
var doc Document
err := json.Unmarshal(j, &doc)
if err != nil {
return nil, err
}

return doc.Verify()
}
2 changes: 1 addition & 1 deletion pkg/attestation/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestMainNitroVerifier(t *testing.T) {
undo := mockNitroVerifier()
defer undo()

attestation, err := VerifyAttestation([]byte(payload))
attestation, err := VerifyAttestationJSON([]byte(payload))
assert.Nil(t, err)
assert.Equal(t, 3, len(attestation.Registers))
}

0 comments on commit 3a75e5f

Please sign in to comment.