2017-02-21 05:45:29 +01:00
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/crypto_sync.h>
|
|
|
|
#include <common/cryptomsg.h>
|
|
|
|
#include <common/dev_disconnect.h>
|
|
|
|
#include <common/status.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2017-02-24 06:52:56 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2017-05-24 12:10:16 +02:00
|
|
|
#include <wire/wire.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2017-06-27 04:55:06 +02:00
|
|
|
bool sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2017-06-27 04:55:06 +02:00
|
|
|
int type = fromwire_peektype(msg);
|
|
|
|
u8 *enc = cryptomsg_encrypt_msg(NULL, cs, msg);
|
2017-02-21 05:45:29 +01:00
|
|
|
bool ret;
|
2017-05-24 12:10:16 +02:00
|
|
|
bool post_sabotage = false;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-06-27 04:55:06 +02:00
|
|
|
switch (dev_disconnect(type)) {
|
2017-05-24 12:10:16 +02:00
|
|
|
case DEV_DISCONNECT_BEFORE:
|
|
|
|
dev_sabotage_fd(fd);
|
|
|
|
return false;
|
|
|
|
case DEV_DISCONNECT_DROPPKT:
|
|
|
|
enc = tal_free(enc); /* FALL THRU */
|
|
|
|
case DEV_DISCONNECT_AFTER:
|
|
|
|
post_sabotage = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-02-24 06:52:35 +01:00
|
|
|
ret = write_all(fd, enc, tal_len(enc));
|
2017-02-21 05:45:29 +01:00
|
|
|
tal_free(enc);
|
2017-05-24 12:10:16 +02:00
|
|
|
|
|
|
|
if (post_sabotage)
|
|
|
|
dev_sabotage_fd(fd);
|
2017-02-21 05:45:29 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 *sync_crypto_read(const tal_t *ctx, struct crypto_state *cs, int fd)
|
|
|
|
{
|
|
|
|
u8 hdr[18], *enc, *dec;
|
|
|
|
u16 len;
|
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
if (!read_all(fd, hdr, sizeof(hdr))) {
|
|
|
|
status_trace("Failed reading header: %s", strerror(errno));
|
2017-02-21 05:45:29 +01:00
|
|
|
return NULL;
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-02-24 06:52:56 +01:00
|
|
|
if (!cryptomsg_decrypt_header(cs, hdr, &len)) {
|
|
|
|
status_trace("Failed hdr decrypt with rn=%"PRIu64, cs->rn-1);
|
2017-02-21 05:45:29 +01:00
|
|
|
return NULL;
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-02-24 06:52:35 +01:00
|
|
|
enc = tal_arr(ctx, u8, len + 16);
|
2017-02-24 06:52:56 +01:00
|
|
|
if (!read_all(fd, enc, tal_len(enc))) {
|
|
|
|
status_trace("Failed reading body: %s", strerror(errno));
|
2017-02-21 05:45:29 +01:00
|
|
|
return tal_free(enc);
|
2017-02-24 06:52:56 +01:00
|
|
|
}
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
dec = cryptomsg_decrypt_body(ctx, cs, enc);
|
|
|
|
tal_free(enc);
|
2017-02-24 06:52:56 +01:00
|
|
|
if (!dec)
|
|
|
|
status_trace("Failed body decrypt with rn=%"PRIu64, cs->rn-2);
|
|
|
|
else
|
|
|
|
status_trace("Read decrypt %s", tal_hex(trc, dec));
|
2017-02-21 05:45:29 +01:00
|
|
|
return dec;
|
|
|
|
}
|