2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2021-07-29 19:59:40 +01:00
|
|
|
|
2021-12-03 02:04:48 +00:00
|
|
|
package controlbase
|
2021-07-29 19:59:40 +01:00
|
|
|
|
|
|
|
import "encoding/binary"
|
|
|
|
|
2021-07-30 19:38:10 +01:00
|
|
|
const (
|
|
|
|
// msgTypeInitiation frames carry a Noise IK handshake initiation message.
|
|
|
|
msgTypeInitiation = 1
|
|
|
|
// msgTypeResponse frames carry a Noise IK handshake response message.
|
|
|
|
msgTypeResponse = 2
|
|
|
|
// msgTypeError frames carry an unauthenticated human-readable
|
|
|
|
// error message.
|
|
|
|
//
|
|
|
|
// Errors reported in this message type must be treated as public
|
|
|
|
// hints only. They are not encrypted or authenticated, and so can
|
|
|
|
// be seen and tampered with on the wire.
|
|
|
|
msgTypeError = 3
|
2021-10-26 00:41:30 +01:00
|
|
|
// msgTypeRecord frames carry session data bytes.
|
2021-07-30 19:38:10 +01:00
|
|
|
msgTypeRecord = 4
|
|
|
|
|
2021-10-26 01:24:32 +01:00
|
|
|
// headerLen is the size of the header on all messages except msgTypeInitiation.
|
|
|
|
headerLen = 3
|
|
|
|
// initiationHeaderLen is the size of the header on all msgTypeInitiation messages.
|
|
|
|
initiationHeaderLen = 5
|
|
|
|
)
|
2021-07-29 19:59:40 +01:00
|
|
|
|
2021-10-26 00:41:30 +01:00
|
|
|
// initiationMessage is the protocol message sent from a client
|
|
|
|
// machine to a control server.
|
2021-07-29 19:59:40 +01:00
|
|
|
//
|
2021-10-26 01:24:32 +01:00
|
|
|
// 2b: protocol version
|
|
|
|
// 1b: message type (0x01)
|
|
|
|
// 2b: payload length (96)
|
2021-07-29 19:59:40 +01:00
|
|
|
// 5b: header (see headerLen for fields)
|
|
|
|
// 32b: client ephemeral public key (cleartext)
|
|
|
|
// 48b: client machine public key (encrypted)
|
|
|
|
// 16b: message tag (authenticates the whole message)
|
|
|
|
type initiationMessage [101]byte
|
|
|
|
|
2022-04-07 20:03:32 +01:00
|
|
|
func mkInitiationMessage(protocolVersion uint16) initiationMessage {
|
2021-07-29 19:59:40 +01:00
|
|
|
var ret initiationMessage
|
2022-04-07 20:03:32 +01:00
|
|
|
binary.BigEndian.PutUint16(ret[:2], protocolVersion)
|
2021-10-26 01:24:32 +01:00
|
|
|
ret[2] = msgTypeInitiation
|
|
|
|
binary.BigEndian.PutUint16(ret[3:5], uint16(len(ret.Payload())))
|
2021-07-29 19:59:40 +01:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-10-26 01:24:32 +01:00
|
|
|
func (m *initiationMessage) Header() []byte { return m[:initiationHeaderLen] }
|
|
|
|
func (m *initiationMessage) Payload() []byte { return m[initiationHeaderLen:] }
|
2021-07-29 19:59:40 +01:00
|
|
|
|
2021-10-26 01:24:32 +01:00
|
|
|
func (m *initiationMessage) Version() uint16 { return binary.BigEndian.Uint16(m[:2]) }
|
|
|
|
func (m *initiationMessage) Type() byte { return m[2] }
|
|
|
|
func (m *initiationMessage) Length() int { return int(binary.BigEndian.Uint16(m[3:5])) }
|
2021-07-29 19:59:40 +01:00
|
|
|
|
2021-10-26 01:24:32 +01:00
|
|
|
func (m *initiationMessage) EphemeralPub() []byte {
|
|
|
|
return m[initiationHeaderLen : initiationHeaderLen+32]
|
|
|
|
}
|
|
|
|
func (m *initiationMessage) MachinePub() []byte {
|
|
|
|
return m[initiationHeaderLen+32 : initiationHeaderLen+32+48]
|
|
|
|
}
|
|
|
|
func (m *initiationMessage) Tag() []byte { return m[initiationHeaderLen+32+48:] }
|
2021-07-29 19:59:40 +01:00
|
|
|
|
2021-10-26 00:41:30 +01:00
|
|
|
// responseMessage is the protocol message sent from a control server
|
|
|
|
// to a client machine.
|
2021-07-29 19:59:40 +01:00
|
|
|
//
|
2021-10-26 01:24:32 +01:00
|
|
|
// 1b: message type (0x02)
|
|
|
|
// 2b: payload length (48)
|
2021-07-29 19:59:40 +01:00
|
|
|
// 32b: control ephemeral public key (cleartext)
|
|
|
|
// 16b: message tag (authenticates the whole message)
|
2021-10-26 01:24:32 +01:00
|
|
|
type responseMessage [51]byte
|
2021-07-29 19:59:40 +01:00
|
|
|
|
|
|
|
func mkResponseMessage() responseMessage {
|
|
|
|
var ret responseMessage
|
2021-10-26 01:24:32 +01:00
|
|
|
ret[0] = msgTypeResponse
|
|
|
|
binary.BigEndian.PutUint16(ret[1:], uint16(len(ret.Payload())))
|
2021-07-29 19:59:40 +01:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-07-30 19:38:10 +01:00
|
|
|
func (m *responseMessage) Header() []byte { return m[:headerLen] }
|
|
|
|
func (m *responseMessage) Payload() []byte { return m[headerLen:] }
|
2021-07-29 19:59:40 +01:00
|
|
|
|
2021-10-26 01:24:32 +01:00
|
|
|
func (m *responseMessage) Type() byte { return m[0] }
|
|
|
|
func (m *responseMessage) Length() int { return int(binary.BigEndian.Uint16(m[1:3])) }
|
2021-07-29 19:59:40 +01:00
|
|
|
|
2021-07-30 19:38:10 +01:00
|
|
|
func (m *responseMessage) EphemeralPub() []byte { return m[headerLen : headerLen+32] }
|
|
|
|
func (m *responseMessage) Tag() []byte { return m[headerLen+32:] }
|