summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2017-03-24 15:44:30 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2017-03-31 18:43:53 +0200
commit8f329292316dd38297bb0b95d9f10d7f3935a07b (patch)
treef8f1c17ac1050f1a096fa1f9af0b53b3c6aead25 /crypto
parent73f3dae29a761034459a41f6300dcf4665f78af5 (diff)
downloadbarebox-8f329292316dd38297bb0b95d9f10d7f3935a07b.tar.gz
barebox-8f329292316dd38297bb0b95d9f10d7f3935a07b.tar.xz
keystore: implement forgetting secrets
To be able to change secrets add a function to forget secrets. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/keystore.c53
1 files changed, 38 insertions, 15 deletions
diff --git a/crypto/keystore.c b/crypto/keystore.c
index 90b470fe67..f2b25ca6c9 100644
--- a/crypto/keystore.c
+++ b/crypto/keystore.c
@@ -16,8 +16,8 @@ static LIST_HEAD(keystore_list);
struct keystore_key {
struct list_head list;
- const char *name;
- const u8 *secret;
+ char *name;
+ u8 *secret;
int secret_len;
};
@@ -29,6 +29,17 @@ static int keystore_compare(struct list_head *a, struct list_head *b)
return strcmp(na, nb);
}
+static struct keystore_key *get_key(const char *name)
+{
+ struct keystore_key *key;
+
+ for_each_key(key)
+ if (!strcmp(name, key->name))
+ return key;
+
+ return NULL;
+};
+
/**
* @param[in] name Name of the secret to get
* @param[out] secret Double pointer to memory representing the secret, do _not_ free() after use
@@ -38,19 +49,17 @@ int keystore_get_secret(const char *name, const u8 **secret, int *secret_len)
{
struct keystore_key *key;
- for_each_key(key) {
- if (!strcmp(name, key->name)) {
- if (!secret || !secret_len)
- return 0;
+ if (!secret || !secret_len)
+ return 0;
- *secret = key->secret;
- *secret_len = key->secret_len;
+ key = get_key(name);
+ if (!key)
+ return -ENOENT;
- return 0;
- }
- }
+ *secret = key->secret;
+ *secret_len = key->secret_len;
- return -ENOENT;
+ return 0;
}
/**
@@ -61,11 +70,10 @@ int keystore_get_secret(const char *name, const u8 **secret, int *secret_len)
int keystore_set_secret(const char *name, const u8 *secret, int secret_len)
{
struct keystore_key *key;
- int ret;
/* check if key is already in store */
- ret = keystore_get_secret(name, NULL, NULL);
- if (!ret)
+ key = get_key(name);
+ if (key)
return -EBUSY;
key = xzalloc(sizeof(*key));
@@ -78,3 +86,18 @@ int keystore_set_secret(const char *name, const u8 *secret, int secret_len)
return 0;
}
+
+void keystore_forget_secret(const char *name)
+{
+ struct keystore_key *key;
+
+ key = get_key(name);
+ if (!key)
+ return;
+
+ list_del(&key->list);
+
+ free(key->name);
+ free(key->secret);
+ free(key);
+}