keygen.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/slackhq/nebula/pkclient"
  8. "github.com/slackhq/nebula/cert"
  9. )
  10. type keygenFlags struct {
  11. set *flag.FlagSet
  12. outKeyPath *string
  13. outPubPath *string
  14. curve *string
  15. p11url *string
  16. }
  17. func newKeygenFlags() *keygenFlags {
  18. cf := keygenFlags{set: flag.NewFlagSet("keygen", flag.ContinueOnError)}
  19. cf.set.Usage = func() {}
  20. cf.outPubPath = cf.set.String("out-pub", "", "Required: path to write the public key to")
  21. cf.outKeyPath = cf.set.String("out-key", "", "Required: path to write the private key to")
  22. cf.curve = cf.set.String("curve", "25519", "ECDH Curve (25519, P256)")
  23. cf.p11url = p11Flag(cf.set)
  24. return &cf
  25. }
  26. func keygen(args []string, out io.Writer, errOut io.Writer) error {
  27. cf := newKeygenFlags()
  28. err := cf.set.Parse(args)
  29. if err != nil {
  30. return err
  31. }
  32. isP11 := len(*cf.p11url) > 0
  33. if !isP11 {
  34. if err = mustFlagString("out-key", cf.outKeyPath); err != nil {
  35. return err
  36. }
  37. }
  38. if err = mustFlagString("out-pub", cf.outPubPath); err != nil {
  39. return err
  40. }
  41. var pub, rawPriv []byte
  42. var curve cert.Curve
  43. if isP11 {
  44. switch *cf.curve {
  45. case "P256":
  46. curve = cert.Curve_P256
  47. default:
  48. return fmt.Errorf("invalid curve for PKCS#11: %s", *cf.curve)
  49. }
  50. } else {
  51. switch *cf.curve {
  52. case "25519", "X25519", "Curve25519", "CURVE25519":
  53. pub, rawPriv = x25519Keypair()
  54. curve = cert.Curve_CURVE25519
  55. case "P256":
  56. pub, rawPriv = p256Keypair()
  57. curve = cert.Curve_P256
  58. default:
  59. return fmt.Errorf("invalid curve: %s", *cf.curve)
  60. }
  61. }
  62. if isP11 {
  63. p11Client, err := pkclient.FromUrl(*cf.p11url)
  64. if err != nil {
  65. return fmt.Errorf("error while creating PKCS#11 client: %w", err)
  66. }
  67. defer func(client *pkclient.PKClient) {
  68. _ = client.Close()
  69. }(p11Client)
  70. pub, err = p11Client.GetPubKey()
  71. if err != nil {
  72. return fmt.Errorf("error while getting public key: %w", err)
  73. }
  74. } else {
  75. err = os.WriteFile(*cf.outKeyPath, cert.MarshalPrivateKeyToPEM(curve, rawPriv), 0600)
  76. if err != nil {
  77. return fmt.Errorf("error while writing out-key: %s", err)
  78. }
  79. }
  80. err = os.WriteFile(*cf.outPubPath, cert.MarshalPublicKeyToPEM(curve, pub), 0600)
  81. if err != nil {
  82. return fmt.Errorf("error while writing out-pub: %s", err)
  83. }
  84. return nil
  85. }
  86. func keygenSummary() string {
  87. return "keygen <flags>: create a public/private key pair. the public key can be passed to `nebula-cert sign`"
  88. }
  89. func keygenHelp(out io.Writer) {
  90. cf := newKeygenFlags()
  91. _, _ = out.Write([]byte("Usage of " + os.Args[0] + " " + keygenSummary() + "\n"))
  92. cf.set.SetOutput(out)
  93. cf.set.PrintDefaults()
  94. }