pki.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package nebula
  2. import (
  3. "encoding/binary"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net"
  9. "net/netip"
  10. "os"
  11. "slices"
  12. "strings"
  13. "sync/atomic"
  14. "time"
  15. "github.com/gaissmai/bart"
  16. "github.com/sirupsen/logrus"
  17. "github.com/slackhq/nebula/cert"
  18. "github.com/slackhq/nebula/config"
  19. "github.com/slackhq/nebula/util"
  20. )
  21. type PKI struct {
  22. cs atomic.Pointer[CertState]
  23. caPool atomic.Pointer[cert.CAPool]
  24. l *logrus.Logger
  25. }
  26. type CertState struct {
  27. v1Cert cert.Certificate
  28. v1HandshakeBytes []byte
  29. v2Cert cert.Certificate
  30. v2HandshakeBytes []byte
  31. initiatingVersion cert.Version
  32. privateKey []byte
  33. pkcs11Backed bool
  34. cipher string
  35. myVpnNetworks []netip.Prefix
  36. myVpnNetworksTable *bart.Lite
  37. myVpnAddrs []netip.Addr
  38. myVpnAddrsTable *bart.Lite
  39. myVpnBroadcastAddrsTable *bart.Lite
  40. }
  41. func NewPKIFromConfig(l *logrus.Logger, c *config.C) (*PKI, error) {
  42. pki := &PKI{l: l}
  43. err := pki.reload(c, true)
  44. if err != nil {
  45. return nil, err
  46. }
  47. c.RegisterReloadCallback(func(c *config.C) {
  48. rErr := pki.reload(c, false)
  49. if rErr != nil {
  50. util.LogWithContextIfNeeded("Failed to reload PKI from config", rErr, l)
  51. }
  52. })
  53. return pki, nil
  54. }
  55. func (p *PKI) GetCAPool() *cert.CAPool {
  56. return p.caPool.Load()
  57. }
  58. func (p *PKI) getCertState() *CertState {
  59. return p.cs.Load()
  60. }
  61. func (p *PKI) reload(c *config.C, initial bool) error {
  62. err := p.reloadCerts(c, initial)
  63. if err != nil {
  64. if initial {
  65. return err
  66. }
  67. err.Log(p.l)
  68. }
  69. err = p.reloadCAPool(c)
  70. if err != nil {
  71. if initial {
  72. return err
  73. }
  74. err.Log(p.l)
  75. }
  76. return nil
  77. }
  78. func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
  79. newState, err := newCertStateFromConfig(c)
  80. if err != nil {
  81. return util.NewContextualError("Could not load client cert", nil, err)
  82. }
  83. if !initial {
  84. currentState := p.cs.Load()
  85. if newState.v1Cert != nil {
  86. if currentState.v1Cert == nil {
  87. //adding certs is fine, actually. Networks-in-common confirmed in newCertState().
  88. } else {
  89. // did IP in cert change? if so, don't set
  90. if !slices.Equal(currentState.v1Cert.Networks(), newState.v1Cert.Networks()) {
  91. return util.NewContextualError(
  92. "Networks in new cert was different from old",
  93. m{"new_networks": newState.v1Cert.Networks(), "old_networks": currentState.v1Cert.Networks(), "cert_version": cert.Version1},
  94. nil,
  95. )
  96. }
  97. if currentState.v1Cert.Curve() != newState.v1Cert.Curve() {
  98. return util.NewContextualError(
  99. "Curve in new v1 cert was different from old",
  100. m{"new_curve": newState.v1Cert.Curve(), "old_curve": currentState.v1Cert.Curve(), "cert_version": cert.Version1},
  101. nil,
  102. )
  103. }
  104. }
  105. }
  106. if newState.v2Cert != nil {
  107. if currentState.v2Cert == nil {
  108. //adding certs is fine, actually
  109. } else {
  110. // did IP in cert change? if so, don't set
  111. if !slices.Equal(currentState.v2Cert.Networks(), newState.v2Cert.Networks()) {
  112. return util.NewContextualError(
  113. "Networks in new cert was different from old",
  114. m{"new_networks": newState.v2Cert.Networks(), "old_networks": currentState.v2Cert.Networks(), "cert_version": cert.Version2},
  115. nil,
  116. )
  117. }
  118. if currentState.v2Cert.Curve() != newState.v2Cert.Curve() {
  119. return util.NewContextualError(
  120. "Curve in new cert was different from old",
  121. m{"new_curve": newState.v2Cert.Curve(), "old_curve": currentState.v2Cert.Curve(), "cert_version": cert.Version2},
  122. nil,
  123. )
  124. }
  125. }
  126. } else if currentState.v2Cert != nil {
  127. //newState.v1Cert is non-nil bc empty certstates aren't permitted
  128. if newState.v1Cert == nil {
  129. return util.NewContextualError("v1 and v2 certs are nil, this should be impossible", nil, err)
  130. }
  131. //if we're going to v1-only, we need to make sure we didn't orphan any v2-cert vpnaddrs
  132. if !slices.Equal(currentState.v2Cert.Networks(), newState.v1Cert.Networks()) {
  133. return util.NewContextualError(
  134. "Removing a V2 cert is not permitted unless it has identical networks to the new V1 cert",
  135. m{"new_v1_networks": newState.v1Cert.Networks(), "old_v2_networks": currentState.v2Cert.Networks()},
  136. nil,
  137. )
  138. }
  139. }
  140. // Cipher cant be hot swapped so just leave it at what it was before
  141. newState.cipher = currentState.cipher
  142. } else {
  143. newState.cipher = c.GetString("cipher", "aes")
  144. //TODO: this sucks and we should make it not a global
  145. switch newState.cipher {
  146. case "aes":
  147. noiseEndianness = binary.BigEndian
  148. case "chachapoly":
  149. noiseEndianness = binary.LittleEndian
  150. default:
  151. return util.NewContextualError(
  152. "unknown cipher",
  153. m{"cipher": newState.cipher},
  154. nil,
  155. )
  156. }
  157. }
  158. p.cs.Store(newState)
  159. if initial {
  160. p.l.WithField("cert", newState).Debug("Client nebula certificate(s)")
  161. } else {
  162. p.l.WithField("cert", newState).Info("Client certificate(s) refreshed from disk")
  163. }
  164. return nil
  165. }
  166. func (p *PKI) reloadCAPool(c *config.C) *util.ContextualError {
  167. caPool, err := loadCAPoolFromConfig(p.l, c)
  168. if err != nil {
  169. return util.NewContextualError("Failed to load ca from config", nil, err)
  170. }
  171. p.caPool.Store(caPool)
  172. p.l.WithField("fingerprints", caPool.GetFingerprints()).Debug("Trusted CA fingerprints")
  173. return nil
  174. }
  175. func (cs *CertState) GetDefaultCertificate() cert.Certificate {
  176. c := cs.getCertificate(cs.initiatingVersion)
  177. if c == nil {
  178. panic("No default certificate found")
  179. }
  180. return c
  181. }
  182. func (cs *CertState) getCertificate(v cert.Version) cert.Certificate {
  183. switch v {
  184. case cert.Version1:
  185. return cs.v1Cert
  186. case cert.Version2:
  187. return cs.v2Cert
  188. }
  189. return nil
  190. }
  191. // getHandshakeBytes returns the cached bytes to be used in a handshake message for the requested version.
  192. // Callers must check if the return []byte is nil.
  193. func (cs *CertState) getHandshakeBytes(v cert.Version) []byte {
  194. switch v {
  195. case cert.Version1:
  196. return cs.v1HandshakeBytes
  197. case cert.Version2:
  198. return cs.v2HandshakeBytes
  199. default:
  200. return nil
  201. }
  202. }
  203. func (cs *CertState) String() string {
  204. b, err := cs.MarshalJSON()
  205. if err != nil {
  206. return fmt.Sprintf("error marshaling certificate state: %v", err)
  207. }
  208. return string(b)
  209. }
  210. func (cs *CertState) MarshalJSON() ([]byte, error) {
  211. msg := []json.RawMessage{}
  212. if cs.v1Cert != nil {
  213. b, err := cs.v1Cert.MarshalJSON()
  214. if err != nil {
  215. return nil, err
  216. }
  217. msg = append(msg, b)
  218. }
  219. if cs.v2Cert != nil {
  220. b, err := cs.v2Cert.MarshalJSON()
  221. if err != nil {
  222. return nil, err
  223. }
  224. msg = append(msg, b)
  225. }
  226. return json.Marshal(msg)
  227. }
  228. func newCertStateFromConfig(c *config.C) (*CertState, error) {
  229. var err error
  230. privPathOrPEM := c.GetString("pki.key", "")
  231. if privPathOrPEM == "" {
  232. return nil, errors.New("no pki.key path or PEM data provided")
  233. }
  234. rawKey, curve, isPkcs11, err := loadPrivateKey(privPathOrPEM)
  235. if err != nil {
  236. return nil, err
  237. }
  238. var rawCert []byte
  239. pubPathOrPEM := c.GetString("pki.cert", "")
  240. if pubPathOrPEM == "" {
  241. return nil, errors.New("no pki.cert path or PEM data provided")
  242. }
  243. if strings.Contains(pubPathOrPEM, "-----BEGIN") {
  244. rawCert = []byte(pubPathOrPEM)
  245. pubPathOrPEM = "<inline>"
  246. } else {
  247. rawCert, err = os.ReadFile(pubPathOrPEM)
  248. if err != nil {
  249. return nil, fmt.Errorf("unable to read pki.cert file %s: %s", pubPathOrPEM, err)
  250. }
  251. }
  252. var crt, v1, v2 cert.Certificate
  253. for {
  254. // Load the certificate
  255. crt, rawCert, err = loadCertificate(rawCert)
  256. if err != nil {
  257. return nil, err
  258. }
  259. switch crt.Version() {
  260. case cert.Version1:
  261. if v1 != nil {
  262. return nil, fmt.Errorf("v1 certificate already found in pki.cert")
  263. }
  264. v1 = crt
  265. case cert.Version2:
  266. if v2 != nil {
  267. return nil, fmt.Errorf("v2 certificate already found in pki.cert")
  268. }
  269. v2 = crt
  270. default:
  271. return nil, fmt.Errorf("unknown certificate version %v", crt.Version())
  272. }
  273. if len(rawCert) == 0 || strings.TrimSpace(string(rawCert)) == "" {
  274. break
  275. }
  276. }
  277. if v1 == nil && v2 == nil {
  278. return nil, errors.New("no certificates found in pki.cert")
  279. }
  280. useInitiatingVersion := uint32(1)
  281. if v1 == nil {
  282. // The only condition that requires v2 as the default is if only a v2 certificate is present
  283. // We do this to avoid having to configure it specifically in the config file
  284. useInitiatingVersion = 2
  285. }
  286. rawInitiatingVersion := c.GetUint32("pki.initiating_version", useInitiatingVersion)
  287. var initiatingVersion cert.Version
  288. switch rawInitiatingVersion {
  289. case 1:
  290. if v1 == nil {
  291. return nil, fmt.Errorf("can not use pki.initiating_version 1 without a v1 certificate in pki.cert")
  292. }
  293. initiatingVersion = cert.Version1
  294. case 2:
  295. initiatingVersion = cert.Version2
  296. default:
  297. return nil, fmt.Errorf("unknown pki.initiating_version: %v", rawInitiatingVersion)
  298. }
  299. return newCertState(initiatingVersion, v1, v2, isPkcs11, curve, rawKey)
  300. }
  301. func newCertState(dv cert.Version, v1, v2 cert.Certificate, pkcs11backed bool, privateKeyCurve cert.Curve, privateKey []byte) (*CertState, error) {
  302. cs := CertState{
  303. privateKey: privateKey,
  304. pkcs11Backed: pkcs11backed,
  305. myVpnNetworksTable: new(bart.Lite),
  306. myVpnAddrsTable: new(bart.Lite),
  307. myVpnBroadcastAddrsTable: new(bart.Lite),
  308. }
  309. if v1 != nil && v2 != nil {
  310. if !slices.Equal(v1.PublicKey(), v2.PublicKey()) {
  311. return nil, util.NewContextualError("v1 and v2 public keys are not the same, ignoring", nil, nil)
  312. }
  313. if v1.Curve() != v2.Curve() {
  314. return nil, util.NewContextualError("v1 and v2 curve are not the same, ignoring", nil, nil)
  315. }
  316. if v1.Networks()[0] != v2.Networks()[0] {
  317. return nil, util.NewContextualError("v1 and v2 networks are not the same", nil, nil)
  318. }
  319. cs.initiatingVersion = dv
  320. }
  321. if v1 != nil {
  322. if pkcs11backed {
  323. //NOTE: We do not currently have a method to verify a public private key pair when the private key is in an hsm
  324. } else {
  325. if err := v1.VerifyPrivateKey(privateKeyCurve, privateKey); err != nil {
  326. return nil, fmt.Errorf("private key is not a pair with public key in nebula cert")
  327. }
  328. }
  329. v1hs, err := v1.MarshalForHandshakes()
  330. if err != nil {
  331. return nil, fmt.Errorf("error marshalling certificate for handshake: %w", err)
  332. }
  333. cs.v1Cert = v1
  334. cs.v1HandshakeBytes = v1hs
  335. if cs.initiatingVersion == 0 {
  336. cs.initiatingVersion = cert.Version1
  337. }
  338. }
  339. if v2 != nil {
  340. if pkcs11backed {
  341. //NOTE: We do not currently have a method to verify a public private key pair when the private key is in an hsm
  342. } else {
  343. if err := v2.VerifyPrivateKey(privateKeyCurve, privateKey); err != nil {
  344. return nil, fmt.Errorf("private key is not a pair with public key in nebula cert")
  345. }
  346. }
  347. v2hs, err := v2.MarshalForHandshakes()
  348. if err != nil {
  349. return nil, fmt.Errorf("error marshalling certificate for handshake: %w", err)
  350. }
  351. cs.v2Cert = v2
  352. cs.v2HandshakeBytes = v2hs
  353. if cs.initiatingVersion == 0 {
  354. cs.initiatingVersion = cert.Version2
  355. }
  356. }
  357. var crt cert.Certificate
  358. crt = cs.getCertificate(cert.Version2)
  359. if crt == nil {
  360. // v2 certificates are a superset, only look at v1 if its all we have
  361. crt = cs.getCertificate(cert.Version1)
  362. }
  363. for _, network := range crt.Networks() {
  364. cs.myVpnNetworks = append(cs.myVpnNetworks, network)
  365. cs.myVpnNetworksTable.Insert(network)
  366. cs.myVpnAddrs = append(cs.myVpnAddrs, network.Addr())
  367. cs.myVpnAddrsTable.Insert(netip.PrefixFrom(network.Addr(), network.Addr().BitLen()))
  368. if network.Addr().Is4() {
  369. addr := network.Masked().Addr().As4()
  370. mask := net.CIDRMask(network.Bits(), network.Addr().BitLen())
  371. binary.BigEndian.PutUint32(addr[:], binary.BigEndian.Uint32(addr[:])|^binary.BigEndian.Uint32(mask))
  372. cs.myVpnBroadcastAddrsTable.Insert(netip.PrefixFrom(netip.AddrFrom4(addr), network.Addr().BitLen()))
  373. }
  374. }
  375. return &cs, nil
  376. }
  377. func loadPrivateKey(privPathOrPEM string) (rawKey []byte, curve cert.Curve, isPkcs11 bool, err error) {
  378. var pemPrivateKey []byte
  379. if strings.Contains(privPathOrPEM, "-----BEGIN") {
  380. pemPrivateKey = []byte(privPathOrPEM)
  381. privPathOrPEM = "<inline>"
  382. rawKey, _, curve, err = cert.UnmarshalPrivateKeyFromPEM(pemPrivateKey)
  383. if err != nil {
  384. return nil, curve, false, fmt.Errorf("error while unmarshaling pki.key %s: %s", privPathOrPEM, err)
  385. }
  386. } else if strings.HasPrefix(privPathOrPEM, "pkcs11:") {
  387. rawKey = []byte(privPathOrPEM)
  388. return rawKey, cert.Curve_P256, true, nil
  389. } else {
  390. pemPrivateKey, err = os.ReadFile(privPathOrPEM)
  391. if err != nil {
  392. return nil, curve, false, fmt.Errorf("unable to read pki.key file %s: %s", privPathOrPEM, err)
  393. }
  394. rawKey, _, curve, err = cert.UnmarshalPrivateKeyFromPEM(pemPrivateKey)
  395. if err != nil {
  396. return nil, curve, false, fmt.Errorf("error while unmarshaling pki.key %s: %s", privPathOrPEM, err)
  397. }
  398. }
  399. return
  400. }
  401. func loadCertificate(b []byte) (cert.Certificate, []byte, error) {
  402. c, b, err := cert.UnmarshalCertificateFromPEM(b)
  403. if err != nil {
  404. return nil, b, fmt.Errorf("error while unmarshaling pki.cert: %w", err)
  405. }
  406. if c.Expired(time.Now()) {
  407. return nil, b, fmt.Errorf("nebula certificate for this host is expired")
  408. }
  409. if len(c.Networks()) == 0 {
  410. return nil, b, fmt.Errorf("no networks encoded in certificate")
  411. }
  412. if c.IsCA() {
  413. return nil, b, fmt.Errorf("host certificate is a CA certificate")
  414. }
  415. return c, b, nil
  416. }
  417. func loadCAPoolFromConfig(l *logrus.Logger, c *config.C) (*cert.CAPool, error) {
  418. caPathOrPEM := c.GetString("pki.ca", "")
  419. if caPathOrPEM == "" {
  420. return nil, errors.New("no pki.ca path or PEM data provided")
  421. }
  422. var caReader io.ReadCloser
  423. var err error
  424. if strings.Contains(caPathOrPEM, "-----BEGIN") {
  425. caReader = io.NopCloser(strings.NewReader(caPathOrPEM))
  426. } else {
  427. caReader, err = os.Open(caPathOrPEM)
  428. if err != nil {
  429. return nil, fmt.Errorf("unable to read pki.ca file %s: %s", caPathOrPEM, err)
  430. }
  431. }
  432. defer caReader.Close()
  433. caPool, err := cert.NewCAPoolFromPEMReader(caReader)
  434. if errors.Is(err, cert.ErrExpired) {
  435. var expired int
  436. for _, crt := range caPool.CAs {
  437. if crt.Certificate.Expired(time.Now()) {
  438. expired++
  439. l.WithField("cert", crt).Warn("expired certificate present in CA pool")
  440. }
  441. }
  442. if expired >= len(caPool.CAs) {
  443. return nil, errors.New("no valid CA certificates present")
  444. }
  445. } else if err != nil {
  446. return nil, fmt.Errorf("error while adding CA certificate to CA trust store: %s", err)
  447. }
  448. bl := c.GetStringSlice("pki.blocklist", []string{})
  449. if len(bl) > 0 {
  450. for _, fp := range bl {
  451. caPool.BlocklistFingerprint(fp)
  452. }
  453. l.WithField("fingerprintCount", len(bl)).Info("Blocklisted certificates")
  454. }
  455. return caPool, nil
  456. }