1
0

pki.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. package nebula
  2. import (
  3. "crypto/fips140"
  4. "crypto/sha256"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "log/slog"
  12. "net"
  13. "net/netip"
  14. "os"
  15. "slices"
  16. "strings"
  17. "sync/atomic"
  18. "time"
  19. "github.com/flynn/noise"
  20. "github.com/gaissmai/bart"
  21. "github.com/slackhq/nebula/cert"
  22. "github.com/slackhq/nebula/config"
  23. "github.com/slackhq/nebula/handshake"
  24. "github.com/slackhq/nebula/noiseutil"
  25. "github.com/slackhq/nebula/util"
  26. )
  27. type PKI struct {
  28. cs atomic.Pointer[CertState]
  29. caPool atomic.Pointer[cert.CAPool]
  30. l *slog.Logger
  31. }
  32. type CertState struct {
  33. v1Cert cert.Certificate
  34. v1Credential *handshake.Credential
  35. v2Cert cert.Certificate
  36. v2Credential *handshake.Credential
  37. initiatingVersion cert.Version
  38. privateKey []byte
  39. pkcs11Backed bool
  40. cipher string
  41. myVpnNetworks []netip.Prefix
  42. myVpnNetworksTable *bart.Lite
  43. myVpnAddrs []netip.Addr
  44. myVpnAddrsTable *bart.Lite
  45. myVpnBroadcastAddrsTable *bart.Lite
  46. }
  47. // errCertNeedsRestart marks a new cert that is refused on reload because only a restart can apply it
  48. var errCertNeedsRestart = errors.New("a restart is required to use the new cert")
  49. func NewPKIFromConfig(l *slog.Logger, c *config.C) (*PKI, error) {
  50. pki := &PKI{l: l}
  51. err := pki.reload(c, true, nil)
  52. if err != nil {
  53. return nil, err
  54. }
  55. c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
  56. // Compare with the running cipher, not the last config. A restart with one we can't run would fail, so say that
  57. // now rather than ask for one, and going back to the running one after a bad reload needs no restart
  58. if cipher := c.GetString("cipher", "aes"); !validCipher(cipher) {
  59. l.Error("Unknown cipher, a restart with it would fail", "cipher", cipher)
  60. r.Failed()
  61. } else if cipher == "chachapoly" && fipsOnly() {
  62. // newCipherSuite refuses it at startup
  63. l.Error("ChaChaPoly is not allowed in FIPS 140-only mode, a restart with it would fail", "cipher", cipher)
  64. r.Failed()
  65. } else if cipher != pki.getCertState().cipher {
  66. r.RestartRequiredIfChanged("cipher")
  67. }
  68. rErr := pki.reload(c, false, r)
  69. if rErr != nil {
  70. util.LogWithContextIfNeeded("Failed to reload PKI from config", rErr, l)
  71. }
  72. })
  73. return pki, nil
  74. }
  75. func (p *PKI) GetCAPool() *cert.CAPool {
  76. return p.caPool.Load()
  77. }
  78. func (p *PKI) getCertState() *CertState {
  79. return p.cs.Load()
  80. }
  81. // r is nil on the initial load
  82. func (p *PKI) reload(c *config.C, initial bool, r *config.Reload) error {
  83. before := p.cs.Load()
  84. err := p.reloadCerts(c, initial)
  85. if err != nil {
  86. if initial {
  87. return err
  88. }
  89. err.Log(p.l)
  90. if errors.Is(err, errCertNeedsRestart) {
  91. r.RestartRequired()
  92. } else {
  93. r.Failed()
  94. }
  95. } else if !initial {
  96. // A cert for a version we weren't running is accepted without comparing its networks to ours. The tun and the
  97. // lighthouse keep the networks they started with
  98. after := p.cs.Load()
  99. if !slices.Equal(before.myVpnNetworks, after.myVpnNetworks) {
  100. p.l.Warn("The new cert changes our networks, a restart is required to use them",
  101. "old_networks", before.myVpnNetworks, "new_networks", after.myVpnNetworks)
  102. r.RestartRequired()
  103. }
  104. }
  105. err = p.reloadCAPool(c)
  106. if err != nil {
  107. if initial {
  108. return err
  109. }
  110. err.Log(p.l)
  111. r.Failed()
  112. }
  113. return nil
  114. }
  115. // fipsOnly is fips140.Enforced, a variable so a test can run the FIPS 140-only reload check without GODEBUG
  116. var fipsOnly = fips140.Enforced
  117. // validCipher is true for the ciphers nebula can run. Each post-handshake CipherState in noiseutil hardcodes its own
  118. // nonce endianness, so there's nothing to set up for either.
  119. func validCipher(cipher string) bool {
  120. return cipher == "aes" || cipher == "chachapoly"
  121. }
  122. func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
  123. var cipher string
  124. var currentState *CertState
  125. if initial {
  126. cipher = c.GetString("cipher", "aes")
  127. if !validCipher(cipher) {
  128. return util.NewContextualError(
  129. "unknown cipher",
  130. m{"cipher": cipher},
  131. nil,
  132. )
  133. }
  134. } else {
  135. // Cipher cant be hot swapped so just leave it at what it was before
  136. currentState = p.cs.Load()
  137. cipher = currentState.cipher
  138. }
  139. newState, err := newCertStateFromConfig(c, cipher)
  140. if err != nil {
  141. return util.NewContextualError("Could not load client cert", nil, err)
  142. }
  143. if currentState != nil {
  144. if newState.v1Cert != nil {
  145. if currentState.v1Cert == nil {
  146. //adding certs is fine, actually. Networks-in-common confirmed in newCertState().
  147. } else {
  148. // did IP in cert change? if so, don't set
  149. if !slices.Equal(currentState.v1Cert.Networks(), newState.v1Cert.Networks()) {
  150. return util.NewContextualError(
  151. "Networks in new cert was different from old",
  152. m{"new_networks": newState.v1Cert.Networks(), "old_networks": currentState.v1Cert.Networks(), "cert_version": cert.Version1},
  153. errCertNeedsRestart,
  154. )
  155. }
  156. if currentState.v1Cert.Curve() != newState.v1Cert.Curve() {
  157. return util.NewContextualError(
  158. "Curve in new v1 cert was different from old",
  159. m{"new_curve": newState.v1Cert.Curve(), "old_curve": currentState.v1Cert.Curve(), "cert_version": cert.Version1},
  160. errCertNeedsRestart,
  161. )
  162. }
  163. }
  164. }
  165. if newState.v2Cert != nil {
  166. if currentState.v2Cert == nil {
  167. //adding certs is fine, actually
  168. } else {
  169. // did IP in cert change? if so, don't set
  170. if !slices.Equal(currentState.v2Cert.Networks(), newState.v2Cert.Networks()) {
  171. return util.NewContextualError(
  172. "Networks in new cert was different from old",
  173. m{"new_networks": newState.v2Cert.Networks(), "old_networks": currentState.v2Cert.Networks(), "cert_version": cert.Version2},
  174. errCertNeedsRestart,
  175. )
  176. }
  177. if currentState.v2Cert.Curve() != newState.v2Cert.Curve() {
  178. return util.NewContextualError(
  179. "Curve in new cert was different from old",
  180. m{"new_curve": newState.v2Cert.Curve(), "old_curve": currentState.v2Cert.Curve(), "cert_version": cert.Version2},
  181. errCertNeedsRestart,
  182. )
  183. }
  184. }
  185. } else if currentState.v2Cert != nil {
  186. //newState.v1Cert is non-nil bc empty certstates aren't permitted
  187. if newState.v1Cert == nil {
  188. return util.NewContextualError("v1 and v2 certs are nil, this should be impossible", nil, err)
  189. }
  190. //if we're going to v1-only, we need to make sure we didn't orphan any v2-cert vpnaddrs
  191. if !slices.Equal(currentState.v2Cert.Networks(), newState.v1Cert.Networks()) {
  192. return util.NewContextualError(
  193. "Removing a V2 cert is not permitted unless it has identical networks to the new V1 cert",
  194. m{"new_v1_networks": newState.v1Cert.Networks(), "old_v2_networks": currentState.v2Cert.Networks()},
  195. errCertNeedsRestart,
  196. )
  197. }
  198. }
  199. }
  200. p.cs.Store(newState)
  201. if initial {
  202. p.l.Debug("Client nebula certificate(s)", "cert", newState)
  203. } else {
  204. p.l.Info("Client certificate(s) refreshed from disk", "cert", newState)
  205. }
  206. return nil
  207. }
  208. func (p *PKI) reloadCAPool(c *config.C) *util.ContextualError {
  209. caPool, err := loadCAPoolFromConfig(p.l, c)
  210. if caPool != nil {
  211. p.caPool.Store(caPool)
  212. p.l.Debug("Trusted CA fingerprints", "fingerprints", caPool.GetFingerprints())
  213. }
  214. if err != nil {
  215. return util.NewContextualError("Failed to load ca from config", nil, err)
  216. }
  217. return nil
  218. }
  219. func (cs *CertState) GetDefaultCertificate() cert.Certificate {
  220. c := cs.getCertificate(cs.initiatingVersion)
  221. if c == nil {
  222. panic("No default certificate found")
  223. }
  224. return c
  225. }
  226. // DefaultVersion returns the preferred cert version for initiating handshakes.
  227. func (cs *CertState) DefaultVersion() cert.Version { return cs.initiatingVersion }
  228. // GetCredential returns the pre-computed handshake credential for the given version, or nil.
  229. func (cs *CertState) GetCredential(v cert.Version) *handshake.Credential {
  230. switch v {
  231. case cert.Version1:
  232. return cs.v1Credential
  233. case cert.Version2:
  234. return cs.v2Credential
  235. }
  236. return nil
  237. }
  238. func (cs *CertState) getCertificate(v cert.Version) cert.Certificate {
  239. switch v {
  240. case cert.Version1:
  241. return cs.v1Cert
  242. case cert.Version2:
  243. return cs.v2Cert
  244. }
  245. return nil
  246. }
  247. // newCipherSuite builds the noise.CipherSuite for the given curve and cipher.
  248. // When fips140Enforced is true (FIPS 140-only mode), non-approved algorithms
  249. // (Curve25519 and ChaChaPoly) are rejected with an error. Callers pass
  250. // fips140.Enforced() for fips140Enforced.
  251. func newCipherSuite(curve cert.Curve, pkcs11backed bool, cipher string, fips140Enforced bool) (noise.CipherSuite, error) {
  252. var dhFunc noise.DHFunc
  253. switch curve {
  254. case cert.Curve_CURVE25519:
  255. if fips140Enforced {
  256. return nil, errors.New("pki: use of Curve25519 is not allowed in FIPS 140-only mode")
  257. }
  258. dhFunc = noise.DH25519
  259. case cert.Curve_P256:
  260. if pkcs11backed {
  261. dhFunc = noiseutil.DHP256PKCS11
  262. } else {
  263. dhFunc = noiseutil.DHP256
  264. }
  265. default:
  266. return nil, fmt.Errorf("unsupported curve: %s", curve)
  267. }
  268. if cipher == "chachapoly" {
  269. if fips140Enforced {
  270. return nil, errors.New("pki: use of ChaChaPoly is not allowed in FIPS 140-only mode")
  271. }
  272. return noise.NewCipherSuite(dhFunc, noise.CipherChaChaPoly, noise.HashSHA256), nil
  273. }
  274. return noise.NewCipherSuite(dhFunc, noiseutil.CipherAESGCM, noise.HashSHA256), nil
  275. }
  276. func (cs *CertState) String() string {
  277. b, err := cs.MarshalJSON()
  278. if err != nil {
  279. return fmt.Sprintf("error marshaling certificate state: %v", err)
  280. }
  281. return string(b)
  282. }
  283. func (cs *CertState) MarshalJSON() ([]byte, error) {
  284. msg := []json.RawMessage{}
  285. if cs.v1Cert != nil {
  286. b, err := cs.v1Cert.MarshalJSON()
  287. if err != nil {
  288. return nil, err
  289. }
  290. msg = append(msg, b)
  291. }
  292. if cs.v2Cert != nil {
  293. b, err := cs.v2Cert.MarshalJSON()
  294. if err != nil {
  295. return nil, err
  296. }
  297. msg = append(msg, b)
  298. }
  299. return json.Marshal(msg)
  300. }
  301. func newCertStateFromConfig(c *config.C, cipher string) (*CertState, error) {
  302. var err error
  303. privPathOrPEM := c.GetString("pki.key", "")
  304. if privPathOrPEM == "" {
  305. return nil, errors.New("no pki.key path or PEM data provided")
  306. }
  307. rawKey, curve, isPkcs11, err := loadPrivateKey(privPathOrPEM)
  308. if err != nil {
  309. return nil, err
  310. }
  311. var rawCert []byte
  312. pubPathOrPEM := c.GetString("pki.cert", "")
  313. if pubPathOrPEM == "" {
  314. return nil, errors.New("no pki.cert path or PEM data provided")
  315. }
  316. if strings.Contains(pubPathOrPEM, "-----BEGIN") {
  317. rawCert = []byte(pubPathOrPEM)
  318. pubPathOrPEM = "<inline>"
  319. } else {
  320. rawCert, err = os.ReadFile(pubPathOrPEM)
  321. if err != nil {
  322. return nil, fmt.Errorf("unable to read pki.cert file %s: %s", pubPathOrPEM, err)
  323. }
  324. }
  325. var crt, v1, v2 cert.Certificate
  326. for {
  327. // Load the certificate
  328. crt, rawCert, err = loadCertificate(rawCert)
  329. if err != nil {
  330. return nil, err
  331. }
  332. if fips140.Enforced() && crt.Curve() != cert.Curve_P256 {
  333. return nil, fmt.Errorf("pki: use of %s is not allowed in FIPS 140-only mode", crt.Curve())
  334. }
  335. switch crt.Version() {
  336. case cert.Version1:
  337. if v1 != nil {
  338. return nil, fmt.Errorf("v1 certificate already found in pki.cert")
  339. }
  340. v1 = crt
  341. case cert.Version2:
  342. if v2 != nil {
  343. return nil, fmt.Errorf("v2 certificate already found in pki.cert")
  344. }
  345. v2 = crt
  346. default:
  347. return nil, fmt.Errorf("unknown certificate version %v", crt.Version())
  348. }
  349. if len(rawCert) == 0 || strings.TrimSpace(string(rawCert)) == "" {
  350. break
  351. }
  352. }
  353. if v1 == nil && v2 == nil {
  354. return nil, errors.New("no certificates found in pki.cert")
  355. }
  356. useInitiatingVersion := uint32(1)
  357. if v1 == nil {
  358. // The only condition that requires v2 as the default is if only a v2 certificate is present
  359. // We do this to avoid having to configure it specifically in the config file
  360. useInitiatingVersion = 2
  361. }
  362. rawInitiatingVersion := c.GetUint32("pki.initiating_version", useInitiatingVersion)
  363. var initiatingVersion cert.Version
  364. switch rawInitiatingVersion {
  365. case 1:
  366. if v1 == nil {
  367. return nil, fmt.Errorf("can not use pki.initiating_version 1 without a v1 certificate in pki.cert")
  368. }
  369. initiatingVersion = cert.Version1
  370. case 2:
  371. initiatingVersion = cert.Version2
  372. default:
  373. return nil, fmt.Errorf("unknown pki.initiating_version: %v", rawInitiatingVersion)
  374. }
  375. return newCertState(initiatingVersion, v1, v2, isPkcs11, curve, rawKey, cipher)
  376. }
  377. func newCertState(dv cert.Version, v1, v2 cert.Certificate, pkcs11backed bool, privateKeyCurve cert.Curve, privateKey []byte, cipher string) (*CertState, error) {
  378. cs := CertState{
  379. privateKey: privateKey,
  380. pkcs11Backed: pkcs11backed,
  381. cipher: cipher,
  382. myVpnNetworksTable: new(bart.Lite),
  383. myVpnAddrsTable: new(bart.Lite),
  384. myVpnBroadcastAddrsTable: new(bart.Lite),
  385. }
  386. if v1 != nil && v2 != nil {
  387. if !slices.Equal(v1.PublicKey(), v2.PublicKey()) {
  388. return nil, util.NewContextualError("v1 and v2 public keys are not the same, ignoring", nil, nil)
  389. }
  390. if v1.Curve() != v2.Curve() {
  391. return nil, util.NewContextualError("v1 and v2 curve are not the same, ignoring", nil, nil)
  392. }
  393. if v1.Networks()[0] != v2.Networks()[0] {
  394. return nil, util.NewContextualError("v1 and v2 networks are not the same", nil, nil)
  395. }
  396. cs.initiatingVersion = dv
  397. }
  398. if v1 != nil {
  399. if pkcs11backed {
  400. //NOTE: We do not currently have a method to verify a public private key pair when the private key is in an hsm
  401. } else {
  402. if err := v1.VerifyPrivateKey(privateKeyCurve, privateKey); err != nil {
  403. return nil, fmt.Errorf("private key is not a pair with public key in nebula cert: %w", err)
  404. }
  405. }
  406. v1hs, err := v1.MarshalForHandshakes()
  407. if err != nil {
  408. return nil, fmt.Errorf("error marshalling v1 certificate for handshake: %w", err)
  409. }
  410. ncs, err := newCipherSuite(v1.Curve(), pkcs11backed, cipher, fips140.Enforced())
  411. if err != nil {
  412. return nil, err
  413. }
  414. cs.v1Cert = v1
  415. cs.v1Credential = handshake.NewCredential(v1, v1hs, privateKey, ncs)
  416. if cs.initiatingVersion == 0 {
  417. cs.initiatingVersion = cert.Version1
  418. }
  419. }
  420. if v2 != nil {
  421. if pkcs11backed {
  422. //NOTE: We do not currently have a method to verify a public private key pair when the private key is in an hsm
  423. } else {
  424. if err := v2.VerifyPrivateKey(privateKeyCurve, privateKey); err != nil {
  425. return nil, fmt.Errorf("private key is not a pair with public key in nebula cert: %w", err)
  426. }
  427. }
  428. v2hs, err := v2.MarshalForHandshakes()
  429. if err != nil {
  430. return nil, fmt.Errorf("error marshalling v2 certificate for handshake: %w", err)
  431. }
  432. ncs, err := newCipherSuite(v2.Curve(), pkcs11backed, cipher, fips140.Enforced())
  433. if err != nil {
  434. return nil, err
  435. }
  436. cs.v2Cert = v2
  437. cs.v2Credential = handshake.NewCredential(v2, v2hs, privateKey, ncs)
  438. if cs.initiatingVersion == 0 {
  439. cs.initiatingVersion = cert.Version2
  440. }
  441. }
  442. var crt cert.Certificate
  443. crt = cs.getCertificate(cert.Version2)
  444. if crt == nil {
  445. // v2 certificates are a superset, only look at v1 if its all we have
  446. crt = cs.getCertificate(cert.Version1)
  447. }
  448. for _, network := range crt.Networks() {
  449. cs.myVpnNetworks = append(cs.myVpnNetworks, network)
  450. cs.myVpnNetworksTable.Insert(network)
  451. cs.myVpnAddrs = append(cs.myVpnAddrs, network.Addr())
  452. cs.myVpnAddrsTable.Insert(netip.PrefixFrom(network.Addr(), network.Addr().BitLen()))
  453. if network.Addr().Is4() {
  454. addr := network.Masked().Addr().As4()
  455. mask := net.CIDRMask(network.Bits(), network.Addr().BitLen())
  456. binary.BigEndian.PutUint32(addr[:], binary.BigEndian.Uint32(addr[:])|^binary.BigEndian.Uint32(mask))
  457. cs.myVpnBroadcastAddrsTable.Insert(netip.PrefixFrom(netip.AddrFrom4(addr), network.Addr().BitLen()))
  458. }
  459. }
  460. return &cs, nil
  461. }
  462. func loadPrivateKey(privPathOrPEM string) (rawKey []byte, curve cert.Curve, isPkcs11 bool, err error) {
  463. var pemPrivateKey []byte
  464. if strings.Contains(privPathOrPEM, "-----BEGIN") {
  465. pemPrivateKey = []byte(privPathOrPEM)
  466. privPathOrPEM = "<inline>"
  467. rawKey, _, curve, err = cert.UnmarshalPrivateKeyFromPEM(pemPrivateKey)
  468. if err != nil {
  469. return nil, curve, false, fmt.Errorf("error while unmarshaling pki.key %s: %s", privPathOrPEM, err)
  470. }
  471. } else if strings.HasPrefix(privPathOrPEM, "pkcs11:") {
  472. rawKey = []byte(privPathOrPEM)
  473. return rawKey, cert.Curve_P256, true, nil
  474. } else {
  475. pemPrivateKey, err = os.ReadFile(privPathOrPEM)
  476. if err != nil {
  477. return nil, curve, false, fmt.Errorf("unable to read pki.key file %s: %s", privPathOrPEM, err)
  478. }
  479. rawKey, _, curve, err = cert.UnmarshalPrivateKeyFromPEM(pemPrivateKey)
  480. if err != nil {
  481. return nil, curve, false, fmt.Errorf("error while unmarshaling pki.key %s: %s", privPathOrPEM, err)
  482. }
  483. }
  484. return
  485. }
  486. func loadCertificate(b []byte) (cert.Certificate, []byte, error) {
  487. c, b, err := cert.UnmarshalCertificateFromPEM(b)
  488. if err != nil {
  489. return nil, b, fmt.Errorf("error while unmarshaling pki.cert: %w", err)
  490. }
  491. if c.Expired(time.Now()) {
  492. return nil, b, fmt.Errorf("nebula certificate for this host is expired")
  493. }
  494. if len(c.Networks()) == 0 {
  495. return nil, b, fmt.Errorf("no networks encoded in certificate")
  496. }
  497. if c.IsCA() {
  498. return nil, b, fmt.Errorf("host certificate is a CA certificate")
  499. }
  500. return c, b, nil
  501. }
  502. func loadCAPoolFromConfig(l *slog.Logger, c *config.C) (*cert.CAPool, error) {
  503. caPathOrPEM := c.GetString("pki.ca", "")
  504. if caPathOrPEM == "" {
  505. return nil, errors.New("no pki.ca path or PEM data provided")
  506. }
  507. var caReader io.ReadCloser
  508. var err error
  509. if strings.Contains(caPathOrPEM, "-----BEGIN") {
  510. caReader = io.NopCloser(strings.NewReader(caPathOrPEM))
  511. } else {
  512. caReader, err = os.Open(caPathOrPEM)
  513. if err != nil {
  514. return nil, fmt.Errorf("unable to read pki.ca file %s: %s", caPathOrPEM, err)
  515. }
  516. }
  517. defer caReader.Close()
  518. caPool, err := cert.NewCAPoolFromPEMReader(caReader)
  519. if errors.Is(err, cert.ErrExpired) {
  520. var expired int
  521. for _, crt := range caPool.CAs {
  522. if crt.Certificate.Expired(time.Now()) {
  523. expired++
  524. l.Warn("expired certificate present in CA pool", "cert", crt)
  525. }
  526. }
  527. if expired >= len(caPool.CAs) {
  528. return nil, errors.New("no valid CA certificates present")
  529. }
  530. } else if err != nil {
  531. return nil, fmt.Errorf("error while adding CA certificate to CA trust store: %s", err)
  532. }
  533. bl := c.GetStringSlice("pki.blocklist", []string{})
  534. if len(bl) > 0 {
  535. var bad []string
  536. for i, raw := range bl {
  537. // Fingerprints are compared as lowercase hex strings, a malformed entry would silently block nothing
  538. fp := strings.ToLower(strings.TrimSpace(raw))
  539. if b, err := hex.DecodeString(fp); err != nil || len(b) != sha256.Size {
  540. bad = append(bad, fmt.Sprintf("entry %d %q", i+1, raw))
  541. continue
  542. }
  543. caPool.BlocklistFingerprint(fp)
  544. }
  545. l.Info("Blocklisted certificates", "fingerprintCount", len(bl)-len(bad))
  546. if len(bad) > 0 {
  547. // The valid entries are still returned so a failed reload blocks them, startup refuses the error
  548. return caPool, fmt.Errorf("pki.blocklist entries are not hex certificate fingerprints: %s", strings.Join(bad, ", "))
  549. }
  550. }
  551. return caPool, nil
  552. }