filter.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Package ubiquity contains the ubiquity scoring logic for CFSSL bundling.
  2. package ubiquity
  3. // Ubiquity is addressed as selecting the chains that are most likely being accepted for different client systems.
  4. // To select, we decide to do multi-round filtering from different ranking perpectives.
  5. import (
  6. "crypto/x509"
  7. )
  8. // RankingFunc returns the relative rank between chain1 and chain2.
  9. // Return value:
  10. // positive integer if rank(chain1) > rank(chain2),
  11. // negative integer if rank(chain1) < rank(chain2),
  12. // 0 if rank(chain1) == (chain2).
  13. type RankingFunc func(chain1, chain2 []*x509.Certificate) int
  14. // Filter filters out the chains with highest rank according to the ranking function f.
  15. func Filter(chains [][]*x509.Certificate, f RankingFunc) [][]*x509.Certificate {
  16. // If there are no chain or only 1 chain, we are done.
  17. if len(chains) <= 1 {
  18. return chains
  19. }
  20. bestChain := chains[0]
  21. var candidateChains [][]*x509.Certificate
  22. for _, chain := range chains {
  23. r := f(bestChain, chain)
  24. if r < 0 {
  25. bestChain = chain
  26. candidateChains = [][]*x509.Certificate{chain}
  27. } else if r == 0 {
  28. candidateChains = append(candidateChains, chain)
  29. }
  30. }
  31. return candidateChains
  32. }