forked from Venafi/vcert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificatePolicies.go
150 lines (139 loc) · 5.36 KB
/
certificatePolicies.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
/*
* 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 cloud
import (
"github.com/Venafi/vcert/pkg/endpoint"
"regexp"
"strings"
"time"
)
type certificatePolicy struct {
CertificatePolicyType certificatePolicyType `json:"certificatePolicyType,omitempty"`
ID string `json:"id,omitempty"`
CompanyID string `json:"companyId,omitempty"`
Name string `json:"name,omitempty"`
SystemGenerated bool `json:"systemGeneratedate,omitempty"`
CreationDateString string `json:"creationDate,omitempty"`
CreationDate time.Time `json:"-"`
CertificateProviderID string `json:"certificateProviderId,omitempty"`
SubjectCNRegexes []string `json:"subjectCNRegexes,omitempty"`
SubjectORegexes []string `json:"subjectORegexes,omitempty"`
SubjectOURegexes []string `json:"subjectOURegexes,omitempty"`
SubjectSTRegexes []string `json:"subjectSTRegexes,omitempty"`
SubjectLRegexes []string `json:"subjectLRegexes,omitempty"`
SubjectCRegexes []string `json:"subjectCValues,omitempty"`
SANRegexes []string `json:"sanRegexes,omitempty"`
KeyTypes []allowedKeyType `json:"keyTypes,omitempty"`
KeyReuse bool `json:"keyReuse,omitempty"`
}
type allowedKeyType struct {
KeyType keyType
KeyLengths []int
}
type certificatePolicyType string
const (
certificatePolicyTypeIdentity certificatePolicyType = "CERTIFICATE_IDENTITY"
certificatePolicyTypeUse = "CERTIFICATE_USE"
)
type keyType string
func (cp certificatePolicy) toPolicy() (p endpoint.Policy) {
addStartEnd := func(s string) string {
if !strings.HasPrefix(s, "^") {
s = "^" + s
}
if !strings.HasSuffix(s, "$") {
s = s + "$"
}
return s
}
addStartEndToArray := func(ss []string) []string {
a := make([]string, len(ss))
for i, s := range ss {
a[i] = addStartEnd(s)
}
return a
}
p.SubjectCNRegexes = addStartEndToArray(cp.SubjectCNRegexes)
p.SubjectOURegexes = addStartEndToArray(cp.SubjectOURegexes)
p.SubjectCRegexes = addStartEndToArray(cp.SubjectCRegexes)
p.SubjectSTRegexes = addStartEndToArray(cp.SubjectSTRegexes)
p.SubjectLRegexes = addStartEndToArray(cp.SubjectLRegexes)
p.SubjectORegexes = addStartEndToArray(cp.SubjectORegexes)
p.DnsSanRegExs = addStartEndToArray(cp.SANRegexes)
p.AllowKeyReuse = cp.KeyReuse
allowWildCards := false
for _, s := range p.SubjectCNRegexes {
if strings.HasPrefix(s, "^.*") {
allowWildCards = true
}
}
if !allowWildCards {
for _, s := range p.DnsSanRegExs {
if strings.HasPrefix(s, "^.*") {
allowWildCards = true
}
}
}
p.AllowWildcards = allowWildCards
for _, kt := range cp.KeyTypes {
keyConfiguration := endpoint.AllowedKeyConfiguration{}
if err := keyConfiguration.KeyType.Set(string(kt.KeyType)); err != nil {
panic(err)
}
keyConfiguration.KeySizes = kt.KeyLengths[:]
p.AllowedKeyConfigurations = append(p.AllowedKeyConfigurations, keyConfiguration)
}
return
}
func isNotRegexp(s string) bool {
matched, err := regexp.MatchString(`[a-zA-Z0-9 ]+`, s)
if !matched || err != nil {
return false
}
return true
}
func (cp certificatePolicy) toZoneConfig(zc *endpoint.ZoneConfiguration) {
if len(cp.SubjectCRegexes) > 0 && isNotRegexp(cp.SubjectCRegexes[0]) {
zc.Country = cp.SubjectCRegexes[0]
}
if len(cp.SubjectORegexes) > 0 && isNotRegexp(cp.SubjectORegexes[0]) {
zc.Organization = cp.SubjectORegexes[0]
}
if len(cp.SubjectSTRegexes) > 0 && isNotRegexp(cp.SubjectSTRegexes[0]) {
zc.Province = cp.SubjectSTRegexes[0]
}
if len(cp.SubjectLRegexes) > 0 && isNotRegexp(cp.SubjectLRegexes[0]) {
zc.Locality = cp.SubjectLRegexes[0]
}
for _, ou := range cp.SubjectOURegexes {
if isNotRegexp(ou) {
zc.OrganizationalUnit = append(zc.OrganizationalUnit, ou)
}
}
}
const (
keyTypeRSA keyType = "RSA"
keyTypeDSA = "DSA"
keyTypeEC = "EC"
keyTypeGost3410 = "GOST3410"
keyTypeECGost3410 = "ECGOST3410"
keyTypeReserved3 = "RESERVED3"
keyTypeUnknown = "UNKNOWN"
)
/*
"signatureAlgorithm":{"type":"string","enum":["MD2_WITH_RSA_ENCRYPTION","MD5_WITH_RSA_ENCRYPTION","SHA1_WITH_RSA_ENCRYPTION","SHA1_WITH_RSA_ENCRYPTION2","SHA256_WITH_RSA_ENCRYPTION","SHA384_WITH_RSA_ENCRYPTION","SHA512_WITH_RSA_ENCRYPTION","ID_DSA_WITH_SHA1","dsaWithSHA1","EC_DSA_WITH_SHA1","EC_DSA_WITH_SHA224","EC_DSA_WITH_SHA256","EC_DSA_WITH_SHA384","EC_DSA_WITH_SHA512","UNKNOWN","SHA1_WITH_RSAandMGF1","GOST_R3411_94_WITH_GOST_R3410_2001","GOST_R3411_94_WITH_GOST_R3410_94"]},
"signatureHashAlgorithm":{"type":"string","enum":["MD5","SHA1","MD2","SHA224","SHA256","SHA384","SHA512","UNKNOWN","GOSTR3411_94"]}
*/