Skip to content

Commit a2b3d33

Browse files
committed
connectd: do decryption for peers.
We temporarily hack to sync_crypto_write/sync_crypto_read functions to not do any crypto, and do it all in connectd. Signed-off-by: Rusty Russell <[email protected]>
1 parent 71f7366 commit a2b3d33

File tree

5 files changed

+95
-93
lines changed

5 files changed

+95
-93
lines changed

common/crypto_sync.c

+6-28
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
#include <netinet/tcp.h>
1313
#include <sys/socket.h>
1414
#include <wire/wire.h>
15+
#include <wire/wire_io.h>
16+
#include <wire/wire_sync.h>
1517

1618
void sync_crypto_write(struct per_peer_state *pps, const void *msg TAKES)
1719
{
1820
#if DEVELOPER
1921
bool post_sabotage = false, post_close;
2022
int type = fromwire_peektype(msg);
2123
#endif
22-
u8 *enc;
2324

2425
status_peer_io(LOG_IO_OUT, NULL, msg);
25-
enc = cryptomsg_encrypt_msg(NULL, &pps->cs, msg);
2626

2727
#if DEVELOPER
2828
switch (dev_disconnect(type)) {
@@ -44,9 +44,8 @@ void sync_crypto_write(struct per_peer_state *pps, const void *msg TAKES)
4444
break;
4545
}
4646
#endif
47-
if (!write_all(pps->peer_fd, enc, tal_count(enc)))
47+
if (!wire_sync_write(pps->peer_fd, msg))
4848
peer_failed_connection_lost();
49-
tal_free(enc);
5049

5150
#if DEVELOPER
5251
if (post_sabotage)
@@ -101,32 +100,11 @@ void sync_crypto_write_no_delay(struct per_peer_state *pps,
101100

102101
u8 *sync_crypto_read(const tal_t *ctx, struct per_peer_state *pps)
103102
{
104-
u8 hdr[18], *enc, *dec;
105-
u16 len;
106-
107-
if (!read_all(pps->peer_fd, hdr, sizeof(hdr))) {
108-
status_debug("Failed reading header: %s", strerror(errno));
109-
peer_failed_connection_lost();
110-
}
111-
112-
if (!cryptomsg_decrypt_header(&pps->cs, hdr, &len)) {
113-
status_debug("Failed hdr decrypt with rn=%"PRIu64,
114-
pps->cs.rn-1);
115-
peer_failed_connection_lost();
116-
}
117-
118-
enc = tal_arr(ctx, u8, len + 16);
119-
if (!read_all(pps->peer_fd, enc, tal_count(enc))) {
120-
status_debug("Failed reading body: %s", strerror(errno));
121-
peer_failed_connection_lost();
122-
}
123-
124-
dec = cryptomsg_decrypt_body(ctx, &pps->cs, enc);
125-
tal_free(enc);
103+
u8 *dec = wire_sync_read(ctx, pps->peer_fd);
126104
if (!dec)
127105
peer_failed_connection_lost();
128-
else
129-
status_peer_io(LOG_IO_IN, NULL, dec);
106+
107+
status_peer_io(LOG_IO_IN, NULL, dec);
130108

131109
return dec;
132110
}

connectd/connectd.c

-2
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ static struct peer *new_peer(struct daemon *daemon,
452452
peer->final_msg = NULL;
453453
peer->subd_in = NULL;
454454
peer->peer_in = NULL;
455-
peer->sent_to_subd = NULL;
456455
peer->sent_to_peer = NULL;
457456
peer->peer_outq = msg_queue_new(peer);
458457
peer->subd_outq = msg_queue_new(peer);
@@ -1988,7 +1987,6 @@ static void peer_final_msg(struct io_conn *conn,
19881987
if (peer) {
19891988
/* Log and encrypt message for peer. */
19901989
status_peer_io(LOG_IO_OUT, &id, finalmsg);
1991-
finalmsg = cryptomsg_encrypt_msg(NULL, &pps->cs, take(finalmsg));
19921990
multiplex_final_msg(peer, take(finalmsg));
19931991
}
19941992
}

connectd/multiplex.c

+82-60
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
#include "config.h"
44
#include <assert.h>
55
#include <ccan/io/io.h>
6+
#include <common/cryptomsg.h>
7+
#include <common/per_peer_state.h>
68
#include <common/status.h>
79
#include <common/utils.h>
810
#include <connectd/multiplex.h>
911
#include <errno.h>
1012
#include <sys/socket.h>
1113
#include <sys/types.h>
14+
#include <wire/wire_io.h>
15+
16+
void queue_peer_msg(struct peer *peer, const u8 *msg TAKES)
17+
{
18+
msg_enqueue(peer->peer_outq, msg);
19+
}
1220

1321
/* These four function handle subd->peer */
1422
static struct io_plan *after_final_msg(struct io_conn *peer_conn,
@@ -24,25 +32,39 @@ static struct io_plan *after_final_msg(struct io_conn *peer_conn,
2432
return io_close(peer_conn);
2533
}
2634

35+
static struct io_plan *encrypt_and_send(struct peer *peer,
36+
const u8 *msg TAKES,
37+
struct io_plan *(*next)
38+
(struct io_conn *peer_conn,
39+
struct peer *peer))
40+
{
41+
/* We free this and the encrypted version in next write_to_peer */
42+
peer->sent_to_peer = cryptomsg_encrypt_msg(peer, &peer->pps->cs, msg);
43+
return io_write(peer->to_peer,
44+
peer->sent_to_peer,
45+
tal_bytelen(peer->sent_to_peer),
46+
next, peer);
47+
}
48+
2749
static struct io_plan *write_to_peer(struct io_conn *peer_conn,
2850
struct peer *peer)
2951
{
52+
const u8 *msg;
3053
assert(peer->to_peer == peer_conn);
3154

3255
/* Free last sent one (if any) */
33-
tal_free(peer->sent_to_peer);
56+
peer->sent_to_peer = tal_free(peer->sent_to_peer);
3457

3558
/* Pop tail of send queue */
36-
peer->sent_to_peer = msg_dequeue(peer->peer_outq);
59+
msg = msg_dequeue(peer->peer_outq);
3760

3861
/* Nothing to send? */
39-
if (!peer->sent_to_peer) {
62+
if (!msg) {
4063
/* Send final once subd is not longer connected */
4164
if (peer->final_msg && !peer->to_subd) {
42-
return io_write(peer_conn,
43-
peer->final_msg,
44-
tal_bytelen(peer->final_msg),
45-
after_final_msg, peer);
65+
return encrypt_and_send(peer,
66+
peer->final_msg,
67+
after_final_msg);
4668
}
4769
/* Tell them to read again, */
4870
io_wake(&peer->subd_in);
@@ -52,102 +74,102 @@ static struct io_plan *write_to_peer(struct io_conn *peer_conn,
5274
write_to_peer, peer);
5375
}
5476

55-
return io_write(peer_conn,
56-
peer->sent_to_peer,
57-
tal_bytelen(peer->sent_to_peer),
58-
write_to_peer, peer);
77+
return encrypt_and_send(peer, take(msg), write_to_peer);
5978
}
6079

6180
static struct io_plan *read_from_subd(struct io_conn *subd_conn,
6281
struct peer *peer);
6382
static struct io_plan *read_from_subd_done(struct io_conn *subd_conn,
6483
struct peer *peer)
6584
{
66-
size_t len = ((size_t *)peer->subd_in)[1023];
67-
assert(peer->to_subd == subd_conn);
68-
69-
/* Trim to length */
70-
tal_resize(&peer->subd_in, len);
71-
72-
/* Tell them to write. */
73-
msg_enqueue(peer->peer_outq, take(peer->subd_in));
85+
/* Tell them to encrypt & write. */
86+
queue_peer_msg(peer, take(peer->subd_in));
7487
peer->subd_in = NULL;
88+
7589
/* Wait for them to wake us */
7690
return io_wait(subd_conn, &peer->subd_in, read_from_subd, peer);
7791
}
7892

7993
static struct io_plan *read_from_subd(struct io_conn *subd_conn,
8094
struct peer *peer)
8195
{
82-
/* We stash the length at the end */
83-
size_t *buf = tal_arr(peer, size_t, 1024);
84-
assert(peer->to_subd == subd_conn);
85-
86-
peer->subd_in = (u8 *)buf;
87-
return io_read_partial(subd_conn, peer->subd_in,
88-
sizeof(size_t) * 1023,
89-
&buf[1023],
90-
read_from_subd_done, peer);
96+
return io_read_wire(subd_conn, peer, &peer->subd_in,
97+
read_from_subd_done, peer);
9198
}
9299

93100
/* These four function handle peer->subd */
94101
static struct io_plan *write_to_subd(struct io_conn *subd_conn,
95102
struct peer *peer)
96103
{
104+
const u8 *msg;
97105
assert(peer->to_subd == subd_conn);
98106

99-
/* Free last sent one (if any) */
100-
tal_free(peer->sent_to_subd);
101-
102107
/* Pop tail of send queue */
103-
peer->sent_to_subd = msg_dequeue(peer->subd_outq);
108+
msg = msg_dequeue(peer->subd_outq);
104109

105110
/* Nothing to send? */
106-
if (!peer->sent_to_subd) {
107-
/* Tell them to read again, */
111+
if (!msg) {
112+
/* Tell them to read again. */
108113
io_wake(&peer->peer_in);
109114

110115
/* Wait for them to wake us */
111116
return msg_queue_wait(subd_conn, peer->subd_outq,
112117
write_to_subd, peer);
113118
}
114119

115-
return io_write(subd_conn,
116-
peer->sent_to_subd,
117-
tal_bytelen(peer->sent_to_subd),
118-
write_to_subd, peer);
120+
return io_write_wire(subd_conn, take(msg), write_to_subd, peer);
119121
}
120122

121-
static struct io_plan *read_from_peer(struct io_conn *peer_conn,
122-
struct peer *peer);
123-
static struct io_plan *read_from_peer_done(struct io_conn *peer_conn,
123+
static struct io_plan *read_hdr_from_peer(struct io_conn *peer_conn,
124+
struct peer *peer);
125+
static struct io_plan *read_body_from_peer_done(struct io_conn *peer_conn,
126+
struct peer *peer)
127+
{
128+
u8 *decrypted;
129+
130+
decrypted = cryptomsg_decrypt_body(NULL, &peer->pps->cs,
131+
peer->peer_in);
132+
if (!decrypted)
133+
return io_close(peer_conn);
134+
tal_free(peer->peer_in);
135+
136+
/* Tell them to write. */
137+
msg_enqueue(peer->subd_outq, take(decrypted));
138+
139+
/* Wait for them to wake us */
140+
return io_wait(peer_conn, &peer->peer_in, read_hdr_from_peer, peer);
141+
}
142+
143+
static struct io_plan *read_body_from_peer(struct io_conn *peer_conn,
124144
struct peer *peer)
125145
{
126-
size_t len = ((size_t *)peer->peer_in)[1023];
127-
assert(peer->to_peer == peer_conn);
146+
u16 len;
128147

129-
/* Trim to length */
130-
tal_resize(&peer->peer_in, len);
148+
if (!cryptomsg_decrypt_header(&peer->pps->cs, peer->peer_in, &len))
149+
return io_close(peer_conn);
131150

132-
/* Tell them to write. */
133-
msg_enqueue(peer->subd_outq, take(peer->peer_in));
134-
peer->peer_in = NULL;
135-
/* Wait for them to wake us */
136-
return io_wait(peer_conn, &peer->peer_in, read_from_peer, peer);
151+
tal_resize(&peer->peer_in, (u32)len + CRYPTOMSG_BODY_OVERHEAD);
152+
return io_read(peer_conn, peer->peer_in, tal_count(peer->peer_in),
153+
read_body_from_peer_done, peer);
137154
}
138155

139-
static struct io_plan *read_from_peer(struct io_conn *peer_conn,
140-
struct peer *peer)
156+
static struct io_plan *read_hdr_from_peer(struct io_conn *peer_conn,
157+
struct peer *peer)
141158
{
142-
/* We stash the length at the end */
143-
size_t *buf = tal_arr(peer, size_t, 1024);
144159
assert(peer->to_peer == peer_conn);
145160

146-
peer->peer_in = (u8 *)buf;
147-
return io_read_partial(peer_conn, peer->peer_in,
148-
sizeof(size_t) * 1023,
149-
&buf[1023],
150-
read_from_peer_done, peer);
161+
/* BOLT #8:
162+
*
163+
* ### Receiving and Decrypting Messages
164+
*
165+
* In order to decrypt the _next_ message in the network
166+
* stream, the following steps are completed:
167+
*
168+
* 1. Read _exactly_ 18 bytes from the network buffer.
169+
*/
170+
peer->peer_in = tal_arr(peer, u8, CRYPTOMSG_HDR_SIZE);
171+
return io_read(peer_conn, peer->peer_in, CRYPTOMSG_HDR_SIZE,
172+
read_body_from_peer, peer);
151173
}
152174

153175
static struct io_plan *subd_conn_init(struct io_conn *subd_conn, struct peer *peer)
@@ -200,7 +222,7 @@ struct io_plan *multiplex_peer_setup(struct io_conn *peer_conn,
200222
tal_add_destructor2(peer_conn, destroy_peer_conn, peer);
201223

202224
return io_duplex(peer_conn,
203-
read_from_peer(peer_conn, peer),
225+
read_hdr_from_peer(peer_conn, peer),
204226
write_to_peer(peer_conn, peer));
205227
}
206228

connectd/multiplex.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ struct peer {
2424
/* Output buffers. */
2525
struct msg_queue *subd_outq, *peer_outq;
2626

27-
/* Sent buffers (for freeing after sending) */
28-
const u8 *sent_to_subd, *sent_to_peer;
27+
/* Peer sent buffer (for freeing after sending) */
28+
const u8 *sent_to_peer;
2929
};
3030

3131
/* Set up peer->to_subd; sets fd_for_subd to pass to lightningd. */
@@ -38,4 +38,8 @@ struct io_plan *multiplex_peer_setup(struct io_conn *peer_conn,
3838
/* Send this message to peer and disconnect. */
3939
void multiplex_final_msg(struct peer *peer,
4040
const u8 *final_msg TAKES);
41+
42+
/* Inject a message into the output stream */
43+
void queue_peer_msg(struct peer *peer, const u8 *msg TAKES);
44+
4145
#endif /* LIGHTNING_CONNECTD_MULTIPLEX_H */

wire/wire_io.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct io_plan *io_read_wire_(struct io_conn *conn,
2727

2828
/* Write message from data (tal_count(data) gives length). data can be take() */
2929
struct io_plan *io_write_wire_(struct io_conn *conn,
30-
const u8 *data,
30+
const u8 *data TAKES,
3131
struct io_plan *(*next)(struct io_conn *, void *),
3232
void *next_arg);
3333

0 commit comments

Comments
 (0)