order_remote_attestation_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package api
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "github.com/smallstep/certificates/acme"
  10. "github.com/smallstep/certificates/authority/provisioner"
  11. )
  12. func newRemoteAttestationProv(t *testing.T, challenges []provisioner.ACMEChallenge, o *provisioner.ACMERemoteAttestation) *provisioner.ACME {
  13. t.Helper()
  14. p := &provisioner.ACME{
  15. Type: "ACME",
  16. Name: "attested",
  17. Challenges: challenges,
  18. RemoteAttestation: o,
  19. }
  20. require.NoError(t, p.Init(provisioner.Config{Claims: globalProvisionerClaims}))
  21. return p
  22. }
  23. func TestNewOrderRequest_Validate_remoteAttestation(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. ids []acme.Identifier
  27. wantErr string
  28. }{
  29. {"ok empty hint", []acme.Identifier{{Type: acme.RemoteAttestation}}, ""},
  30. {"ok with dns", []acme.Identifier{{Type: acme.DNS, Value: "example.com"}, {Type: acme.RemoteAttestation, Value: "measured_boot"}}, ""},
  31. {"ok longest hint", []acme.Identifier{{Type: acme.RemoteAttestation, Value: strings.Repeat("a", maxRemoteAttestationHint)}}, ""},
  32. {"fail hint too long", []acme.Identifier{{Type: acme.RemoteAttestation, Value: strings.Repeat("a", maxRemoteAttestationHint+1)}},
  33. "remote-attestation identifier value cannot be longer than 128 bytes"},
  34. {"fail two", []acme.Identifier{{Type: acme.RemoteAttestation}, {Type: acme.RemoteAttestation, Value: "hsm"}},
  35. "an order cannot have more than one remote-attestation identifier"},
  36. }
  37. for _, tt := range tests {
  38. t.Run(tt.name, func(t *testing.T) {
  39. err := (&NewOrderRequest{Identifiers: tt.ids}).Validate()
  40. if tt.wantErr == "" {
  41. assert.NoError(t, err)
  42. return
  43. }
  44. var acmeErr *acme.Error
  45. require.ErrorAs(t, err, &acmeErr)
  46. assert.Equal(t, acme.NewError(acme.ErrorMalformedType, "").Type, acmeErr.Type)
  47. assert.ErrorContains(t, err, tt.wantErr)
  48. })
  49. }
  50. }
  51. func TestAddRemoteAttestationIdentifier(t *testing.T) {
  52. enabled := []provisioner.ACMEChallenge{provisioner.DNS_01, provisioner.REMOTE_ATTEST_01}
  53. dns := acme.Identifier{Type: acme.DNS, Value: "example.com"}
  54. ra := acme.Identifier{Type: acme.RemoteAttestation, Value: "hsm"}
  55. tests := []struct {
  56. name string
  57. prov *provisioner.ACME
  58. ids []acme.Identifier
  59. want []acme.Identifier
  60. wantErr bool
  61. }{
  62. {"ok not required", newRemoteAttestationProv(t, enabled, nil), []acme.Identifier{dns}, []acme.Identifier{dns}, false},
  63. {"ok required", newRemoteAttestationProv(t, enabled, &provisioner.ACMERemoteAttestation{Require: true}),
  64. []acme.Identifier{dns}, []acme.Identifier{dns, {Type: acme.RemoteAttestation}}, false},
  65. {"ok required and present", newRemoteAttestationProv(t, enabled, &provisioner.ACMERemoteAttestation{Require: true}),
  66. []acme.Identifier{ra, dns}, []acme.Identifier{ra, dns}, false},
  67. {"ok required but not enabled", newRemoteAttestationProv(t, nil, &provisioner.ACMERemoteAttestation{Require: true}),
  68. []acme.Identifier{dns}, []acme.Identifier{dns}, false},
  69. {"fail not enabled", newRemoteAttestationProv(t, nil, nil), []acme.Identifier{dns, ra}, nil, true},
  70. }
  71. for _, tt := range tests {
  72. t.Run(tt.name, func(t *testing.T) {
  73. nor := &NewOrderRequest{Identifiers: tt.ids}
  74. err := addRemoteAttestationIdentifier(context.Background(), tt.prov, nor)
  75. if tt.wantErr {
  76. var acmeErr *acme.Error
  77. require.ErrorAs(t, err, &acmeErr)
  78. assert.Equal(t, acme.NewError(acme.ErrorUnsupportedIdentifierType, "").Type, acmeErr.Type)
  79. return
  80. }
  81. require.NoError(t, err)
  82. assert.Equal(t, tt.want, nor.Identifiers)
  83. })
  84. }
  85. }
  86. func TestChallengeTypes_remoteAttestation(t *testing.T) {
  87. got := challengeTypes(&acme.Authorization{Identifier: acme.Identifier{Type: acme.RemoteAttestation}})
  88. assert.Equal(t, []acme.ChallengeType{acme.REMOTEATTEST01}, got)
  89. }
  90. func TestNewAuthorization_remoteAttestation(t *testing.T) {
  91. prov := newRemoteAttestationProv(t, []provisioner.ACMEChallenge{provisioner.REMOTE_ATTEST_01}, &provisioner.ACMERemoteAttestation{
  92. ClaimsHint: []string{"hwmodel", "swversion"},
  93. })
  94. var created []*acme.Challenge
  95. db := &acme.MockDB{
  96. MockCreateChallenge: func(_ context.Context, ch *acme.Challenge) error {
  97. ch.ID = "chID"
  98. created = append(created, ch)
  99. return nil
  100. },
  101. MockCreateAuthorization: func(_ context.Context, az *acme.Authorization) error {
  102. az.ID = "azID"
  103. return nil
  104. },
  105. }
  106. ctx := acme.NewProvisionerContext(context.Background(), prov)
  107. ctx = acme.NewDatabaseContext(ctx, db)
  108. az := &acme.Authorization{
  109. AccountID: "accID",
  110. Identifier: acme.Identifier{Type: acme.RemoteAttestation, Value: "measured_boot"},
  111. Status: acme.StatusPending,
  112. }
  113. require.NoError(t, newAuthorization(ctx, az))
  114. require.Len(t, created, 1)
  115. ch := created[0]
  116. assert.Equal(t, acme.REMOTEATTEST01, ch.Type)
  117. assert.Equal(t, "measured_boot", ch.Value)
  118. assert.Equal(t, []string{"hwmodel", "swversion"}, ch.AttestClaimsHint)
  119. nonce, err := base64.RawURLEncoding.DecodeString(ch.FreshnessNonce)
  120. require.NoError(t, err)
  121. assert.Len(t, nonce, 32)
  122. assert.Equal(t, []*acme.Challenge{ch}, az.Challenges)
  123. // Each challenge gets its own nonce.
  124. require.NoError(t, newAuthorization(ctx, &acme.Authorization{
  125. AccountID: "accID",
  126. Identifier: acme.Identifier{Type: acme.RemoteAttestation},
  127. }))
  128. require.Len(t, created, 2)
  129. assert.NotEqual(t, created[0].FreshnessNonce, created[1].FreshnessNonce)
  130. }