summaryrefslogtreecommitdiffstats
path: root/crypto/asymmetric_keys
diff options
context:
space:
mode:
authorDavid Howells <dhowells@redhat.com>2016-04-06 11:14:25 -0400
committerDavid Howells <dhowells@redhat.com>2016-04-11 17:41:28 -0400
commit983023f28bff62b4462fd3575a86a8947ac592d8 (patch)
tree4bb0a779ff606b66d429d725108701b7cbf3f37d /crypto/asymmetric_keys
parent5ac7eace2d00eab5ae0e9fdee63e38aee6001f7c (diff)
KEYS: Move x509_request_asymmetric_key() to asymmetric_type.c
Move x509_request_asymmetric_key() to asymmetric_type.c so that it can be generalised. Signed-off-by: David Howells <dhowells@redhat.com>
Diffstat (limited to 'crypto/asymmetric_keys')
-rw-r--r--crypto/asymmetric_keys/asymmetric_type.c89
-rw-r--r--crypto/asymmetric_keys/x509_public_key.c89
2 files changed, 89 insertions, 89 deletions
diff --git a/crypto/asymmetric_keys/asymmetric_type.c b/crypto/asymmetric_keys/asymmetric_type.c
index a79d30128821..c4d66cd82860 100644
--- a/crypto/asymmetric_keys/asymmetric_type.c
+++ b/crypto/asymmetric_keys/asymmetric_type.c
@@ -35,6 +35,95 @@ static LIST_HEAD(asymmetric_key_parsers);
35static DECLARE_RWSEM(asymmetric_key_parsers_sem); 35static DECLARE_RWSEM(asymmetric_key_parsers_sem);
36 36
37/** 37/**
38 * x509_request_asymmetric_key - Request a key by X.509 certificate params.
39 * @keyring: The keys to search.
40 * @id: The issuer & serialNumber to look for or NULL.
41 * @skid: The subjectKeyIdentifier to look for or NULL.
42 * @partial: Use partial match if true, exact if false.
43 *
44 * Find a key in the given keyring by identifier. The preferred identifier is
45 * the issuer + serialNumber and the fallback identifier is the
46 * subjectKeyIdentifier. If both are given, the lookup is by the former, but
47 * the latter must also match.
48 */
49struct key *x509_request_asymmetric_key(struct key *keyring,
50 const struct asymmetric_key_id *id,
51 const struct asymmetric_key_id *skid,
52 bool partial)
53{
54 struct key *key;
55 key_ref_t ref;
56 const char *lookup;
57 char *req, *p;
58 int len;
59
60 if (id) {
61 lookup = id->data;
62 len = id->len;
63 } else {
64 lookup = skid->data;
65 len = skid->len;
66 }
67
68 /* Construct an identifier "id:<keyid>". */
69 p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
70 if (!req)
71 return ERR_PTR(-ENOMEM);
72
73 if (partial) {
74 *p++ = 'i';
75 *p++ = 'd';
76 } else {
77 *p++ = 'e';
78 *p++ = 'x';
79 }
80 *p++ = ':';
81 p = bin2hex(p, lookup, len);
82 *p = 0;
83
84 pr_debug("Look up: \"%s\"\n", req);
85
86 ref = keyring_search(make_key_ref(keyring, 1),
87 &key_type_asymmetric, req);
88 if (IS_ERR(ref))
89 pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
90 kfree(req);
91
92 if (IS_ERR(ref)) {
93 switch (PTR_ERR(ref)) {
94 /* Hide some search errors */
95 case -EACCES:
96 case -ENOTDIR:
97 case -EAGAIN:
98 return ERR_PTR(-ENOKEY);
99 default:
100 return ERR_CAST(ref);
101 }
102 }
103
104 key = key_ref_to_ptr(ref);
105 if (id && skid) {
106 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
107 if (!kids->id[1]) {
108 pr_debug("issuer+serial match, but expected SKID missing\n");
109 goto reject;
110 }
111 if (!asymmetric_key_id_same(skid, kids->id[1])) {
112 pr_debug("issuer+serial match, but SKID does not\n");
113 goto reject;
114 }
115 }
116
117 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
118 return key;
119
120reject:
121 key_put(key);
122 return ERR_PTR(-EKEYREJECTED);
123}
124EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
125
126/**
38 * asymmetric_key_generate_id: Construct an asymmetric key ID 127 * asymmetric_key_generate_id: Construct an asymmetric key ID
39 * @val_1: First binary blob 128 * @val_1: First binary blob
40 * @len_1: Length of first binary blob 129 * @len_1: Length of first binary blob
diff --git a/crypto/asymmetric_keys/x509_public_key.c b/crypto/asymmetric_keys/x509_public_key.c
index fc77a2bd70ba..2fb594175cef 100644
--- a/crypto/asymmetric_keys/x509_public_key.c
+++ b/crypto/asymmetric_keys/x509_public_key.c
@@ -58,95 +58,6 @@ static int __init ca_keys_setup(char *str)
58__setup("ca_keys=", ca_keys_setup); 58__setup("ca_keys=", ca_keys_setup);
59#endif 59#endif
60 60
61/**
62 * x509_request_asymmetric_key - Request a key by X.509 certificate params.
63 * @keyring: The keys to search.
64 * @id: The issuer & serialNumber to look for or NULL.
65 * @skid: The subjectKeyIdentifier to look for or NULL.
66 * @partial: Use partial match if true, exact if false.
67 *
68 * Find a key in the given keyring by identifier. The preferred identifier is
69 * the issuer + serialNumber and the fallback identifier is the
70 * subjectKeyIdentifier. If both are given, the lookup is by the former, but
71 * the latter must also match.
72 */
73struct key *x509_request_asymmetric_key(struct key *keyring,
74 const struct asymmetric_key_id *id,
75 const struct asymmetric_key_id *skid,
76 bool partial)
77{
78 struct key *key;
79 key_ref_t ref;
80 const char *lookup;
81 char *req, *p;
82 int len;
83
84 if (id) {
85 lookup = id->data;
86 len = id->len;
87 } else {
88 lookup = skid->data;
89 len = skid->len;
90 }
91
92 /* Construct an identifier "id:<keyid>". */
93 p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
94 if (!req)
95 return ERR_PTR(-ENOMEM);
96
97 if (partial) {
98 *p++ = 'i';
99 *p++ = 'd';
100 } else {
101 *p++ = 'e';
102 *p++ = 'x';
103 }
104 *p++ = ':';
105 p = bin2hex(p, lookup, len);
106 *p = 0;
107
108 pr_debug("Look up: \"%s\"\n", req);
109
110 ref = keyring_search(make_key_ref(keyring, 1),
111 &key_type_asymmetric, req);
112 if (IS_ERR(ref))
113 pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
114 kfree(req);
115
116 if (IS_ERR(ref)) {
117 switch (PTR_ERR(ref)) {
118 /* Hide some search errors */
119 case -EACCES:
120 case -ENOTDIR:
121 case -EAGAIN:
122 return ERR_PTR(-ENOKEY);
123 default:
124 return ERR_CAST(ref);
125 }
126 }
127
128 key = key_ref_to_ptr(ref);
129 if (id && skid) {
130 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
131 if (!kids->id[1]) {
132 pr_debug("issuer+serial match, but expected SKID missing\n");
133 goto reject;
134 }
135 if (!asymmetric_key_id_same(skid, kids->id[1])) {
136 pr_debug("issuer+serial match, but SKID does not\n");
137 goto reject;
138 }
139 }
140
141 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
142 return key;
143
144reject:
145 key_put(key);
146 return ERR_PTR(-EKEYREJECTED);
147}
148EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
149
150/* 61/*
151 * Set up the signature parameters in an X.509 certificate. This involves 62 * Set up the signature parameters in an X.509 certificate. This involves
152 * digesting the signed data and extracting the signature. 63 * digesting the signed data and extracting the signature.