eab.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if _, ok := errors.AsType[*acme.Error](err); ok {
  45. return nil, acme.WrapError(acme.ErrorUnauthorizedType, err, "the field 'kid' references an unknown key")
  46. }
  47. return nil, acme.WrapErrorISE(err, "error retrieving external account key")
  48. }
  49. if externalAccountKey == nil {
  50. return nil, acme.NewError(acme.ErrorUnauthorizedType, "the field 'kid' references an unknown key")
  51. }
  52. // A bound key has had its secret cleared (see ExternalAccountKey.BindTo),
  53. // so this must be checked before the secret: a client reusing a key is
  54. // unauthorized, not a server error.
  55. if externalAccountKey.AlreadyBound() {
  56. 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)
  57. }
  58. if len(externalAccountKey.HmacKey) == 0 {
  59. return nil, acme.NewError(acme.ErrorServerInternalType, "external account binding key with id '%s' does not have secret bytes", keyID)
  60. }
  61. payload, err := eabJWS.Verify(externalAccountKey.HmacKey)
  62. if err != nil {
  63. return nil, acme.WrapErrorISE(err, "error verifying externalAccountBinding signature")
  64. }
  65. jwk, err := jwkFromContext(ctx)
  66. if err != nil {
  67. return nil, err
  68. }
  69. var payloadJWK *jose.JSONWebKey
  70. if err = json.Unmarshal(payload, &payloadJWK); err != nil {
  71. return nil, acme.WrapError(acme.ErrorMalformedType, err, "error unmarshaling payload into jwk")
  72. }
  73. if !keysAreEqual(jwk, payloadJWK) {
  74. return nil, acme.NewError(acme.ErrorUnauthorizedType, "keys in jws and eab payload do not match")
  75. }
  76. return externalAccountKey, nil
  77. }
  78. // keysAreEqual performs an equality check on two JWKs by comparing
  79. // the (base64 encoding) of the Key IDs.
  80. func keysAreEqual(x, y *jose.JSONWebKey) bool {
  81. if x == nil || y == nil {
  82. return false
  83. }
  84. digestX, errX := acme.KeyToID(x)
  85. digestY, errY := acme.KeyToID(y)
  86. if errX != nil || errY != nil {
  87. return false
  88. }
  89. return digestX == digestY
  90. }
  91. // validateEABJWS verifies the contents of the External Account Binding JWS.
  92. // The protected header of the JWS MUST meet the following criteria:
  93. //
  94. // - The "alg" field MUST indicate a MAC-based algorithm
  95. // - The "kid" field MUST contain the key identifier provided by the CA
  96. // - The "nonce" field MUST NOT be present
  97. // - The "url" field MUST be set to the same value as the outer JWS
  98. func validateEABJWS(ctx context.Context, jws *jose.JSONWebSignature) (string, *acme.Error) {
  99. if jws == nil {
  100. return "", acme.NewErrorISE("no JWS provided")
  101. }
  102. if len(jws.Signatures) != 1 {
  103. return "", acme.NewError(acme.ErrorMalformedType, "JWS must have one signature")
  104. }
  105. header := jws.Signatures[0].Protected
  106. algorithm := header.Algorithm
  107. keyID := header.KeyID
  108. nonce := header.Nonce
  109. if algorithm != jose.HS256 && algorithm != jose.HS384 && algorithm != jose.HS512 {
  110. return "", acme.NewError(acme.ErrorMalformedType, "'alg' field set to invalid algorithm '%s'", algorithm)
  111. }
  112. if keyID == "" {
  113. return "", acme.NewError(acme.ErrorMalformedType, "'kid' field is required")
  114. }
  115. if nonce != "" {
  116. return "", acme.NewError(acme.ErrorMalformedType, "'nonce' must not be present")
  117. }
  118. jwsURL, ok := header.ExtraHeaders["url"]
  119. if !ok {
  120. return "", acme.NewError(acme.ErrorMalformedType, "'url' field is required")
  121. }
  122. outerJWS, err := jwsFromContext(ctx)
  123. if err != nil {
  124. return "", acme.WrapErrorISE(err, "could not retrieve outer JWS from context")
  125. }
  126. if len(outerJWS.Signatures) != 1 {
  127. return "", acme.NewError(acme.ErrorMalformedType, "outer JWS must have one signature")
  128. }
  129. outerJWSURL, ok := outerJWS.Signatures[0].Protected.ExtraHeaders["url"]
  130. if !ok {
  131. return "", acme.NewError(acme.ErrorMalformedType, "'url' field must be set in outer JWS")
  132. }
  133. if jwsURL != outerJWSURL {
  134. return "", acme.NewError(acme.ErrorMalformedType, "'url' field is not the same value as the outer JWS")
  135. }
  136. return keyID, nil
  137. }