deviceid.go 4.4 KB

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