Skip to content

Commit 46c6f17

Browse files
dhowellsrustyrussell
authored andcommitted
KEYS: Asymmetric key pluggable data parsers
The instantiation data passed to the asymmetric key type are expected to be formatted in some way, and there are several possible standard ways to format the data. The two obvious standards are OpenPGP keys and X.509 certificates. The latter is especially useful when dealing with UEFI, and the former might be useful when dealing with, say, eCryptfs. Further, it might be desirable to provide formatted blobs that indicate hardware is to be accessed to retrieve the keys or that the keys live unretrievably in a hardware store, but that the keys can be used by means of the hardware. From userspace, the keys can be loaded using the keyctl command, for example, an X.509 binary certificate: keyctl padd asymmetric foo @s <dhowells.pem or a PGP key: keyctl padd asymmetric bar @s <dhowells.pub or a pointer into the contents of the TPM: keyctl add asymmetric zebra "TPM:04982390582905f8" @s Inside the kernel, pluggable parsers register themselves and then get to examine the payload data to see if they can handle it. If they can, they get to: (1) Propose a name for the key, to be used it the name is "" or NULL. (2) Specify the key subtype. (3) Provide the data for the subtype. The key type asks the parser to do its stuff before a key is allocated and thus before the name is set. If successful, the parser stores the suggested data into the key_preparsed_payload struct, which will be either used (if the key is successfully created and instantiated or updated) or discarded. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 964f3b3 commit 46c6f17

2 files changed

Lines changed: 156 additions & 1 deletion

File tree

‎crypto/asymmetric_keys/asymmetric_type.c‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
* 2 of the Licence, or (at your option) any later version.
1212
*/
1313
#include <keys/asymmetric-subtype.h>
14+
#include <keys/asymmetric-parser.h>
1415
#include <linux/seq_file.h>
1516
#include <linux/module.h>
1617
#include <linux/slab.h>
1718
#include "asymmetric_keys.h"
1819

1920
MODULE_LICENSE("GPL");
2021

22+
static LIST_HEAD(asymmetric_key_parsers);
23+
static DECLARE_RWSEM(asymmetric_key_parsers_sem);
24+
2125
/*
2226
* Match asymmetric keys on (part of) their name
2327
* We have some shorthand methods for matching keys. We allow:
@@ -106,13 +110,80 @@ static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
106110
}
107111
}
108112

113+
/*
114+
* Preparse a asymmetric payload to get format the contents appropriately for the
115+
* internal payload to cut down on the number of scans of the data performed.
116+
*
117+
* We also generate a proposed description from the contents of the key that
118+
* can be used to name the key if the user doesn't want to provide one.
119+
*/
120+
static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
121+
{
122+
struct asymmetric_key_parser *parser;
123+
int ret;
124+
125+
pr_devel("==>%s()\n", __func__);
126+
127+
if (prep->datalen == 0)
128+
return -EINVAL;
129+
130+
down_read(&asymmetric_key_parsers_sem);
131+
132+
ret = -EBADMSG;
133+
list_for_each_entry(parser, &asymmetric_key_parsers, link) {
134+
pr_debug("Trying parser '%s'\n", parser->name);
135+
136+
ret = parser->parse(prep);
137+
if (ret != -EBADMSG) {
138+
pr_debug("Parser recognised the format (ret %d)\n",
139+
ret);
140+
break;
141+
}
142+
}
143+
144+
up_read(&asymmetric_key_parsers_sem);
145+
pr_devel("<==%s() = %d\n", __func__, ret);
146+
return ret;
147+
}
148+
149+
/*
150+
* Clean up the preparse data
151+
*/
152+
static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
153+
{
154+
struct asymmetric_key_subtype *subtype = prep->type_data[0];
155+
156+
pr_devel("==>%s()\n", __func__);
157+
158+
if (subtype) {
159+
subtype->destroy(prep->payload);
160+
module_put(subtype->owner);
161+
}
162+
kfree(prep->type_data[1]);
163+
kfree(prep->description);
164+
}
165+
109166
/*
110167
* Instantiate a asymmetric_key defined key. The key was preparsed, so we just
111168
* have to transfer the data here.
112169
*/
113170
static int asymmetric_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
114171
{
115-
return -EOPNOTSUPP;
172+
int ret;
173+
174+
pr_devel("==>%s()\n", __func__);
175+
176+
ret = key_payload_reserve(key, prep->quotalen);
177+
if (ret == 0) {
178+
key->type_data.p[0] = prep->type_data[0];
179+
key->type_data.p[1] = prep->type_data[1];
180+
key->payload.data = prep->payload;
181+
prep->type_data[0] = NULL;
182+
prep->type_data[1] = NULL;
183+
prep->payload = NULL;
184+
}
185+
pr_devel("<==%s() = %d\n", __func__, ret);
186+
return ret;
116187
}
117188

118189
/*
@@ -132,13 +203,60 @@ static void asymmetric_key_destroy(struct key *key)
132203

133204
struct key_type key_type_asymmetric = {
134205
.name = "asymmetric",
206+
.preparse = asymmetric_key_preparse,
207+
.free_preparse = asymmetric_key_free_preparse,
135208
.instantiate = asymmetric_key_instantiate,
136209
.match = asymmetric_key_match,
137210
.destroy = asymmetric_key_destroy,
138211
.describe = asymmetric_key_describe,
139212
};
140213
EXPORT_SYMBOL_GPL(key_type_asymmetric);
141214

215+
/**
216+
* register_asymmetric_key_parser - Register a asymmetric key blob parser
217+
* @parser: The parser to register
218+
*/
219+
int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
220+
{
221+
struct asymmetric_key_parser *cursor;
222+
int ret;
223+
224+
down_write(&asymmetric_key_parsers_sem);
225+
226+
list_for_each_entry(cursor, &asymmetric_key_parsers, link) {
227+
if (strcmp(cursor->name, parser->name) == 0) {
228+
pr_err("Asymmetric key parser '%s' already registered\n",
229+
parser->name);
230+
ret = -EEXIST;
231+
goto out;
232+
}
233+
}
234+
235+
list_add_tail(&parser->link, &asymmetric_key_parsers);
236+
237+
pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
238+
ret = 0;
239+
240+
out:
241+
up_write(&asymmetric_key_parsers_sem);
242+
return ret;
243+
}
244+
EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
245+
246+
/**
247+
* unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
248+
* @parser: The parser to unregister
249+
*/
250+
void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
251+
{
252+
down_write(&asymmetric_key_parsers_sem);
253+
list_del(&parser->link);
254+
up_write(&asymmetric_key_parsers_sem);
255+
256+
pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
257+
}
258+
EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
259+
142260
/*
143261
* Module stuff
144262
*/

‎include/keys/asymmetric-parser.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Asymmetric public-key cryptography data parser
2+
*
3+
* See Documentation/crypto/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+
14+
#ifndef _KEYS_ASYMMETRIC_PARSER_H
15+
#define _KEYS_ASYMMETRIC_PARSER_H
16+
17+
/*
18+
* Key data parser. Called during key instantiation.
19+
*/
20+
struct asymmetric_key_parser {
21+
struct list_head link;
22+
struct module *owner;
23+
const char *name;
24+
25+
/* Attempt to parse a key from the data blob passed to add_key() or
26+
* keyctl_instantiate(). Should also generate a proposed description
27+
* that the caller can optionally use for the key.
28+
*
29+
* Return EBADMSG if not recognised.
30+
*/
31+
int (*parse)(struct key_preparsed_payload *prep);
32+
};
33+
34+
extern int register_asymmetric_key_parser(struct asymmetric_key_parser *);
35+
extern void unregister_asymmetric_key_parser(struct asymmetric_key_parser *);
36+
37+
#endif /* _KEYS_ASYMMETRIC_PARSER_H */

0 commit comments

Comments
 (0)