security.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package dht
  2. import (
  3. "hash/crc32"
  4. "net"
  5. "github.com/anacrolix/dht/v2/krpc"
  6. )
  7. func maskForIP(ip net.IP) []byte {
  8. switch {
  9. case ip.To4() != nil:
  10. return []byte{0x03, 0x0f, 0x3f, 0xff}
  11. default:
  12. return []byte{0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff}
  13. }
  14. }
  15. // Generate the CRC used to make or validate secure node ID.
  16. func crcIP(ip net.IP, rand uint8) uint32 {
  17. if ip4 := ip.To4(); ip4 != nil {
  18. ip = ip4
  19. }
  20. // Copy IP so we can make changes. Go sux at this.
  21. ip = append(make(net.IP, 0, len(ip)), ip...)
  22. mask := maskForIP(ip)
  23. for i := range mask {
  24. ip[i] &= mask[i]
  25. }
  26. r := rand & 7
  27. ip[0] |= r << 5
  28. return crc32.Checksum(ip[:len(mask)], crc32.MakeTable(crc32.Castagnoli))
  29. }
  30. // Makes a node ID secure, in-place. The ID is 20 raw bytes.
  31. // http://www.libtorrent.org/dht_sec.html
  32. func SecureNodeId(id *krpc.ID, ip net.IP) {
  33. crc := crcIP(ip, id[19])
  34. id[0] = byte(crc >> 24 & 0xff)
  35. id[1] = byte(crc >> 16 & 0xff)
  36. id[2] = byte(crc>>8&0xf8) | id[2]&7
  37. }
  38. // Returns whether the node ID is considered secure. The id is the 20 raw
  39. // bytes. http://www.libtorrent.org/dht_sec.html
  40. func NodeIdSecure(id [20]byte, ip net.IP) bool {
  41. if isLocalNetwork(ip) {
  42. return true
  43. }
  44. if ip4 := ip.To4(); ip4 != nil {
  45. ip = ip4
  46. }
  47. crc := crcIP(ip, id[19])
  48. if id[0] != byte(crc>>24&0xff) {
  49. return false
  50. }
  51. if id[1] != byte(crc>>16&0xff) {
  52. return false
  53. }
  54. if id[2]&0xf8 != byte(crc>>8&0xf8) {
  55. return false
  56. }
  57. return true
  58. }
  59. var classA, classB, classC *net.IPNet
  60. func mustParseCIDRIPNet(s string) *net.IPNet {
  61. _, ret, err := net.ParseCIDR(s)
  62. if err != nil {
  63. panic(err)
  64. }
  65. return ret
  66. }
  67. func init() {
  68. classA = mustParseCIDRIPNet("10.0.0.0/8")
  69. classB = mustParseCIDRIPNet("172.16.0.0/12")
  70. classC = mustParseCIDRIPNet("192.168.0.0/16")
  71. }
  72. // Per http://www.libtorrent.org/dht_sec.html#enforcement, the IP is
  73. // considered a local network address and should be exempted from node ID
  74. // verification.
  75. func isLocalNetwork(ip net.IP) bool {
  76. if classA.Contains(ip) {
  77. return true
  78. }
  79. if classB.Contains(ip) {
  80. return true
  81. }
  82. if classC.Contains(ip) {
  83. return true
  84. }
  85. if ip.IsLinkLocalUnicast() {
  86. return true
  87. }
  88. if ip.IsLoopback() {
  89. return true
  90. }
  91. return false
  92. }