main.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package main
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/sha1" //nolint:gosec // used to create the Subject Key Identifier by RFC 5280
  7. "crypto/x509"
  8. "crypto/x509/pkix"
  9. "encoding/pem"
  10. "flag"
  11. "fmt"
  12. "math/big"
  13. "os"
  14. "time"
  15. "go.step.sm/cli-utils/fileutil"
  16. "go.step.sm/cli-utils/ui"
  17. "go.step.sm/crypto/kms/apiv1"
  18. "go.step.sm/crypto/kms/awskms"
  19. "go.step.sm/crypto/pemutil"
  20. "golang.org/x/crypto/ssh"
  21. )
  22. func main() {
  23. var credentialsFile, region string
  24. var enableSSH bool
  25. flag.StringVar(&credentialsFile, "credentials-file", "", "Path to the `file` containing the AWS KMS credentials.")
  26. flag.StringVar(&region, "region", "", "AWS KMS region name.")
  27. flag.BoolVar(&enableSSH, "ssh", false, "Create SSH keys.")
  28. flag.Usage = usage
  29. flag.Parse()
  30. // Initialize windows terminal
  31. ui.Init()
  32. ui.Println("⚠️ This command is deprecated and will be removed in future releases.")
  33. ui.Println("⚠️ Please use https://github.com/smallstep/step-kms-plugin instead.")
  34. c, err := awskms.New(context.Background(), apiv1.Options{
  35. Type: apiv1.AmazonKMS,
  36. Region: region,
  37. CredentialsFile: credentialsFile,
  38. })
  39. if err != nil {
  40. fatal(err)
  41. }
  42. if err := createX509(c); err != nil {
  43. fatal(err)
  44. }
  45. if enableSSH {
  46. ui.Println()
  47. if err := createSSH(c); err != nil {
  48. fatal(err)
  49. }
  50. }
  51. // Reset windows terminal
  52. ui.Reset()
  53. }
  54. func fatal(err error) {
  55. fmt.Fprintln(os.Stderr, err)
  56. ui.Reset()
  57. os.Exit(1)
  58. }
  59. func usage() {
  60. fmt.Fprintln(os.Stderr, "Usage: step-awskms-init")
  61. fmt.Fprintln(os.Stderr, `
  62. The step-awskms-init command initializes a public key infrastructure (PKI)
  63. to be used by step-ca.
  64. This tool is experimental and in the future it will be integrated in step cli.
  65. OPTIONS`)
  66. fmt.Fprintln(os.Stderr)
  67. flag.PrintDefaults()
  68. fmt.Fprintf(os.Stderr, `
  69. COPYRIGHT
  70. (c) 2018-%d Smallstep Labs, Inc.
  71. `, time.Now().Year())
  72. os.Exit(1)
  73. }
  74. func createX509(c *awskms.KMS) error {
  75. ui.Println("Creating X.509 PKI ...")
  76. // Root Certificate
  77. resp, err := c.CreateKey(&apiv1.CreateKeyRequest{
  78. Name: "root",
  79. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  80. })
  81. if err != nil {
  82. return err
  83. }
  84. signer, err := c.CreateSigner(&resp.CreateSignerRequest)
  85. if err != nil {
  86. return err
  87. }
  88. now := time.Now()
  89. root := &x509.Certificate{
  90. IsCA: true,
  91. NotBefore: now,
  92. NotAfter: now.Add(time.Hour * 24 * 365 * 10),
  93. KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
  94. BasicConstraintsValid: true,
  95. MaxPathLen: 1,
  96. MaxPathLenZero: false,
  97. Issuer: pkix.Name{CommonName: "Smallstep Root"},
  98. Subject: pkix.Name{CommonName: "Smallstep Root"},
  99. SerialNumber: mustSerialNumber(),
  100. SubjectKeyId: mustSubjectKeyID(resp.PublicKey),
  101. AuthorityKeyId: mustSubjectKeyID(resp.PublicKey),
  102. }
  103. b, err := x509.CreateCertificate(rand.Reader, root, root, resp.PublicKey, signer)
  104. if err != nil {
  105. return err
  106. }
  107. if err := fileutil.WriteFile("root_ca.crt", pem.EncodeToMemory(&pem.Block{
  108. Type: "CERTIFICATE",
  109. Bytes: b,
  110. }), 0600); err != nil {
  111. return err
  112. }
  113. ui.PrintSelected("Root Key", resp.Name)
  114. ui.PrintSelected("Root Certificate", "root_ca.crt")
  115. root, err = pemutil.ReadCertificate("root_ca.crt")
  116. if err != nil {
  117. return err
  118. }
  119. // Intermediate Certificate
  120. resp, err = c.CreateKey(&apiv1.CreateKeyRequest{
  121. Name: "intermediate",
  122. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  123. })
  124. if err != nil {
  125. return err
  126. }
  127. intermediate := &x509.Certificate{
  128. IsCA: true,
  129. NotBefore: now,
  130. NotAfter: now.Add(time.Hour * 24 * 365 * 10),
  131. KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
  132. BasicConstraintsValid: true,
  133. MaxPathLen: 0,
  134. MaxPathLenZero: true,
  135. Issuer: root.Subject,
  136. Subject: pkix.Name{CommonName: "Smallstep Intermediate"},
  137. SerialNumber: mustSerialNumber(),
  138. SubjectKeyId: mustSubjectKeyID(resp.PublicKey),
  139. }
  140. b, err = x509.CreateCertificate(rand.Reader, intermediate, root, resp.PublicKey, signer)
  141. if err != nil {
  142. return err
  143. }
  144. if err := fileutil.WriteFile("intermediate_ca.crt", pem.EncodeToMemory(&pem.Block{
  145. Type: "CERTIFICATE",
  146. Bytes: b,
  147. }), 0600); err != nil {
  148. return err
  149. }
  150. ui.PrintSelected("Intermediate Key", resp.Name)
  151. ui.PrintSelected("Intermediate Certificate", "intermediate_ca.crt")
  152. return nil
  153. }
  154. func createSSH(c *awskms.KMS) error {
  155. ui.Println("Creating SSH Keys ...")
  156. // User Key
  157. resp, err := c.CreateKey(&apiv1.CreateKeyRequest{
  158. Name: "ssh-user-key",
  159. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  160. })
  161. if err != nil {
  162. return err
  163. }
  164. key, err := ssh.NewPublicKey(resp.PublicKey)
  165. if err != nil {
  166. return err
  167. }
  168. if err := fileutil.WriteFile("ssh_user_ca_key.pub", ssh.MarshalAuthorizedKey(key), 0600); err != nil {
  169. return err
  170. }
  171. ui.PrintSelected("SSH User Public Key", "ssh_user_ca_key.pub")
  172. ui.PrintSelected("SSH User Private Key", resp.Name)
  173. // Host Key
  174. resp, err = c.CreateKey(&apiv1.CreateKeyRequest{
  175. Name: "ssh-host-key",
  176. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  177. })
  178. if err != nil {
  179. return err
  180. }
  181. key, err = ssh.NewPublicKey(resp.PublicKey)
  182. if err != nil {
  183. return err
  184. }
  185. if err := fileutil.WriteFile("ssh_host_ca_key.pub", ssh.MarshalAuthorizedKey(key), 0600); err != nil {
  186. return err
  187. }
  188. ui.PrintSelected("SSH Host Public Key", "ssh_host_ca_key.pub")
  189. ui.PrintSelected("SSH Host Private Key", resp.Name)
  190. return nil
  191. }
  192. func mustSerialNumber() *big.Int {
  193. serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  194. sn, err := rand.Int(rand.Reader, serialNumberLimit)
  195. if err != nil {
  196. panic(err)
  197. }
  198. return sn
  199. }
  200. func mustSubjectKeyID(key crypto.PublicKey) []byte {
  201. b, err := x509.MarshalPKIXPublicKey(key)
  202. if err != nil {
  203. panic(err)
  204. }
  205. //nolint:gosec // used to create the Subject Key Identifier by RFC 5280
  206. hash := sha1.Sum(b)
  207. return hash[:]
  208. }