main.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.Fprintf(os.Stderr, `
  67. COPYRIGHT
  68. (c) 2018-%d Smallstep Labs, Inc.
  69. `, time.Now().Year())
  70. os.Exit(1)
  71. }
  72. func createX509(c *awskms.KMS) error {
  73. ui.Println("Creating X.509 PKI ...")
  74. // Root Certificate
  75. resp, err := c.CreateKey(&apiv1.CreateKeyRequest{
  76. Name: "root",
  77. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  78. })
  79. if err != nil {
  80. return err
  81. }
  82. signer, err := c.CreateSigner(&resp.CreateSignerRequest)
  83. if err != nil {
  84. return err
  85. }
  86. now := time.Now()
  87. root := &x509.Certificate{
  88. IsCA: true,
  89. NotBefore: now,
  90. NotAfter: now.Add(time.Hour * 24 * 365 * 10),
  91. KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
  92. BasicConstraintsValid: true,
  93. MaxPathLen: 1,
  94. MaxPathLenZero: false,
  95. Issuer: pkix.Name{CommonName: "Smallstep Root"},
  96. Subject: pkix.Name{CommonName: "Smallstep Root"},
  97. SerialNumber: mustSerialNumber(),
  98. SubjectKeyId: mustSubjectKeyID(resp.PublicKey),
  99. AuthorityKeyId: mustSubjectKeyID(resp.PublicKey),
  100. }
  101. b, err := x509.CreateCertificate(rand.Reader, root, root, resp.PublicKey, signer)
  102. if err != nil {
  103. return err
  104. }
  105. if err := fileutil.WriteFile("root_ca.crt", pem.EncodeToMemory(&pem.Block{
  106. Type: "CERTIFICATE",
  107. Bytes: b,
  108. }), 0600); err != nil {
  109. return err
  110. }
  111. ui.PrintSelected("Root Key", resp.Name)
  112. ui.PrintSelected("Root Certificate", "root_ca.crt")
  113. root, err = pemutil.ReadCertificate("root_ca.crt")
  114. if err != nil {
  115. return err
  116. }
  117. // Intermediate Certificate
  118. resp, err = c.CreateKey(&apiv1.CreateKeyRequest{
  119. Name: "intermediate",
  120. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  121. })
  122. if err != nil {
  123. return err
  124. }
  125. intermediate := &x509.Certificate{
  126. IsCA: true,
  127. NotBefore: now,
  128. NotAfter: now.Add(time.Hour * 24 * 365 * 10),
  129. KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
  130. BasicConstraintsValid: true,
  131. MaxPathLen: 0,
  132. MaxPathLenZero: true,
  133. Issuer: root.Subject,
  134. Subject: pkix.Name{CommonName: "Smallstep Intermediate"},
  135. SerialNumber: mustSerialNumber(),
  136. SubjectKeyId: mustSubjectKeyID(resp.PublicKey),
  137. }
  138. b, err = x509.CreateCertificate(rand.Reader, intermediate, root, resp.PublicKey, signer)
  139. if err != nil {
  140. return err
  141. }
  142. if err := fileutil.WriteFile("intermediate_ca.crt", pem.EncodeToMemory(&pem.Block{
  143. Type: "CERTIFICATE",
  144. Bytes: b,
  145. }), 0600); err != nil {
  146. return err
  147. }
  148. ui.PrintSelected("Intermediate Key", resp.Name)
  149. ui.PrintSelected("Intermediate Certificate", "intermediate_ca.crt")
  150. return nil
  151. }
  152. func createSSH(c *awskms.KMS) error {
  153. ui.Println("Creating SSH Keys ...")
  154. // User Key
  155. resp, err := c.CreateKey(&apiv1.CreateKeyRequest{
  156. Name: "ssh-user-key",
  157. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  158. })
  159. if err != nil {
  160. return err
  161. }
  162. key, err := ssh.NewPublicKey(resp.PublicKey)
  163. if err != nil {
  164. return err
  165. }
  166. if err := fileutil.WriteFile("ssh_user_ca_key.pub", ssh.MarshalAuthorizedKey(key), 0600); err != nil {
  167. return err
  168. }
  169. ui.PrintSelected("SSH User Public Key", "ssh_user_ca_key.pub")
  170. ui.PrintSelected("SSH User Private Key", resp.Name)
  171. // Host Key
  172. resp, err = c.CreateKey(&apiv1.CreateKeyRequest{
  173. Name: "ssh-host-key",
  174. SignatureAlgorithm: apiv1.ECDSAWithSHA256,
  175. })
  176. if err != nil {
  177. return err
  178. }
  179. key, err = ssh.NewPublicKey(resp.PublicKey)
  180. if err != nil {
  181. return err
  182. }
  183. if err := fileutil.WriteFile("ssh_host_ca_key.pub", ssh.MarshalAuthorizedKey(key), 0600); err != nil {
  184. return err
  185. }
  186. ui.PrintSelected("SSH Host Public Key", "ssh_host_ca_key.pub")
  187. ui.PrintSelected("SSH Host Private Key", resp.Name)
  188. return nil
  189. }
  190. func mustSerialNumber() *big.Int {
  191. serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  192. sn, err := rand.Int(rand.Reader, serialNumberLimit)
  193. if err != nil {
  194. panic(err)
  195. }
  196. return sn
  197. }
  198. func mustSubjectKeyID(key crypto.PublicKey) []byte {
  199. b, err := x509.MarshalPKIXPublicKey(key)
  200. if err != nil {
  201. panic(err)
  202. }
  203. hash := sha1.Sum(b)
  204. return hash[:]
  205. }