Commit Graph

34 Commits

Author SHA1 Message Date
Christian Decker 6a072c4c6e Fix a double-free bug in the negotiation phase
The callback on `key_negotiate` was closing the connection under
certain circumstances and would also `free` the key_negotiate, which
would then be freed again once it returns. We steal it off of the
connection during the callback and doing the free manually afterwards
to make sure this can't happen.

Thanks to @jgriffiths for tracking this one down.

Fixes #142

Reported-By: @bjd and @bgorlick
2017-04-24 05:17:13 +00:00
Rusty Russell 2ce6a4bcca daemon/peer: move struct peer to internal header.
We use a different 'struct peer' in the new daemons, so make sure
the structure isn't assumed in any shared files.

This is a temporary shim.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-03-02 22:51:49 +10:30
Rusty Russell 1edce4878d bitcoin/signature.h: remove struct signature.
It's a wrapper around secp256k1_ecdsa_signature, so use that directly.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-25 11:03:55 +10:30
Rusty Russell c2cc164d6d daemon: disable old BOLT checks.
This is useful for the next patch.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-01-04 14:09:20 +10:30
Lucas Betschart 33d6cbe6bc Fix -Wgnu-variable-sized-type-not-at-end 2016-12-14 08:41:30 +01:00
Rusty Russell 36c8fc7ef8 lightningd: remove secpctx
Use the global in the few remaining places.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-12-02 18:13:27 +10:30
Rusty Russell a4fdaab5b3 Use global secp256k1_ctx instead of passing it around.
If I'd known how large this patch would be (though trivial), I'd
have done it in parts.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-12-02 18:12:58 +10:30
Rusty Russell f5c00deec7 Remove trailing whitespace from source.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-11 09:32:04 +10:30
Rusty Russell 8a1559a343 peer: free init packet.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:04 +10:30
Rusty Russell e0368cc82d cryptopkt: don't leak negotiation state.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-04 11:17:03 +10:30
Rusty Russell 642a2f2d4e cryptopkt: free output packets after encryption.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:34:58 +10:30
Rusty Russell 95a01670ac cryptopkt: free incoming packet after decryption.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-11-01 21:34:57 +10:30
Rusty Russell bb0bf2b5ad log: add easy way to log hex blobs.
Use it in one case, and also fix another logging FIXME.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:32 +09:30
Rusty Russell 42cf0ef543 peer: do logging before crypto is on.
We create a logging object when we connect, then carry it through.  If
it comes from the database, we just use the peerid as the log prefix.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-31 16:06:31 +09:30
Rusty Russell 7709eb9b4a protocol: use separate ack packet.
It's a data-leak to send ack before we have verified identity of peer.
Plus, we can't send it until we know which peer it is, anyway!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-18 14:23:46 +09:30
Rusty Russell 8345bff825 cryptopkt: revert ack split in authenticate_packet
We need to remember the transmit order anyway, so a single counter works.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-08-09 13:11:11 +09:30
Rusty Russell 5472f73f9c cryptopkt: update to latest encryption BOLT.
As per lightning-rfc commit b579b16866855da166981192c0f0549517069d4e.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-19 12:37:33 +09:30
Rusty Russell a613d8d1fb peer: make id a pointer, NULL until we know peer's ID.
Much better than undefined, and testing for NULL is better than
testing for STATE_INIT.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell f2d835522c bitcoin: implement sig_valid.
Update libsecp256k1 has a normalize function, which allows us to test
if the signature was in low-S form.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell c649f62e43 libsecp256k1: update.
Add Makefile target update-secp256k1, and run it.

The only API change is that len is now an IN-OUT parameter to serialization
functions.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 69cb158edd base58, script, protobuf_convert: don't use temporary secp256k1 context.
We use libsecp256k1 to convert signatures to DER; we were creating a
temporary one, but we really should be handing the one we have in dstate
through.  This does that, everywhere.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-01 12:00:17 +09:30
Rusty Russell 45a6f81c3c protocol: remove ack fields.
As per lightning-rfc commit 8ee09e749990a11fa53bea03d5961cfde4be4616,
we remove the acks from the protocol now they're no longer needed (and
all the infrastructure).

We also place the commit number in the commit_info where it logically
belongs, removing it from the peer struct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell f43cc72d6a Makefile: add generated packet names.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-26 15:25:24 +09:30
Rusty Russell 03a538ca02 check-bolt: check that comments in code match the specs.
And fix the mistakes!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-05-03 11:27:56 +09:30
Rusty Russell 0f35441a29 protocol: move `ack` out of header into specific packets.
This reflects the BOLT #1/#2 protocol change, as suggeted by Pierre.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:31:43 +09:30
Rusty Russell 15f7ec436c Fix "make check-source"
Obviously, haven't run it in a while.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-04-11 16:28:43 +09:30
Rusty Russell afedf0e8ac cryptopkt: implement ack callbacks.
For the change to asynchronous updates as specified by BOLT #2, we
need to know when the other side acknowledged a packet.  This creates
a simple callback mechanism for it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:03:44 +10:30
Rusty Russell 8131ff57a8 cryptopkt: don't create clever io routine, embed callback in io_data.
We have a structure, let's use it to store the callback and avoid
lots of complex code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-24 12:02:25 +10:30
Rusty Russell 18a9e5bda2 daemon: Implement encryption as per BOLT #1.
This uses libsodium (we could use openssl, but the required primitives
are only in 1.1.0 which is still in alpha).

It doesn't handle reconnections yet, either.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-16 16:06:17 +10:30
Rusty Russell 923313e3d3 crypto: add length prefix to handshake.
As per BOLT#1.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:10:42 +10:30
Rusty Russell 6a062d8527 daemon: check protobuf-c version, report failure.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-03-15 17:09:42 +10:30
Rusty Russell fc49e3fd74 daemon: rename 'state' to 'dstate' everywhere.
This is the daemon state, not the state machine state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:49 +10:30
Rusty Russell 08ccb4b6f0 getpeers: new command.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30
Rusty Russell 74f294e36c daemon: encrypted communication (version 3)
After useful feedback from Anthony Towns and Mats Jerratsch (of
thunder.network fame), this is the third version of inter-node crypto.

1) First, each side sends a 33-byte session pubkey.  This is a
   bitcoin-style compressed EC key, unique for each session.
  
2) ECDH is used to derive a shared secret.  From this we generate
   the following transmission encoding parameters for each side:
   Session AES-128 key: SHA256(shared-secret || my-sessionpubkey || 0)
   Session HMAC key: SHA256(shared-secret || my-sessionpubkey || 1)
   IV for AES: SHA256(shared-secret || my-sessionpubkey || 2)

3) All packets from then on are encrypted of form:
	/* HMAC, covering totlen and data */
	struct sha256 hmac;
	/* Total data transmitted (including this). */
	le64 totlen;
	/* Encrypted contents, rounded up to 16 byte boundary. */
	u8 data[];

4) The first packet is an Authenticate protobuf, containing this node's
   pubkey, and a bitcoin-style EC signature of the other side's session
   pubkey.

5) Unknown protobuf fields are handled in the protocol as follows
   (including in the initial Authenticate packet):

   1) Odd numbered fields are optional, and backwards compatible.
   2) Even numbered fields are required; abort if you get one.

Currently both sides just send an error packet "hello" after the
handshake, and make sure they receive the same.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-01-22 06:41:48 +10:30