authorize.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package authority
  2. import (
  3. "crypto/x509"
  4. "encoding/asn1"
  5. "net/http"
  6. "time"
  7. "github.com/pkg/errors"
  8. "gopkg.in/square/go-jose.v2/jwt"
  9. )
  10. type idUsed struct {
  11. UsedAt int64 `json:"ua,omitempty"`
  12. Subject string `json:"sub,omitempty"`
  13. }
  14. // matchesOne returns true if A and B share at least one element.
  15. func matchesOne(as, bs []string) bool {
  16. if len(bs) == 0 || len(as) == 0 {
  17. return false
  18. }
  19. for _, b := range bs {
  20. for _, a := range as {
  21. if b == a {
  22. return true
  23. }
  24. }
  25. }
  26. return false
  27. }
  28. // Authorize authorizes a signature request by validating and authenticating
  29. // a OTT that must be sent w/ the request.
  30. func (a *Authority) Authorize(ott string) ([]interface{}, error) {
  31. var (
  32. errContext = map[string]interface{}{"ott": ott}
  33. claims = jwt.Claims{}
  34. )
  35. // Validate payload
  36. token, err := jwt.ParseSigned(ott)
  37. if err != nil {
  38. return nil, &apiError{errors.Wrapf(err, "authorize: error parsing token"),
  39. http.StatusUnauthorized, errContext}
  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. if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
  45. return nil, &apiError{err, http.StatusUnauthorized, errContext}
  46. }
  47. kid := token.Headers[0].KeyID // JWT will only have 1 header.
  48. if len(kid) == 0 {
  49. return nil, &apiError{errors.New("authorize: token KeyID cannot be empty"),
  50. http.StatusUnauthorized, errContext}
  51. }
  52. pid := claims.Issuer + ":" + kid
  53. val, ok := a.provisionerIDIndex.Load(pid)
  54. if !ok {
  55. return nil, &apiError{errors.Errorf("authorize: provisioner with id %s not found", pid),
  56. http.StatusUnauthorized, errContext}
  57. }
  58. p, ok := val.(*Provisioner)
  59. if !ok {
  60. return nil, &apiError{errors.Errorf("authorize: invalid provisioner type"),
  61. http.StatusInternalServerError, errContext}
  62. }
  63. if err = token.Claims(p.Key, &claims); err != nil {
  64. return nil, &apiError{err, http.StatusUnauthorized, errContext}
  65. }
  66. // According to "rfc7519 JSON Web Token" acceptable skew should be no
  67. // more than a few minutes.
  68. if err = claims.ValidateWithLeeway(jwt.Expected{
  69. Issuer: p.Name,
  70. }, time.Minute); err != nil {
  71. return nil, &apiError{errors.Wrapf(err, "authorize: invalid token"),
  72. http.StatusUnauthorized, errContext}
  73. }
  74. // Do not accept tokens issued before the start of the ca.
  75. // This check is meant as a stopgap solution to the current lack of a persistence layer.
  76. if a.config.AuthorityConfig != nil && !a.config.AuthorityConfig.DisableIssuedAtCheck {
  77. if claims.IssuedAt > 0 && claims.IssuedAt.Time().Before(a.startTime) {
  78. return nil, &apiError{errors.New("token issued before the bootstrap of certificate authority"),
  79. http.StatusUnauthorized, errContext}
  80. }
  81. }
  82. if !matchesOne(claims.Audience, a.audiences) {
  83. return nil, &apiError{errors.New("authorize: token audience invalid"), http.StatusUnauthorized,
  84. errContext}
  85. }
  86. if claims.Subject == "" {
  87. return nil, &apiError{errors.New("authorize: token subject cannot be empty"),
  88. http.StatusUnauthorized, errContext}
  89. }
  90. signOps := []interface{}{
  91. &commonNameClaim{claims.Subject},
  92. &dnsNamesClaim{claims.Subject},
  93. &ipAddressesClaim{claims.Subject},
  94. p,
  95. }
  96. // Store the token to protect against reuse.
  97. if _, ok := a.ottMap.LoadOrStore(claims.ID, &idUsed{
  98. UsedAt: time.Now().Unix(),
  99. Subject: claims.Subject,
  100. }); ok {
  101. return nil, &apiError{errors.Errorf("token already used"), http.StatusUnauthorized,
  102. errContext}
  103. }
  104. return signOps, nil
  105. }
  106. // authorizeRenewal tries to locate the step provisioner extension, and checks
  107. // if for the configured provisioner, the renewal is enabled or not. If the
  108. // extra extension cannot be found, authorize the renewal by default.
  109. //
  110. // TODO(mariano): should we authorize by default?
  111. func (a *Authority) authorizeRenewal(crt *x509.Certificate) error {
  112. errContext := map[string]interface{}{"serialNumber": crt.SerialNumber.String()}
  113. for _, e := range crt.Extensions {
  114. if e.Id.Equal(stepOIDProvisioner) {
  115. var provisioner stepProvisionerASN1
  116. if _, err := asn1.Unmarshal(e.Value, &provisioner); err != nil {
  117. return &apiError{
  118. err: errors.Wrap(err, "error decoding step provisioner extension"),
  119. code: http.StatusInternalServerError,
  120. context: errContext,
  121. }
  122. }
  123. // Look for the provisioner, if it cannot be found, renewal will not
  124. // be authorized.
  125. pid := string(provisioner.Name) + ":" + string(provisioner.CredentialID)
  126. val, ok := a.provisionerIDIndex.Load(pid)
  127. if !ok {
  128. return &apiError{
  129. err: errors.Errorf("not found: provisioner %s", pid),
  130. code: http.StatusUnauthorized,
  131. context: errContext,
  132. }
  133. }
  134. p, ok := val.(*Provisioner)
  135. if !ok {
  136. return &apiError{
  137. err: errors.Errorf("invalid type: provisioner %s, type %T", pid, val),
  138. code: http.StatusInternalServerError,
  139. context: errContext,
  140. }
  141. }
  142. if p.Claims.IsDisableRenewal() {
  143. return &apiError{
  144. err: errors.Errorf("renew disabled: provisioner %s", pid),
  145. code: http.StatusUnauthorized,
  146. context: errContext,
  147. }
  148. }
  149. return nil
  150. }
  151. }
  152. return nil
  153. }