authorize.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package authority
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "net/http"
  6. "strings"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. "github.com/smallstep/certificates/errs"
  9. "github.com/smallstep/cli/jose"
  10. "golang.org/x/crypto/ssh"
  11. )
  12. // Claims extends jose.Claims with step attributes.
  13. type Claims struct {
  14. jose.Claims
  15. SANs []string `json:"sans,omitempty"`
  16. Email string `json:"email,omitempty"`
  17. Nonce string `json:"nonce,omitempty"`
  18. }
  19. type skipTokenReuseKey struct{}
  20. // NewContextWithSkipTokenReuse creates a new context from ctx and attaches a
  21. // value to skip the token reuse.
  22. func NewContextWithSkipTokenReuse(ctx context.Context) context.Context {
  23. return context.WithValue(ctx, skipTokenReuseKey{}, true)
  24. }
  25. // SkipTokenReuseFromContext returns if the token reuse needs to be ignored.
  26. func SkipTokenReuseFromContext(ctx context.Context) bool {
  27. m, _ := ctx.Value(skipTokenReuseKey{}).(bool)
  28. return m
  29. }
  30. // authorizeToken parses the token and returns the provisioner used to generate
  31. // the token. This method enforces the One-Time use policy (tokens can only be
  32. // used once).
  33. func (a *Authority) authorizeToken(ctx context.Context, token string) (provisioner.Interface, error) {
  34. // Validate payload
  35. tok, err := jose.ParseSigned(token)
  36. if err != nil {
  37. return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.authorizeToken: error parsing token")
  38. }
  39. // Get claims w/out verification. We need to look up the provisioner
  40. // key in order to verify the claims and we need the issuer from the claims
  41. // before we can look up the provisioner.
  42. var claims Claims
  43. if err = tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
  44. return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.authorizeToken")
  45. }
  46. // TODO: use new persistence layer abstraction.
  47. // Do not accept tokens issued before the start of the ca.
  48. // This check is meant as a stopgap solution to the current lack of a persistence layer.
  49. if a.config.AuthorityConfig != nil && !a.config.AuthorityConfig.DisableIssuedAtCheck {
  50. if claims.IssuedAt != nil && claims.IssuedAt.Time().Before(a.startTime) {
  51. return nil, errs.Unauthorized("authority.authorizeToken: token issued before the bootstrap of certificate authority")
  52. }
  53. }
  54. // This method will also validate the audiences for JWK provisioners.
  55. p, ok := a.provisioners.LoadByToken(tok, &claims.Claims)
  56. if !ok {
  57. return nil, errs.Unauthorized("authority.authorizeToken: provisioner "+
  58. "not found or invalid audience (%s)", strings.Join(claims.Audience, ", "))
  59. }
  60. // Store the token to protect against reuse unless it's skipped.
  61. if !SkipTokenReuseFromContext(ctx) {
  62. if reuseKey, err := p.GetTokenID(token); err == nil {
  63. ok, err := a.db.UseToken(reuseKey, token)
  64. if err != nil {
  65. return nil, errs.Wrap(http.StatusInternalServerError, err,
  66. "authority.authorizeToken: failed when attempting to store token")
  67. }
  68. if !ok {
  69. return nil, errs.Unauthorized("authority.authorizeToken: token already used")
  70. }
  71. }
  72. }
  73. return p, nil
  74. }
  75. // Authorize grabs the method from the context and authorizes the request by
  76. // validating the one-time-token.
  77. func (a *Authority) Authorize(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  78. var opts = []interface{}{errs.WithKeyVal("token", token)}
  79. switch m := provisioner.MethodFromContext(ctx); m {
  80. case provisioner.SignMethod:
  81. signOpts, err := a.authorizeSign(ctx, token)
  82. return signOpts, errs.Wrap(http.StatusInternalServerError, err, "authority.Authorize", opts...)
  83. case provisioner.RevokeMethod:
  84. return nil, errs.Wrap(http.StatusInternalServerError, a.authorizeRevoke(ctx, token), "authority.Authorize", opts...)
  85. case provisioner.SSHSignMethod:
  86. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  87. return nil, errs.NotImplemented("authority.Authorize; ssh certificate flows are not enabled", opts...)
  88. }
  89. _, err := a.authorizeSSHSign(ctx, token)
  90. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Authorize", opts...)
  91. case provisioner.SSHRenewMethod:
  92. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  93. return nil, errs.NotImplemented("authority.Authorize; ssh certificate flows are not enabled", opts...)
  94. }
  95. _, err := a.authorizeSSHRenew(ctx, token)
  96. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.Authorize", opts...)
  97. case provisioner.SSHRevokeMethod:
  98. return nil, errs.Wrap(http.StatusInternalServerError, a.authorizeSSHRevoke(ctx, token), "authority.Authorize", opts...)
  99. case provisioner.SSHRekeyMethod:
  100. if a.sshCAHostCertSignKey == nil && a.sshCAUserCertSignKey == nil {
  101. return nil, errs.NotImplemented("authority.Authorize; ssh certificate flows are not enabled", opts...)
  102. }
  103. _, signOpts, err := a.authorizeSSHRekey(ctx, token)
  104. return signOpts, errs.Wrap(http.StatusInternalServerError, err, "authority.Authorize", opts...)
  105. default:
  106. return nil, errs.InternalServer("authority.Authorize; method %d is not supported", append([]interface{}{m}, opts...)...)
  107. }
  108. }
  109. // authorizeSign loads the provisioner from the token and calls the provisioner
  110. // AuthorizeSign method. Returns a list of methods to apply to the signing flow.
  111. func (a *Authority) authorizeSign(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  112. p, err := a.authorizeToken(ctx, token)
  113. if err != nil {
  114. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSign")
  115. }
  116. signOpts, err := p.AuthorizeSign(ctx, token)
  117. if err != nil {
  118. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSign")
  119. }
  120. return signOpts, nil
  121. }
  122. // AuthorizeSign authorizes a signature request by validating and authenticating
  123. // a token that must be sent w/ the request.
  124. //
  125. // NOTE: This method is deprecated and should not be used. We make it available
  126. // in the short term os as not to break existing clients.
  127. func (a *Authority) AuthorizeSign(token string) ([]provisioner.SignOption, error) {
  128. ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.SignMethod)
  129. return a.Authorize(ctx, token)
  130. }
  131. // authorizeRevoke locates the provisioner used to generate the authenticating
  132. // token and then performs the token validation flow.
  133. func (a *Authority) authorizeRevoke(ctx context.Context, token string) error {
  134. p, err := a.authorizeToken(ctx, token)
  135. if err != nil {
  136. return errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeRevoke")
  137. }
  138. if err = p.AuthorizeRevoke(ctx, token); err != nil {
  139. return errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeRevoke")
  140. }
  141. return nil
  142. }
  143. // authorizeRenew locates the provisioner (using the provisioner extension in the cert), and checks
  144. // if for the configured provisioner, the renewal is enabled or not. If the
  145. // extra extension cannot be found, authorize the renewal by default.
  146. //
  147. // TODO(mariano): should we authorize by default?
  148. func (a *Authority) authorizeRenew(cert *x509.Certificate) error {
  149. var opts = []interface{}{errs.WithKeyVal("serialNumber", cert.SerialNumber.String())}
  150. // Check the passive revocation table.
  151. isRevoked, err := a.db.IsRevoked(cert.SerialNumber.String())
  152. if err != nil {
  153. return errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeRenew", opts...)
  154. }
  155. if isRevoked {
  156. return errs.Unauthorized("authority.authorizeRenew: certificate has been revoked", opts...)
  157. }
  158. p, ok := a.provisioners.LoadByCertificate(cert)
  159. if !ok {
  160. return errs.Unauthorized("authority.authorizeRenew: provisioner not found", opts...)
  161. }
  162. if err := p.AuthorizeRenew(context.Background(), cert); err != nil {
  163. return errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeRenew", opts...)
  164. }
  165. return nil
  166. }
  167. // authorizeSSHSign loads the provisioner from the token, checks that it has not
  168. // been used again and calls the provisioner AuthorizeSSHSign method. Returns a
  169. // list of methods to apply to the signing flow.
  170. func (a *Authority) authorizeSSHSign(ctx context.Context, token string) ([]provisioner.SignOption, error) {
  171. p, err := a.authorizeToken(ctx, token)
  172. if err != nil {
  173. return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.authorizeSSHSign")
  174. }
  175. signOpts, err := p.AuthorizeSSHSign(ctx, token)
  176. if err != nil {
  177. return nil, errs.Wrap(http.StatusUnauthorized, err, "authority.authorizeSSHSign")
  178. }
  179. return signOpts, nil
  180. }
  181. // authorizeSSHRenew authorizes an SSH certificate renewal request, by
  182. // validating the contents of an SSHPOP token.
  183. func (a *Authority) authorizeSSHRenew(ctx context.Context, token string) (*ssh.Certificate, error) {
  184. p, err := a.authorizeToken(ctx, token)
  185. if err != nil {
  186. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSSHRenew")
  187. }
  188. cert, err := p.AuthorizeSSHRenew(ctx, token)
  189. if err != nil {
  190. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSSHRenew")
  191. }
  192. return cert, nil
  193. }
  194. // authorizeSSHRekey authorizes an SSH certificate rekey request, by
  195. // validating the contents of an SSHPOP token.
  196. func (a *Authority) authorizeSSHRekey(ctx context.Context, token string) (*ssh.Certificate, []provisioner.SignOption, error) {
  197. p, err := a.authorizeToken(ctx, token)
  198. if err != nil {
  199. return nil, nil, errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSSHRekey")
  200. }
  201. cert, signOpts, err := p.AuthorizeSSHRekey(ctx, token)
  202. if err != nil {
  203. return nil, nil, errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSSHRekey")
  204. }
  205. return cert, signOpts, nil
  206. }
  207. // authorizeSSHRevoke authorizes an SSH certificate revoke request, by
  208. // validating the contents of an SSHPOP token.
  209. func (a *Authority) authorizeSSHRevoke(ctx context.Context, token string) error {
  210. p, err := a.authorizeToken(ctx, token)
  211. if err != nil {
  212. return errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSSHRevoke")
  213. }
  214. if err = p.AuthorizeSSHRevoke(ctx, token); err != nil {
  215. return errs.Wrap(http.StatusInternalServerError, err, "authority.authorizeSSHRevoke")
  216. }
  217. return nil
  218. }