authorize.go 5.7 KB

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