authorize.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. type skipTokenReuseKey struct{}
  19. // NewContextWithSkipTokenReuse creates a new context from ctx and attaches a
  20. // value to skip the token reuse.
  21. func NewContextWithSkipTokenReuse(ctx context.Context) context.Context {
  22. return context.WithValue(ctx, skipTokenReuseKey{}, true)
  23. }
  24. // SkipTokenReuseFromContext returns if the token reuse needs to be ignored.
  25. func SkipTokenReuseFromContext(ctx context.Context) bool {
  26. m, _ := ctx.Value(skipTokenReuseKey{}).(bool)
  27. return m
  28. }
  29. // authorizeToken parses the token and returns the provisioner used to generate
  30. // the token. This method enforces the One-Time use policy (tokens can only be
  31. // used once).
  32. func (a *Authority) authorizeToken(ctx context.Context, ott string) (provisioner.Interface, error) {
  33. var errContext = map[string]interface{}{"ott": ott}
  34. // Validate payload
  35. token, err := jose.ParseSigned(ott)
  36. if err != nil {
  37. return nil, &apiError{errors.Wrapf(err, "authorizeToken: error parsing token"),
  38. http.StatusUnauthorized, errContext}
  39. }
  40. // Get claims w/out verification. We need to look up the provisioner
  41. // key in order to verify the claims and we need the issuer from the claims
  42. // before we can look up the provisioner.
  43. var claims Claims
  44. if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
  45. return nil, &apiError{errors.Wrap(err, "authorizeToken"), http.StatusUnauthorized, errContext}
  46. }
  47. // TODO: use new persistence layer abstraction.
  48. // Do not accept tokens issued before the start of the ca.
  49. // This check is meant as a stopgap solution to the current lack of a persistence layer.
  50. if a.config.AuthorityConfig != nil && !a.config.AuthorityConfig.DisableIssuedAtCheck {
  51. if claims.IssuedAt != nil && claims.IssuedAt.Time().Before(a.startTime) {
  52. return nil, &apiError{errors.New("authorizeToken: token issued before the bootstrap of certificate authority"),
  53. http.StatusUnauthorized, errContext}
  54. }
  55. }
  56. // This method will also validate the audiences for JWK provisioners.
  57. p, ok := a.provisioners.LoadByToken(token, &claims.Claims)
  58. if !ok {
  59. return nil, &apiError{
  60. errors.Errorf("authorizeToken: provisioner not found or invalid audience (%s)", strings.Join(claims.Audience, ", ")),
  61. http.StatusUnauthorized, errContext}
  62. }
  63. // Store the token to protect against reuse unless it's skipped.
  64. if !SkipTokenReuseFromContext(ctx) {
  65. if reuseKey, err := p.GetTokenID(ott); err == nil {
  66. ok, err := a.db.UseToken(reuseKey, ott)
  67. if err != nil {
  68. return nil, &apiError{errors.Wrap(err, "authorizeToken: failed when checking if token already used"),
  69. http.StatusInternalServerError, errContext}
  70. }
  71. if !ok {
  72. return nil, &apiError{errors.Errorf("authorizeToken: token already used"), http.StatusUnauthorized, errContext}
  73. }
  74. }
  75. }
  76. return p, nil
  77. }
  78. // Authorize grabs the method from the context and authorizes a signature
  79. // request by validating the one-time-token.
  80. func (a *Authority) Authorize(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  81. var errContext = apiCtx{"ott": ott}
  82. switch m := provisioner.MethodFromContext(ctx); m {
  83. case provisioner.SignMethod:
  84. return a.authorizeSign(ctx, ott)
  85. case provisioner.RevokeMethod:
  86. return nil, a.authorizeRevoke(ctx, ott)
  87. case provisioner.SignSSHMethod:
  88. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  89. return nil, &apiError{errors.New("authorize: ssh signing is not enabled"), http.StatusNotImplemented, errContext}
  90. }
  91. return a.authorizeSSHSign(ctx, ott)
  92. case provisioner.RenewSSHMethod:
  93. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  94. return nil, &apiError{errors.New("authorize: ssh signing is not enabled"), http.StatusNotImplemented, errContext}
  95. }
  96. if _, err := a.authorizeSSHRenew(ctx, ott); err != nil {
  97. return nil, err
  98. }
  99. return nil, nil
  100. case provisioner.RevokeSSHMethod:
  101. return nil, a.authorizeSSHRevoke(ctx, ott)
  102. case provisioner.RekeySSHMethod:
  103. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  104. return nil, &apiError{errors.New("authorize: ssh signing is not enabled"), http.StatusNotImplemented, errContext}
  105. }
  106. _, opts, err := a.authorizeSSHRekey(ctx, ott)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return opts, nil
  111. default:
  112. return nil, &apiError{errors.Errorf("authorize: method %d is not supported", m), http.StatusInternalServerError, errContext}
  113. }
  114. }
  115. // authorizeSign loads the provisioner from the token, checks that it has not
  116. // been used again and calls the provisioner AuthorizeSign method. Returns a
  117. // list of methods to apply to the signing flow.
  118. func (a *Authority) authorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  119. var errContext = apiCtx{"ott": ott}
  120. p, err := a.authorizeToken(ctx, ott)
  121. if err != nil {
  122. return nil, &apiError{errors.Wrap(err, "authorizeSign"), http.StatusUnauthorized, errContext}
  123. }
  124. opts, err := p.AuthorizeSign(ctx, ott)
  125. if err != nil {
  126. return nil, &apiError{errors.Wrap(err, "authorizeSign"), http.StatusUnauthorized, errContext}
  127. }
  128. return opts, nil
  129. }
  130. // AuthorizeSign authorizes a signature request by validating and authenticating
  131. // a OTT that must be sent w/ the request.
  132. //
  133. // NOTE: This method is deprecated and should not be used. We make it available
  134. // in the short term os as not to break existing clients.
  135. func (a *Authority) AuthorizeSign(ott string) ([]provisioner.SignOption, error) {
  136. ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignMethod)
  137. return a.Authorize(ctx, ott)
  138. }
  139. // authorizeRevoke authorizes a revocation request by validating and authenticating
  140. // the RevokeOptions POSTed with the request.
  141. // Returns a tuple of the provisioner ID and error, if one occurred.
  142. func (a *Authority) authorizeRevoke(ctx context.Context, token string) error {
  143. errContext := map[string]interface{}{"ott": token}
  144. p, err := a.authorizeToken(ctx, token)
  145. if err != nil {
  146. return &apiError{errors.Wrap(err, "authorizeRevoke"), http.StatusUnauthorized, errContext}
  147. }
  148. if err = p.AuthorizeSSHRevoke(ctx, token); err != nil {
  149. return &apiError{errors.Wrap(err, "authorizeRevoke"), http.StatusUnauthorized, errContext}
  150. }
  151. return nil
  152. }
  153. // authorizeRenewl tries to locate the step provisioner extension, and checks
  154. // if for the configured provisioner, the renewal is enabled or not. If the
  155. // extra extension cannot be found, authorize the renewal by default.
  156. //
  157. // TODO(mariano): should we authorize by default?
  158. func (a *Authority) authorizeRenew(crt *x509.Certificate) error {
  159. errContext := map[string]interface{}{"serialNumber": crt.SerialNumber.String()}
  160. // Check the passive revocation table.
  161. isRevoked, err := a.db.IsRevoked(crt.SerialNumber.String())
  162. if err != nil {
  163. return &apiError{
  164. err: errors.Wrap(err, "renew"),
  165. code: http.StatusInternalServerError,
  166. context: errContext,
  167. }
  168. }
  169. if isRevoked {
  170. return &apiError{
  171. err: errors.New("renew: certificate has been revoked"),
  172. code: http.StatusUnauthorized,
  173. context: errContext,
  174. }
  175. }
  176. p, ok := a.provisioners.LoadByCertificate(crt)
  177. if !ok {
  178. return &apiError{
  179. err: errors.New("renew: provisioner not found"),
  180. code: http.StatusUnauthorized,
  181. context: errContext,
  182. }
  183. }
  184. if err := p.AuthorizeRenew(context.Background(), crt); err != nil {
  185. return &apiError{
  186. err: errors.Wrap(err, "renew"),
  187. code: http.StatusUnauthorized,
  188. context: errContext,
  189. }
  190. }
  191. return nil
  192. }