Debian Codegraph @

svcb/pack.go

raw
1 package svcb
2
3 import (
4 "errors"
5 "fmt"
6
7 "codeberg.org/miekg/dns/internal/pack"
8 "codeberg.org/miekg/dns/internal/unpack"
9 "golang.org/x/crypto/cryptobyte"
10 )
11
12 // _pack converts a pair to wire-format.
13 func _pack(p Pair, msg []byte, off int) (int, error) {
14 switch x := p.(type) {
15 case *MANDATORY:
16 return x.pack(msg, off)
17 case *ALPN:
18 return x.pack(msg, off)
19 case *NODEFAULTALPN:
20 return x.pack(msg, off)
21 case *PORT:
22 return x.pack(msg, off)
23 case *IPV4HINT:
24 return x.pack(msg, off)
25 case *ECHCONFIG:
26 return x.pack(msg, off)
27 case *IPV6HINT:
28 return x.pack(msg, off)
29 case *DOHPATH:
30 return x.pack(msg, off)
31 case *OHTTP:
32 return x.pack(msg, off)
33 case *LOCAL:
34 return x.pack(msg, off)
35 }
36 return 0, fmt.Errorf("dns: no svcb pack defined")
37 }
38
39 // _unpack converts wire-format to a pair. Only exported to make it available to the dns unpacker.
40 func _unpack(p Pair, data *cryptobyte.String) error {
41 switch x := p.(type) {
42 case *MANDATORY:
43 return x.unpack(data)
44 case *ALPN:
45 return x.unpack(data)
46 case *NODEFAULTALPN:
47 return x.unpack(data)
48 case *PORT:
49 return x.unpack(data)
50 case *IPV4HINT:
51 return x.unpack(data)
52 case *ECHCONFIG:
53 return x.unpack(data)
54 case *IPV6HINT:
55 return x.unpack(data)
56 case *DOHPATH:
57 return x.unpack(data)
58 case *OHTTP:
59 return x.unpack(data)
60 case *LOCAL:
61 return x.unpack(data)
62 }
63 return fmt.Errorf("dns: no svcb unpack defined")
64 }
65
66 func (s *MANDATORY) pack(msg []byte, off int) (off1 int, err error) {
67 off, err = packTLV(s, msg, off)
68 if err != nil {
69 return len(msg), err
70 }
71 for _, k := range s.Key {
72 if off, err = pack.Uint16(k, msg, off); err != nil {
73 return len(msg), err
74 }
75 }
76 return off, nil
77 }
78
79 func (s *MANDATORY) unpack(sc *cryptobyte.String) error {
80 for !sc.Empty() {
81 var key uint16
82 if !sc.ReadUint16(&key) {
83 return errors.New("dns: svcbmandatory: value length is not a multiple of 2")
84 }
85 s.Key = append(s.Key, key)
86 }
87 return nil
88 }
89
90 func (s *ALPN) pack(msg []byte, off int) (off1 int, err error) {
91 off, err = packTLV(s, msg, off)
92 if err != nil {
93 return len(msg), err
94 }
95 for _, e := range s.Alpn {
96 if e == "" {
97 return len(msg), errors.New("dns: svcbalpn: empty alpn-id")
98 }
99 if len(e) > 255 {
100 return len(msg), errors.New("dns: svcbalpn: alpn-id too long")
101 }
102
103 if off, err = pack.Uint8(byte(len(e)), msg, off); err != nil {
104 return len(msg), err
105 }
106 if off, err = pack.StringAny(e, msg, off); err != nil {
107 return len(msg), err
108 }
109 }
110 return off, nil
111 }
112
113 func (s *ALPN) unpack(sc *cryptobyte.String) error {
114 for !sc.Empty() {
115 var data cryptobyte.String
116 if !sc.ReadUint8LengthPrefixed(&data) {
117 return fmt.Errorf("dns: overflow unpacking data")
118 }
119 s.Alpn = append(s.Alpn, string(data))
120 }
121 return nil
122 }
123
124 func (s *NODEFAULTALPN) pack(msg []byte, off int) (int, error) {
125 off, err := packTLV(s, msg, off)
126 return off, err
127 }
128
129 func (*NODEFAULTALPN) unpack(sc *cryptobyte.String) error {
130 if !sc.Empty() {
131 return errors.New("dns: svcbnodefaultalpn: no-default-alpn must have no value")
132 }
133 return nil
134 }
135
136 func (s *PORT) pack(msg []byte, off int) (off1 int, err error) {
137 off, err = packTLV(s, msg, off)
138 if err != nil {
139 return off, err
140 }
141 off, err = pack.Uint16(s.Port, msg, off)
142 return off, err
143 }
144
145 func (s *PORT) unpack(sc *cryptobyte.String) error {
146 if !sc.ReadUint16(&s.Port) {
147 return errors.New("dns: svcbport: port length is not exactly 2 octets")
148 }
149 return nil
150 }
151
152 func (s *IPV4HINT) pack(msg []byte, off int) (int, error) {
153 off, err := packTLV(s, msg, off)
154 if err != nil {
155 return off, err
156 }
157 for _, ip := range s.Hint {
158 off, err = pack.A(ip, msg, off)
159 if err != nil {
160 return off, err
161 }
162 }
163 return off, nil
164 }
165
166 func (s *IPV4HINT) unpack(sc *cryptobyte.String) error {
167 for !sc.Empty() {
168 ip, err := unpack.A(sc)
169 if err != nil {
170 return errors.New("dns: svcbipv4hint: ipv4 address byte array length is not a multiple of 4")
171 }
172 s.Hint = append(s.Hint, ip)
173 }
174 return nil
175 }
176
177 func (s *ECHCONFIG) pack(msg []byte, off int) (int, error) {
178 off, err := packTLV(s, msg, off)
179 if err != nil {
180 return off, err
181 }
182 if len(s.ECH) > len(msg) {
183 return off, errors.New("dns: svcbechconfig: overflow packing")
184 }
185 n := copy(msg[off:], s.ECH)
186 return off + n, nil
187 }
188
189 func (s *ECHCONFIG) unpack(sc *cryptobyte.String) error {
190 s.ECH = make([]byte, len([]byte(*sc)))
191 if !sc.CopyBytes(s.ECH) {
192 return errors.New("dns: svcbechconfig overflow unpacking")
193 }
194 return nil
195 }
196
197 func (s *IPV6HINT) pack(msg []byte, off int) (int, error) {
198 off, err := packTLV(s, msg, off)
199 if err != nil {
200 return off, err
201 }
202 for _, ip := range s.Hint {
203 off, err = pack.AAAA(ip, msg, off)
204 if err != nil {
205 return off, err
206 }
207 }
208 return off, nil
209 }
210
211 func (s *IPV6HINT) unpack(sc *cryptobyte.String) error {
212 for !sc.Empty() {
213 ip, err := unpack.AAAA(sc)
214 if err != nil {
215 return errors.New("dns: svcbipv6hint: expected ipv6, got ipv4")
216 }
217 s.Hint = append(s.Hint, ip)
218 }
219 return nil
220 }
221
222 func (s *DOHPATH) pack(msg []byte, off int) (int, error) {
223 off, err := packTLV(s, msg, off)
224 if err != nil {
225 return off, err
226 }
227 return pack.StringAny(s.Template, msg, off)
228 }
229
230 func (s *DOHPATH) unpack(sc *cryptobyte.String) (err error) {
231 s.Template, err = unpack.StringAny(sc, len(*sc))
232 return err
233 }
234
235 func (s *OHTTP) pack(msg []byte, off int) (int, error) {
236 off, err := packTLV(s, msg, off)
237 return off, err
238 }
239
240 func (*OHTTP) unpack(sc *cryptobyte.String) error {
241 if !sc.Empty() {
242 return errors.New("dns: svcbotthp: svcbotthp must have no value")
243 }
244 return nil
245 }
246
247 func (s *LOCAL) pack(msg []byte, off int) (int, error) {
248 off, err := pack.Uint16(s.KeyCode, msg, off)
249 if err != nil {
250 return len(msg), fmt.Errorf("dns: svcblocal: overflow packing keycode")
251 }
252 off, err = pack.Uint16(uint16(len(s.Data)), msg, off)
253 if err != nil {
254 return len(msg), fmt.Errorf("dns: svcblocal: overflow packing length")
255 }
256 n := copy(msg[off:], s.Data)
257 return off + n, nil
258 }
259
260 func (s *LOCAL) unpack(sc *cryptobyte.String) error {
261 // keys also, custom TLV
262 s.Data = make([]byte, len(*sc))
263 if !sc.CopyBytes(s.Data) {
264 return errors.New("dns: svcblocal overflow unpacking")
265 }
266 return nil
267 }
268
269 func packTLV(p Pair, msg []byte, off int) (off1 int, err error) {
270 key := PairToKey(p)
271 length := uint16(p.Len()) - tlv // now here we do the rdata length, not the 4 octets we encoding here
272 off, err = pack.Uint16(key, msg, off)
273 if err != nil {
274 return len(msg), fmt.Errorf("dns: overflow packing SVCB")
275 }
276 off, err = pack.Uint16(length, msg, off)
277 if err != nil {
278 return len(msg), fmt.Errorf("dns: overflow packing SVCB")
279 }
280 return off, err
281 }