-
Notifications
You must be signed in to change notification settings - Fork 5
/
caddyfile.go
181 lines (166 loc) · 4.75 KB
/
caddyfile.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
package revocation
import (
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/gr33nbl00d/caddy-revocation-validator/config"
"strconv"
)
type CertRevocationValidatorConfig struct {
CRLConfig *config.CRLConfig
OCSPConfig *config.OCSPConfig
Mode string
}
func parseConfigFromCaddyfile(d *caddyfile.Dispenser) (*CertRevocationValidatorConfig, error) {
// initialize structs
crlConfig := config.CRLConfig{
CDPConfig: &config.CDPConfig{},
CRLUrls: []string{},
CRLFiles: []string{},
TrustedSignatureCertsFiles: []string{},
}
ocspConfig := config.OCSPConfig{
TrustedResponderCertsFiles: []string{},
}
certRevocationValidatorConfig := CertRevocationValidatorConfig{
OCSPConfig: &ocspConfig,
CRLConfig: &crlConfig,
}
for d.Next() {
for nesting := d.Nesting(); d.NextBlock(nesting); {
key := d.Val()
validatorConfig, err, done := parseConfigEntryFromCaddyfile(d, key, certRevocationValidatorConfig)
if done {
return validatorConfig, err
}
}
}
return &certRevocationValidatorConfig, nil
}
func parseConfigEntryFromCaddyfile(d *caddyfile.Dispenser, key string, certRevocationValidatorConfig CertRevocationValidatorConfig) (*CertRevocationValidatorConfig, error, bool) {
switch key {
case "mode":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
certRevocationValidatorConfig.Mode = d.Val()
case "crl_config":
crlConfig, err := parseCaddyfileCRLConfig(d)
if err != nil {
return nil, err, true
}
certRevocationValidatorConfig.CRLConfig = crlConfig
case "ocsp_config":
ocspConfig, err := parseCaddyfileOCSPConfig(d)
if err != nil {
return nil, err, true
}
certRevocationValidatorConfig.OCSPConfig = ocspConfig
default:
return nil, d.Errf("unknown subdirective for the revocation verifier: %s", key), true
}
return nil, nil, false
}
func parseCaddyfileOCSPConfig(d *caddyfile.Dispenser) (*config.OCSPConfig, error) {
ocspConfig := config.OCSPConfig{}
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "default_cache_duration":
if !d.NextArg() {
return nil, d.ArgErr()
}
ocspConfig.DefaultCacheDuration = d.Val()
case "trusted_responder_cert_file":
if !d.NextArg() {
return nil, d.ArgErr()
}
ocspConfig.TrustedResponderCertsFiles = append(ocspConfig.TrustedResponderCertsFiles, d.Val())
case "ocsp_aia_strict":
if !d.NextArg() {
return nil, d.ArgErr()
}
ocspConfig.OCSPAIAStrict = false
default:
return nil, d.Errf("unknown subdirective for the ocsp config in the revocation verifier: %s", d.Val())
}
}
return &ocspConfig, nil
}
func parseCaddyfileCRLConfig(d *caddyfile.Dispenser) (*config.CRLConfig, error) {
crlConfig := config.CRLConfig{}
for nesting := d.Nesting(); d.NextBlock(nesting); {
c, err, done := parseCaddyFileCrlConfigEntry(d, crlConfig)
if done {
return c, err
}
}
return &crlConfig, nil
}
func parseCaddyFileCrlConfigEntry(d *caddyfile.Dispenser, crlConfig config.CRLConfig) (*config.CRLConfig, error, bool) {
switch d.Val() {
case "work_dir":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
crlConfig.WorkDir = d.Val()
case "cdp_config":
cdpConfig, err := parseCaddyfileCRLCDPConfig(d)
if err != nil {
return nil, err, true
}
crlConfig.CDPConfig = cdpConfig
case "storage_type":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
crlConfig.StorageType = d.Val()
case "update_interval":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
crlConfig.UpdateInterval = d.Val()
case "signature_validation_mode":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
crlConfig.SignatureValidationMode = d.Val()
case "crl_url":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
crlConfig.CRLUrls = append(crlConfig.CRLUrls, d.Val())
case "crl_file":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
crlConfig.CRLFiles = append(crlConfig.CRLFiles, d.Val())
case "trusted_signature_cert_file":
if !d.NextArg() {
return nil, d.ArgErr(), true
}
crlConfig.TrustedSignatureCertsFiles = append(crlConfig.TrustedSignatureCertsFiles, d.Val())
default:
return nil, d.Errf("unknown subdirective for the crl config in the revocation verifier: %s", d.Val()), true
}
return nil, nil, false
}
func parseCaddyfileCRLCDPConfig(d *caddyfile.Dispenser) (*config.CDPConfig, error) {
cdpConfig := config.CDPConfig{}
for nesting := d.Nesting(); d.NextBlock(nesting); {
switch d.Val() {
case "crl_fetch_mode":
if !d.NextArg() {
return nil, d.ArgErr()
}
cdpConfig.CRLFetchMode = d.Val()
case "crl_cdp_strict":
if !d.NextArg() {
return nil, d.ArgErr()
}
b, err := strconv.ParseBool(d.Val())
if err != nil {
return nil, d.ArgErr()
}
cdpConfig.CRLCDPStrict = b
}
}
return &cdpConfig, nil
}