|
|
|
@ -26,29 +26,108 @@
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
#include <Winsock2.h>
|
|
|
|
|
#define EBADE 1
|
|
|
|
|
#define NETDB_SUCCESS 0
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
#include <openssl/engine.h>
|
|
|
|
|
|
|
|
|
|
#include <sxt/errno.h>
|
|
|
|
|
#include <sxt/sxtkey.h>
|
|
|
|
|
|
|
|
|
|
int sxt_key_generate(sxtkey_t *key, int type, int opt)
|
|
|
|
|
{
|
|
|
|
|
int r = 0;
|
|
|
|
|
|
|
|
|
|
if(!key) return SXT_EINVAL;
|
|
|
|
|
|
|
|
|
|
/* set defaults */
|
|
|
|
|
key->type = type;
|
|
|
|
|
key->flags = SXT_PPKP_PRIVATE | SXT_PPKP_PUBLIC;
|
|
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
|
case PPKP_ED25519:
|
|
|
|
|
key->pubkey = malloc(sizeof(ed25519_pubkey));
|
|
|
|
|
if(!key->pubkey) {
|
|
|
|
|
r = SXT_ENOMEM;
|
|
|
|
|
goto __fall;
|
|
|
|
|
}
|
|
|
|
|
key->privkey = malloc(sizeof(ed25519_privkey));
|
|
|
|
|
if(!key->privkey) {
|
|
|
|
|
r = SXT_ENOMEM;
|
|
|
|
|
goto __safefall0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = crypto_sign_ed25519_keypair(*key->pubkey, *key->privkey);
|
|
|
|
|
if(r) {
|
|
|
|
|
r = SXT_ECRYPTO;
|
|
|
|
|
goto __safefall0;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return SXT_EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!r) return SXT_SUCCESS;
|
|
|
|
|
|
|
|
|
|
__safefall0:
|
|
|
|
|
if(key->pubkey) free(key->pubkey);
|
|
|
|
|
if(key->privkey) free(key->privkey);
|
|
|
|
|
|
|
|
|
|
__fall:
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sxtkey_t *sxt_key_alloc(void)
|
|
|
|
|
{
|
|
|
|
|
sxtkey_t *key = malloc(sizeof(sxtkey_t));
|
|
|
|
|
|
|
|
|
|
if(!key) return NULL;
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sxt_key_burn(sxtkey_t *key)
|
|
|
|
|
{
|
|
|
|
|
if(!key) return;
|
|
|
|
|
|
|
|
|
|
key->priv = NULL;
|
|
|
|
|
|
|
|
|
|
switch(key->type) {
|
|
|
|
|
case PPKP_ED25519:
|
|
|
|
|
if(key->pubkey) memset(key->pubkey, 0, sizeof(ed25519_pubkey));
|
|
|
|
|
if(key->privkey) memset(key->privkey, 0, sizeof(ed25519_privkey));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
key->type = 0;
|
|
|
|
|
key->flags = 0;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void sxt_key_free(sxtkey_t *key)
|
|
|
|
|
{
|
|
|
|
|
if(!key) return;
|
|
|
|
|
|
|
|
|
|
switch(key->type) {
|
|
|
|
|
case PPKP_ED25519:
|
|
|
|
|
if(key->pubkey) free(key->pubkey);
|
|
|
|
|
if(key->privkey) free(key->privkey);
|
|
|
|
|
break;
|
|
|
|
|
default: return; /* cannot free unrecognized key due to the
|
|
|
|
|
* potential memleak
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|