ctrlmsg

package
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: GPL-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ctrlmsg has the messages that travel inside the TLS session of the control channel: the key exchange message (OpenVPN's "key method 2"), the options string and the peer info that go in it, and, in later steps, the text messages (PUSH_REPLY, AUTH_FAILED, HALT, RESTART).

The functions are pure: bytes to structs and back, with explicit bounds on every length. Nothing here reads a clock, a random source or a connection, except NewClientKeyExchange, which reads crypto/rand.

Index

Constants

View Source
const (
	// KeyMethod is the only method of the key exchange that is supported.
	KeyMethod = 2
	// PreMasterSize is the size of the random pre-master secret that a client sends.
	PreMasterSize = 48
	// RandomSize is the size of each of the two random strings that each side sends.
	RandomSize = 32
	// MaxOptionsLen is the longest options string, with its NUL: OpenVPN's
	// TLS_OPTIONS_LEN.
	MaxOptionsLen = 512
	// MaxCredentialLen is the longest user name or password, with its NUL:
	// OpenVPN's USER_PASS_LEN when it is built with PKCS#11, as the packages of the
	// systems are; a build without it has 128, and answers a longer one with
	// AUTH_FAILED ("Username or password is too long"). What bounds a credential
	// first is MaxKeyExchangeLen.
	//
	// OpenVPN: misc.h USER_PASS_LEN.
	MaxCredentialLen = 4096
	// MaxKeyExchangeLen is the longest key exchange message: OpenVPN reads the
	// cleartext of the control channel into a buffer of this size, so a server cuts a
	// longer message there, loses the peer info and reads the rest as a control
	// message. OpenVPN's client cannot write a longer one either.
	//
	// OpenVPN: common.h TLS_CHANNEL_BUF_SIZE, ssl.c key_state_init.
	MaxKeyExchangeLen = 2048
)

The sizes of the fields of the key exchange message.

View Source
const (
	// IVProtoDataV2 is the data packet with a peer id: P_DATA_V2.
	IVProtoDataV2 = 1 << 1
	// IVProtoRequestPush is "push before the request": the server may send
	// PUSH_REPLY without waiting for PUSH_REQUEST.
	IVProtoRequestPush = 1 << 2
	// IVProtoTLSKeyExport is the data keys from the TLS exporter.
	IVProtoTLSKeyExport = 1 << 3
	// IVProtoAuthPendingKW is the keywords of AUTH_PENDING.
	IVProtoAuthPendingKW = 1 << 4
	// IVProtoCCExitNotify is the exit message in the control channel.
	IVProtoCCExitNotify = 1 << 7
	// IVProtoAuthFailTemp is AUTH_FAILED,TEMP.
	IVProtoAuthFailTemp = 1 << 8
	// IVProtoDynTLSCrypt is dynamic tls-crypt at a renegotiation.
	IVProtoDynTLSCrypt = 1 << 9
	// IVProtoDataEpoch is the epoch data format.
	IVProtoDataEpoch = 1 << 10
	// IVProtoDNSOptionV2 is the dns option.
	IVProtoDNSOptionV2 = 1 << 11
)

The bits of IV_PROTO: what a client says it can do. A bit is set only by the step that implements the feature (SPEC §8.2).

OpenVPN: ssl.h IV_PROTO_*.

View Source
const (
	// TagUnsupportedPushedOption: the server pushed an option that changes
	// what is on the wire in a way this library does not implement (compression,
	// fragmentation, a data cipher that is not AEAD). Metadata: "option".
	TagUnsupportedPushedOption = "UnsupportedPushedOption"
	// TagCipherNegotiationFailed: the server pushed a data cipher that the
	// client did not offer. Metadata: "cipher".
	TagCipherNegotiationFailed = "CipherNegotiationFailed"

	// TagPushedOptionRejected: a pull filter of the profile with the action reject
	// matched a pushed option. Metadata: "option" (the name of the option, never its
	// arguments).
	TagPushedOptionRejected = "PushedOptionRejected"
)

Tags of the errors about the options that a server pushes.

View Source
const Exit = "EXIT"

Exit is the text of the message by which a peer says that it has ended the session. A client sends it from Close when the server pushed cc-exit, and a server sends it when it stops.

View Source
const PushRequest = "PUSH_REQUEST"

PushRequest is the text of the message that asks the server for the settings.

View Source
const TagKeyExchangeTooLong = "KeyExchangeTooLong"

TagKeyExchangeTooLong is the tag of the error of a key exchange message that is longer than MaxKeyExchangeLen. Metadata: "length", "max".

Variables

This section is empty.

Functions

func AppendClientKeyExchange

func AppendClientKeyExchange(dst []byte, m *ClientKeyExchange) ([]byte, error)

AppendClientKeyExchange appends the message to dst: four zero bytes, the key method, the random fields, and the four strings. A string is a 16-bit length that counts the NUL at its end, the bytes and the NUL; with no user name and no password, those two are strings of length 0, with no NUL. A string that is too long, or has a NUL inside, is refused, and so is a message that is longer than MaxKeyExchangeLen: a user name or password is never part of the error.

func AppendMessage

func AppendMessage(dst []byte, text string) []byte

AppendMessage appends a text message to dst as the control channel carries it: the text and a NUL. Each message is written to TLS in one Write, so it is one record.

func IsAuthFailed

func IsAuthFailed(text string) bool

IsAuthFailed reports whether a message is an AUTH_FAILED.

func IsAuthPending

func IsAuthPending(text string) bool

IsAuthPending reports whether a message is AUTH_PENDING.

func IsPushReply

func IsPushReply(text string) bool

IsPushReply reports whether a message is a PUSH_REPLY.

func Name

func Name(text string) string

Name is the first word of a message or an option: what is safe to put in a log line when the rest could be a secret.

func OptionsString

func OptionsString(o OptionsParams) string

OptionsString is the options string of the key exchange message, in the form of OpenVPN's client. The server only compares it with its own idea of what the client sends and warns (SPEC §20 V1), so the values are honest rather than exact: they describe this client.

func ParseAuthPending

func ParseAuthPending(text string) time.Duration

ParseAuthPending reads an AUTH_PENDING message and returns the time the server asks the client to wait, or zero when it asks for none ("AUTH_PENDING" alone, or without a "timeout" keyword). Other keywords are ignored, as OpenVPN ignores them.

Draft: "AUTH_PENDING". OpenVPN: push.c parse_auth_pending_keywords.

Types

type AuthFailed

type AuthFailed struct {
	// Reason is the text of the server, or empty.
	Reason string
	// Session is true for "SESSION:" (the session or its token has expired: a
	// new login may work).
	Session bool
	// Temporary is true for "TEMP": the refusal is for now, and the client may
	// try again after Backoff.
	Temporary bool
	// Backoff is how long the server asks the client to wait, or zero.
	Backoff time.Duration
	// Advance is what the server says about the next server: "no", "remote" or
	// "addr", or empty when it said nothing.
	Advance string
}

AuthFailed is the server's refusal of the client's credentials.

The forms are "AUTH_FAILED", "AUTH_FAILED,reason", "AUTH_FAILED,SESSION:reason", "AUTH_FAILED,TEMP:reason" and "AUTH_FAILED,TEMP[backoff 42,advance no]:reason".

Draft: "AUTH_FAILED". OpenVPN: push.c receive_auth_failed.

func ParseAuthFailed

func ParseAuthFailed(text string) *AuthFailed

ParseAuthFailed reads an AUTH_FAILED message. It does not fail: a message that does not follow a form is read as far as it goes, as OpenVPN reads it, and the rest is the reason. A flag that is not known is ignored, and a backoff that is not a number is none.

type ClientKeyExchange

type ClientKeyExchange struct {
	PreMaster [PreMasterSize]byte
	Random1   [RandomSize]byte
	Random2   [RandomSize]byte
	// Options is the options string (SPEC §20 V1: the server only compares it).
	Options string
	// Username and Password are empty when the client has no credentials.
	Username string
	Password string
	// PeerInfo is the text of the peer info, lines "NAME=value\n".
	PeerInfo string
}

ClientKeyExchange is the message that a client sends first in the TLS session. The three random fields are not used by this library (the data keys come from the TLS exporter), but they are sent, random, as the server expects.

Draft: "Key exchange message" (key method 2).

func NewClientKeyExchange

func NewClientKeyExchange(options, username, password, peerInfo string) *ClientKeyExchange

NewClientKeyExchange makes a message with random fields from crypto/rand.

type OptionsParams

type OptionsParams struct {
	// Stream is true for TCP.
	Stream bool
	// IPv6 is true when the link is IPv6.
	IPv6 bool
	// TunMTU is the MTU of the tunnel.
	TunMTU int
	// Cipher is the data cipher the client expects to use (its first choice),
	// and KeySizeBits is its key size.
	Cipher      string
	KeySizeBits int
}

OptionsParams are the values of the options string of a client.

type PeerInfo

type PeerInfo struct {
	// Version is IV_VER. The server does not read it (it only gives it to its
	// scripts), so it says what this client is.
	Version string
	// Platform is IV_PLAT: "linux", "win" or "mac".
	Platform string
	// MTU is IV_MTU, the largest tun MTU that the client can use; the server
	// does not push a larger one.
	MTU int
	// Ciphers is IV_CIPHERS, the data ciphers the client offers, best first.
	Ciphers []string
	// Proto is IV_PROTO, the sum of the IVProto bits.
	Proto int
	// UV is the variables the caller set, which all start with "UV_".
	UV map[string]string
}

PeerInfo is what a client tells the server about itself, as the lines "NAME=value" of the key exchange message.

func (*PeerInfo) String

func (p *PeerInfo) String() string

String is the text of the peer info: one line for each variable, in the order of OpenVPN's client, with the UV_ variables sorted by name at the end.

IV_NCP=2 is sent when AES-128-GCM and AES-256-GCM are both offered (the servers that know IV_CIPHERS ignore it, and older ones need it), and IV_TCPNL=1 says that the client does not need packets in order over TCP.

type PushReply

type PushReply struct {
	// Options are the options of this part, without push-continuation.
	Options []string
	// More is true when the part ends with "push-continuation 2": another part
	// follows.
	More bool
}

PushReply is one message of the server's answer to PUSH_REQUEST. A long reply comes in parts: every part but the last ends with the option "push-continuation 2".

func ParsePushReply

func ParsePushReply(text string) (*PushReply, error)

ParsePushReply parses a message that IsPushReply: the options are separated by commas. The option "push-continuation N" says whether more parts follow: 2 for more, 1 (or nothing) for the last.

Draft: "PUSH_REPLY". OpenVPN: push.c process_incoming_push_reply.

func (*PushReply) Filter added in v0.3.0

func (r *PushReply) Filter(decide func(option string) Verdict) error

Filter applies a pull filter to the options of a part of the reply: the options for which decide says Ignore are taken out, and one for which it says Reject is an error.

OpenVPN: options.c apply_pull_filter.

type Pushed

type Pushed struct {
	// Options are all the options as received, without push-continuation. The
	// value of auth-token is not kept: it is a secret.
	Options []string
	// Unknown are the names of the options that are not known.
	Unknown []string

	Topology        string // "subnet", "net30" or "p2p"
	Addrs           []netip.Prefix
	Gateway         netip.Addr
	Routes          []Route
	RedirectGateway bool
	DNSServers      []netip.Addr
	SearchDomains   []string
	MTU             int // 0 when not pushed
	PingInterval    time.Duration
	PingRestart     time.Duration // ping-restart or ping-exit: the silence that ends the session
	PeerID          uint32
	HasPeerID       bool
	Cipher          string
	KeyDerivation   string
	ProtocolFlags   []string
	AuthToken       string // a secret: never logged
	AuthTokenUser   string
	RenegInterval   time.Duration
	HasReneg        bool
	ExitNotify      int // explicit-exit-notify: the number of tries, 0 when not pushed

	// Skipped are the pushed options that could not be read and were left out,
	// with what was wrong with each: the connection goes on without them.
	Skipped []SkippedOption
	// contains filtered or unexported fields
}

Pushed is what a server pushed: the options of every part of its PUSH_REPLY, understood. A Pushed is built by Add for each part and Finish after the last.

func (*Pushed) Add

func (p *Pushed) Add(reply *PushReply, offered []string) error

Add takes the options of one part of the reply. offered are the data ciphers the client offered, in capitals: a pushed cipher must be one of them.

func (*Pushed) Finish

func (p *Pushed) Finish() error

Finish ends the reply, after the last part: the addresses that depend on the topology are worked out. With topology subnet "ifconfig" is an address and a netmask; with net30 (the default) and p2p it is the local and the remote address.

type Route

type Route struct {
	Prefix netip.Prefix
	// Gateway is the next hop when the option names one; the zero Addr means
	// the gateway of the VPN.
	Gateway netip.Addr
	// Metric is the metric, or 0 when the option has none.
	Metric int
}

Route is a route that the server pushed.

type ServerKeyExchange

type ServerKeyExchange struct {
	Random1  [RandomSize]byte
	Random2  [RandomSize]byte
	Options  string
	Username string
	Password string
	PeerInfo string
}

ServerKeyExchange is the message that the server sends in answer: the same as the client's without the pre-master secret. Its strings are kept as sent; the user name and password are the server's, and empty.

func ParseServerKeyExchange

func ParseServerKeyExchange(b []byte) (*ServerKeyExchange, error)

ParseServerKeyExchange parses the server's message. The four leading bytes are not checked (OpenVPN does not), the key method (the low 4 bits of the next byte) must be 2, and every string must be as long as its length says, end in a NUL and have none inside. The options string must be present; any other string may have the length 0, which OpenVPN reads as "no string". Bytes after the peer info are an error.

type Signal

type Signal struct {
	// Restart is true for RESTART and false for HALT.
	Restart bool
	// Reason is the text of the server, or empty.
	Reason string
	// Flags are the letters between square brackets that may start a RESTART's
	// text: P (keep the credentials) and N (go on to the next server).
	Flags string
}

Signal is a HALT or RESTART message: the server tells the client to stop, or to connect again.

Draft: "RESTART and HALT". OpenVPN: push.c server_pushed_signal.

func ParseSignal

func ParseSignal(text string) (*Signal, bool)

ParseSignal reads a message that is HALT or RESTART, with or without a comma and a text. It reports false for any other message.

type SkippedOption added in v1.0.1

type SkippedOption struct {
	Option  string // the name of the option
	Problem string // what was wrong, in words that carry no value of the option
}

SkippedOption is a pushed option that was left out, and why.

type Verdict added in v0.3.0

type Verdict uint8

Verdict is what a pull filter decides about one pushed option.

const (
	// Accept keeps the option.
	Accept Verdict = iota
	// Ignore takes the option out, as if the server had not pushed it.
	Ignore
	// Reject ends the handshake with an error.
	Reject
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL