22
22
#define MAX_PKT_LEN (1024 * 1024)
23
23
24
24
/* BOLT#1:
25
- The header consists of the following fields in order:
26
-
27
- * `acknowledge`: an 8-byte little-endian field indicating the number of non-`authenticate` messages received and processed so far.
28
- * `length`: a 4-byte little-endian field indicating the size of the unencrypted body.
25
+ `length` is a 4-byte little-endian field indicating the size of the unencrypted body.
29
26
*/
30
27
31
- /* Do NOT take sizeof() this, since there's extra padding at the end! */
32
28
struct crypto_pkt {
33
- le64 acknowledge ;
34
29
le32 length ;
35
30
u8 auth_tag [crypto_aead_chacha20poly1305_ABYTES ];
36
31
37
32
/* ... contents... */
38
33
u8 data [];
39
34
};
40
35
41
- /* Use this instead of sizeof(struct crypto_pkt) */
42
- #define CRYPTO_HDR_LEN_NOTAG (8 + 4)
43
- #define CRYPTO_HDR_LEN (CRYPTO_HDR_LEN_NOTAG + crypto_aead_chacha20poly1305_ABYTES)
44
-
45
36
/* Temporary structure for negotiation (peer->io_data->neg) */
46
37
struct key_negotiate {
47
38
/* Our session secret key. */
@@ -113,7 +104,7 @@ struct io_data {
113
104
/* Stuff we need to keep around to talk to peer. */
114
105
struct dir_state in , out ;
115
106
116
- /* Header we're currently reading. */
107
+ /* Length we're currently reading. */
117
108
struct crypto_pkt hdr_in ;
118
109
119
110
/* Callback once packet decrypted. */
@@ -221,22 +212,21 @@ static Pkt *decrypt_pkt(struct peer *peer, struct crypto_pkt *cpkt,
221
212
return ret ;
222
213
}
223
214
224
- static struct crypto_pkt * encrypt_pkt (struct peer * peer , const Pkt * pkt , u64 ack ,
215
+ static struct crypto_pkt * encrypt_pkt (struct peer * peer , const Pkt * pkt ,
225
216
size_t * totlen )
226
217
{
227
218
struct crypto_pkt * cpkt ;
228
219
size_t len ;
229
220
struct io_data * iod = peer -> io_data ;
230
221
231
222
len = pkt__get_packed_size (pkt );
232
- * totlen = CRYPTO_HDR_LEN + len + crypto_aead_chacha20poly1305_ABYTES ;
223
+ * totlen = sizeof ( * cpkt ) + len + crypto_aead_chacha20poly1305_ABYTES ;
233
224
234
225
cpkt = (struct crypto_pkt * )tal_arr (peer , char , * totlen );
235
- cpkt -> acknowledge = cpu_to_le64 (ack );
236
226
cpkt -> length = cpu_to_le32 (len );
237
227
238
228
/* Encrypt header. */
239
- encrypt_in_place (cpkt , CRYPTO_HDR_LEN_NOTAG ,
229
+ encrypt_in_place (cpkt , sizeof ( cpkt -> length ) ,
240
230
& iod -> out .nonce , & iod -> out .enckey );
241
231
242
232
/* Encrypt body. */
@@ -246,11 +236,29 @@ static struct crypto_pkt *encrypt_pkt(struct peer *peer, const Pkt *pkt, u64 ack
246
236
return cpkt ;
247
237
}
248
238
249
- static struct io_plan * decrypt_body (struct io_conn * conn , struct peer * peer )
239
+ void peer_process_acks (struct peer * peer , uint64_t acknum )
250
240
{
251
241
struct io_data * iod = peer -> io_data ;
252
242
struct ack * ack ;
253
243
244
+ while ((ack = list_top (& iod -> acks , struct ack , list )) != NULL ) {
245
+ if (acknum < ack -> pktnum )
246
+ break ;
247
+ ack -> ack_cb (peer , ack -> ack_arg );
248
+ list_del_from (& iod -> acks , & ack -> list );
249
+ tal_free (ack );
250
+ }
251
+ }
252
+
253
+ uint64_t peer_outgoing_ack (const struct peer * peer )
254
+ {
255
+ return peer -> io_data -> in .count ;
256
+ }
257
+
258
+ static struct io_plan * decrypt_body (struct io_conn * conn , struct peer * peer )
259
+ {
260
+ struct io_data * iod = peer -> io_data ;
261
+
254
262
/* We have full packet. */
255
263
peer -> inpkt = decrypt_pkt (peer , iod -> in .cpkt ,
256
264
le32_to_cpu (iod -> hdr_in .length ));
@@ -261,21 +269,11 @@ static struct io_plan *decrypt_body(struct io_conn *conn, struct peer *peer)
261
269
if (peer -> inpkt -> pkt_case != PKT__PKT_AUTH )
262
270
iod -> in .count ++ ;
263
271
264
- log_debug (peer -> log , "Received packet ACK=%" PRIu64 " LEN=%u, type=%s" ,
265
- le64_to_cpu (iod -> hdr_in .acknowledge ),
272
+ log_debug (peer -> log , "Received packet LEN=%u, type=%s" ,
266
273
le32_to_cpu (iod -> hdr_in .length ),
267
274
peer -> inpkt -> pkt_case == PKT__PKT_AUTH ? "PKT_AUTH"
268
275
: input_name (peer -> inpkt -> pkt_case ));
269
276
270
- /* Do callbacks for any packets it acknowledged receiving. */
271
- while ((ack = list_top (& iod -> acks , struct ack , list )) != NULL ) {
272
- if (le64_to_cpu (iod -> hdr_in .acknowledge ) < ack -> pktnum )
273
- break ;
274
- ack -> ack_cb (peer , ack -> ack_arg );
275
- list_del_from (& iod -> acks , & ack -> list );
276
- tal_free (ack );
277
- }
278
-
279
277
return iod -> cb (conn , peer );
280
278
}
281
279
@@ -284,8 +282,8 @@ static struct io_plan *decrypt_header(struct io_conn *conn, struct peer *peer)
284
282
struct io_data * iod = peer -> io_data ;
285
283
size_t body_len ;
286
284
287
- /* We have header : Check it. */
288
- if (!decrypt_in_place (& iod -> hdr_in , CRYPTO_HDR_LEN_NOTAG ,
285
+ /* We have length : Check it. */
286
+ if (!decrypt_in_place (& iod -> hdr_in . length , sizeof ( iod -> hdr_in . length ) ,
289
287
& iod -> in .nonce , & iod -> in .enckey )) {
290
288
log_unusual (peer -> log , "Header decryption failed" );
291
289
return io_close (conn );
@@ -305,9 +303,9 @@ static struct io_plan *decrypt_header(struct io_conn *conn, struct peer *peer)
305
303
body_len = le32_to_cpu (iod -> hdr_in .length )
306
304
+ crypto_aead_chacha20poly1305_ABYTES ;
307
305
308
- iod -> in .cpkt = (struct crypto_pkt * )tal_arr ( peer , char ,
309
- CRYPTO_HDR_LEN + body_len );
310
- memcpy ( iod -> in .cpkt , & iod -> hdr_in , CRYPTO_HDR_LEN ) ;
306
+ iod -> in .cpkt = (struct crypto_pkt * )
307
+ tal_arr ( peer , char , sizeof ( iod -> hdr_in ) + body_len );
308
+ * iod -> in .cpkt = iod -> hdr_in ;
311
309
312
310
return io_read (conn , iod -> in .cpkt -> data , body_len , decrypt_body , peer );
313
311
}
@@ -320,7 +318,7 @@ struct io_plan *peer_read_packet(struct io_conn *conn,
320
318
struct io_data * iod = peer -> io_data ;
321
319
322
320
iod -> cb = cb ;
323
- return io_read (conn , & iod -> hdr_in , CRYPTO_HDR_LEN ,
321
+ return io_read (conn , & iod -> hdr_in , sizeof ( iod -> hdr_in ) ,
324
322
decrypt_header , peer );
325
323
}
326
324
@@ -340,7 +338,7 @@ struct io_plan *peer_write_packet_(struct io_conn *conn,
340
338
* via io_write */
341
339
tal_free (iod -> out .cpkt );
342
340
343
- iod -> out .cpkt = encrypt_pkt (peer , pkt , peer -> io_data -> in . count , & totlen );
341
+ iod -> out .cpkt = encrypt_pkt (peer , pkt , & totlen );
344
342
345
343
/* Set up ack callback if any. */
346
344
if (ack_cb ) {
@@ -417,24 +415,26 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer)
417
415
return io_close (conn );
418
416
}
419
417
420
- tal_free (auth );
421
-
422
418
/* Auth messages don't add to count. */
423
419
assert (peer -> io_data -> in .count == 0 );
424
420
425
421
/* BOLT #1:
426
- * The receiver MUST NOT examine the `acknowledge` value until
427
- * after the authentication fields have been successfully
428
- * validated. The `acknowledge` field MUST BE set to the
429
- * number of non-authenticate messages received and processed.
422
+ *
423
+ * The receiver MUST NOT examine the `ack` value until after
424
+ * the authentication fields have been successfully validated.
425
+ * The `ack` field MUST BE set to the number of
426
+ * non-authenticate messages received and processed if
427
+ * non-zero.
430
428
*/
431
429
/* FIXME: Handle reconnects. */
432
- if (le64_to_cpu ( peer -> io_data -> hdr_in . acknowledge ) != 0 ) {
430
+ if (auth -> ack != 0 ) {
433
431
log_unusual (peer -> log , "FIXME: non-zero acknowledge %" PRIu64 ,
434
- le64_to_cpu ( peer -> io_data -> hdr_in . acknowledge ) );
432
+ auth -> ack );
435
433
return io_close (conn );
436
434
}
437
435
436
+ tal_free (auth );
437
+
438
438
/* All complete, return to caller. */
439
439
cb = neg -> cb ;
440
440
peer -> io_data -> neg = tal_free (neg );
@@ -524,6 +524,8 @@ static struct io_plan *discard_extra(struct io_conn *conn, struct peer *peer)
524
524
525
525
len -= sizeof (neg -> their_sessionpubkey );
526
526
discard = tal_arr (neg , char , len );
527
+ log_unusual (peer -> log , "Ignoring %zu extra handshake bytes" ,
528
+ len );
527
529
return io_read (conn , discard , len , keys_exchanged , peer );
528
530
}
529
531
@@ -592,7 +594,12 @@ struct io_plan *peer_crypto_setup(struct io_conn *conn, struct peer *peer,
592
594
secp256k1_pubkey sessionkey ;
593
595
struct key_negotiate * neg ;
594
596
595
- BUILD_ASSERT (CRYPTO_HDR_LEN == offsetof(struct crypto_pkt , data ));
597
+ /* BOLT #1:
598
+ *
599
+ * The 4-byte length for each message is encrypted separately
600
+ * (resulting in a 20 byte header when the authentication tag
601
+ * is appended) */
602
+ BUILD_ASSERT (sizeof (struct crypto_pkt ) == 20 );
596
603
597
604
peer -> io_data = tal (peer , struct io_data );
598
605
list_head_init (& peer -> io_data -> acks );
0 commit comments