-
Notifications
You must be signed in to change notification settings - Fork 125
/
local.go
524 lines (461 loc) · 14.4 KB
/
local.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package store
import (
"bufio"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io"
"math/big"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/openziti/ziti/ziti/pki/certificate"
"crypto/x509"
)
// Predefined directory names.
const (
LocalCertsDir = "certs"
LocalKeysDir = "keys"
LocalCrlsDir = "crls"
)
var (
// Index format
// 0 full string
// 1 Valid/Revoked/Expired
// 2 Expiration date
// 3 Revocation date
// 4 Serial
// 5 Filename
// 6 Subject
indexRegexp = regexp.MustCompile("^([VRE])\t([0-9]{12}Z)\t([0-9]{12}Z)?\t([0-9a-fA-F]{2,})\t([^\t]+)\t(.+)")
)
// Local lets us store a Certificate Authority on the local filesystem.
//
// The structure used makes it compatible with openssl.
type Local struct {
Root string
}
// path returns private and public key path.
func (l *Local) path(caName, name string) (key string, cert string) {
key = filepath.Join(l.Root, caName, LocalKeysDir, name+".key")
cert = filepath.Join(l.Root, caName, LocalCertsDir, name+".cert")
return
}
// Exists checks if a certificate or private key already exist on the local
// filesystem for a given name.
func (l *Local) Exists(caName, name string) bool {
privPath, certPath := l.path(caName, name)
if _, err := os.Stat(privPath); err == nil {
return true
}
if _, err := os.Stat(certPath); err == nil {
return true
}
return false
}
// Fetch fetches the private key and certificate for a given name signed by caName.
func (l *Local) Fetch(caName, name string) ([]byte, []byte, error) {
filepath.Join(l.Root, caName)
keyPath, certPath := l.path(caName, name)
k, err := readPEM(keyPath)
if err != nil {
return nil, nil, fmt.Errorf("failed reading CA private key from file %v: %v", keyPath, err)
}
c, err := readPEM(certPath)
if err != nil {
return nil, nil, fmt.Errorf("failed reading CA cert from file %v: %v", certPath, err)
}
return k, c, nil
}
// FetchKeyBytes fetches the private key and certificate for a given name signed by caName.
func (l *Local) FetchKeyBytes(caName, name string) ([]byte, error) {
filepath.Join(l.Root, caName)
keyPath, _ := l.path(caName, name)
bytes, err := os.ReadFile(keyPath)
if err != nil {
return nil, fmt.Errorf("failed reading %v: %v", keyPath, err)
}
return bytes, nil
}
func readPEM(path string) ([]byte, error) {
bytes, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed reading %v: %v", path, err)
}
p, _ := pem.Decode(bytes)
if p == nil {
return nil, fmt.Errorf("no PEM data found for certificate")
}
return p.Bytes, nil
}
// Add adds the given bundle to the local filesystem.
func (l *Local) Add(caName, name string, isCa bool, key, cert []byte, allowOverwrite bool) error {
if !allowOverwrite && l.Exists(caName, name) {
return fmt.Errorf("a bundle already exists for the name %v within CA %v", name, caName)
}
if err := l.writeBundle(caName, name, isCa, key, cert); err != nil {
return fmt.Errorf("failed writing bundle %v within CA %v to the local filesystem: %v", name, caName, err)
}
if err := l.updateIndex(caName, name, cert); err != nil {
return fmt.Errorf("failed updating CA %v index: %v", caName, err)
}
return nil
}
// Chain concats an intermediate cert and a newly signed certificate bundle and adds the chained cert to the store.
func (l *Local) Chain(caName, destCaName, name string) error {
chainName := name + ".chain.pem"
if l.Exists(destCaName, chainName) {
return fmt.Errorf("a bundle already exists for the name %v within CA %v", chainName, destCaName)
}
if err := l.writeChainBundle(caName, destCaName, name, chainName); err != nil {
return fmt.Errorf("failed writing chain %v to the local filesystem: %v", chainName, err)
}
return nil
}
// AddCSR adds the given csr to the local filesystem.
func (l *Local) AddCSR(caName, name string, isCa bool, key, cert []byte) error {
if l.Exists(caName, name) {
return fmt.Errorf("a CSR already exists for the name %v within CA %v", name, caName)
}
if err := l.writeBundle(caName, name, isCa, key, cert); err != nil {
return fmt.Errorf("failed writing CSR %v within CA %v to the local filesystem: %v", name, caName, err)
}
return nil
}
// AddKey adds the given private key to the local filesystem.
func (l *Local) AddKey(caName string, name string, key []byte) error {
if l.Exists(caName, name) {
return fmt.Errorf("a key already exists for the key name %v within CA %v", name, caName)
}
pemType := getPkPemType(key)
if err := l.writeKey(caName, name, pemType, key); err != nil {
return fmt.Errorf("failed writing key %v within CA %v to the local filesystem: %v", name, caName, err)
}
return nil
}
// writeKey encodes in PEM format the bundle private key and stores it on the local filesystem.
func (l *Local) writeKey(caName, name, pemType string, key []byte) error {
caDir := filepath.Join(l.Root, caName)
if _, err := os.Stat(caDir); err != nil {
if err := InitCADir(caDir); err != nil {
return fmt.Errorf("root directory for CA %v does not exist and cannot be created: %v", caDir, err)
}
}
keyPath, _ := l.path(caName, name)
if err := encodeAndWrite(keyPath, pemType, key); err != nil {
return fmt.Errorf("failed encoding and writing private key file: %v", err)
}
return nil
}
func getPkPemType(key []byte) string {
var pkcs1Err error
_, pkcs1Err = x509.ParsePKCS1PrivateKey(key)
if pkcs1Err == nil {
return "RSA PRIVATE KEY"
}
var pkcsEcErr error
_, pkcsEcErr = x509.ParseECPrivateKey(key)
if pkcsEcErr == nil {
return "EC PRIVATE KEY"
}
return "PRIVATE KEY"
}
// writeBundle encodes in PEM format the bundle private key and
// certificate and stores them on the local filesystem.
func (l *Local) writeBundle(caName, name string, isCa bool, key, cert []byte) error {
caDir := filepath.Join(l.Root, caName)
if _, err := os.Stat(caDir); err != nil {
if err := InitCADir(caDir); err != nil {
return fmt.Errorf("root directory for CA %v does not exist and cannot be created: %v", caDir, err)
}
}
keyPemType := getPkPemType(key)
keyPath, certPath := l.path(caName, name)
if err := encodeAndWrite(keyPath, keyPemType, key); err != nil {
return fmt.Errorf("failed encoding and writing private key file: %v", err)
}
if err := encodeAndWrite(certPath, "CERTIFICATE", cert); err != nil {
return fmt.Errorf("failed encoding and writing cert file: %v", err)
}
if isCa && name != caName {
intCaDir := filepath.Join(l.Root, name)
if err := InitCADir(intCaDir); err != nil {
return fmt.Errorf("root directory for CA %v does not exist and cannot be created: %v", intCaDir, err)
}
kp, cp := l.path(name, name)
if err := os.Link(keyPath, kp); err != nil {
return fmt.Errorf("failed creating hard link from %v to %v: %v", keyPath, kp, err)
}
if err := os.Link(certPath, cp); err != nil {
return fmt.Errorf("failed creating hard link from %v to %v: %v", certPath, cp, err)
}
}
return nil
}
// writeChainBundle concats...
func (l *Local) writeChainBundle(caName, destCaName, name string, chainName string) error {
caDir := filepath.Join(l.Root, caName)
if _, err := os.Stat(caDir); err != nil {
if err := InitCADir(caDir); err != nil {
return fmt.Errorf("root directory for CA %v does not exist and cannot be created: %v", caDir, err)
}
}
caChainPath := filepath.Join(l.Root, caName, LocalCertsDir, caName+".chain.pem")
caIn, err := os.Open(caChainPath)
if err != nil {
// no chain path, likely means we're using the root as the CA, so fallback to the cert
caPath := filepath.Join(l.Root, caName, LocalCertsDir, caName+".cert")
caIn, err = os.Open(caPath)
if err != nil {
return fmt.Errorf("failed to open CA: %v: %v", caPath, err)
}
}
defer caIn.Close()
serverCertPath := filepath.Join(l.Root, caName, LocalCertsDir, name+".cert")
serverCertIn, err := os.Open(serverCertPath)
if err != nil {
return fmt.Errorf("failed to open server cert: %v: %v", serverCertPath, err)
}
defer serverCertIn.Close()
chainCertPath := filepath.Join(l.Root, destCaName, LocalCertsDir, chainName)
out, err := os.OpenFile(chainCertPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open chain file: %v: %v", chainCertPath, err)
}
defer out.Close()
_, err = io.Copy(out, serverCertIn)
if err != nil {
return fmt.Errorf("failed to append server cert to output: %v", err)
}
_, err = io.Copy(out, caIn)
if err != nil {
return fmt.Errorf("failed to append ca to output: %v", err)
}
return nil
}
func encodeAndWrite(path, pemType string, data []byte) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return pem.Encode(f, &pem.Block{
Type: pemType,
Bytes: data,
})
}
// updateIndex appends a line to the index.txt with few information about the
// given the certificate.
func (l *Local) updateIndex(caName, name string, rawCert []byte) error {
f, err := os.OpenFile(filepath.Join(l.Root, caName, "index.txt"), os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
}
defer f.Close()
cert, err := x509.ParseCertificate(rawCert)
if err != nil {
return fmt.Errorf("failed parsing raw certificate %v: %v", name, err)
}
sn := fmt.Sprintf("%X", cert.SerialNumber)
// For compatibility with openssl we need an even length.
if len(sn)%2 == 1 {
sn = "0" + sn
}
// Date format: yymmddHHMMSSZ
// E|R|V<tab>Expiry<tab>[RevocationDate]<tab>Serial<tab>filename<tab>SubjectDN
var subject string
if strs := cert.Subject.Country; len(strs) == 1 {
subject += "/C=" + strs[0]
}
if strs := cert.Subject.Organization; len(strs) == 1 {
subject += "/O=" + strs[0]
}
if strs := cert.Subject.OrganizationalUnit; len(strs) == 1 {
subject += "/OU=" + strs[0]
}
if strs := cert.Subject.Locality; len(strs) == 1 {
subject += "/L=" + strs[0]
}
if strs := cert.Subject.Province; len(strs) == 1 {
subject += "/ST=" + strs[0]
}
subject += "/CN=" + cert.Subject.CommonName
n, err := fmt.Fprintf(f, "V\t%vZ\t\t%v\t%v.cert\t%v\n",
cert.NotAfter.UTC().Format("060102150405"),
sn,
name,
subject)
if err != nil {
return err
}
if n == 0 {
return fmt.Errorf("written 0 bytes in index file")
}
return nil
}
// Update updates the state of a given certificate in the index.txt.
func (l *Local) Update(caName string, sn *big.Int, st certificate.State) error {
f, err := os.OpenFile(filepath.Join(l.Root, caName, "index.txt"), os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
var state string
switch st {
case certificate.Valid:
state = "V"
case certificate.Revoked:
state = "R"
case certificate.Expired:
state = "E"
default:
return fmt.Errorf("unhandled certificate state: %v", st)
}
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
matches := indexRegexp.FindStringSubmatch(scanner.Text())
if len(matches) != 7 {
return fmt.Errorf("line [%v] is incorrectly formatted", scanner.Text())
}
matchedSerial := big.NewInt(0)
fmt.Sscanf(matches[4], "%X", matchedSerial)
if matchedSerial.Cmp(sn) == 0 {
if matches[1] == state {
return nil
}
lines = append(lines, fmt.Sprintf("%v\t%v\t%vZ\t%v\t%v\t%v",
state,
matches[2],
time.Now().UTC().Format("060102150405"),
matches[4],
matches[5],
matches[6]))
} else {
lines = append(lines, matches[0])
}
}
if err = f.Truncate(0); err != nil {
return err
}
if _, err = f.Seek(0, 0); err != nil {
return err
}
for _, line := range lines {
n, err := fmt.Fprintln(f, line)
if err != nil {
return fmt.Errorf("failed writing line [%v]: %v", line, err)
}
if n == 0 {
return fmt.Errorf("failed writing line [%v]: written 0 bytes", line)
}
}
return nil
}
// Revoked returns a list of revoked certificates.
func (l *Local) Revoked(caName string) ([]pkix.RevokedCertificate, error) {
index, err := os.Open(filepath.Join(l.Root, caName, "index.txt"))
if err != nil {
return nil, err
}
defer index.Close()
var revokedCerts []pkix.RevokedCertificate
scanner := bufio.NewScanner(index)
for scanner.Scan() {
matches := indexRegexp.FindStringSubmatch(scanner.Text())
if len(matches) != 7 {
return nil, fmt.Errorf("line [%v] is incorrectly formatted", scanner.Text())
}
if matches[1] != "R" {
continue
}
sn := big.NewInt(0)
fmt.Sscanf(matches[4], "%X", sn)
t, err := time.Parse("060102150405", strings.TrimSuffix(matches[3], "Z"))
if err != nil {
return nil, fmt.Errorf("failed parsing revocation time %v: %v", matches[3], err)
}
revokedCerts = append(revokedCerts, pkix.RevokedCertificate{
SerialNumber: sn,
RevocationTime: t,
})
}
return revokedCerts, nil
}
// InitCADir creates the basic structure of a CA subdirectory.
//
// |- crlnumber
// |- index.txt
// |- index.txt.attr
// |- serial
// |- certs/
// |- ca.cert
// |- name.cert
// |- keys/
// |- ca.key
// |- name.key
func InitCADir(path string) error {
if _, err := os.Stat(path); err == nil {
return nil
}
if err := os.MkdirAll(path, 0755); err != nil {
return fmt.Errorf("failed creating CA root directory %v: %v", path, err)
}
dirs := map[string]os.FileMode{
filepath.Join(path, LocalCrlsDir): 0700,
filepath.Join(path, LocalCertsDir): 0755,
filepath.Join(path, LocalKeysDir): 0700,
}
for d, m := range dirs {
if err := os.Mkdir(d, m); err != nil {
return fmt.Errorf("failed creating directory %v: %v", d, err)
}
}
files := []struct {
Name string
Content string
}{
{Name: "serial", Content: "01"},
{Name: "crlnumber", Content: "01"},
{Name: "index.txt", Content: ""},
{Name: "index.txt.attr", Content: "unique_subject = no"},
}
for _, f := range files {
if err := createFile(filepath.Join(path, f.Name), f.Content); err != nil {
return err
}
}
return nil
}
func createFile(path, content string) error {
fh, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed creating file %v: %v", path, err)
}
defer fh.Close()
if content == "" {
return nil
}
n, err := fmt.Fprintln(fh, content)
if err != nil {
return fmt.Errorf("failed wrinting %v in %v: %v", content, path, err)
}
if n == 0 {
return fmt.Errorf("failed writing %v in %v: 0 bytes written", content, path)
}
return nil
}