Debian Codegraph @

caddyconfig/httpcaddyfile/pkiapp.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package httpcaddyfile
16
17 import (
18 "slices"
19 "strconv"
20
21 "github.com/caddyserver/caddy/v2"
22 "github.com/caddyserver/caddy/v2/caddyconfig"
23 "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
24 "github.com/caddyserver/caddy/v2/modules/caddypki"
25 )
26
27 func init() {
28 RegisterGlobalOption("pki", parsePKIApp)
29 }
30
31 // parsePKIApp parses the global pki option. Syntax:
32 //
33 // pki {
34 // ca [<id>] {
35 // name <name>
36 // root_cn <name>
37 // intermediate_cn <name>
38 // intermediate_lifetime <duration>
39 // maintenance_interval <duration>
40 // renewal_window_ratio <ratio>
41 // root {
42 // cert <path>
43 // key <path>
44 // format <format>
45 // }
46 // intermediate {
47 // cert <path>
48 // key <path>
49 // format <format>
50 // }
51 // }
52 // }
53 //
54 // When the CA ID is unspecified, 'local' is assumed.
55 func parsePKIApp(d *caddyfile.Dispenser, existingVal any) (any, error) {
56 d.Next() // consume app name
57
58 pki := &caddypki.PKI{
59 CAs: make(map[string]*caddypki.CA),
60 }
61 for d.NextBlock(0) {
62 switch d.Val() {
63 case "ca":
64 pkiCa := new(caddypki.CA)
65 if d.NextArg() {
66 pkiCa.ID = d.Val()
67 if d.NextArg() {
68 return nil, d.ArgErr()
69 }
70 }
71 if pkiCa.ID == "" {
72 pkiCa.ID = caddypki.DefaultCAID
73 }
74
75 for nesting := d.Nesting(); d.NextBlock(nesting); {
76 switch d.Val() {
77 case "name":
78 if !d.NextArg() {
79 return nil, d.ArgErr()
80 }
81 pkiCa.Name = d.Val()
82
83 case "root_cn":
84 if !d.NextArg() {
85 return nil, d.ArgErr()
86 }
87 pkiCa.RootCommonName = d.Val()
88
89 case "intermediate_cn":
90 if !d.NextArg() {
91 return nil, d.ArgErr()
92 }
93 pkiCa.IntermediateCommonName = d.Val()
94
95 case "intermediate_lifetime":
96 if !d.NextArg() {
97 return nil, d.ArgErr()
98 }
99 dur, err := caddy.ParseDuration(d.Val())
100 if err != nil {
101 return nil, err
102 }
103 pkiCa.IntermediateLifetime = caddy.Duration(dur)
104
105 case "maintenance_interval":
106 if !d.NextArg() {
107 return nil, d.ArgErr()
108 }
109 dur, err := caddy.ParseDuration(d.Val())
110 if err != nil {
111 return nil, err
112 }
113 pkiCa.MaintenanceInterval = caddy.Duration(dur)
114
115 case "renewal_window_ratio":
116 if !d.NextArg() {
117 return nil, d.ArgErr()
118 }
119 ratio, err := strconv.ParseFloat(d.Val(), 64)
120 if err != nil || ratio <= 0 || ratio > 1 {
121 return nil, d.Errf("renewal_window_ratio must be a number in (0, 1], got %s", d.Val())
122 }
123 pkiCa.RenewalWindowRatio = ratio
124
125 case "root":
126 if pkiCa.Root == nil {
127 pkiCa.Root = new(caddypki.KeyPair)
128 }
129 for nesting := d.Nesting(); d.NextBlock(nesting); {
130 switch d.Val() {
131 case "cert":
132 if !d.NextArg() {
133 return nil, d.ArgErr()
134 }
135 pkiCa.Root.Certificate = d.Val()
136
137 case "key":
138 if !d.NextArg() {
139 return nil, d.ArgErr()
140 }
141 pkiCa.Root.PrivateKey = d.Val()
142
143 case "format":
144 if !d.NextArg() {
145 return nil, d.ArgErr()
146 }
147 pkiCa.Root.Format = d.Val()
148
149 default:
150 return nil, d.Errf("unrecognized pki ca root option '%s'", d.Val())
151 }
152 }
153
154 case "intermediate":
155 if pkiCa.Intermediate == nil {
156 pkiCa.Intermediate = new(caddypki.KeyPair)
157 }
158 for nesting := d.Nesting(); d.NextBlock(nesting); {
159 switch d.Val() {
160 case "cert":
161 if !d.NextArg() {
162 return nil, d.ArgErr()
163 }
164 pkiCa.Intermediate.Certificate = d.Val()
165
166 case "key":
167 if !d.NextArg() {
168 return nil, d.ArgErr()
169 }
170 pkiCa.Intermediate.PrivateKey = d.Val()
171
172 case "format":
173 if !d.NextArg() {
174 return nil, d.ArgErr()
175 }
176 pkiCa.Intermediate.Format = d.Val()
177
178 default:
179 return nil, d.Errf("unrecognized pki ca intermediate option '%s'", d.Val())
180 }
181 }
182
183 default:
184 return nil, d.Errf("unrecognized pki ca option '%s'", d.Val())
185 }
186 }
187
188 pki.CAs[pkiCa.ID] = pkiCa
189
190 default:
191 return nil, d.Errf("unrecognized pki option '%s'", d.Val())
192 }
193 }
194 return pki, nil
195 }
196
197 func (st ServerType) buildPKIApp(
198 pairings []sbAddrAssociation,
199 options map[string]any,
200 warnings []caddyconfig.Warning,
201 ) (*caddypki.PKI, []caddyconfig.Warning, error) {
202 skipInstallTrust := false
203 if _, ok := options["skip_install_trust"]; ok {
204 skipInstallTrust = true
205 }
206
207 // check if auto_https is off - in that case we should not create
208 // any PKI infrastructure even with skip_install_trust directive
209 autoHTTPS := []string{}
210 if ah, ok := options["auto_https"].([]string); ok {
211 autoHTTPS = ah
212 }
213 autoHTTPSOff := slices.Contains(autoHTTPS, "off")
214
215 falseBool := false
216
217 // Load the PKI app configured via global options
218 var pkiApp *caddypki.PKI
219 unwrappedPki, ok := options["pki"].(*caddypki.PKI)
220 if ok {
221 pkiApp = unwrappedPki
222 } else {
223 pkiApp = &caddypki.PKI{CAs: make(map[string]*caddypki.CA)}
224 }
225 for _, ca := range pkiApp.CAs {
226 if skipInstallTrust {
227 ca.InstallTrust = &falseBool
228 }
229 pkiApp.CAs[ca.ID] = ca
230 }
231
232 // Add in the CAs configured via directives
233 for _, p := range pairings {
234 for _, sblock := range p.serverBlocks {
235 // find all the CAs that were defined and add them to the app config
236 // i.e. from any "acme_server" directives
237 for _, caCfgValue := range sblock.pile["pki.ca"] {
238 ca := caCfgValue.Value.(*caddypki.CA)
239 if skipInstallTrust {
240 ca.InstallTrust = &falseBool
241 }
242
243 // the CA might already exist from global options, so
244 // don't overwrite it in that case
245 if _, ok := pkiApp.CAs[ca.ID]; !ok {
246 pkiApp.CAs[ca.ID] = ca
247 }
248 }
249 }
250 }
251
252 // if there was no CAs defined in any of the servers,
253 // and we were requested to not install trust, then
254 // add one for the default/local CA to do so
255 // only if auto_https is not completely disabled
256 if len(pkiApp.CAs) == 0 && skipInstallTrust && !autoHTTPSOff {
257 ca := new(caddypki.CA)
258 ca.ID = caddypki.DefaultCAID
259 ca.InstallTrust = &falseBool
260 pkiApp.CAs[ca.ID] = ca
261 }
262
263 return pkiApp, warnings, nil
264 }