forked from cory/tildefriends
No more secrets in ~/.config, and speed up some tests.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4002 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
146
src/ssb.c
146
src/ssb.c
@ -39,8 +39,6 @@ const uint8_t k_ssb_network[] = {
|
||||
0x08, 0x39, 0xb7, 0x55, 0x84, 0x5a, 0x9f, 0xfb
|
||||
};
|
||||
|
||||
const char* k_secrets_path = "/.config/tildefriends/secret";
|
||||
|
||||
typedef enum {
|
||||
k_tf_ssb_state_invalid,
|
||||
k_tf_ssb_state_connected,
|
||||
@ -138,8 +136,6 @@ typedef struct _tf_ssb_t
|
||||
sqlite3* db;
|
||||
bool owns_db;
|
||||
|
||||
const char* secrets_path;
|
||||
|
||||
uv_loop_t own_loop;
|
||||
uv_loop_t* loop;
|
||||
uv_udp_t broadcast_listener;
|
||||
@ -1367,7 +1363,6 @@ void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* rea
|
||||
}
|
||||
while (connection->requests)
|
||||
{
|
||||
printf("%d %d\n", connection->requests_count, connection->requests->request_number);
|
||||
_tf_ssb_connection_remove_request(connection, connection->requests->request_number);
|
||||
}
|
||||
if (!JS_IsUndefined(connection->object))
|
||||
@ -1551,139 +1546,22 @@ static void _tf_ssb_connection_on_connect(uv_connect_t* connect, int status)
|
||||
}
|
||||
}
|
||||
|
||||
static bool _tf_ssb_load_keys(tf_ssb_t* ssb)
|
||||
static void _load_keys_callback(const char* identity, void* user_data)
|
||||
{
|
||||
const char* home = getenv("HOME");
|
||||
if (!home)
|
||||
tf_ssb_t* ssb = user_data;
|
||||
if (*ssb->pub)
|
||||
{
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
bool result = false;
|
||||
char* json = NULL;
|
||||
size_t path_size = strlen(home) + strlen(ssb->secrets_path) + 1;
|
||||
char* path = tf_malloc(path_size);
|
||||
snprintf(path, path_size, "%s%s", home, ssb->secrets_path);
|
||||
|
||||
FILE* file = fopen(path, "rb");
|
||||
if (!file)
|
||||
{
|
||||
printf("Failed to open %s: %s.\n", path, strerror(errno));
|
||||
goto failed;
|
||||
}
|
||||
|
||||
if (fseek(file, 0, SEEK_END) != 0)
|
||||
{
|
||||
printf("Failed to seek %s: %s\n.", path, strerror(errno));
|
||||
goto failed;
|
||||
}
|
||||
long len = ftell(file);
|
||||
if (len < 0 ||
|
||||
fseek(file, 0, SEEK_SET) != 0)
|
||||
{
|
||||
printf("Failed to seek %s: %s\n.", path, strerror(errno));
|
||||
goto failed;
|
||||
}
|
||||
|
||||
json = tf_malloc(len + 1);
|
||||
if (fread(json, 1, len, file) != (size_t)len)
|
||||
{
|
||||
printf("Failed to read %s: %s\n.", path, strerror(errno));
|
||||
goto failed;
|
||||
}
|
||||
|
||||
json[len] = '\0';
|
||||
|
||||
JSContext* context = ssb->context;
|
||||
JSValue root = JS_ParseJSON(context, (const char*)json, len, NULL);
|
||||
|
||||
JSValue pubvalue = JS_GetPropertyStr(context, root, "public");
|
||||
size_t pubstrlen = 0;
|
||||
const char* pubstr = JS_ToCStringLen(context, &pubstrlen, pubvalue);
|
||||
size_t privstrlen = 0;
|
||||
JSValue privvalue = JS_GetPropertyStr(context, root, "private");
|
||||
const char* privstr = JS_ToCStringLen(context, &privstrlen, privvalue);
|
||||
|
||||
if (pubstr && privstr)
|
||||
{
|
||||
result =
|
||||
base64c_decode((const uint8_t*)pubstr, pubstrlen - strlen(".ed25519"), ssb->pub, sizeof(ssb->pub)) != 0 &&
|
||||
base64c_decode((const uint8_t*)privstr, privstrlen - strlen(".ed25519"), ssb->priv, sizeof(ssb->priv)) != 0;
|
||||
}
|
||||
|
||||
JS_FreeCString(context, pubstr);
|
||||
JS_FreeCString(context, privstr);
|
||||
|
||||
JS_FreeValue(context, pubvalue);
|
||||
JS_FreeValue(context, privvalue);
|
||||
|
||||
JS_FreeValue(context, root);
|
||||
|
||||
failed:
|
||||
if (json)
|
||||
{
|
||||
tf_free(json);
|
||||
}
|
||||
if (file)
|
||||
{
|
||||
fclose(file);
|
||||
}
|
||||
if (path)
|
||||
{
|
||||
tf_free(path);
|
||||
}
|
||||
return result;
|
||||
tf_ssb_id_str_to_bin(ssb->pub, identity);
|
||||
tf_ssb_db_identity_get_private_key(ssb, ":admin", identity, ssb->priv, sizeof(ssb->priv));
|
||||
}
|
||||
|
||||
static bool _tf_ssb_save_keys(tf_ssb_t* ssb)
|
||||
static bool _tf_ssb_load_keys(tf_ssb_t* ssb)
|
||||
{
|
||||
bool result = false;
|
||||
char private_base64[crypto_sign_SECRETKEYBYTES * 2];
|
||||
char public_base64[crypto_sign_PUBLICKEYBYTES * 2];
|
||||
char private[crypto_sign_SECRETKEYBYTES * 2 + 16];
|
||||
char public[crypto_sign_PUBLICKEYBYTES * 2 + 16];
|
||||
char id[crypto_sign_PUBLICKEYBYTES * 2 + 16];
|
||||
base64c_encode(ssb->pub, sizeof(ssb->pub), (uint8_t*)public_base64, sizeof(public_base64));
|
||||
base64c_encode(ssb->priv, sizeof(ssb->priv), (uint8_t*)private_base64, sizeof(private_base64));
|
||||
|
||||
snprintf(private, sizeof(private), "%s.ed25519", private_base64);
|
||||
snprintf(public, sizeof(public), "%s.ed25519", public_base64);
|
||||
snprintf(id, sizeof(id), "@%s.ed25519", public_base64);
|
||||
|
||||
const char* home = getenv("HOME");
|
||||
if (!home)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t path_size = strlen(home) + strlen(ssb->secrets_path) + 1;
|
||||
char* path = tf_malloc(path_size);
|
||||
snprintf(path, path_size, "%s%s", home, ssb->secrets_path);
|
||||
|
||||
JSContext* context = ssb->context;
|
||||
JSValue root = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, root, "curve", JS_NewString(context, "ed25519"));
|
||||
JS_SetPropertyStr(context, root, "public", JS_NewString(context, public));
|
||||
JS_SetPropertyStr(context, root, "private", JS_NewString(context, private));
|
||||
JS_SetPropertyStr(context, root, "id", JS_NewString(context, id));
|
||||
|
||||
JSValue jsonval = JS_JSONStringify(context, root, JS_NULL, JS_NewInt32(context, 2));
|
||||
size_t len = 0;
|
||||
const char* json = JS_ToCStringLen(context, &len, jsonval);
|
||||
|
||||
FILE* file = fopen(path, "wb");
|
||||
if (file)
|
||||
{
|
||||
result = fwrite(json, 1, len, file) == len;
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
JS_FreeCString(context, json);
|
||||
JS_FreeValue(context, jsonval);
|
||||
JS_FreeValue(context, root);
|
||||
|
||||
tf_free(path);
|
||||
return result;
|
||||
tf_ssb_db_identity_visit(ssb, ":admin", _load_keys_callback, ssb);
|
||||
return *ssb->pub != '\0' && *ssb->priv != '\0';
|
||||
}
|
||||
|
||||
static void _tf_ssb_trace_timer(uv_timer_t* timer)
|
||||
@ -1738,11 +1616,10 @@ void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats)
|
||||
ssb->rpc_out = 0;
|
||||
}
|
||||
|
||||
tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db, const char* secrets_path)
|
||||
tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db)
|
||||
{
|
||||
tf_ssb_t* ssb = tf_malloc(sizeof(tf_ssb_t));
|
||||
memset(ssb, 0, sizeof(*ssb));
|
||||
ssb->secrets_path = secrets_path ? secrets_path : k_secrets_path;
|
||||
if (context)
|
||||
{
|
||||
ssb->context = context;
|
||||
@ -1805,8 +1682,7 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db, const
|
||||
if (!_tf_ssb_load_keys(ssb))
|
||||
{
|
||||
printf("Generating a new keypair.\n");
|
||||
tf_ssb_generate_keys(ssb);
|
||||
_tf_ssb_save_keys(ssb);
|
||||
tf_ssb_db_identity_create(ssb, ":admin", ssb->pub, ssb->priv);
|
||||
}
|
||||
|
||||
ssb->connections_tracker = tf_ssb_connections_create(ssb);
|
||||
|
Reference in New Issue
Block a user