main.go 5.5 KB

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