aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/asymmetric_keys
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2012-09-13 10:17:21 -0400
committerRusty Russell <rusty@rustcorp.com.au>2012-10-07 23:20:12 -0400
commit964f3b3bf49eb84b5831121446a28b76de3fb23a (patch)
tree7d299f690c8323c931b59bc142c45ac75097963f /crypto/asymmetric_keys
parent9a83b46578df149160b1da057656d2f0cfcbb5b6 (diff)
KEYS: Implement asymmetric key type
Create a key type that can be used to represent an asymmetric key type for use in appropriate cryptographic operations, such as encryption, decryption, signature generation and signature verification. The key type is "asymmetric" and can provide access to a variety of cryptographic algorithms. Possibly, this would be better as "public_key" - but that has the disadvantage that "public key" is an overloaded term. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Diffstat (limited to 'crypto/asymmetric_keys')
-rw-r--r--crypto/asymmetric_keys/Kconfig13
-rw-r--r--crypto/asymmetric_keys/Makefile7
-rw-r--r--crypto/asymmetric_keys/asymmetric_keys.h15
-rw-r--r--crypto/asymmetric_keys/asymmetric_type.c156
4 files changed, 191 insertions, 0 deletions
diff --git a/crypto/asymmetric_keys/Kconfig b/crypto/asymmetric_keys/Kconfig
new file mode 100644
index 000000000000..cad29b3efa0e
--- /dev/null
+++ b/crypto/asymmetric_keys/Kconfig
@@ -0,0 +1,13 @@
1menuconfig ASYMMETRIC_KEY_TYPE
2 tristate "Asymmetric (public-key cryptographic) key type"
3 depends on KEYS
4 help
5 This option provides support for a key type that holds the data for
6 the asymmetric keys used for public key cryptographic operations such
7 as encryption, decryption, signature generation and signature
8 verification.
9
10if ASYMMETRIC_KEY_TYPE
11
12
13endif # ASYMMETRIC_KEY_TYPE
diff --git a/crypto/asymmetric_keys/Makefile b/crypto/asymmetric_keys/Makefile
new file mode 100644
index 000000000000..b725bcce4cfd
--- /dev/null
+++ b/crypto/asymmetric_keys/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for asymmetric cryptographic keys
3#
4
5obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o
6
7asymmetric_keys-y := asymmetric_type.o
diff --git a/crypto/asymmetric_keys/asymmetric_keys.h b/crypto/asymmetric_keys/asymmetric_keys.h
new file mode 100644
index 000000000000..515b63430812
--- /dev/null
+++ b/crypto/asymmetric_keys/asymmetric_keys.h
@@ -0,0 +1,15 @@
1/* Internal definitions for asymmetric key type
2 *
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12static inline const char *asymmetric_key_id(const struct key *key)
13{
14 return key->type_data.p[1];
15}
diff --git a/crypto/asymmetric_keys/asymmetric_type.c b/crypto/asymmetric_keys/asymmetric_type.c
new file mode 100644
index 000000000000..bfb0424026aa
--- /dev/null
+++ b/crypto/asymmetric_keys/asymmetric_type.c
@@ -0,0 +1,156 @@
1/* Asymmetric public-key cryptography key type
2 *
3 * See Documentation/security/asymmetric-keys.txt
4 *
5 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
6 * Written by David Howells (dhowells@redhat.com)
7 *
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.
12 */
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"
18
19MODULE_LICENSE("GPL");
20
21/*
22 * Match asymmetric keys on (part of) their name
23 * We have some shorthand methods for matching keys. We allow:
24 *
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
28 */
29static int asymmetric_key_match(const struct key *key, const void *description)
30{
31 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
32 const char *spec = description;
33 const char *id, *kid;
34 ptrdiff_t speclen;
35 size_t idlen, kidlen;
36
37 if (!subtype || !spec || !*spec)
38 return 0;
39
40 /* See if the full key description matches as is */
41 if (key->description && strcmp(key->description, description) == 0)
42 return 1;
43
44 /* All tests from here on break the criterion description into a
45 * specifier, a colon and then an identifier.
46 */
47 id = strchr(spec, ':');
48 if (!id)
49 return 0;
50
51 speclen = id - spec;
52 id++;
53
54 /* Anything after here requires a partial match on the ID string */
55 kid = asymmetric_key_id(key);
56 if (!kid)
57 return 0;
58
59 idlen = strlen(id);
60 kidlen = strlen(kid);
61 if (idlen > kidlen)
62 return 0;
63
64 kid += kidlen - idlen;
65 if (strcasecmp(id, kid) != 0)
66 return 0;
67
68 if (speclen == 2 &&
69 memcmp(spec, "id", 2) == 0)
70 return 1;
71
72 if (speclen == subtype->name_len &&
73 memcmp(spec, subtype->name, speclen) == 0)
74 return 1;
75
76 return 0;
77}
78
79/*
80 * Describe the asymmetric key
81 */
82static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
83{
84 const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
85 const char *kid = asymmetric_key_id(key);
86 size_t n;
87
88 seq_puts(m, key->description);
89
90 if (subtype) {
91 seq_puts(m, ": ");
92 subtype->describe(key, m);
93
94 if (kid) {
95 seq_putc(m, ' ');
96 n = strlen(kid);
97 if (n <= 8)
98 seq_puts(m, kid);
99 else
100 seq_puts(m, kid + n - 8);
101 }
102
103 seq_puts(m, " [");
104 /* put something here to indicate the key's capabilities */
105 seq_putc(m, ']');
106 }
107}
108
109/*
110 * Instantiate a asymmetric_key defined key. The key was preparsed, so we just
111 * have to transfer the data here.
112 */
113static int asymmetric_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
114{
115 return -EOPNOTSUPP;
116}
117
118/*
119 * dispose of the data dangling from the corpse of a asymmetric key
120 */
121static void asymmetric_key_destroy(struct key *key)
122{
123 struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
124 if (subtype) {
125 subtype->destroy(key->payload.data);
126 module_put(subtype->owner);
127 key->type_data.p[0] = NULL;
128 }
129 kfree(key->type_data.p[1]);
130 key->type_data.p[1] = NULL;
131}
132
133struct 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,
139};
140EXPORT_SYMBOL_GPL(key_type_asymmetric);
141
142/*
143 * Module stuff
144 */
145static int __init asymmetric_key_init(void)
146{
147 return register_key_type(&key_type_asymmetric);
148}
149
150static void __exit asymmetric_key_cleanup(void)
151{
152 unregister_key_type(&key_type_asymmetric);
153}
154
155module_init(asymmetric_key_init);
156module_exit(asymmetric_key_cleanup);