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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package openbao
import (
"context"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/vault"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/lease"
)
type CertRequest struct {
CommonName string
AltNames []string
IPSANs []string
TTL string
ExcludeCNFromSANs bool
NotAfter string
}
type Cert struct {
Certificate string
PrivateKey string
IssuingCA string
CAChain []string
Serial string
}
type PKIRole struct {
AllowedDomains []string
AllowSubdomains bool
MaxTTL string
}
func (c *Client) IssueCert(ctx context.Context, pkiPath, role string, req CertRequest) (Cert, error) {
body := map[string]any{"common_name": req.CommonName}
if len(req.AltNames) > 0 {
body["alt_names"] = strings.Join(req.AltNames, ",")
}
if len(req.IPSANs) > 0 {
body["ip_sans"] = strings.Join(req.IPSANs, ",")
}
if req.TTL != "" {
body["ttl"] = req.TTL
}
if req.ExcludeCNFromSANs {
body["exclude_cn_from_sans"] = true
}
if req.NotAfter != "" {
body["not_after"] = req.NotAfter
}
raw, err := c.do(ctx, http.MethodPost, strings.Trim(pkiPath, "/")+"/issue/"+strings.Trim(role, "/"), body)
if err != nil {
return Cert{}, err
}
return parseCert(raw)
}
func (c *Client) ConfigurePKIURLs(ctx context.Context, pkiPath, issuingCertificates, crlDistributionPoints string) error {
_, err := c.do(ctx, http.MethodPost, strings.Trim(pkiPath, "/")+"/config/urls", map[string]any{
"issuing_certificates": issuingCertificates,
"crl_distribution_points": crlDistributionPoints,
})
return err
}
func (c *Client) GeneratePKIRoot(ctx context.Context, pkiPath, commonName, ttl string) (Cert, error) {
body := map[string]any{"common_name": commonName}
if ttl != "" {
body["ttl"] = ttl
}
raw, err := c.do(ctx, http.MethodPost, strings.Trim(pkiPath, "/")+"/root/generate/internal", body)
if err != nil {
return Cert{}, err
}
return parseCert(raw)
}
func (c *Client) WritePKIRole(ctx context.Context, pkiPath, role string, cfg PKIRole) error {
body := map[string]any{
"allowed_domains": cfg.AllowedDomains,
"allow_subdomains": cfg.AllowSubdomains,
}
if cfg.MaxTTL != "" {
body["max_ttl"] = cfg.MaxTTL
}
_, err := c.do(ctx, http.MethodPost, strings.Trim(pkiPath, "/")+"/roles/"+strings.Trim(role, "/"), body)
return err
}
func (c *Client) CAPEM(ctx context.Context, pkiMount string) (string, error) {
return c.rawPEM(ctx, strings.Trim(pkiMount, "/")+"/ca/pem")
}
func (c *Client) CAChainPEM(ctx context.Context, pkiMount string) (string, error) {
return c.rawPEM(ctx, strings.Trim(pkiMount, "/")+"/ca_chain")
}
func (c *Client) rawPEM(ctx context.Context, path string) (string, error) {
status, body, err := c.request(ctx, http.MethodGet, path, nil)
if err != nil {
return "", err
}
if status < 200 || status > 299 {
return "", &ResponseError{Method: http.MethodGet, Path: path, Status: status, Body: strings.TrimSpace(string(body))}
}
out := strings.TrimSpace(string(body))
if out == "" {
return "", fmt.Errorf("openbao: %s holds no certificate", path)
}
return out + "\n", nil
}
func parseCert(raw json.RawMessage) (Cert, error) {
var payload struct {
Certificate string `json:"certificate"`
PrivateKey string `json:"private_key"`
IssuingCA string `json:"issuing_ca"`
CAChain []string `json:"ca_chain"`
SerialNumber string `json:"serial_number"`
}
if err := json.Unmarshal(raw, &payload); err != nil {
return Cert{}, fmt.Errorf("openbao: a PKI response was not JSON: %v", err)
}
return Cert{Certificate: payload.Certificate, PrivateKey: payload.PrivateKey, IssuingCA: payload.IssuingCA,
CAChain: payload.CAChain, Serial: payload.SerialNumber}, nil
}
func (c *Client) Namespace(namespace string) *Client {
out := c.with(c.token, c.renews)
if out.api != nil {
out.api = out.api.WithNamespace(namespace)
}
return out
}
type Backend struct {
Client *Client
}
var _ vault.PKIBackend = Backend{}
func (b Backend) in(namespace string) *Client {
if namespace == "" {
return b.Client
}
return b.Client.Namespace(namespace)
}
func (b Backend) EnsureNamespace(ctx context.Context, path string) error {
if path == "" {
return nil
}
_, err := b.Client.Read(ctx, "sys/namespaces/"+path)
if err == nil {
return nil
}
if !errors.Is(err, ErrNotFound) {
return err
}
return b.Client.Write(ctx, "sys/namespaces/"+path, map[string]any{})
}
func (b Backend) DeleteNamespace(ctx context.Context, path string) error {
return b.Client.Delete(ctx, "sys/namespaces/"+path)
}
func (b Backend) EnsureMount(ctx context.Context, namespace, path, kind string) error {
c := b.in(namespace)
mounts, err := c.SecretMounts(ctx)
if err != nil {
return err
}
key := strings.Trim(path, "/")
if info, ok := mounts[key]; ok {
if info.Type != kind {
return fmt.Errorf("openbao: %s in namespace %q is mounted as %q, not %q, and this will not replace a mount in use",
key, namespace, info.Type, kind)
}
return nil
}
return c.enableMount(ctx, key, map[string]any{"type": kind})
}
func (b Backend) GenerateRoot(ctx context.Context, namespace, mount, commonName, ttl string) (vault.RootCA, error) {
cert, err := b.in(namespace).GeneratePKIRoot(ctx, mount, commonName, ttl)
if err != nil {
return vault.RootCA{}, err
}
out := vault.RootCA{Certificate: cert.Certificate, Serial: cert.Serial}
if out.Serial == "" {
out.Serial = serialOfPEM(out.Certificate)
}
return out, nil
}
func (b Backend) CASerial(ctx context.Context, namespace, mount string) (string, error) {
data, err := b.in(namespace).Read(ctx, strings.Trim(mount, "/")+"/cert/ca")
if err != nil {
var response *ResponseError
if errors.Is(err, ErrNotFound) ||
(errors.As(err, &response) && response.Status == http.StatusBadRequest && strings.Contains(response.Body, "no default issuer")) {
return "", fmt.Errorf("%w: %w: no CA at %s", vault.ErrNoAuthority, ErrNotFound, mount)
}
return "", err
}
certificate, _ := data["certificate"].(string)
if certificate == "" {
return "", fmt.Errorf("%w: %w: no CA at %s", vault.ErrNoAuthority, ErrNotFound, mount)
}
return serialOfPEM(certificate), nil
}
func (b Backend) CAChain(ctx context.Context, namespace, mount string) (string, error) {
chain, err := b.in(namespace).CAChainPEM(ctx, mount)
return strings.TrimSpace(chain), err
}
func (b Backend) GenerateIntermediate(ctx context.Context, namespace, mount, commonName string) (vault.IntermediateCSR, error) {
data, err := b.in(namespace).WriteData(ctx, strings.Trim(mount, "/")+"/intermediate/generate/internal", map[string]any{
"common_name": commonName,
"key_type": "ec",
"key_bits": 256,
})
if err != nil {
return vault.IntermediateCSR{}, err
}
return vault.IntermediateCSR{CSR: stringField(data, "csr"), PrivateKey: stringField(data, "private_key")}, nil
}
func (b Backend) SignIntermediate(ctx context.Context, rootNamespace, mount, csr, commonName, ttl string) (string, error) {
body := map[string]any{"csr": csr, "common_name": commonName}
if ttl != "" {
body["ttl"] = ttl
}
data, err := b.in(rootNamespace).WriteData(ctx, strings.Trim(mount, "/")+"/root/sign-intermediate", body)
if err != nil {
return "", err
}
var chain strings.Builder
chain.WriteString(stringField(data, "certificate"))
for _, entry := range stringList(data, "ca_chain") {
chain.WriteString("\n" + entry)
}
return chain.String(), nil
}
func (b Backend) SetSignedIntermediate(ctx context.Context, namespace, mount, chain string) error {
return b.in(namespace).Write(ctx, strings.Trim(mount, "/")+"/intermediate/set-signed", map[string]any{"certificate": chain})
}
func (b Backend) WriteRole(ctx context.Context, namespace, mount, name string, role vault.Role) error {
body := map[string]any{
"allow_subdomains": role.AllowSubdomains,
"allow_bare_domains": role.AllowBareDomains,
"enforce_hostnames": role.EnforceHostnames,
"allow_ip_sans": true,
"no_store": role.NoStore,
}
if len(role.AllowedDomains) > 0 {
body["allowed_domains"] = role.AllowedDomains
}
if role.KeyType != "" {
body["key_type"] = role.KeyType
}
if role.KeyBits > 0 {
body["key_bits"] = role.KeyBits
}
if role.MaxTTL != "" {
body["max_ttl"] = role.MaxTTL
}
return b.in(namespace).Write(ctx, strings.Trim(mount, "/")+"/roles/"+strings.Trim(name, "/"), body)
}
func (b Backend) Issue(ctx context.Context, namespace, mount, role string, req vault.CertRequest) (vault.Cert, error) {
cert, err := b.in(namespace).IssueCert(ctx, mount, role, CertRequest{CommonName: req.CommonName, AltNames: req.AltNames,
IPSANs: req.IPSANs, TTL: req.TTL})
if err != nil {
return vault.Cert{}, err
}
return vault.Cert{Certificate: cert.Certificate, PrivateKey: cert.PrivateKey, IssuingCA: cert.IssuingCA,
CAChain: cert.CAChain, Serial: cert.Serial}, nil
}
func CAPoolFetch(pki vault.PKI, mount string, lifetime time.Duration) lease.Fetch[*x509.CertPool] {
return func(ctx context.Context) (*x509.CertPool, time.Time, error) {
chain, err := pki.CAChain(ctx, mount)
if err != nil {
return nil, time.Time{}, err
}
pool, err := CertPoolFromPEM([]byte(chain))
if err != nil {
return nil, time.Time{}, err
}
return pool, time.Now().Add(lifetime), nil
}
}
func serialOfPEM(body string) string {
block, _ := pem.Decode([]byte(body))
if block == nil {
return ""
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return ""
}
hex := strings.ToUpper(cert.SerialNumber.Text(16))
if len(hex)%2 == 1 {
hex = "0" + hex
}
pairs := make([]string, 0, len(hex)/2)
for i := 0; i < len(hex); i += 2 {
pairs = append(pairs, hex[i:i+2])
}
return strings.Join(pairs, ":")
}
func stringField(data map[string]any, key string) string {
value, _ := data[key].(string)
return value
}
func stringList(data map[string]any, key string) []string {
raw, ok := data[key].([]any)
if !ok {
return nil
}
out := make([]string, 0, len(raw))
for _, entry := range raw {
if value, ok := entry.(string); ok {
out = append(out, value)
}
}
return out
}