1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

Macroise the cumbersome read idioms in the BPPs.

Now the three 'proper' BPPs each have a BPP_READ() macro that wraps up
the fiddly combination of crMaybeWaitUntilV and bufchainery they use
to read a fixed-length amount of input data. The sshverstring 'BPP'
doesn't read fixed-length data in quite the same way, but it has a
similar BPP_WAITFOR macro.

No functional change. Mostly this is just a cleanup to make the code
more legible, but also, the new macros will be a good place to
centralise anything else that needs doing on every read, such as EOF
checking.
This commit is contained in:
Simon Tatham 2018-09-24 14:23:52 +01:00
parent 96622d17a3
commit d77b95cb42
4 changed files with 39 additions and 27 deletions

View File

@ -91,6 +91,12 @@ void ssh1_bpp_requested_compression(BinaryPacketProtocol *bpp)
s->pending_compression_request = TRUE;
}
#define BPP_READ(ptr, len) do \
{ \
crMaybeWaitUntilV(bufchain_try_fetch_consume( \
s->bpp.in_raw, ptr, len)); \
} while (0)
static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp)
{
struct ssh1_bpp_state *s = FROMFIELD(bpp, struct ssh1_bpp_state, bpp);
@ -103,8 +109,7 @@ static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp)
{
unsigned char lenbuf[4];
crMaybeWaitUntilV(bufchain_try_fetch_consume(
bpp->in_raw, lenbuf, 4));
BPP_READ(lenbuf, 4);
s->len = toint(GET_32BIT_MSB_FIRST(lenbuf));
}
@ -130,8 +135,7 @@ static void ssh1_bpp_handle_input(BinaryPacketProtocol *bpp)
s->maxlen = s->biglen;
s->data = snew_plus_get_aux(s->pktin);
crMaybeWaitUntilV(bufchain_try_fetch_consume(
bpp->in_raw, s->data, s->biglen));
BPP_READ(s->data, s->biglen);
if (s->cipher && detect_attack(s->crcda_ctx,
s->data, s->biglen, NULL)) {

View File

@ -48,6 +48,12 @@ static void ssh2_bare_bpp_free(BinaryPacketProtocol *bpp)
sfree(s);
}
#define BPP_READ(ptr, len) do \
{ \
crMaybeWaitUntilV(bufchain_try_fetch_consume( \
s->bpp.in_raw, ptr, len)); \
} while (0)
static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp)
{
struct ssh2_bare_bpp_state *s =
@ -59,8 +65,7 @@ static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp)
/* Read the length field. */
{
unsigned char lenbuf[4];
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw, lenbuf, 4));
BPP_READ(lenbuf, 4);
s->packetlen = toint(GET_32BIT_MSB_FIRST(lenbuf));
}
@ -83,8 +88,7 @@ static void ssh2_bare_bpp_handle_input(BinaryPacketProtocol *bpp)
/*
* Read the remainder of the packet.
*/
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw, s->data, s->packetlen));
BPP_READ(s->data, s->packetlen);
/*
* The data we just read is precisely the initial type byte

View File

@ -158,6 +158,12 @@ void ssh2_bpp_new_incoming_crypto(
s->pending_newkeys = FALSE;
}
#define BPP_READ(ptr, len) do \
{ \
crMaybeWaitUntilV(bufchain_try_fetch_consume( \
s->bpp.in_raw, ptr, len)); \
} while (0)
static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
{
struct ssh2_bpp_state *s = FROMFIELD(bpp, struct ssh2_bpp_state, bpp);
@ -206,8 +212,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
}
/* Read an amount corresponding to the MAC. */
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw, s->buf, s->maclen));
BPP_READ(s->buf, s->maclen);
s->packetlen = 0;
ssh2_mac_start(s->in.mac);
@ -216,10 +221,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
for (;;) { /* Once around this loop per cipher block. */
/* Read another cipher-block's worth, and tack it on to
* the end. */
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw,
s->buf + (s->packetlen + s->maclen),
s->cipherblk));
BPP_READ(s->buf + (s->packetlen + s->maclen), s->cipherblk);
/* Decrypt one more block (a little further back in
* the stream). */
ssh2_cipher_decrypt(s->in.cipher,
@ -262,8 +264,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
* OpenSSH encrypt-then-MAC mode: the packet length is
* unencrypted, unless the cipher supports length encryption.
*/
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw, s->buf, 4));
BPP_READ(s->buf, 4);
/* Cipher supports length decryption, so do it */
if (s->in.cipher && (ssh2_cipher_alg(s->in.cipher)->flags &
@ -307,9 +308,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
/*
* Read the remainder of the packet.
*/
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw, s->data + 4,
s->packetlen + s->maclen - 4));
BPP_READ(s->data + 4, s->packetlen + s->maclen - 4);
/*
* Check the MAC.
@ -334,8 +333,7 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
* Acquire and decrypt the first block of the packet. This will
* contain the length and padding details.
*/
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw, s->buf, s->cipherblk));
BPP_READ(s->buf, s->cipherblk);
if (s->in.cipher)
ssh2_cipher_decrypt(
@ -376,9 +374,8 @@ static void ssh2_bpp_handle_input(BinaryPacketProtocol *bpp)
/*
* Read and decrypt the remainder of the packet.
*/
crMaybeWaitUntilV(bufchain_try_fetch_consume(
s->bpp.in_raw, s->data + s->cipherblk,
s->packetlen + s->maclen - s->cipherblk));
BPP_READ(s->data + s->cipherblk,
s->packetlen + s->maclen - s->cipherblk);
/* Decrypt everything _except_ the MAC. */
if (s->in.cipher)

View File

@ -197,6 +197,12 @@ static void ssh_verstring_send(struct ssh_verstring_state *s)
vs_logevent(("We claim version: %s", s->our_vstring));
}
#define BPP_WAITFOR(minlen) do \
{ \
crMaybeWaitUntilV( \
bufchain_size(s->bpp.in_raw) >= (minlen)); \
} while (0)
void ssh_verstring_handle_input(BinaryPacketProtocol *bpp)
{
struct ssh_verstring_state *s =
@ -221,8 +227,7 @@ void ssh_verstring_handle_input(BinaryPacketProtocol *bpp)
* Every time round this loop, we're at the start of a new
* line, so look for the prefix.
*/
crMaybeWaitUntilV(bufchain_size(s->bpp.in_raw) >=
s->prefix_wanted.len);
BPP_WAITFOR(s->prefix_wanted.len);
bufchain_fetch(s->bpp.in_raw, s->prefix, s->prefix_wanted.len);
if (!memcmp(s->prefix, s->prefix_wanted.ptr, s->prefix_wanted.len)) {
bufchain_consume(s->bpp.in_raw, s->prefix_wanted.len);
@ -237,7 +242,9 @@ void ssh_verstring_handle_input(BinaryPacketProtocol *bpp)
void *data;
char *nl;
crMaybeWaitUntilV(bufchain_size(s->bpp.in_raw) > 0);
/* Wait to receive at least 1 byte, but then consume more
* than that if it's there. */
BPP_WAITFOR(1);
bufchain_prefix(s->bpp.in_raw, &data, &len);
if ((nl = memchr(data, '\012', len)) != NULL) {
bufchain_consume(s->bpp.in_raw, nl - (char *)data + 1);