Documentation
¶
Overview ¶
Package openvpn is an OpenVPN client library with the patterns of the Go standard library.
A program gives a Config (or an .ovpn profile, parsed by package ovpn) and gets a *Conn: a net.Conn whose Read returns one IP packet from the VPN and whose Write sends one. It needs no root and changes nothing on the machine. Package vpnnet builds on a *Conn to give DialContext, Listen and ListenPacket over the tunnel, with a TCP/IP stack inside the program.
The library speaks the modern OpenVPN protocol (servers from OpenVPN 2.6 on) over UDP and TCP, with TLS from crypto/tls, the control channel protections tls-auth, tls-crypt and tls-crypt-v2, and AEAD data channel ciphers.
This is not OpenVPN's own program and is not affiliated with OpenVPN Inc. The library is licensed GPL-2.0-only; see the files LICENSE and NOTICE.
Index ¶
Constants ¶
const ( // TagHandshakeTimeout: the handshake did not finish within // Config.HandshakeTimeout. Metadata: "timeout". TagHandshakeTimeout = session.TagHandshakeTimeout // TagLinkFailed: reading or writing the link to the server failed. The // cause (an io.EOF, a closed connection) can be found with errors.Is. // Metadata: "op" ("read" or "write"). TagLinkFailed = session.TagLinkFailed // TagTLSHandshakeFailed: the TLS handshake with the server failed: a // certificate that does not verify, a client certificate that the server // refuses, no TLS version in common. The cause, a crypto/tls or x509 // error, can be found with errors.Is and errors.As. TagTLSHandshakeFailed = session.TagTLSHandshakeFailed )
Tags of the errors that a caller may branch on, with errors.FromError and the Tag field of the error.
const ( // TagDialFailed: the connection to the server could not be made. The // cause (a refused connection, a timeout, the context) can be found with // errors.Is. Metadata: "network", "address". TagDialFailed = "DialFailed" // TagUnsupportedNetwork: the network is not one of "udp", "udp4", "udp6", // "tcp", "tcp4" or "tcp6". Metadata: "network". TagUnsupportedNetwork = "UnsupportedNetwork" )
Tags of the errors of Dial.
const DefaultHandshakeTimeout = 60 * time.Second
DefaultHandshakeTimeout is how long the handshake may take when Config.HandshakeTimeout is zero: OpenVPN's --hand-window.
Variables ¶
This section is empty.
Functions ¶
func NewTLSConfig ¶
func NewTLSConfig(o TLSOptions) *tls.Config
NewTLSConfig builds a tls.Config for Config.TLS that verifies the server the way OpenVPN does: the certificate chain must lead to RootCAs, and the name in the certificate is not compared with the address that was dialed (a profile asks for that with verify-x509-name, which a later version of the library supports). Nothing is left unverified: crypto/tls would check the name, so it is told to skip only its own check, and the chain is verified in VerifyConnection.
OpenVPN: ssl_verify.c verify_callback; doc: tls-options.rst --remote-cert-tls.
Types ¶
type Config ¶
type Config struct {
// TLS is the configuration of the TLS client of the control channel: the
// server's certificate is verified with it, and its Certificates are the
// client's. It is required. NewTLSConfig builds one that verifies the
// server the way OpenVPN does. The library uses a copy, and raises a
// MinVersion below TLS 1.2 to it.
TLS *tls.Config
// ControlWrap protects the control channel with a key shared in advance:
// the value of NewTLSAuth, NewTLSCrypt or NewTLSCryptV2. Nil means no
// protection, which the servers that are set up for it refuse.
ControlWrap ControlWrap
// HandshakeTimeout is how long the handshake may take; zero means
// DefaultHandshakeTimeout. It is OpenVPN's --hand-window.
HandshakeTimeout time.Duration
// Logger gets the events of the connection, with the error of a failure
// as the attribute "err". It never gets a key, a password or a packet's
// bytes. Nil means no logs.
Logger *slog.Logger
// contains filtered or unexported fields
}
Config is the configuration of a client, as tls.Config is for TLS. The zero value of a field means its default. A Config is not changed by the library and may be shared by connections.
The fields that the later parts of the protocol need (credentials, data ciphers, keepalive, MTU, peer info) are added as the library learns those parts.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is one session with a server.
The data channel (Read and Write of IP packets) and the rest of net.Conn come with the later steps of the library; so far a Conn completes the TLS handshake with the server.
func Client ¶
Client makes a Conn over conn, which is a stream (TCP framing). It returns at once and starts nothing: the handshake runs at HandshakeContext. The Conn takes conn over: Close closes it, and so does the end of the session. A nil config is the zero Config.
func Dial ¶
Dial connects to the server at address and returns when the handshake is complete, or when it fails or ctx is done. network is "udp", "udp4", "udp6", "tcp", "tcp4" or "tcp6". A nil config is the zero Config.
ctx bounds the connecting and the handshake; it does not bound the life of the Conn that is returned.
func PacketClient ¶
PacketClient is Client for a connected datagram socket: each Read and Write of conn is one packet.
func (*Conn) Close ¶
Close ends the session, closes the connection to the server and waits for the goroutines of the Conn. After Close, every method returns net.ErrClosed.
func (*Conn) HandshakeContext ¶
HandshakeContext runs the handshake if it has not run, and returns when it is complete, when it fails, or when ctx is done. A done ctx ends the session: the Conn is then unusable and the caller closes it. Calls after the handshake returns the same result.
func (*Conn) NetConn ¶
NetConn returns the connection to the server that was given to Client or PacketClient. Reading or writing it breaks the session.
func (*Conn) RemoteAddr ¶
RemoteAddr is the address of the server on the link.
type ControlWrap ¶
type ControlWrap interface {
// contains filtered or unexported methods
}
ControlWrap is a protection of the control channel (OpenVPN's tls-auth, tls-crypt and tls-crypt-v2). Make one with NewTLSAuth, NewTLSCrypt or NewTLSCryptV2. A ControlWrap holds a key and no connection state, so one value may be in the Config of many connections.
func NewTLSAuth ¶
func NewTLSAuth(key []byte, direction KeyDirection, digest crypto.Hash) (ControlWrap, error)
NewTLSAuth makes the protection of tls-auth. key is the contents of the static key file (the text between the "OpenVPN Static key V1" lines, as `openvpn --genkey tls-auth` writes it). digest is the profile's auth option: crypto.SHA1 (OpenVPN's default), SHA256, SHA384 or SHA512.
Draft: "tls-auth".
func NewTLSCrypt ¶
func NewTLSCrypt(key []byte) (ControlWrap, error)
NewTLSCrypt makes the protection of tls-crypt. key is the contents of the static key file that `openvpn --genkey tls-crypt` writes.
Draft: "tls-crypt".
func NewTLSCryptV2 ¶
func NewTLSCryptV2(key []byte) (ControlWrap, error)
NewTLSCryptV2 makes the protection of tls-crypt-v2. key is the contents of the client key file that `openvpn --genkey tls-crypt-v2-client` writes (a PEM block, "OpenVPN tls-crypt-v2 client key").
Draft: "tls-crypt-v2".
type Dialer ¶
type Dialer struct {
// NetDialer makes the connection to the server (the link). Nil means the
// zero net.Dialer. It is not used when DialContext is set.
NetDialer *net.Dialer
// DialContext, when set, makes the link in place of NetDialer: a way to
// go through a proxy, or to use a link of the caller's own.
DialContext func(ctx context.Context, network, address string) (net.Conn, error)
// Config is the configuration of the connections; nil is the zero Config.
Config *Config
}
Dialer makes connections to servers with the same settings, as net.Dialer does.
type KeyDirection ¶
type KeyDirection int
KeyDirection is the direction of the keys of a static key file, OpenVPN's key-direction: which half of the file this side sends with.
const ( // KeyDirectionNone uses the first keys of the file in both directions: the // profile has no key-direction. KeyDirectionNone KeyDirection = iota // KeyDirection0 sends with the first keys and receives with the second: // key-direction 0, the usual setting of a server. KeyDirection0 // KeyDirection1 sends with the second keys and receives with the first: // key-direction 1, the usual setting of a client. KeyDirection1 )
type TLSOptions ¶
type TLSOptions struct {
// RootCAs are the authorities that sign the server's certificate (the
// profile's ca). Nil means the roots of the system.
RootCAs *x509.CertPool
// Certificates is the client's certificate and key (the profile's cert and
// key). The servers that are set up as usual refuse a client without one.
Certificates []tls.Certificate
// RemoteCertTLSServer requires the server's certificate to be good for a
// TLS server: the extended key usage "TLS Web Server Authentication"
// (--remote-cert-tls server).
RemoteCertTLSServer bool
// MinVersion is the lowest TLS version, tls.VersionTLS12 or
// tls.VersionTLS13; zero means TLS 1.2, which is also the lowest that the
// library accepts.
MinVersion uint16
}
TLSOptions say how NewTLSConfig builds the TLS configuration of a client, in the terms of the options of an OpenVPN profile.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
ctrlwrap
Package ctrlwrap protects the packets of the control channel: it adds the authentication (tls-auth) or the encryption and authentication (tls-crypt, tls-crypt-v2) that a pre-shared key gives, and takes it off again, with the replay protection that goes with it.
|
Package ctrlwrap protects the packets of the control channel: it adds the authentication (tls-auth) or the encryption and authentication (tls-crypt, tls-crypt-v2) that a pre-shared key gives, and takes it off again, with the replay protection that goes with it. |
|
keys
Package keys reads the key files of OpenVPN and derives the keys that the protocol uses from them: the static key file of tls-auth and tls-crypt, and (from step 1.7) the tls-crypt-v2 client key.
|
Package keys reads the key files of OpenVPN and derives the keys that the protocol uses from them: the static key file of tls-auth and tls-crypt, and (from step 1.7) the tls-crypt-v2 client key. |
|
reliable
Package reliable is the reliability layer of the control channel: it makes a link that loses, repeats and reorders packets deliver the control payloads exactly once and in order.
|
Package reliable is the reliability layer of the control channel: it makes a link that loses, repeats and reorders packets deliver the control payloads exactly once and in order. |
|
replay
Package replay protects against replayed packets: a Window decides whether a received packet counter is new, and a Sender makes the packet ids of the packets this side sends.
|
Package replay protects against replayed packets: a Window decides whether a received packet counter is new, and a Sender makes the packet ids of the packets this side sends. |
|
servertest
Package servertest starts a real OpenVPN server for the tests of gopenvpn-conn (SPEC §17).
|
Package servertest starts a real OpenVPN server for the tests of gopenvpn-conn (SPEC §17). |
|
session
Package session is the state machine of one connection to a server: it owns the link, reads and writes control packets through the control channel protection and the reliability layer, and runs the handshake.
|
Package session is the state machine of one connection to a server: it owns the link, reads and writes control packets through the control channel protection and the reliability layer, and runs the handshake. |
|
tools/errcheck
command
Command errcheck checks the error rules of SPEC §14.1 on the Go files of the module: the ones that a program can check.
|
Command errcheck checks the error rules of SPEC §14.1 on the Go files of the module: the ones that a program can check. |
|
vpntest
Package vpntest is the in-process peer of the unit tests: links in memory (datagrams with loss, duplicates and reordering from a seed, and streams) and a small server side made of the packet packages of this module, so that the unit tests need no openvpn binary and no privileges.
|
Package vpntest is the in-process peer of the unit tests: links in memory (datagrams with loss, duplicates and reordering from a seed, and streams) and a small server side made of the packet packages of this module, so that the unit tests need no openvpn binary and no privileges. |
|
wire
Package wire has the formats of OpenVPN packets as pure functions from bytes to structs and back: the first byte (opcode and key id), session ids, the control packet with its acknowledgement array, and the 16-bit length that frames packets on a TCP stream.
|
Package wire has the formats of OpenVPN packets as pure functions from bytes to structs and back: the first byte (opcode and key id), session ids, the control packet with its acknowledgement array, and the 16-bit length that frames packets on a TCP stream. |