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 | |
David Howells | 1e684d3 | 2017-11-15 16:38:45 +0000 | [diff] [blame] | 25 | MODULE_DESCRIPTION("In-software asymmetric public-key subtype"); |
| 26 | MODULE_AUTHOR("Red Hat, Inc."); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 27 | MODULE_LICENSE("GPL"); |
| 28 | |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 29 | /* |
| 30 | * Provide a part of a description of the key for /proc/keys. |
| 31 | */ |
| 32 | static void public_key_describe(const struct key *asymmetric_key, |
| 33 | struct seq_file *m) |
| 34 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 35 | struct public_key *key = asymmetric_key->payload.data[asym_crypto]; |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 36 | |
| 37 | if (key) |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 38 | seq_printf(m, "%s.%s", key->id_type, key->pkey_algo); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Destroy a public key algorithm key. |
| 43 | */ |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 44 | void public_key_free(struct public_key *key) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 45 | { |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 46 | if (key) { |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 47 | kfree(key->key); |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 48 | kfree(key); |
| 49 | } |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 50 | } |
David Howells | 3b76456 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 51 | EXPORT_SYMBOL_GPL(public_key_free); |
| 52 | |
| 53 | /* |
| 54 | * Destroy a public key algorithm key. |
| 55 | */ |
| 56 | static void public_key_destroy(void *payload0, void *payload3) |
| 57 | { |
| 58 | public_key_free(payload0); |
| 59 | public_key_signature_free(payload3); |
| 60 | } |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Verify a signature using a public key. |
| 64 | */ |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 65 | int public_key_verify_signature(const struct public_key *pkey, |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 66 | const struct public_key_signature *sig) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 67 | { |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 68 | struct crypto_wait cwait; |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 69 | struct crypto_akcipher *tfm; |
| 70 | struct akcipher_request *req; |
| 71 | struct scatterlist sig_sg, digest_sg; |
| 72 | const char *alg_name; |
| 73 | char alg_name_buf[CRYPTO_MAX_ALG_NAME]; |
| 74 | void *output; |
| 75 | unsigned int outlen; |
| 76 | int ret = -ENOMEM; |
| 77 | |
| 78 | pr_devel("==>%s()\n", __func__); |
| 79 | |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 80 | BUG_ON(!pkey); |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 81 | BUG_ON(!sig); |
| 82 | BUG_ON(!sig->digest); |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 83 | BUG_ON(!sig->s); |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 84 | |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 85 | alg_name = sig->pkey_algo; |
| 86 | if (strcmp(sig->pkey_algo, "rsa") == 0) { |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 87 | /* The data wangled by the RSA algorithm is typically padded |
| 88 | * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447 |
| 89 | * sec 8.2]. |
| 90 | */ |
| 91 | if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME, |
David Howells | 4e8ae72 | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 92 | "pkcs1pad(rsa,%s)", sig->hash_algo |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 93 | ) >= CRYPTO_MAX_ALG_NAME) |
| 94 | return -EINVAL; |
| 95 | alg_name = alg_name_buf; |
| 96 | } |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 97 | |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 98 | tfm = crypto_alloc_akcipher(alg_name, 0, 0); |
| 99 | if (IS_ERR(tfm)) |
| 100 | return PTR_ERR(tfm); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 101 | |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 102 | req = akcipher_request_alloc(tfm, GFP_KERNEL); |
| 103 | if (!req) |
| 104 | goto error_free_tfm; |
| 105 | |
| 106 | ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen); |
| 107 | if (ret) |
| 108 | goto error_free_req; |
| 109 | |
Pan Bian | fbb7263 | 2016-12-13 09:26:18 +0000 | [diff] [blame] | 110 | ret = -ENOMEM; |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 111 | outlen = crypto_akcipher_maxsize(tfm); |
| 112 | output = kmalloc(outlen, GFP_KERNEL); |
| 113 | if (!output) |
| 114 | goto error_free_req; |
| 115 | |
| 116 | sg_init_one(&sig_sg, sig->s, sig->s_size); |
| 117 | sg_init_one(&digest_sg, output, outlen); |
| 118 | akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size, |
| 119 | outlen); |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 120 | crypto_init_wait(&cwait); |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 121 | akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | |
| 122 | CRYPTO_TFM_REQ_MAY_SLEEP, |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 123 | crypto_req_done, &cwait); |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 124 | |
| 125 | /* Perform the verification calculation. This doesn't actually do the |
| 126 | * verification, but rather calculates the hash expected by the |
| 127 | * signature and returns that to us. |
| 128 | */ |
Gilad Ben-Yossef | 0ca2a04 | 2017-10-18 08:00:40 +0100 | [diff] [blame] | 129 | ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait); |
David Howells | d43de6c | 2016-03-03 21:49:27 +0000 | [diff] [blame] | 130 | if (ret < 0) |
| 131 | goto out_free_output; |
| 132 | |
| 133 | /* Do the actual verification step. */ |
| 134 | if (req->dst_len != sig->digest_size || |
| 135 | memcmp(sig->digest, output, sig->digest_size) != 0) |
| 136 | ret = -EKEYREJECTED; |
| 137 | |
| 138 | out_free_output: |
| 139 | kfree(output); |
| 140 | error_free_req: |
| 141 | akcipher_request_free(req); |
| 142 | error_free_tfm: |
| 143 | crypto_free_akcipher(tfm); |
| 144 | pr_devel("<==%s() = %d\n", __func__, ret); |
| 145 | return ret; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 146 | } |
| 147 | EXPORT_SYMBOL_GPL(public_key_verify_signature); |
| 148 | |
| 149 | static int public_key_verify_signature_2(const struct key *key, |
| 150 | const struct public_key_signature *sig) |
| 151 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 152 | const struct public_key *pk = key->payload.data[asym_crypto]; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 153 | return public_key_verify_signature(pk, sig); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | * Public key algorithm asymmetric key subtype |
| 158 | */ |
| 159 | struct asymmetric_key_subtype public_key_subtype = { |
| 160 | .owner = THIS_MODULE, |
| 161 | .name = "public_key", |
David Howells | 876c6e3 | 2014-09-02 13:52:10 +0100 | [diff] [blame] | 162 | .name_len = sizeof("public_key") - 1, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 163 | .describe = public_key_describe, |
| 164 | .destroy = public_key_destroy, |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 165 | .verify_signature = public_key_verify_signature_2, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 166 | }; |
| 167 | EXPORT_SYMBOL_GPL(public_key_subtype); |