2021-05-23 19:42:26 +00:00
|
|
|
package core
|
2018-06-09 23:44:59 +00:00
|
|
|
|
|
|
|
// This file contains the version metadata struct
|
2019-11-29 09:45:02 +00:00
|
|
|
// Used in the initial connection setup and key exchange
|
2018-06-09 23:44:59 +00:00
|
|
|
// Some of this could arguably go in wire.go instead
|
|
|
|
|
2021-05-08 13:35:58 +00:00
|
|
|
import "crypto/ed25519"
|
2018-12-15 02:49:18 +00:00
|
|
|
|
2018-06-10 23:03:28 +00:00
|
|
|
// This is the version-specific metadata exchanged at the start of a connection.
|
2019-11-29 09:45:02 +00:00
|
|
|
// It must always begin with the 4 bytes "meta" and a wire formatted uint64 major version number.
|
|
|
|
// The current version also includes a minor version number, and the box/sig/link keys that need to be exchanged to open a connection.
|
2018-06-09 23:44:59 +00:00
|
|
|
type version_metadata struct {
|
|
|
|
meta [4]byte
|
2021-05-08 13:35:58 +00:00
|
|
|
ver uint8 // 1 byte in this version
|
2018-06-09 23:44:59 +00:00
|
|
|
// Everything after this point potentially depends on the version number, and is subject to change in future versions
|
2021-05-08 13:35:58 +00:00
|
|
|
minorVer uint8 // 1 byte in this version
|
|
|
|
key ed25519.PublicKey
|
2018-06-09 23:44:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 23:03:28 +00:00
|
|
|
// Gets a base metadata with no keys set, but with the correct version numbers.
|
2018-06-09 23:44:59 +00:00
|
|
|
func version_getBaseMetadata() version_metadata {
|
|
|
|
return version_metadata{
|
|
|
|
meta: [4]byte{'m', 'e', 't', 'a'},
|
|
|
|
ver: 0,
|
2021-06-28 17:21:53 +00:00
|
|
|
minorVer: 4,
|
2018-06-09 23:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 13:42:46 +00:00
|
|
|
// Gets the length of the metadata for this version, used to know how many bytes to read from the start of a connection.
|
2018-06-09 23:44:59 +00:00
|
|
|
func version_getMetaLength() (mlen int) {
|
2021-05-08 13:35:58 +00:00
|
|
|
mlen += 4 // meta
|
|
|
|
mlen++ // ver, as long as it's < 127, which it is in this version
|
|
|
|
mlen++ // minorVer, as long as it's < 127, which it is in this version
|
|
|
|
mlen += ed25519.PublicKeySize // key
|
2018-06-09 23:44:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:03:28 +00:00
|
|
|
// Encodes version metadata into its wire format.
|
2018-06-09 23:44:59 +00:00
|
|
|
func (m *version_metadata) encode() []byte {
|
|
|
|
bs := make([]byte, 0, version_getMetaLength())
|
|
|
|
bs = append(bs, m.meta[:]...)
|
2021-05-08 13:35:58 +00:00
|
|
|
bs = append(bs, m.ver)
|
|
|
|
bs = append(bs, m.minorVer)
|
|
|
|
bs = append(bs, m.key[:]...)
|
2018-06-09 23:44:59 +00:00
|
|
|
if len(bs) != version_getMetaLength() {
|
|
|
|
panic("Inconsistent metadata length")
|
|
|
|
}
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:03:28 +00:00
|
|
|
// Decodes version metadata from its wire format into the struct.
|
2018-06-09 23:44:59 +00:00
|
|
|
func (m *version_metadata) decode(bs []byte) bool {
|
2021-05-08 13:35:58 +00:00
|
|
|
if len(bs) != version_getMetaLength() {
|
2018-06-09 23:44:59 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-05-08 13:35:58 +00:00
|
|
|
offset := 0
|
|
|
|
offset += copy(m.meta[:], bs[offset:])
|
|
|
|
m.ver, offset = bs[offset], offset+1
|
|
|
|
m.minorVer, offset = bs[offset], offset+1
|
|
|
|
m.key = append([]byte(nil), bs[offset:]...)
|
2018-06-09 23:44:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-06-10 23:03:28 +00:00
|
|
|
// Checks that the "meta" bytes and the version numbers are the expected values.
|
2018-06-09 23:44:59 +00:00
|
|
|
func (m *version_metadata) check() bool {
|
|
|
|
base := version_getBaseMetadata()
|
|
|
|
return base.meta == m.meta && base.ver == m.ver && base.minorVer == m.minorVer
|
|
|
|
}
|