mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 01:32:34 +01:00
daemon: primitive privkey handling.
Eventually this will be in a separate process, etc. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
29c8611e53
commit
9449f387ac
5 changed files with 117 additions and 1 deletions
|
@ -19,6 +19,7 @@ DAEMON_SRC := \
|
|||
daemon/lightningd.c \
|
||||
daemon/netaddr.c \
|
||||
daemon/peer.c \
|
||||
daemon/secrets.c \
|
||||
daemon/timeout.c
|
||||
DAEMON_OBJS := $(DAEMON_SRC:.c=.o)
|
||||
|
||||
|
@ -38,6 +39,7 @@ DAEMON_HEADERS := \
|
|||
daemon/netaddr.h \
|
||||
daemon/peer.h \
|
||||
daemon/pseudorand.h \
|
||||
daemon/secrets.h \
|
||||
daemon/timeout.h
|
||||
|
||||
$(DAEMON_OBJS) $(DAEMON_LIB_OBJS) $(DAEMON_CLI_OBJS): $(DAEMON_HEADERS) $(DAEMON_JSMN_HEADERS) $(BITCOIN_HEADERS) $(CORE_HEADERS) $(GEN_HEADERS) $(CCAN_HEADERS)
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "lightningd.h"
|
||||
#include "log.h"
|
||||
#include "peer.h"
|
||||
#include "secrets.h"
|
||||
#include "timeout.h"
|
||||
#include <ccan/container_of/container_of.h>
|
||||
#include <ccan/err/err.h>
|
||||
|
@ -104,7 +105,10 @@ int main(int argc, char *argv[])
|
|||
|
||||
/* Set up connections from peers. */
|
||||
setup_listeners(state, portnum);
|
||||
|
||||
|
||||
/* Set up node ID and private key. */
|
||||
secrets_init(state);
|
||||
|
||||
log_info(state->base_log, "Hello world!");
|
||||
|
||||
/* If io_loop returns NULL, either a timer expired, or all fds closed */
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef LIGHTNING_DAEMON_LIGHTNING_H
|
||||
#define LIGHTNING_DAEMON_LIGHTNING_H
|
||||
#include "config.h"
|
||||
#include "bitcoin/pubkey.h"
|
||||
#include <ccan/list/list.h>
|
||||
#include <ccan/timer/timer.h>
|
||||
#include <secp256k1.h>
|
||||
|
@ -25,5 +26,11 @@ struct lightningd_state {
|
|||
|
||||
/* Crypto tables for global use. */
|
||||
secp256k1_context *secpctx;
|
||||
|
||||
/* Our private key */
|
||||
struct secret *secret;
|
||||
|
||||
/* This is us. */
|
||||
struct pubkey id;
|
||||
};
|
||||
#endif /* LIGHTNING_DAEMON_LIGHTNING_H */
|
||||
|
|
88
daemon/secrets.c
Normal file
88
daemon/secrets.c
Normal file
|
@ -0,0 +1,88 @@
|
|||
#include "bitcoin/privkey.h"
|
||||
#include "bitcoin/shadouble.h"
|
||||
#include "bitcoin/signature.h"
|
||||
#include "lightningd.h"
|
||||
#include "log.h"
|
||||
#include "peer.h"
|
||||
#include "secrets.h"
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/noerr/noerr.h>
|
||||
#include <ccan/read_write_all/read_write_all.h>
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <secp256k1.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct secret {
|
||||
/* Secret ID of our node; public is state->id. */
|
||||
struct privkey privkey;
|
||||
};
|
||||
|
||||
void privkey_sign(struct peer *peer, const void *src, size_t len,
|
||||
struct signature *sig)
|
||||
{
|
||||
struct sha256_double h;
|
||||
|
||||
sha256_double(&h, memcheck(src, len), len);
|
||||
sign_hash(peer->state->secpctx,
|
||||
&peer->state->secret->privkey, &h, sig);
|
||||
}
|
||||
|
||||
void secrets_init(struct lightningd_state *state)
|
||||
{
|
||||
int fd;
|
||||
|
||||
state->secret = tal(state, struct secret);
|
||||
|
||||
fd = open("privkey", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
if (errno != ENOENT)
|
||||
fatal("Failed to open privkey: %s", strerror(errno));
|
||||
|
||||
log_unusual(state->base_log, "Creating privkey file");
|
||||
do {
|
||||
if (RAND_bytes(state->secret->privkey.secret,
|
||||
sizeof(state->secret->privkey.secret))
|
||||
!= 1)
|
||||
fatal("Could not get random bytes for privkey");
|
||||
} while (!pubkey_from_privkey(state->secpctx,
|
||||
&state->secret->privkey,
|
||||
&state->id,
|
||||
SECP256K1_EC_COMPRESSED));
|
||||
|
||||
fd = open("privkey", O_CREAT|O_EXCL|O_WRONLY, 0400);
|
||||
if (fd < 0)
|
||||
fatal("Failed to create privkey file: %s",
|
||||
strerror(errno));
|
||||
if (!write_all(fd, state->secret->privkey.secret,
|
||||
sizeof(state->secret->privkey.secret))) {
|
||||
unlink_noerr("privkey");
|
||||
fatal("Failed to write to privkey file: %s",
|
||||
strerror(errno));
|
||||
}
|
||||
if (fsync(fd) != 0)
|
||||
fatal("Failed to sync to privkey file: %s",
|
||||
strerror(errno));
|
||||
close(fd);
|
||||
|
||||
fd = open("privkey", O_RDONLY);
|
||||
if (fd < 0)
|
||||
fatal("Failed to reopen privkey: %s", strerror(errno));
|
||||
}
|
||||
if (!read_all(fd, state->secret->privkey.secret,
|
||||
sizeof(state->secret->privkey.secret)))
|
||||
fatal("Failed to read privkey: %s", strerror(errno));
|
||||
close(fd);
|
||||
if (!pubkey_from_privkey(state->secpctx,
|
||||
&state->secret->privkey, &state->id,
|
||||
SECP256K1_EC_COMPRESSED))
|
||||
fatal("Invalid privkey");
|
||||
|
||||
log_info(state->base_log, "ID: ");
|
||||
log_add_hex(state->base_log, state->id.der, pubkey_derlen(&state->id));
|
||||
}
|
15
daemon/secrets.h
Normal file
15
daemon/secrets.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef LIGHTNING_DAEMON_SECRETS_H
|
||||
#define LIGHTNING_DAEMON_SECRETS_H
|
||||
/* Routines to handle private keys. */
|
||||
#include "config.h"
|
||||
|
||||
struct peer;
|
||||
struct lightningd_state;
|
||||
struct signature;
|
||||
|
||||
void privkey_sign(struct peer *peer, const void *src, size_t len,
|
||||
struct signature *sig);
|
||||
|
||||
void secrets_init(struct lightningd_state *state);
|
||||
|
||||
#endif /* LIGHTNING_DAEMON_SECRETS_H */
|
Loading…
Add table
Reference in a new issue