eab.go 5.2 KB

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