1 /* Asymmetric public-key cryptography key type
3 * See Documentation/security/asymmetric-keys.txt
5 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public Licence
10 * as published by the Free Software Foundation; either version
11 * 2 of the Licence, or (at your option) any later version.
13 #include <keys/asymmetric-subtype.h>
14 #include <linux/seq_file.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include "asymmetric_keys.h"
19 MODULE_LICENSE("GPL");
22 * Match asymmetric keys on (part of) their name
23 * We have some shorthand methods for matching keys. We allow:
25 * "<desc>" - request a key by description
26 * "id:<id>" - request a key matching the ID
27 * "<subtype>:<id>" - request a key of a subtype
29 static int asymmetric_key_match(const struct key *key, const void *description)
31 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
32 const char *spec = description;
37 if (!subtype || !spec || !*spec)
40 /* See if the full key description matches as is */
41 if (key->description && strcmp(key->description, description) == 0)
44 /* All tests from here on break the criterion description into a
45 * specifier, a colon and then an identifier.
47 id = strchr(spec, ':');
54 /* Anything after here requires a partial match on the ID string */
55 kid = asymmetric_key_id(key);
64 kid += kidlen - idlen;
65 if (strcasecmp(id, kid) != 0)
69 memcmp(spec, "id", 2) == 0)
72 if (speclen == subtype->name_len &&
73 memcmp(spec, subtype->name, speclen) == 0)
80 * Describe the asymmetric key
82 static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
84 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
85 const char *kid = asymmetric_key_id(key);
88 seq_puts(m, key->description);
92 subtype->describe(key, m);
100 seq_puts(m, kid + n - 8);
104 /* put something here to indicate the key's capabilities */
110 * Instantiate a asymmetric_key defined key. The key was preparsed, so we just
111 * have to transfer the data here.
113 static int asymmetric_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
119 * dispose of the data dangling from the corpse of a asymmetric key
121 static void asymmetric_key_destroy(struct key *key)
123 struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
125 subtype->destroy(key->payload.data);
126 module_put(subtype->owner);
127 key->type_data.p[0] = NULL;
129 kfree(key->type_data.p[1]);
130 key->type_data.p[1] = NULL;
133 struct key_type key_type_asymmetric = {
134 .name = "asymmetric",
135 .instantiate = asymmetric_key_instantiate,
136 .match = asymmetric_key_match,
137 .destroy = asymmetric_key_destroy,
138 .describe = asymmetric_key_describe,
140 EXPORT_SYMBOL_GPL(key_type_asymmetric);
145 static int __init asymmetric_key_init(void)
147 return register_key_type(&key_type_asymmetric);
150 static void __exit asymmetric_key_cleanup(void)
152 unregister_key_type(&key_type_asymmetric);
155 module_init(asymmetric_key_init);
156 module_exit(asymmetric_key_cleanup);