mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 18:11:28 +01:00
13ac472062
wire_sync_write() adds length, but we already have it, so use write_all. sync_crypto_read() handed an on-stack buffer to cryptomsg_decrypt_header, which expected a tal() pointer, so use the known length instead. sync_crypto_read() also failed to read the tag; add that in (no overflow possible as 16 is an int, len is a u16). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
35 lines
757 B
C
35 lines
757 B
C
#include <ccan/read_write_all/read_write_all.h>
|
|
#include <lightningd/crypto_sync.h>
|
|
#include <lightningd/cryptomsg.h>
|
|
#include <wire/wire_sync.h>
|
|
|
|
bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg)
|
|
{
|
|
u8 *enc = cryptomsg_encrypt_msg(msg, cs, msg);
|
|
bool ret;
|
|
|
|
ret = write_all(fd, enc, tal_len(enc));
|
|
tal_free(enc);
|
|
return ret;
|
|
}
|
|
|
|
u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd)
|
|
{
|
|
u8 hdr[18], *enc, *dec;
|
|
u16 len;
|
|
|
|
if (!read_all(fd, hdr, sizeof(hdr)))
|
|
return NULL;
|
|
|
|
if (!cryptomsg_decrypt_header(cs, hdr, &len))
|
|
return NULL;
|
|
|
|
enc = tal_arr(ctx, u8, len + 16);
|
|
if (!read_all(fd, enc, tal_len(enc)))
|
|
return tal_free(enc);
|
|
|
|
dec = cryptomsg_decrypt_body(ctx, cs, enc);
|
|
tal_free(enc);
|
|
return dec;
|
|
}
|