main.go 5.6 KB

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