filter.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. //
  11. // - positive integer if rank(chain1) > rank(chain2),
  12. // - negative integer if rank(chain1) < rank(chain2),
  13. // - 0 if rank(chain1) == (chain2).
  14. type RankingFunc func(chain1, chain2 []*x509.Certificate) int
  15. // Filter filters out the chains with highest rank according to the ranking function f.
  16. func Filter(chains [][]*x509.Certificate, f RankingFunc) [][]*x509.Certificate {
  17. // If there are no chain or only 1 chain, we are done.
  18. if len(chains) <= 1 {
  19. return chains
  20. }
  21. bestChain := chains[0]
  22. var candidateChains [][]*x509.Certificate
  23. for _, chain := range chains {
  24. r := f(bestChain, chain)
  25. if r < 0 {
  26. bestChain = chain
  27. candidateChains = [][]*x509.Certificate{chain}
  28. } else if r == 0 {
  29. candidateChains = append(candidateChains, chain)
  30. }
  31. }
  32. return candidateChains
  33. }