| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"codeberg.org/miekg/dns/internal/pack" |
| 5 |
"codeberg.org/miekg/dns/internal/unpack" |
| 6 |
"golang.org/x/crypto/cryptobyte" |
| 7 |
) |
| 8 |
|
| 9 |
func unpackNSEC(s *cryptobyte.String) ([]uint16, error) { |
| 10 |
var nsec []uint16 |
| 11 |
lastwindow := -1 |
| 12 |
for !s.Empty() { |
| 13 |
var ( |
| 14 |
window byte |
| 15 |
bits cryptobyte.String |
| 16 |
) |
| 17 |
if !s.ReadUint8(&window) || |
| 18 |
!s.ReadUint8LengthPrefixed(&bits) { |
| 19 |
return nsec, unpack.ErrOverflow |
| 20 |
} |
| 21 |
if int(window) <= lastwindow { |
| 22 |
// RFC 4034: Blocks are present in the NSEC RR RDATA in |
| 23 |
// increasing numerical order. |
| 24 |
return nsec, &unpack.Error{Err: "out of order NSEC(3) BLock in type bitmap"} |
| 25 |
} |
| 26 |
if len(bits) == 0 { |
| 27 |
// RFC 4034: Blocks with no types present MUST NOT be included. |
| 28 |
return nsec, &unpack.Error{Err: "empty NSEC(3) block in type bitmap"} |
| 29 |
} |
| 30 |
if len(bits) > 32 { |
| 31 |
return nsec, &unpack.Error{Err: "NSEC(3) block too long in type bitmap"} |
| 32 |
} |
| 33 |
|
| 34 |
// Walk the bytes in the window and extract the type bits |
| 35 |
for i, b := range bits { |
| 36 |
for n := range uint(8) { |
| 37 |
if b&(1<<(7-n)) != 0 { |
| 38 |
nsec = append(nsec, uint16(int(window)*256+i*8+int(n))) |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
lastwindow = int(window) |
| 44 |
} |
| 45 |
return nsec, nil |
| 46 |
} |
| 47 |
|
| 48 |
// typeBitMapLen is a helper function which computes the "maximum" length of |
| 49 |
// a the NSEC Type BitMap field. |
| 50 |
func typeBitMapLen(bitmap []uint16) int { |
| 51 |
var l int |
| 52 |
var lastwindow, lastlength uint16 |
| 53 |
for _, t := range bitmap { |
| 54 |
window := t / 256 |
| 55 |
length := (t-window*256)/8 + 1 |
| 56 |
if window > lastwindow && lastlength != 0 { // New window, jump to the new off |
| 57 |
l += int(lastlength) + 2 |
| 58 |
lastlength = 0 |
| 59 |
} |
| 60 |
if window < lastwindow || length < lastlength { |
| 61 |
// packNsec would return Error{err: "nsec bits out of order"} here, but |
| 62 |
// when computing the length, we want do be liberal. |
| 63 |
continue |
| 64 |
} |
| 65 |
lastwindow, lastlength = window, length |
| 66 |
} |
| 67 |
l += int(lastlength) + 2 |
| 68 |
return l |
| 69 |
} |
| 70 |
|
| 71 |
func packNSEC(bitmap []uint16, msg []byte, off int) (int, error) { |
| 72 |
if len(bitmap) == 0 { |
| 73 |
return off, nil |
| 74 |
} |
| 75 |
if off > len(msg) { |
| 76 |
return off, &pack.Error{Err: "overflow NSEC(3)"} |
| 77 |
} |
| 78 |
toZero := msg[off:] |
| 79 |
if maxLen := typeBitMapLen(bitmap); maxLen < len(toZero) { |
| 80 |
toZero = toZero[:maxLen] |
| 81 |
} |
| 82 |
for i := range toZero { |
| 83 |
toZero[i] = 0 |
| 84 |
} |
| 85 |
var lastwindow, lastlength uint16 |
| 86 |
for _, t := range bitmap { |
| 87 |
window := t / 256 |
| 88 |
length := (t-window*256)/8 + 1 |
| 89 |
if window > lastwindow && lastlength != 0 { // New window, jump to the new off |
| 90 |
off += int(lastlength) + 2 |
| 91 |
lastlength = 0 |
| 92 |
} |
| 93 |
if window < lastwindow || length < lastlength { |
| 94 |
return len(msg), &pack.Error{Err: "NSEC(3) bits out of order"} |
| 95 |
} |
| 96 |
if off+2+int(length) > len(msg) { |
| 97 |
return len(msg), &pack.Error{Err: "overflow NSEC(3)"} |
| 98 |
} |
| 99 |
// Setting the window # |
| 100 |
msg[off] = byte(window) |
| 101 |
// Setting the octets length |
| 102 |
msg[off+1] = byte(length) |
| 103 |
// Setting the bit value for the type in the right octet |
| 104 |
msg[off+1+int(length)] |= byte(1 << (7 - t%8)) |
| 105 |
lastwindow, lastlength = window, length |
| 106 |
} |
| 107 |
off += int(lastlength) + 2 |
| 108 |
return off, nil |
| 109 |
} |