deviceid.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package protocol
  16. import (
  17. "bytes"
  18. "crypto/sha256"
  19. "encoding/base32"
  20. "errors"
  21. "fmt"
  22. "regexp"
  23. "strings"
  24. "github.com/syncthing/syncthing/internal/luhn"
  25. )
  26. type DeviceID [32]byte
  27. var LocalDeviceID = DeviceID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  28. // NewDeviceID generates a new device ID from the raw bytes of a certificate
  29. func NewDeviceID(rawCert []byte) DeviceID {
  30. var n DeviceID
  31. hf := sha256.New()
  32. hf.Write(rawCert)
  33. hf.Sum(n[:0])
  34. return n
  35. }
  36. func DeviceIDFromString(s string) (DeviceID, error) {
  37. var n DeviceID
  38. err := n.UnmarshalText([]byte(s))
  39. return n, err
  40. }
  41. func DeviceIDFromBytes(bs []byte) DeviceID {
  42. var n DeviceID
  43. if len(bs) != len(n) {
  44. panic("incorrect length of byte slice representing device ID")
  45. }
  46. copy(n[:], bs)
  47. return n
  48. }
  49. // String returns the canonical string representation of the device ID
  50. func (n DeviceID) String() string {
  51. id := base32.StdEncoding.EncodeToString(n[:])
  52. id = strings.Trim(id, "=")
  53. id, err := luhnify(id)
  54. if err != nil {
  55. // Should never happen
  56. panic(err)
  57. }
  58. id = chunkify(id)
  59. return id
  60. }
  61. func (n DeviceID) GoString() string {
  62. return n.String()
  63. }
  64. func (n DeviceID) Compare(other DeviceID) int {
  65. return bytes.Compare(n[:], other[:])
  66. }
  67. func (n DeviceID) Equals(other DeviceID) bool {
  68. return bytes.Compare(n[:], other[:]) == 0
  69. }
  70. func (n *DeviceID) MarshalText() ([]byte, error) {
  71. return []byte(n.String()), nil
  72. }
  73. func (n *DeviceID) UnmarshalText(bs []byte) error {
  74. id := string(bs)
  75. id = strings.Trim(id, "=")
  76. id = strings.ToUpper(id)
  77. id = untypeoify(id)
  78. id = unchunkify(id)
  79. var err error
  80. switch len(id) {
  81. case 56:
  82. // New style, with check digits
  83. id, err = unluhnify(id)
  84. if err != nil {
  85. return err
  86. }
  87. fallthrough
  88. case 52:
  89. // Old style, no check digits
  90. dec, err := base32.StdEncoding.DecodeString(id + "====")
  91. if err != nil {
  92. return err
  93. }
  94. copy(n[:], dec)
  95. return nil
  96. default:
  97. return errors.New("device ID invalid: incorrect length")
  98. }
  99. }
  100. func luhnify(s string) (string, error) {
  101. if len(s) != 52 {
  102. panic("unsupported string length")
  103. }
  104. res := make([]string, 0, 4)
  105. for i := 0; i < 4; i++ {
  106. p := s[i*13 : (i+1)*13]
  107. l, err := luhn.Base32.Generate(p)
  108. if err != nil {
  109. return "", err
  110. }
  111. res = append(res, fmt.Sprintf("%s%c", p, l))
  112. }
  113. return res[0] + res[1] + res[2] + res[3], nil
  114. }
  115. func unluhnify(s string) (string, error) {
  116. if len(s) != 56 {
  117. return "", fmt.Errorf("unsupported string length %d", len(s))
  118. }
  119. res := make([]string, 0, 4)
  120. for i := 0; i < 4; i++ {
  121. p := s[i*14 : (i+1)*14-1]
  122. l, err := luhn.Base32.Generate(p)
  123. if err != nil {
  124. return "", err
  125. }
  126. if g := fmt.Sprintf("%s%c", p, l); g != s[i*14:(i+1)*14] {
  127. return "", errors.New("check digit incorrect")
  128. }
  129. res = append(res, p)
  130. }
  131. return res[0] + res[1] + res[2] + res[3], nil
  132. }
  133. func chunkify(s string) string {
  134. s = regexp.MustCompile("(.{7})").ReplaceAllString(s, "$1-")
  135. s = strings.Trim(s, "-")
  136. return s
  137. }
  138. func unchunkify(s string) string {
  139. s = strings.Replace(s, "-", "", -1)
  140. s = strings.Replace(s, " ", "", -1)
  141. return s
  142. }
  143. func untypeoify(s string) string {
  144. s = strings.Replace(s, "0", "O", -1)
  145. s = strings.Replace(s, "1", "I", -1)
  146. s = strings.Replace(s, "8", "B", -1)
  147. return s
  148. }