provisioner.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package ca
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "path/filepath"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/cli/config"
  10. "github.com/smallstep/cli/crypto/randutil"
  11. "github.com/smallstep/cli/jose"
  12. "github.com/smallstep/cli/token"
  13. "github.com/smallstep/cli/token/provision"
  14. )
  15. const (
  16. tokenLifetime = 5 * time.Minute
  17. )
  18. // Provisioner is an authorized entity that can sign tokens necessary for
  19. // signature requests.
  20. type Provisioner struct {
  21. name string
  22. kid string
  23. caURL string
  24. caRoot string
  25. jwk *jose.JSONWebKey
  26. tokenLifetime time.Duration
  27. }
  28. // NewProvisioner loads and decrypts key material from the CA for the named
  29. // provisioner. The key identified by `kid` will be used if specified. If `kid`
  30. // is the empty string we'll use the first key for the named provisioner that
  31. // decrypts using `password`.
  32. func NewProvisioner(name, kid, caURL, caRoot string, password []byte) (*Provisioner, error) {
  33. var jwk *jose.JSONWebKey
  34. var err error
  35. switch {
  36. case name == "":
  37. return nil, errors.New("provisioner name cannot be empty")
  38. case kid == "":
  39. jwk, err = loadProvisionerJWKByName(name, caURL, caRoot, password)
  40. default:
  41. jwk, err = loadProvisionerJWKByKid(kid, caURL, caRoot, password)
  42. }
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &Provisioner{
  47. name: name,
  48. kid: jwk.KeyID,
  49. caURL: caURL,
  50. caRoot: caRoot,
  51. jwk: jwk,
  52. tokenLifetime: tokenLifetime,
  53. }, nil
  54. }
  55. // Name returns the provisioner's name.
  56. func (p *Provisioner) Name() string {
  57. return p.name
  58. }
  59. // Kid returns the provisioners key ID.
  60. func (p *Provisioner) Kid() string {
  61. return p.kid
  62. }
  63. // Token generates a bootstrap token for a subject.
  64. func (p *Provisioner) Token(subject string) (string, error) {
  65. // A random jwt id will be used to identify duplicated tokens
  66. jwtID, err := randutil.Hex(64) // 256 bits
  67. if err != nil {
  68. return "", err
  69. }
  70. notBefore := time.Now()
  71. notAfter := notBefore.Add(tokenLifetime)
  72. signURL := fmt.Sprintf("%v/1.0/sign", p.caURL)
  73. tokOptions := []token.Options{
  74. token.WithJWTID(jwtID),
  75. token.WithKid(p.kid),
  76. token.WithIssuer(p.name),
  77. token.WithAudience(signURL),
  78. token.WithValidity(notBefore, notAfter),
  79. token.WithRootCA(p.caRoot),
  80. token.WithSANS([]string{subject}),
  81. }
  82. tok, err := provision.New(subject, tokOptions...)
  83. if err != nil {
  84. return "", err
  85. }
  86. return tok.SignedString(p.jwk.Algorithm, p.jwk.Key)
  87. }
  88. func decryptProvisionerJWK(encryptedKey string, password []byte) (*jose.JSONWebKey, error) {
  89. enc, err := jose.ParseEncrypted(encryptedKey)
  90. if err != nil {
  91. return nil, err
  92. }
  93. data, err := enc.Decrypt(password)
  94. if err != nil {
  95. return nil, err
  96. }
  97. jwk := new(jose.JSONWebKey)
  98. if err := json.Unmarshal(data, jwk); err != nil {
  99. return nil, errors.Wrap(err, "error unmarshaling provisioning key")
  100. }
  101. return jwk, nil
  102. }
  103. // loadProvisionerJWKByKid retrieves a provisioner key from the CA by key ID and
  104. // decrypts it using the specified password.
  105. func loadProvisionerJWKByKid(kid, caURL, caRoot string, password []byte) (*jose.JSONWebKey, error) {
  106. encrypted, err := getProvisionerKey(caURL, caRoot, kid)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return decryptProvisionerJWK(encrypted, password)
  111. }
  112. // loadProvisionerJWKByName retrieves the list of provisioners and encrypted key then
  113. // returns the key of the first provisioner with a matching name that can be successfully
  114. // decrypted with the specified password.
  115. func loadProvisionerJWKByName(name, caURL, caRoot string, password []byte) (key *jose.JSONWebKey, err error) {
  116. provisioners, err := getProvisioners(caURL, caRoot)
  117. if err != nil {
  118. err = errors.Wrap(err, "error getting the provisioners")
  119. return
  120. }
  121. for _, provisioner := range provisioners {
  122. if provisioner.GetName() == name {
  123. if _, encryptedKey, ok := provisioner.GetEncryptedKey(); ok {
  124. key, err = decryptProvisionerJWK(encryptedKey, password)
  125. if err == nil {
  126. return
  127. }
  128. }
  129. }
  130. }
  131. return nil, errors.Errorf("provisioner '%s' not found (or your password is wrong)", name)
  132. }
  133. // getRootCAPath returns the path where the root CA is stored based on the
  134. // STEPPATH environment variable.
  135. func getRootCAPath() string {
  136. return filepath.Join(config.StepPath(), "certs", "root_ca.crt")
  137. }
  138. // getProvisioners returns the map of provisioners on the given CA.
  139. func getProvisioners(caURL, rootFile string) (provisioner.List, error) {
  140. if len(rootFile) == 0 {
  141. rootFile = getRootCAPath()
  142. }
  143. client, err := NewClient(caURL, WithRootFile(rootFile))
  144. if err != nil {
  145. return nil, err
  146. }
  147. cursor := ""
  148. var provisioners provisioner.List
  149. for {
  150. resp, err := client.Provisioners(WithProvisionerCursor(cursor), WithProvisionerLimit(100))
  151. if err != nil {
  152. return nil, err
  153. }
  154. provisioners = append(provisioners, resp.Provisioners...)
  155. if resp.NextCursor == "" {
  156. return provisioners, nil
  157. }
  158. cursor = resp.NextCursor
  159. }
  160. }
  161. // getProvisionerKey returns the encrypted provisioner key for the given kid.
  162. func getProvisionerKey(caURL, rootFile, kid string) (string, error) {
  163. if len(rootFile) == 0 {
  164. rootFile = getRootCAPath()
  165. }
  166. client, err := NewClient(caURL, WithRootFile(rootFile))
  167. if err != nil {
  168. return "", err
  169. }
  170. resp, err := client.ProvisionerKey(kid)
  171. if err != nil {
  172. return "", err
  173. }
  174. return resp.Key, nil
  175. }