authorize.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package authority
  2. import (
  3. "crypto/x509"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/cli/jose"
  10. )
  11. type idUsed struct {
  12. UsedAt int64 `json:"ua,omitempty"`
  13. Subject string `json:"sub,omitempty"`
  14. }
  15. // Claims extends jose.Claims with step attributes.
  16. type Claims struct {
  17. jose.Claims
  18. SANs []string `json:"sans,omitempty"`
  19. Email string `json:"email,omitempty"`
  20. Nonce string `json:"nonce,omitempty"`
  21. }
  22. // authorizeToken parses the token and returns the provisioner used to generate
  23. // the token. This method enforces the One-Time use policy (tokens can only be
  24. // used once).
  25. func (a *Authority) authorizeToken(ott string) (provisioner.Interface, error) {
  26. var errContext = map[string]interface{}{"ott": ott}
  27. // Validate payload
  28. token, err := jose.ParseSigned(ott)
  29. if err != nil {
  30. return nil, &apiError{errors.Wrapf(err, "authorizeToken: error parsing token"),
  31. http.StatusUnauthorized, errContext}
  32. }
  33. // Get claims w/out verification. We need to look up the provisioner
  34. // key in order to verify the claims and we need the issuer from the claims
  35. // before we can look up the provisioner.
  36. var claims Claims
  37. if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
  38. return nil, &apiError{errors.Wrap(err, "authorizeToken"), http.StatusUnauthorized, errContext}
  39. }
  40. // TODO: use new persistence layer abstraction.
  41. // Do not accept tokens issued before the start of the ca.
  42. // This check is meant as a stopgap solution to the current lack of a persistence layer.
  43. if a.config.AuthorityConfig != nil && !a.config.AuthorityConfig.DisableIssuedAtCheck {
  44. if claims.IssuedAt != nil && claims.IssuedAt.Time().Before(a.startTime) {
  45. return nil, &apiError{errors.New("authorizeToken: token issued before the bootstrap of certificate authority"),
  46. http.StatusUnauthorized, errContext}
  47. }
  48. }
  49. // This method will also validate the audiences for JWK provisioners.
  50. p, ok := a.provisioners.LoadByToken(token, &claims.Claims)
  51. if !ok {
  52. return nil, &apiError{
  53. errors.Errorf("authorizeToken: provisioner not found or invalid audience (%s)", strings.Join(claims.Audience, ", ")),
  54. http.StatusUnauthorized, errContext}
  55. }
  56. // Store the token to protect against reuse.
  57. var reuseKey string
  58. switch p.GetType() {
  59. case provisioner.TypeJWK:
  60. reuseKey = claims.ID
  61. case provisioner.TypeOIDC:
  62. reuseKey = claims.Nonce
  63. }
  64. if reuseKey != "" {
  65. if _, ok := a.ottMap.LoadOrStore(reuseKey, &idUsed{
  66. UsedAt: time.Now().Unix(),
  67. Subject: claims.Subject,
  68. }); ok {
  69. return nil, &apiError{errors.Errorf("authorizeToken: token already used"), http.StatusUnauthorized, errContext}
  70. }
  71. }
  72. return p, nil
  73. }
  74. // Authorize is a passthrough to AuthorizeSign.
  75. // NOTE: Authorize will be deprecated in a future release. Please use the
  76. // context specific Authorize[Sign|Revoke|etc.] going forwards.
  77. func (a *Authority) Authorize(ott string) ([]provisioner.SignOption, error) {
  78. return a.AuthorizeSign(ott)
  79. }
  80. // AuthorizeSign authorizes a signature request by validating and authenticating
  81. // a OTT that must be sent w/ the request.
  82. func (a *Authority) AuthorizeSign(ott string) ([]provisioner.SignOption, error) {
  83. var errContext = context{"ott": ott}
  84. p, err := a.authorizeToken(ott)
  85. if err != nil {
  86. return nil, &apiError{errors.Wrap(err, "authorizeSign"), http.StatusUnauthorized, errContext}
  87. }
  88. // Call the provisioner AuthorizeSign method to apply provisioner specific
  89. // auth claims and get the signing options.
  90. opts, err := p.AuthorizeSign(ott)
  91. if err != nil {
  92. return nil, &apiError{errors.Wrap(err, "authorizeSign"), http.StatusUnauthorized, errContext}
  93. }
  94. return opts, nil
  95. }
  96. // authorizeRevoke authorizes a revocation request by validating and authenticating
  97. // the RevokeOptions POSTed with the request.
  98. // Returns a tuple of the provisioner ID and error, if one occurred.
  99. func (a *Authority) authorizeRevoke(opts *RevokeOptions) (p provisioner.Interface, err error) {
  100. if opts.MTLS {
  101. if opts.Crt.SerialNumber.String() != opts.Serial {
  102. return nil, errors.New("authorizeRevoke: serial number in certificate different than body")
  103. }
  104. // Load the Certificate provisioner if one exists.
  105. p, err = a.LoadProvisionerByCertificate(opts.Crt)
  106. if err != nil {
  107. return nil, errors.Wrap(err, "authorizeRevoke")
  108. }
  109. } else {
  110. // Gets the token provisioner and validates common token fields.
  111. p, err = a.authorizeToken(opts.OTT)
  112. if err != nil {
  113. return nil, errors.Wrap(err, "authorizeRevoke")
  114. }
  115. // Call the provisioner AuthorizeRevoke to apply provisioner specific auth claims.
  116. err = p.AuthorizeRevoke(opts.OTT)
  117. if err != nil {
  118. return nil, errors.Wrap(err, "authorizeRevoke")
  119. }
  120. }
  121. return
  122. }
  123. // authorizeRenewal tries to locate the step provisioner extension, and checks
  124. // if for the configured provisioner, the renewal is enabled or not. If the
  125. // extra extension cannot be found, authorize the renewal by default.
  126. //
  127. // TODO(mariano): should we authorize by default?
  128. func (a *Authority) authorizeRenewal(crt *x509.Certificate) error {
  129. errContext := map[string]interface{}{"serialNumber": crt.SerialNumber.String()}
  130. // Check the passive revocation table.
  131. isRevoked, err := a.db.IsRevoked(crt.SerialNumber.String())
  132. if err != nil {
  133. return &apiError{
  134. err: errors.Wrap(err, "renew"),
  135. code: http.StatusInternalServerError,
  136. context: errContext,
  137. }
  138. }
  139. if isRevoked {
  140. return &apiError{
  141. err: errors.New("renew: certificate has been revoked"),
  142. code: http.StatusUnauthorized,
  143. context: errContext,
  144. }
  145. }
  146. p, ok := a.provisioners.LoadByCertificate(crt)
  147. if !ok {
  148. return &apiError{
  149. err: errors.New("renew: provisioner not found"),
  150. code: http.StatusUnauthorized,
  151. context: errContext,
  152. }
  153. }
  154. if err := p.AuthorizeRenewal(crt); err != nil {
  155. return &apiError{
  156. err: errors.Wrap(err, "renew"),
  157. code: http.StatusUnauthorized,
  158. context: errContext,
  159. }
  160. }
  161. return nil
  162. }