deviceid.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "bytes"
  5. "encoding/base32"
  6. "encoding/binary"
  7. "errors"
  8. "fmt"
  9. "strings"
  10. "github.com/syncthing/syncthing/lib/sha256"
  11. )
  12. const DeviceIDLength = 32
  13. type DeviceID [DeviceIDLength]byte
  14. type ShortID uint64
  15. var (
  16. LocalDeviceID = repeatedDeviceID(0xff)
  17. GlobalDeviceID = repeatedDeviceID(0xf8)
  18. EmptyDeviceID = DeviceID{ /* all zeroes */ }
  19. )
  20. func repeatedDeviceID(v byte) (d DeviceID) {
  21. for i := range d {
  22. d[i] = v
  23. }
  24. return
  25. }
  26. // NewDeviceID generates a new device ID from the raw bytes of a certificate
  27. func NewDeviceID(rawCert []byte) DeviceID {
  28. return DeviceID(sha256.Sum256(rawCert))
  29. }
  30. func DeviceIDFromString(s string) (DeviceID, error) {
  31. var n DeviceID
  32. err := n.UnmarshalText([]byte(s))
  33. return n, err
  34. }
  35. func DeviceIDFromBytes(bs []byte) (DeviceID, error) {
  36. var n DeviceID
  37. if len(bs) != len(n) {
  38. return n, errors.New("incorrect length of byte slice representing device ID")
  39. }
  40. copy(n[:], bs)
  41. return n, nil
  42. }
  43. // String returns the canonical string representation of the device ID
  44. func (n DeviceID) String() string {
  45. if n == EmptyDeviceID {
  46. return ""
  47. }
  48. id := base32.StdEncoding.EncodeToString(n[:])
  49. id = strings.Trim(id, "=")
  50. id, err := luhnify(id)
  51. if err != nil {
  52. // Should never happen
  53. panic(err)
  54. }
  55. id = chunkify(id)
  56. return id
  57. }
  58. func (n DeviceID) GoString() string {
  59. return n.String()
  60. }
  61. func (n DeviceID) Compare(other DeviceID) int {
  62. return bytes.Compare(n[:], other[:])
  63. }
  64. func (n DeviceID) Equals(other DeviceID) bool {
  65. return bytes.Equal(n[:], other[:])
  66. }
  67. // Short returns an integer representing bits 0-63 of the device ID.
  68. func (n DeviceID) Short() ShortID {
  69. return ShortID(binary.BigEndian.Uint64(n[:]))
  70. }
  71. func (n DeviceID) MarshalText() ([]byte, error) {
  72. return []byte(n.String()), nil
  73. }
  74. func (s ShortID) String() string {
  75. if s == 0 {
  76. return ""
  77. }
  78. var bs [8]byte
  79. binary.BigEndian.PutUint64(bs[:], uint64(s))
  80. return base32.StdEncoding.EncodeToString(bs[:])[:7]
  81. }
  82. func (n *DeviceID) UnmarshalText(bs []byte) error {
  83. id := string(bs)
  84. id = strings.Trim(id, "=")
  85. id = strings.ToUpper(id)
  86. id = untypeoify(id)
  87. id = unchunkify(id)
  88. var err error
  89. switch len(id) {
  90. case 0:
  91. *n = EmptyDeviceID
  92. return nil
  93. case 56:
  94. // New style, with check digits
  95. id, err = unluhnify(id)
  96. if err != nil {
  97. return err
  98. }
  99. fallthrough
  100. case 52:
  101. // Old style, no check digits
  102. dec, err := base32.StdEncoding.DecodeString(id + "====")
  103. if err != nil {
  104. return err
  105. }
  106. copy(n[:], dec)
  107. return nil
  108. default:
  109. return fmt.Errorf("%q: device ID invalid: incorrect length", bs)
  110. }
  111. }
  112. func (*DeviceID) ProtoSize() int {
  113. // Used by protobuf marshaller.
  114. return DeviceIDLength
  115. }
  116. func (n *DeviceID) MarshalTo(bs []byte) (int, error) {
  117. // Used by protobuf marshaller.
  118. if len(bs) < DeviceIDLength {
  119. return 0, errors.New("destination too short")
  120. }
  121. copy(bs, (*n)[:])
  122. return DeviceIDLength, nil
  123. }
  124. func (n *DeviceID) Unmarshal(bs []byte) error {
  125. // Used by protobuf marshaller.
  126. if len(bs) < DeviceIDLength {
  127. return fmt.Errorf("%q: not enough data", bs)
  128. }
  129. copy((*n)[:], bs)
  130. return nil
  131. }
  132. func luhnify(s string) (string, error) {
  133. if len(s) != 52 {
  134. panic("unsupported string length")
  135. }
  136. res := make([]byte, 4*(13+1))
  137. for i := 0; i < 4; i++ {
  138. p := s[i*13 : (i+1)*13]
  139. copy(res[i*(13+1):], p)
  140. l, err := luhn32(p)
  141. if err != nil {
  142. return "", err
  143. }
  144. res[(i+1)*(13)+i] = byte(l)
  145. }
  146. return string(res), nil
  147. }
  148. func unluhnify(s string) (string, error) {
  149. if len(s) != 56 {
  150. return "", fmt.Errorf("%q: unsupported string length %d", s, len(s))
  151. }
  152. res := make([]byte, 52)
  153. for i := 0; i < 4; i++ {
  154. p := s[i*(13+1) : (i+1)*(13+1)-1]
  155. copy(res[i*13:], p)
  156. l, err := luhn32(p)
  157. if err != nil {
  158. return "", err
  159. }
  160. if s[(i+1)*14-1] != byte(l) {
  161. return "", fmt.Errorf("%q: check digit incorrect", s)
  162. }
  163. }
  164. return string(res), nil
  165. }
  166. func chunkify(s string) string {
  167. chunks := len(s) / 7
  168. res := make([]byte, chunks*(7+1)-1)
  169. for i := 0; i < chunks; i++ {
  170. if i > 0 {
  171. res[i*(7+1)-1] = '-'
  172. }
  173. copy(res[i*(7+1):], s[i*7:(i+1)*7])
  174. }
  175. return string(res)
  176. }
  177. func unchunkify(s string) string {
  178. s = strings.ReplaceAll(s, "-", "")
  179. s = strings.ReplaceAll(s, " ", "")
  180. return s
  181. }
  182. func untypeoify(s string) string {
  183. s = strings.ReplaceAll(s, "0", "O")
  184. s = strings.ReplaceAll(s, "1", "I")
  185. s = strings.ReplaceAll(s, "8", "B")
  186. return s
  187. }