Skip to content

Commit e6156c4

Browse files
committed
重命名GenerateWrongKey函数以提高可读性,修复生成错误DNSKEY RDATA的逻辑,并更新相应的单元测试
1 parent 638d2cb commit e6156c4

File tree

2 files changed

+37
-58
lines changed

2 files changed

+37
-58
lines changed

dns/xperi/dnssec.go

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func GenerateRRDS(oName string, kRDATA dns.DNSRDATADNSKEY, dType dns.DNSSECDiges
259259
return rr
260260
}
261261

262-
// GenerateWrongKey 生成一个具有指定KeyTag,且能通过检验,但错误的 DNSKEY RDATA
262+
// GenerateWrongKeyWithTag 生成一个具有指定KeyTag,且能通过检验,但错误的 DNSKEY RDATA
263263
// 传入参数:
264264
// - algo: DNSSEC 算法
265265
// - flag: DNSKEY Flag
@@ -279,66 +279,45 @@ func GenerateWrongKeyWithTag(algo dns.DNSSECAlgorithm, flag dns.DNSKEYFlag, tag
279279

280280
rTag := CalculateKeyTag(pKey)
281281
dif := tag - int(rTag)
282-
283-
fmt.Printf("rTag:%d, tTag:%d, dif: %d\n", rTag, tag, dif)
284-
285282
if dif < 0 {
286-
dif = -dif
287-
hDif := dif >> 8
288-
lDif := dif & 0xFF
289-
290-
for tvlr, _ := range pubKey {
291-
if tvlr&1 == 0 {
292-
if int(pubKey[tvlr])-int(hDif) < 0 {
293-
pubKey[tvlr] = 0
294-
hDif -= int(pubKey[tvlr])
295-
} else {
296-
pubKey[tvlr] -= byte(hDif)
297-
hDif = 0
298-
}
283+
dif = 0xFFFF + dif
284+
}
285+
hDif := dif & 0xFF00 >> 8
286+
lDif := dif & 0xFF
287+
for i := 0; i < len(pubKey); i++ {
288+
bVal := int(pubKey[i])
289+
if lDif != 0 && i&1 == 1 {
290+
if bVal+lDif > 255 {
291+
lDif = 255 - int(pubKey[i])
292+
pubKey[i] = 255
293+
} else if bVal+lDif < 0 {
294+
lDif -= int(pubKey[i])
295+
pubKey[i] = 0
299296
} else {
300-
if int(pubKey[tvlr])-int(hDif) < 0 {
301-
pubKey[tvlr] = 0
302-
lDif -= int(pubKey[tvlr])
303-
} else {
304-
pubKey[tvlr] -= byte(lDif)
305-
lDif = 0
306-
}
307-
}
308-
if hDif == 0 && lDif == 0 {
309-
break
297+
pubKey[i] = byte(int(pubKey[i]) + lDif)
298+
lDif = 0
310299
}
311300
}
312-
} else {
313-
hDif := dif >> 8
314-
lDif := dif & 0xFF
315-
316-
for tvlr, _ := range pubKey {
317-
if tvlr&1 == 0 {
318-
if int(pubKey[tvlr])+int(hDif) > 0xFF {
319-
pubKey[tvlr] = 0xFF
320-
hDif -= int(0xFF) - int(pubKey[tvlr])
321-
} else {
322-
pubKey[tvlr] += byte(hDif)
323-
hDif = 0
324-
}
301+
if hDif != 0 && i&1 == 0 {
302+
if bVal+hDif > 255 {
303+
hDif = 255 - int(pubKey[i])
304+
pubKey[i] = 255
305+
} else if bVal+hDif < 0 {
306+
hDif -= int(pubKey[i])
307+
pubKey[i] = 0
325308
} else {
326-
if int(pubKey[tvlr])+int(lDif) > 0xFF {
327-
pubKey[tvlr] = 0xFF
328-
lDif -= int(0xFF) - int(pubKey[tvlr])
329-
} else {
330-
pubKey[tvlr] += byte(lDif)
331-
lDif = 0
332-
}
333-
}
334-
if hDif == 0 && lDif == 0 {
335-
break
309+
pubKey[i] = byte(int(pubKey[i]) + hDif)
310+
hDif = 0
336311
}
337312
}
338313
}
339314

315+
nTag := CalculateKeyTag(pKey)
316+
fmt.Printf("rTag:%d, tTag:%d, dif: %d, nTag:%d, ldif: %d, hdif: %d\n",
317+
rTag, tag, dif, nTag, lDif, hDif)
318+
340319
// 重新计算 Key Tag, 算法不能保证成功
341-
if rTag != uint16(tag) {
320+
if nTag != uint16(tag) {
342321
return GenerateWrongKeyWithTag(algo, flag, tag)
343322
}
344323

dns/xperi/dnssec_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"github.com/tochusc/godns/dns"
1212
)
1313

14-
// // TestGenWrongKeyWithTag 测试 GenWrongKeyWithTag 函数
15-
// func TestGenWrongKeyWithTag(t *testing.T) {
16-
// key := GenWrongKeyWithTag(dns.DNSSECAlgorithmRSASHA256, dns.DNSKEYFlagZoneKey, 12345)
17-
// if CalculateKeyTag(key) != 12345 {
18-
// t.Errorf("Key Tag not match, got: %d, expected: %d", CalculateKeyTag(key), 12345)
19-
// }
20-
// }
14+
// TestGenerateWrongKeyWithTag 测试 GenerateWrongKeyWithTag 函数
15+
func TestGenerateWrongKeyWithTag(t *testing.T) {
16+
key := GenerateWrongKeyWithTag(dns.DNSSECAlgorithmRSASHA256, dns.DNSKEYFlagZoneKey, 12345)
17+
if CalculateKeyTag(key) != 12345 {
18+
t.Errorf("Key Tag not match, got: %d, expected: %d", CalculateKeyTag(key), 12345)
19+
}
20+
}
2121

2222
// TestGenKeyWithTag 测试 GenKeyWithTag 函数
2323
// func TestGenKeyWithTag(t *testing.T) {

0 commit comments

Comments
 (0)