-
Notifications
You must be signed in to change notification settings - Fork 61
/
validators.go
575 lines (487 loc) · 16.7 KB
/
validators.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
package main
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
"github.com/Venafi/vcert/v4/pkg/certificate"
)
// RevocationReasonOptions is an array of strings containing reasons for certificate revocation
var RevocationReasonOptions = []string{
"none",
"key-compromise",
"ca-compromise",
"affiliation-changed",
"superseded",
"cessation-of-operation",
}
func readData(commandName string) error {
if strings.HasPrefix(flags.distinguishedName, "file:") {
fileName := flags.distinguishedName[5:]
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return fmt.Errorf("Failed to read Certificate DN: %s", err)
}
flags.distinguishedName = strings.TrimSpace(string(bytes))
}
if strings.HasPrefix(flags.keyPassword, "file:") {
fileName := flags.keyPassword[5:]
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return fmt.Errorf("Failed to read password from file: %s", err)
}
flags.keyPassword = strings.TrimSpace(string(bytes))
}
var err error
if strings.HasPrefix(flags.thumbprint, "file:") {
certFileName := flags.thumbprint[5:]
flags.thumbprint, err = readThumbprintFromFile(certFileName)
if err != nil {
return fmt.Errorf("Failed to read certificate fingerprint: %s", err)
}
}
if err = readPasswordsFromInputFlags(commandName, &flags); err != nil {
return fmt.Errorf("Failed to read password from input: %s", err)
}
return nil
}
func validateCommonFlags(commandName string) error {
if flags.format != "" && flags.format != "pem" && flags.format != "json" && flags.format != "pkcs12" {
return fmt.Errorf("Unexpected output format: %s", flags.format)
}
if flags.file != "" && (flags.certFile != "" || flags.chainFile != "" || flags.keyFile != "") {
return fmt.Errorf("The '-file' option cannot be used used with any other -*-file flags. Either all data goes into one file or individual files must be specified using the appropriate flags")
}
csrOptionRegex := regexp.MustCompile(`(^file:).*$|^local$|^service$|^$`)
if !csrOptionRegex.MatchString(flags.csrOption) {
return fmt.Errorf("unexpected --csr option provided: %s; specify one of the following options: %s, %s, or %s", flags.csrOption, "'file:<filename>'", "'local'", "'service'")
}
csrOptFlagResults := csrOptionRegex.FindStringSubmatch(flags.csrOption)
if csrOptFlagResults[1] != "" && (flags.keyTypeString != "" || flags.keyCurveString != "" || flags.keySize > 0) {
return fmt.Errorf("the '--keytype','--keycurve' and '--key-size' options cannot be used when '--csr file:' option is provided")
}
switch flags.keyTypeString {
case "rsa":
kt := certificate.KeyTypeRSA
flags.keyType = &kt
case "ecdsa":
kt := certificate.KeyTypeECDSA
flags.keyType = &kt
case "":
default:
return fmt.Errorf("unknown key type: %s", flags.keyTypeString)
}
switch strings.ToLower(flags.keyCurveString) {
case "p256":
flags.keyCurve = certificate.EllipticCurveP256
case "p384":
flags.keyCurve = certificate.EllipticCurveP384
case "p521":
flags.keyCurve = certificate.EllipticCurveP521
case "":
default:
return fmt.Errorf("unknown EC key curve: %s", flags.keyTypeString)
}
return nil
}
func validateConnectionFlags(commandName string) error {
if flags.config != "" {
if flags.apiKey != "" ||
flags.tppUser != "" ||
flags.tppPassword != "" ||
flags.tppToken != "" ||
flags.url != "" ||
flags.testMode {
return fmt.Errorf("connection details cannot be specified with flags when -config is used")
}
} else {
if flags.profile != "" {
return fmt.Errorf("-profile option cannot be used without -config option")
}
tppToken := flags.tppToken
if tppToken == "" {
tppToken = getPropertyFromEnvironment(vCertToken)
}
if flags.testMode {
return nil
}
if flags.tppUser == "" && tppToken == "" {
// should be SaaS endpoint
if flags.apiKey == "" && getPropertyFromEnvironment(vCertApiKey) == "" {
return fmt.Errorf("An API key is required for communicating with Venafi Cloud")
}
} else {
// should be TPP service
if flags.url == "" && getPropertyFromEnvironment(vCertURL) == "" {
return fmt.Errorf("missing -u (URL) parameter")
}
if flags.noPrompt && flags.tppPassword == "" && tppToken == "" {
return fmt.Errorf("An access token or password is required for communicating with Trust Protection Platform")
}
// mutual TLS with TPP service
if flags.clientP12 == "" && flags.clientP12PW != "" {
return fmt.Errorf("-client-pkcs12-pw can only be specified in combination with -client-pkcs12")
}
}
}
return nil
}
func validatePKCS12Flags(commandName string) error {
if flags.format == "pkcs12" {
if commandName == commandEnrollName {
if flags.file == "" && flags.csrOption != "service" {
return fmt.Errorf("PKCS#12 format can only be used if all objects are written to one file (see -file option)")
}
if flags.certFile != "" || flags.chainFile != "" || flags.keyFile != "" {
return fmt.Errorf("The '-file' cannot be used used with any other -*-file flags. Either all data goes into one file or individual files must be specified using the appropriate flags")
}
if strings.Index(flags.csrOption, "file:") == 0 {
return fmt.Errorf(`PKCS#12 format is not allowed for the enroll or renew actions when -csr is "file"`)
}
if (flags.csrOption == "" || flags.csrOption == "local") && flags.noPickup {
return fmt.Errorf(`PKCS#12 format is not allowed for the enroll or renew actions when -csr is "local" and -no-pickup is specified`)
}
} else {
if flags.file == "" { // todo: for enroll it also checks flags.csrOption != "service"
return fmt.Errorf("PKCS#12 format can only be used if all objects are written to one file (see -file option)")
}
}
if flags.certFile != "" || flags.chainFile != "" || flags.keyFile != "" {
return fmt.Errorf("The '-file' cannot be used used with any other -*-file flags. Either all data goes into one file or individual files must be specified using the appropriate flags")
}
if strings.HasPrefix(flags.csrOption, "file:") {
return fmt.Errorf(`PKCS#12 format is not allowed for the enroll or renew actions when -csr is "file"`)
}
if (flags.csrOption == "" || flags.csrOption == "local") && flags.noPickup {
return fmt.Errorf(`PKCS#12 format is not allowed for the enroll or renew actions when -csr is "local" and -no-pickup is specified`)
}
}
return nil
}
func validateEnrollFlags(commandName string) error {
err := validateConnectionFlags(commandName)
if err != nil {
return err
}
err = validateCommonFlags(commandName)
if err != nil {
return err
}
err = readData(commandName)
if err != nil {
return err
}
if strings.Index(flags.csrOption, "file:") == 0 {
if flags.commonName != "" {
return fmt.Errorf("The '-cn' option cannot be used in -csr file: provided mode")
}
} else {
if flags.commonName == "" {
return fmt.Errorf("A Common Name is required for enrollment")
}
}
if flags.chainOption == "ignore" && flags.chainFile != "" {
return fmt.Errorf("The `-chain ignore` option cannot be used with -chain-file option")
}
apiKey := flags.apiKey
if apiKey == "" {
apiKey = getPropertyFromEnvironment(vCertApiKey)
}
if !flags.testMode && flags.config == "" {
tppToken := flags.tppToken
if tppToken == "" {
tppToken = getPropertyFromEnvironment(vCertToken)
}
zone := flags.zone
if zone == "" {
zone = getPropertyFromEnvironment(vCertZone)
}
if flags.tppUser == "" && tppToken == "" {
// should be SaaS endpoint
if apiKey == "" {
return fmt.Errorf("An API key is required for enrollment with Venafi Cloud")
}
if zone == "" {
return fmt.Errorf("A zone is required for requesting a certificate from Venafi Cloud")
}
} else {
// should be TPP service
if flags.tppUser == "" && tppToken == "" {
return fmt.Errorf("An access token or username is required for communicating with Trust Protection Platform")
}
if flags.noPrompt && flags.tppPassword == "" && tppToken == "" {
return fmt.Errorf("An access token or password is required for communicating with Trust Protection Platform")
}
if zone == "" {
return fmt.Errorf("A zone is required for requesting a certificate from Trust Protection Platform")
}
// mutual TLS with TPP service
if flags.clientP12 == "" && flags.clientP12PW != "" {
return fmt.Errorf("-client-pkcs12-pw can only be specified in combination with -client-pkcs12")
}
}
if flags.validDays != "" {
valid := validateValidDaysFlag(commandName)
if !valid {
return fmt.Errorf("--valid-days is set but, it have an invalid format/data")
}
}
}
if flags.csrOption == "file" && flags.keyFile != "" { // Do not specify -key-file with -csr file as VCert cannot access the private key
return fmt.Errorf("-key-file cannot be used with -csr file as VCert cannot access the private key")
}
if flags.csrOption == "service" && (!flags.noPickup) { // Key password is required here
if flags.noPrompt && len(flags.keyPassword) == 0 {
return fmt.Errorf("-key-password cannot be empty in -csr service mode unless -no-pickup specified")
}
}
err = validatePKCS12Flags(commandName)
if err != nil {
return err
}
if flags.tppUser != "" || flags.tppPassword != "" {
logf("Warning: User\\Password authentication is deprecated, please use access token instead.")
}
if flags.tlsAddress != "" && flags.instance == "" {
return fmt.Errorf("--tls-address cannot be used without --instance")
}
if (flags.tlsAddress != "" || flags.instance != "") && apiKey != "" {
return fmt.Errorf("--instance and --tls-address are not applicable to Venafi Cloud")
}
return nil
}
func validateValidDaysFlag(cn string) bool {
if cn != "enroll" {
return false
}
if flags.validDays != "" {
validDays := flags.validDays
var regex = regexp.MustCompile("[1-9]+[0-9]*(#[DdEeMm])?")
return regex.MatchString(validDays)
}
return true
}
func validateGetcredFlags1(commandName string) error {
var err error
tppTokenS := flags.tppToken
if tppTokenS == "" {
tppTokenS = getPropertyFromEnvironment(vCertToken)
}
if flags.config != "" {
if flags.apiKey != "" ||
flags.tppUser != "" ||
flags.tppPassword != "" ||
tppTokenS != "" ||
flags.url != "" ||
flags.testMode {
return fmt.Errorf("connection details cannot be specified with flags or environment variables when -config is used")
}
} else {
if flags.profile != "" {
return fmt.Errorf("-profile option cannot be used without -config option")
}
if flags.testMode {
return fmt.Errorf("There is no test mode for getcred command")
}
if flags.tppUser == "" && tppTokenS == "" && flags.clientP12 == "" {
return fmt.Errorf("either -username, -p12-file, or -t must be specified")
}
if flags.url == "" && getPropertyFromEnvironment(vCertURL) == "" {
return fmt.Errorf("missing -u (URL) parameter")
}
if flags.noPrompt && flags.tppPassword == "" && tppTokenS == "" {
return fmt.Errorf("An access token or password is required for communicating with Trust Protection Platform")
}
// mutual TLS with TPP service
if flags.clientP12 == "" && flags.clientP12PW != "" {
return fmt.Errorf("-client-pkcs12-pw can only be specified in combination with -client-pkcs12")
}
}
err = validateCommonFlags(commandName)
if err != nil {
return err
}
err = readData(commandName)
if err != nil {
return err
}
if tppTokenS == "" && flags.tppUser == "" && flags.clientP12 == "" {
return fmt.Errorf("either -username, -p12-file, or -t must be specified")
}
err = validatePKCS12Flags(commandName)
if err != nil {
return err
}
return nil
}
func validateGenerateFlags1(commandName string) error {
err := validateCommonFlags(commandName)
if err != nil {
return err
}
err = readData(commandName)
if err != nil {
return err
}
if flags.commonName == "" && len(flags.dnsSans) == 0 {
return fmt.Errorf("A Common Name (cn) or Subject Alternative Name: DNS (san-dns) value is required")
}
return nil
}
func validateRenewFlags1(commandName string) error {
err := validateConnectionFlags(commandName)
if err != nil {
return err
}
err = validateCommonFlags(commandName)
if err != nil {
return err
}
err = readData(commandName)
if err != nil {
return err
}
if flags.distinguishedName == "" && flags.thumbprint == "" {
return fmt.Errorf("-id or -thumbprint required to identify the certificate to renew")
}
if flags.distinguishedName != "" && flags.thumbprint != "" {
return fmt.Errorf("-id and -thumbprint cannot be used at the same time")
}
if flags.chainOption == "ignore" && flags.chainFile != "" {
return fmt.Errorf("The `-chain ignore` option cannot be used with -chain-file option")
}
if flags.csrOption == "service" {
if !(flags.noPickup) && flags.noPrompt && len(flags.keyPassword) == 0 && (flags.tppUser != "" || flags.tppToken != "") {
return fmt.Errorf("-key-password cannot be empty in -csr service mode for TPP unless -no-pickup specified")
}
if flags.commonName != "" ||
flags.country != "" ||
flags.org != "" ||
flags.state != "" ||
flags.keySize != 0 ||
len(flags.orgUnits) > 0 ||
len(flags.dnsSans) > 0 ||
len(flags.emailSans) > 0 ||
len(flags.ipSans) > 0 {
return fmt.Errorf("Renewal with -csr=service does not allow options: " +
"-cn, -c, -o, -ou, -l, -st, -san-*, -key-type, -key-size")
}
}
if strings.HasPrefix(flags.csrOption, "file:") {
if flags.commonName != "" ||
flags.country != "" ||
flags.org != "" ||
flags.state != "" ||
flags.keySize != 0 ||
len(flags.orgUnits) > 0 ||
len(flags.dnsSans) > 0 ||
len(flags.emailSans) > 0 ||
len(flags.ipSans) > 0 {
return fmt.Errorf("Renewal with -csr file:CSR.pem does not allow options: " +
"-cn, -c, -o, -ou, -l, -st, -san-*, -key-type, -key-size")
}
}
if flags.csrOption == "" || flags.csrOption == "local" {
if flags.commonName != "" ||
flags.country != "" ||
flags.org != "" ||
flags.state != "" ||
flags.locality != "" ||
len(flags.orgUnits) > 0 {
return fmt.Errorf("Renewal does not allow options: -cn, -c, -o, -ou, -l, -st")
}
}
err = validatePKCS12Flags(commandName)
if err != nil {
return err
}
return nil
}
func validatePickupFlags1(commandName string) error {
err := validateConnectionFlags(commandName)
if err != nil {
return err
}
err = validateCommonFlags(commandName)
if err != nil {
return err
}
err = readData(commandName)
if err != nil {
return err
}
if flags.pickupID == "" && flags.pickupIDFile == "" {
return fmt.Errorf("A Pickup ID is required to pickup a certificate provided by -pickup-id OR -pickup-id-file options")
}
if flags.pickupID != "" && flags.pickupIDFile != "" {
return fmt.Errorf("Both -pickup-id and -pickup-id-file options cannot be specified at the same time")
}
err = validatePKCS12Flags(commandName)
if err != nil {
return err
}
return nil
}
func validateRevokeFlags1(commandName string) error {
err := validateConnectionFlags(commandName)
if err != nil {
return err
}
err = validateCommonFlags(commandName)
if err != nil {
return err
}
err = readData(commandName)
if err != nil {
return err
}
if flags.distinguishedName == "" && flags.thumbprint == "" {
return fmt.Errorf("Certificate DN or Thumbprint is required to revoke the certificate")
}
if flags.distinguishedName != "" && flags.thumbprint != "" {
return fmt.Errorf("Either -id or -thumbprint can be used")
}
if flags.revocationReason != "" {
isValidReason := func(reason string) bool {
for _, v := range RevocationReasonOptions {
if v == reason {
return true
}
}
return false
}(flags.revocationReason)
if !isValidReason {
return fmt.Errorf("%s is not valid revocation reason. it should be one of %v", flags.revocationReason, RevocationReasonOptions)
}
}
return nil
}
func validateOverWritingEnviromentVariables() {
colorYellow := "\033[33m"
colorReset := "\033[0m"
if getPropertyFromEnvironment(vCertURL) != "" {
if flags.url != "" {
logger.Println(colorYellow, "Warning Command line parameter -u has overridden environment variable VCERT_URL", colorReset)
}
}
if getPropertyFromEnvironment(vCertZone) != "" {
if flags.zone != "" {
logger.Println(colorYellow, "Warning: Command line parameter -z has overridden environment variable VCERT_ZONE", colorReset)
}
}
if getPropertyFromEnvironment(vCertToken) != "" {
if flags.tppToken != "" {
logger.Println(colorYellow, "Warning: Command line parameter -t has overridden environment variable VCERT_TOKEN", colorReset)
}
}
if getPropertyFromEnvironment(vCertApiKey) != "" {
if flags.apiKey != "" {
logger.Println(colorYellow, "Warning: Command line parameter -k has overridden environment variable VCERT_APIKEY", colorReset)
}
}
if getPropertyFromEnvironment(vCertTrustBundle) != "" {
if flags.trustBundle != "" {
logger.Println(colorYellow, "Warning: Command line parameter --trust-bundle has overridden environment variable VCERT_TRUST_BUNDLE", colorReset)
}
}
}