Commit b798460d authored by Maarten de Waard's avatar Maarten de Waard 🤘🏻
Browse files

Merge branch '6-delete-luks-key-endpoint' into 'master'

Add endpoint for deleting encryption key

Closes #6

See merge request !15
parents babda843 8de743ac
/**
* Check if the given keyslot is the only active one.
* @param[in] cd crypto context
* @param[in] keyslot index of encryption key
* @param[out] only_active function result: whether the given keyslot is
the only active one
* @return error string to return to client; or null
when the function was successful.
*/
char * keyslot_only_active(struct crypt_device * cd, const int keyslot,
bool * only_active)
{
int i;
int keyslot_max = crypt_keyslot_max(CRYPT_LUKS1);
if (keyslot_max < 0)
{
return "crypt_keyslot_max failed";
}
// Go through all keyslots.
for (i = 0; i < keyslot_max; i++)
{
// If it's not the given one, check if it's active.
crypt_keyslot_info s = crypt_keyslot_status(cd, i);
if (s == CRYPT_SLOT_INVALID)
{
char * error;
asprintf(&error, "crypt_keyslot_info: invalid keyslot %d", i);
return error;
}
if (i != keyslot && (s == CRYPT_SLOT_ACTIVE || s == CRYPT_SLOT_ACTIVE_LAST))
{
// We found a keyslot other than the given one that's active.
*only_active = false;
return NULL;
}
}
// None of the other keyslots is active.
*only_active = true;
return NULL;
}
/**
* Callback function to delete a luks encryption password.
* The keyslot to delete is read from the request URI;
* no request body is necessary.
* @param[in] request incoming HTTP request
* @param[out] response HTTP response to the request
* @param[in] user_data extra data to pass between main thread and callbacks
* @return internal status code
*/
int callback_encryption_keys_delete(const struct _u_request * request,
struct _u_response * response, void * user_data)
{
int r;
// Read keyslot from request URI.
const char * keyslot_string = u_map_get(request->map_url, "slot");
if (keyslot_string == NULL)
{
return send_simple_response(response, 400, "error",
"missing url parameter `slot`");
}
int keyslot;
r = parse_int(keyslot_string, &keyslot);
if (r != 0)
{
printf("invalid url parameter `slot`: %s\n", keyslot_string);
return send_simple_response(response, 400, "error",
"invalid url parameter `slot`");
}
// Initialise encrypted container.
struct crypt_device * cd = NULL;
r = container_initialise(&cd, DATA_PARTITION_DEVICE, true);
if (r < 0)
{
crypt_free(cd);
}
if (r != 0)
{
printf("container_initialise failed with status %d\n", r);
return send_simple_response(response, 500, "error",
"initialising encrypted container failed");
}
// Check if this is the only active keyslot.
bool only_active = true;
char * error = keyslot_only_active(cd, keyslot, &only_active);
if (error != NULL)
{
return send_simple_response(response, 500, "error", error);
}
if (only_active)
{
// The keyslot we're asked to delete is the only active one; deny this.
return send_simple_response(response, 500, "error",
"not deleting only active keyslot");
}
// Delete encryption password.
r = crypt_keyslot_destroy(cd, keyslot);
if (r < 0)
{
// Something else went wrong.
printf("crypt_keyslot_destroy failed with status %d\n", r);
return send_simple_response(response, 500, "error",
"error deleting encryption password");
}
// If we reach this point, apparently everything went well.
return send_simple_response(response, 200, "status", "ok");
}
......@@ -11,6 +11,7 @@
#include <api/encryption_remove_post.c>
#include <api/encryption_unlock_post.c>
#include <api/encryption_keys_get.c>
#include <api/encryption_keys_delete.c>
#include <api/encryption_keys_put_post.c>
#include <api/ssh_keys_get.c>
#include <api/ssh_keys_put.c>
......@@ -64,6 +65,9 @@ int main(int argc, char ** argv)
ulfius_add_endpoint_by_val(&instance, "PUT" , PREFIX,
"/encryption/keys/:slot",
0, &callback_encryption_keys_put, NULL);
ulfius_add_endpoint_by_val(&instance, "DELETE" , PREFIX,
"/encryption/keys/:slot",
0, &callback_encryption_keys_delete, NULL);
ulfius_add_endpoint_by_val(&instance, "GET" , PREFIX,
"/ssh/keys",
0, &callback_ssh_keys_get, NULL);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment