eab.go 5.2 KB

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