1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-11-16 01:10:10 +00:00

Added key exchange to LAIKA_HANDSHAKE_*

- test keys are defined in laika.h
- bug fixes & refactoring with laikaM_growarray()
This commit is contained in:
2022-01-27 19:55:28 -06:00
parent 203b5ce38f
commit a023929190
11 changed files with 129 additions and 15 deletions

View File

@@ -8,6 +8,7 @@
#include "lpeer.h"
struct sLaika_cnc {
uint8_t priv[crypto_box_SECRETKEYBYTES], pub[crypto_box_PUBLICKEYBYTES];
struct sLaika_socket sock;
struct sLaika_pollList pList;
};

View File

@@ -1,17 +1,21 @@
#include "lmem.h"
#include "lrsa.h"
#include "lsocket.h"
#include "lerror.h"
#include "cnc.h"
LAIKAPKT_SIZE laikaC_pktSizeTbl[LAIKAPKT_MAXNONE] = {
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t)
[LAIKAPKT_HANDSHAKE_REQ] = LAIKA_MAGICLEN + sizeof(uint8_t) + sizeof(uint8_t) + crypto_box_SEALBYTES + LAIKA_NONCESIZE + crypto_box_PUBLICKEYBYTES
};
void laikaC_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
switch (id) {
case LAIKAPKT_HANDSHAKE_REQ: {
char magicBuf[LAIKA_MAGICLEN];
uint8_t encNonce[crypto_box_SEALBYTES + LAIKA_NONCESIZE], nonce[LAIKA_NONCESIZE];
uint8_t major, minor;
laikaS_read(&peer->sock, (void*)magicBuf, LAIKA_MAGICLEN);
@@ -21,11 +25,24 @@ void laikaC_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
if (memcmp(magicBuf, LAIKA_MAGIC, LAIKA_MAGICLEN) != 0
|| major != LAIKA_VERSION_MAJOR
|| minor != LAIKA_VERSION_MINOR)
LAIKA_ERROR("invalid handshake request!");
LAIKA_ERROR("invalid handshake request!\n");
/* read & decrypt nonce */
laikaS_read(&peer->sock, encNonce, sizeof(encNonce));
if (crypto_box_seal_open(nonce, encNonce, sizeof(encNonce), cnc->pub, cnc->priv) != 0)
LAIKA_ERROR("Failed to decrypt nonce!\n");
/* read peer's public key */
laikaS_read(&peer->sock, peer->peerPub, sizeof(peer->peerPub));
/* encrypt decrypted nonce with peer's pub key */
if (crypto_box_seal(encNonce, nonce, sizeof(nonce), peer->peerPub) != 0)
LAIKA_ERROR("Failed to enc nonce!\n");
/* queue response */
laikaS_writeByte(&peer->sock, LAIKAPKT_HANDSHAKE_RES);
laikaS_writeByte(&peer->sock, laikaS_isBigEndian());
laikaS_write(&peer->sock, encNonce, sizeof(encNonce));
LAIKA_DEBUG("accepted handshake from peer %x\n", peer);
break;
@@ -35,6 +52,7 @@ void laikaC_pktHandler(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData) {
struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
struct sLaika_cnc *cnc = laikaM_malloc(sizeof(struct sLaika_cnc));
size_t _unused;
/* init socket & pollList */
laikaS_initSocket(&cnc->sock);
@@ -46,6 +64,21 @@ struct sLaika_cnc *laikaC_newCNC(uint16_t port) {
/* add sock to pollList */
laikaP_addSock(&cnc->pList, &cnc->sock);
if (sodium_init() < 0) {
laikaC_freeCNC(cnc);
LAIKA_ERROR("LibSodium failed to initialize!\n");
}
if (sodium_hex2bin(cnc->pub, crypto_box_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) {
laikaC_freeCNC(cnc);
LAIKA_ERROR("Failed to init cnc public key!\n");
}
if (sodium_hex2bin(cnc->priv, crypto_box_SECRETKEYBYTES, LAIKA_PRIVKEY, strlen(LAIKA_PRIVKEY), NULL, &_unused, NULL) != 0) {
laikaC_freeCNC(cnc);
LAIKA_ERROR("Failed to init cnc private key!\n");
}
return cnc;
}
@@ -82,6 +115,7 @@ bool laikaC_pollPeers(struct sLaika_cnc *cnc, int timeout) {
&cnc->pList,
(void*)cnc
);
laikaS_setKeys(peer, cnc->priv, cnc->pub);
/* setup and accept new peer */
laikaS_acceptFrom(&peer->sock, &cnc->sock);