2015-01-13 12:31:14 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base32"
|
2015-03-20 08:58:32 +00:00
|
|
|
"encoding/binary"
|
2014-09-22 19:42:11 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-27 05:45:35 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2016-10-29 21:56:24 +00:00
|
|
|
const DeviceIDLength = 32
|
|
|
|
|
|
|
|
type DeviceID [DeviceIDLength]byte
|
2016-01-20 19:10:22 +00:00
|
|
|
type ShortID uint64
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-11-15 06:22:36 +00:00
|
|
|
var (
|
2018-07-12 08:15:57 +00:00
|
|
|
LocalDeviceID = repeatedDeviceID(0xff)
|
|
|
|
GlobalDeviceID = repeatedDeviceID(0xf8)
|
|
|
|
EmptyDeviceID = DeviceID{ /* all zeroes */ }
|
2016-11-15 06:22:36 +00:00
|
|
|
)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2018-07-12 08:15:57 +00:00
|
|
|
func repeatedDeviceID(v byte) (d DeviceID) {
|
|
|
|
for i := range d {
|
|
|
|
d[i] = v
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// NewDeviceID generates a new device ID from the raw bytes of a certificate
|
|
|
|
func NewDeviceID(rawCert []byte) DeviceID {
|
|
|
|
var n DeviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
hf := sha256.New()
|
2019-02-02 11:16:27 +00:00
|
|
|
hf.Write(rawCert)
|
2014-09-22 19:42:11 +00:00
|
|
|
hf.Sum(n[:0])
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func DeviceIDFromString(s string) (DeviceID, error) {
|
|
|
|
var n DeviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
err := n.UnmarshalText([]byte(s))
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func DeviceIDFromBytes(bs []byte) DeviceID {
|
|
|
|
var n DeviceID
|
2014-09-22 19:42:11 +00:00
|
|
|
if len(bs) != len(n) {
|
2014-09-28 11:00:38 +00:00
|
|
|
panic("incorrect length of byte slice representing device ID")
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
copy(n[:], bs)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// String returns the canonical string representation of the device ID
|
|
|
|
func (n DeviceID) String() string {
|
2016-11-15 06:22:36 +00:00
|
|
|
if n == EmptyDeviceID {
|
|
|
|
return ""
|
|
|
|
}
|
2014-09-22 19:42:11 +00: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 11:00:38 +00:00
|
|
|
func (n DeviceID) GoString() string {
|
2014-09-22 19:42:11 +00:00
|
|
|
return n.String()
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (n DeviceID) Compare(other DeviceID) int {
|
2014-09-22 19:42:11 +00:00
|
|
|
return bytes.Compare(n[:], other[:])
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (n DeviceID) Equals(other DeviceID) bool {
|
2016-03-31 15:12:46 +00:00
|
|
|
return bytes.Equal(n[:], other[:])
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:58:32 +00:00
|
|
|
// Short returns an integer representing bits 0-63 of the device ID.
|
2016-01-20 19:10:22 +00:00
|
|
|
func (n DeviceID) Short() ShortID {
|
|
|
|
return ShortID(binary.BigEndian.Uint64(n[:]))
|
2015-03-20 08:58:32 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (n *DeviceID) MarshalText() ([]byte, error) {
|
2014-09-22 19:42:11 +00:00
|
|
|
return []byte(n.String()), nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 19:10:22 +00:00
|
|
|
func (s ShortID) String() string {
|
2016-12-21 16:35:20 +00:00
|
|
|
if s == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2016-01-20 19:10:22 +00:00
|
|
|
var bs [8]byte
|
|
|
|
binary.BigEndian.PutUint64(bs[:], uint64(s))
|
|
|
|
return base32.StdEncoding.EncodeToString(bs[:])[:7]
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func (n *DeviceID) UnmarshalText(bs []byte) error {
|
2014-09-22 19:42:11 +00: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-15 06:22:36 +00:00
|
|
|
case 0:
|
|
|
|
*n = EmptyDeviceID
|
|
|
|
return nil
|
2014-09-22 19:42:11 +00: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-27 05:45:35 +00:00
|
|
|
return fmt.Errorf("%q: device ID invalid: incorrect length", bs)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 21:56:24 +00:00
|
|
|
func (n *DeviceID) ProtoSize() int {
|
|
|
|
// 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-27 05:45:35 +00:00
|
|
|
return fmt.Errorf("%q: not enough data", bs)
|
2016-10-29 21:56:24 +00:00
|
|
|
}
|
|
|
|
copy((*n)[:], bs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
func luhnify(s string) (string, error) {
|
|
|
|
if len(s) != 52 {
|
|
|
|
panic("unsupported string length")
|
|
|
|
}
|
|
|
|
|
2017-09-03 10:26:12 +00:00
|
|
|
res := make([]byte, 4*(13+1))
|
2014-09-22 19:42:11 +00:00
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
p := s[i*13 : (i+1)*13]
|
2017-09-03 10:26:12 +00:00
|
|
|
copy(res[i*(13+1):], p)
|
2020-03-29 20:28:04 +00:00
|
|
|
l, err := luhn32(p)
|
2014-09-22 19:42:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-03 10:26:12 +00:00
|
|
|
res[(i+1)*(13)+i] = byte(l)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
2017-09-03 10:26:12 +00:00
|
|
|
return string(res), nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unluhnify(s string) (string, error) {
|
|
|
|
if len(s) != 56 {
|
2017-04-27 05:45:35 +00:00
|
|
|
return "", fmt.Errorf("%q: unsupported string length %d", s, len(s))
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 10:26:12 +00:00
|
|
|
res := make([]byte, 52)
|
2014-09-22 19:42:11 +00:00
|
|
|
for i := 0; i < 4; i++ {
|
2017-09-03 10:26:12 +00:00
|
|
|
p := s[i*(13+1) : (i+1)*(13+1)-1]
|
|
|
|
copy(res[i*13:], p)
|
2020-03-29 20:28:04 +00:00
|
|
|
l, err := luhn32(p)
|
2014-09-22 19:42:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-03 10:26:12 +00:00
|
|
|
if s[(i+1)*14-1] != byte(l) {
|
2017-04-27 05:45:35 +00:00
|
|
|
return "", fmt.Errorf("%q: check digit incorrect", s)
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 10:26:12 +00:00
|
|
|
return string(res), nil
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func chunkify(s string) string {
|
2017-09-03 10:26:12 +00: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 19:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unchunkify(s string) string {
|
|
|
|
s = strings.Replace(s, "-", "", -1)
|
|
|
|
s = strings.Replace(s, " ", "", -1)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func untypeoify(s string) string {
|
|
|
|
s = strings.Replace(s, "0", "O", -1)
|
|
|
|
s = strings.Replace(s, "1", "I", -1)
|
|
|
|
s = strings.Replace(s, "8", "B", -1)
|
|
|
|
return s
|
|
|
|
}
|
2016-11-17 06:45:45 +00:00
|
|
|
|
|
|
|
// DeviceIDs is a sortable slice of DeviceID
|
|
|
|
type DeviceIDs []DeviceID
|
|
|
|
|
|
|
|
func (l DeviceIDs) Len() int {
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DeviceIDs) Less(a, b int) bool {
|
|
|
|
return l[a].Compare(l[b]) == -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DeviceIDs) Swap(a, b int) {
|
|
|
|
l[a], l[b] = l[b], l[a]
|
|
|
|
}
|