cert.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. package cert
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "encoding/binary"
  8. "encoding/hex"
  9. "encoding/json"
  10. "encoding/pem"
  11. "fmt"
  12. "net"
  13. "time"
  14. "golang.org/x/crypto/curve25519"
  15. "golang.org/x/crypto/ed25519"
  16. "google.golang.org/protobuf/proto"
  17. )
  18. const publicKeyLen = 32
  19. const (
  20. CertBanner = "NEBULA CERTIFICATE"
  21. X25519PrivateKeyBanner = "NEBULA X25519 PRIVATE KEY"
  22. X25519PublicKeyBanner = "NEBULA X25519 PUBLIC KEY"
  23. Ed25519PrivateKeyBanner = "NEBULA ED25519 PRIVATE KEY"
  24. Ed25519PublicKeyBanner = "NEBULA ED25519 PUBLIC KEY"
  25. )
  26. type NebulaCertificate struct {
  27. Details NebulaCertificateDetails
  28. Signature []byte
  29. }
  30. type NebulaCertificateDetails struct {
  31. Name string
  32. Ips []*net.IPNet
  33. Subnets []*net.IPNet
  34. Groups []string
  35. NotBefore time.Time
  36. NotAfter time.Time
  37. PublicKey []byte
  38. IsCA bool
  39. Issuer string
  40. // Map of groups for faster lookup
  41. InvertedGroups map[string]struct{}
  42. }
  43. type m map[string]interface{}
  44. // UnmarshalNebulaCertificate will unmarshal a protobuf byte representation of a nebula cert
  45. func UnmarshalNebulaCertificate(b []byte) (*NebulaCertificate, error) {
  46. if len(b) == 0 {
  47. return nil, fmt.Errorf("nil byte array")
  48. }
  49. var rc RawNebulaCertificate
  50. err := proto.Unmarshal(b, &rc)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if rc.Details == nil {
  55. return nil, fmt.Errorf("encoded Details was nil")
  56. }
  57. if len(rc.Details.Ips)%2 != 0 {
  58. return nil, fmt.Errorf("encoded IPs should be in pairs, an odd number was found")
  59. }
  60. if len(rc.Details.Subnets)%2 != 0 {
  61. return nil, fmt.Errorf("encoded Subnets should be in pairs, an odd number was found")
  62. }
  63. nc := NebulaCertificate{
  64. Details: NebulaCertificateDetails{
  65. Name: rc.Details.Name,
  66. Groups: make([]string, len(rc.Details.Groups)),
  67. Ips: make([]*net.IPNet, len(rc.Details.Ips)/2),
  68. Subnets: make([]*net.IPNet, len(rc.Details.Subnets)/2),
  69. NotBefore: time.Unix(rc.Details.NotBefore, 0),
  70. NotAfter: time.Unix(rc.Details.NotAfter, 0),
  71. PublicKey: make([]byte, len(rc.Details.PublicKey)),
  72. IsCA: rc.Details.IsCA,
  73. InvertedGroups: make(map[string]struct{}),
  74. },
  75. Signature: make([]byte, len(rc.Signature)),
  76. }
  77. copy(nc.Signature, rc.Signature)
  78. copy(nc.Details.Groups, rc.Details.Groups)
  79. nc.Details.Issuer = hex.EncodeToString(rc.Details.Issuer)
  80. if len(rc.Details.PublicKey) < publicKeyLen {
  81. return nil, fmt.Errorf("Public key was fewer than 32 bytes; %v", len(rc.Details.PublicKey))
  82. }
  83. copy(nc.Details.PublicKey, rc.Details.PublicKey)
  84. for i, rawIp := range rc.Details.Ips {
  85. if i%2 == 0 {
  86. nc.Details.Ips[i/2] = &net.IPNet{IP: int2ip(rawIp)}
  87. } else {
  88. nc.Details.Ips[i/2].Mask = net.IPMask(int2ip(rawIp))
  89. }
  90. }
  91. for i, rawIp := range rc.Details.Subnets {
  92. if i%2 == 0 {
  93. nc.Details.Subnets[i/2] = &net.IPNet{IP: int2ip(rawIp)}
  94. } else {
  95. nc.Details.Subnets[i/2].Mask = net.IPMask(int2ip(rawIp))
  96. }
  97. }
  98. for _, g := range rc.Details.Groups {
  99. nc.Details.InvertedGroups[g] = struct{}{}
  100. }
  101. return &nc, nil
  102. }
  103. // UnmarshalNebulaCertificateFromPEM will unmarshal the first pem block in a byte array, returning any non consumed data
  104. // or an error on failure
  105. func UnmarshalNebulaCertificateFromPEM(b []byte) (*NebulaCertificate, []byte, error) {
  106. p, r := pem.Decode(b)
  107. if p == nil {
  108. return nil, r, fmt.Errorf("input did not contain a valid PEM encoded block")
  109. }
  110. if p.Type != CertBanner {
  111. return nil, r, fmt.Errorf("bytes did not contain a proper nebula certificate banner")
  112. }
  113. nc, err := UnmarshalNebulaCertificate(p.Bytes)
  114. return nc, r, err
  115. }
  116. // MarshalX25519PrivateKey is a simple helper to PEM encode an X25519 private key
  117. func MarshalX25519PrivateKey(b []byte) []byte {
  118. return pem.EncodeToMemory(&pem.Block{Type: X25519PrivateKeyBanner, Bytes: b})
  119. }
  120. // MarshalEd25519PrivateKey is a simple helper to PEM encode an Ed25519 private key
  121. func MarshalEd25519PrivateKey(key ed25519.PrivateKey) []byte {
  122. return pem.EncodeToMemory(&pem.Block{Type: Ed25519PrivateKeyBanner, Bytes: key})
  123. }
  124. // UnmarshalX25519PrivateKey will try to pem decode an X25519 private key, returning any other bytes b
  125. // or an error on failure
  126. func UnmarshalX25519PrivateKey(b []byte) ([]byte, []byte, error) {
  127. k, r := pem.Decode(b)
  128. if k == nil {
  129. return nil, r, fmt.Errorf("input did not contain a valid PEM encoded block")
  130. }
  131. if k.Type != X25519PrivateKeyBanner {
  132. return nil, r, fmt.Errorf("bytes did not contain a proper nebula X25519 private key banner")
  133. }
  134. if len(k.Bytes) != publicKeyLen {
  135. return nil, r, fmt.Errorf("key was not 32 bytes, is invalid X25519 private key")
  136. }
  137. return k.Bytes, r, nil
  138. }
  139. // UnmarshalEd25519PrivateKey will try to pem decode an Ed25519 private key, returning any other bytes b
  140. // or an error on failure
  141. func UnmarshalEd25519PrivateKey(b []byte) (ed25519.PrivateKey, []byte, error) {
  142. k, r := pem.Decode(b)
  143. if k == nil {
  144. return nil, r, fmt.Errorf("input did not contain a valid PEM encoded block")
  145. }
  146. if k.Type != Ed25519PrivateKeyBanner {
  147. return nil, r, fmt.Errorf("bytes did not contain a proper nebula Ed25519 private key banner")
  148. }
  149. if len(k.Bytes) != ed25519.PrivateKeySize {
  150. return nil, r, fmt.Errorf("key was not 64 bytes, is invalid ed25519 private key")
  151. }
  152. return k.Bytes, r, nil
  153. }
  154. // MarshalX25519PublicKey is a simple helper to PEM encode an X25519 public key
  155. func MarshalX25519PublicKey(b []byte) []byte {
  156. return pem.EncodeToMemory(&pem.Block{Type: X25519PublicKeyBanner, Bytes: b})
  157. }
  158. // MarshalEd25519PublicKey is a simple helper to PEM encode an Ed25519 public key
  159. func MarshalEd25519PublicKey(key ed25519.PublicKey) []byte {
  160. return pem.EncodeToMemory(&pem.Block{Type: Ed25519PublicKeyBanner, Bytes: key})
  161. }
  162. // UnmarshalX25519PublicKey will try to pem decode an X25519 public key, returning any other bytes b
  163. // or an error on failure
  164. func UnmarshalX25519PublicKey(b []byte) ([]byte, []byte, error) {
  165. k, r := pem.Decode(b)
  166. if k == nil {
  167. return nil, r, fmt.Errorf("input did not contain a valid PEM encoded block")
  168. }
  169. if k.Type != X25519PublicKeyBanner {
  170. return nil, r, fmt.Errorf("bytes did not contain a proper nebula X25519 public key banner")
  171. }
  172. if len(k.Bytes) != publicKeyLen {
  173. return nil, r, fmt.Errorf("key was not 32 bytes, is invalid X25519 public key")
  174. }
  175. return k.Bytes, r, nil
  176. }
  177. // UnmarshalEd25519PublicKey will try to pem decode an Ed25519 public key, returning any other bytes b
  178. // or an error on failure
  179. func UnmarshalEd25519PublicKey(b []byte) (ed25519.PublicKey, []byte, error) {
  180. k, r := pem.Decode(b)
  181. if k == nil {
  182. return nil, r, fmt.Errorf("input did not contain a valid PEM encoded block")
  183. }
  184. if k.Type != Ed25519PublicKeyBanner {
  185. return nil, r, fmt.Errorf("bytes did not contain a proper nebula Ed25519 public key banner")
  186. }
  187. if len(k.Bytes) != ed25519.PublicKeySize {
  188. return nil, r, fmt.Errorf("key was not 32 bytes, is invalid ed25519 public key")
  189. }
  190. return k.Bytes, r, nil
  191. }
  192. // Sign signs a nebula cert with the provided private key
  193. func (nc *NebulaCertificate) Sign(key ed25519.PrivateKey) error {
  194. b, err := proto.Marshal(nc.getRawDetails())
  195. if err != nil {
  196. return err
  197. }
  198. sig, err := key.Sign(rand.Reader, b, crypto.Hash(0))
  199. if err != nil {
  200. return err
  201. }
  202. nc.Signature = sig
  203. return nil
  204. }
  205. // CheckSignature verifies the signature against the provided public key
  206. func (nc *NebulaCertificate) CheckSignature(key ed25519.PublicKey) bool {
  207. b, err := proto.Marshal(nc.getRawDetails())
  208. if err != nil {
  209. return false
  210. }
  211. return ed25519.Verify(key, b, nc.Signature)
  212. }
  213. // Expired will return true if the nebula cert is too young or too old compared to the provided time, otherwise false
  214. func (nc *NebulaCertificate) Expired(t time.Time) bool {
  215. return nc.Details.NotBefore.After(t) || nc.Details.NotAfter.Before(t)
  216. }
  217. // Verify will ensure a certificate is good in all respects (expiry, group membership, signature, cert blocklist, etc)
  218. func (nc *NebulaCertificate) Verify(t time.Time, ncp *NebulaCAPool) (bool, error) {
  219. if ncp.IsBlocklisted(nc) {
  220. return false, fmt.Errorf("certificate has been blocked")
  221. }
  222. signer, err := ncp.GetCAForCert(nc)
  223. if err != nil {
  224. return false, err
  225. }
  226. if signer.Expired(t) {
  227. return false, fmt.Errorf("root certificate is expired")
  228. }
  229. if nc.Expired(t) {
  230. return false, fmt.Errorf("certificate is expired")
  231. }
  232. if !nc.CheckSignature(signer.Details.PublicKey) {
  233. return false, fmt.Errorf("certificate signature did not match")
  234. }
  235. if err := nc.CheckRootConstrains(signer); err != nil {
  236. return false, err
  237. }
  238. return true, nil
  239. }
  240. // CheckRootConstrains returns an error if the certificate violates constraints set on the root (groups, ips, subnets)
  241. func (nc *NebulaCertificate) CheckRootConstrains(signer *NebulaCertificate) error {
  242. // Make sure this cert wasn't valid before the root
  243. if signer.Details.NotAfter.Before(nc.Details.NotAfter) {
  244. return fmt.Errorf("certificate expires after signing certificate")
  245. }
  246. // Make sure this cert isn't valid after the root
  247. if signer.Details.NotBefore.After(nc.Details.NotBefore) {
  248. return fmt.Errorf("certificate is valid before the signing certificate")
  249. }
  250. // If the signer has a limited set of groups make sure the cert only contains a subset
  251. if len(signer.Details.InvertedGroups) > 0 {
  252. for _, g := range nc.Details.Groups {
  253. if _, ok := signer.Details.InvertedGroups[g]; !ok {
  254. return fmt.Errorf("certificate contained a group not present on the signing ca: %s", g)
  255. }
  256. }
  257. }
  258. // If the signer has a limited set of ip ranges to issue from make sure the cert only contains a subset
  259. if len(signer.Details.Ips) > 0 {
  260. for _, ip := range nc.Details.Ips {
  261. if !netMatch(ip, signer.Details.Ips) {
  262. return fmt.Errorf("certificate contained an ip assignment outside the limitations of the signing ca: %s", ip.String())
  263. }
  264. }
  265. }
  266. // If the signer has a limited set of subnet ranges to issue from make sure the cert only contains a subset
  267. if len(signer.Details.Subnets) > 0 {
  268. for _, subnet := range nc.Details.Subnets {
  269. if !netMatch(subnet, signer.Details.Subnets) {
  270. return fmt.Errorf("certificate contained a subnet assignment outside the limitations of the signing ca: %s", subnet)
  271. }
  272. }
  273. }
  274. return nil
  275. }
  276. // VerifyPrivateKey checks that the public key in the Nebula certificate and a supplied private key match
  277. func (nc *NebulaCertificate) VerifyPrivateKey(key []byte) error {
  278. if nc.Details.IsCA {
  279. // the call to PublicKey below will panic slice bounds out of range otherwise
  280. if len(key) != ed25519.PrivateKeySize {
  281. return fmt.Errorf("key was not 64 bytes, is invalid ed25519 private key")
  282. }
  283. if !ed25519.PublicKey(nc.Details.PublicKey).Equal(ed25519.PrivateKey(key).Public()) {
  284. return fmt.Errorf("public key in cert and private key supplied don't match")
  285. }
  286. return nil
  287. }
  288. pub, err := curve25519.X25519(key, curve25519.Basepoint)
  289. if err != nil {
  290. return err
  291. }
  292. if !bytes.Equal(pub, nc.Details.PublicKey) {
  293. return fmt.Errorf("public key in cert and private key supplied don't match")
  294. }
  295. return nil
  296. }
  297. // String will return a pretty printed representation of a nebula cert
  298. func (nc *NebulaCertificate) String() string {
  299. if nc == nil {
  300. return "NebulaCertificate {}\n"
  301. }
  302. s := "NebulaCertificate {\n"
  303. s += "\tDetails {\n"
  304. s += fmt.Sprintf("\t\tName: %v\n", nc.Details.Name)
  305. if len(nc.Details.Ips) > 0 {
  306. s += "\t\tIps: [\n"
  307. for _, ip := range nc.Details.Ips {
  308. s += fmt.Sprintf("\t\t\t%v\n", ip.String())
  309. }
  310. s += "\t\t]\n"
  311. } else {
  312. s += "\t\tIps: []\n"
  313. }
  314. if len(nc.Details.Subnets) > 0 {
  315. s += "\t\tSubnets: [\n"
  316. for _, ip := range nc.Details.Subnets {
  317. s += fmt.Sprintf("\t\t\t%v\n", ip.String())
  318. }
  319. s += "\t\t]\n"
  320. } else {
  321. s += "\t\tSubnets: []\n"
  322. }
  323. if len(nc.Details.Groups) > 0 {
  324. s += "\t\tGroups: [\n"
  325. for _, g := range nc.Details.Groups {
  326. s += fmt.Sprintf("\t\t\t\"%v\"\n", g)
  327. }
  328. s += "\t\t]\n"
  329. } else {
  330. s += "\t\tGroups: []\n"
  331. }
  332. s += fmt.Sprintf("\t\tNot before: %v\n", nc.Details.NotBefore)
  333. s += fmt.Sprintf("\t\tNot After: %v\n", nc.Details.NotAfter)
  334. s += fmt.Sprintf("\t\tIs CA: %v\n", nc.Details.IsCA)
  335. s += fmt.Sprintf("\t\tIssuer: %s\n", nc.Details.Issuer)
  336. s += fmt.Sprintf("\t\tPublic key: %x\n", nc.Details.PublicKey)
  337. s += "\t}\n"
  338. fp, err := nc.Sha256Sum()
  339. if err == nil {
  340. s += fmt.Sprintf("\tFingerprint: %s\n", fp)
  341. }
  342. s += fmt.Sprintf("\tSignature: %x\n", nc.Signature)
  343. s += "}"
  344. return s
  345. }
  346. // getRawDetails marshals the raw details into protobuf ready struct
  347. func (nc *NebulaCertificate) getRawDetails() *RawNebulaCertificateDetails {
  348. rd := &RawNebulaCertificateDetails{
  349. Name: nc.Details.Name,
  350. Groups: nc.Details.Groups,
  351. NotBefore: nc.Details.NotBefore.Unix(),
  352. NotAfter: nc.Details.NotAfter.Unix(),
  353. PublicKey: make([]byte, len(nc.Details.PublicKey)),
  354. IsCA: nc.Details.IsCA,
  355. }
  356. for _, ipNet := range nc.Details.Ips {
  357. rd.Ips = append(rd.Ips, ip2int(ipNet.IP), ip2int(ipNet.Mask))
  358. }
  359. for _, ipNet := range nc.Details.Subnets {
  360. rd.Subnets = append(rd.Subnets, ip2int(ipNet.IP), ip2int(ipNet.Mask))
  361. }
  362. copy(rd.PublicKey, nc.Details.PublicKey[:])
  363. // I know, this is terrible
  364. rd.Issuer, _ = hex.DecodeString(nc.Details.Issuer)
  365. return rd
  366. }
  367. // Marshal will marshal a nebula cert into a protobuf byte array
  368. func (nc *NebulaCertificate) Marshal() ([]byte, error) {
  369. rc := RawNebulaCertificate{
  370. Details: nc.getRawDetails(),
  371. Signature: nc.Signature,
  372. }
  373. return proto.Marshal(&rc)
  374. }
  375. // MarshalToPEM will marshal a nebula cert into a protobuf byte array and pem encode the result
  376. func (nc *NebulaCertificate) MarshalToPEM() ([]byte, error) {
  377. b, err := nc.Marshal()
  378. if err != nil {
  379. return nil, err
  380. }
  381. return pem.EncodeToMemory(&pem.Block{Type: CertBanner, Bytes: b}), nil
  382. }
  383. // Sha256Sum calculates a sha-256 sum of the marshaled certificate
  384. func (nc *NebulaCertificate) Sha256Sum() (string, error) {
  385. b, err := nc.Marshal()
  386. if err != nil {
  387. return "", err
  388. }
  389. sum := sha256.Sum256(b)
  390. return hex.EncodeToString(sum[:]), nil
  391. }
  392. func (nc *NebulaCertificate) MarshalJSON() ([]byte, error) {
  393. toString := func(ips []*net.IPNet) []string {
  394. s := []string{}
  395. for _, ip := range ips {
  396. s = append(s, ip.String())
  397. }
  398. return s
  399. }
  400. fp, _ := nc.Sha256Sum()
  401. jc := m{
  402. "details": m{
  403. "name": nc.Details.Name,
  404. "ips": toString(nc.Details.Ips),
  405. "subnets": toString(nc.Details.Subnets),
  406. "groups": nc.Details.Groups,
  407. "notBefore": nc.Details.NotBefore,
  408. "notAfter": nc.Details.NotAfter,
  409. "publicKey": fmt.Sprintf("%x", nc.Details.PublicKey),
  410. "isCa": nc.Details.IsCA,
  411. "issuer": nc.Details.Issuer,
  412. },
  413. "fingerprint": fp,
  414. "signature": fmt.Sprintf("%x", nc.Signature),
  415. }
  416. return json.Marshal(jc)
  417. }
  418. //func (nc *NebulaCertificate) Copy() *NebulaCertificate {
  419. // r, err := nc.Marshal()
  420. // if err != nil {
  421. // //TODO
  422. // return nil
  423. // }
  424. //
  425. // c, err := UnmarshalNebulaCertificate(r)
  426. // return c
  427. //}
  428. func (nc *NebulaCertificate) Copy() *NebulaCertificate {
  429. c := &NebulaCertificate{
  430. Details: NebulaCertificateDetails{
  431. Name: nc.Details.Name,
  432. Groups: make([]string, len(nc.Details.Groups)),
  433. Ips: make([]*net.IPNet, len(nc.Details.Ips)),
  434. Subnets: make([]*net.IPNet, len(nc.Details.Subnets)),
  435. NotBefore: nc.Details.NotBefore,
  436. NotAfter: nc.Details.NotAfter,
  437. PublicKey: make([]byte, len(nc.Details.PublicKey)),
  438. IsCA: nc.Details.IsCA,
  439. Issuer: nc.Details.Issuer,
  440. InvertedGroups: make(map[string]struct{}, len(nc.Details.InvertedGroups)),
  441. },
  442. Signature: make([]byte, len(nc.Signature)),
  443. }
  444. copy(c.Signature, nc.Signature)
  445. copy(c.Details.Groups, nc.Details.Groups)
  446. copy(c.Details.PublicKey, nc.Details.PublicKey)
  447. for i, p := range nc.Details.Ips {
  448. c.Details.Ips[i] = &net.IPNet{
  449. IP: make(net.IP, len(p.IP)),
  450. Mask: make(net.IPMask, len(p.Mask)),
  451. }
  452. copy(c.Details.Ips[i].IP, p.IP)
  453. copy(c.Details.Ips[i].Mask, p.Mask)
  454. }
  455. for i, p := range nc.Details.Subnets {
  456. c.Details.Subnets[i] = &net.IPNet{
  457. IP: make(net.IP, len(p.IP)),
  458. Mask: make(net.IPMask, len(p.Mask)),
  459. }
  460. copy(c.Details.Subnets[i].IP, p.IP)
  461. copy(c.Details.Subnets[i].Mask, p.Mask)
  462. }
  463. for g := range nc.Details.InvertedGroups {
  464. c.Details.InvertedGroups[g] = struct{}{}
  465. }
  466. return c
  467. }
  468. func netMatch(certIp *net.IPNet, rootIps []*net.IPNet) bool {
  469. for _, net := range rootIps {
  470. if net.Contains(certIp.IP) && maskContains(net.Mask, certIp.Mask) {
  471. return true
  472. }
  473. }
  474. return false
  475. }
  476. func maskContains(caMask, certMask net.IPMask) bool {
  477. caM := maskTo4(caMask)
  478. cM := maskTo4(certMask)
  479. // Make sure forcing to ipv4 didn't nuke us
  480. if caM == nil || cM == nil {
  481. return false
  482. }
  483. // Make sure the cert mask is not greater than the ca mask
  484. for i := 0; i < len(caMask); i++ {
  485. if caM[i] > cM[i] {
  486. return false
  487. }
  488. }
  489. return true
  490. }
  491. func maskTo4(ip net.IPMask) net.IPMask {
  492. if len(ip) == net.IPv4len {
  493. return ip
  494. }
  495. if len(ip) == net.IPv6len && isZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
  496. return ip[12:16]
  497. }
  498. return nil
  499. }
  500. func isZeros(b []byte) bool {
  501. for i := 0; i < len(b); i++ {
  502. if b[i] != 0 {
  503. return false
  504. }
  505. }
  506. return true
  507. }
  508. func ip2int(ip []byte) uint32 {
  509. if len(ip) == 16 {
  510. return binary.BigEndian.Uint32(ip[12:16])
  511. }
  512. return binary.BigEndian.Uint32(ip)
  513. }
  514. func int2ip(nn uint32) net.IP {
  515. ip := make(net.IP, net.IPv4len)
  516. binary.BigEndian.PutUint32(ip, nn)
  517. return ip
  518. }