4
0

authorize.go 10 KB

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