authorize.go 6.8 KB

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