forked from smallstep/scep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certs_selector.go
57 lines (50 loc) · 1.63 KB
/
certs_selector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package scep
import (
"bytes"
"crypto"
"crypto/x509"
)
// A CertsSelector filters certificates.
type CertsSelector interface {
SelectCerts([]*x509.Certificate) []*x509.Certificate
}
// CertsSelectorFunc is a type of function that filters certificates.
type CertsSelectorFunc func([]*x509.Certificate) []*x509.Certificate
func (f CertsSelectorFunc) SelectCerts(certs []*x509.Certificate) []*x509.Certificate {
return f(certs)
}
// NopCertsSelector returns a CertsSelectorFunc that does not do anything.
func NopCertsSelector() CertsSelectorFunc {
return func(certs []*x509.Certificate) []*x509.Certificate {
return certs
}
}
// A EnciphermentCertsSelector returns a CertsSelectorFunc that selects
// certificates eligible for key encipherment. This certsSelector can be used
// to filter PKCSReq recipients.
func EnciphermentCertsSelector() CertsSelectorFunc {
return func(certs []*x509.Certificate) (selected []*x509.Certificate) {
enciphermentKeyUsages := x509.KeyUsageKeyEncipherment | x509.KeyUsageDataEncipherment
for _, cert := range certs {
if cert.KeyUsage&enciphermentKeyUsages != 0 {
selected = append(selected, cert)
}
}
return selected
}
}
// FingerprintCertsSelector selects a certificate that matches hash using
// hashType against the digest of the raw certificate DER bytes
func FingerprintCertsSelector(hashType crypto.Hash, hash []byte) CertsSelectorFunc {
return func(certs []*x509.Certificate) (selected []*x509.Certificate) {
for _, cert := range certs {
h := hashType.New()
h.Write(cert.Raw)
if bytes.Equal(hash, h.Sum(nil)) {
selected = append(selected, cert)
return
}
}
return
}
}