2021-07-29 18:59:40 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-12-03 02:04:48 +00:00
|
|
|
package controlbase
|
2021-07-29 18:59:40 +00:00
|
|
|
|
|
|
|
import "encoding/binary"
|
|
|
|
|
2021-07-30 18:38:10 +00: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-25 23:41:30 +00:00
|
|
|
// msgTypeRecord frames carry session data bytes.
|
2021-07-30 18:38:10 +00:00
|
|
|
msgTypeRecord = 4
|
|
|
|
|
2021-10-26 00:24:32 +00: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 18:59:40 +00:00
|
|
|
|
2021-10-25 23:41:30 +00:00
|
|
|
// initiationMessage is the protocol message sent from a client
|
|
|
|
// machine to a control server.
|
2021-07-29 18:59:40 +00:00
|
|
|
//
|
2021-10-26 00:24:32 +00:00
|
|
|
// 2b: protocol version
|
|
|
|
// 1b: message type (0x01)
|
|
|
|
// 2b: payload length (96)
|
2021-07-29 18:59:40 +00: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
|
|
|
|
|
|
|
|
func mkInitiationMessage() initiationMessage {
|
|
|
|
var ret initiationMessage
|
2021-10-26 00:24:32 +00:00
|
|
|
binary.BigEndian.PutUint16(ret[:2], uint16(protocolVersion))
|
|
|
|
ret[2] = msgTypeInitiation
|
|
|
|
binary.BigEndian.PutUint16(ret[3:5], uint16(len(ret.Payload())))
|
2021-07-29 18:59:40 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-10-26 00:24:32 +00:00
|
|
|
func (m *initiationMessage) Header() []byte { return m[:initiationHeaderLen] }
|
|
|
|
func (m *initiationMessage) Payload() []byte { return m[initiationHeaderLen:] }
|
2021-07-29 18:59:40 +00:00
|
|
|
|
2021-10-26 00:24:32 +00: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 18:59:40 +00:00
|
|
|
|
2021-10-26 00:24:32 +00: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 18:59:40 +00:00
|
|
|
|
2021-10-25 23:41:30 +00:00
|
|
|
// responseMessage is the protocol message sent from a control server
|
|
|
|
// to a client machine.
|
2021-07-29 18:59:40 +00:00
|
|
|
//
|
2021-10-26 00:24:32 +00:00
|
|
|
// 1b: message type (0x02)
|
|
|
|
// 2b: payload length (48)
|
2021-07-29 18:59:40 +00:00
|
|
|
// 32b: control ephemeral public key (cleartext)
|
|
|
|
// 16b: message tag (authenticates the whole message)
|
2021-10-26 00:24:32 +00:00
|
|
|
type responseMessage [51]byte
|
2021-07-29 18:59:40 +00:00
|
|
|
|
|
|
|
func mkResponseMessage() responseMessage {
|
|
|
|
var ret responseMessage
|
2021-10-26 00:24:32 +00:00
|
|
|
ret[0] = msgTypeResponse
|
|
|
|
binary.BigEndian.PutUint16(ret[1:], uint16(len(ret.Payload())))
|
2021-07-29 18:59:40 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:38:10 +00:00
|
|
|
func (m *responseMessage) Header() []byte { return m[:headerLen] }
|
|
|
|
func (m *responseMessage) Payload() []byte { return m[headerLen:] }
|
2021-07-29 18:59:40 +00:00
|
|
|
|
2021-10-26 00:24:32 +00: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 18:59:40 +00:00
|
|
|
|
2021-07-30 18:38:10 +00:00
|
|
|
func (m *responseMessage) EphemeralPub() []byte { return m[headerLen : headerLen+32] }
|
|
|
|
func (m *responseMessage) Tag() []byte { return m[headerLen+32:] }
|