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> |
| 20 | #include <keys/asymmetric-subtype.h> |
| 21 | #include "public_key.h" |
| 22 | |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 25 | const char *const pkey_algo_name[PKEY_ALGO__LAST] = { |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 26 | [PKEY_ALGO_DSA] = "DSA", |
| 27 | [PKEY_ALGO_RSA] = "RSA", |
| 28 | }; |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 29 | EXPORT_SYMBOL_GPL(pkey_algo_name); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 30 | |
David Howells | 206ce59 | 2013-08-30 16:15:18 +0100 | [diff] [blame] | 31 | const struct public_key_algorithm *pkey_algo[PKEY_ALGO__LAST] = { |
| 32 | #if defined(CONFIG_PUBLIC_KEY_ALGO_RSA) || \ |
| 33 | defined(CONFIG_PUBLIC_KEY_ALGO_RSA_MODULE) |
| 34 | [PKEY_ALGO_RSA] = &RSA_public_key_algorithm, |
| 35 | #endif |
| 36 | }; |
| 37 | EXPORT_SYMBOL_GPL(pkey_algo); |
| 38 | |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 39 | const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = { |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 40 | [PKEY_ID_PGP] = "PGP", |
| 41 | [PKEY_ID_X509] = "X509", |
David Howells | f29299b4 | 2015-07-31 11:43:23 +0100 | [diff] [blame] | 42 | [PKEY_ID_PKCS7] = "PKCS#7", |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 43 | }; |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 44 | EXPORT_SYMBOL_GPL(pkey_id_type_name); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Provide a part of a description of the key for /proc/keys. |
| 48 | */ |
| 49 | static void public_key_describe(const struct key *asymmetric_key, |
| 50 | struct seq_file *m) |
| 51 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 52 | struct public_key *key = asymmetric_key->payload.data[asym_crypto]; |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 53 | |
| 54 | if (key) |
| 55 | seq_printf(m, "%s.%s", |
David Howells | 9abc4e6 | 2013-08-30 16:15:10 +0100 | [diff] [blame] | 56 | pkey_id_type_name[key->id_type], key->algo->name); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Destroy a public key algorithm key. |
| 61 | */ |
| 62 | void public_key_destroy(void *payload) |
| 63 | { |
| 64 | struct public_key *key = payload; |
| 65 | int i; |
| 66 | |
| 67 | if (key) { |
| 68 | for (i = 0; i < ARRAY_SIZE(key->mpi); i++) |
| 69 | mpi_free(key->mpi[i]); |
| 70 | kfree(key); |
| 71 | } |
| 72 | } |
| 73 | EXPORT_SYMBOL_GPL(public_key_destroy); |
| 74 | |
| 75 | /* |
| 76 | * Verify a signature using a public key. |
| 77 | */ |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 78 | int public_key_verify_signature(const struct public_key *pk, |
| 79 | const struct public_key_signature *sig) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 80 | { |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 81 | const struct public_key_algorithm *algo; |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 82 | |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 83 | BUG_ON(!pk); |
| 84 | BUG_ON(!pk->mpi[0]); |
| 85 | BUG_ON(!pk->mpi[1]); |
| 86 | BUG_ON(!sig); |
| 87 | BUG_ON(!sig->digest); |
| 88 | BUG_ON(!sig->mpi[0]); |
| 89 | |
| 90 | algo = pk->algo; |
| 91 | if (!algo) { |
| 92 | if (pk->pkey_algo >= PKEY_ALGO__LAST) |
| 93 | return -ENOPKG; |
| 94 | algo = pkey_algo[pk->pkey_algo]; |
| 95 | if (!algo) |
| 96 | return -ENOPKG; |
| 97 | } |
| 98 | |
| 99 | if (!algo->verify_signature) |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 100 | return -ENOTSUPP; |
| 101 | |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 102 | if (sig->nr_mpi != algo->n_sig_mpi) { |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 103 | pr_debug("Signature has %u MPI not %u\n", |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 104 | sig->nr_mpi, algo->n_sig_mpi); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 105 | return -EINVAL; |
| 106 | } |
| 107 | |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 108 | return algo->verify_signature(pk, sig); |
| 109 | } |
| 110 | EXPORT_SYMBOL_GPL(public_key_verify_signature); |
| 111 | |
| 112 | static int public_key_verify_signature_2(const struct key *key, |
| 113 | const struct public_key_signature *sig) |
| 114 | { |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 115 | const struct public_key *pk = key->payload.data[asym_crypto]; |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 116 | return public_key_verify_signature(pk, sig); |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Public key algorithm asymmetric key subtype |
| 121 | */ |
| 122 | struct asymmetric_key_subtype public_key_subtype = { |
| 123 | .owner = THIS_MODULE, |
| 124 | .name = "public_key", |
David Howells | 876c6e3 | 2014-09-02 13:52:10 +0100 | [diff] [blame] | 125 | .name_len = sizeof("public_key") - 1, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 126 | .describe = public_key_describe, |
| 127 | .destroy = public_key_destroy, |
David Howells | 3d167d6 | 2013-08-30 16:15:30 +0100 | [diff] [blame] | 128 | .verify_signature = public_key_verify_signature_2, |
David Howells | a9681bf | 2012-09-21 23:24:55 +0100 | [diff] [blame] | 129 | }; |
| 130 | EXPORT_SYMBOL_GPL(public_key_subtype); |