Repository navigation
Expand file tree
/
Copy pathcoverage.html
More file actions
2369 lines (2177 loc) · 134 KB
/
Copy pathcoverage.html
File metadata and controls
2369 lines (2177 loc) · 134 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>packet: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/poyrazK/cloudDNS/internal/dns/packet/buffer.go (93.0%)</option>
<option value="file1">github.com/poyrazK/cloudDNS/internal/dns/packet/dnssec.go (75.8%)</option>
<option value="file2">github.com/poyrazK/cloudDNS/internal/dns/packet/dnssec_verify.go (69.4%)</option>
<option value="file3">github.com/poyrazK/cloudDNS/internal/dns/packet/nsec3.go (100.0%)</option>
<option value="file4">github.com/poyrazK/cloudDNS/internal/dns/packet/packet.go (84.7%)</option>
<option value="file5">github.com/poyrazK/cloudDNS/internal/dns/packet/tsig.go (75.9%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">no coverage</span>
<span class="cov1">low coverage</span>
<span class="cov2">*</span>
<span class="cov3">*</span>
<span class="cov4">*</span>
<span class="cov5">*</span>
<span class="cov6">*</span>
<span class="cov7">*</span>
<span class="cov8">*</span>
<span class="cov9">*</span>
<span class="cov10">high coverage</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">// Package packet provides functionality for parsing and serializing DNS packets.
package packet
import (
"errors"
"strings"
"sync"
)
// BytePacketBuffer simplifies reading and writing the DNS packet buffer.
type BytePacketBuffer struct {
Buf []byte
Pos int
Len int // High-water mark of data loaded or written
names map[string]int // For Name Compression
HasNames bool // Enable/Disable name compression tracking
parsing bool // Strict bounds checking mode for Step/Seek
}
// MaxPacketSize is the maximum size of a DNS packet over UDP (RFC 1035).
const MaxPacketSize = 65535
var bufferPool = sync.Pool{
New: func() interface{} <span class="cov1" title="1">{
return &BytePacketBuffer{
Buf: make([]byte, MaxPacketSize),
Pos: 0,
Len: 0,
}
}</span>,
}
// GetBuffer retrieves a buffer from the pool.
func GetBuffer() *BytePacketBuffer <span class="cov2" title="3">{
b := bufferPool.Get().(*BytePacketBuffer)
b.Reset()
return b
}</span>
// PutBuffer returns a buffer to the pool.
func PutBuffer(b *BytePacketBuffer) <span class="cov1" title="2">{
bufferPool.Put(b)
}</span>
// Reset clears the buffer state for reuse.
func (b *BytePacketBuffer) Reset() <span class="cov3" title="21">{
b.Pos = 0
b.Len = 0
b.parsing = false
if b.names == nil </span><span class="cov1" title="1">{
b.names = make(map[string]int)
}</span> else<span class="cov3" title="20"> {
clear(b.names)
}</span>
<span class="cov3" title="21">b.HasNames = false</span>
}
// NewBytePacketBuffer creates and returns a new BytePacketBuffer instance.
func NewBytePacketBuffer() *BytePacketBuffer <span class="cov7" title="1534">{
return &BytePacketBuffer{
Buf: make([]byte, MaxPacketSize),
Pos: 0,
Len: 0,
names: make(map[string]int),
}
}</span>
// Load copies the provided data into the buffer and sets its length.
func (b *BytePacketBuffer) Load(data []byte) <span class="cov7" title="608">{
copy(b.Buf, data)
b.Pos = 0
b.Len = len(data)
b.parsing = true
if b.names == nil </span><span class="cov0" title="0">{
b.names = make(map[string]int)
}</span> else<span class="cov7" title="608"> {
clear(b.names)
}</span>
}
// Position returns the current cursor position
func (b *BytePacketBuffer) Position() int <span class="cov8" title="2085">{
return b.Pos
}</span>
// Step moves the cursor forward by steps
func (b *BytePacketBuffer) Step(steps int) error <span class="cov5" title="123">{
if b.Pos+steps > MaxPacketSize || (b.parsing && b.Pos+steps > b.Len) </span><span class="cov1" title="2">{
return errors.New("step out of bounds")
}</span>
<span class="cov5" title="121">b.Pos += steps
if !b.parsing && b.Pos > b.Len </span><span class="cov1" title="2">{
b.Len = b.Pos
}</span>
<span class="cov5" title="121">return nil</span>
}
// Seek moves the cursor to a specific position
func (b *BytePacketBuffer) Seek(pos int) error <span class="cov7" title="1029">{
if pos > MaxPacketSize || (b.parsing && pos > b.Len) </span><span class="cov2" title="4">{
return errors.New("seek out of bounds")
}</span>
<span class="cov7" title="1025">b.Pos = pos
if !b.parsing && b.Pos > b.Len </span><span class="cov0" title="0">{
b.Len = b.Pos
}</span>
<span class="cov7" title="1025">return nil</span>
}
// Read reads a single byte
func (b *BytePacketBuffer) Read() (byte, error) <span class="cov6" title="251">{
if b.Pos >= b.Len </span><span class="cov3" title="22">{ // Strict check: can't read what isn't there
return 0, errors.New("end of buffer")
}</span>
<span class="cov6" title="229">res := b.Buf[b.Pos]
b.Pos++
return res, nil</span>
}
// ReadRange reads a slice of bytes
func (b *BytePacketBuffer) ReadRange(start int, length int) ([]byte, error) <span class="cov5" title="181">{
if start+length > b.Len </span><span class="cov4" title="69">{ // Strict check
return nil, errors.New("out of bounds")
}</span>
<span class="cov5" title="112">res := make([]byte, length)
copy(res, b.Buf[start:start+length])
return res, nil</span>
}
// Readu16 reads 2 bytes as uint16 (Big Endian)
func (b *BytePacketBuffer) Readu16() (uint16, error) <span class="cov8" title="1887">{
if b.Pos+2 > b.Len </span><span class="cov5" title="146">{ // Strict check
return 0, errors.New("end of buffer")
}</span>
<span class="cov8" title="1741">b1 := b.Buf[b.Pos]
b2 := b.Buf[b.Pos+1]
b.Pos += 2
return uint16(b1)<<8 | uint16(b2), nil</span>
}
// Readu32 reads 4 bytes as uint32 (Big Endian)
func (b *BytePacketBuffer) Readu32() (uint32, error) <span class="cov7" title="650">{
if b.Pos+4 > b.Len </span><span class="cov5" title="105">{ // Strict check
return 0, errors.New("end of buffer")
}</span>
<span class="cov6" title="545">b1 := b.Buf[b.Pos]
b2 := b.Buf[b.Pos+1]
b3 := b.Buf[b.Pos+2]
b4 := b.Buf[b.Pos+3]
b.Pos += 4
return uint32(b1)<<24 | uint32(b2)<<16 | uint32(b3)<<8 | uint32(b4), nil</span>
}
// ReadName reads a domain name, handling compression
func (b *BytePacketBuffer) ReadName() (string, error) <span class="cov7" title="962">{
pos := b.Pos
jumped := false
maxJumps := 5
jumpsPerformed := 0
var out strings.Builder
for </span><span class="cov8" title="2493">{
if jumpsPerformed > maxJumps </span><span class="cov2" title="4">{
return "", errors.New("limit of jumps exceeded")
}</span>
<span class="cov8" title="2489">lenByte, err := b.Get(pos)
if err != nil </span><span class="cov4" title="64">{
return "", err
}</span>
// End of labels
<span class="cov8" title="2425">if lenByte == 0 </span><span class="cov7" title="710">{
pos++
if !jumped </span><span class="cov7" title="708">{
if err := b.Seek(pos); err != nil </span><span class="cov0" title="0">{
return "", err
}</span>
}
<span class="cov7" title="710">res := out.String()
if res == "" </span><span class="cov4" title="46">{
return ".", nil
}</span>
<span class="cov7" title="664">return res, nil</span>
}
// Compression pointer (11xxxxxx)
<span class="cov8" title="1715">if (lenByte & 0xC0) == 0xC0 </span><span class="cov4" title="27">{
if !jumped </span><span class="cov2" title="7">{
if err := b.Seek(pos + 2); err != nil </span><span class="cov0" title="0">{
return "", err
}</span>
}
<span class="cov4" title="27">b2, err := b.Get(pos + 1)
if err != nil </span><span class="cov0" title="0">{
return "", err
}</span>
<span class="cov4" title="27">offset := ((uint16(lenByte) ^ 0xC0) << 8) | uint16(b2)
pos = int(offset)
jumped = true
jumpsPerformed++
continue</span>
}
// Normal label
<span class="cov7" title="1688">pos++
lenInt := int(lenByte)
if pos+lenInt > b.Len </span><span class="cov5" title="184">{
return "", errors.New("out of bounds")
}</span>
<span class="cov7" title="1504">label := b.Buf[pos : pos+lenInt]
for _, char := range label </span><span class="cov9" title="5735">{
if char >= 'A' && char <= 'Z' </span><span class="cov0" title="0">{
out.WriteByte(char + 32) // tolower
}</span> else<span class="cov9" title="5735"> {
out.WriteByte(char)
}</span>
}
<span class="cov7" title="1504">out.WriteByte('.')
pos += lenInt</span>
}
}
// Get reads a byte at a specific position without moving cursor
func (b *BytePacketBuffer) Get(pos int) (byte, error) <span class="cov8" title="2519">{
if pos >= b.Len </span><span class="cov4" title="65">{
return 0, errors.New("end of buffer")
}</span>
<span class="cov8" title="2454">return b.Buf[pos], nil</span>
}
// GetRange reads a range without moving cursor
func (b *BytePacketBuffer) GetRange(start int, length int) ([]byte, error) <span class="cov2" title="5">{
if start+length > b.Len </span><span class="cov1" title="1">{
return nil, errors.New("out of bounds")
}</span>
<span class="cov2" title="4">return b.Buf[start : start+length], nil</span>
}
// Write writes a single byte
func (b *BytePacketBuffer) Write(val byte) error <span class="cov10" title="14288">{
if b.Pos >= MaxPacketSize </span><span class="cov6" title="376">{
return errors.New("end of buffer")
}</span>
<span class="cov9" title="13912">b.Buf[b.Pos] = val
b.Pos++
if b.Pos > b.Len </span><span class="cov9" title="13907">{
b.Len = b.Pos
}</span>
<span class="cov9" title="13912">return nil</span>
}
// Writeu16 writes a uint16
func (b *BytePacketBuffer) Writeu16(val uint16) error <span class="cov8" title="2335">{
if b.Pos+2 > MaxPacketSize </span><span class="cov5" title="155">{
return errors.New("end of buffer")
}</span>
<span class="cov8" title="2180">b.Buf[b.Pos] = byte(val >> 8)
b.Buf[b.Pos+1] = byte(val & 0xFF)
b.Pos += 2
if b.Pos > b.Len </span><span class="cov8" title="2087">{
b.Len = b.Pos
}</span>
<span class="cov8" title="2180">return nil</span>
}
// Writeu32 writes a uint32
func (b *BytePacketBuffer) Writeu32(val uint32) error <span class="cov7" title="768">{
if b.Pos+4 > MaxPacketSize </span><span class="cov5" title="113">{
return errors.New("end of buffer")
}</span>
<span class="cov7" title="655">b.Buf[b.Pos] = byte(val >> 24)
b.Buf[b.Pos+1] = byte(val >> 16)
b.Buf[b.Pos+2] = byte(val >> 8)
b.Buf[b.Pos+3] = byte(val & 0xFF)
b.Pos += 4
if b.Pos > b.Len </span><span class="cov7" title="655">{
b.Len = b.Pos
}</span>
<span class="cov7" title="655">return nil</span>
}
// WriteName writes a domain name with compression support
func (b *BytePacketBuffer) WriteName(name string) error <span class="cov7" title="1123">{
if name == "" || name == "." </span><span class="cov3" title="14">{
return b.Write(0)
}</span>
// Standardize: ensure name ends with a dot
<span class="cov7" title="1109">if !strings.HasSuffix(name, ".") </span><span class="cov0" title="0">{
name += "."
}</span>
<span class="cov7" title="1109">curr := name
for </span><span class="cov8" title="2946">{
if curr == "" || curr == "." </span><span class="cov7" title="836">{
return b.Write(0)
}</span>
<span class="cov8" title="2110">if b.HasNames </span><span class="cov3" title="10">{
lower := strings.ToLower(curr)
if pos, ok := b.names[lower]; ok </span><span class="cov1" title="2">{
return b.Writeu16(uint16(pos) | 0xC000) // #nosec G115
}</span>
<span class="cov2" title="8">if b.Pos < 0x4000 </span><span class="cov2" title="8">{
b.names[lower] = b.Pos
}</span>
}
<span class="cov8" title="2108">dotIdx := strings.IndexByte(curr, '.')
if dotIdx == -1 </span><span class="cov0" title="0">{
return b.Write(0)
}</span>
<span class="cov8" title="2108">label := curr[:dotIdx]
if len(label) > 63 </span><span class="cov2" title="5">{
return errors.New("label too long")
}</span>
<span class="cov8" title="2103">if len(label) > 0 </span><span class="cov8" title="2103">{
if err := b.Write(byte(len(label))); err != nil </span><span class="cov4" title="40">{
return err
}</span>
<span class="cov8" title="2063">for i := 0; i < len(label); i++ </span><span class="cov9" title="7826">{
if err := b.Write(label[i]); err != nil </span><span class="cov6" title="226">{
return err
}</span>
}
}
<span class="cov8" title="1837">curr = curr[dotIdx+1:]</span>
}
}
// WriteNameUncompressed writes a domain name without compression
func (b *BytePacketBuffer) WriteNameUncompressed(name string) error <span class="cov1" title="2">{
if name == "" || name == "." </span><span class="cov0" title="0">{
return b.Write(0)
}</span>
<span class="cov1" title="2">if !strings.HasSuffix(name, ".") </span><span class="cov0" title="0">{
name += "."
}</span>
<span class="cov1" title="2">curr := name
for </span><span class="cov2" title="5">{
if curr == "" || curr == "." </span><span class="cov1" title="1">{
return b.Write(0)
}</span>
<span class="cov2" title="4">dotIdx := strings.IndexByte(curr, '.')
if dotIdx == -1 </span><span class="cov0" title="0">{
return b.Write(0)
}</span>
<span class="cov2" title="4">label := curr[:dotIdx]
if len(label) > 63 </span><span class="cov1" title="1">{
return errors.New("label too long")
}</span>
<span class="cov2" title="3">if len(label) > 0 </span><span class="cov2" title="3">{
if err := b.Write(byte(len(label))); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov2" title="3">for i := 0; i < len(label); i++ </span><span class="cov3" title="10">{
if err := b.Write(label[i]); err != nil </span><span class="cov0" title="0">{
return err
}</span>
}
}
<span class="cov2" title="3">curr = curr[dotIdx+1:]</span>
}
}
// WriteRange writes a slice of bytes at a specific position
func (b *BytePacketBuffer) WriteRange(start int, data []byte) error <span class="cov4" title="32">{
if start+len(data) > MaxPacketSize </span><span class="cov1" title="1">{
return errors.New("out of bounds")
}</span>
<span class="cov4" title="31">copy(b.Buf[start:start+len(data)], data)
if start+len(data) > b.Pos </span><span class="cov3" title="20">{
b.Pos = start + len(data)
}</span>
<span class="cov4" title="31">if b.Pos > b.Len </span><span class="cov3" title="19">{
b.Len = b.Pos
}</span>
<span class="cov4" title="31">return nil</span>
}
</pre>
<pre class="file" id="file1" style="display: none">package packet
import (
"crypto"
"crypto/ecdsa"
"crypto/rand"
"crypto/sha1" // #nosec G505 -- SHA-1 required for DNSSEC DS records (RFC 4034)
"crypto/sha256"
"strings"
)
// ComputeKeyTag calculates the key tag for a DNSKEY record according to RFC 4034 Appendix B.
// This is used to quickly identify which DNSKEY a signature refers to.
func (r *DNSRecord) ComputeKeyTag() uint16 <span class="cov5" title="30">{
if r.Type != DNSKEY </span><span class="cov1" title="1">{
return 0
}</span>
<span class="cov5" title="29">buf := NewBytePacketBuffer()
if err := buf.Writeu16(r.Flags); err != nil </span><span class="cov0" title="0">{
return 0
}</span>
<span class="cov5" title="29">if err := buf.Write(3); err != nil </span><span class="cov0" title="0">{
return 0
}</span> // Protocol: MUST be 3 (RFC 4034 Section 2.1.2)
<span class="cov5" title="29">if err := buf.Write(r.Algorithm); err != nil </span><span class="cov0" title="0">{
return 0
}</span>
<span class="cov5" title="29">for _, b := range r.PublicKey </span><span class="cov9" title="1513">{
if err := buf.Write(b); err != nil </span><span class="cov0" title="0">{
return 0
}</span>
}
<span class="cov5" title="29">data := buf.Buf[:buf.Position()]
var ac uint32
for i, b := range data </span><span class="cov10" title="1629">{
if i%2 == 0 </span><span class="cov9" title="827">{
ac += uint32(b) << 8
}</span> else<span class="cov9" title="802"> {
ac += uint32(b)
}</span>
}
<span class="cov5" title="29">ac += (ac >> 16) & 0xFFFF
return uint16(ac & 0xFFFF)</span> // #nosec G115
}
// ComputeDS generates a Delegation Signer (DS) record from a DNSKEY record (RFC 4034 Section 5.2).
// Supported digest types:
// - 1: SHA-1
// - 2: SHA-256
func (r *DNSRecord) ComputeDS(digestType uint8) (DNSRecord, error) <span class="cov4" title="14">{
if r.Type != DNSKEY </span><span class="cov1" title="2">{
return DNSRecord{}, nil
}</span>
// 1. Prepare Buffer: The digest is calculated over [owner name | DNSKEY RDATA]
// Owner name MUST be in its canonical wire format (lowercase, no compression).
<span class="cov4" title="12">buf := NewBytePacketBuffer()
if err := buf.WriteName(strings.ToLower(r.Name)); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
<span class="cov4" title="12">if err := buf.Writeu16(r.Flags); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
<span class="cov4" title="12">if err := buf.Write(3); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span> // Protocol
<span class="cov4" title="12">if err := buf.Write(r.Algorithm); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
<span class="cov4" title="12">for _, b := range r.PublicKey </span><span class="cov8" title="409">{
if err := buf.Write(b); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
}
// 2. Calculate the cryptographic digest
<span class="cov4" title="12">var digest []byte
switch digestType </span>{
case 1:<span class="cov1" title="2"> // SHA-1
hashed := sha1.Sum(buf.Buf[:buf.Position()]) // #nosec G401
digest = hashed[:]</span>
case 2:<span class="cov3" title="7"> // SHA-256
hashed := sha256.Sum256(buf.Buf[:buf.Position()])
digest = hashed[:]</span>
default:<span class="cov2" title="3">
// Unsupported digest type - return empty record per RFC 4034 expectations in some contexts
return DNSRecord{}, nil</span>
}
<span class="cov3" title="9">return DNSRecord{
Name: r.Name,
Type: DS,
Class: 1,
TTL: r.TTL,
KeyTag: r.ComputeKeyTag(),
Algorithm: r.Algorithm,
DigestType: digestType,
Digest: digest,
}, nil</span>
}
// SignRRSet generates an RRSIG for a set of records.
// This is a simplified implementation optimized for ECDSA P-256 (Algorithm 13).
func SignRRSet(records []DNSRecord, privKey *ecdsa.PrivateKey, signerName string, keyTag uint16, inception, expiration uint32) (DNSRecord, error) <span class="cov3" title="11">{
if len(records) == 0 </span><span class="cov1" title="1">{
return DNSRecord{}, nil
}</span>
<span class="cov3" title="10">sig := DNSRecord{
Name: records[0].Name,
Type: RRSIG,
Class: 1,
TTL: records[0].TTL,
TypeCovered: uint16(records[0].Type),
Algorithm: 13, // ECDSAP256SHA256
Labels: uint8(countLabels(records[0].Name)), // #nosec G115
OrigTTL: records[0].TTL,
Expiration: expiration,
Inception: inception,
KeyTag: keyTag,
SignerName: signerName,
}
buf := NewBytePacketBuffer()
for _, r := range records </span><span class="cov3" title="10">{
if err := buf.WriteName(strings.ToLower(r.Name)); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
<span class="cov3" title="10">if err := buf.Writeu16(uint16(r.Type)); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
<span class="cov3" title="10">if err := buf.Writeu16(uint16(1)); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span> // Class IN
<span class="cov3" title="10">if err := buf.Writeu32(r.TTL); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
// Write RDATA in canonical form
<span class="cov3" title="10">if err := writeSignCanonicalRData(&r, buf); err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
}
<span class="cov3" title="10">hashed := crypto.SHA256.New()
hashed.Write(buf.Buf[:buf.Position()])
h := hashed.Sum(nil)
rb, sb, err := ecdsa.Sign(rand.Reader, privKey, h)
if err != nil </span><span class="cov0" title="0">{
return DNSRecord{}, err
}</span>
<span class="cov3" title="10">rBytes := rb.FillBytes(make([]byte, 32))
sBytes := sb.FillBytes(make([]byte, 32))
sigData := make([]byte, 64)
copy(sigData[0:32], rBytes)
copy(sigData[32:64], sBytes)
sig.Signature = sigData
return sig, nil</span>
}
func countLabels(name string) int <span class="cov3" title="11">{
name = strings.TrimSuffix(name, ".")
if name == "" </span><span class="cov1" title="1">{
return 0
}</span>
<span class="cov3" title="10">return len(strings.Split(name, "."))</span>
}
// writeSignCanonicalRData writes the RDATA portion of a record in canonical form for signing.
// This is a copy of writeCanonicalRData from dnssec_verify.go but without the switch on type
// since SignRRSet only signs A records here (based on existing usage).
func writeSignCanonicalRData(r *DNSRecord, buf *BytePacketBuffer) error <span class="cov3" title="11">{
switch r.Type </span>{
case A:<span class="cov3" title="11">
// A record needs IP; if missing, fall through to data fallback
if len(r.IP) == 0 </span><span class="cov1" title="1">{
break</span>
}
<span class="cov3" title="10">if err := buf.Writeu16(4); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov3" title="10">ip4 := r.IP.To4()
if ip4 == nil </span><span class="cov0" title="0">{
break</span>
}
<span class="cov3" title="10">for _, b := range ip4 </span><span class="cov5" title="40">{
if err := buf.Write(b); err != nil </span><span class="cov0" title="0">{
return err
}</span>
}
<span class="cov3" title="10">return nil</span>
default:<span class="cov0" title="0">
// Fallback: write raw data if available
if len(r.Data) > 0 </span><span class="cov0" title="0">{
for _, b := range r.Data </span><span class="cov0" title="0">{
if err := buf.Write(b); err != nil </span><span class="cov0" title="0">{
return err
}</span>
}
}
}
<span class="cov1" title="1">return nil</span>
}
</pre>
<pre class="file" id="file2" style="display: none">package packet
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"errors"
"math/big"
"strings"
)
var (
// ErrSignatureExpired indicates the signature expiration time has passed.
ErrSignatureExpired = errors.New("dnssec: signature is expired")
// ErrSignatureNotYetValid indicates the signature inception time has not been reached.
ErrSignatureNotYetValid = errors.New("dnssec: signature is not yet valid")
// ErrKeyTagMismatch indicates the RRSIG key tag doesn't match the DNSKEY.
ErrKeyTagMismatch = errors.New("dnssec: key tag mismatch")
// ErrAlgorithmMismatch indicates the RRSIG algorithm doesn't match the DNSKEY.
ErrAlgorithmMismatch = errors.New("dnssec: algorithm mismatch")
// ErrInvalidDNSKEY indicates the DNSKEY has invalid flags.
ErrInvalidDNSKEY = errors.New("dnssec: invalid DNSKEY flags")
// ErrInvalidSignature indicates the signature verification failed.
ErrInvalidSignature = errors.New("dnssec: invalid signature")
// ErrNoPublicKey indicates the DNSKEY has no public key data.
ErrNoPublicKey = errors.New("dnssec: no public key in DNSKEY")
// ErrUnsupportedAlgorithm indicates the algorithm is not supported.
ErrUnsupportedAlgorithm = errors.New("dnssec: unsupported algorithm")
)
// writeBytes writes a byte slice to the buffer using individual byte writes.
func writeBytes(buf *BytePacketBuffer, data []byte) error <span class="cov5" title="16">{
for _, b := range data </span><span class="cov10" title="215">{
if err := buf.Write(b); err != nil </span><span class="cov2" title="2">{
return err
}</span>
}
<span class="cov5" title="14">return nil</span>
}
// CanonicalWireMarshal serializes a DNS record in canonical wire format per RFC 4034 Section 6.
// This format is used for DNSSEC signature verification.
func CanonicalWireMarshal(r *DNSRecord, buf *BytePacketBuffer) error <span class="cov5" title="18">{
// Owner name: lowercase, no compression
if err := buf.WriteName(strings.ToLower(r.Name)); err != nil </span><span class="cov4" title="7">{
return err
}</span>
<span class="cov5" title="11">switch r.Type </span>{
case DNSKEY:<span class="cov1" title="1">
// Canonical DNSKEY RDATA: flags | protocol | algorithm | publickey
if err := buf.Writeu16(r.Flags); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Write(3); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Write(r.Algorithm); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := writeBytes(buf, r.PublicKey); err != nil </span><span class="cov0" title="0">{
return err
}</span>
case RRSIG:<span class="cov1" title="1">
// Canonical RRSIG RDATA: typecovered | algorithm | labels | origttl |
// expiration | inception | keytag | signername | signature
if err := buf.Writeu16(r.TypeCovered); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Write(r.Algorithm); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Write(r.Labels); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu32(r.OrigTTL); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu32(r.Expiration); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu32(r.Inception); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu16(r.KeyTag); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.WriteName(strings.ToLower(r.SignerName)); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := writeBytes(buf, r.Signature); err != nil </span><span class="cov0" title="0">{
return err
}</span>
case DS:<span class="cov1" title="1">
// Canonical DS RDATA: keytag | algorithm | digesttype | digest
if err := buf.Writeu16(r.KeyTag); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Write(r.Algorithm); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Write(r.DigestType); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := writeBytes(buf, r.Digest); err != nil </span><span class="cov0" title="0">{
return err
}</span>
case A:<span class="cov1" title="1">
if err := buf.Writeu16(4); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">ip4 := r.IP.To4()
if ip4 == nil </span><span class="cov0" title="0">{
return errors.New("invalid IPv4 address")
}</span>
<span class="cov1" title="1">return writeBytes(buf, ip4)</span>
case AAAA:<span class="cov1" title="1">
if err := buf.Writeu16(16); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return writeBytes(buf, r.IP.To16())</span>
case CNAME, NS, PTR, MD, MF, MB, MG, MR:<span class="cov1" title="1">
return buf.WriteName(strings.ToLower(r.Host))</span>
case TXT:<span class="cov1" title="1">
// TXT: length prefix followed by ASCII bytes
for _, chunk := range stringsToChunks(r.Txt) </span><span class="cov1" title="1">{
if err := buf.Write(byte(len(chunk))); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := writeBytes(buf, []byte(chunk)); err != nil </span><span class="cov0" title="0">{
return err
}</span>
}
<span class="cov1" title="1">return nil</span>
case MX:<span class="cov0" title="0">
if err := buf.Writeu16(r.Priority); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">return buf.WriteName(strings.ToLower(r.Host))</span>
case SOA:<span class="cov1" title="1">
if err := buf.WriteName(strings.ToLower(r.MName)); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.WriteName(strings.ToLower(r.RName)); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu32(r.Serial); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu32(r.Refresh); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu32(r.Retry); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu32(r.Expire); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return buf.Writeu32(r.Minimum)</span>
case SRV:<span class="cov1" title="1">
if err := buf.Writeu16(r.Priority); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu16(r.Weight); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">if err := buf.Writeu16(r.Port); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return buf.WriteName(strings.ToLower(r.Host))</span>
case NSEC:<span class="cov1" title="1">
if err := buf.WriteName(strings.ToLower(r.NextName)); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return writeBytes(buf, r.TypeBitMap)</span>
default:<span class="cov1" title="1">
// Fallback: write raw data if available
if len(r.Data) > 0 </span><span class="cov1" title="1">{
return writeBytes(buf, r.Data)
}</span>
}
<span class="cov2" title="3">return nil</span>
}
// stringsToChunks splits a string into 255-byte chunks for TXT records.
func stringsToChunks(s string) []string <span class="cov2" title="2">{
var chunks []string
for i := 0; i < len(s); i += 255 </span><span class="cov2" title="2">{
end := i + 255
if end > len(s) </span><span class="cov2" title="2">{
end = len(s)
}</span>
<span class="cov2" title="2">chunks = append(chunks, s[i:end])</span>
}
<span class="cov2" title="2">return chunks</span>
}
// VerifyRRSet verifies an RRSIG signature over an RRSet.
// It supports ECDSA P-256 (Algorithm 13) signatures.
func VerifyRRSet(rrset []DNSRecord, rrsig DNSRecord, dnskey DNSRecord, now uint32) (bool, error) <span class="cov4" title="8">{
if len(rrset) == 0 </span><span class="cov1" title="1">{
return false, errors.New("dnssec: empty rrset")
}</span>
// 1. Check signature expiration
<span class="cov4" title="7">if now > rrsig.Expiration </span><span class="cov1" title="1">{
return false, ErrSignatureExpired
}</span>
// 2. Check signature inception
<span class="cov4" title="6">if now < rrsig.Inception </span><span class="cov1" title="1">{
return false, ErrSignatureNotYetValid
}</span>
// 3. Verify key tag matches
<span class="cov3" title="5">if rrsig.KeyTag != dnskey.ComputeKeyTag() </span><span class="cov1" title="1">{
return false, ErrKeyTagMismatch
}</span>
// 4. Verify algorithm matches
<span class="cov3" title="4">if rrsig.Algorithm != dnskey.Algorithm </span><span class="cov1" title="1">{
return false, ErrAlgorithmMismatch
}</span>
// 5. Check DNSKEY type
<span class="cov2" title="3">if dnskey.Type != DNSKEY </span><span class="cov0" title="0">{
return false, ErrInvalidDNSKEY
}</span>
// 6. Extract ECDSA public key from DNSKEY
<span class="cov2" title="3">publicKey, err := extractECDSAPublicKey(dnskey)
if err != nil </span><span class="cov0" title="0">{
return false, err
}</span>
// 7. Reconstruct canonical wire format of RRSet
<span class="cov2" title="3">buf := NewBytePacketBuffer()
for _, r := range rrset </span><span class="cov2" title="3">{
// Write owner name in canonical form
if err := buf.WriteName(strings.ToLower(r.Name)); err != nil </span><span class="cov0" title="0">{
return false, err
}</span>
<span class="cov2" title="3">if err := buf.Writeu16(uint16(r.Type)); err != nil </span><span class="cov0" title="0">{
return false, err
}</span>
<span class="cov2" title="3">if err := buf.Writeu16(1); err != nil </span><span class="cov0" title="0">{ // Class IN (RDATA class, per RFC 4034)
return false, err
}</span>
<span class="cov2" title="3">if err := buf.Writeu32(r.TTL); err != nil </span><span class="cov0" title="0">{
return false, err
}</span>
// Write RDATA in canonical form
<span class="cov2" title="3">if err := writeCanonicalRData(&r, buf); err != nil </span><span class="cov0" title="0">{
return false, err
}</span>
}
// 8. Compute hash
<span class="cov2" title="3">hashed := sha256.Sum256(buf.Buf[:buf.Position()])
// 9. Extract R and S from signature
if len(rrsig.Signature) < 64 </span><span class="cov0" title="0">{
return false, ErrInvalidSignature
}</span>
<span class="cov2" title="3">r := new(big.Int).SetBytes(rrsig.Signature[0:32])
s := new(big.Int).SetBytes(rrsig.Signature[32:64])
// 10. Verify ECDSA signature
if !ecdsa.Verify(publicKey, hashed[:], r, s) </span><span class="cov1" title="1">{
return false, ErrInvalidSignature
}</span>
<span class="cov2" title="2">return true, nil</span>
}
// extractECDSAPublicKey extracts an ECDSA P-256 public key from a DNSKEY record.
func extractECDSAPublicKey(dnskey DNSRecord) (*ecdsa.PublicKey, error) <span class="cov4" title="6">{
if len(dnskey.PublicKey) < 65 </span><span class="cov1" title="1">{
return nil, ErrNoPublicKey
}</span>
// ECDSA P-256 public key is an uncompressed curve point:
// 0x04 || X (32 bytes) || Y (32 bytes)
<span class="cov3" title="5">if dnskey.PublicKey[0] != 0x04 </span><span class="cov1" title="1">{
return nil, ErrUnsupportedAlgorithm
}</span>
<span class="cov3" title="4">x := new(big.Int).SetBytes(dnskey.PublicKey[1:33])
y := new(big.Int).SetBytes(dnskey.PublicKey[33:65])
return &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: x,
Y: y,
}, nil</span>
}
// writeCanonicalRData writes the RDATA portion of a record in canonical form.
func writeCanonicalRData(r *DNSRecord, buf *BytePacketBuffer) error <span class="cov6" title="20">{
switch r.Type </span>{
case A:<span class="cov3" title="4">
if err := buf.Writeu16(4); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov3" title="4">ip4 := r.IP.To4()
if ip4 == nil </span><span class="cov1" title="1">{
return errors.New("invalid IPv4 address")
}</span>
<span class="cov2" title="3">return writeBytes(buf, ip4)</span>
case AAAA:<span class="cov0" title="0">
if err := buf.Writeu16(16); err != nil </span><span class="cov0" title="0">{
return err