/
helpers.go
84 lines (69 loc) · 1.82 KB
/
helpers.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package simple
import (
"bytes"
"container/list"
"crypto/tls"
"crypto/x509"
"darvaza.org/core"
"darvaza.org/darvaza/shared/storage/certpool"
"darvaza.org/darvaza/shared/x509utils"
)
// FindSupportedInMap attempts to find a matching supported tls.Certificate
// on a MapList
func FindSupportedInMap(chi *tls.ClientHelloInfo,
name string, m map[string]*list.List) *tls.Certificate {
//
var out *tls.Certificate
if name == "" {
// no sanitied name provided, produce one
s, ok := x509utils.SanitiseName(chi.ServerName)
if !ok {
return nil
}
name = s
}
core.MapListForEach(m, name, func(c *tls.Certificate) bool {
if err := chi.SupportsCertificate(c); err == nil {
out = c
}
// stop on the first supported match
return out != nil
})
return out
}
// FindInMap attempts to find matching [tls.Certificate]s on a MapList
func FindInMap(name string, m map[string]*list.List, once bool) []*tls.Certificate {
var out []*tls.Certificate
core.MapListForEach(m, name, func(c *tls.Certificate) bool {
if c != nil {
out = append(out, c)
return once
}
core.Panic("unreachable")
return false
})
return out
}
func mapListContainsHash(m map[string]*list.List, name string, hash certpool.Hash) bool {
var found bool
core.MapListForEach(m, name, func(c *tls.Certificate) bool {
h := certpool.HashCert(c.Leaf)
if bytes.Equal(hash[:], h[:]) {
found = true
}
return found
})
return found
}
// PairMatch tells if the public key of a PrivateKey is the
// same as included in a *x509.Certificate
func PairMatch(cert *x509.Certificate, pk x509utils.PrivateKey) bool {
if pub, ok := pk.Public().(x509utils.PublicKey); ok {
return pub.Equal(cert.PublicKey)
}
return false
}
// PrivateKeyEqual tells if two private keys are the same
func PrivateKeyEqual(a, b x509utils.PrivateKey) bool {
return a.Equal(b)
}