|
|
|
@ -52,8 +52,14 @@
|
|
|
|
|
|
|
|
|
|
#include <tdata/list.h>
|
|
|
|
|
|
|
|
|
|
#include <sxt/errno.h>
|
|
|
|
|
#include <sxt/ciphers.h>
|
|
|
|
|
#include <sxt/lcrypt.h>
|
|
|
|
|
#include <sxt/safebuf.h>
|
|
|
|
|
#include <sxt/rdb.h>
|
|
|
|
|
#include <sxt/sxtkey.h>
|
|
|
|
|
#include <sxt/socket.h>
|
|
|
|
|
#include <sxt/sxt.h>
|
|
|
|
|
|
|
|
|
|
int sxt_init(void)
|
|
|
|
|
{
|
|
|
|
@ -91,6 +97,119 @@ int sxt_get_random(void *data, int len, int pseudo)
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sxt_genbeer(sxtsession_t *session, sxtsafebuffer_t *o)
|
|
|
|
|
{
|
|
|
|
|
char *beer;
|
|
|
|
|
|
|
|
|
|
if(!o || !session) return SXT_EINVAL;
|
|
|
|
|
if(!session->linkset) return SXT_EINVAL;
|
|
|
|
|
|
|
|
|
|
if(!(beer = malloc(128))) return SXT_ENOMEM;
|
|
|
|
|
else memset(beer, 0, 128);
|
|
|
|
|
|
|
|
|
|
snprintf(beer, 128, "sxtbeer-%s-%d-%lu-%s", SXTBEER_TYPE, SXTPROTO_VERSION,
|
|
|
|
|
session->linkset->hash,
|
|
|
|
|
session->linkset->strctx ? session->linkset->strctx : "nil");
|
|
|
|
|
|
|
|
|
|
sxtsafebuffer_setdata(o, beer, strlen(beer));
|
|
|
|
|
|
|
|
|
|
return SXT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sxt_scanbeer(sxtsession_t *session, const char *rbuf, size_t rlen)
|
|
|
|
|
{
|
|
|
|
|
char *buf = (char *)rbuf, *tbuf;
|
|
|
|
|
char tuple[32];
|
|
|
|
|
uint64_t hash = 0;
|
|
|
|
|
int i, st, version, len = 0;
|
|
|
|
|
|
|
|
|
|
if(!session || !rbuf) return SXT_EINVAL;
|
|
|
|
|
if(!rlen || !session->peerbeer) return SXT_EINVAL;
|
|
|
|
|
|
|
|
|
|
memset(tuple, 0, 32);
|
|
|
|
|
|
|
|
|
|
for(i = 0, tbuf = NULL, st = 0; i < rlen; i++, buf++) {
|
|
|
|
|
switch(st) {
|
|
|
|
|
case 0: /* first tuple */
|
|
|
|
|
if(!tbuf) tbuf = buf;
|
|
|
|
|
if(*buf == '-') {
|
|
|
|
|
if(i != strlen("sxtbeer")) return SXT_EPROTO;
|
|
|
|
|
if(strncmp(tbuf, "sxtbeer", i)) return SXT_EPROTO; /* that's wine! */
|
|
|
|
|
st++;
|
|
|
|
|
tbuf = NULL;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
if(!tbuf) tbuf = buf;
|
|
|
|
|
if(*buf == '-') {
|
|
|
|
|
len = strlen(tbuf) - strlen(buf);
|
|
|
|
|
if(len < 1 || len >= 32) return SXT_EPROTO;
|
|
|
|
|
memcpy(tuple, tbuf, len);
|
|
|
|
|
/* currently one type supported */
|
|
|
|
|
if(strcmp(tuple, SXTBEER_TYPE)) return SXT_EPROTO;
|
|
|
|
|
|
|
|
|
|
memset(tuple, 0, 32);
|
|
|
|
|
tbuf = NULL; st++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
if(!tbuf) tbuf = buf;
|
|
|
|
|
if(*buf == '-') {
|
|
|
|
|
len = strlen(tbuf) - strlen(buf);
|
|
|
|
|
if(len < 1 || len >= 32) return SXT_EPROTO;
|
|
|
|
|
memcpy(tuple, tbuf, len);
|
|
|
|
|
|
|
|
|
|
version = atoi(tuple);
|
|
|
|
|
/* first version supported only */
|
|
|
|
|
if(version != SXTPROTO_VERSION) return SXT_EPROTO;
|
|
|
|
|
|
|
|
|
|
memset(tuple, 0, 32);
|
|
|
|
|
tbuf = NULL; st++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3: /* hash value */
|
|
|
|
|
if(!tbuf) tbuf = buf;
|
|
|
|
|
if(*buf == '-') {
|
|
|
|
|
len = strlen(tbuf) - strlen(buf);
|
|
|
|
|
if(len < 1 || len >= 32) return SXT_EPROTO;
|
|
|
|
|
memcpy(tuple, tbuf, len);
|
|
|
|
|
|
|
|
|
|
hash = strtoul(tuple, NULL, 0);
|
|
|
|
|
memset(tuple, 0, 32);
|
|
|
|
|
tbuf = NULL; st++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 4: /* the rest of buffer is some custom plate */
|
|
|
|
|
if(!tbuf) {
|
|
|
|
|
tbuf = buf;
|
|
|
|
|
len = i;
|
|
|
|
|
memset(tuple, 0, 32);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ok check for validity */
|
|
|
|
|
if(st < 3) return SXT_EPROTO;
|
|
|
|
|
if(!tbuf) return SXT_EPROTO;
|
|
|
|
|
else len = rlen - len;
|
|
|
|
|
|
|
|
|
|
if(len >= 32 || len < 1) return SXT_EPROTO;
|
|
|
|
|
else memcpy(tuple, tbuf, len);
|
|
|
|
|
|
|
|
|
|
session->sxt_version = version;
|
|
|
|
|
session->peer_swhash = hash;
|
|
|
|
|
|
|
|
|
|
if(sxtsafebuffer_length(session->peerbeer) < 128)
|
|
|
|
|
return SXT_ENOMEM; /* no room for peer beer */
|
|
|
|
|
|
|
|
|
|
snprintf((char *)sxtsafebuffer_getdata(session->peerbeer), 128,
|
|
|
|
|
"sxtbeer-%s-%d-%lu-%s", SXTBEER_TYPE, session->sxt_version,
|
|
|
|
|
session->peer_swhash, tuple);
|
|
|
|
|
|
|
|
|
|
return SXT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* wrappers */
|
|
|
|
|
SHA512CTX sha512_init(void){
|
|
|
|
|
SHA512CTX c = malloc(sizeof(*c));
|
|
|
|
|