4
0

eab.go 4.9 KB

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