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>
|
2018-08-02 08:49:55 +02:00
|
|
|
#include <common/peer_failed.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#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>
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
void sync_crypto_write(struct crypto_state *cs, int fd, const void *msg TAKES)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
bool post_sabotage = false;
|
2017-06-27 04:55:06 +02:00
|
|
|
int type = fromwire_peektype(msg);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2018-02-05 05:09:28 +01:00
|
|
|
u8 *enc;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-05-10 01:18:24 +02:00
|
|
|
status_peer_io(LOG_IO_OUT, msg);
|
2018-02-05 05:09:28 +01:00
|
|
|
enc = cryptomsg_encrypt_msg(NULL, cs, msg);
|
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
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);
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-05-24 12:10:16 +02:00
|
|
|
case DEV_DISCONNECT_DROPPKT:
|
|
|
|
enc = tal_free(enc); /* FALL THRU */
|
|
|
|
case DEV_DISCONNECT_AFTER:
|
|
|
|
post_sabotage = true;
|
|
|
|
break;
|
2017-09-06 03:07:19 +02:00
|
|
|
case DEV_DISCONNECT_BLACKHOLE:
|
|
|
|
dev_blackhole_fd(fd);
|
|
|
|
break;
|
2017-09-26 06:57:31 +02:00
|
|
|
case DEV_DISCONNECT_NORMAL:
|
2017-05-24 12:10:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2018-08-02 08:49:55 +02:00
|
|
|
if (!write_all(fd, enc, tal_count(enc)))
|
|
|
|
peer_failed_connection_lost();
|
2017-02-21 05:45:29 +01:00
|
|
|
tal_free(enc);
|
2017-05-24 12:10:16 +02:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-05-24 12:10:16 +02:00
|
|
|
if (post_sabotage)
|
|
|
|
dev_sabotage_fd(fd);
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
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);
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
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);
|
2018-07-28 08:00:16 +02:00
|
|
|
if (!read_all(fd, enc, tal_count(enc))) {
|
2017-02-24 06:52:56 +01:00
|
|
|
status_trace("Failed reading body: %s", strerror(errno));
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
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)
|
2018-08-02 08:49:55 +02:00
|
|
|
peer_failed_connection_lost();
|
2017-02-24 06:52:56 +01:00
|
|
|
else
|
2018-05-10 01:18:24 +02:00
|
|
|
status_peer_io(LOG_IO_IN, dec);
|
2018-02-05 05:09:28 +01:00
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
return dec;
|
|
|
|
}
|