forked from pki-io/admin
/
runCA.go
304 lines (251 loc) · 9.12 KB
/
runCA.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
package main
import (
"fmt"
"github.com/docopt/docopt-go"
"github.com/pki-io/core/crypto"
"github.com/pki-io/core/fs"
"github.com/pki-io/core/x509"
"time"
)
func caNew(argv map[string]interface{}) (err error) {
name := ArgString(argv["<name>"], nil)
inTags := ArgString(argv["--tags"], nil)
caExpiry := ArgInt(argv["--ca-expiry"], 365)
certExpiry := ArgInt(argv["--cert-expiry"], 90)
dnLocality := ArgString(argv["--dn-l"], "")
dnState := ArgString(argv["--dn-st"], "")
dnOrg := ArgString(argv["--dn-o"], "")
dnOrgUnit := ArgString(argv["--dn-ou"], "")
dnCountry := ArgString(argv["--dn-c"], "")
dnStreet := ArgString(argv["--dn-street"], "")
dnPostal := ArgString(argv["--dn-postal"], "")
app := NewAdminApp()
app.Load()
ca, _ := x509.NewCA(nil)
ca.Data.Body.Name = name
ca.Data.Body.CAExpiry = caExpiry
ca.Data.Body.CertExpiry = certExpiry
if dnLocality != "" {
ca.Data.Body.DNScope.Locality = dnLocality
}
if dnState != "" {
ca.Data.Body.DNScope.Province = dnState
}
if dnOrg != "" {
ca.Data.Body.DNScope.Organization = dnOrg
}
if dnOrgUnit != "" {
ca.Data.Body.DNScope.OrganizationalUnit = dnOrgUnit
}
if dnCountry != "" {
ca.Data.Body.DNScope.Country = dnCountry
}
if dnStreet != "" {
ca.Data.Body.DNScope.StreetAddress = dnStreet
}
if dnPostal != "" {
ca.Data.Body.DNScope.PostalCode = dnPostal
}
ca.GenerateRoot()
logger.Info("Saving CA")
caContainer, err := app.entities.org.EncryptThenSignString(ca.Dump(), nil)
checkAppFatal("Could not encrypt CA: %s", err)
err = app.fs.api.Authenticate(app.entities.org.Data.Body.Id, "")
checkAppFatal("Could not authenticate to API as Org: %s", err)
err = app.fs.api.StorePrivate(ca.Data.Body.Id, caContainer.Dump())
checkAppFatal("Could not save CA: %s", err)
logger.Info("Updating index")
app.LoadOrgIndex()
app.index.org.AddCA(ca.Data.Body.Name, ca.Data.Body.Id)
app.index.org.AddCATags(ca.Data.Body.Id, ParseTags(inTags))
app.SaveOrgIndex()
return nil
}
func caList(argv map[string]interface{}) (err error) {
app := NewAdminApp()
app.Load()
app.LoadOrgIndex()
logger.Info("CAs:")
logger.Flush()
for name, id := range app.index.org.GetCAs() {
fmt.Printf("* %s %s\n", name, id)
}
return nil
}
func caShow(argv map[string]interface{}) (err error) {
name := ArgString(argv["<name>"], nil)
//exportFile := ArgString(argv["--export"], "")
private := ArgBool(argv["--private"], false)
app := NewAdminApp()
app.Load()
app.LoadOrgIndex()
// TODO - refactor
var caSerial string
for n, id := range app.index.org.GetCAs() {
if n == name {
caSerial = id
}
}
if len(caSerial) == 0 {
checkUserFatal("Could not find CA: %s", name)
}
ca := app.GetCA(caSerial)
fmt.Printf("Name: %s\n", ca.Data.Body.Name)
fmt.Printf("ID: %s\n", ca.Data.Body.Id)
fmt.Printf("CA expiry period: %d\n", ca.Data.Body.CAExpiry)
fmt.Printf("Cert expiry period: %d\n", ca.Data.Body.CertExpiry)
fmt.Printf("Key type: %s\n", ca.Data.Body.KeyType)
fmt.Printf("DN country: %s\n", ca.Data.Body.DNScope.Country)
fmt.Printf("DN organization: %s\n", ca.Data.Body.DNScope.Organization)
fmt.Printf("DN organizational unit: %s\n", ca.Data.Body.DNScope.OrganizationalUnit)
fmt.Printf("DN locality: %s\n", ca.Data.Body.DNScope.Locality)
fmt.Printf("DN province: %s\n", ca.Data.Body.DNScope.Province)
fmt.Printf("DN street address: %s\n", ca.Data.Body.DNScope.StreetAddress)
fmt.Printf("DN postal code: %s\n", ca.Data.Body.DNScope.PostalCode)
fmt.Printf("Certficate:\n%s\n", ca.Data.Body.Certificate)
if private {
fmt.Printf("Private key:\n%s\n", ca.Data.Body.PrivateKey)
}
return nil
}
func caDelete(argv map[string]interface{}) (err error) {
name := ArgString(argv["<name>"], nil)
reason := ArgString(argv["--confirm-delete"], nil)
app := NewAdminApp()
app.Load()
app.LoadOrgIndex()
logger.Infof("Deleting CA %s with reason: %s", name, reason)
logger.Info("Note: This does not revoke existing certificates signed by the CA")
caId, err := app.index.org.GetCA(name)
checkAppFatal("Could not get CA ID: %s", err)
err = app.fs.api.Authenticate(app.entities.org.Data.Body.Id, "")
checkAppFatal("Could not authenticate to API as Org: %s", err)
err = app.fs.api.DeletePrivate(caId)
checkAppFatal("Could not delete CA: %s", err)
err = app.index.org.RemoveCA(name)
checkAppFatal("Could not remove CA: %s", err)
app.SaveOrgIndex()
return nil
}
func caImport(argv map[string]interface{}) (err error) {
name := ArgString(argv["<name>"], nil)
inTags := ArgString(argv["--tags"], nil)
certFile := ArgString(argv["<cert>"], nil)
keyFile := ArgString(argv["<privateKey>"], "")
certExpiry := ArgInt(argv["--cert-expiry"], 90)
dnLocality := ArgString(argv["--dn-l"], "")
dnState := ArgString(argv["--dn-st"], "")
dnOrg := ArgString(argv["--dn-o"], "")
dnOrgUnit := ArgString(argv["--dn-ou"], "")
dnCountry := ArgString(argv["--dn-c"], "")
dnStreet := ArgString(argv["--dn-street"], "")
dnPostal := ArgString(argv["--dn-postal"], "")
logger.Infof("Importing %s as %s", certFile, name)
app := NewAdminApp()
app.Load()
ca, _ := x509.NewCA(nil)
ca.Data.Body.Name = name
if dnLocality != "" {
ca.Data.Body.DNScope.Locality = dnLocality
}
if dnState != "" {
ca.Data.Body.DNScope.Province = dnState
}
if dnOrg != "" {
ca.Data.Body.DNScope.Organization = dnOrg
}
if dnOrgUnit != "" {
ca.Data.Body.DNScope.OrganizationalUnit = dnOrgUnit
}
if dnCountry != "" {
ca.Data.Body.DNScope.Country = dnCountry
}
if dnStreet != "" {
ca.Data.Body.DNScope.StreetAddress = dnStreet
}
if dnPostal != "" {
ca.Data.Body.DNScope.PostalCode = dnPostal
}
ok, err := fs.Exists(certFile)
checkAppFatal("Could not check file existence for %s: %s", certFile, err)
if !ok {
checkUserFatal("File does not exist: %s", certFile)
}
certPem, err := fs.ReadFile(certFile)
cert, err := x509.PemDecodeX509Certificate([]byte(certPem))
checkUserFatal("Not a valid certificate PEM for %s: %s", certFile, err)
// TODO - consider converting cert back to pem to use for consistency
// We generate a random ID instead of using the serial number because we
// don't control the serial
ca.Data.Body.Id = NewID()
ca.Data.Body.Certificate = certPem
ca.Data.Body.CertExpiry = certExpiry
caExpiry := int(cert.NotAfter.Sub(cert.NotBefore) / (time.Hour * 24))
ca.Data.Body.CAExpiry = caExpiry
if keyFile != "" {
ok, err = fs.Exists(keyFile)
checkAppFatal("Could not check file existence for %s: %s", keyFile, err)
if !ok {
checkUserFatal("File does not exist: %s", keyFile)
}
keyPem, err := fs.ReadFile(keyFile)
key, err := crypto.PemDecodePrivate([]byte(keyPem))
checkUserFatal("Not a valid private key PEM for %s: %s", keyFile, err)
// TODO - consider converting key back to pem to use for consistency
keyType, err := crypto.GetKeyType(key)
checkUserFatal("Unknow private key file for %s: %s", keyFile, err)
ca.Data.Body.KeyType = string(keyType)
ca.Data.Body.PrivateKey = keyPem
}
logger.Info("Saving CA")
caContainer, err := app.entities.org.EncryptThenSignString(ca.Dump(), nil)
checkAppFatal("Could not encrypt CA: %s", err)
err = app.fs.api.Authenticate(app.entities.org.Data.Body.Id, "")
checkAppFatal("Could not authenticate to API as Org: %s", err)
err = app.fs.api.StorePrivate(ca.Data.Body.Id, caContainer.Dump())
checkAppFatal("Could not save CA: %s", err)
logger.Info("Updating index")
app.LoadOrgIndex()
app.index.org.AddCA(ca.Data.Body.Name, ca.Data.Body.Id)
app.index.org.AddCATags(ca.Data.Body.Id, ParseTags(inTags))
app.SaveOrgIndex()
return nil
}
func runCA(args []string) (err error) {
usage := `
Manages Certificate Authorities
Usage:
pki.io ca [--help]
pki.io ca new <name> --tags <tags> [--ca-expiry <days>] [--cert-expiry <days>] [--dn-l <locality>] [--dn-st <state>] [--dn-o <org>] [--dn-ou <orgUnit>] [--dn-c <country>] [--dn-street <street>] [--dn-postal <postalCode>]
pki.io ca list
pki.io ca show <name> [--export <file>] [--private]
pki.io ca delete <name> --confirm-delete <reason>
pki.io ca import <name> <cert> [<privateKey>] --tags <tags> [--ca-expiry <days>] [--cert-expiry <days>] [--dn-l <locality>] [--dn-st <state>] [--dn-o <org>] [--dn-ou <orgUnit>] [--dn-c <country>] [--dn-street <street>] [--dn-postal <postalCode>]
Options:
--tags <tags> List of comma-separated tags
--ca-expiry <days> Expiry period for CA in days [default: 365]
--cert-expiry <days> Expiry period for certs in day [default: 90]
--dn-l <locality> Locality for DN scope
--dn-st <state> State/province for DN scope
--dn-o <org> Organization for DN scope
--dn-ou <orgUnit> Organizational unit for DN scope
--dn-c <country> Country for DN scope
--dn-street <street> Street for DN scope
--dn-postal <postalCode> Postal code for DN scope
--confirm-delete <reason> Reason for deleting node
--private Show private data (e.g. keys)
`
argv, _ := docopt.Parse(usage, args, true, "", false)
if argv["new"].(bool) {
caNew(argv)
} else if argv["list"].(bool) {
caList(argv)
} else if argv["show"].(bool) {
caShow(argv)
} else if argv["delete"].(bool) {
caDelete(argv)
} else if argv["import"].(bool) {
caImport(argv)
}
return nil
}