2
0

pki.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package nebula
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "sync/atomic"
  8. "time"
  9. "github.com/sirupsen/logrus"
  10. "github.com/slackhq/nebula/cert"
  11. "github.com/slackhq/nebula/config"
  12. "github.com/slackhq/nebula/util"
  13. )
  14. type PKI struct {
  15. cs atomic.Pointer[CertState]
  16. caPool atomic.Pointer[cert.NebulaCAPool]
  17. l *logrus.Logger
  18. }
  19. type CertState struct {
  20. Certificate *cert.NebulaCertificate
  21. RawCertificate []byte
  22. RawCertificateNoKey []byte
  23. PublicKey []byte
  24. PrivateKey []byte
  25. }
  26. func NewPKIFromConfig(l *logrus.Logger, c *config.C) (*PKI, error) {
  27. pki := &PKI{l: l}
  28. err := pki.reload(c, true)
  29. if err != nil {
  30. return nil, err
  31. }
  32. c.RegisterReloadCallback(func(c *config.C) {
  33. rErr := pki.reload(c, false)
  34. if rErr != nil {
  35. util.LogWithContextIfNeeded("Failed to reload PKI from config", rErr, l)
  36. }
  37. })
  38. return pki, nil
  39. }
  40. func (p *PKI) GetCertState() *CertState {
  41. return p.cs.Load()
  42. }
  43. func (p *PKI) GetCAPool() *cert.NebulaCAPool {
  44. return p.caPool.Load()
  45. }
  46. func (p *PKI) reload(c *config.C, initial bool) error {
  47. err := p.reloadCert(c, initial)
  48. if err != nil {
  49. if initial {
  50. return err
  51. }
  52. err.Log(p.l)
  53. }
  54. err = p.reloadCAPool(c)
  55. if err != nil {
  56. if initial {
  57. return err
  58. }
  59. err.Log(p.l)
  60. }
  61. return nil
  62. }
  63. func (p *PKI) reloadCert(c *config.C, initial bool) *util.ContextualError {
  64. cs, err := newCertStateFromConfig(c)
  65. if err != nil {
  66. return util.NewContextualError("Could not load client cert", nil, err)
  67. }
  68. if !initial {
  69. // did IP in cert change? if so, don't set
  70. currentCert := p.cs.Load().Certificate
  71. oldIPs := currentCert.Details.Ips
  72. newIPs := cs.Certificate.Details.Ips
  73. if len(oldIPs) > 0 && len(newIPs) > 0 && oldIPs[0].String() != newIPs[0].String() {
  74. return util.NewContextualError(
  75. "IP in new cert was different from old",
  76. m{"new_ip": newIPs[0], "old_ip": oldIPs[0]},
  77. nil,
  78. )
  79. }
  80. }
  81. p.cs.Store(cs)
  82. if initial {
  83. p.l.WithField("cert", cs.Certificate).Debug("Client nebula certificate")
  84. } else {
  85. p.l.WithField("cert", cs.Certificate).Info("Client cert refreshed from disk")
  86. }
  87. return nil
  88. }
  89. func (p *PKI) reloadCAPool(c *config.C) *util.ContextualError {
  90. caPool, err := loadCAPoolFromConfig(p.l, c)
  91. if err != nil {
  92. return util.NewContextualError("Failed to load ca from config", nil, err)
  93. }
  94. p.caPool.Store(caPool)
  95. p.l.WithField("fingerprints", caPool.GetFingerprints()).Debug("Trusted CA fingerprints")
  96. return nil
  97. }
  98. func newCertState(certificate *cert.NebulaCertificate, privateKey []byte) (*CertState, error) {
  99. // Marshal the certificate to ensure it is valid
  100. rawCertificate, err := certificate.Marshal()
  101. if err != nil {
  102. return nil, fmt.Errorf("invalid nebula certificate on interface: %s", err)
  103. }
  104. publicKey := certificate.Details.PublicKey
  105. cs := &CertState{
  106. RawCertificate: rawCertificate,
  107. Certificate: certificate,
  108. PrivateKey: privateKey,
  109. PublicKey: publicKey,
  110. }
  111. cs.Certificate.Details.PublicKey = nil
  112. rawCertNoKey, err := cs.Certificate.Marshal()
  113. if err != nil {
  114. return nil, fmt.Errorf("error marshalling certificate no key: %s", err)
  115. }
  116. cs.RawCertificateNoKey = rawCertNoKey
  117. // put public key back
  118. cs.Certificate.Details.PublicKey = cs.PublicKey
  119. return cs, nil
  120. }
  121. func newCertStateFromConfig(c *config.C) (*CertState, error) {
  122. var pemPrivateKey []byte
  123. var err error
  124. privPathOrPEM := c.GetString("pki.key", "")
  125. if privPathOrPEM == "" {
  126. return nil, errors.New("no pki.key path or PEM data provided")
  127. }
  128. if strings.Contains(privPathOrPEM, "-----BEGIN") {
  129. pemPrivateKey = []byte(privPathOrPEM)
  130. privPathOrPEM = "<inline>"
  131. } else {
  132. pemPrivateKey, err = os.ReadFile(privPathOrPEM)
  133. if err != nil {
  134. return nil, fmt.Errorf("unable to read pki.key file %s: %s", privPathOrPEM, err)
  135. }
  136. }
  137. rawKey, _, curve, err := cert.UnmarshalPrivateKey(pemPrivateKey)
  138. if err != nil {
  139. return nil, fmt.Errorf("error while unmarshaling pki.key %s: %s", privPathOrPEM, err)
  140. }
  141. var rawCert []byte
  142. pubPathOrPEM := c.GetString("pki.cert", "")
  143. if pubPathOrPEM == "" {
  144. return nil, errors.New("no pki.cert path or PEM data provided")
  145. }
  146. if strings.Contains(pubPathOrPEM, "-----BEGIN") {
  147. rawCert = []byte(pubPathOrPEM)
  148. pubPathOrPEM = "<inline>"
  149. } else {
  150. rawCert, err = os.ReadFile(pubPathOrPEM)
  151. if err != nil {
  152. return nil, fmt.Errorf("unable to read pki.cert file %s: %s", pubPathOrPEM, err)
  153. }
  154. }
  155. nebulaCert, _, err := cert.UnmarshalNebulaCertificateFromPEM(rawCert)
  156. if err != nil {
  157. return nil, fmt.Errorf("error while unmarshaling pki.cert %s: %s", pubPathOrPEM, err)
  158. }
  159. if nebulaCert.Expired(time.Now()) {
  160. return nil, fmt.Errorf("nebula certificate for this host is expired")
  161. }
  162. if len(nebulaCert.Details.Ips) == 0 {
  163. return nil, fmt.Errorf("no IPs encoded in certificate")
  164. }
  165. if err = nebulaCert.VerifyPrivateKey(curve, rawKey); err != nil {
  166. return nil, fmt.Errorf("private key is not a pair with public key in nebula cert")
  167. }
  168. return newCertState(nebulaCert, rawKey)
  169. }
  170. func loadCAPoolFromConfig(l *logrus.Logger, c *config.C) (*cert.NebulaCAPool, error) {
  171. var rawCA []byte
  172. var err error
  173. caPathOrPEM := c.GetString("pki.ca", "")
  174. if caPathOrPEM == "" {
  175. return nil, errors.New("no pki.ca path or PEM data provided")
  176. }
  177. if strings.Contains(caPathOrPEM, "-----BEGIN") {
  178. rawCA = []byte(caPathOrPEM)
  179. } else {
  180. rawCA, err = os.ReadFile(caPathOrPEM)
  181. if err != nil {
  182. return nil, fmt.Errorf("unable to read pki.ca file %s: %s", caPathOrPEM, err)
  183. }
  184. }
  185. caPool, err := cert.NewCAPoolFromBytes(rawCA)
  186. if errors.Is(err, cert.ErrExpired) {
  187. var expired int
  188. for _, crt := range caPool.CAs {
  189. if crt.Expired(time.Now()) {
  190. expired++
  191. l.WithField("cert", crt).Warn("expired certificate present in CA pool")
  192. }
  193. }
  194. if expired >= len(caPool.CAs) {
  195. return nil, errors.New("no valid CA certificates present")
  196. }
  197. } else if err != nil {
  198. return nil, fmt.Errorf("error while adding CA certificate to CA trust store: %s", err)
  199. }
  200. for _, fp := range c.GetStringSlice("pki.blocklist", []string{}) {
  201. l.WithField("fingerprint", fp).Info("Blocklisting cert")
  202. caPool.BlocklistFingerprint(fp)
  203. }
  204. return caPool, nil
  205. }