authorize.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.RevokeMethod:
  73. return nil, a.authorizeRevoke(ctx, ott)
  74. case provisioner.SignSSHMethod:
  75. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  76. return nil, &apiError{errors.New("authorize: ssh signing is not enabled"), http.StatusNotImplemented, errContext}
  77. }
  78. return a.authorizeSSHSign(ctx, ott)
  79. case provisioner.RenewSSHMethod:
  80. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  81. return nil, &apiError{errors.New("authorize: ssh signing is not enabled"), http.StatusNotImplemented, errContext}
  82. }
  83. if _, err := a.authorizeSSHRenew(ctx, ott); err != nil {
  84. return nil, err
  85. }
  86. return nil, nil
  87. case provisioner.RevokeSSHMethod:
  88. return nil, a.authorizeSSHRevoke(ctx, ott)
  89. case provisioner.RekeySSHMethod:
  90. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  91. return nil, &apiError{errors.New("authorize: ssh signing is not enabled"), http.StatusNotImplemented, errContext}
  92. }
  93. _, opts, err := a.authorizeSSHRekey(ctx, ott)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return opts, nil
  98. default:
  99. return nil, &apiError{errors.Errorf("authorize: method %d is not supported", m), http.StatusInternalServerError, errContext}
  100. }
  101. }
  102. // authorizeSign loads the provisioner from the token, checks that it has not
  103. // been used again and calls the provisioner AuthorizeSign method. Returns a
  104. // list of methods to apply to the signing flow.
  105. func (a *Authority) authorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  106. var errContext = apiCtx{"ott": ott}
  107. p, err := a.authorizeToken(ott)
  108. if err != nil {
  109. return nil, &apiError{errors.Wrap(err, "authorizeSign"), http.StatusUnauthorized, errContext}
  110. }
  111. opts, err := p.AuthorizeSign(ctx, ott)
  112. if err != nil {
  113. return nil, &apiError{errors.Wrap(err, "authorizeSign"), http.StatusUnauthorized, errContext}
  114. }
  115. return opts, nil
  116. }
  117. // AuthorizeSign authorizes a signature request by validating and authenticating
  118. // a OTT that must be sent w/ the request.
  119. //
  120. // NOTE: This method is deprecated and should not be used. We make it available
  121. // in the short term os as not to break existing clients.
  122. func (a *Authority) AuthorizeSign(ott string) ([]provisioner.SignOption, error) {
  123. ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignMethod)
  124. return a.Authorize(ctx, ott)
  125. }
  126. // authorizeRevoke authorizes a revocation request by validating and authenticating
  127. // the RevokeOptions POSTed with the request.
  128. // Returns a tuple of the provisioner ID and error, if one occurred.
  129. func (a *Authority) authorizeRevoke(ctx context.Context, token string) error {
  130. errContext := map[string]interface{}{"ott": token}
  131. p, err := a.authorizeToken(token)
  132. if err != nil {
  133. return &apiError{errors.Wrap(err, "authorizeRevoke"), http.StatusUnauthorized, errContext}
  134. }
  135. if err = p.AuthorizeSSHRevoke(ctx, token); err != nil {
  136. return &apiError{errors.Wrap(err, "authorizeRevoke"), http.StatusUnauthorized, errContext}
  137. }
  138. return nil
  139. }
  140. // authorizeRenewl tries to locate the step provisioner extension, and checks
  141. // if for the configured provisioner, the renewal is enabled or not. If the
  142. // extra extension cannot be found, authorize the renewal by default.
  143. //
  144. // TODO(mariano): should we authorize by default?
  145. func (a *Authority) authorizeRenew(crt *x509.Certificate) error {
  146. errContext := map[string]interface{}{"serialNumber": crt.SerialNumber.String()}
  147. // Check the passive revocation table.
  148. isRevoked, err := a.db.IsRevoked(crt.SerialNumber.String())
  149. if err != nil {
  150. return &apiError{
  151. err: errors.Wrap(err, "renew"),
  152. code: http.StatusInternalServerError,
  153. context: errContext,
  154. }
  155. }
  156. if isRevoked {
  157. return &apiError{
  158. err: errors.New("renew: certificate has been revoked"),
  159. code: http.StatusUnauthorized,
  160. context: errContext,
  161. }
  162. }
  163. p, ok := a.provisioners.LoadByCertificate(crt)
  164. if !ok {
  165. return &apiError{
  166. err: errors.New("renew: provisioner not found"),
  167. code: http.StatusUnauthorized,
  168. context: errContext,
  169. }
  170. }
  171. if err := p.AuthorizeRenew(context.Background(), crt); err != nil {
  172. return &apiError{
  173. err: errors.Wrap(err, "renew"),
  174. code: http.StatusUnauthorized,
  175. context: errContext,
  176. }
  177. }
  178. return nil
  179. }