forked from Venafi/vcert
/
endpoint.go
320 lines (291 loc) · 10.5 KB
/
endpoint.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
/*
* Copyright 2018 Venafi, Inc.
*
* 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.
*/
package endpoint
import (
"crypto/x509"
"fmt"
"github.com/Venafi/vcert/pkg/certificate"
"log"
"regexp"
"sort"
"strings"
)
// ConnectorType represents the available connectors
type ConnectorType int
const (
ConnectorTypeUndefined ConnectorType = iota
// ConnectorTypeFake is a fake connector for tests
ConnectorTypeFake
// ConnectorTypeCloud represents the Cloud connector type
ConnectorTypeCloud
// ConnectorTypeTPP represents the TPP connector type
ConnectorTypeTPP
)
func (t ConnectorType) String() string {
switch t {
case ConnectorTypeUndefined:
return "Undefined Endpoint"
case ConnectorTypeFake:
return "Fake Endpoint"
case ConnectorTypeCloud:
return "Venafi Cloud"
case ConnectorTypeTPP:
return "TPP"
default:
return fmt.Sprintf("unexpected connector type: %d", t)
}
}
// Connector provides a common interface for external communications with TPP or Venafi Cloud
type Connector interface {
GetType() ConnectorType
SetBaseURL(url string) (err error)
SetZone(z string)
Ping() (err error)
Register(email string) (err error)
Authenticate(auth *Authentication) (err error)
ReadZoneConfiguration(zone string) (config *ZoneConfiguration, err error)
GenerateRequest(config *ZoneConfiguration, req *certificate.Request) (err error)
RequestCertificate(req *certificate.Request, zone string) (requestID string, err error)
RetrieveCertificate(req *certificate.Request) (certificates *certificate.PEMCollection, err error)
RevokeCertificate(req *certificate.RevocationRequest) error
RenewCertificate(req *certificate.RenewalRequest) (requestID string, err error)
ImportCertificate(req *certificate.ImportRequest) (*certificate.ImportResponse, error)
ReadPolicyConfiguration(zone string) (policy *Policy, err error)
}
// Authentication provides a data construct for authentication data
type Authentication struct {
User string
Password string
APIKey string
}
// ErrRetrieveCertificateTimeout provides a common error structure for a timeout while retrieving a certificate
type ErrRetrieveCertificateTimeout struct {
CertificateID string
}
func (err ErrRetrieveCertificateTimeout) Error() string {
return fmt.Sprintf("Operation timed out. You may try retrieving the certificate later using Pickup ID: %s", err.CertificateID)
}
// ErrCertificatePending provides a common error structure for a timeout while retrieving a certificate
type ErrCertificatePending struct {
CertificateID string
Status string
}
func (err ErrCertificatePending) Error() string {
if err.Status == "" {
return fmt.Sprintf("Issuance is pending. You may try retrieving the certificate later using Pickup ID: %s", err.CertificateID)
}
return fmt.Sprintf("Issuance is pending. You may try retrieving the certificate later using Pickup ID: %s\n\tStatus: %s", err.CertificateID, err.Status)
}
type Policy struct {
SubjectCNRegexes []string
SubjectORegexes []string
SubjectOURegexes []string
SubjectSTRegexes []string
SubjectLRegexes []string
SubjectCRegexes []string
AllowedKeyConfigurations []AllowedKeyConfiguration
DnsSanRegExs []string
IpSanRegExs []string
EmailSanRegExs []string
UriSanRegExs []string
UpnSanRegExs []string
AllowWildcards bool
AllowKeyReuse bool
}
// ZoneConfiguration provides a common structure for certificate request data provided by the remote endpoint
type ZoneConfiguration struct {
Organization string
OrganizationalUnit []string
Country string
Province string
Locality string
Policy
HashAlgorithm x509.SignatureAlgorithm
CustomAttributeValues map[string]string
}
// AllowedKeyConfiguration contains an allowed key type with its sizes or curves
type AllowedKeyConfiguration struct {
KeyType certificate.KeyType
KeySizes []int
KeyCurves []certificate.EllipticCurve
}
// NewZoneConfiguration creates a new zone configuration which creates the map used in the configuration
func NewZoneConfiguration() *ZoneConfiguration {
zc := ZoneConfiguration{}
zc.CustomAttributeValues = make(map[string]string)
return &zc
}
// ValidateCertificateRequest validates the request against the zone configuration
func (z *ZoneConfiguration) ValidateCertificateRequest(request *certificate.Request) error {
if !isComponentValid(z.SubjectCNRegexes, []string{request.Subject.CommonName}) {
return fmt.Errorf("The requested CN does not match any of the allowed CN regular expressions")
}
if !isComponentValid(z.SubjectORegexes, request.Subject.Organization) {
return fmt.Errorf("The requested Organization does not match any of the allowed Organization regular expressions")
}
if !isComponentValid(z.SubjectOURegexes, request.Subject.OrganizationalUnit) {
return fmt.Errorf("The requested Organizational Unit does not match any of the allowed Organization Unit regular expressions")
}
if !isComponentValid(z.SubjectSTRegexes, request.Subject.Province) {
return fmt.Errorf("The requested State/Province does not match any of the allowed State/Province regular expressions")
}
if !isComponentValid(z.SubjectLRegexes, request.Subject.Locality) {
return fmt.Errorf("The requested Locality does not match any of the allowed Locality regular expressions")
}
if !isComponentValid(z.SubjectCRegexes, request.Subject.Country) {
return fmt.Errorf("The requested Country does not match any of the allowed Country regular expressions")
}
if !isComponentValid(z.DnsSanRegExs, request.DNSNames) {
return fmt.Errorf("The requested Subject Alternative Name does not match any of the allowed Country regular expressions")
}
//todo: add ip, email and over cheking
if z.AllowedKeyConfigurations != nil && len(z.AllowedKeyConfigurations) > 0 {
match := false
for _, keyConf := range z.AllowedKeyConfigurations {
if keyConf.KeyType == request.KeyType {
if request.KeyLength > 0 {
for _, size := range keyConf.KeySizes {
if size == request.KeyLength {
match = true
break
}
}
} else {
match = true
}
}
if match {
break
}
}
if !match {
return fmt.Errorf("The requested Key Type and Size do not match any of the allowed Key Types and Sizes")
}
}
return nil
}
func isComponentValid(regexes []string, component []string) bool {
if len(regexes) == 0 || len(component) == 0 {
return true
}
regexOk := false
for _, subReg := range regexes {
matchedAny := false
reg, err := regexp.Compile(subReg)
if err != nil {
log.Printf("Bad regexp: %s", subReg)
return false
}
for _, c := range component {
if reg.FindStringIndex(c) != nil {
matchedAny = true
break
}
}
if matchedAny {
regexOk = true
break
}
}
return regexOk
}
// UpdateCertificateRequest updates a certificate request based on the zone configurataion retrieved from the remote endpoint
func (z *ZoneConfiguration) UpdateCertificateRequest(request *certificate.Request) {
if len(request.Subject.Organization) == 0 && z.Organization != "" {
request.Subject.Organization = []string{z.Organization}
} else if len(request.Subject.Organization) > 0 && !strings.EqualFold(request.Subject.Organization[0], z.Organization) {
request.Subject.Organization = []string{z.Organization}
}
if len(request.Subject.OrganizationalUnit) == 0 && z.OrganizationalUnit != nil {
request.Subject.OrganizationalUnit = z.OrganizationalUnit
}
if len(request.Subject.Country) == 0 && z.Country != "" {
request.Subject.Country = []string{z.Country}
} else if len(request.Subject.Country) > 0 && !strings.EqualFold(request.Subject.Country[0], z.Country) {
request.Subject.Country = []string{z.Country}
}
if len(request.Subject.Province) == 0 && z.Province != "" {
request.Subject.Province = []string{z.Province}
} else if len(request.Subject.Province) > 0 && !strings.EqualFold(request.Subject.Province[0], z.Province) {
request.Subject.Province = []string{z.Province}
}
if len(request.Subject.Locality) == 0 && z.Locality != "" {
request.Subject.Locality = []string{z.Locality}
} else if len(request.Subject.Locality) > 0 && !strings.EqualFold(request.Subject.Locality[0], z.Locality) {
request.Subject.Locality = []string{z.Locality}
}
if z.HashAlgorithm != x509.UnknownSignatureAlgorithm {
request.SignatureAlgorithm = z.HashAlgorithm
} else {
request.SignatureAlgorithm = x509.SHA256WithRSA
}
if len(z.AllowedKeyConfigurations) != 0 {
foundMatch := false
for _, keyConf := range z.AllowedKeyConfigurations {
if keyConf.KeyType == request.KeyType {
foundMatch = true
switch request.KeyType {
case certificate.KeyTypeECDSA:
if len(keyConf.KeyCurves) != 0 {
request.KeyCurve = keyConf.KeyCurves[0]
} else {
request.KeyCurve = certificate.EllipticCurveDefault
}
case certificate.KeyTypeRSA:
if len(keyConf.KeySizes) != 0 {
sizeOK := false
for _, size := range keyConf.KeySizes {
if size == request.KeyLength {
sizeOK = true
}
}
if !sizeOK {
sort.Sort(sort.Reverse(sort.IntSlice(keyConf.KeySizes)))
request.KeyLength = keyConf.KeySizes[0]
}
} else {
request.KeyLength = 2048
}
}
}
}
if !foundMatch {
configuration := z.AllowedKeyConfigurations[0]
request.KeyType = configuration.KeyType
switch request.KeyType {
case certificate.KeyTypeECDSA:
if len(configuration.KeyCurves) != 0 {
request.KeyCurve = configuration.KeyCurves[0]
} else {
request.KeyCurve = certificate.EllipticCurveDefault
}
case certificate.KeyTypeRSA:
if len(configuration.KeySizes) != 0 {
sort.Sort(sort.Reverse(sort.IntSlice(configuration.KeySizes)))
request.KeyLength = configuration.KeySizes[0]
} else {
request.KeyLength = 2048
}
}
}
} else {
// Zone config has no key length parameters, so we just pass user's -key-size or fall to default 2048
if request.KeyType == certificate.KeyTypeRSA && request.KeyLength == 0 {
request.KeyLength = 2048
}
}
return
}