/
writer.go
214 lines (176 loc) · 4.24 KB
/
writer.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package simple
import (
"bytes"
"container/list"
"context"
"crypto/tls"
"crypto/x509"
"io/fs"
"strings"
"darvaza.org/core"
"darvaza.org/darvaza/shared/storage/certpool"
"darvaza.org/darvaza/shared/x509utils"
)
var (
_ x509utils.WriteStore = (*Store)(nil)
)
// AddCert adds a Certificate to be paired with a key and bundled
func (s *Store) AddCert(name string, cert *x509.Certificate) error {
s.lockInit()
defer s.mu.Unlock()
hash := certpool.HashCert(cert)
if ci, ok := s.hashed[hash]; ok {
// known
if name != "" {
// maybe new name?
return s.appendName(ci, name)
}
return fs.ErrExist
}
key := s.findMatchingKey(cert)
if key == nil {
err := core.Wrap(fs.ErrNotExist, "no suitable key available")
return err
}
c, err := s.bundler.Bundle(cert, key)
if err != nil {
err = core.Wrap(err, "failed to bundle certificate")
return err
}
addCerts(s, c)
return nil
}
// Put adds a Certificate to be paired with a key and bundled
func (s *Store) Put(_ context.Context, name string, cert *x509.Certificate) error {
return s.AddCert(name, cert)
}
func (s *Store) appendName(ci *certInfo, name string) error {
if strings.HasPrefix(name, "*.") {
// pattern
k := name[1:]
if mapListContainsHash(s.patterns, k, ci.hash) {
return fs.ErrExist
}
ci.patterns = append(ci.patterns, k)
core.MapListAppend(s.patterns, k, ci.c)
return nil
}
if n, ok := x509utils.NameAsIP(name); ok {
// IP
name = n
}
if mapListContainsHash(s.names, name, ci.hash) {
// already set
return fs.ErrExist
}
ci.names = append(ci.names, name)
core.MapListAppend(s.names, name, ci.c)
return nil
}
func (s *Store) findMatchingKey(cert *x509.Certificate) x509utils.PrivateKey {
for _, key := range s.keys {
if PairMatch(cert, key) {
return key
}
}
return nil
}
// Delete removes a certificate by name
func (s *Store) Delete(_ context.Context, name string) error {
const once = false // all matches
var certs []*tls.Certificate
s.lockInit()
defer s.mu.Unlock()
// find by name
if strings.HasPrefix(name, "*.") {
// pattern
certs = FindInMap(name[1:], s.patterns, once)
} else {
if n, ok := x509utils.NameAsIP(name); ok {
// exact match for IP
name = n
}
certs = FindInMap(name, s.names, once)
}
if len(certs) == 0 {
// none found
return fs.ErrNotExist
}
// delete all matches
for _, c := range certs {
if ci := s.findCertInfo(c.Leaf); ci != nil {
s.deleteByCertInfo(ci)
}
}
return nil
}
// DeleteCert removes a certificate from the store
func (s *Store) DeleteCert(_ context.Context, cert *x509.Certificate) error {
s.lockInit()
defer s.mu.Unlock()
if ci := s.findCertInfo(cert); ci != nil {
s.deleteByCertInfo(ci)
return nil
}
return fs.ErrNotExist
}
func (s *Store) findCertInfo(cert *x509.Certificate) *certInfo {
hash := certpool.HashCert(cert)
if ci, ok := s.hashed[hash]; ok {
return ci
}
return nil
}
func (s *Store) deleteByCertInfo(ci *certInfo) {
deleteMapListElementByHash(s.names, ci.names, ci.hash)
deleteMapListElementByHash(s.patterns, ci.patterns, ci.hash)
deleteListElementByPointer(s.certs, ci, true)
delete(s.hashed, ci.hash)
}
func deleteMapListElementByHash(m map[string]*list.List,
keys []string, hash certpool.Hash) int {
// for each name we can only have one match
const once = true
// leaf has the hash we need
match := func(c *tls.Certificate) bool {
h := certpool.HashCert(c.Leaf)
return bytes.Equal(hash[:], h[:])
}
return deleteMapListElementByMatcher(m, once, keys, match)
}
func deleteMapListElementByMatcher[T any, K comparable](m map[K]*list.List, once bool, keys []K,
match func(v T) bool) int {
var count int
fn := func(key K, el *list.Element) bool {
if v, ok := el.Value.(T); ok {
if match(v) {
m[key].Remove(el)
count++
return once
}
}
return false // continue
}
for _, key := range keys {
core.MapListForEachElement(m, key, func(el *list.Element) bool {
return fn(key, el)
})
}
return count
}
func deleteListElementByPointer[T any](l *list.List, ptr *T, once bool) int {
var count int
fn := func(el *list.Element) bool {
if p, ok := el.Value.(*T); ok {
if ptr == p {
// match
l.Remove(el)
count++
return once
}
}
return false // continue
}
core.ListForEachElement(l, fn)
return count
}