jwk.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "time"
  5. "github.com/pkg/errors"
  6. "github.com/smallstep/cli/crypto/x509util"
  7. "github.com/smallstep/cli/jose"
  8. )
  9. // jwtPayload extends jwt.Claims with step attributes.
  10. type jwtPayload struct {
  11. jose.Claims
  12. SANs []string `json:"sans,omitempty"`
  13. }
  14. // JWK is the default provisioner, an entity that can sign tokens necessary for
  15. // signature requests.
  16. type JWK struct {
  17. Name string `json:"name,omitempty"`
  18. Type string `json:"type,omitempty"`
  19. Key *jose.JSONWebKey `json:"key,omitempty"`
  20. EncryptedKey string `json:"encryptedKey,omitempty"`
  21. Claims *Claims `json:"claims,omitempty"`
  22. audiences []string
  23. }
  24. // GetID returns the provisioner unique identifier. The name and credential id
  25. // should uniquely identify any JWK provisioner.
  26. func (p *JWK) GetID() string {
  27. return p.Name + ":" + p.Key.KeyID
  28. }
  29. // GetName returns the name of the provisioner.
  30. func (p *JWK) GetName() string {
  31. return p.Name
  32. }
  33. // GetType returns the type of provisioner.
  34. func (p *JWK) GetType() Type {
  35. return TypeJWK
  36. }
  37. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  38. func (p *JWK) GetEncryptedKey() (string, string, bool) {
  39. return p.Key.KeyID, p.EncryptedKey, len(p.EncryptedKey) > 0
  40. }
  41. // Init initializes and validates a the fields of Provisioner type.
  42. func (p *JWK) Init(config Config) (err error) {
  43. switch {
  44. case p.Name == "":
  45. return errors.New("provisioner name cannot be empty")
  46. case p.Type == "":
  47. return errors.New("provisioner type cannot be empty")
  48. case p.Key == nil:
  49. return errors.New("provisioner key cannot be empty")
  50. }
  51. p.Claims, err = p.Claims.Init(&config.Claims)
  52. p.audiences = config.Audiences
  53. return err
  54. }
  55. // Authorize validates the given token.
  56. func (p *JWK) Authorize(token string) ([]SignOption, error) {
  57. jwt, err := jose.ParseSigned(token)
  58. if err != nil {
  59. return nil, errors.Wrapf(err, "error parsing token")
  60. }
  61. var claims jwtPayload
  62. if err = jwt.Claims(p.Key, &claims); err != nil {
  63. return nil, errors.Wrap(err, "error parsing claims")
  64. }
  65. // According to "rfc7519 JSON Web Token" acceptable skew should be no
  66. // more than a few minutes.
  67. if err = claims.ValidateWithLeeway(jose.Expected{
  68. Issuer: p.Name,
  69. Time: time.Now().UTC(),
  70. }, time.Minute); err != nil {
  71. return nil, errors.Wrapf(err, "invalid token")
  72. }
  73. // validate audiences with the defaults
  74. if !matchesAudience(claims.Audience, p.audiences) {
  75. return nil, errors.New("invalid token: invalid audience claim (aud)")
  76. }
  77. if claims.Subject == "" {
  78. return nil, errors.New("token subject cannot be empty")
  79. }
  80. // NOTE: This is for backwards compatibility with older versions of cli
  81. // and certificates. Older versions added the token subject as the only SAN
  82. // in a CSR by default.
  83. if len(claims.SANs) == 0 {
  84. claims.SANs = []string{claims.Subject}
  85. }
  86. dnsNames, ips := x509util.SplitSANs(claims.SANs)
  87. return []SignOption{
  88. commonNameValidator(claims.Subject),
  89. dnsNamesValidator(dnsNames),
  90. ipAddressesValidator(ips),
  91. profileDefaultDuration(p.Claims.DefaultTLSCertDuration()),
  92. newProvisionerExtensionOption(TypeJWK, p.Name, p.Key.KeyID),
  93. newValidityValidator(p.Claims.MinTLSCertDuration(), p.Claims.MaxTLSCertDuration()),
  94. }, nil
  95. }
  96. // AuthorizeRenewal returns an error if the renewal is disabled.
  97. func (p *JWK) AuthorizeRenewal(cert *x509.Certificate) error {
  98. if p.Claims.IsDisableRenewal() {
  99. return errors.Errorf("renew is disabled for provisioner %s", p.GetID())
  100. }
  101. return nil
  102. }
  103. // AuthorizeRevoke returns an error if the provisioner does not have rights to
  104. // revoke the certificate with serial number in the `sub` property.
  105. func (p *JWK) AuthorizeRevoke(token string) error {
  106. return errors.New("not implemented")
  107. }