eab.go 5.0 KB

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