Пример #1
0
return_status master_keys_import(
		master_keys ** const keys,
		const Key * const public_signing_key,
		const Key * const private_signing_key,
		const Key * const public_identity_key,
		const Key * const private_identity_key) {
	return_status status = return_status_init();

	//check inputs
	if ((keys == NULL)
			|| (public_signing_key == NULL) || (private_signing_key == NULL)
			|| (public_identity_key == NULL) || (private_identity_key == NULL)) {
		throw(INVALID_INPUT, "Invalid input to master_keys_import.");
	}

	*keys = sodium_malloc(sizeof(master_keys));
	throw_on_failed_alloc(*keys);

	//initialize the buffers
	buffer_init_with_pointer((*keys)->public_signing_key, (*keys)->public_signing_key_storage, PUBLIC_MASTER_KEY_SIZE, 0);
	buffer_init_with_pointer((*keys)->private_signing_key, (*keys)->private_signing_key_storage, PRIVATE_MASTER_KEY_SIZE, 0);
	buffer_init_with_pointer((*keys)->public_identity_key, (*keys)->public_identity_key_storage, PUBLIC_KEY_SIZE, 0);
	buffer_init_with_pointer((*keys)->private_identity_key, (*keys)->private_identity_key_storage, PRIVATE_KEY_SIZE, 0);

	//copy the keys
	if (buffer_clone_from_raw((*keys)->public_signing_key, public_signing_key->key.data, public_signing_key->key.len) != 0) {
		throw(BUFFER_ERROR, "Failed to copy public signing key.");
	}
	if (buffer_clone_from_raw((*keys)->private_signing_key, private_signing_key->key.data, private_signing_key->key.len) != 0) {
		throw(BUFFER_ERROR, "Failed to copy private signing key.");
	}
	if (buffer_clone_from_raw((*keys)->public_identity_key, public_identity_key->key.data, public_identity_key->key.len) != 0) {
		throw(BUFFER_ERROR, "Failed to copy public identity key.");
	}
	if (buffer_clone_from_raw((*keys)->private_identity_key, private_identity_key->key.data, private_identity_key->key.len) != 0) {
		throw(BUFFER_ERROR, "Failed to copy private identity key.");
	}

	sodium_mprotect_noaccess(*keys);

cleanup:
	on_error {
		if (keys != NULL) {
			sodium_free_and_null_if_valid(*keys);
		}
	}

	return status;
}
Пример #2
0
/*
 * Copy a raw array to a buffer and return the
 * buffer.
 *
 * This should not be used directly, it is intended for the use
 * with the macro buffer_create_from_string.
 *
 * Returns NULL on error.
 */
buffer_t* buffer_create_from_string_helper(
		buffer_t * const buffer,
		const unsigned char * const content,
		const size_t content_length) {
	if (buffer->buffer_length < content_length) {
		return NULL;
	}

	if (buffer_clone_from_raw(buffer, content, content_length) != 0) {
		return NULL;
	}

	return buffer;
}
Пример #3
0
return_status user_store_node_import(user_store_node ** const node, const User * const user) {
	return_status status = return_status_init();

	//check input
	if ((node == NULL) || (user == NULL)) {
		throw(INVALID_INPUT, "Invalid input to user_store_node_import.");
	}

	status = create_user_store_node(node);
	throw_on_error(CREATION_ERROR, "Failed to create user store node.");

	//master keys
	status = master_keys_import(
		&((*node)->master_keys),
		user->public_signing_key,
		user->private_signing_key,
		user->public_identity_key,
		user->private_identity_key);
	throw_on_error(IMPORT_ERROR, "Failed to import master keys.");

	//public signing key
	if (user->public_signing_key == NULL) {
		throw(PROTOBUF_MISSING_ERROR, "Missing public signing key in Protobuf-C struct.");
	}
	if (buffer_clone_from_raw((*node)->public_signing_key, user->public_signing_key->key.data, user->public_signing_key->key.len) != 0) {
		throw(BUFFER_ERROR, "Failed to copy public signing key.");
	}

	status = conversation_store_import(
		(*node)->conversations,
		user->conversations,
		user->n_conversations);
	throw_on_error(IMPORT_ERROR, "Failed to import conversations.");

	status = prekey_store_import(
		&((*node)->prekeys),
		user->prekeys,
		user->n_prekeys,
		user->deprecated_prekeys,
		user->n_deprecated_prekeys);
	throw_on_error(IMPORT_ERROR, "Failed to import prekeys.");

cleanup:
	return status;
}