]>
Commit | Line | Data |
---|---|---|
a9681bf3 DH |
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 | ||
9abc4e66 | 25 | const char *const pkey_algo_name[PKEY_ALGO__LAST] = { |
a9681bf3 DH |
26 | [PKEY_ALGO_DSA] = "DSA", |
27 | [PKEY_ALGO_RSA] = "RSA", | |
28 | }; | |
9abc4e66 | 29 | EXPORT_SYMBOL_GPL(pkey_algo_name); |
a9681bf3 | 30 | |
9abc4e66 | 31 | const char *const pkey_hash_algo_name[PKEY_HASH__LAST] = { |
a9681bf3 DH |
32 | [PKEY_HASH_MD4] = "md4", |
33 | [PKEY_HASH_MD5] = "md5", | |
34 | [PKEY_HASH_SHA1] = "sha1", | |
35 | [PKEY_HASH_RIPE_MD_160] = "rmd160", | |
36 | [PKEY_HASH_SHA256] = "sha256", | |
37 | [PKEY_HASH_SHA384] = "sha384", | |
38 | [PKEY_HASH_SHA512] = "sha512", | |
39 | [PKEY_HASH_SHA224] = "sha224", | |
40 | }; | |
9abc4e66 | 41 | EXPORT_SYMBOL_GPL(pkey_hash_algo_name); |
a9681bf3 | 42 | |
9abc4e66 | 43 | const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = { |
a9681bf3 DH |
44 | [PKEY_ID_PGP] = "PGP", |
45 | [PKEY_ID_X509] = "X509", | |
46 | }; | |
9abc4e66 | 47 | EXPORT_SYMBOL_GPL(pkey_id_type_name); |
a9681bf3 DH |
48 | |
49 | /* | |
50 | * Provide a part of a description of the key for /proc/keys. | |
51 | */ | |
52 | static void public_key_describe(const struct key *asymmetric_key, | |
53 | struct seq_file *m) | |
54 | { | |
55 | struct public_key *key = asymmetric_key->payload.data; | |
56 | ||
57 | if (key) | |
58 | seq_printf(m, "%s.%s", | |
9abc4e66 | 59 | pkey_id_type_name[key->id_type], key->algo->name); |
a9681bf3 DH |
60 | } |
61 | ||
62 | /* | |
63 | * Destroy a public key algorithm key. | |
64 | */ | |
65 | void public_key_destroy(void *payload) | |
66 | { | |
67 | struct public_key *key = payload; | |
68 | int i; | |
69 | ||
70 | if (key) { | |
71 | for (i = 0; i < ARRAY_SIZE(key->mpi); i++) | |
72 | mpi_free(key->mpi[i]); | |
73 | kfree(key); | |
74 | } | |
75 | } | |
76 | EXPORT_SYMBOL_GPL(public_key_destroy); | |
77 | ||
78 | /* | |
79 | * Verify a signature using a public key. | |
80 | */ | |
81 | static int public_key_verify_signature(const struct key *key, | |
82 | const struct public_key_signature *sig) | |
83 | { | |
84 | const struct public_key *pk = key->payload.data; | |
85 | ||
86 | if (!pk->algo->verify_signature) | |
87 | return -ENOTSUPP; | |
88 | ||
89 | if (sig->nr_mpi != pk->algo->n_sig_mpi) { | |
90 | pr_debug("Signature has %u MPI not %u\n", | |
91 | sig->nr_mpi, pk->algo->n_sig_mpi); | |
92 | return -EINVAL; | |
93 | } | |
94 | ||
95 | return pk->algo->verify_signature(pk, sig); | |
96 | } | |
97 | ||
98 | /* | |
99 | * Public key algorithm asymmetric key subtype | |
100 | */ | |
101 | struct asymmetric_key_subtype public_key_subtype = { | |
102 | .owner = THIS_MODULE, | |
103 | .name = "public_key", | |
104 | .describe = public_key_describe, | |
105 | .destroy = public_key_destroy, | |
106 | .verify_signature = public_key_verify_signature, | |
107 | }; | |
108 | EXPORT_SYMBOL_GPL(public_key_subtype); |