David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 1 | /* 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 Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame^] | 20 | #include <linux/scatterlist.h> |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 21 | #include <keys/asymmetric-subtype.h> |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 22 | #include <crypto/public_key.h> |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame^] | 23 | #include <crypto/akcipher.h> |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 24 | |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 27 | const char *const pkey_algo_name[PKEY_ALGO__LAST] = { |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 28 | [PKEY_ALGO_DSA] = "dsa", |
| 29 | [PKEY_ALGO_RSA] = "rsa", |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 30 | }; |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 31 | EXPORT_SYMBOL_GPL(pkey_algo_name); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 32 | |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 33 | const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = { |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 34 | [PKEY_ID_PGP] = "PGP", |
| 35 | [PKEY_ID_X509] = "X509", |
David Howells | f29299b4 | 2015-07-31 11:43:23 +0100 | [diff] [blame] | 36 | [PKEY_ID_PKCS7] = "PKCS#7", |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 37 | }; |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 38 | EXPORT_SYMBOL_GPL(pkey_id_type_name); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Provide a part of a description of the key for /proc/keys. |
| 42 | */ |
| 43 | static void public_key_describe(const struct key *asymmetric_key, |
| 44 | struct seq_file *m) |
| 45 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 46 | struct public_key *key = asymmetric_key->payload.data[asym_crypto]; |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 47 | |
| 48 | if (key) |
| 49 | seq_printf(m, "%s.%s", |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 50 | pkey_id_type_name[key->id_type], |
| 51 | pkey_algo_name[key->pkey_algo]); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Destroy a public key algorithm key. |
| 56 | */ |
| 57 | void public_key_destroy(void *payload) |
| 58 | { |
| 59 | struct public_key *key = payload; |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 60 | |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 61 | if (key) |
| 62 | kfree(key->key); |
| 63 | kfree(key); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 64 | } |
| 65 | EXPORT_SYMBOL_GPL(public_key_destroy); |
| 66 | |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame^] | 67 | struct public_key_completion { |
| 68 | struct completion completion; |
| 69 | int err; |
| 70 | }; |
| 71 | |
| 72 | static 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 Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 83 | /* |
| 84 | * Verify a signature using a public key. |
| 85 | */ |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 86 | int public_key_verify_signature(const struct public_key *pkey, |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 87 | const struct public_key_signature *sig) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 88 | { |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame^] | 89 | 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 Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 101 | BUG_ON(!pkey); |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 102 | BUG_ON(!sig); |
| 103 | BUG_ON(!sig->digest); |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 104 | BUG_ON(!sig->s); |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 105 | |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame^] | 106 | 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 Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 119 | |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame^] | 120 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 121 | if (IS_ERR(tfm)) |
| 122 | return PTR_ERR(tfm); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 123 | |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame^] | 124 | 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 | |
| 163 | out_free_output: |
| 164 | kfree(output); |
| 165 | error_free_req: |
| 166 | akcipher_request_free(req); |
| 167 | error_free_tfm: |
| 168 | crypto_free_akcipher(tfm); |
| 169 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 170 | return ret; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 171 | } |
| 172 | EXPORT_SYMBOL_GPL(public_key_verify_signature); |
| 173 | |
| 174 | static int public_key_verify_signature_2(const struct key *key, |
| 175 | const struct public_key_signature *sig) |
| 176 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 177 | const struct public_key *pk = key->payload.data[asym_crypto]; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 178 | return public_key_verify_signature(pk, sig); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Public key algorithm asymmetric key subtype |
| 183 | */ |
| 184 | struct asymmetric_key_subtype public_key_subtype = { |
| 185 | .owner = THIS_MODULE, |
| 186 | .name = "public_key", |
David Howells | 876c6e3 | 2014-09-02 13:52:10 +0100 | [diff] [blame] | 187 | .name_len = sizeof("public_key") - 1, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 188 | .describe = public_key_describe, |
| 189 | .destroy = public_key_destroy, |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 190 | .verify_signature = public_key_verify_signature_2, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 191 | }; |
| 192 | EXPORT_SYMBOL_GPL(public_key_subtype); |