ca.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package main
  2. import (
  3. "crypto/rand"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "math"
  9. "net"
  10. "os"
  11. "strings"
  12. "time"
  13. "github.com/skip2/go-qrcode"
  14. "github.com/slackhq/nebula/cert"
  15. "golang.org/x/crypto/ed25519"
  16. )
  17. type caFlags struct {
  18. set *flag.FlagSet
  19. name *string
  20. duration *time.Duration
  21. outKeyPath *string
  22. outCertPath *string
  23. outQRPath *string
  24. groups *string
  25. ips *string
  26. subnets *string
  27. argonMemory *uint
  28. argonIterations *uint
  29. argonParallelism *uint
  30. encryption *bool
  31. }
  32. func newCaFlags() *caFlags {
  33. cf := caFlags{set: flag.NewFlagSet("ca", flag.ContinueOnError)}
  34. cf.set.Usage = func() {}
  35. cf.name = cf.set.String("name", "", "Required: name of the certificate authority")
  36. 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\"")
  37. cf.outKeyPath = cf.set.String("out-key", "ca.key", "Optional: path to write the private key to")
  38. cf.outCertPath = cf.set.String("out-crt", "ca.crt", "Optional: path to write the certificate to")
  39. cf.outQRPath = cf.set.String("out-qr", "", "Optional: output a qr code image (png) of the certificate")
  40. cf.groups = cf.set.String("groups", "", "Optional: comma separated list of groups. This will limit which groups subordinate certs can use")
  41. 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")
  42. 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")
  43. cf.argonMemory = cf.set.Uint("argon-memory", 2*1024*1024, "Optional: Argon2 memory parameter (in KiB) used for encrypted private key passphrase")
  44. cf.argonParallelism = cf.set.Uint("argon-parallelism", 4, "Optional: Argon2 parallelism parameter used for encrypted private key passphrase")
  45. cf.argonIterations = cf.set.Uint("argon-iterations", 1, "Optional: Argon2 iterations parameter used for encrypted private key passphrase")
  46. cf.encryption = cf.set.Bool("encrypt", false, "Optional: prompt for passphrase and write out-key in an encrypted format")
  47. return &cf
  48. }
  49. func parseArgonParameters(memory uint, parallelism uint, iterations uint) (*cert.Argon2Parameters, error) {
  50. if memory <= 0 || memory > math.MaxUint32 {
  51. return nil, newHelpErrorf("-argon-memory must be be greater than 0 and no more than %d KiB", uint32(math.MaxUint32))
  52. }
  53. if parallelism <= 0 || parallelism > math.MaxUint8 {
  54. return nil, newHelpErrorf("-argon-parallelism must be be greater than 0 and no more than %d", math.MaxUint8)
  55. }
  56. if iterations <= 0 || iterations > math.MaxUint32 {
  57. return nil, newHelpErrorf("-argon-iterations must be be greater than 0 and no more than %d", uint32(math.MaxUint32))
  58. }
  59. return cert.NewArgon2Parameters(uint32(memory), uint8(parallelism), uint32(iterations)), nil
  60. }
  61. func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error {
  62. cf := newCaFlags()
  63. err := cf.set.Parse(args)
  64. if err != nil {
  65. return err
  66. }
  67. if err := mustFlagString("name", cf.name); err != nil {
  68. return err
  69. }
  70. if err := mustFlagString("out-key", cf.outKeyPath); err != nil {
  71. return err
  72. }
  73. if err := mustFlagString("out-crt", cf.outCertPath); err != nil {
  74. return err
  75. }
  76. var kdfParams *cert.Argon2Parameters
  77. if *cf.encryption {
  78. if kdfParams, err = parseArgonParameters(*cf.argonMemory, *cf.argonParallelism, *cf.argonIterations); err != nil {
  79. return err
  80. }
  81. }
  82. if *cf.duration <= 0 {
  83. return &helpError{"-duration must be greater than 0"}
  84. }
  85. var groups []string
  86. if *cf.groups != "" {
  87. for _, rg := range strings.Split(*cf.groups, ",") {
  88. g := strings.TrimSpace(rg)
  89. if g != "" {
  90. groups = append(groups, g)
  91. }
  92. }
  93. }
  94. var ips []*net.IPNet
  95. if *cf.ips != "" {
  96. for _, rs := range strings.Split(*cf.ips, ",") {
  97. rs := strings.Trim(rs, " ")
  98. if rs != "" {
  99. ip, ipNet, err := net.ParseCIDR(rs)
  100. if err != nil {
  101. return newHelpErrorf("invalid ip definition: %s", err)
  102. }
  103. if ip.To4() == nil {
  104. return newHelpErrorf("invalid ip definition: can only be ipv4, have %s", rs)
  105. }
  106. ipNet.IP = ip
  107. ips = append(ips, ipNet)
  108. }
  109. }
  110. }
  111. var subnets []*net.IPNet
  112. if *cf.subnets != "" {
  113. for _, rs := range strings.Split(*cf.subnets, ",") {
  114. rs := strings.Trim(rs, " ")
  115. if rs != "" {
  116. _, s, err := net.ParseCIDR(rs)
  117. if err != nil {
  118. return newHelpErrorf("invalid subnet definition: %s", err)
  119. }
  120. if s.IP.To4() == nil {
  121. return newHelpErrorf("invalid subnet definition: can only be ipv4, have %s", rs)
  122. }
  123. subnets = append(subnets, s)
  124. }
  125. }
  126. }
  127. var passphrase []byte
  128. if *cf.encryption {
  129. for i := 0; i < 5; i++ {
  130. out.Write([]byte("Enter passphrase: "))
  131. passphrase, err = pr.ReadPassword()
  132. if err == ErrNoTerminal {
  133. return fmt.Errorf("out-key must be encrypted interactively")
  134. } else if err != nil {
  135. return fmt.Errorf("error reading passphrase: %s", err)
  136. }
  137. if len(passphrase) > 0 {
  138. break
  139. }
  140. }
  141. if len(passphrase) == 0 {
  142. return fmt.Errorf("no passphrase specified, remove -encrypt flag to write out-key in plaintext")
  143. }
  144. }
  145. pub, rawPriv, err := ed25519.GenerateKey(rand.Reader)
  146. if err != nil {
  147. return fmt.Errorf("error while generating ed25519 keys: %s", err)
  148. }
  149. nc := cert.NebulaCertificate{
  150. Details: cert.NebulaCertificateDetails{
  151. Name: *cf.name,
  152. Groups: groups,
  153. Ips: ips,
  154. Subnets: subnets,
  155. NotBefore: time.Now(),
  156. NotAfter: time.Now().Add(*cf.duration),
  157. PublicKey: pub,
  158. IsCA: true,
  159. },
  160. }
  161. if _, err := os.Stat(*cf.outKeyPath); err == nil {
  162. return fmt.Errorf("refusing to overwrite existing CA key: %s", *cf.outKeyPath)
  163. }
  164. if _, err := os.Stat(*cf.outCertPath); err == nil {
  165. return fmt.Errorf("refusing to overwrite existing CA cert: %s", *cf.outCertPath)
  166. }
  167. err = nc.Sign(rawPriv)
  168. if err != nil {
  169. return fmt.Errorf("error while signing: %s", err)
  170. }
  171. if *cf.encryption {
  172. b, err := cert.EncryptAndMarshalEd25519PrivateKey(rawPriv, passphrase, kdfParams)
  173. if err != nil {
  174. return fmt.Errorf("error while encrypting out-key: %s", err)
  175. }
  176. err = ioutil.WriteFile(*cf.outKeyPath, b, 0600)
  177. } else {
  178. err = ioutil.WriteFile(*cf.outKeyPath, cert.MarshalEd25519PrivateKey(rawPriv), 0600)
  179. }
  180. if err != nil {
  181. return fmt.Errorf("error while writing out-key: %s", err)
  182. }
  183. b, err := nc.MarshalToPEM()
  184. if err != nil {
  185. return fmt.Errorf("error while marshalling certificate: %s", err)
  186. }
  187. err = ioutil.WriteFile(*cf.outCertPath, b, 0600)
  188. if err != nil {
  189. return fmt.Errorf("error while writing out-crt: %s", err)
  190. }
  191. if *cf.outQRPath != "" {
  192. b, err = qrcode.Encode(string(b), qrcode.Medium, -5)
  193. if err != nil {
  194. return fmt.Errorf("error while generating qr code: %s", err)
  195. }
  196. err = ioutil.WriteFile(*cf.outQRPath, b, 0600)
  197. if err != nil {
  198. return fmt.Errorf("error while writing out-qr: %s", err)
  199. }
  200. }
  201. return nil
  202. }
  203. func caSummary() string {
  204. return "ca <flags>: create a self signed certificate authority"
  205. }
  206. func caHelp(out io.Writer) {
  207. cf := newCaFlags()
  208. out.Write([]byte("Usage of " + os.Args[0] + " " + caSummary() + "\n"))
  209. cf.set.SetOutput(out)
  210. cf.set.PrintDefaults()
  211. }