4
0

eab.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "go.step.sm/crypto/jose"
  7. "github.com/smallstep/certificates/acme"
  8. )
  9. // ExternalAccountBinding represents the ACME externalAccountBinding JWS
  10. type ExternalAccountBinding struct {
  11. Protected string `json:"protected"`
  12. Payload string `json:"payload"`
  13. Sig string `json:"signature"`
  14. }
  15. // validateExternalAccountBinding validates the externalAccountBinding property in a call to new-account.
  16. func validateExternalAccountBinding(ctx context.Context, nar *NewAccountRequest) (*acme.ExternalAccountKey, error) {
  17. acmeProv, err := acmeProvisionerFromContext(ctx)
  18. if err != nil {
  19. return nil, acme.WrapErrorISE(err, "could not load ACME provisioner from context")
  20. }
  21. if !acmeProv.RequireEAB {
  22. //nolint:nilnil // legacy
  23. return nil, nil
  24. }
  25. if nar.ExternalAccountBinding == nil {
  26. return nil, acme.NewError(acme.ErrorExternalAccountRequiredType, "no external account binding provided")
  27. }
  28. eabJSONBytes, err := json.Marshal(nar.ExternalAccountBinding)
  29. if err != nil {
  30. return nil, acme.WrapErrorISE(err, "error marshaling externalAccountBinding into bytes")
  31. }
  32. eabJWS, err := jose.ParseJWS(string(eabJSONBytes))
  33. if err != nil {
  34. return nil, acme.WrapErrorISE(err, "error parsing externalAccountBinding jws")
  35. }
  36. // TODO(hs): implement strategy pattern to allow for different ways of verification (i.e. webhook call) based on configuration?
  37. keyID, acmeErr := validateEABJWS(ctx, eabJWS)
  38. if acmeErr != nil {
  39. return nil, acmeErr
  40. }
  41. db := acme.MustDatabaseFromContext(ctx)
  42. externalAccountKey, err := db.GetExternalAccountKey(ctx, acmeProv.ID, keyID)
  43. if err != nil {
  44. var ae *acme.Error
  45. if errors.As(err, &ae) {
  46. return nil, acme.WrapError(acme.ErrorUnauthorizedType, err, "the field 'kid' references an unknown key")
  47. }
  48. return nil, acme.WrapErrorISE(err, "error retrieving external account key")
  49. }
  50. if externalAccountKey == nil {
  51. return nil, acme.NewError(acme.ErrorUnauthorizedType, "the field 'kid' references an unknown key")
  52. }
  53. if len(externalAccountKey.HmacKey) == 0 {
  54. return nil, acme.NewError(acme.ErrorServerInternalType, "external account binding key with id '%s' does not have secret bytes", keyID)
  55. }
  56. if externalAccountKey.AlreadyBound() {
  57. return nil, acme.NewError(acme.ErrorUnauthorizedType, "external account binding key with id '%s' was already bound to account '%s' on %s", keyID, externalAccountKey.AccountID, externalAccountKey.BoundAt)
  58. }
  59. payload, err := eabJWS.Verify(externalAccountKey.HmacKey)
  60. if err != nil {
  61. return nil, acme.WrapErrorISE(err, "error verifying externalAccountBinding signature")
  62. }
  63. jwk, err := jwkFromContext(ctx)
  64. if err != nil {
  65. return nil, err
  66. }
  67. var payloadJWK *jose.JSONWebKey
  68. if err = json.Unmarshal(payload, &payloadJWK); err != nil {
  69. return nil, acme.WrapError(acme.ErrorMalformedType, err, "error unmarshaling payload into jwk")
  70. }
  71. if !keysAreEqual(jwk, payloadJWK) {
  72. return nil, acme.NewError(acme.ErrorUnauthorizedType, "keys in jws and eab payload do not match")
  73. }
  74. return externalAccountKey, nil
  75. }
  76. // keysAreEqual performs an equality check on two JWKs by comparing
  77. // the (base64 encoding) of the Key IDs.
  78. func keysAreEqual(x, y *jose.JSONWebKey) bool {
  79. if x == nil || y == nil {
  80. return false
  81. }
  82. digestX, errX := acme.KeyToID(x)
  83. digestY, errY := acme.KeyToID(y)
  84. if errX != nil || errY != nil {
  85. return false
  86. }
  87. return digestX == digestY
  88. }
  89. // validateEABJWS verifies the contents of the External Account Binding JWS.
  90. // The protected header of the JWS MUST meet the following criteria:
  91. //
  92. // - The "alg" field MUST indicate a MAC-based algorithm
  93. // - The "kid" field MUST contain the key identifier provided by the CA
  94. // - The "nonce" field MUST NOT be present
  95. // - The "url" field MUST be set to the same value as the outer JWS
  96. func validateEABJWS(ctx context.Context, jws *jose.JSONWebSignature) (string, *acme.Error) {
  97. if jws == nil {
  98. return "", acme.NewErrorISE("no JWS provided")
  99. }
  100. if len(jws.Signatures) != 1 {
  101. return "", acme.NewError(acme.ErrorMalformedType, "JWS must have one signature")
  102. }
  103. header := jws.Signatures[0].Protected
  104. algorithm := header.Algorithm
  105. keyID := header.KeyID
  106. nonce := header.Nonce
  107. if algorithm != jose.HS256 && algorithm != jose.HS384 && algorithm != jose.HS512 {
  108. return "", acme.NewError(acme.ErrorMalformedType, "'alg' field set to invalid algorithm '%s'", algorithm)
  109. }
  110. if keyID == "" {
  111. return "", acme.NewError(acme.ErrorMalformedType, "'kid' field is required")
  112. }
  113. if nonce != "" {
  114. return "", acme.NewError(acme.ErrorMalformedType, "'nonce' must not be present")
  115. }
  116. jwsURL, ok := header.ExtraHeaders["url"]
  117. if !ok {
  118. return "", acme.NewError(acme.ErrorMalformedType, "'url' field is required")
  119. }
  120. outerJWS, err := jwsFromContext(ctx)
  121. if err != nil {
  122. return "", acme.WrapErrorISE(err, "could not retrieve outer JWS from context")
  123. }
  124. if len(outerJWS.Signatures) != 1 {
  125. return "", acme.NewError(acme.ErrorMalformedType, "outer JWS must have one signature")
  126. }
  127. outerJWSURL, ok := outerJWS.Signatures[0].Protected.ExtraHeaders["url"]
  128. if !ok {
  129. return "", acme.NewError(acme.ErrorMalformedType, "'url' field must be set in outer JWS")
  130. }
  131. if jwsURL != outerJWSURL {
  132. return "", acme.NewError(acme.ErrorMalformedType, "'url' field is not the same value as the outer JWS")
  133. }
  134. return keyID, nil
  135. }