eab.go 5.2 KB

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