int serval_init_keyring(svl_crypto_ctx *ctx) { keyring_identity *new_ident; CHECK_ERR(ctx,"Invalid ctx"); CHECK_ERR(serval_open_keyring(ctx), "Failed to open keyring"); if (!ctx->sid[0]) { //create new sid // cycle through the keyring contexts until we find one with room for another identity for(int c = 0; c < (ctx->keyring_file)->context_count; c++) { // create new Serval identity new_ident = keyring_create_identity(ctx->keyring_file, (ctx->keyring_file)->contexts[c], KEYRING_PIN); if (new_ident) break; } CHECK_ERR(new_ident, "Failed to create new SID"); // need to commit keyring or else new identity won't be saved (needs permissions) CHECK_ERR(keyring_commit(ctx->keyring_file) == 0, "Failed to save new SID into keyring"); memcpy(ctx->sid,new_ident->subscriber->sid,SID_SIZE); } CHECK(serval_extract_sas(ctx), "Failed to fetch SAS keys"); return 1; error: return 0; }
int serval_init_keyring(unsigned char *sid, const size_t sid_len, const char *keyring_path, const size_t keyring_len, keyring_file **_keyring, unsigned char **key, int *key_len) { keyring_identity *new_ident; unsigned char *_sid = sid; if (sid) CHECK(sid_len == SID_SIZE,"Invalid SID"); CHECK_ERR(serval_open_keyring(keyring_path,keyring_len,_keyring),"Failed to open keyring"); if (!sid) { //create new sid int c; for(c = 0; c < (*_keyring)->context_count; c++) { // cycle through the keyring contexts until we find one with room for another identity new_ident = keyring_create_identity(*_keyring,(*_keyring)->contexts[c], KEYRING_PIN); // create new Serval identity if (new_ident) break; } CHECK_ERR(new_ident,"failed to create new SID"); CHECK_ERR(keyring_commit(*_keyring) == 0,"Failed to save new SID into keyring"); // need to commit keyring or else new identity won't be saved (needs permissions) _sid = new_ident->subscriber->sid; } if (key) CHECK(serval_extract_sas(key,key_len, *_keyring, _sid),"Failed to fetch SAS key"); return 1; error: return 0; }