ca.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package main
  2. import (
  3. "crypto/rand"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "os"
  10. "strings"
  11. "time"
  12. "github.com/skip2/go-qrcode"
  13. "github.com/slackhq/nebula/cert"
  14. "golang.org/x/crypto/ed25519"
  15. )
  16. type caFlags struct {
  17. set *flag.FlagSet
  18. name *string
  19. duration *time.Duration
  20. outKeyPath *string
  21. outCertPath *string
  22. outQRPath *string
  23. groups *string
  24. ips *string
  25. subnets *string
  26. }
  27. func newCaFlags() *caFlags {
  28. cf := caFlags{set: flag.NewFlagSet("ca", flag.ContinueOnError)}
  29. cf.set.Usage = func() {}
  30. cf.name = cf.set.String("name", "", "Required: name of the certificate authority")
  31. cf.duration = cf.set.Duration("duration", time.Duration(time.Hour*8760), "Optional: amount of time the certificate should be valid for. Valid time units are seconds: \"s\", minutes: \"m\", hours: \"h\"")
  32. cf.outKeyPath = cf.set.String("out-key", "ca.key", "Optional: path to write the private key to")
  33. cf.outCertPath = cf.set.String("out-crt", "ca.crt", "Optional: path to write the certificate to")
  34. cf.outQRPath = cf.set.String("out-qr", "", "Optional: output a qr code image (png) of the certificate")
  35. cf.groups = cf.set.String("groups", "", "Optional: comma separated list of groups. This will limit which groups subordinate certs can use")
  36. cf.ips = cf.set.String("ips", "", "Optional: comma separated list of ipv4 address and network in CIDR notation. This will limit which ipv4 addresses and networks subordinate certs can use for ip addresses")
  37. cf.subnets = cf.set.String("subnets", "", "Optional: comma separated list of ipv4 address and network in CIDR notation. This will limit which ipv4 addresses and networks subordinate certs can use in subnets")
  38. return &cf
  39. }
  40. func ca(args []string, out io.Writer, errOut io.Writer) error {
  41. cf := newCaFlags()
  42. err := cf.set.Parse(args)
  43. if err != nil {
  44. return err
  45. }
  46. if err := mustFlagString("name", cf.name); err != nil {
  47. return err
  48. }
  49. if err := mustFlagString("out-key", cf.outKeyPath); err != nil {
  50. return err
  51. }
  52. if err := mustFlagString("out-crt", cf.outCertPath); err != nil {
  53. return err
  54. }
  55. if *cf.duration <= 0 {
  56. return &helpError{"-duration must be greater than 0"}
  57. }
  58. var groups []string
  59. if *cf.groups != "" {
  60. for _, rg := range strings.Split(*cf.groups, ",") {
  61. g := strings.TrimSpace(rg)
  62. if g != "" {
  63. groups = append(groups, g)
  64. }
  65. }
  66. }
  67. var ips []*net.IPNet
  68. if *cf.ips != "" {
  69. for _, rs := range strings.Split(*cf.ips, ",") {
  70. rs := strings.Trim(rs, " ")
  71. if rs != "" {
  72. ip, ipNet, err := net.ParseCIDR(rs)
  73. if err != nil {
  74. return newHelpErrorf("invalid ip definition: %s", err)
  75. }
  76. if ip.To4() == nil {
  77. return newHelpErrorf("invalid ip definition: can only be ipv4, have %s", rs)
  78. }
  79. ipNet.IP = ip
  80. ips = append(ips, ipNet)
  81. }
  82. }
  83. }
  84. var subnets []*net.IPNet
  85. if *cf.subnets != "" {
  86. for _, rs := range strings.Split(*cf.subnets, ",") {
  87. rs := strings.Trim(rs, " ")
  88. if rs != "" {
  89. _, s, err := net.ParseCIDR(rs)
  90. if err != nil {
  91. return newHelpErrorf("invalid subnet definition: %s", err)
  92. }
  93. if s.IP.To4() == nil {
  94. return newHelpErrorf("invalid subnet definition: can only be ipv4, have %s", rs)
  95. }
  96. subnets = append(subnets, s)
  97. }
  98. }
  99. }
  100. pub, rawPriv, err := ed25519.GenerateKey(rand.Reader)
  101. if err != nil {
  102. return fmt.Errorf("error while generating ed25519 keys: %s", err)
  103. }
  104. nc := cert.NebulaCertificate{
  105. Details: cert.NebulaCertificateDetails{
  106. Name: *cf.name,
  107. Groups: groups,
  108. Ips: ips,
  109. Subnets: subnets,
  110. NotBefore: time.Now(),
  111. NotAfter: time.Now().Add(*cf.duration),
  112. PublicKey: pub,
  113. IsCA: true,
  114. },
  115. }
  116. if _, err := os.Stat(*cf.outKeyPath); err == nil {
  117. return fmt.Errorf("refusing to overwrite existing CA key: %s", *cf.outKeyPath)
  118. }
  119. if _, err := os.Stat(*cf.outCertPath); err == nil {
  120. return fmt.Errorf("refusing to overwrite existing CA cert: %s", *cf.outCertPath)
  121. }
  122. err = nc.Sign(rawPriv)
  123. if err != nil {
  124. return fmt.Errorf("error while signing: %s", err)
  125. }
  126. err = ioutil.WriteFile(*cf.outKeyPath, cert.MarshalEd25519PrivateKey(rawPriv), 0600)
  127. if err != nil {
  128. return fmt.Errorf("error while writing out-key: %s", err)
  129. }
  130. b, err := nc.MarshalToPEM()
  131. if err != nil {
  132. return fmt.Errorf("error while marshalling certificate: %s", err)
  133. }
  134. err = ioutil.WriteFile(*cf.outCertPath, b, 0600)
  135. if err != nil {
  136. return fmt.Errorf("error while writing out-crt: %s", err)
  137. }
  138. if *cf.outQRPath != "" {
  139. b, err = qrcode.Encode(string(b), qrcode.Medium, -5)
  140. if err != nil {
  141. return fmt.Errorf("error while generating qr code: %s", err)
  142. }
  143. err = ioutil.WriteFile(*cf.outQRPath, b, 0600)
  144. if err != nil {
  145. return fmt.Errorf("error while writing out-qr: %s", err)
  146. }
  147. }
  148. return nil
  149. }
  150. func caSummary() string {
  151. return "ca <flags>: create a self signed certificate authority"
  152. }
  153. func caHelp(out io.Writer) {
  154. cf := newCaFlags()
  155. out.Write([]byte("Usage of " + os.Args[0] + " " + caSummary() + "\n"))
  156. cf.set.SetOutput(out)
  157. cf.set.PrintDefaults()
  158. }