Documentation
¶
Overview ¶
Package protocol provides the protocol used by the server and the client.
Index ¶
- Constants
- func Nonce(direction byte, counter uint64, size int) []byte
- func SetupTranscript(clientPublicKey, serverPublicKey *ecdh.PublicKey, id [16]byte) ([]byte, error)
- func WithProcessRequestExpectReply(expectReply bool) func(*ProcessRequestOptions)
- type Action
- type ErrorCode
- type ErrorResponse
- type FetchKeyRequest
- type FetchKeyResponse
- type HashType
- type HelloRequest
- type HelloResponse
- type KeyAlgorithm
- type ProcessRequest
- type ProcessRequestOptions
- type ProcessResponse
- type RequestCommon
- type ResponseCommon
- type SetupRequest
- type SetupResponse
- type Version
Constants ¶
const ( // MaxKeySize is the maximum size in bytes accepted for a marshalled public // key field. MaxKeySize = 1 << 16 // 64 KiB // MaxSignatureSize is the maximum size in bytes accepted for a signature // field. MaxSignatureSize = 1 << 16 // 64 KiB // MaxMessageSize is the maximum size in bytes accepted for an encrypted // message payload. MaxMessageSize = 1 << 30 // 1 GiB )
Maximum sizes accepted when parsing length-prefixed fields received from the network. Without these bounds a peer could advertise an arbitrarily large size and force the parser to allocate that much memory (or panic), which is a trivial denial-of-service vector. The limits are generous relative to any legitimate value while still preventing pathological allocations.
const ( DirectionClientToServer byte = 0x00 DirectionServerToClient byte = 0x01 )
Direction bytes distinguish the two halves of a conversation so that the client and the server never derive the same AEAD nonce from the same counter under the shared secret.
Variables ¶
This section is empty.
Functions ¶
func Nonce ¶
Nonce builds a deterministic AEAD nonce of the given size from a direction byte and a message counter. Using a direction byte plus a per-secret monotonic counter guarantees the nonce is never reused under the same key, which is required for the security of AES-GCM. The size must be at least 9 bytes (one direction byte plus an 8-byte counter); GCM uses 12.
func SetupTranscript ¶
SetupTranscript builds the byte string that the server signs and the client verifies during the setup handshake. Binding both public keys and the session id (rather than only the server's ephemeral key) prevents an attacker from transplanting a valid server signature onto a different key exchange.
func WithProcessRequestExpectReply ¶
func WithProcessRequestExpectReply(expectReply bool) func(*ProcessRequestOptions)
WithProcessRequestExpectReply indicates if the client is expecting a reply from the server. This is useful to avoid the client to wait for a reply when the server doesn't need to send one. By default is true.
Types ¶
type Action ¶
type Action uint8
Action is the type of action to be performed by the client/server.
type ErrorResponse ¶
type ErrorResponse struct {
ResponseCommon
// contains filtered or unexported fields
}
ErrorResponse is the response sent by the server to the client when an error occurs.
func NewErrorResponse ¶
func NewErrorResponse(errorCode ErrorCode, errorMessage string) ErrorResponse
NewErrorResponse creates a new ErrorResponse.
func ParseErrorResponse ¶
func ParseErrorResponse(r io.Reader, responseCommon ResponseCommon) (ErrorResponse, error)
ParseErrorResponse parses an ErrorResponse received from the server.
func (ErrorResponse) Bytes ¶
func (e ErrorResponse) Bytes() []byte
Bytes returns the byte representation of the response.
func (ErrorResponse) ErrorCode ¶
func (e ErrorResponse) ErrorCode() ErrorCode
ErrorCode returns the error code if the action failed.
func (ErrorResponse) ErrorMessage ¶
func (e ErrorResponse) ErrorMessage() string
ErrorMessage returns the error message if the action failed.
type FetchKeyRequest ¶
type FetchKeyRequest struct {
RequestCommon
}
FetchKeyRequest is the request sent by the client to the server to retrieve the public key.
func NewFetchKeyRequest ¶
func NewFetchKeyRequest() FetchKeyRequest
NewFetchKeyRequest creates a new FetchKeyRequest.
type FetchKeyResponse ¶
type FetchKeyResponse struct {
ResponseCommon
// contains filtered or unexported fields
}
FetchKeyResponse is the response sent by the server to the client containing the public key.
func NewFetchKeyResponse ¶
func NewFetchKeyResponse(keyAlgorithm KeyAlgorithm, publicKey []byte) FetchKeyResponse
NewFetchKeyResponse creates a new NewFetchKeyResponse.
func ParseFetchKeyResponse ¶
func ParseFetchKeyResponse(r io.Reader, responseCommon ResponseCommon) (FetchKeyResponse, error)
ParseFetchKeyResponse parses a FetchKeyRespons received from a server.
func (FetchKeyResponse) Bytes ¶
func (g FetchKeyResponse) Bytes() []byte
Bytes returns the byte representation of the FetchKeyResponse.
func (FetchKeyResponse) KeyAlgorithm ¶
func (g FetchKeyResponse) KeyAlgorithm() KeyAlgorithm
KeyAlgorithm returns the algorithm used to generate the key-pair.
func (FetchKeyResponse) PublicKey ¶
func (g FetchKeyResponse) PublicKey() []byte
PublicKey returns the public key.
type HashType ¶
type HashType uint8
HashType is the hash type used for verifying the signature.
Supported hash types. SHA-1 is intentionally not supported: it is cryptographically broken and must not be used for signatures.
func NewHashType ¶
NewHashType creates a new HashType from the crypto.Hash.
func (HashType) CryptoHash ¶
CryptoHash returns the crypto.Hash corresponding to the hash type.
type HelloRequest ¶
type HelloRequest struct {
RequestCommon
}
HelloRequest is the request sent by the client to the server to keep the connection alive.
func NewHelloRequest ¶
func NewHelloRequest() HelloRequest
NewHelloRequest creates a new HelloRequest.
type HelloResponse ¶
type HelloResponse struct {
ResponseCommon
}
HelloResponse is the response sent by the server to the client acknowledging that everything is fine.
func NewHelloResponse ¶
func NewHelloResponse() HelloResponse
NewHelloResponse creates a new NewHelloResponse.
type KeyAlgorithm ¶
type KeyAlgorithm uint8
KeyAlgorithm is the algorithm used to generate the key-pair.
const ( KeyAlgorithmRSA KeyAlgorithm = 0x1 KeyAlgorithmECDSA KeyAlgorithm = 0x2 KeyAlgorithmED25519 KeyAlgorithm = 0x3 )
Supported key algorithms.
func (KeyAlgorithm) String ¶
func (k KeyAlgorithm) String() string
String returns the string representation of the key algorithm.
type ProcessRequest ¶
type ProcessRequest struct {
RequestCommon
// contains filtered or unexported fields
}
ProcessRequest is the request sent by the client to the server with a generic encrypted message.
func NewProcessRequest ¶
func NewProcessRequest( id [16]byte, counter uint64, message []byte, optFuncs ...func(*ProcessRequestOptions), ) ProcessRequest
NewProcessRequest creates a new ProcessRequest. The counter must be unique and increasing for every message encrypted under the same shared secret; it is used to build the AEAD nonce and to reject replays.
func ParseProcessRequest ¶
func ParseProcessRequest(requestCommon RequestCommon, r io.Reader) (ProcessRequest, error)
ParseProcessRequest parses a ProcessRequest received from a client. The request common part must have already been read. This could return an io.EOF error if the response is not complete.
func (ProcessRequest) Bytes ¶
func (g ProcessRequest) Bytes() []byte
Bytes returns the byte representation of the ProcessRequest.
func (ProcessRequest) Counter ¶
func (g ProcessRequest) Counter() uint64
Counter returns the message counter used to derive the AEAD nonce and to reject replays.
func (ProcessRequest) ExpectReply ¶
func (g ProcessRequest) ExpectReply() bool
ExpectReply returns true if the client is expecting a reply from the server.
func (ProcessRequest) ID ¶
func (g ProcessRequest) ID() [16]byte
ID returns the id of the shared secret.
func (ProcessRequest) Message ¶
func (g ProcessRequest) Message() []byte
Message returns the encrypted message.
type ProcessRequestOptions ¶
type ProcessRequestOptions struct {
// contains filtered or unexported fields
}
ProcessRequestOptions contains the options for a ProcessRequest.
type ProcessResponse ¶
type ProcessResponse struct {
ResponseCommon
// contains filtered or unexported fields
}
ProcessResponse is the response sent by the server to the client after generating a shared message.
func NewProcessResponse ¶
func NewProcessResponse(counter uint64, message []byte) ProcessResponse
NewProcessResponse creates a new NewProcessResponse. The counter is echoed from the request it answers and is used to build the AEAD nonce.
func ParseProcessResponse ¶
func ParseProcessResponse(r io.Reader, responseCommon ResponseCommon) (ProcessResponse, error)
ParseProcessResponse parses a ProcessRespons received from a server. This could return an io.EOF error if the response is not complete.
func (ProcessResponse) Bytes ¶
func (g ProcessResponse) Bytes() []byte
Bytes returns the byte representation of the ProcessResponse.
func (ProcessResponse) Counter ¶
func (g ProcessResponse) Counter() uint64
Counter returns the message counter used to derive the AEAD nonce.
func (ProcessResponse) Message ¶
func (g ProcessResponse) Message() []byte
Message returns the encrypted message.
type RequestCommon ¶
type RequestCommon struct {
// contains filtered or unexported fields
}
RequestCommon is the common fields for all requests.
func ParseRequestCommon ¶
func ParseRequestCommon(r io.Reader) (RequestCommon, error)
ParseRequestCommon parses the common fields of a request. It could return io.EOF if the client closes the connection.
func (RequestCommon) Action ¶
func (r RequestCommon) Action() Action
Action returns the action to be performed.
func (RequestCommon) Bytes ¶
func (r RequestCommon) Bytes() []byte
Bytes returns the byte representation of the request.
func (RequestCommon) Version ¶
func (r RequestCommon) Version() Version
Version returns the protocol version.
type ResponseCommon ¶
type ResponseCommon struct {
// contains filtered or unexported fields
}
ResponseCommon is the common fields for all responses.
func ParseResponseCommon ¶
func ParseResponseCommon(r io.Reader) (ResponseCommon, error)
ParseResponseCommon parses the common fields of a response. It could return io.EOF if the server closes the connection.
func (ResponseCommon) Bytes ¶
func (r ResponseCommon) Bytes() []byte
Bytes returns the byte representation of the response.
func (ResponseCommon) Success ¶
func (r ResponseCommon) Success() bool
Success returns true if the action was successful.
type SetupRequest ¶
type SetupRequest struct {
RequestCommon
// contains filtered or unexported fields
}
SetupRequest is the request sent by the client to the server to register a shared secret.
func NewSetupRequest ¶
func NewSetupRequest(id [16]byte, publicKey *ecdh.PublicKey) SetupRequest
NewSetupRequest creates a new SetupRequest.
func ParseSetupRequest ¶
func ParseSetupRequest(requestCommon RequestCommon, r io.Reader) (SetupRequest, error)
ParseSetupRequest parses a SetupRequest received from a client. The request common part must have already been read.
func (SetupRequest) Bytes ¶
func (s SetupRequest) Bytes() []byte
Bytes returns the byte representation of the SetupRequest.
func (SetupRequest) ID ¶
func (s SetupRequest) ID() [16]byte
ID returns the id of the shared secret.
func (SetupRequest) PublicKey ¶
func (s SetupRequest) PublicKey() *ecdh.PublicKey
PublicKey returns the public key for the Diffie-Hellman exchange.
type SetupResponse ¶
type SetupResponse struct {
ResponseCommon
// contains filtered or unexported fields
}
SetupResponse is the response sent by the server to the client after generating a shared secret.
func NewSetupResponse ¶
func NewSetupResponse(publicKey *ecdh.PublicKey) SetupResponse
NewSetupResponse creates a new SetupResponse.
func ParseSetupResponse ¶
func ParseSetupResponse(responseCommon ResponseCommon, r io.Reader) (SetupResponse, error)
ParseSetupResponse parses a SetupResponse received from the server. The response common part must have already been read.
func (SetupResponse) Bytes ¶
func (s SetupResponse) Bytes() []byte
Bytes returns the byte representation of the SetupResponse.
func (SetupResponse) HashType ¶
func (s SetupResponse) HashType() HashType
HashType returns the hash type used to sign the response.
func (SetupResponse) PublicKey ¶
func (s SetupResponse) PublicKey() *ecdh.PublicKey
PublicKey returns the public key for the Diffie-Hellman exchange.
func (*SetupResponse) SetSignature ¶
func (s *SetupResponse) SetSignature(hashType HashType, signature []byte)
SetSignature sets the signature of the response.
func (SetupResponse) Signature ¶
func (s SetupResponse) Signature() []byte
Signature prooves that the message was sent by the expected server, allowing to validate the response using the server's public key.