wrapper: fix z_stream leak

Ensure to free the z_stream structures as allocated from
the gzip.c initcompress, initdecompress functions.

Signed-off-by: Jon Simons <jon@jonsimons.org>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
crypto: check malloc return in ssh_mac_ctx_init

Signed-off-by: Jon Simons <jon@jonsimons.org>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
pki: fail when pubkey buffer length is not ED25519_PK_LEN

Fail fast in 'pki_import_pubkey_buffer' for the ED25519 case if a buffer
sized ED25519_PK_LEN can not be retrieved.  Before, the 'memcpy' could
have read beyond the bounds of 'ssh_string_data(pubkey)'.

Signed-off-by: Jon Simons <jon@jonsimons.org>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
pki: check ssh_buffer_pack return in ssh_pki_do_sign

Check the 'ssh_buffer_pack' return in ssh_pki_do_sign for the ED25519 case.

Signed-off-by: Jon Simons <jon@jonsimons.org>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
ed25519: fix leak in pki_ed25519_sign

Signed-off-by: Jon Simons <jon@jonsimons.org>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
server: fix auth_interactive_request reply

Fix a missing 'buffer_pack' formatter in 'ssh_message_auth_interactive_request'.
With this fix the 'examples/samplesshd-kbdint' program is working again for me.

Signed-off-by: Jon Simons <jon@jonsimons.org>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
pki_gcrypt: Initialize 'type_c' in 'pki_do_sign_sessionid'

Add missing initialization of 'type_c' field of a SSH signature in
'pki_do_sign_sessionid' procedure.

If libssh is compiled with GCrypt, 'dh_handshake_server' fails with
"Could not sign the session id" error.  The change fixes that.

Signed-off-by: Artyom V. Poptsov <poptsov.artyom@gmail.com>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
messages: emit pubkey failures to callbacks

With this change, errors encountered while processing public keys
in 'ssh_packet_userauth_request' will be propagated back up to the
user-provided 'auth_pubkey_function' callback.

For example, if signature validation fails, the user callback will
be invoked with a signature state of SSH_PUBLICKEY_STATE_WRONG.

Before this change, a failure in signature validation would not be
made explicit via the 'ssh_server_callbacks'.

Conflicts:
	src/messages.c
channel: add ssh_channel_write timeout family

Conflicts:
	include/libssh/channels.h
	src/channels.c
buffer: always start off with BUFFER_INIT_SIZE

Initialize all new buffers with a BUFFER_INIT_SIZE allocation right
off the bat, to avoid later byte-by-byte repeated realloc calls.
buffer: never realloc down in buffer_reinit

Never realloc buffers down to 127 bytes in buffer_reinit: during large
transmits, this causes a "realloc ping-pong" with the session out buffer
constantly being realloc'd down to 127 bytes, just to be realloc'd back
up to 32 kilobytes for the next packet send (for example).

Were the realloc behavior desired in buffer_reinit, it would be better
to issue a free followed by malloc (as-is, 127 bytes that we don't care
about are memmove'd to the new buffer).
bind: add selective use of XCLOSE
socket: add selective use of XCLOSE
channel: is_local_eof|closed getters

Given an invocation to 'ssh_channel_close', upon SSH_AGAIN, there
is currently no way to know whether the SSH_AGAIN means that EOF
has been totally sent and CLOSE message bytes have been enqueued,
or whether sending and flushing EOF needs to be retried, and CLOSE
message bytes are yet to be queued.

Expose getters for 'local_eof' and the new field 'local_closed'.
Until the latter is set, it is not the case the CLOSE message
bytes have been enqueued to the outgoing session socket.

Motivation: when unable to distinguish the above ssh_channel_close
cases, clients either have to resort to missing a channel close, or
possibly sending more than one CLOSE message for a given channel.
Both of those options are bad for the case of session multiplexing
(for example, OpenSSH might indefinitely hang if it never sees a
corresponding CLOSE response for a closed channel; likewise, it
may abruptly disconnect if it receives a duplicate CLOSE or a
CLOSE for an unknown channel).
channel: DISABLE_AUTH_AGENT_REQ test

Conflicts:
	src/channels.c
session: check session error in blocking_flush
tests: introduce pkd_hello

Introduce a sample public-key testing daemon to the 'pkd' test directory,
and add support code for cycling through various combinations of different
key exchange, cipher, and MAC algorithms.

The goal of the 'pkd_hello' test is to make it easy to test interactions
between non-libssh clients and a libssh-server, and to provide a starting
point for testing new implementations for key types, ciphers, MACs, and
so on.  The thinking is that testing a new algorithm should be as simple
as adding a new line for it in the PKDTESTS_* lists.

Macros are used to generate the tests and helper functions for a couple of
clients -- here, OpenSSH and dropbear are included for the first cut.  If
binaries are found for these clients, their test lists will be enabled;
when binaries are not found for a given client, those tests are skipped.

Tests are run in one large batch by default, but can also be run individually
to help with tracking down things like signature bugs that may take many
iterations to reproduce.

Each test logs its stdout and stderr to its own file, which is cleaned up
when a test succeeds.  For failures, those logs can be combined with verbose
libssh output from pkd itself to start debugging things.

Some example usages:

  pkd_hello
    Run all tests with default number of iterations.

  pkd_hello --list
    List available individual test names.

  pkd_hello -i 1000 -t torture_pkd_openssh_ecdsa_256_ecdh_sha2_nistp256
    Run only the torture_pkd_openssh_ecdsa_256_ecdh_sha2_nistp256
    testcase 1000 times.

  pkd_hello -v -v -v -v -e -o
    Run all tests with maximum libssh and pkd logging.

Included now in the tests are passes for the new SHA2 HMAC support,
as well as all existing kex, cipher, and MAC algorithms.

BUG: https://red.libssh.org/issues/144

Signed-off-by: Jon Simons <jon@jonsimons.org>
session: add get_in|outbuffer_rest_len helpers