blob: 27ebc2f443944f83d35a372fa46bcecf00efbe72 [file] [log] [blame]
David Howellsa9681bf2012-09-21 23:24:55 +01001/* In-software asymmetric public-key crypto subtype
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#define pr_fmt(fmt) "PKEY: "fmt
15#include <linux/module.h>
16#include <linux/export.h>
17#include <linux/kernel.h>
18#include <linux/slab.h>
19#include <linux/seq_file.h>
David Howellsd43de6c2016-03-03 21:49:27 +000020#include <linux/scatterlist.h>
David Howellsa9681bf2012-09-21 23:24:55 +010021#include <keys/asymmetric-subtype.h>
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080022#include <crypto/public_key.h>
David Howellsd43de6c2016-03-03 21:49:27 +000023#include <crypto/akcipher.h>
David Howellsa9681bf2012-09-21 23:24:55 +010024
25MODULE_LICENSE("GPL");
26
David Howells9abc4e62013-08-30 16:15:10 +010027const char *const pkey_algo_name[PKEY_ALGO__LAST] = {
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080028 [PKEY_ALGO_DSA] = "dsa",
29 [PKEY_ALGO_RSA] = "rsa",
David Howellsa9681bf2012-09-21 23:24:55 +010030};
David Howells9abc4e62013-08-30 16:15:10 +010031EXPORT_SYMBOL_GPL(pkey_algo_name);
David Howellsa9681bf2012-09-21 23:24:55 +010032
David Howells9abc4e62013-08-30 16:15:10 +010033const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = {
David Howellsa9681bf2012-09-21 23:24:55 +010034 [PKEY_ID_PGP] = "PGP",
35 [PKEY_ID_X509] = "X509",
David Howellsf29299b42015-07-31 11:43:23 +010036 [PKEY_ID_PKCS7] = "PKCS#7",
David Howellsa9681bf2012-09-21 23:24:55 +010037};
David Howells9abc4e62013-08-30 16:15:10 +010038EXPORT_SYMBOL_GPL(pkey_id_type_name);
David Howellsa9681bf2012-09-21 23:24:55 +010039
40/*
41 * Provide a part of a description of the key for /proc/keys.
42 */
43static void public_key_describe(const struct key *asymmetric_key,
44 struct seq_file *m)
45{
David Howells146aa8b2015-10-21 14:04:48 +010046 struct public_key *key = asymmetric_key->payload.data[asym_crypto];
David Howellsa9681bf2012-09-21 23:24:55 +010047
48 if (key)
49 seq_printf(m, "%s.%s",
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080050 pkey_id_type_name[key->id_type],
51 pkey_algo_name[key->pkey_algo]);
David Howellsa9681bf2012-09-21 23:24:55 +010052}
53
54/*
55 * Destroy a public key algorithm key.
56 */
57void public_key_destroy(void *payload)
58{
59 struct public_key *key = payload;
David Howellsa9681bf2012-09-21 23:24:55 +010060
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080061 if (key)
62 kfree(key->key);
63 kfree(key);
David Howellsa9681bf2012-09-21 23:24:55 +010064}
65EXPORT_SYMBOL_GPL(public_key_destroy);
66
David Howellsd43de6c2016-03-03 21:49:27 +000067struct public_key_completion {
68 struct completion completion;
69 int err;
70};
71
72static void public_key_verify_done(struct crypto_async_request *req, int err)
73{
74 struct public_key_completion *compl = req->data;
75
76 if (err == -EINPROGRESS)
77 return;
78
79 compl->err = err;
80 complete(&compl->completion);
81}
82
David Howellsa9681bf2012-09-21 23:24:55 +010083/*
84 * Verify a signature using a public key.
85 */
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080086int public_key_verify_signature(const struct public_key *pkey,
David Howells3d167d62013-08-30 16:15:30 +010087 const struct public_key_signature *sig)
David Howellsa9681bf2012-09-21 23:24:55 +010088{
David Howellsd43de6c2016-03-03 21:49:27 +000089 struct public_key_completion compl;
90 struct crypto_akcipher *tfm;
91 struct akcipher_request *req;
92 struct scatterlist sig_sg, digest_sg;
93 const char *alg_name;
94 char alg_name_buf[CRYPTO_MAX_ALG_NAME];
95 void *output;
96 unsigned int outlen;
97 int ret = -ENOMEM;
98
99 pr_devel("==>%s()\n", __func__);
100
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -0800101 BUG_ON(!pkey);
David Howells3d167d62013-08-30 16:15:30 +0100102 BUG_ON(!sig);
103 BUG_ON(!sig->digest);
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -0800104 BUG_ON(!sig->s);
David Howells3d167d62013-08-30 16:15:30 +0100105
David Howellsd43de6c2016-03-03 21:49:27 +0000106 alg_name = pkey_algo_name[sig->pkey_algo];
107 if (sig->pkey_algo == PKEY_ALGO_RSA) {
108 /* The data wangled by the RSA algorithm is typically padded
109 * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
110 * sec 8.2].
111 */
112 if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
113 "pkcs1pad(rsa,%s)",
114 hash_algo_name[sig->pkey_hash_algo]
115 ) >= CRYPTO_MAX_ALG_NAME)
116 return -EINVAL;
117 alg_name = alg_name_buf;
118 }
David Howells3d167d62013-08-30 16:15:30 +0100119
David Howellsd43de6c2016-03-03 21:49:27 +0000120 tfm = crypto_alloc_akcipher(alg_name, 0, 0);
121 if (IS_ERR(tfm))
122 return PTR_ERR(tfm);
David Howellsa9681bf2012-09-21 23:24:55 +0100123
David Howellsd43de6c2016-03-03 21:49:27 +0000124 req = akcipher_request_alloc(tfm, GFP_KERNEL);
125 if (!req)
126 goto error_free_tfm;
127
128 ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
129 if (ret)
130 goto error_free_req;
131
132 outlen = crypto_akcipher_maxsize(tfm);
133 output = kmalloc(outlen, GFP_KERNEL);
134 if (!output)
135 goto error_free_req;
136
137 sg_init_one(&sig_sg, sig->s, sig->s_size);
138 sg_init_one(&digest_sg, output, outlen);
139 akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
140 outlen);
141 init_completion(&compl.completion);
142 akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
143 CRYPTO_TFM_REQ_MAY_SLEEP,
144 public_key_verify_done, &compl);
145
146 /* Perform the verification calculation. This doesn't actually do the
147 * verification, but rather calculates the hash expected by the
148 * signature and returns that to us.
149 */
150 ret = crypto_akcipher_verify(req);
151 if (ret == -EINPROGRESS) {
152 wait_for_completion(&compl.completion);
153 ret = compl.err;
154 }
155 if (ret < 0)
156 goto out_free_output;
157
158 /* Do the actual verification step. */
159 if (req->dst_len != sig->digest_size ||
160 memcmp(sig->digest, output, sig->digest_size) != 0)
161 ret = -EKEYREJECTED;
162
163out_free_output:
164 kfree(output);
165error_free_req:
166 akcipher_request_free(req);
167error_free_tfm:
168 crypto_free_akcipher(tfm);
169 pr_devel("<==%s() = %d\n", __func__, ret);
170 return ret;
David Howells3d167d62013-08-30 16:15:30 +0100171}
172EXPORT_SYMBOL_GPL(public_key_verify_signature);
173
174static int public_key_verify_signature_2(const struct key *key,
175 const struct public_key_signature *sig)
176{
David Howells146aa8b2015-10-21 14:04:48 +0100177 const struct public_key *pk = key->payload.data[asym_crypto];
David Howells3d167d62013-08-30 16:15:30 +0100178 return public_key_verify_signature(pk, sig);
David Howellsa9681bf2012-09-21 23:24:55 +0100179}
180
181/*
182 * Public key algorithm asymmetric key subtype
183 */
184struct asymmetric_key_subtype public_key_subtype = {
185 .owner = THIS_MODULE,
186 .name = "public_key",
David Howells876c6e32014-09-02 13:52:10 +0100187 .name_len = sizeof("public_key") - 1,
David Howellsa9681bf2012-09-21 23:24:55 +0100188 .describe = public_key_describe,
189 .destroy = public_key_destroy,
David Howells3d167d62013-08-30 16:15:30 +0100190 .verify_signature = public_key_verify_signature_2,
David Howellsa9681bf2012-09-21 23:24:55 +0100191};
192EXPORT_SYMBOL_GPL(public_key_subtype);