-
Notifications
You must be signed in to change notification settings - Fork 61
/
policyUtils.go
1595 lines (1306 loc) · 44.5 KB
/
policyUtils.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package policy
import (
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"gopkg.in/yaml.v2"
)
// TppKeyType represents the Private Key types supported by TPP
var TppKeyType = []string{"RSA", "ECDSA"}
// TppRsaKeySize represents the Key sizes supported by TPP for RSA Private Keys
var TppRsaKeySize = []int{512, 1024, 2048, 3072, 4096}
// CloudRsaKeySize represents the Key sizes supported by VaaS for RSA Private Keys
var CloudRsaKeySize = []int{1024, 2048, 3072, 4096}
// TppEllipticCurves represents the curves supported by TPP for ECDSA Private Keys
var TppEllipticCurves = []string{"P256", "P384", "P521"}
func GetFileType(f string) string {
extension := filepath.Ext(f)
//As yaml extension could be yaml or yml then convert it to just .yaml
extension = strings.ToLower(extension)
if extension == ".yml" {
extension = YamlExtension
}
return extension
}
func GetParent(p string) string {
lastIndex := strings.LastIndex(p, "\\")
parentPath := p[:lastIndex]
return parentPath
}
func ValidateTppPolicySpecification(ps *PolicySpecification) error {
if ps.Policy != nil {
err := validatePolicySubject(ps)
if err != nil {
return err
}
err = validateKeyPair(ps)
if err != nil {
return err
}
}
err := validateDefaultSubject(ps)
if err != nil {
return err
}
err = validateDefaultKeyPairWithPolicySubject(ps)
if err != nil {
return err
}
err = validateDefaultKeyPair(ps)
if err != nil {
return err
}
if ps.Default != nil && ps.Default.AutoInstalled != nil && ps.Policy != nil && ps.Policy.AutoInstalled != nil {
if *(ps.Default.AutoInstalled) != *(ps.Policy.AutoInstalled) {
return fmt.Errorf("default autoInstalled attribute value doesn't match with policy's autoInstalled attribute value")
}
}
return nil
}
func validatePolicySubject(ps *PolicySpecification) error {
if ps.Policy.Subject == nil {
return nil
}
subject := ps.Policy.Subject
if len(subject.Orgs) > 1 {
return fmt.Errorf("attribute orgs has more than one value")
}
if len(subject.Localities) > 1 {
return fmt.Errorf("attribute localities has more than one value")
}
if len(subject.States) > 1 {
return fmt.Errorf("attribute states has more than one value")
}
if len(subject.Countries) > 1 {
return fmt.Errorf("attribute countries has more than one value")
}
if len(subject.Countries) > 0 {
if len(subject.Countries[0]) != 2 {
return fmt.Errorf("number of country's characters, doesn't match to two characters")
}
}
return nil
}
func validateKeyPair(ps *PolicySpecification) error {
if ps.Policy.KeyPair == nil {
return nil
}
keyPair := ps.Policy.KeyPair
//validate algorithm
if len(keyPair.KeyTypes) > 1 {
return fmt.Errorf("attribute keyTypes has more than one value")
}
if len(keyPair.KeyTypes) > 0 && !existStringInArray(keyPair.KeyTypes, TppKeyType) {
return fmt.Errorf("specified keyTypes doesn't match with the supported ones")
}
//validate key bit strength
if len(keyPair.RsaKeySizes) > 1 {
return fmt.Errorf("attribute rsaKeySizes has more than one value")
}
if len(keyPair.RsaKeySizes) > 0 && !existIntInArray(keyPair.RsaKeySizes, TppRsaKeySize) {
return fmt.Errorf("specified rsaKeySizes doesn't match with the supported ones")
}
//validate elliptic curve
if len(keyPair.EllipticCurves) > 1 {
return fmt.Errorf("attribute ellipticCurves has more than one value")
}
if len(keyPair.EllipticCurves) > 0 && !existStringInArray(keyPair.EllipticCurves, TppEllipticCurves) {
return fmt.Errorf("specified ellipticCurves doesn't match with the supported ones")
}
return nil
}
func existStringInArray(userValue []string, supportedValues []string) bool {
for _, uv := range userValue {
match := false
for _, sv := range supportedValues {
if uv == sv {
match = true
}
}
if !match {
return false
}
}
return true
}
func existIntInArray(userValue []int, supportedValues []int) bool {
for _, uv := range userValue {
match := false
for _, sv := range supportedValues {
if uv == sv {
match = true
}
}
if !match {
return false
}
}
return true
}
func validateDefaultSubject(ps *PolicySpecification) error {
if ps.Default != nil && ps.Default.Subject != nil {
defaultSubject := ps.Default.Subject
if ps.Policy != nil && ps.Policy.Subject != nil {
policySubject := ps.Policy.Subject
if policySubject.Orgs != nil && policySubject.Orgs[0] != "" && defaultSubject.Org != nil && *(defaultSubject.Org) != "" {
if policySubject.Orgs[0] != *(defaultSubject.Org) {
return fmt.Errorf("policy default org doesn't match with policy's orgs value")
}
}
if len(policySubject.OrgUnits) > 0 && len(defaultSubject.OrgUnits) > 0 {
if !existStringInArray(defaultSubject.OrgUnits, policySubject.OrgUnits) {
return fmt.Errorf("policy default orgUnits doesn't match with policy's orgUnits value")
}
}
if policySubject.Localities != nil && policySubject.Localities[0] != "" && defaultSubject.Locality != nil && *(defaultSubject.Locality) != "" {
if policySubject.Localities[0] != *(defaultSubject.Locality) {
return fmt.Errorf("policy default locality doesn't match with policy's localities value")
}
}
if policySubject.States != nil && policySubject.States[0] != "" && defaultSubject.State != nil && *(defaultSubject.State) != "" {
if policySubject.States[0] != *(defaultSubject.State) {
return fmt.Errorf("policy default state doesn't match with policy's states value")
}
}
if policySubject.Countries != nil && policySubject.Countries[0] != "" && defaultSubject.Country != nil && *(defaultSubject.Country) != "" {
if policySubject.Countries[0] != *(defaultSubject.Country) {
return fmt.Errorf("policy default country doesn't match with policy's countries value")
}
}
if defaultSubject.Country != nil && *(defaultSubject.Country) != "" {
if len(*(defaultSubject.Country)) != 2 {
return fmt.Errorf("number of defualt country's characters, doesn't match to two characters")
}
}
} else {
//there is nothing to validate
return nil
}
}
return nil
}
func validateDefaultKeyPairWithPolicySubject(ps *PolicySpecification) error {
if ps.Default == nil || ps.Default.KeyPair == nil || ps.Policy == nil || ps.Policy.KeyPair == nil {
return nil
}
defaultKeyPair := ps.Default.KeyPair
policyKeyPair := ps.Policy.KeyPair
if policyKeyPair.KeyTypes != nil && policyKeyPair.KeyTypes[0] != "" && defaultKeyPair.KeyType != nil && *(defaultKeyPair.KeyType) != "" {
if policyKeyPair.KeyTypes[0] != *(defaultKeyPair.KeyType) {
return fmt.Errorf("policy default keyType doesn't match with policy's keyType value")
}
}
if policyKeyPair.RsaKeySizes != nil && policyKeyPair.RsaKeySizes[0] != 0 && defaultKeyPair.RsaKeySize != nil && *(defaultKeyPair.RsaKeySize) != 0 {
if policyKeyPair.RsaKeySizes[0] != *(defaultKeyPair.RsaKeySize) {
return fmt.Errorf("policy default rsaKeySize doesn't match with policy's rsaKeySize value")
}
}
if policyKeyPair.EllipticCurves != nil && policyKeyPair.EllipticCurves[0] != "" && defaultKeyPair.EllipticCurve != nil && *(defaultKeyPair.EllipticCurve) != "" {
if policyKeyPair.EllipticCurves[0] != *(defaultKeyPair.EllipticCurve) {
return fmt.Errorf("policy default ellipticCurve doesn't match with policy's ellipticCurve value")
}
}
if policyKeyPair.ServiceGenerated != nil && defaultKeyPair.ServiceGenerated != nil {
if *(policyKeyPair.ServiceGenerated) != *(defaultKeyPair.ServiceGenerated) {
return fmt.Errorf("policy default serviceGenerated generated doesn't match with policy's serviceGenerated value")
}
}
return nil
}
func validateDefaultKeyPair(ps *PolicySpecification) error {
if ps.Default == nil {
return nil
}
if ps.Default.KeyPair == nil {
return nil
}
keyPair := ps.Default.KeyPair
if keyPair.KeyType != nil && *(keyPair.KeyType) != "" && !existStringInArray([]string{*(keyPair.KeyType)}, TppKeyType) {
return fmt.Errorf("specified default keyType doesn't match with the supported ones")
}
//validate key bit strength
if keyPair.RsaKeySize != nil && *(keyPair.RsaKeySize) > 0 && !existIntInArray([]int{*(keyPair.RsaKeySize)}, TppRsaKeySize) {
return fmt.Errorf("specified default rsaKeySize doesn't match with the supported ones")
}
//validate elliptic curve
if keyPair.EllipticCurve != nil && *(keyPair.EllipticCurve) != "" && !existStringInArray([]string{*(keyPair.EllipticCurve)}, TppEllipticCurves) {
return fmt.Errorf("specified default ellipticCurve doesn't match with the supported ones")
}
return nil
}
func BuildTppPolicy(ps *PolicySpecification) TppPolicy {
/*
"owners": string[], (permissions only) prefixed name/universal
"userAccess": string, (permissions) prefixed name/universal
}
*/
var tppPolicy TppPolicy
tppPolicy.Contact = ps.Users
tppPolicy.Approver = ps.Approvers
//policy attributes
if ps.Policy != nil {
tppPolicy.DomainSuffixWhitelist = ps.Policy.Domains
}
if ps.Policy != nil && ps.Policy.WildcardAllowed != nil {
if *(ps.Policy.WildcardAllowed) { //this is true so we revert it to false(0)
intValZero := 0
tppPolicy.ProhibitWildcard = &intValZero
} else {
intValOne := 1
tppPolicy.ProhibitWildcard = &intValOne
}
}
if ps.Policy != nil && ps.Policy.CertificateAuthority != nil {
tppPolicy.CertificateAuthority = ps.Policy.CertificateAuthority
}
managementType := TppManagementTypeEnrollment
if ps.Policy != nil && ps.Policy.AutoInstalled != nil {
if *(ps.Policy.AutoInstalled) {
managementType = TppManagementTypeProvisioning
}
tppPolicy.ManagementType = createLockedAttribute(managementType, true)
} else if ps.Default != nil && ps.Default.AutoInstalled != nil {
if *(ps.Default.AutoInstalled) {
managementType = TppManagementTypeProvisioning
}
tppPolicy.ManagementType = createLockedAttribute(managementType, false)
}
//policy subject attributes
if ps.Policy != nil && ps.Policy.Subject != nil && len(ps.Policy.Subject.Orgs) > 0 && ps.Policy.Subject.Orgs[0] != "" {
tppPolicy.Organization = createLockedAttribute(ps.Policy.Subject.Orgs[0], true)
} else if ps.Default != nil && ps.Default.Subject != nil && *(ps.Default.Subject.Org) != "" {
tppPolicy.Organization = createLockedAttribute(*(ps.Default.Subject.Org), false)
}
if ps.Policy != nil && ps.Policy.Subject != nil && len(ps.Policy.Subject.OrgUnits) > 0 && ps.Policy.Subject.OrgUnits[0] != "" {
tppPolicy.OrganizationalUnit = createLockedArrayAttribute(ps.Policy.Subject.OrgUnits, true)
} else if ps.Default != nil && ps.Default.Subject != nil && len(ps.Default.Subject.OrgUnits) > 0 && ps.Default.Subject.OrgUnits[0] != "" {
tppPolicy.OrganizationalUnit = createLockedArrayAttribute(ps.Default.Subject.OrgUnits, false)
}
if ps.Policy != nil && ps.Policy.Subject != nil && len(ps.Policy.Subject.Localities) > 0 && ps.Policy.Subject.Localities[0] != "" {
tppPolicy.City = createLockedAttribute(ps.Policy.Subject.Localities[0], true)
} else if ps.Default != nil && ps.Default.Subject != nil && (ps.Default.Subject.Locality != nil) && (*(ps.Default.Subject.Locality) != "") {
tppPolicy.City = createLockedAttribute(*(ps.Default.Subject.Locality), false)
}
if ps.Policy != nil && ps.Policy.Subject != nil && len(ps.Policy.Subject.States) > 0 && ps.Policy.Subject.States[0] != "" {
tppPolicy.State = createLockedAttribute(ps.Policy.Subject.States[0], true)
} else if ps.Default != nil && ps.Default.Subject != nil && (ps.Default.Subject.State != nil) && (*(ps.Default.Subject.State) != "") {
tppPolicy.State = createLockedAttribute(*(ps.Default.Subject.State), false)
}
if ps.Policy != nil && ps.Policy.Subject != nil && len(ps.Policy.Subject.Countries) > 0 && ps.Policy.Subject.Countries[0] != "" {
tppPolicy.Country = createLockedAttribute(ps.Policy.Subject.Countries[0], true)
} else if ps.Default != nil && ps.Default.Subject != nil && (ps.Default.Subject.Country != nil) && (*(ps.Default.Subject.Country) != "") {
tppPolicy.Country = createLockedAttribute(*(ps.Default.Subject.Country), false)
}
if ps.Policy != nil && ps.Policy.KeyPair != nil && len(ps.Policy.KeyPair.KeyTypes) > 0 && ps.Policy.KeyPair.KeyTypes[0] != "" {
tppPolicy.KeyAlgorithm = createLockedAttribute(ps.Policy.KeyPair.KeyTypes[0], true)
} else if ps.Default != nil && ps.Default.KeyPair != nil && (ps.Default.KeyPair.KeyType != nil) && (*(ps.Default.KeyPair.KeyType) != "") {
tppPolicy.KeyAlgorithm = createLockedAttribute(*(ps.Default.KeyPair.KeyType), false)
}
if ps.Policy != nil && ps.Policy.KeyPair != nil && len(ps.Policy.KeyPair.RsaKeySizes) > 0 && ps.Policy.KeyPair.RsaKeySizes[0] != 0 {
rsaKey := ps.Policy.KeyPair.RsaKeySizes[0]
tppPolicy.KeyBitStrength = createLockedAttribute(strconv.Itoa(rsaKey), true)
} else if ps.Default != nil && ps.Default.KeyPair != nil && (ps.Default.KeyPair.RsaKeySize != nil) && *(ps.Default.KeyPair.RsaKeySize) != 0 {
tppPolicy.KeyBitStrength = createLockedAttribute(strconv.Itoa(*(ps.Default.KeyPair.RsaKeySize)), false)
}
if ps.Policy != nil && ps.Policy.KeyPair != nil && len(ps.Policy.KeyPair.EllipticCurves) > 0 && ps.Policy.KeyPair.EllipticCurves[0] != "" {
tppPolicy.EllipticCurve = createLockedAttribute(ps.Policy.KeyPair.EllipticCurves[0], true)
} else if ps.Default != nil && ps.Default.KeyPair != nil && (ps.Default.KeyPair.EllipticCurve != nil) && (*(ps.Default.KeyPair.EllipticCurve) != "") {
tppPolicy.EllipticCurve = createLockedAttribute(*(ps.Default.KeyPair.EllipticCurve), false)
}
if ps.Policy != nil && ps.Policy.KeyPair != nil && ps.Policy.KeyPair.ServiceGenerated != nil {
strVal := "1"
if *(ps.Policy.KeyPair.ServiceGenerated) {
strVal = "0"
}
tppPolicy.ManualCsr = createLockedAttribute(strVal, true)
} else if ps.Default != nil && ps.Default.KeyPair != nil && (ps.Default.KeyPair.ServiceGenerated != nil) {
strVal := "1"
if *(ps.Default.KeyPair.ServiceGenerated) {
strVal = "0"
}
tppPolicy.ManualCsr = createLockedAttribute(strVal, false)
}
if ps.Policy != nil && ps.Policy.KeyPair != nil && ps.Policy.KeyPair.ReuseAllowed != nil {
var intVal int
if *(ps.Policy.KeyPair.ReuseAllowed) {
intVal = 1
} else {
intVal = 0
}
tppPolicy.AllowPrivateKeyReuse = &intVal
tppPolicy.WantRenewal = &intVal
}
if ps.Policy != nil && ps.Policy.SubjectAltNames != nil {
prohibitedSANType := getProhibitedSanTypes(*(ps.Policy.SubjectAltNames))
if prohibitedSANType != nil {
tppPolicy.ProhibitedSANType = prohibitedSANType
}
}
return tppPolicy
}
func createLockedAttribute(value string, locked bool) *LockedAttribute {
lockedAtr := LockedAttribute{
Value: value,
Locked: locked,
}
return &lockedAtr
}
func createLockedArrayAttribute(value []string, locked bool) *LockedArrayAttribute {
lockedAtr := LockedArrayAttribute{
Value: value,
Locked: locked,
}
return &lockedAtr
}
func getProhibitedSanTypes(sa SubjectAltNames) []string {
var prohibitedSanTypes []string
if (sa.DnsAllowed != nil) && !*(sa.DnsAllowed) {
prohibitedSanTypes = append(prohibitedSanTypes, "DNS")
}
if (sa.IpAllowed != nil) && !*(sa.IpAllowed) {
prohibitedSanTypes = append(prohibitedSanTypes, "IP")
}
if (sa.EmailAllowed != nil) && !*(sa.EmailAllowed) {
prohibitedSanTypes = append(prohibitedSanTypes, "Email")
}
if (sa.UriAllowed != nil) && !*(sa.UriAllowed) {
prohibitedSanTypes = append(prohibitedSanTypes, "URI")
}
if (sa.UpnAllowed != nil) && !*(sa.UpnAllowed) {
prohibitedSanTypes = append(prohibitedSanTypes, "UPN")
}
if len(prohibitedSanTypes) == 0 {
return nil
}
return prohibitedSanTypes
}
func BuildPolicySpecificationForTPP(checkPolicyResp CheckPolicyResponse) (*PolicySpecification, error) {
if checkPolicyResp.Policy == nil {
return nil, fmt.Errorf("policy is nul")
}
policy := checkPolicyResp.Policy
var ps PolicySpecification
/*ps.Users = tppPolicy.Contact
ps.Approvers = tppPolicy.Approver*/
var p Policy
if policy.WhitelistedDomains != nil {
p.Domains = policy.WhitelistedDomains
}
if policy.CertificateAuthority.Value != "" {
p.CertificateAuthority = &policy.CertificateAuthority.Value
}
var subject Subject
shouldCreateSubject := false
var defaultSubject DefaultSubject
shouldCreateDefSubject := false
var keyPair KeyPair
shouldCreateKeyPair := false
var defaultKeyPair DefaultKeyPair
shouldCreateDefKeyPair := false
var def Default
p.WildcardAllowed = &policy.WildcardsAllowed
if policy.ManagementType.Value != "" {
boolVal := false
if policy.ManagementType.Value == TppManagementTypeProvisioning {
boolVal = true
}
if policy.ManagementType.Locked {
p.AutoInstalled = &boolVal
} else {
def.AutoInstalled = &boolVal
}
}
//resolve subject's attributes
//resolve org
if policy.Subject.Organization.Value != "" {
if policy.Subject.Organization.Locked {
shouldCreateSubject = true
subject.Orgs = []string{policy.Subject.Organization.Value}
} else {
shouldCreateDefSubject = true
defaultSubject.Org = &policy.Subject.Organization.Value
}
}
//resolve orgUnit
if len(policy.Subject.OrganizationalUnit.Value) > 0 {
if policy.Subject.OrganizationalUnit.Locked {
shouldCreateSubject = true
subject.OrgUnits = policy.Subject.OrganizationalUnit.Value
} else {
shouldCreateDefSubject = true
defaultSubject.OrgUnits = policy.Subject.OrganizationalUnit.Value
}
}
//resolve localities
if policy.Subject.City.Value != "" {
if policy.Subject.City.Locked {
shouldCreateSubject = true
subject.Localities = []string{policy.Subject.City.Value}
} else {
shouldCreateDefSubject = true
defaultSubject.Locality = &policy.Subject.City.Value
}
}
//resolve states
if policy.Subject.State.Value != "" {
if policy.Subject.State.Locked {
shouldCreateSubject = true
subject.States = []string{policy.Subject.State.Value}
} else {
shouldCreateDefSubject = true
defaultSubject.State = &policy.Subject.State.Value
}
}
//resolve countries
if policy.Subject.Country.Value != "" {
if policy.Subject.Country.Locked {
shouldCreateSubject = true
subject.Countries = []string{policy.Subject.Country.Value}
} else {
shouldCreateDefSubject = true
defaultSubject.Country = &policy.Subject.Country.Value
}
}
//resolve key pair's attributes
//resolve keyTypes
if policy.KeyPairResponse.KeyAlgorithm.Value != "" {
if policy.KeyPairResponse.KeyAlgorithm.Locked {
keyPair.KeyTypes = []string{policy.KeyPairResponse.KeyAlgorithm.Value}
} else {
shouldCreateDefKeyPair = true
defaultKeyPair.KeyType = &policy.KeyPairResponse.KeyAlgorithm.Value
}
}
//resolve rsaKeySizes
if policy.KeyPairResponse.KeySize.Value > 0 {
if policy.KeyPairResponse.KeySize.Locked {
keyPair.RsaKeySizes = []int{policy.KeyPairResponse.KeySize.Value}
} else {
shouldCreateDefKeyPair = true
defaultKeyPair.RsaKeySize = &policy.KeyPairResponse.KeySize.Value
}
}
//resolve ellipticCurves
/*if tppPolicy.EllipticCurve != nil {
if tppPolicy.EllipticCurve.Locked {
shouldCreateKeyPair = true
keyPair.EllipticCurves = []string{tppPolicy.EllipticCurve.Value}
} else {
shouldCreateDefKeyPair = true
defaultKeyPair.EllipticCurve = &tppPolicy.EllipticCurve.Value
}
}*/
//resolve generationType
value := policy.CsrGeneration.Value
if value != "" {
booleanValue := true
//this mean that is a generated csr so ServiceGenerated is false
if value == UserProvided {
booleanValue = false
}
if policy.CsrGeneration.Locked {
keyPair.ServiceGenerated = &booleanValue
} else {
shouldCreateDefKeyPair = true
defaultKeyPair.ServiceGenerated = &booleanValue
}
}
keyPair.ReuseAllowed = &policy.PrivateKeyReuseAllowed
shouldCreateKeyPair = true
//assign policy's subject and key pair values
if shouldCreateSubject {
p.Subject = &subject
}
if shouldCreateKeyPair {
p.KeyPair = &keyPair
}
subjectAltNames := resolveSubjectAltNames(*policy)
if subjectAltNames != nil {
p.SubjectAltNames = subjectAltNames
}
//set policy and defaults to policy specification.
ps.Policy = &p
if shouldCreateDefSubject {
def.Subject = &defaultSubject
}
if shouldCreateDefKeyPair {
def.KeyPair = &defaultKeyPair
}
if shouldCreateDefSubject || shouldCreateDefKeyPair || def.AutoInstalled != nil {
ps.Default = &def
}
return &ps, nil
}
func resolveSubjectAltNames(policy PolicyResponse) *SubjectAltNames {
trueVal := true
falseVal := false
var subjectAltName SubjectAltNames
if policy.SubjAltNameDnsAllowed {
subjectAltName.DnsAllowed = &trueVal
} else {
subjectAltName.DnsAllowed = &falseVal
}
if policy.SubjAltNameIpAllowed {
subjectAltName.IpAllowed = &trueVal
} else {
subjectAltName.IpAllowed = &falseVal
}
if policy.SubjAltNameEmailAllowed {
subjectAltName.EmailAllowed = &trueVal
} else {
subjectAltName.EmailAllowed = &falseVal
}
if policy.SubjAltNameUriAllowed {
subjectAltName.UriAllowed = &trueVal
} else {
subjectAltName.UriAllowed = &falseVal
}
if policy.SubjAltNameUpnAllowed {
subjectAltName.UpnAllowed = &trueVal
} else {
subjectAltName.UpnAllowed = &falseVal
}
return &subjectAltName
}
func existValueInArray(array []string, value string) bool {
for _, currentValue := range array {
if currentValue == value {
return true
}
}
return false
}
//////////////////////---------------------Venafi Cloud policy management code-------------//////////////////////////////////////
func validateDefaultStringCloudValues(array []string, value string) bool {
if len(array) == 1 {
if array[0] == AllowAll { // this means that we are allowing everything
return true
}
}
return existValueInArray(array, value)
}
func validateDefaultSubjectOrgsCloudValues(defaultValues []string, policyValues []string) bool {
if len(policyValues) == 1 {
if policyValues[0] == AllowAll { // this means that we are allowing everything
return true
}
}
return existStringInArray(defaultValues, policyValues)
}
func ValidateCloudPolicySpecification(ps *PolicySpecification) error {
//validate key type
if ps.Policy != nil {
if ps.Policy.KeyPair != nil {
//validate key KeyTypes:keyLengths
if len(ps.Policy.KeyPair.RsaKeySizes) > 0 {
unSupported := getInvalidCloudRsaKeySizeValue(ps.Policy.KeyPair.RsaKeySizes)
if unSupported != nil {
return fmt.Errorf("specified attribute key length value: %s is not supported on VaaS", strconv.Itoa(*(unSupported)))
}
}
}
//validate subjectCNRegexes & sanRegexes
if ps.Policy.SubjectAltNames != nil {
subjectAltNames := getSubjectAltNames(*(ps.Policy.SubjectAltNames))
if len(subjectAltNames) > 0 {
for k, v := range subjectAltNames {
if k == "upnAllowed" && v {
return fmt.Errorf("specified subjectAltNames: %s value is true, this value is not allowed ", k)
}
if k == "uriAllowed" && v {
if len(ps.Policy.SubjectAltNames.UriProtocols) == 0 {
return fmt.Errorf("uriAllowed attribute is true, but uriProtocols is not specified or empty")
}
}
}
}
}
//if defaults are define validate that them matches with policy values
if ps.Policy.Subject != nil {
if ps.Default != nil && ps.Default.Subject != nil && ps.Default.Subject.Org != nil && len(ps.Policy.Subject.Orgs) > 0 {
exist := validateDefaultStringCloudValues(ps.Policy.Subject.Orgs, *(ps.Default.Subject.Org))
if !exist {
return fmt.Errorf("specified default org value: %s doesn't match with specified policy org", *(ps.Default.Subject.Org))
}
}
if ps.Default != nil && ps.Default.Subject != nil && len(ps.Default.Subject.OrgUnits) > 0 && len(ps.Policy.Subject.OrgUnits) > 0 {
exist := validateDefaultSubjectOrgsCloudValues(ps.Default.Subject.OrgUnits, ps.Policy.Subject.OrgUnits)
if !exist {
return fmt.Errorf("specified default org unit value: %s doesn't match with specified policy org unit", *(ps.Default.Subject.Org))
}
}
if ps.Default != nil && ps.Default.Subject != nil && ps.Default.Subject.Locality != nil && len(ps.Policy.Subject.Localities) > 0 {
exist := validateDefaultStringCloudValues(ps.Policy.Subject.Localities, *(ps.Default.Subject.Locality))
if !exist {
return fmt.Errorf("specified default locality value: %s doesn't match with specified policy locality", *(ps.Default.Subject.Locality))
}
}
if ps.Default != nil && ps.Default.Subject != nil && ps.Default.Subject.State != nil && len(ps.Policy.Subject.States) > 0 {
exist := validateDefaultStringCloudValues(ps.Policy.Subject.States, *(ps.Default.Subject.State))
if !exist {
return fmt.Errorf("specified default state value: %s doesn't match with specified policy state", *(ps.Default.Subject.State))
}
}
if ps.Default != nil && ps.Default.Subject != nil && ps.Default.Subject.Country != nil && len(ps.Policy.Subject.Countries) > 0 {
exist := validateDefaultStringCloudValues(ps.Policy.Subject.Countries, *(ps.Default.Subject.Country))
if !exist {
return fmt.Errorf("specified default country value: %s doesn't match with specified policy country", *(ps.Default.Subject.Country))
}
}
}
if ps.Policy.KeyPair != nil {
if ps.Default != nil && ps.Default.KeyPair != nil && ps.Default.KeyPair.KeyType != nil && len(ps.Policy.KeyPair.KeyTypes) > 0 {
exist := existValueInArray(ps.Policy.KeyPair.KeyTypes, *(ps.Default.KeyPair.KeyType))
if !exist {
return fmt.Errorf("specified default key type value: %s doesn't match with specified policy key type", *(ps.Default.KeyPair.KeyType))
}
}
if ps.Default != nil && ps.Default.KeyPair != nil && ps.Default.KeyPair.RsaKeySize != nil && len(ps.Policy.KeyPair.RsaKeySizes) > 0 {
exist := existIntInArray([]int{*(ps.Default.KeyPair.RsaKeySize)}, ps.Policy.KeyPair.RsaKeySizes)
if !exist {
return fmt.Errorf("specified default rsa key size value: %s doesn't match with specified policy rsa key size", strconv.Itoa(*(ps.Default.KeyPair.RsaKeySize)))
}
}
}
}
//now in case that policy is empty but defaults key types and rsa sizes not, we need to validate them
if ps.Default != nil && ps.Default.KeyPair != nil {
if ps.Default.KeyPair.KeyType != nil && *(ps.Default.KeyPair.KeyType) != "" {
if *(ps.Default.KeyPair.KeyType) != "RSA" && *(ps.Default.KeyPair.KeyType) != "EC" {
return fmt.Errorf("specified default attribute keyType value is not supported on VaaS")
}
}
//validate key KeyTypes:keyLengths
if ps.Default.KeyPair.RsaKeySize != nil && *(ps.Default.KeyPair.RsaKeySize) != 0 {
unSupported := getInvalidCloudRsaKeySizeValue([]int{*(ps.Default.KeyPair.RsaKeySize)})
if unSupported != nil {
return fmt.Errorf("specified attribute key length value: %s is not supported on VaaS", strconv.Itoa(*(unSupported)))
}
}
}
return nil
}
func getInvalidCloudRsaKeySizeValue(specifiedRSAKeys []int) *int {
for _, currentUserVal := range specifiedRSAKeys {
valid := false
for _, rsaKey := range CloudRsaKeySize {
if currentUserVal == rsaKey {
valid = true
break
}
}
if !valid {
return ¤tUserVal
}
}
return nil
}
func getSubjectAltNames(names SubjectAltNames) map[string]bool {
subjectAltNames := make(map[string]bool)
if names.DnsAllowed != nil {
subjectAltNames["dnsAllowed"] = *(names.DnsAllowed)
}
if names.IpAllowed != nil {
subjectAltNames["ipAllowed"] = *(names.IpAllowed)
}
if names.EmailAllowed != nil {
subjectAltNames["emailAllowed"] = *(names.EmailAllowed)
}
if names.UriAllowed != nil {
subjectAltNames["uriAllowed"] = *(names.UriAllowed)
}
if names.UpnAllowed != nil {
subjectAltNames["upnAllowed"] = *(names.UpnAllowed)
}
return subjectAltNames
}
func BuildCloudCitRequest(ps *PolicySpecification, ca *CADetails) (*CloudPolicyRequest, error) {
var cloudPolicyRequest CloudPolicyRequest
var certAuth CertificateAuthorityInfo
var err error
var period int
if ps.Policy != nil && ps.Policy.CertificateAuthority != nil && *(ps.Policy.CertificateAuthority) != "" {
certAuth, err = GetCertAuthorityInfo(*(ps.Policy.CertificateAuthority))
if err != nil {
return nil, err
}
} else {
certAuth, err = GetCertAuthorityInfo(DefaultCA)
if err != nil {
return nil, err
}
}
cloudPolicyRequest.CertificateAuthority = certAuth.CAType
cloudPolicyRequest.CertificateAuthorityProductOptionId = *(ca.CertificateAuthorityProductOptionId)
if ps.Policy != nil && ps.Policy.MaxValidDays != nil {
period = *(ps.Policy.MaxValidDays)
if period == 0 {
period = 365
}
} else {
period = 365
}
product := Product{
CertificateAuthority: certAuth.CAType,
ProductName: certAuth.VendorProductName,
ValidityPeriod: fmt.Sprint("P", strconv.Itoa(period), "D"),
}
if certAuth.CAType == CloudDigicertCA {
alg := "SHA256"
autoRen := false
product.HashAlgorithm = &alg
product.AutoRenew = &autoRen
product.OrganizationId = ca.CertificateAuthorityOrganizationId
}
if certAuth.CAType == CloudEntrustCA {
td := TrackingData{
CertificateAuthority: CloudEntrustCA,
RequesterName: CloudRequesterName,
RequesterEmail: CloudRequesterEmail,
RequesterPhone: CloudRequesterPhone,
}
cloudPolicyRequest.TrackingData = &td
}
cloudPolicyRequest.Product = product
if ps.Policy != nil && len(ps.Policy.Domains) > 0 {
regexValues := ConvertToRegex(ps.Policy.Domains, IsWildcardAllowed(*(ps)))
cloudPolicyRequest.SubjectCNRegexes = regexValues
if ps.Policy.SubjectAltNames != nil && ps.Policy.SubjectAltNames.DnsAllowed != nil {
if *(ps.Policy.SubjectAltNames.DnsAllowed) {
cloudPolicyRequest.SanRegexes = regexValues //in cloud subject CN and SAN have the same values and we use domains as those values
} else {
cloudPolicyRequest.SanRegexes = nil
}
} else {
cloudPolicyRequest.SanRegexes = regexValues //in cloud subject CN and SAN have the same values and we use domains as those values
}
if ps.Policy.SubjectAltNames != nil && ps.Policy.SubjectAltNames.EmailAllowed != nil {
if *(ps.Policy.SubjectAltNames.EmailAllowed) {
rfc882Regex := ConvertToRfc822Regex(ps.Policy.Domains)
cloudPolicyRequest.SanRfc822NameRegexes = rfc882Regex
} else {
cloudPolicyRequest.SanRfc822NameRegexes = nil
}
}
if ps.Policy != nil && ps.Policy.SubjectAltNames != nil && len(ps.Policy.SubjectAltNames.UriProtocols) > 0 {
uriRegex := convertToUriRegex(ps.Policy.SubjectAltNames.UriProtocols, ps.Policy.Domains)
cloudPolicyRequest.SanUniformResourceIdentifierRegexes = uriRegex
}
} else {
cloudPolicyRequest.SubjectCNRegexes = []string{".*"}
cloudPolicyRequest.SanRegexes = []string{".*"}
if ps.Policy != nil {
if ps.Policy.SubjectAltNames != nil && ps.Policy.SubjectAltNames.EmailAllowed != nil {
if *(ps.Policy.SubjectAltNames.EmailAllowed) {
cloudPolicyRequest.SanRfc822NameRegexes = []string{".*@.*"}
}
}
if ps.Policy.SubjectAltNames != nil && ps.Policy.SubjectAltNames.IpAllowed != nil {
if *(ps.Policy.SubjectAltNames.IpAllowed) {
cloudPolicyRequest.SanIpAddressRegexes = []string{}
}
}
//to be implemented.
if ps.Policy != nil && ps.Policy.SubjectAltNames != nil && len(ps.Policy.SubjectAltNames.UriProtocols) > 0 {
uriRegex := convertToUriRegex(ps.Policy.SubjectAltNames.UriProtocols, []string{".*"})
cloudPolicyRequest.SanUniformResourceIdentifierRegexes = uriRegex
}
}
}
if ps.Policy != nil && ps.Policy.SubjectAltNames != nil && ps.Policy.SubjectAltNames.IpAllowed != nil {
if *(ps.Policy.SubjectAltNames.IpAllowed) {
if len(ps.Policy.SubjectAltNames.IpConstraints) > 0 {
cloudPolicyRequest.SanIpAddressRegexes = getIpRegexes(ps.Policy.SubjectAltNames.IpConstraints)
} else {
cloudPolicyRequest.SanIpAddressRegexes = []string{
ipv4, ipv6,
}