helpers.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package e2e
  2. import (
  3. "crypto/rand"
  4. "io"
  5. "net"
  6. "time"
  7. "github.com/slackhq/nebula/cert"
  8. "golang.org/x/crypto/curve25519"
  9. "golang.org/x/crypto/ed25519"
  10. )
  11. // NewTestCaCert will generate a CA cert
  12. func NewTestCaCert(before, after time.Time, ips, subnets []*net.IPNet, groups []string) (*cert.NebulaCertificate, []byte, []byte, []byte) {
  13. pub, priv, err := ed25519.GenerateKey(rand.Reader)
  14. if before.IsZero() {
  15. before = time.Now().Add(time.Second * -60).Round(time.Second)
  16. }
  17. if after.IsZero() {
  18. after = time.Now().Add(time.Second * 60).Round(time.Second)
  19. }
  20. nc := &cert.NebulaCertificate{
  21. Details: cert.NebulaCertificateDetails{
  22. Name: "test ca",
  23. NotBefore: time.Unix(before.Unix(), 0),
  24. NotAfter: time.Unix(after.Unix(), 0),
  25. PublicKey: pub,
  26. IsCA: true,
  27. InvertedGroups: make(map[string]struct{}),
  28. },
  29. }
  30. if len(ips) > 0 {
  31. nc.Details.Ips = ips
  32. }
  33. if len(subnets) > 0 {
  34. nc.Details.Subnets = subnets
  35. }
  36. if len(groups) > 0 {
  37. nc.Details.Groups = groups
  38. }
  39. err = nc.Sign(cert.Curve_CURVE25519, priv)
  40. if err != nil {
  41. panic(err)
  42. }
  43. pem, err := nc.MarshalToPEM()
  44. if err != nil {
  45. panic(err)
  46. }
  47. return nc, pub, priv, pem
  48. }
  49. // NewTestCert will generate a signed certificate with the provided details.
  50. // Expiry times are defaulted if you do not pass them in
  51. func NewTestCert(ca *cert.NebulaCertificate, key []byte, name string, before, after time.Time, ip *net.IPNet, subnets []*net.IPNet, groups []string) (*cert.NebulaCertificate, []byte, []byte, []byte) {
  52. issuer, err := ca.Sha256Sum()
  53. if err != nil {
  54. panic(err)
  55. }
  56. if before.IsZero() {
  57. before = time.Now().Add(time.Second * -60).Round(time.Second)
  58. }
  59. if after.IsZero() {
  60. after = time.Now().Add(time.Second * 60).Round(time.Second)
  61. }
  62. pub, rawPriv := x25519Keypair()
  63. nc := &cert.NebulaCertificate{
  64. Details: cert.NebulaCertificateDetails{
  65. Name: name,
  66. Ips: []*net.IPNet{ip},
  67. Subnets: subnets,
  68. Groups: groups,
  69. NotBefore: time.Unix(before.Unix(), 0),
  70. NotAfter: time.Unix(after.Unix(), 0),
  71. PublicKey: pub,
  72. IsCA: false,
  73. Issuer: issuer,
  74. InvertedGroups: make(map[string]struct{}),
  75. },
  76. }
  77. err = nc.Sign(ca.Details.Curve, key)
  78. if err != nil {
  79. panic(err)
  80. }
  81. pem, err := nc.MarshalToPEM()
  82. if err != nil {
  83. panic(err)
  84. }
  85. return nc, pub, cert.MarshalX25519PrivateKey(rawPriv), pem
  86. }
  87. func x25519Keypair() ([]byte, []byte) {
  88. privkey := make([]byte, 32)
  89. if _, err := io.ReadFull(rand.Reader, privkey); err != nil {
  90. panic(err)
  91. }
  92. pubkey, err := curve25519.X25519(privkey, curve25519.Basepoint)
  93. if err != nil {
  94. panic(err)
  95. }
  96. return pubkey, privkey
  97. }