2015-01-13 05:31:14 -07:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 12:42:11 -07:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-08-10 04:58:20 -07:00
|
|
|
"crypto/sha256"
|
2014-09-22 12:42:11 -07:00
|
|
|
"encoding/base32"
|
2015-03-20 01:58:32 -07:00
|
|
|
"encoding/binary"
|
2014-09-22 12:42:11 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-11-14 03:57:39 -07:00
|
|
|
const (
|
2024-01-02 09:31:57 -07:00
|
|
|
DeviceIDLength = 32
|
|
|
|
// keep consistent with shortIDStringLength in gui/default/syncthing/app.js
|
2023-11-14 03:57:39 -07:00
|
|
|
ShortIDStringLength = 7
|
|
|
|
)
|
2016-10-29 14:56:24 -07:00
|
|
|
|
2023-11-14 03:57:39 -07:00
|
|
|
type (
|
|
|
|
DeviceID [DeviceIDLength]byte
|
|
|
|
ShortID uint64
|
|
|
|
)
|
2014-09-22 12:42:11 -07:00
|
|
|
|
2016-11-14 23:22:36 -07:00
|
|
|
var (
|
2018-07-12 01:15:57 -07:00
|
|
|
LocalDeviceID = repeatedDeviceID(0xff)
|
|
|
|
GlobalDeviceID = repeatedDeviceID(0xf8)
|
|
|
|
EmptyDeviceID = DeviceID{ /* all zeroes */ }
|
2016-11-14 23:22:36 -07:00
|
|
|
)
|
2014-09-22 12:42:11 -07:00
|
|
|
|
2018-07-12 01:15:57 -07:00
|
|
|
func repeatedDeviceID(v byte) (d DeviceID) {
|
|
|
|
for i := range d {
|
|
|
|
d[i] = v
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// NewDeviceID generates a new device ID from the raw bytes of a certificate
|
|
|
|
func NewDeviceID(rawCert []byte) DeviceID {
|
2020-06-21 11:38:06 -07:00
|
|
|
return DeviceID(sha256.Sum256(rawCert))
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func DeviceIDFromString(s string) (DeviceID, error) {
|
|
|
|
var n DeviceID
|
2014-09-22 12:42:11 -07:00
|
|
|
err := n.UnmarshalText([]byte(s))
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2020-06-07 01:31:12 -07:00
|
|
|
func DeviceIDFromBytes(bs []byte) (DeviceID, error) {
|
2014-09-28 04:00:38 -07:00
|
|
|
var n DeviceID
|
2014-09-22 12:42:11 -07:00
|
|
|
if len(bs) != len(n) {
|
2022-04-27 11:30:13 -07:00
|
|
|
return n, errors.New("incorrect length of byte slice representing device ID")
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
copy(n[:], bs)
|
2020-06-07 01:31:12 -07:00
|
|
|
return n, nil
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// String returns the canonical string representation of the device ID
|
|
|
|
func (n DeviceID) String() string {
|
2016-11-14 23:22:36 -07:00
|
|
|
if n == EmptyDeviceID {
|
|
|
|
return ""
|
|
|
|
}
|
2014-09-22 12:42:11 -07:00
|
|
|
id := base32.StdEncoding.EncodeToString(n[:])
|
|
|
|
id = strings.Trim(id, "=")
|
|
|
|
id, err := luhnify(id)
|
|
|
|
if err != nil {
|
|
|
|
// Should never happen
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
id = chunkify(id)
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (n DeviceID) GoString() string {
|
2014-09-22 12:42:11 -07:00
|
|
|
return n.String()
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (n DeviceID) Compare(other DeviceID) int {
|
2014-09-22 12:42:11 -07:00
|
|
|
return bytes.Compare(n[:], other[:])
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (n DeviceID) Equals(other DeviceID) bool {
|
2016-03-31 08:12:46 -07:00
|
|
|
return bytes.Equal(n[:], other[:])
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
2015-03-20 01:58:32 -07:00
|
|
|
// Short returns an integer representing bits 0-63 of the device ID.
|
2016-01-20 12:10:22 -07:00
|
|
|
func (n DeviceID) Short() ShortID {
|
|
|
|
return ShortID(binary.BigEndian.Uint64(n[:]))
|
2015-03-20 01:58:32 -07:00
|
|
|
}
|
|
|
|
|
2020-10-22 10:54:35 -07:00
|
|
|
func (n DeviceID) MarshalText() ([]byte, error) {
|
2014-09-22 12:42:11 -07:00
|
|
|
return []byte(n.String()), nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 12:10:22 -07:00
|
|
|
func (s ShortID) String() string {
|
2016-12-21 09:35:20 -07:00
|
|
|
if s == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2016-01-20 12:10:22 -07:00
|
|
|
var bs [8]byte
|
|
|
|
binary.BigEndian.PutUint64(bs[:], uint64(s))
|
2023-11-14 03:57:39 -07:00
|
|
|
return base32.StdEncoding.EncodeToString(bs[:])[:ShortIDStringLength]
|
2016-01-20 12:10:22 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func (n *DeviceID) UnmarshalText(bs []byte) error {
|
2014-09-22 12:42:11 -07:00
|
|
|
id := string(bs)
|
|
|
|
id = strings.Trim(id, "=")
|
|
|
|
id = strings.ToUpper(id)
|
|
|
|
id = untypeoify(id)
|
|
|
|
id = unchunkify(id)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
switch len(id) {
|
2016-11-14 23:22:36 -07:00
|
|
|
case 0:
|
|
|
|
*n = EmptyDeviceID
|
|
|
|
return nil
|
2014-09-22 12:42:11 -07:00
|
|
|
case 56:
|
|
|
|
// New style, with check digits
|
|
|
|
id, err = unluhnify(id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
case 52:
|
|
|
|
// Old style, no check digits
|
|
|
|
dec, err := base32.StdEncoding.DecodeString(id + "====")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(n[:], dec)
|
|
|
|
return nil
|
|
|
|
default:
|
2017-04-26 22:45:35 -07:00
|
|
|
return fmt.Errorf("%q: device ID invalid: incorrect length", bs)
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 11:30:13 -07:00
|
|
|
func (*DeviceID) ProtoSize() int {
|
2016-10-29 14:56:24 -07:00
|
|
|
// Used by protobuf marshaller.
|
|
|
|
return DeviceIDLength
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *DeviceID) MarshalTo(bs []byte) (int, error) {
|
|
|
|
// Used by protobuf marshaller.
|
|
|
|
if len(bs) < DeviceIDLength {
|
|
|
|
return 0, errors.New("destination too short")
|
|
|
|
}
|
|
|
|
copy(bs, (*n)[:])
|
|
|
|
return DeviceIDLength, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *DeviceID) Unmarshal(bs []byte) error {
|
|
|
|
// Used by protobuf marshaller.
|
|
|
|
if len(bs) < DeviceIDLength {
|
2017-04-26 22:45:35 -07:00
|
|
|
return fmt.Errorf("%q: not enough data", bs)
|
2016-10-29 14:56:24 -07:00
|
|
|
}
|
|
|
|
copy((*n)[:], bs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-22 12:42:11 -07:00
|
|
|
func luhnify(s string) (string, error) {
|
|
|
|
if len(s) != 52 {
|
|
|
|
panic("unsupported string length")
|
|
|
|
}
|
|
|
|
|
2017-09-03 03:26:12 -07:00
|
|
|
res := make([]byte, 4*(13+1))
|
2014-09-22 12:42:11 -07:00
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
p := s[i*13 : (i+1)*13]
|
2017-09-03 03:26:12 -07:00
|
|
|
copy(res[i*(13+1):], p)
|
2020-03-29 13:28:04 -07:00
|
|
|
l, err := luhn32(p)
|
2014-09-22 12:42:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-03 03:26:12 -07:00
|
|
|
res[(i+1)*(13)+i] = byte(l)
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
2017-09-03 03:26:12 -07:00
|
|
|
return string(res), nil
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func unluhnify(s string) (string, error) {
|
|
|
|
if len(s) != 56 {
|
2017-04-26 22:45:35 -07:00
|
|
|
return "", fmt.Errorf("%q: unsupported string length %d", s, len(s))
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
2017-09-03 03:26:12 -07:00
|
|
|
res := make([]byte, 52)
|
2014-09-22 12:42:11 -07:00
|
|
|
for i := 0; i < 4; i++ {
|
2017-09-03 03:26:12 -07:00
|
|
|
p := s[i*(13+1) : (i+1)*(13+1)-1]
|
|
|
|
copy(res[i*13:], p)
|
2020-03-29 13:28:04 -07:00
|
|
|
l, err := luhn32(p)
|
2014-09-22 12:42:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-03 03:26:12 -07:00
|
|
|
if s[(i+1)*14-1] != byte(l) {
|
2017-04-26 22:45:35 -07:00
|
|
|
return "", fmt.Errorf("%q: check digit incorrect", s)
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 03:26:12 -07:00
|
|
|
return string(res), nil
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func chunkify(s string) string {
|
2017-09-03 03:26:12 -07:00
|
|
|
chunks := len(s) / 7
|
|
|
|
res := make([]byte, chunks*(7+1)-1)
|
|
|
|
for i := 0; i < chunks; i++ {
|
|
|
|
if i > 0 {
|
|
|
|
res[i*(7+1)-1] = '-'
|
|
|
|
}
|
|
|
|
copy(res[i*(7+1):], s[i*7:(i+1)*7])
|
|
|
|
}
|
|
|
|
return string(res)
|
2014-09-22 12:42:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func unchunkify(s string) string {
|
2021-03-17 15:12:26 -07:00
|
|
|
s = strings.ReplaceAll(s, "-", "")
|
|
|
|
s = strings.ReplaceAll(s, " ", "")
|
2014-09-22 12:42:11 -07:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func untypeoify(s string) string {
|
2021-03-17 15:12:26 -07:00
|
|
|
s = strings.ReplaceAll(s, "0", "O")
|
|
|
|
s = strings.ReplaceAll(s, "1", "I")
|
|
|
|
s = strings.ReplaceAll(s, "8", "B")
|
2014-09-22 12:42:11 -07:00
|
|
|
return s
|
|
|
|
}
|