-
Notifications
You must be signed in to change notification settings - Fork 1
/
authority.go
171 lines (143 loc) · 4.48 KB
/
authority.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
package authority
import (
"bytes"
"crypto"
"github.com/effective-security/xlog"
"github.com/effective-security/xpki/certutil"
"github.com/effective-security/xpki/cryptoprov"
"github.com/pkg/errors"
)
var logger = xlog.NewPackageLogger("github.com/effective-security/xpki", "authority")
// Authority defines the CA
type Authority struct {
issuers map[string]*Issuer // label => Issuer
issuersByProfile map[string]*Issuer // cert profile => Issuer
issuersByKeyID map[string]*Issuer // SKID => Issuer
// Crypto holds providers for HSM, SoftHSM, KMS, etc.
crypto *cryptoprov.Crypto
// keep track of Wildcard profiles
profiles map[string]*CertProfile
RootBundle []byte
CaBundle []byte
}
// NewAuthority returns new instance of Authority
func NewAuthority(cfg *Config, crypto *cryptoprov.Crypto) (*Authority, error) {
if cfg.Authority == nil {
return nil, errors.New("missing Authority configuration")
}
cfg = cfg.Copy()
ca := &Authority{
crypto: crypto,
issuers: make(map[string]*Issuer),
issuersByProfile: make(map[string]*Issuer),
issuersByKeyID: make(map[string]*Issuer),
profiles: cfg.Profiles,
}
rootBundle, err := certutil.LoadPEMFiles(cfg.Authority.RootsBundleFiles...)
if err != nil {
return nil, errors.WithMessagef(err, "unable to load Root bundles")
}
ca.RootBundle = rootBundle
caBundle, err := certutil.LoadPEMFiles(cfg.Authority.CABundleFiles...)
if err != nil {
return nil, errors.WithMessagef(err, "unable to load CA bundles")
}
ca.CaBundle = caBundle
if ca.profiles == nil {
ca.profiles = make(map[string]*CertProfile)
}
for _, isscfg := range cfg.Authority.Issuers {
if isscfg.GetDisabled() {
logger.KV(xlog.INFO, "reason", "disabled", "issuer", isscfg.Label)
continue
}
ccfg := isscfg.Copy()
issuer, err := NewIssuerWithBundles(ccfg, crypto, ca.CaBundle, ca.RootBundle)
if err != nil {
return nil, errors.WithMessagef(err, "unable to create issuer: %q", isscfg.Label)
}
err = ca.AddIssuer(issuer)
if err != nil {
return nil, errors.WithMessagef(err, "unable to add issuer: %q", isscfg.Label)
}
}
return ca, nil
}
// Crypto returns the provider
func (s *Authority) Crypto() *cryptoprov.Crypto {
return s.crypto
}
// AddProfile adds CertProfile
func (s *Authority) AddProfile(label string, p *CertProfile) {
s.profiles[label] = p
}
// Profiles returns profiles map
func (s *Authority) Profiles() map[string]*CertProfile {
return s.profiles
}
// AddIssuer add issuer to the Authority
func (s *Authority) AddIssuer(issuer *Issuer) error {
s.issuers[issuer.Label()] = issuer
s.issuersByKeyID[issuer.SubjectKID()] = issuer
for profileName, profile := range issuer.Profiles() {
if profile.IssuerLabel == "*" {
continue
}
// Maybe this is a redundand check, after config loaded and Validate() call
if is := s.issuersByProfile[profileName]; is != nil {
return errors.Errorf("profile %q is already registered by %q issuer", profileName, is.Label())
}
s.issuersByProfile[profileName] = issuer
}
return nil
}
// GetIssuerByKeyID by IKID
func (s *Authority) GetIssuerByKeyID(ikid string) (*Issuer, error) {
issuer, ok := s.issuersByKeyID[ikid]
if ok {
return issuer, nil
}
return nil, errors.Errorf("issuer not found: %s", ikid)
}
// GetIssuerByLabel by label
func (s *Authority) GetIssuerByLabel(label string) (*Issuer, error) {
issuer, ok := s.issuers[label]
if ok {
return issuer, nil
}
return nil, errors.Errorf("issuer not found: %s", label)
}
// GetIssuerByProfile by profile
func (s *Authority) GetIssuerByProfile(profile string) (*Issuer, error) {
issuer, ok := s.issuersByProfile[profile]
if ok {
return issuer, nil
}
return nil, errors.Errorf("issuer not found for profile: %s", profile)
}
// GetIssuerByKeyHash returns matching Issuer by key hash
func (s *Authority) GetIssuerByKeyHash(alg crypto.Hash, val []byte) (*Issuer, error) {
for _, iss := range s.issuers {
if bytes.Equal(iss.keyHash[alg], val) {
return iss, nil
}
}
return nil, errors.New("issuer not found")
}
// GetIssuerByNameHash returns matching Issuer by name hash
func (s *Authority) GetIssuerByNameHash(alg crypto.Hash, val []byte) (*Issuer, error) {
for _, iss := range s.issuers {
if bytes.Equal(iss.nameHash[alg], val) {
return iss, nil
}
}
return nil, errors.New("issuer not found")
}
// Issuers returns a list of issuers
func (s *Authority) Issuers() []*Issuer {
list := make([]*Issuer, 0, len(s.issuers))
for _, ca := range s.issuers {
list = append(list, ca)
}
return list
}