cert.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package nebula
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "strings"
  7. "time"
  8. "github.com/sirupsen/logrus"
  9. "github.com/slackhq/nebula/cert"
  10. "github.com/slackhq/nebula/config"
  11. )
  12. type CertState struct {
  13. certificate *cert.NebulaCertificate
  14. rawCertificate []byte
  15. rawCertificateNoKey []byte
  16. publicKey []byte
  17. privateKey []byte
  18. }
  19. func NewCertState(certificate *cert.NebulaCertificate, privateKey []byte) (*CertState, error) {
  20. // Marshal the certificate to ensure it is valid
  21. rawCertificate, err := certificate.Marshal()
  22. if err != nil {
  23. return nil, fmt.Errorf("invalid nebula certificate on interface: %s", err)
  24. }
  25. publicKey := certificate.Details.PublicKey
  26. cs := &CertState{
  27. rawCertificate: rawCertificate,
  28. certificate: certificate, // PublicKey has been set to nil above
  29. privateKey: privateKey,
  30. publicKey: publicKey,
  31. }
  32. cs.certificate.Details.PublicKey = nil
  33. rawCertNoKey, err := cs.certificate.Marshal()
  34. if err != nil {
  35. return nil, fmt.Errorf("error marshalling certificate no key: %s", err)
  36. }
  37. cs.rawCertificateNoKey = rawCertNoKey
  38. // put public key back
  39. cs.certificate.Details.PublicKey = cs.publicKey
  40. return cs, nil
  41. }
  42. func NewCertStateFromConfig(c *config.C) (*CertState, error) {
  43. var pemPrivateKey []byte
  44. var err error
  45. privPathOrPEM := c.GetString("pki.key", "")
  46. if privPathOrPEM == "" {
  47. return nil, errors.New("no pki.key path or PEM data provided")
  48. }
  49. if strings.Contains(privPathOrPEM, "-----BEGIN") {
  50. pemPrivateKey = []byte(privPathOrPEM)
  51. privPathOrPEM = "<inline>"
  52. } else {
  53. pemPrivateKey, err = ioutil.ReadFile(privPathOrPEM)
  54. if err != nil {
  55. return nil, fmt.Errorf("unable to read pki.key file %s: %s", privPathOrPEM, err)
  56. }
  57. }
  58. rawKey, _, err := cert.UnmarshalX25519PrivateKey(pemPrivateKey)
  59. if err != nil {
  60. return nil, fmt.Errorf("error while unmarshaling pki.key %s: %s", privPathOrPEM, err)
  61. }
  62. var rawCert []byte
  63. pubPathOrPEM := c.GetString("pki.cert", "")
  64. if pubPathOrPEM == "" {
  65. return nil, errors.New("no pki.cert path or PEM data provided")
  66. }
  67. if strings.Contains(pubPathOrPEM, "-----BEGIN") {
  68. rawCert = []byte(pubPathOrPEM)
  69. pubPathOrPEM = "<inline>"
  70. } else {
  71. rawCert, err = ioutil.ReadFile(pubPathOrPEM)
  72. if err != nil {
  73. return nil, fmt.Errorf("unable to read pki.cert file %s: %s", pubPathOrPEM, err)
  74. }
  75. }
  76. nebulaCert, _, err := cert.UnmarshalNebulaCertificateFromPEM(rawCert)
  77. if err != nil {
  78. return nil, fmt.Errorf("error while unmarshaling pki.cert %s: %s", pubPathOrPEM, err)
  79. }
  80. if nebulaCert.Expired(time.Now()) {
  81. return nil, fmt.Errorf("nebula certificate for this host is expired")
  82. }
  83. if len(nebulaCert.Details.Ips) == 0 {
  84. return nil, fmt.Errorf("no IPs encoded in certificate")
  85. }
  86. if err = nebulaCert.VerifyPrivateKey(rawKey); err != nil {
  87. return nil, fmt.Errorf("private key is not a pair with public key in nebula cert")
  88. }
  89. return NewCertState(nebulaCert, rawKey)
  90. }
  91. func loadCAFromConfig(l *logrus.Logger, c *config.C) (*cert.NebulaCAPool, error) {
  92. var rawCA []byte
  93. var err error
  94. caPathOrPEM := c.GetString("pki.ca", "")
  95. if caPathOrPEM == "" {
  96. return nil, errors.New("no pki.ca path or PEM data provided")
  97. }
  98. if strings.Contains(caPathOrPEM, "-----BEGIN") {
  99. rawCA = []byte(caPathOrPEM)
  100. } else {
  101. rawCA, err = ioutil.ReadFile(caPathOrPEM)
  102. if err != nil {
  103. return nil, fmt.Errorf("unable to read pki.ca file %s: %s", caPathOrPEM, err)
  104. }
  105. }
  106. CAs, err := cert.NewCAPoolFromBytes(rawCA)
  107. if errors.Is(err, cert.ErrExpired) {
  108. var expired int
  109. for _, cert := range CAs.CAs {
  110. if cert.Expired(time.Now()) {
  111. expired++
  112. l.WithField("cert", cert).Warn("expired certificate present in CA pool")
  113. }
  114. }
  115. if expired >= len(CAs.CAs) {
  116. return nil, errors.New("no valid CA certificates present")
  117. }
  118. } else if err != nil {
  119. return nil, fmt.Errorf("error while adding CA certificate to CA trust store: %s", err)
  120. }
  121. for _, fp := range c.GetStringSlice("pki.blocklist", []string{}) {
  122. l.WithField("fingerprint", fp).Info("Blocklisting cert")
  123. CAs.BlocklistFingerprint(fp)
  124. }
  125. // Support deprecated config for at least one minor release to allow for migrations
  126. //TODO: remove in 2022 or later
  127. for _, fp := range c.GetStringSlice("pki.blacklist", []string{}) {
  128. l.WithField("fingerprint", fp).Info("Blocklisting cert")
  129. l.Warn("pki.blacklist is deprecated and will not be supported in a future release. Please migrate your config to use pki.blocklist")
  130. CAs.BlocklistFingerprint(fp)
  131. }
  132. return CAs, nil
  133. }