-
Notifications
You must be signed in to change notification settings - Fork 4
/
est_ca_config.go
1276 lines (1066 loc) · 27.8 KB
/
est_ca_config.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
/* Copyright (c) 2018 Cisco and/or its affiliates.
*
* 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
*
* http://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.
*/
/*
* Public Interfaces:
*
* Init Config
* ---------------------------------------------------
* func InitGlobalConfig()
* Call for initializing/resetting Config to defaults
* ---------------------------------------------------
* General Purpose APIs
* ---------------------------------------------------
* func PrintConfigInfo()
* func GetLDAPInfo() *LDAPInfo
* func GetDBInfo() *DBInfo
* func GetESTInterface() *ESTInterface
* func GetAdminInterface() *AdminInterface
* func GetCRLSize() uint32
* func GetDebugFlag() bool
* ---------------------------------------------------
* Set CRL Size and Debug
* ---------------------------------------------------
* func SetCRLSize(s uint32)
* func SetDebug(sense bool)
* ---------------------------------------------------
* Set Crypto Info
* ---------------------------------------------------
* func SetCryptoHash(hash string) bool
* func SetCryptoEncryptMode(enc string) bool
* func SetCryptoEncryption(enc string) bool
* ---------------------------------------------------
* Set Admin Interface
* ---------------------------------------------------
* func SetAdminInterface(ip string, port uint16) bool
* ---------------------------------------------------
* Set EST Interface
* ---------------------------------------------------
* func SetESTTLSInfo(id, key, tstore string) bool
* func SetESTAuthMethod(auth string) bool
* func SetESTAddr(ip string, port uint16) bool
* ---------------------------------------------------
* Set LDAP Info
* ---------------------------------------------------
* func SetLDAPTLSInfo(id, key, tstore string) bool
* func SetLDAPAddr(ip string, port uint16) bool
* func SetLDAPURL(url string) bool
* ---------------------------------------------------
* Set DB info
* ---------------------------------------------------
* func SetDBURL(url string) bool
* func SetDBIP(ip string, port uint16) bool
* func SetDBTLSInfo(id, key, tstore string) bool
* func SetDBCredentials(uname string, pass string) bool
* func SetDBFile(file string) bool
* func SetDBType(t string) bool
* ---------------------------------------------------
* Set CA Profile
* ---------------------------------------------------
* func CreateCAProfile(name string) *CAProfile
* func DeleteCAProfilebyName(name string)
* func FindCAProfileByName(name string) *CAProfile
* func SetDefaultCAProfile(name string) bool
* func SetProfileValidity(prof *CAProfile, val uint64, unit string) bool
* func SetProfileBasicAttr(prof *CAProfile, isca bool, maxpath int) bool
* func SetProfileSubjectAltName(prof *CAProfile, ip, host, string) bool
* func SetProfileSubjectName(prof *CAProfile, cn, o, c, ou string) bool
* ---------------------------------------------------
* Set CA
* ---------------------------------------------------
* func DeleteCAbyName(name string)
* func CreateCA(name string) *CA
* func FindCAByName(name string) *CA
* func GetCANameList() []CA
* func SetCASignatureAlgoritm(ca *CA, algo string) bool
* func SetCAKeyInfo(ca *CA, ktype string, klen int) bool
* func SetCABasicAttr(ca *CA, maxpath int) bool
* func SetCASubjectAltName(ca *CA, ip, host, string) bool
* func SetCASubjectName(ca *CA, cn, o, c, ou string) bool
* func SetCAValidity(ca *CA, val uint64, unit string) bool
* func SetCACRLPath(ca *CA, crlpath string) bool
* func SetCACertPath(ca *CA, cpath, kpath, chainpath string) bool
* func SetCAMode(ca *CA, camode string) bool
* func SetDefaultCA(name string) bool
* func GetDefaultCA() string
* func SetCATLSInfo(ca *CA, id, key, tstore string) bool
* func SetEnrollParameter(ca *CA, fingerprint, uname, passwd string) bool
* func SetParentCAAuthMode(ca *CA, mode string) bool {
* func SetParentCAURL(ca *CA, url string) bool
* func SetParentCAIP(ca *CA, ip string, port uint16) bool
* ---------------------------------------------------
*/
package config
import (
"fmt"
"strings"
)
// Enum Definitions
const (
CEST_ENCRYPTION_INVALID = iota
CEST_ENCRYPTION_AES128
CEST_ENCRYPTION_AES256
)
const (
CEST_STR_ENCRYPTION_AES128 = "aes128"
CEST_STR_ENCRYPTION_AES256 = "aes256"
)
const (
CEST_MODE_INVALID = iota
CEST_MODE_CBC
CEST_MODE_GCM
)
const (
CEST_STR_ENC_MODE_CBC = "cbc"
CEST_STR_ENC_MODE_GCM = "gcm"
)
const (
CEST_HASH_INVALID = iota
CEST_HASH_SHA1
CEST_HASH_SHA256
CEST_HASH_SHA384
CEST_HASH_SHA512
)
const (
CEST_STR_HASH_SHA1 = "sha1"
CEST_STR_HASH_SHA256 = "sha256"
CEST_STR_HASH_SHA384 = "sha384"
CEST_STR_HASH_SHA512 = "sha512"
)
const (
CEST_ENGINE_INVALID = iota
CEST_ENGINE_SW
CEST_ENGINE_CUSTOM
)
const (
CEST_STR_ENGINE_SW = "sw"
)
const (
CEST_DBTYPE_INVALID = iota
CEST_DBTYPE_SQLLITE
CEST_DBTYPE_MYSQL
)
const (
CEST_STR_DBTYPE_SQLLITE = "sqllite"
CEST_STR_DBTYPE_MYSQL = "mysql"
)
const (
CEST_AUTH_METHOD_INVALID = iota
CEST_AUTH_METHOD_TLSPSK
CEST_AUTH_METHOD_CERTAUTH
CEST_AUTH_METHOD_HTTPBASICAUTH
)
const (
CEST_STR_AUTH_TLSPSK = "tls-psk"
CEST_STR_AUTH_CERTAUTH = "cert-auth"
CEST_STR_AUTH_HTTPBASICAUTH = "http-basic-auth"
)
const (
CEST_KEYTYPE_INVALID = iota
CEST_KEYTYPE_RSA
CEST_KEYTYPE_ECDSA
)
const (
CEST_STR_KEYTYPE_RSA = "rsa"
CEST_STR_KEYTYPE_ECDSA = "ecdsa"
)
const (
CEST_CA_MODE_INVALID = iota
CEST_CA_MODE_ROOT
CEST_CA_MODE_SUBCA
)
const (
CEST_STR_CA_MODE_ROOT = "ca"
CEST_STR_CA_MODE_SUBCA = "subca"
)
const (
CEST_VALIDITY_UNIT_DAYS = "day"
CEST_VALIDITY_UNIT_HOURS = "hour"
CEST_VALIDITY_UNIT_YEARS = "year"
)
// Taken from the x509 package in go lang
const (
CEST_SIGNATURE_INVALID = iota
CEST_SIGNATURE_ECDSA_SHA1 = 9
CEST_SIGNATURE_ECDSA_SHA256 = 10
CEST_SIGNATURE_ECDSA_SHA384 = 11
CEST_SIGNATURE_ECDSA_SHA512 = 12
)
const (
CEST_STR_SIGNATURE_ECDSA_SHA1 = "ecdsa-with-sha1"
CEST_STR_SIGNATURE_ECDSA_SHA256 = "ecdsa-with-sha256"
CEST_STR_SIGNATURE_ECDSA_SHA384 = "ecdsa-with-sha384"
CEST_STR_SIGNATURE_ECDSA_SHA512 = "ecdsa-with-sha512"
)
// Defaults for the configuration
const CEST_ENCRYPTION_DEFAULT = CEST_ENCRYPTION_AES128
const CEST_ENCRYPTION_MODE_DEFAULT = CEST_MODE_CBC
const CEST_HASH_DEFAULT = CEST_HASH_SHA256
const CEST_ENGINE_DEFAULT = CEST_ENGINE_SW
const CEST_DBTYPE_DEFAULT = CEST_DBTYPE_SQLLITE
const CEST_DBFILE_DEFAULT = "est_ca_database.db"
const CEST_AUTH_METHOD_DEFAULT = CEST_AUTH_METHOD_TLSPSK
const CEST_KEYTYPE_DEFAULT = CEST_KEYTYPE_ECDSA
const CEST_CA_MODE_DEFAULT = CEST_CA_MODE_ROOT
const CEST_SIGNATURE_DEFAULT = CEST_SIGNATURE_ECDSA_SHA512
const CEST_KEYLENGTH_DEFAULT = 512
const CEST_CA_VALIDITY_DEFAULT = 43830 // In hours
const CEST_CLIENT_VALIDITY_DEFAULT = 8766 // In hours
const CEST_CRLSIZE_DEFAULT = 512000
type KeyInfo struct {
Ktype uint8
Length uint16
}
type Crypto struct {
Encryption uint8
Mode uint8
Hash uint8
Engine uint8
}
type Address struct {
Ip string
Port uint16
Url string
}
type TLSInfo struct {
Enabled bool
Idcertpath string
Keypath string
Tstorepath string
}
type CertSubjectName struct {
Cn string
O string
C string
Ou string
}
type CertSubjectAltName struct {
Ip string
Host string
}
type CertBasicAttr struct {
Isca bool
Maxpathlen uint8
}
type CertAttributes struct {
Subname CertSubjectName
Subaltname CertSubjectAltName
Basicattr CertBasicAttr
}
type AdminInterface struct {
Addr Address
}
type ESTInterface struct {
Addr Address
Authmethod uint8
Tls TLSInfo
}
type DBInfo struct {
Dbtype uint8
Dbfile string
Addr Address
Uname string
Passwd string
Tls TLSInfo
}
type LDAPInfo struct {
Enabled bool
Addr Address
Tls TLSInfo
}
type CAProfile struct {
Name string
Attr CertAttributes
Validity uint64
next *CAProfile
}
type CAProfileList struct {
head *CAProfile
count uint32
Default_prof string
}
type ParentCAInfo struct {
Addr Address
Authmode uint8
Cafingerprint string
Enrollid string
Passwd string
Tls TLSInfo
}
type CA struct {
Name string
Mode uint8
Certpath string
Keypath string
Chainpath string
Crlpath string
Validity uint64
Csr CertAttributes
Key KeyInfo
Signature uint8
Parent ParentCAInfo
next *CA
}
type CAList struct {
head *CA
count uint32
Defaultca string
}
type CESTConfig struct {
debug bool
crlsize uint32
crypto Crypto
admin AdminInterface
est ESTInterface
db DBInfo
ldap LDAPInfo
caprofiles CAProfileList
calist CAList
}
// Global Config Structure
var cest_global_config CESTConfig
// Interfaces for Config manipulation
// Returns the Global Config Block
func GetGlobalConfig() *CESTConfig {
return (&cest_global_config)
}
// Set the defaults in a config structure
func SetConfigDefaults(conf *CESTConfig) {
if conf == nil {
return
}
// Set CRL SIZE
conf.crlsize = CEST_CRLSIZE_DEFAULT
// Set Crypto Parameters
conf.crypto.Encryption = CEST_ENCRYPTION_DEFAULT
conf.crypto.Mode = CEST_ENCRYPTION_MODE_DEFAULT
conf.crypto.Hash = CEST_HASH_DEFAULT
conf.crypto.Engine = CEST_ENGINE_DEFAULT
// Set DB defaults
conf.db.Dbtype = CEST_DBTYPE_DEFAULT
conf.db.Dbfile = CEST_DBFILE_DEFAULT
}
// Initializes the global config to defaults
func InitGlobalConfig() {
SetConfigDefaults(GetGlobalConfig())
}
// Set Debug mode
func SetDebug(sense bool) {
GetGlobalConfig().debug = sense
}
// Set the CRL size
func SetCRLSize(s uint32) {
GetGlobalConfig().crlsize = s
}
// Get Crypto Encryption type from String
func GetCryptoEncryptionType(t string) uint8 {
switch strings.ToLower(t) {
case CEST_STR_ENCRYPTION_AES128:
return CEST_ENCRYPTION_AES128
case CEST_STR_ENCRYPTION_AES256:
return CEST_ENCRYPTION_AES256
default:
return CEST_ENCRYPTION_INVALID
}
}
// Get Encryption Mode type
func GetCryptoEncryptionModeType(t string) uint8 {
switch strings.ToLower(t) {
case CEST_STR_ENC_MODE_CBC:
return CEST_MODE_CBC
case CEST_STR_ENC_MODE_GCM:
return CEST_MODE_GCM
default:
return CEST_MODE_INVALID
}
}
// Get Hash type
func GetCryptoHashType(t string) uint8 {
switch strings.ToLower(t) {
case CEST_STR_HASH_SHA1:
return CEST_HASH_SHA1
case CEST_STR_HASH_SHA256:
return CEST_HASH_SHA256
case CEST_STR_HASH_SHA384:
return CEST_HASH_SHA384
case CEST_STR_HASH_SHA512:
return CEST_HASH_SHA512
default:
return CEST_HASH_INVALID
}
}
// Get Auth method type
func GetESTAuthMethod(t string) uint8 {
switch strings.ToLower(t) {
case CEST_STR_AUTH_TLSPSK:
return CEST_AUTH_METHOD_TLSPSK
case CEST_STR_AUTH_CERTAUTH:
return CEST_AUTH_METHOD_CERTAUTH
case CEST_STR_AUTH_HTTPBASICAUTH:
return CEST_AUTH_METHOD_HTTPBASICAUTH
default:
return CEST_AUTH_METHOD_INVALID
}
}
// Get the DB type from string
func GetDBType(t string) uint8 {
switch strings.ToLower(t) {
case CEST_STR_DBTYPE_SQLLITE:
return CEST_DBTYPE_SQLLITE
case CEST_STR_DBTYPE_MYSQL:
return CEST_DBTYPE_MYSQL
default:
return CEST_DBTYPE_INVALID
}
}
// Set Crypto Encryption
func SetCryptoEncryption(enc string) bool {
var x uint8
x = GetCryptoEncryptionType(enc)
if x != CEST_ENCRYPTION_INVALID {
GetGlobalConfig().crypto.Encryption = x
return true
}
return false
}
// Set Crypto Hash
func SetCryptoEncryptMode(enc string) bool {
var x uint8
x = GetCryptoEncryptionModeType(enc)
if x != CEST_MODE_INVALID {
GetGlobalConfig().crypto.Mode = x
return true
}
return false
}
// Set the crypto hash
func SetCryptoHash(hash string) bool {
var x uint8
x = GetCryptoHashType(hash)
if x != CEST_HASH_INVALID {
GetGlobalConfig().crypto.Hash = x
return true
}
return false
}
// Set Admin Interface
func SetAdminInterface(ip string, port uint16) bool {
conf := GetGlobalConfig()
conf.admin.Addr.Ip = ip
conf.admin.Addr.Port = port
return true
}
// Set EST Interface
func SetESTAddr(ip string, port uint16) bool {
conf := GetGlobalConfig()
conf.est.Addr.Ip = ip
conf.est.Addr.Port = port
return true
}
// Set EST Auth Method
func SetESTAuthMethod(auth string) bool {
var x uint8
x = GetESTAuthMethod(auth)
if x != CEST_AUTH_METHOD_INVALID {
GetGlobalConfig().est.Authmethod = x
return true
}
return false
}
// Set EST TLS params
func SetESTTLSInfo(id, key, tstore string) bool {
conf := GetGlobalConfig()
conf.est.Tls.Enabled = true
conf.est.Tls.Idcertpath = id
conf.est.Tls.Keypath = key
conf.est.Tls.Tstorepath = tstore
return true
}
// Set LDAP URL info
func SetLDAPURL(url string) bool {
conf := GetGlobalConfig()
conf.ldap.Enabled = true
conf.ldap.Addr.Url = url
return true
}
// Set LDAP IP/Port
func SetLDAPAddr(ip string, port uint16) bool {
conf := GetGlobalConfig()
conf.ldap.Enabled = true
conf.ldap.Addr.Ip = ip
conf.ldap.Addr.Port = port
return true
}
// Set LDAP TLS info
func SetLDAPTLSInfo(id, key, tstore string) bool {
conf := GetGlobalConfig()
conf.ldap.Tls.Enabled = true
conf.ldap.Tls.Idcertpath = id
conf.ldap.Tls.Keypath = key
conf.ldap.Tls.Tstorepath = tstore
return true
}
// Set DB type
func SetDBType(t string) bool {
var x uint8
x = GetDBType(t)
if x != CEST_DBTYPE_INVALID {
GetGlobalConfig().db.Dbtype = x
return true
}
return false
}
// Set DB Filename
func SetDBFile(file string) bool {
GetGlobalConfig().db.Dbfile = file
return true
}
// Set DB credentials
func SetDBCredentials(uname string, pass string) bool {
conf := GetGlobalConfig()
conf.db.Uname = uname
conf.db.Passwd = pass
return true
}
// Set DB IP/Port
func SetDBIP(ip string, port uint16) bool {
conf := GetGlobalConfig()
conf.db.Addr.Ip = ip
conf.db.Addr.Port = port
return true
}
// Set DB URL
func SetDBURL(url string) bool {
GetGlobalConfig().db.Addr.Url = url
return true
}
// Set DB TLS Info
func SetDBTLSInfo(id, key, tstore string) bool {
conf := GetGlobalConfig()
conf.db.Tls.Enabled = true
conf.db.Tls.Idcertpath = id
conf.db.Tls.Keypath = key
conf.db.Tls.Tstorepath = tstore
return true
}
// Set Default CA Profile if configured
func SetDefaultCAProfile(name string) bool {
// Add validation for default later
GetGlobalConfig().caprofiles.Default_prof = name
return true
}
// Get Default CA Profile Name
func GetDefaultCAProfile() string {
return GetGlobalConfig().caprofiles.Default_prof
}
// Set Default CA if configured
func SetDefaultCA(name string) bool {
// Add validation for default later
GetGlobalConfig().calist.Defaultca = name
return true
}
// Get Default CA
func GetDefaultCA() string {
return GetGlobalConfig().calist.Defaultca
}
// Find CA Profile by name
func FindCAProfileByName(name string) *CAProfile {
var i uint32
var prof *CAProfile
conf := GetGlobalConfig()
prof = conf.caprofiles.head
for i = 0; i < conf.caprofiles.count; i++ {
if (prof != nil) && (prof.Name == name) {
return prof
}
prof = prof.next
}
return nil
}
// Find CA by name
func FindCAByName(name string) *CA {
var i uint32
var ca *CA
conf := GetGlobalConfig()
ca = conf.calist.head
for i = 0; i < conf.calist.count; i++ {
if (ca != nil) && (ca.Name == name) {
return ca
}
ca = ca.next
}
return nil
}
// Create new ca profile object by name
func CreateCAProfile(name string) *CAProfile {
var caprof *CAProfile
conf := GetGlobalConfig()
// Does it exist?
caprof = FindCAProfileByName(name)
if caprof != nil {
return caprof
}
caprof = new(CAProfile)
caprof.Name = name
if conf.caprofiles.head == nil {
conf.caprofiles.head = caprof
} else {
caprof.next = conf.caprofiles.head
conf.caprofiles.head = caprof
}
conf.caprofiles.count++
// Set Defaults
caprof.Validity = CEST_CLIENT_VALIDITY_DEFAULT
return caprof
}
// GetCANameList
func GetCANameList() []string {
var ca *CA
var i uint32
var canamearray [100]string
conf := GetGlobalConfig()
ca = conf.calist.head
for i = 0; i < conf.calist.count; i++ {
canamearray[i] = ca.Name
ca = ca.next
}
list := canamearray[:i]
return list
}
// Create new ca object by name
func CreateCA(name string) *CA {
var ca *CA
conf := GetGlobalConfig()
// Does it exist?
ca = FindCAByName(name)
if ca != nil {
return ca
}
ca = new(CA)
ca.Name = name
if conf.calist.head == nil {
conf.calist.head = ca
} else {
ca.next = conf.calist.head
conf.calist.head = ca
}
conf.calist.count++
// Set defaults for the ca
ca.Mode = CEST_CA_MODE_DEFAULT
ca.Validity = CEST_CA_VALIDITY_DEFAULT
ca.Signature = CEST_SIGNATURE_DEFAULT
ca.Key.Ktype = CEST_KEYTYPE_DEFAULT
ca.Key.Length = CEST_KEYLENGTH_DEFAULT
ca.Csr.Basicattr.Isca = true
return ca
}
// Set Profile Subjectname
func SetProfileSubjectName(prof *CAProfile, cn, o, c, ou string) bool {
if prof == nil {
return false
}
prof.Attr.Subname.Cn = cn
prof.Attr.Subname.O = o
prof.Attr.Subname.C = c
prof.Attr.Subname.Ou = ou
return true
}
// Set Profile Sub Alt name
func SetProfileSubjectAltName(prof *CAProfile, ip, host string) bool {
if prof == nil {
return false
}
prof.Attr.Subaltname.Ip = ip
prof.Attr.Subaltname.Host = host
return true
}
// Set profile basic Attributes
func SetProfileBasicAttr(prof *CAProfile, isca bool, maxpath uint8) bool {
if prof == nil {
return false
}
prof.Attr.Basicattr.Isca = isca
prof.Attr.Basicattr.Maxpathlen = maxpath
return true
}
// find validity in hours given units and number
func GetValidityInHours(val uint64, unit string) uint64 {
var factor uint64
switch strings.ToLower(unit) {
case CEST_VALIDITY_UNIT_HOURS:
factor = 1
case CEST_VALIDITY_UNIT_DAYS:
factor = 24
case CEST_VALIDITY_UNIT_YEARS:
factor = 8766
default:
factor = 0
}
return (val * factor)
}
// Set validity for profile
func SetProfileValidity(prof *CAProfile, val uint64, unit string) bool {
var valhours uint64
if prof == nil {
return false
}
valhours = GetValidityInHours(val, unit)
if valhours == 0 {
return false
}
prof.Validity = valhours
return true
}
// Delete CA Profile
func DeleteCAProfilebyName(name string) {
var caprof *CAProfile
conf := GetGlobalConfig()
// Does it exist?
caprof = FindCAProfileByName(name)
if caprof == nil {
return
}
conf.caprofiles.head = caprof.next
conf.caprofiles.count--
}
// Delete CA Config
func DeleteCAbyName(name string) {
var ca *CA
conf := GetGlobalConfig()
// Does it exist?
ca = FindCAByName(name)
if ca == nil {
return
}
conf.calist.head = ca.next
conf.calist.count--
}
// Get CA mode type
func GetCAModeType(s string) uint8 {
switch strings.ToLower(s) {
case CEST_STR_CA_MODE_ROOT:
return CEST_CA_MODE_ROOT
case CEST_STR_CA_MODE_SUBCA:
return CEST_CA_MODE_SUBCA
default:
return CEST_CA_MODE_INVALID
}
}
// Set the CA mode
func SetCAMode(ca *CA, camode string) bool {
var mode uint8
if ca == nil {
return false
}
mode = GetCAModeType(camode)
if mode != CEST_CA_MODE_INVALID {
ca.Mode = mode
return true
}
return false
}
// Set the CA cert paths
func SetCACertPath(ca *CA, cpath, kpath, chainpath string) bool {
if ca == nil {
return false
}
ca.Certpath = cpath
ca.Keypath = kpath
ca.Chainpath = chainpath
return true
}
// Set CA CRL path
func SetCACRLPath(ca *CA, crlpath string) bool {
if ca == nil {
return false
}
ca.Crlpath = crlpath
return true
}
// Set CA validity
func SetCAValidity(ca *CA, val uint64, unit string) bool {
var valhours uint64
if ca == nil {
return false
}
valhours = GetValidityInHours(val, unit)
if valhours == 0 {
return false
}
ca.Validity = valhours
return true
}
// Set CA Subjectname
func SetCASubjectName(ca *CA, cn, o, c, ou string) bool {
if ca == nil {
return false
}
ca.Csr.Subname.Cn = cn
ca.Csr.Subname.O = o
ca.Csr.Subname.C = c
ca.Csr.Subname.Ou = ou
return true
}
// Set CA Sub Alt name
func SetCASubjectAltName(ca *CA, ip, host string) bool {
if ca == nil {
return false
}
ca.Csr.Subaltname.Ip = ip
ca.Csr.Subaltname.Host = host
return true
}
// Set CA basic Attributes
func SetCABasicAttr(ca *CA, maxpath uint8) bool {
if ca == nil {