2016-01-21 21:11:48 +01:00
|
|
|
#include "bitcoin/shadouble.h"
|
|
|
|
#include "bitcoin/signature.h"
|
|
|
|
#include "cryptopkt.h"
|
|
|
|
#include "lightning.pb-c.h"
|
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
2016-03-16 06:36:17 +01:00
|
|
|
#include "names.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "peer.h"
|
|
|
|
#include "protobuf_convert.h"
|
|
|
|
#include "secrets.h"
|
|
|
|
#include <ccan/build_assert/build_assert.h>
|
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/mem/mem.h>
|
|
|
|
#include <ccan/short_types/short_types.h>
|
2016-07-01 03:57:57 +02:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <secp256k1.h>
|
|
|
|
#include <secp256k1_ecdh.h>
|
2016-04-11 08:58:43 +02:00
|
|
|
#include <sodium/crypto_aead_chacha20poly1305.h>
|
|
|
|
#include <sodium/randombytes.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
#define MAX_PKT_LEN (1024 * 1024)
|
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
/* BOLT#1:
|
2016-04-11 09:01:43 +02:00
|
|
|
`length` is a 4-byte little-endian field indicating the size of the unencrypted body.
|
2016-03-16 06:36:17 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
struct crypto_pkt {
|
2016-03-16 06:36:17 +01:00
|
|
|
le32 length;
|
|
|
|
u8 auth_tag[crypto_aead_chacha20poly1305_ABYTES];
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* ... contents... */
|
|
|
|
u8 data[];
|
|
|
|
};
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Temporary structure for negotiation */
|
2016-01-21 21:11:48 +01:00
|
|
|
struct key_negotiate {
|
2016-08-18 06:53:46 +02:00
|
|
|
struct lightningd_state *dstate;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Our session secret key. */
|
|
|
|
u8 seckey[32];
|
|
|
|
|
|
|
|
/* Our pubkey, their pubkey. */
|
2016-03-15 07:40:42 +01:00
|
|
|
le32 keylen;
|
2016-01-21 21:11:48 +01:00
|
|
|
u8 our_sessionpubkey[33], their_sessionpubkey[33];
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* After DH key exchange, we create io_data to check auth. */
|
|
|
|
struct io_data *iod;
|
|
|
|
|
|
|
|
/* Did we expect a particular ID? */
|
|
|
|
const struct pubkey *expected_id;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Callback once it's all done. */
|
2016-08-18 06:53:46 +02:00
|
|
|
struct io_plan *(*cb)(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct io_data *iod,
|
|
|
|
const struct pubkey *id,
|
|
|
|
void *arg);
|
|
|
|
void *arg;
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct enckey {
|
|
|
|
struct sha256 k;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
/* BOLT #1:
|
2016-05-02 08:28:56 +02:00
|
|
|
* * sending-key: SHA256(shared-secret || sending-node-session-pubkey)
|
|
|
|
* * receiving-key: SHA256(shared-secret || receiving-node-session-pubkey)
|
2016-03-16 06:36:17 +01:00
|
|
|
*/
|
|
|
|
static struct enckey enckey_from_secret(const unsigned char secret[32],
|
|
|
|
const unsigned char serial_pubkey[33])
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct sha256_ctx ctx;
|
2016-03-16 06:36:17 +01:00
|
|
|
struct enckey enckey;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
sha256_init(&ctx);
|
|
|
|
sha256_update(&ctx, memcheck(secret, 32), 32);
|
|
|
|
sha256_update(&ctx, memcheck(serial_pubkey, 33), 33);
|
2016-03-16 06:36:17 +01:00
|
|
|
sha256_done(&ctx, &enckey.k);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
return enckey;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dir_state {
|
2016-03-16 06:36:17 +01:00
|
|
|
u64 nonce;
|
|
|
|
struct enckey enckey;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
/* Current packet (encrypted). */
|
2016-01-21 21:11:48 +01:00
|
|
|
struct crypto_pkt *cpkt;
|
2016-03-16 06:36:17 +01:00
|
|
|
size_t pkt_len;
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
static void setup_crypto(struct dir_state *dir,
|
2016-01-21 21:11:48 +01:00
|
|
|
u8 shared_secret[32], u8 serial_pubkey[33])
|
|
|
|
{
|
2016-03-16 06:36:17 +01:00
|
|
|
/* BOLT #1: Nonces...MUST begin at 0 */
|
|
|
|
dir->nonce = 0;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
dir->enckey = enckey_from_secret(shared_secret, serial_pubkey);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
dir->cpkt = NULL;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct io_data {
|
|
|
|
/* Stuff we need to keep around to talk to peer. */
|
|
|
|
struct dir_state in, out;
|
|
|
|
|
2016-04-11 09:01:43 +02:00
|
|
|
/* Length we're currently reading. */
|
2016-01-21 21:11:48 +01:00
|
|
|
struct crypto_pkt hdr_in;
|
|
|
|
|
2016-03-24 02:32:25 +01:00
|
|
|
/* Callback once packet decrypted. */
|
|
|
|
struct io_plan *(*cb)(struct io_conn *, struct peer *);
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
/* Once peer is assigned, this is set. */
|
|
|
|
struct peer *peer;
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void *proto_tal_alloc(void *allocator_data, size_t size)
|
|
|
|
{
|
|
|
|
return tal_arr(allocator_data, char, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void proto_tal_free(void *allocator_data, void *pointer)
|
|
|
|
{
|
|
|
|
tal_free(pointer);
|
|
|
|
}
|
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
static void le64_nonce(unsigned char *npub, u64 nonce)
|
|
|
|
{
|
|
|
|
/* BOLT #1: Nonces are 64-bit little-endian numbers */
|
|
|
|
le64 le_nonce = cpu_to_le64(nonce);
|
|
|
|
memcpy(npub, &le_nonce, sizeof(le_nonce));
|
|
|
|
BUILD_ASSERT(crypto_aead_chacha20poly1305_NPUBBYTES == sizeof(le_nonce));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encrypts data..data + len - 1 inclusive into data..data + len - 1 and
|
|
|
|
* then writes the authentication tag at data+len.
|
|
|
|
*
|
|
|
|
* This increments nonce every time.
|
|
|
|
*/
|
|
|
|
static void encrypt_in_place(void *data, size_t len,
|
|
|
|
u64 *nonce, const struct enckey *enckey)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long long clen;
|
|
|
|
unsigned char npub[crypto_aead_chacha20poly1305_NPUBBYTES];
|
|
|
|
|
|
|
|
le64_nonce(npub, *nonce);
|
|
|
|
ret = crypto_aead_chacha20poly1305_encrypt(data, &clen,
|
|
|
|
memcheck(data, len), len,
|
|
|
|
NULL, 0, NULL,
|
|
|
|
npub, enckey->k.u.u8);
|
|
|
|
assert(ret == 0);
|
|
|
|
assert(clen == len + crypto_aead_chacha20poly1305_ABYTES);
|
|
|
|
(*nonce)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checks authentication tag at data+len, then
|
|
|
|
* decrypts data..data + len - 1 inclusive into data..data + len - 1.
|
|
|
|
*
|
|
|
|
* This increments nonce every time.
|
|
|
|
*/
|
|
|
|
static bool decrypt_in_place(void *data, size_t len,
|
|
|
|
u64 *nonce, const struct enckey *enckey)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned long long mlen;
|
|
|
|
unsigned char npub[crypto_aead_chacha20poly1305_NPUBBYTES];
|
|
|
|
|
|
|
|
le64_nonce(npub, *nonce);
|
|
|
|
mlen = len + crypto_aead_chacha20poly1305_ABYTES;
|
|
|
|
|
|
|
|
ret = crypto_aead_chacha20poly1305_decrypt(data, &mlen, NULL,
|
|
|
|
memcheck(data, mlen), mlen,
|
|
|
|
NULL, 0,
|
|
|
|
npub, enckey->k.u.u8);
|
|
|
|
if (ret == 0) {
|
|
|
|
assert(mlen == len);
|
|
|
|
(*nonce)++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static Pkt *decrypt_body(const tal_t *ctx, struct io_data *iod, struct log *log,
|
|
|
|
struct crypto_pkt *cpkt, size_t data_len)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct ProtobufCAllocator prototal;
|
|
|
|
Pkt *ret;
|
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
if (!decrypt_in_place(cpkt->data, data_len,
|
|
|
|
&iod->in.nonce, &iod->in.enckey)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(log, "Body decryption failed");
|
2016-01-21 21:11:48 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* De-protobuf it. */
|
|
|
|
prototal.alloc = proto_tal_alloc;
|
|
|
|
prototal.free = proto_tal_free;
|
2016-08-18 06:53:46 +02:00
|
|
|
prototal.allocator_data = tal(ctx, char);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
ret = pkt__unpack(&prototal, data_len, cpkt->data);
|
2016-03-15 07:39:42 +01:00
|
|
|
if (!ret) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(log, "Packet failed to unpack!");
|
2016-01-21 21:11:48 +01:00
|
|
|
tal_free(prototal.allocator_data);
|
2016-08-18 06:53:46 +02:00
|
|
|
} else {
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Make sure packet owns contents */
|
2016-08-18 06:53:46 +02:00
|
|
|
tal_steal(ctx, ret);
|
2016-01-21 21:11:48 +01:00
|
|
|
tal_steal(ret, prototal.allocator_data);
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
log_debug(log, "Received packet LEN=%u, type=%s",
|
|
|
|
le32_to_cpu(iod->hdr_in.length),
|
|
|
|
ret->pkt_case == PKT__PKT_AUTH ? "PKT_AUTH"
|
|
|
|
: pkt_name(ret->pkt_case));
|
|
|
|
}
|
2016-01-21 21:11:48 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct crypto_pkt *encrypt_pkt(struct io_data *iod, const Pkt *pkt,
|
2016-03-16 06:36:17 +01:00
|
|
|
size_t *totlen)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct crypto_pkt *cpkt;
|
2016-03-16 06:36:17 +01:00
|
|
|
size_t len;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
len = pkt__get_packed_size(pkt);
|
2016-04-11 09:01:43 +02:00
|
|
|
*totlen = sizeof(*cpkt) + len + crypto_aead_chacha20poly1305_ABYTES;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
cpkt = (struct crypto_pkt *)tal_arr(iod, char, *totlen);
|
2016-03-16 06:36:17 +01:00
|
|
|
cpkt->length = cpu_to_le32(len);
|
|
|
|
|
|
|
|
/* Encrypt header. */
|
2016-04-11 09:01:43 +02:00
|
|
|
encrypt_in_place(cpkt, sizeof(cpkt->length),
|
2016-03-16 06:36:17 +01:00
|
|
|
&iod->out.nonce, &iod->out.enckey);
|
|
|
|
|
|
|
|
/* Encrypt body. */
|
|
|
|
pkt__pack(pkt, cpkt->data);
|
|
|
|
encrypt_in_place(cpkt->data, len, &iod->out.nonce, &iod->out.enckey);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
return cpkt;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *recv_body(struct io_conn *conn, struct peer *peer)
|
2016-04-11 09:01:43 +02:00
|
|
|
{
|
|
|
|
struct io_data *iod = peer->io_data;
|
|
|
|
|
2016-03-16 06:36:17 +01:00
|
|
|
/* We have full packet. */
|
2016-08-18 06:53:46 +02:00
|
|
|
peer->inpkt = decrypt_body(iod, iod, peer->log, iod->in.cpkt,
|
|
|
|
le32_to_cpu(iod->hdr_in.length));
|
2016-01-21 21:11:48 +01:00
|
|
|
if (!peer->inpkt)
|
2016-03-24 02:32:25 +01:00
|
|
|
return io_close(conn);
|
2016-03-16 06:36:17 +01:00
|
|
|
|
2016-03-24 02:32:25 +01:00
|
|
|
return iod->cb(conn, peer);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static bool decrypt_header(struct log *log, struct io_data *iod,
|
|
|
|
size_t *body_len)
|
2016-03-24 02:32:25 +01:00
|
|
|
{
|
2016-04-11 09:01:43 +02:00
|
|
|
/* We have length: Check it. */
|
|
|
|
if (!decrypt_in_place(&iod->hdr_in.length, sizeof(iod->hdr_in.length),
|
2016-03-24 02:32:25 +01:00
|
|
|
&iod->in.nonce, &iod->in.enckey)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(log, "Header decryption failed");
|
|
|
|
return false;
|
2016-03-24 02:32:25 +01:00
|
|
|
}
|
2016-08-18 06:53:46 +02:00
|
|
|
log_debug(log, "Decrypted header len %u",
|
2016-03-24 02:32:25 +01:00
|
|
|
le32_to_cpu(iod->hdr_in.length));
|
|
|
|
|
|
|
|
/* BOLT #1: `length` MUST NOT exceed 1MB (1048576 bytes). */
|
|
|
|
if (le32_to_cpu(iod->hdr_in.length) > MAX_PKT_LEN) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(log,
|
2016-03-24 02:32:25 +01:00
|
|
|
"Packet overlength: %"PRIu64,
|
|
|
|
le64_to_cpu(iod->hdr_in.length));
|
2016-08-18 06:53:46 +02:00
|
|
|
return false;
|
2016-03-24 02:32:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate room for body, copy header. */
|
2016-08-18 06:53:46 +02:00
|
|
|
*body_len = le32_to_cpu(iod->hdr_in.length)
|
2016-03-24 02:32:25 +01:00
|
|
|
+ crypto_aead_chacha20poly1305_ABYTES;
|
|
|
|
|
2016-04-11 09:01:43 +02:00
|
|
|
iod->in.cpkt = (struct crypto_pkt *)
|
2016-08-18 06:53:46 +02:00
|
|
|
tal_arr(iod, char, sizeof(iod->hdr_in) + *body_len);
|
2016-04-11 09:01:43 +02:00
|
|
|
*iod->in.cpkt = iod->hdr_in;
|
2016-08-18 06:53:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *recv_header(struct io_conn *conn, struct peer *peer)
|
|
|
|
{
|
|
|
|
struct io_data *iod = peer->io_data;
|
|
|
|
size_t body_len;
|
2016-03-24 02:32:25 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!decrypt_header(peer->log, iod, &body_len))
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
return io_read(conn, iod->in.cpkt->data, body_len, recv_body, peer);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct io_plan *peer_read_packet(struct io_conn *conn,
|
|
|
|
struct peer *peer,
|
|
|
|
struct io_plan *(*cb)(struct io_conn *,
|
|
|
|
struct peer *))
|
|
|
|
{
|
2016-03-24 02:32:25 +01:00
|
|
|
struct io_data *iod = peer->io_data;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-03-24 02:32:25 +01:00
|
|
|
iod->cb = cb;
|
2016-04-11 09:01:43 +02:00
|
|
|
return io_read(conn, &iod->hdr_in, sizeof(iod->hdr_in),
|
2016-08-18 06:53:46 +02:00
|
|
|
recv_header, peer);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Caller must free data! */
|
2016-05-26 07:55:24 +02:00
|
|
|
struct io_plan *peer_write_packet(struct io_conn *conn,
|
|
|
|
struct peer *peer,
|
|
|
|
const Pkt *pkt,
|
|
|
|
struct io_plan *(*next)(struct io_conn *,
|
|
|
|
struct peer *))
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct io_data *iod = peer->io_data;
|
|
|
|
size_t totlen;
|
|
|
|
|
|
|
|
/* We free previous packet here, rather than doing indirection
|
|
|
|
* via io_write */
|
|
|
|
tal_free(iod->out.cpkt);
|
2016-03-16 06:36:17 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
iod->out.cpkt = encrypt_pkt(iod, pkt, &totlen);
|
2016-03-16 06:36:17 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
return io_write(conn, iod->out.cpkt, totlen, next, peer);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static void *pkt_unwrap(Pkt *inpkt, struct log *log, Pkt__PktCase which)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
const ProtobufCMessage *base;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (inpkt->pkt_case != which) {
|
|
|
|
log_unusual(log, "Expected %u, got %u",
|
|
|
|
which, inpkt->pkt_case);
|
2016-01-21 21:11:48 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's a union, and each member starts with base. Pick one */
|
2016-08-18 06:53:46 +02:00
|
|
|
base = &inpkt->error->base;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* Look for unknown fields. Remember, "It's OK to be odd!" */
|
|
|
|
for (i = 0; i < base->n_unknown_fields; i++) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_debug(log, "Unknown field in %u: %u",
|
2016-01-21 21:11:48 +01:00
|
|
|
which, base->unknown_fields[i].tag);
|
|
|
|
/* Odd is OK */
|
|
|
|
if (base->unknown_fields[i].tag & 1)
|
|
|
|
continue;
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(log, "Unknown field %u in %u",
|
2016-01-21 21:11:48 +01:00
|
|
|
base->unknown_fields[i].tag, which);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-08-18 06:53:46 +02:00
|
|
|
return inpkt->error;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static bool check_proof(struct key_negotiate *neg, struct log *log,
|
|
|
|
Pkt *inpkt,
|
|
|
|
const struct pubkey *expected_id,
|
|
|
|
struct pubkey *id)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
struct sha256_double sha;
|
|
|
|
struct signature sig;
|
|
|
|
Authenticate *auth;
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
auth = pkt_unwrap(inpkt, log, PKT__PKT_AUTH);
|
2016-01-21 21:11:48 +01:00
|
|
|
if (!auth)
|
2016-08-18 06:53:46 +02:00
|
|
|
return false;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-07-19 05:07:33 +02:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The receiving node MUST check that:
|
|
|
|
*
|
|
|
|
* 1. `node_id` is the expected value for the sending node.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!proto_to_pubkey(neg->dstate->secpctx, auth->node_id, id)) {
|
|
|
|
log_unusual(log, "Invalid auth id");
|
|
|
|
return false;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (expected_id && !structeq(id, expected_id)) {
|
|
|
|
log_unusual(log, "Incorrect auth id");
|
|
|
|
return false;
|
2016-07-01 03:57:57 +02:00
|
|
|
}
|
2016-07-19 05:07:33 +02:00
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* 2. `session_sig` is a valid secp256k1 ECDSA signature encoded as
|
|
|
|
* a 32-byte big endian R value, followed by a 32-byte big
|
|
|
|
* endian S value.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!proto_to_signature(neg->dstate->secpctx, auth->session_sig,
|
2016-07-19 05:07:33 +02:00
|
|
|
&sig)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(log, "Invalid auth signature");
|
|
|
|
return false;
|
2016-07-19 05:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* 3. `session_sig` is the signature of the SHA256 of SHA256 of the
|
|
|
|
* its own sessionpubkey, using the secret key corresponding to
|
|
|
|
* the sender's `node_id`.
|
|
|
|
*/
|
|
|
|
sha256_double(&sha, neg->our_sessionpubkey,
|
|
|
|
sizeof(neg->our_sessionpubkey));
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!check_signed_hash(neg->dstate->secpctx, &sha, &sig, id)) {
|
|
|
|
log_unusual(log, "Bad auth signature");
|
|
|
|
return false;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *recv_body_negotiate(struct io_conn *conn,
|
|
|
|
struct key_negotiate *neg)
|
|
|
|
{
|
|
|
|
struct io_data *iod = neg->iod;
|
|
|
|
struct io_plan *plan;
|
|
|
|
Pkt *pkt;
|
|
|
|
struct pubkey id;
|
2016-03-16 06:36:17 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* We have full packet. */
|
|
|
|
pkt = decrypt_body(neg, iod, neg->dstate->base_log, iod->in.cpkt,
|
|
|
|
le32_to_cpu(iod->hdr_in.length));
|
|
|
|
if (!pkt)
|
2016-03-16 06:36:17 +01:00
|
|
|
return io_close(conn);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!check_proof(neg, neg->dstate->base_log, pkt, neg->expected_id, &id))
|
|
|
|
return io_close(conn);
|
2016-04-11 09:01:43 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
plan = neg->cb(conn, neg->dstate, neg->iod, &id, neg->arg);
|
|
|
|
tal_free(neg);
|
|
|
|
return plan;
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *recv_header_negotiate(struct io_conn *conn,
|
|
|
|
struct key_negotiate *neg)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
size_t body_len;
|
|
|
|
struct io_data *iod = neg->iod;
|
|
|
|
|
|
|
|
if (!decrypt_header(neg->dstate->base_log, iod, &body_len))
|
|
|
|
return io_close(conn);
|
|
|
|
|
|
|
|
return io_read(conn, iod->in.cpkt->data, body_len, recv_body_negotiate,
|
|
|
|
neg);
|
|
|
|
}
|
2016-03-16 06:36:17 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *receive_proof(struct io_conn *conn,
|
|
|
|
struct key_negotiate *neg)
|
|
|
|
{
|
|
|
|
return io_read(conn, &neg->iod->hdr_in, sizeof(neg->iod->hdr_in),
|
|
|
|
recv_header_negotiate, neg);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Steals w onto the returned Pkt */
|
|
|
|
static Pkt *pkt_wrap(const tal_t *ctx, void *w, Pkt__PktCase pkt_case)
|
|
|
|
{
|
|
|
|
Pkt *pkt = tal(ctx, Pkt);
|
|
|
|
pkt__init(pkt);
|
|
|
|
pkt->pkt_case = pkt_case;
|
|
|
|
/* Union, so any will do */
|
|
|
|
pkt->error = tal_steal(pkt, w);
|
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Pkt *authenticate_pkt(const tal_t *ctx,
|
2016-07-01 03:49:28 +02:00
|
|
|
secp256k1_context *secpctx,
|
2016-01-21 21:11:48 +01:00
|
|
|
const struct pubkey *node_id,
|
|
|
|
const struct signature *sig)
|
|
|
|
{
|
|
|
|
Authenticate *auth = tal(ctx, Authenticate);
|
|
|
|
authenticate__init(auth);
|
2016-07-01 03:49:28 +02:00
|
|
|
auth->node_id = pubkey_to_proto(auth, secpctx, node_id);
|
2016-07-01 03:57:57 +02:00
|
|
|
auth->session_sig = signature_to_proto(auth, secpctx, sig);
|
2016-01-21 21:11:48 +01:00
|
|
|
return pkt_wrap(ctx, auth, PKT__PKT_AUTH);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *keys_exchanged(struct io_conn *conn,
|
|
|
|
struct key_negotiate *neg)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
u8 shared_secret[32];
|
|
|
|
struct pubkey sessionkey;
|
|
|
|
struct signature sig;
|
|
|
|
Pkt *auth;
|
2016-08-18 06:53:46 +02:00
|
|
|
size_t totlen;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!pubkey_from_der(neg->dstate->secpctx,
|
2016-01-21 21:11:48 +01:00
|
|
|
neg->their_sessionpubkey,
|
|
|
|
sizeof(neg->their_sessionpubkey),
|
|
|
|
&sessionkey)) {
|
|
|
|
/* FIXME: Dump key in this case. */
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(neg->dstate->base_log, "Bad sessionkey");
|
2016-01-21 21:11:48 +01:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Derive shared secret. */
|
2016-08-18 06:53:46 +02:00
|
|
|
if (!secp256k1_ecdh(neg->dstate->secpctx, shared_secret,
|
2016-01-21 21:11:48 +01:00
|
|
|
&sessionkey.pubkey, neg->seckey)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(neg->dstate->base_log, "Bad ECDH");
|
2016-01-21 21:11:48 +01:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Each side combines with their OWN session key to SENDING crypto. */
|
2016-08-18 06:53:46 +02:00
|
|
|
neg->iod = tal(neg, struct io_data);
|
|
|
|
setup_crypto(&neg->iod->in, shared_secret, neg->their_sessionpubkey);
|
|
|
|
setup_crypto(&neg->iod->out, shared_secret, neg->our_sessionpubkey);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-07-19 05:07:33 +02:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* `session_sig` is the signature of the SHA256 of SHA256 of the its
|
|
|
|
* own sessionpubkey, using the secret key corresponding to the
|
|
|
|
* sender's `node_id`.
|
|
|
|
*/
|
2016-08-18 06:53:46 +02:00
|
|
|
privkey_sign(neg->dstate, neg->their_sessionpubkey,
|
2016-01-21 21:11:48 +01:00
|
|
|
sizeof(neg->their_sessionpubkey), &sig);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
auth = authenticate_pkt(neg, neg->dstate->secpctx,
|
|
|
|
&neg->dstate->id, &sig);
|
|
|
|
|
|
|
|
neg->iod->out.cpkt = encrypt_pkt(neg->iod, auth, &totlen);
|
|
|
|
return io_write(conn, neg->iod->out.cpkt, totlen, receive_proof, neg);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
2016-03-15 07:40:42 +01:00
|
|
|
/* Read and ignore any extra bytes... */
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *discard_extra(struct io_conn *conn,
|
|
|
|
struct key_negotiate *neg)
|
2016-03-15 07:40:42 +01:00
|
|
|
{
|
|
|
|
size_t len = le32_to_cpu(neg->keylen);
|
|
|
|
|
|
|
|
/* BOLT#1: Additional fields MAY be added, and MUST be
|
|
|
|
* included in the `length` field. These MUST be ignored by
|
|
|
|
* implementations which do not understand them. */
|
|
|
|
if (len > sizeof(neg->their_sessionpubkey)) {
|
|
|
|
char *discard;
|
|
|
|
|
|
|
|
len -= sizeof(neg->their_sessionpubkey);
|
|
|
|
discard = tal_arr(neg, char, len);
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(neg->dstate->base_log,
|
|
|
|
"Ignoring %zu extra handshake bytes",
|
2016-04-11 09:01:43 +02:00
|
|
|
len);
|
2016-08-18 06:53:46 +02:00
|
|
|
return io_read(conn, discard, len, keys_exchanged, neg);
|
2016-03-15 07:40:42 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
return keys_exchanged(conn, neg);
|
2016-03-15 07:40:42 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
static struct io_plan *session_key_receive(struct io_conn *conn,
|
2016-08-18 06:53:46 +02:00
|
|
|
struct key_negotiate *neg)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
2016-03-15 07:40:42 +01:00
|
|
|
/* BOLT#1: The `length` field is the length after the field
|
|
|
|
itself, and MUST be 33 or greater. */
|
|
|
|
if (le32_to_cpu(neg->keylen) < sizeof(neg->their_sessionpubkey)) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(neg->dstate->base_log, "short session key length %u",
|
2016-03-15 07:40:42 +01:00
|
|
|
le32_to_cpu(neg->keylen));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT#1: `length` MUST NOT exceed 1MB (1048576 bytes). */
|
|
|
|
if (le32_to_cpu(neg->keylen) > 1048576) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_unusual(neg->dstate->base_log,
|
|
|
|
"Oversize session key length %u",
|
2016-03-15 07:40:42 +01:00
|
|
|
le32_to_cpu(neg->keylen));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
log_debug(neg->dstate->base_log,
|
|
|
|
"Session key length %u", le32_to_cpu(neg->keylen));
|
2016-03-15 07:40:42 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Now read their key. */
|
|
|
|
return io_read(conn, neg->their_sessionpubkey,
|
2016-08-18 06:53:46 +02:00
|
|
|
sizeof(neg->their_sessionpubkey), discard_extra, neg);
|
2016-03-15 07:40:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *session_key_len_receive(struct io_conn *conn,
|
2016-08-18 06:53:46 +02:00
|
|
|
struct key_negotiate *neg)
|
2016-03-15 07:40:42 +01:00
|
|
|
{
|
|
|
|
/* Read the amount of data they will send.. */
|
|
|
|
return io_read(conn, &neg->keylen, sizeof(neg->keylen),
|
2016-08-18 06:53:46 +02:00
|
|
|
session_key_receive, neg);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gen_sessionkey(secp256k1_context *ctx,
|
|
|
|
u8 seckey[32],
|
|
|
|
secp256k1_pubkey *pubkey)
|
|
|
|
{
|
|
|
|
do {
|
2016-03-16 06:36:17 +01:00
|
|
|
randombytes_buf(seckey, 32);
|
2016-01-21 21:11:48 +01:00
|
|
|
} while (!secp256k1_ec_pubkey_create(ctx, pubkey, seckey));
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static struct io_plan *write_sessionkey(struct io_conn *conn,
|
|
|
|
struct key_negotiate *neg)
|
2016-03-15 07:40:42 +01:00
|
|
|
{
|
|
|
|
return io_write(conn, neg->our_sessionpubkey,
|
|
|
|
sizeof(neg->our_sessionpubkey),
|
2016-08-18 06:53:46 +02:00
|
|
|
session_key_len_receive, neg);
|
2016-03-15 07:40:42 +01:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
struct io_plan *peer_crypto_setup_(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
const struct pubkey *id,
|
|
|
|
struct io_plan *(*cb)(struct io_conn *conn,
|
|
|
|
struct lightningd_state *dstate,
|
|
|
|
struct io_data *iod,
|
|
|
|
const struct pubkey *id,
|
|
|
|
void *arg),
|
|
|
|
void *arg)
|
2016-01-21 21:11:48 +01:00
|
|
|
{
|
|
|
|
size_t outputlen;
|
|
|
|
secp256k1_pubkey sessionkey;
|
|
|
|
struct key_negotiate *neg;
|
|
|
|
|
2016-04-11 09:01:43 +02:00
|
|
|
/* BOLT #1:
|
|
|
|
*
|
|
|
|
* The 4-byte length for each message is encrypted separately
|
|
|
|
* (resulting in a 20 byte header when the authentication tag
|
|
|
|
* is appended) */
|
|
|
|
BUILD_ASSERT(sizeof(struct crypto_pkt) == 20);
|
2016-03-16 06:36:17 +01:00
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* We store negotiation state here. */
|
2016-08-18 06:53:46 +02:00
|
|
|
neg = tal(dstate, struct key_negotiate);
|
2016-01-21 21:11:48 +01:00
|
|
|
neg->cb = cb;
|
2016-08-18 06:53:46 +02:00
|
|
|
neg->arg = arg;
|
|
|
|
neg->dstate = dstate;
|
|
|
|
neg->expected_id = id;
|
|
|
|
/* FIXME: Create log buffer for neg, use that then pass to peer. */
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
gen_sessionkey(dstate->secpctx, neg->seckey, &sessionkey);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-07-01 03:57:57 +02:00
|
|
|
outputlen = sizeof(neg->our_sessionpubkey);
|
2016-08-18 06:53:46 +02:00
|
|
|
secp256k1_ec_pubkey_serialize(dstate->secpctx,
|
2016-01-21 21:11:48 +01:00
|
|
|
neg->our_sessionpubkey, &outputlen,
|
|
|
|
&sessionkey,
|
|
|
|
SECP256K1_EC_COMPRESSED);
|
|
|
|
assert(outputlen == sizeof(neg->our_sessionpubkey));
|
2016-03-15 07:40:42 +01:00
|
|
|
neg->keylen = cpu_to_le32(sizeof(neg->our_sessionpubkey));
|
|
|
|
return io_write(conn, &neg->keylen, sizeof(neg->keylen),
|
2016-08-18 06:53:46 +02:00
|
|
|
write_sessionkey, neg);
|
2016-01-21 21:11:48 +01:00
|
|
|
}
|