/
func.misc.go
266 lines (218 loc) · 7.73 KB
/
func.misc.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
package locksmith
import (
"bufio"
b64 "encoding/base64"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gosimple/slug"
"gopkg.in/yaml.v2"
)
// slugger slugs a string
func slugger(textToSlug string) string {
return slug.Make(textToSlug)
}
// currentValue returns the current count of the Counter
func (counter Counter) currentValue() int64 {
return counter.count
}
// increment increases an int/int64 value in a Counter
func (counter *Counter) increment() {
counter.count++
}
// readSerialNumberAsInt is a wrapper that converts the string serial number in a serial file to an int
func readSerialNumberAsInt(rootSlugPath string) int {
i, _ := strconv.Atoi(readSerialNumber(rootSlugPath))
return i
}
// readSerialNumberAsInt64 converts an int converted serial number to int64
func readSerialNumberAsInt64(rootSlugPath string) int64 {
return int64(readSerialNumberAsInt(rootSlugPath))
}
// readSerialNumber reads the ca.serial file out
func readSerialNumber(rootSlug string) string {
filePath, err := filepath.Abs(readConfig.Locksmith.PKIRoot + "/roots/" + rootSlug + "/ca.serial")
check(err)
file, err := os.Open(filePath)
check(err)
defer file.Close()
s := bufio.NewScanner(file)
var serial string
for s.Scan() {
serial = s.Text()
break
}
return serial
}
// readSerialNumberAsIntAbs is a wrapper that converts the string serial number in a serial file to an int
func readSerialNumberAsIntAbs(path string) int {
i, _ := strconv.Atoi(readSerialNumberAbs(path))
return i
}
// readSerialNumberAsInt64Abs converts an int converted serial number to int64
func readSerialNumberAsInt64Abs(path string) int64 {
return int64(readSerialNumberAsIntAbs(path))
}
// readSerialNumberAbs reads the ca.serial file out from an absolute path
func readSerialNumberAbs(path string) string {
file, err := os.Open(path)
check(err)
defer file.Close()
s := bufio.NewScanner(file)
var serial string
for s.Scan() {
serial = s.Text()
break
}
return serial
}
// IncreaseSerialNumber just updates a root CAs serial
func IncreaseSerialNumber(rootSlug string) (bool, error) {
serNum := readSerialNumberAsInt64(rootSlug)
//currentSerialNumString := readSerialNumber(rootSlug)
//logStdOut("currentSerialNumString: " + currentSerialNumString)
//currentSerialNumber, _ := strconv.Atoi(currentSerialNumString)
//log.Printf("i=%d, type: %T\n", currentSerialNumber, currentSerialNumber)
//serNum = int64(currentSerialNumber)
counter := Counter{serNum}
//log.Printf("i=%d, type: %T\n", counter.currentValue(), counter.currentValue())
counter.increment()
//log.Printf("i=%d, type: %T\n", counter.currentValue(), counter.currentValue())
rootSlugPath := readConfig.Locksmith.PKIRoot + "/roots/" + rootSlug
rootCACertSerialFilePath, err := filepath.Abs(rootSlugPath + "/ca.serial")
check(err)
// Update serialFile
serialFile, err := WriteFile(rootCACertSerialFilePath, fmt.Sprintf("%v", counter.currentValue()), 0600, true)
check(err)
//if serialFile {
//logStdOut("Updated serial file")
//}
return serialFile, err
}
// IncreaseSerialNumberAbs just updates a root CAs serial via absolute path to the serial file
func IncreaseSerialNumberAbs(path string) (bool, error) {
//logStdOut("incrementing " + path)
serNum := readSerialNumberAsInt64Abs(path)
counter := Counter{serNum}
counter.increment()
return WriteFile(path, fmt.Sprintf("%v", counter.currentValue()), 0600, true)
}
// bakeURIs converts URL strings to actual URI slices
func bakeURIs(uris []string) ([]*url.URL, error) {
actualURIs := []*url.URL{}
for _, s := range uris {
if err := isIA5String(s); err != nil {
return nil, errors.New("x509: SAN uniformResourceIdentifier is malformed")
}
u, err := url.Parse(s)
check(err)
actualURIs = append(actualURIs, u)
}
return actualURIs, nil
}
// NewConfig returns a new decoded Config struct
func NewConfig(configPath string) (*Config, error) {
// Create config structure
config := &Config{}
// Open config file
file, err := os.Open(configPath)
checkAndFail(err)
defer file.Close()
// Init new YAML decode
d := yaml.NewDecoder(file)
// Start YAML decoding from file
if err := d.Decode(&config); err != nil {
return nil, err
}
readConfig = config
return config, nil
}
// PreflightSetup just makes sure the stage is set
func PreflightSetup() {
// Create PKI Root directory
PKIRootPath, err := filepath.Abs(readConfig.Locksmith.PKIRoot)
checkAndFail(err)
CreateDirectory(PKIRootPath)
// Create PKI Root root directory
PKIRootRootsPath, err := filepath.Abs(readConfig.Locksmith.PKIRoot + "/roots")
checkAndFail(err)
CreateDirectory(PKIRootRootsPath)
// Create PKI Extra Keys directory
PKIKeysRootsPath, err := filepath.Abs(readConfig.Locksmith.PKIRoot + "/keystores")
checkAndFail(err)
CreateDirectory(PKIKeysRootsPath)
CreateDirectory(PKIKeysRootsPath + "/default")
logStdOut("Preflight complete!")
}
/*
// splitSlugToPath takes a slug string and splits it into the relative path
// eg converts "example-labs-root-certificate-authority/example-labs-ica/signing-ca" to "example-labs-root-certificate-authority/intermed-ca/example-labs-ica/intermed-ca/signing-ca/"
func splitSlugToPath(slug string) string {
splitPath := strings.Split(strings.TrimSuffix(strings.TrimPrefix(strings.ToLower(slug), "/"), "/"), "/")
var path string
for i, part := range splitPath {
path = path + slugger(part) + "/"
if i != (len(splitPath) - 1) {
path = path + "intermed-ca/"
}
}
return path
}
// splitCommonNamesToPath takes a CN string and splits it into the relative path while slugging
// eg, converts "Example Labs Root Certificate Authority/Example Labs ICA/Signing CA" to "example-labs-root-certificate-authority/intermed-ca/example-labs-ica/intermed-ca/signing-ca/"
func splitCommonNamesToPath(cnPath string) string {
splitPath := strings.Split(strings.TrimSuffix(strings.TrimPrefix(strings.ToLower(cnPath), "/"), "/"), "/")
var path string
for i, part := range splitPath {
path = path + slugger(part) + "/"
if i != (len(splitPath) - 1) {
path = path + "intermed-ca/"
}
}
return path
}
*/
// splitCACNChainToPath takes a CN string and splits it into the relative path while slugging
// eg, converts "Example Labs Root Certificate Authority/Example Labs ICA/Signing CA" to "example-labs-root-certificate-authority/intermed-ca/example-labs-ica/intermed-ca/signing-ca/"
// or converts "example-labs-root-certificate-authority/example-labs-ica/signing-ca" to "example-labs-root-certificate-authority/intermed-ca/example-labs-ica/intermed-ca/signing-ca/"
// you can even mix and match - the parts are slugged regardless of input and produce the same result
func splitCACNChainToPath(cnPath string) string {
splitPath := splitCAPath(cnPath)
var path string
for i, part := range splitPath {
path = path + slugger(part) + "/"
if i != (len(splitPath) - 1) {
path = path + "intermed-ca/"
}
}
return path
}
// splitCAPath splits a raw CA Path to its parts
func splitCAPath(cnPath string) []string {
return strings.Split(strings.TrimSuffix(strings.TrimPrefix(strings.ToLower(cnPath), "/"), "/"), "/")
}
// rmStrFromStrSlice removes a string from a string slice
func rmStrFromStrSlice(r string, s []string) []string {
for i, v := range s {
if v == r {
return append(s[:i], s[i+1:]...)
}
}
return s
}
// B64EncodeBytesToStr converts a byte slice to a Base64 Encoded String
func B64EncodeBytesToStr(input []byte) string {
return b64.StdEncoding.EncodeToString(input)
}
// B64DecodeBytesToBytes converts a Base64 byte slice to a Base64 Decoded Byte slice
func B64DecodeBytesToBytes(input []byte) ([]byte, error) {
return B64DecodeStrToBytes(string(input))
}
// B64DecodeStrToBytes converts a Base64 string to a Base64 Decoded Byte slice
func B64DecodeStrToBytes(input string) ([]byte, error) {
return b64.StdEncoding.DecodeString(input)
}