cert.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. )
  11. var trustedCAs *cert.NebulaCAPool
  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) (*CertState, error) {
  43. var pemPrivateKey []byte
  44. var err error
  45. privPathOrPEM := c.GetString("pki.key", "")
  46. if privPathOrPEM == "" {
  47. // Support backwards compat with the old x509
  48. //TODO: remove after this is rolled out everywhere - NB 2018/02/23
  49. privPathOrPEM = c.GetString("x509.key", "")
  50. }
  51. if privPathOrPEM == "" {
  52. return nil, errors.New("no pki.key path or PEM data provided")
  53. }
  54. if strings.Contains(privPathOrPEM, "-----BEGIN") {
  55. pemPrivateKey = []byte(privPathOrPEM)
  56. privPathOrPEM = "<inline>"
  57. } else {
  58. pemPrivateKey, err = ioutil.ReadFile(privPathOrPEM)
  59. if err != nil {
  60. return nil, fmt.Errorf("unable to read pki.key file %s: %s", privPathOrPEM, err)
  61. }
  62. }
  63. rawKey, _, err := cert.UnmarshalX25519PrivateKey(pemPrivateKey)
  64. if err != nil {
  65. return nil, fmt.Errorf("error while unmarshaling pki.key %s: %s", privPathOrPEM, err)
  66. }
  67. var rawCert []byte
  68. pubPathOrPEM := c.GetString("pki.cert", "")
  69. if pubPathOrPEM == "" {
  70. // Support backwards compat with the old x509
  71. //TODO: remove after this is rolled out everywhere - NB 2018/02/23
  72. pubPathOrPEM = c.GetString("x509.cert", "")
  73. }
  74. if pubPathOrPEM == "" {
  75. return nil, errors.New("no pki.cert path or PEM data provided")
  76. }
  77. if strings.Contains(pubPathOrPEM, "-----BEGIN") {
  78. rawCert = []byte(pubPathOrPEM)
  79. pubPathOrPEM = "<inline>"
  80. } else {
  81. rawCert, err = ioutil.ReadFile(pubPathOrPEM)
  82. if err != nil {
  83. return nil, fmt.Errorf("unable to read pki.cert file %s: %s", pubPathOrPEM, err)
  84. }
  85. }
  86. nebulaCert, _, err := cert.UnmarshalNebulaCertificateFromPEM(rawCert)
  87. if err != nil {
  88. return nil, fmt.Errorf("error while unmarshaling pki.cert %s: %s", pubPathOrPEM, err)
  89. }
  90. if nebulaCert.Expired(time.Now()) {
  91. return nil, fmt.Errorf("nebula certificate for this host is expired")
  92. }
  93. if len(nebulaCert.Details.Ips) == 0 {
  94. return nil, fmt.Errorf("no IPs encoded in certificate")
  95. }
  96. if err = nebulaCert.VerifyPrivateKey(rawKey); err != nil {
  97. return nil, fmt.Errorf("private key is not a pair with public key in nebula cert")
  98. }
  99. return NewCertState(nebulaCert, rawKey)
  100. }
  101. func loadCAFromConfig(l *logrus.Logger, c *Config) (*cert.NebulaCAPool, error) {
  102. var rawCA []byte
  103. var err error
  104. caPathOrPEM := c.GetString("pki.ca", "")
  105. if caPathOrPEM == "" {
  106. // Support backwards compat with the old x509
  107. //TODO: remove after this is rolled out everywhere - NB 2018/02/23
  108. caPathOrPEM = c.GetString("x509.ca", "")
  109. }
  110. if caPathOrPEM == "" {
  111. return nil, errors.New("no pki.ca path or PEM data provided")
  112. }
  113. if strings.Contains(caPathOrPEM, "-----BEGIN") {
  114. rawCA = []byte(caPathOrPEM)
  115. caPathOrPEM = "<inline>"
  116. } else {
  117. rawCA, err = ioutil.ReadFile(caPathOrPEM)
  118. if err != nil {
  119. return nil, fmt.Errorf("unable to read pki.ca file %s: %s", caPathOrPEM, err)
  120. }
  121. }
  122. CAs, err := cert.NewCAPoolFromBytes(rawCA)
  123. if err != nil {
  124. return nil, fmt.Errorf("error while adding CA certificate to CA trust store: %s", err)
  125. }
  126. for _, fp := range c.GetStringSlice("pki.blocklist", []string{}) {
  127. l.WithField("fingerprint", fp).Infof("Blocklisting cert")
  128. CAs.BlocklistFingerprint(fp)
  129. }
  130. // Support deprecated config for at leaast one minor release to allow for migrations
  131. for _, fp := range c.GetStringSlice("pki.blacklist", []string{}) {
  132. l.WithField("fingerprint", fp).Infof("Blocklisting cert")
  133. l.Warn("pki.blacklist is deprecated and will not be supported in a future release. Please migrate your config to use pki.blocklist")
  134. CAs.BlocklistFingerprint(fp)
  135. }
  136. return CAs, nil
  137. }