Debian Codegraph @

svcb/svcb.go

raw
1 // Package svcb deals with all the intricacies of the SVCB/HTTPS RR. All the sub-types ([Pair]) used in
2 // the RR are defined here.
3 package svcb
4
5 import (
6 "encoding/base64"
7 "net/netip"
8 "slices"
9 "strconv"
10 "strings"
11
12 "codeberg.org/miekg/dns/internal/ddd"
13 "codeberg.org/miekg/dns/internal/reverse"
14 )
15
16 // Keys as defined in RFC 9460.
17 const (
18 KeyMandatory uint16 = iota
19 KeyAlpn
20 KeyNoDefaultALPN
21 KeyPort
22 KeyIPv4Hint
23 KeyEchConfig
24 KeyIPv6Hint
25 KeyDohPath // See RFC 9461 Section 5.
26 KeyOhttp // See RFC 9540 Section 8.
27
28 KeyReserved uint16 = 65535
29 )
30
31 // Pair defines a key=value pair for the SVCB RR type. An SVCB RR can have multiple pairs appended to it.
32 // The numerical key code is derived from the type, see [PairToKey].
33 type Pair interface {
34 String() string // String returns the string representation of the value.
35 Len() int // Len returns the length of value in the wire format.
36 Clone() Pair // Clone returns a deep copy of the Pair.
37 }
38
39 // KeyToString return the string representation for k. For KeyReserved the empty string is returned. For
40 // unknown keys "key"+value is returned, see section 2.1 of RFC 9460.
41 func KeyToString(k uint16) string {
42 if k == KeyReserved {
43 return ""
44 }
45 if s, ok := keyToString[k]; ok {
46 return s
47 }
48 return "key" + strconv.Itoa(int(k))
49 }
50
51 var keyToString = map[uint16]string{
52 KeyMandatory: "mandatory",
53 KeyAlpn: "alpn",
54 KeyNoDefaultALPN: "no-default-alpn",
55 KeyPort: "port",
56 KeyIPv4Hint: "ipv4hint",
57 KeyEchConfig: "ech",
58 KeyIPv6Hint: "ipv6hint",
59 KeyDohPath: "dohpath",
60 KeyOhttp: "ohttp",
61 }
62
63 // StringtoKey is the reverse of KeyToString and takes keyXXXX into account.
64 func StringToKey(s string) uint16 {
65 if k, ok := stringToKey[s]; ok {
66 return k
67 }
68 if strings.HasPrefix(s, "key") {
69 k, _ := strconv.Atoi(s[3:])
70 return uint16(k)
71 }
72 return KeyReserved
73 }
74
75 var stringToKey = reverse.Map(keyToString)
76
77 // KeyToPair converts the key value to a Pair constructor function.
78 func KeyToPair(k uint16) func() Pair {
79 switch k {
80 case KeyMandatory:
81 return func() Pair { return new(MANDATORY) }
82 case KeyAlpn:
83 return func() Pair { return new(ALPN) }
84 case KeyNoDefaultALPN:
85 return func() Pair { return new(NODEFAULTALPN) }
86 case KeyPort:
87 return func() Pair { return new(PORT) }
88 case KeyIPv4Hint:
89 return func() Pair { return new(IPV4HINT) }
90 case KeyEchConfig:
91 return func() Pair { return new(ECHCONFIG) }
92 case KeyIPv6Hint:
93 return func() Pair { return new(IPV6HINT) }
94 case KeyDohPath:
95 return func() Pair { return new(DOHPATH) }
96 case KeyOhttp:
97 return func() Pair { return new(OHTTP) }
98 case KeyReserved:
99 return func() Pair { return nil }
100 default:
101 return func() Pair { return &LOCAL{KeyCode: k} }
102 }
103 }
104
105 // PairToKey is the reverse of KeyToPair.
106 func PairToKey(p Pair) uint16 {
107 switch p := p.(type) {
108 case *MANDATORY:
109 return KeyMandatory
110 case *ALPN:
111 return KeyAlpn
112 case *NODEFAULTALPN:
113 return KeyNoDefaultALPN
114 case *PORT:
115 return KeyPort
116 case *IPV4HINT:
117 return KeyIPv4Hint
118 case *ECHCONFIG:
119 return KeyEchConfig
120 case *IPV6HINT:
121 return KeyIPv6Hint
122 case *DOHPATH:
123 return KeyDohPath
124 case *OHTTP:
125 return KeyOhttp
126 case *LOCAL:
127 return p.KeyCode
128 }
129 return KeyReserved
130 }
131
132 // MANDATORY pair adds to required keys that must be interpreted for the RR
133 // to be functional. If ignored, the whole RRSet must be ignored.
134 // "port" and "no-default-alpn" are mandatory by default if present,
135 // so they shouldn't be included here.
136 //
137 // It is incumbent upon the user of this library to reject the RRSet if or avoid constructing such an RRSet that:
138 //
139 // - "mandatory" is included as one of the keys of mandatory
140 // - no key is listed multiple times in mandatory
141 // - all keys listed in mandatory are present
142 // - escape sequences are not used in mandatory
143 // - mandatory, when present, lists at least one key
144 //
145 // Basic use pattern for creating a mandatory option in a SVCB RR, called s:
146 //
147 // s.Value = append(s.Value, &svcb.MANDATORY{})
148 // t := &svcb.ALPN{Alpn: []string{"xmpp-client"}}
149 // s.Value = append(s.Value, t)
150 type MANDATORY struct {
151 Key []uint16
152 }
153
154 func (s *MANDATORY) String() string {
155 str := make([]string, len(s.Key))
156 for i, e := range s.Key {
157 str[i] = KeyToString(e)
158 }
159 return strings.Join(str, ",")
160 }
161
162 func (s *MANDATORY) Len() int { return tlv + 2*len(s.Key) }
163
164 // ALPN pair is used to list supported connection protocols.
165 // The user of this library must ensure that at least one protocol is listed when alpn is present.
166 // Protocol IDs can be found at:
167 // https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
168 // Basic use pattern for creating an ALPN option, in a SVCB RR called s:
169 //
170 // e := svcb.ALPN{Alpn: []string{"h2", "http/1.1"}}
171 // s.Value = append(s.Value, e)
172 type ALPN struct {
173 Alpn []string
174 }
175
176 func (s *ALPN) String() string {
177 // An ALPN value is a comma-separated list of values, each of which can be
178 // an arbitrary binary value. In order to allow parsing, the comma and
179 // backslash characters are themselves escaped.
180 //
181 // However, this escaping is done in addition to the normal escaping which
182 // happens in zone files, meaning that these values must be
183 // double-escaped. This looks terrible, so if you see a never-ending
184 // sequence of backslash in a zone file this may be why.
185 //
186 // https://datatracker.ietf.org/doc/html/draft-ietf-dnsop-svcb-https-08#appendix-A.1
187 var str strings.Builder
188 for i, alpn := range s.Alpn {
189 // 4*len(alpn) is the worst case where we escape every character in the alpn as \123, plus 1 byte for the ',' separating the alpn from others
190 str.Grow(4*len(alpn) + 1)
191 if i > 0 {
192 str.WriteByte(',')
193 }
194 for j := 0; j < len(alpn); j++ {
195 e := alpn[j]
196 if ' ' > e || e > '~' {
197 str.WriteString(ddd.Escape(e))
198 continue
199 }
200 switch e {
201 // We escape a few characters which may confuse humans or parsers.
202 case '"', ';', ' ':
203 str.WriteByte('\\')
204 str.WriteByte(e)
205 // The comma and backslash characters themselves must be
206 // doubly-escaped. We use `\\` for the first backslash and
207 // the escaped numeric value for the other value. We especially
208 // don't want a comma in the output.
209 case ',':
210 str.WriteString(`\\\044`)
211 case '\\':
212 str.WriteString(`\\\092`)
213 default:
214 str.WriteByte(e)
215 }
216 }
217 }
218 return str.String()
219 }
220
221 func (s *ALPN) Len() int {
222 var l int
223 for _, e := range s.Alpn {
224 l += 1 + len(e)
225 }
226 return l + tlv
227 }
228
229 // NODEFAULTALPN pair signifies no support for default connection protocols.
230 // Should be used in conjunction with alpn.
231 // Basic use pattern for creating a no-default-alpn option:
232 //
233 // t := &svcb.ALPN{Alpn: []string{"xmpp-client"}}
234 // s.Value = append(s.Value, t)
235 // e := &svcb.NODEFAULTALPN{}
236 // s.Value = append(s.Value, e)
237 type NODEFAULTALPN struct{}
238
239 func (*NODEFAULTALPN) String() string { return "" }
240 func (*NODEFAULTALPN) Len() int { return tlv + 0 }
241
242 // PORT pair defines the port for connection.
243 // Basic use pattern for creating a port option:
244 //
245 // s.Value = append(s.Value, &svcb.PORT{Port: 80})
246 type PORT struct {
247 Port uint16
248 }
249
250 func (*PORT) Len() int { return tlv + 2 }
251 func (s *PORT) String() string { return strconv.FormatUint(uint64(s.Port), 10) }
252
253 // IPV4HINT pair suggests an IPv4 address which may be used to open connections
254 // if A and AAAA record responses for SVCB's Target domain haven't been received.
255 // In that case, optionally, A and AAAA requests can be made, after which the connection
256 // to the hinted IP address may be terminated and a new connection may be opened.
257 // Basic use pattern for creating an ipv4hint option:
258 //
259 // h := &dns.HTTPS{Hdr: dns.Header{Name: ".", Class: dns.ClassINET}}
260 // e := &svcb.IPV4HINT{Hint: []netip.Addr{netip.MustParseAddr("1.1.1.1")}}
261 //
262 // Or
263 //
264 // e.Hint = []netip.Addr{netip.MustParseAddr("1.1.1.1")}
265 // h.Value = append(h.Value, e)
266 type IPV4HINT struct {
267 Hint []netip.Addr
268 }
269
270 func (s *IPV4HINT) Len() int { return tlv + 4*len(s.Hint) }
271
272 func (s *IPV4HINT) String() string {
273 str := make([]string, len(s.Hint))
274 for i, e := range s.Hint {
275 if !e.IsValid() || !e.Is4() {
276 return "<nil>"
277 }
278 str[i] = e.String()
279 }
280 return strings.Join(str, ",")
281 }
282
283 // ECHCONFIG pair contains the ECHConfig structure defined in draft-ietf-tls-esni [RFC xxxx].
284 // Basic use pattern for creating an ech option:
285 //
286 // h := &dns.HTTPS{Hdr: dns.Header{Name: ".", Class: dns.ClassINET}}
287 // e := &svcb.ECHCONFIG{ECH: []byte{0xFE, 0x08, ...}}
288 // h.Value = append(h.Value, e)
289 type ECHCONFIG struct {
290 ECH []byte // Specifically ECHConfigList including the redundant length prefix.
291 }
292
293 func (s *ECHCONFIG) String() string { return base64.StdEncoding.EncodeToString(s.ECH) }
294 func (s *ECHCONFIG) Len() int { return tlv + len(s.ECH) }
295
296 // IPV6HINT pair suggests an IPv6 address which may be used to open connections
297 // if A and AAAA record responses for SVCB's Target domain haven't been received.
298 // In that case, optionally, A and AAAA requests can be made, after which the
299 // connection to the hinted IP address may be terminated and a new connection may be opened.
300 // Basic use pattern for creating an ipv6hint option:
301 //
302 // h := &dns.HTTPS{Hdr: dns.Header{Name: ".", Class: dns.ClassINET}}
303 // e := &svcb.IPV6HINT{Hint: []netip.Addr{netip.MustParseAddr("2001:db8::1")}}
304 // h.Value = append(h.Value, e)
305 type IPV6HINT struct {
306 Hint []netip.Addr
307 }
308
309 func (s *IPV6HINT) Len() int { return tlv + 16*len(s.Hint) }
310
311 func (s *IPV6HINT) String() string {
312 str := make([]string, len(s.Hint))
313 for i, e := range s.Hint {
314 if e.Is4() {
315 return "<nil>"
316 }
317 str[i] = e.String()
318 }
319 return strings.Join(str, ",")
320 }
321
322 // DOHPATH pair is used to indicate the URI template that the
323 // clients may use to construct a DNS over HTTPS URI.
324 //
325 // See RFC 9461 (https://datatracker.ietf.org/doc/html/rfc9461)
326 // and RFC 9462 (https://datatracker.ietf.org/doc/html/rfc9462).
327 //
328 // A basic example of using the dohpath option together with the alpn
329 // option to indicate support for DNS over HTTPS on a certain path:
330 //
331 // e := &svcb.ALPN{Alpn: []string{"h2", "h3"}}
332 // p := &svcb.DOHPATH{Template: "/dns-query{?dns}"}
333 // s.Value = append(s.Value, e, p)
334 //
335 // The parsing currently doesn't validate that Template is a valid RFC 6570 URI template.
336 type DOHPATH struct {
337 Template string
338 }
339
340 func (s *DOHPATH) String() string { return pairToString([]byte(s.Template)) }
341 func (s *DOHPATH) Len() int { return tlv + len(s.Template) }
342
343 // The "ohttp" SvcParamKey is used to indicate that a service described in a SVCB RR
344 // can be accessed as a target using an associated gateway.
345 // Both the presentation and wire-format values for the "ohttp" parameter MUST be empty.
346 //
347 // See RFC 9460 (https://datatracker.ietf.org/doc/html/rfc9460/)
348 // and RFC 9230 (https://datatracker.ietf.org/doc/html/rfc9230/)
349 //
350 // A basic example of using the dohpath option together with the alpn
351 // option to indicate support for DNS over HTTPS on a certain path:
352 //
353 // e := &dns.ALPN{Alpn: []string{"h2", "h3"}}
354 // p := &svcb.OHTTP{}
355 // s.Value = append(s.Value, e, p)
356 type OHTTP struct{}
357
358 func (*OHTTP) String() string { return "" }
359 func (*OHTTP) Len() int { return tlv + 0 }
360
361 // LOCAL pair is intended for experimental/private use. The key is recommended
362 // to be in the range [65280, 65534], see Section 14.3.2. of RFC 9460.
363 // Basic use pattern for creating a keyNNNNN option:
364 //
365 // h := &dns.HTTPS{Hdr: dns.Header{Name: ".", Class: dns.ClassINET}}
366 // e := &svcb.LOCAL{KeyCode: 65400, Data: []byte("abc")}
367 // h.Value = append(h.Value, e)
368 type LOCAL struct {
369 KeyCode uint16 // Just like as in RFC 5559.
370 Data []byte // All byte sequences are allowed.
371 }
372
373 func (s *LOCAL) String() string { return pairToString(s.Data) }
374 func (s *LOCAL) Len() int { return tlv + len(s.Data) }
375
376 func (s *MANDATORY) Clone() Pair { return &MANDATORY{slices.Clone(s.Key)} }
377 func (s *ALPN) Clone() Pair { return &ALPN{slices.Clone(s.Alpn)} }
378 func (*NODEFAULTALPN) Clone() Pair { return &NODEFAULTALPN{} }
379 func (s *PORT) Clone() Pair { return &PORT{s.Port} }
380 func (s *ECHCONFIG) Clone() Pair { return &ECHCONFIG{slices.Clone(s.ECH)} }
381 func (*OHTTP) Clone() Pair { return &OHTTP{} }
382 func (s *DOHPATH) Clone() Pair { return &DOHPATH{Template: s.Template} }
383 func (s *LOCAL) Clone() Pair { return &LOCAL{s.KeyCode, slices.Clone(s.Data)} }
384
385 func (s *IPV4HINT) Clone() Pair {
386 return &IPV4HINT{Hint: slices.Clone(s.Hint)}
387 }
388
389 func (s *IPV6HINT) Clone() Pair {
390 return &IPV6HINT{Hint: slices.Clone(s.Hint)}
391 }
392
393 const tlv = 4