diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2016-02-09 03:18:01 -0500 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2016-02-09 03:18:01 -0500 |
commit | f75516a81548074be331b3e41d2e3f448d1f8b8d (patch) | |
tree | ba6e557c7473389709cf7a3bc69e608dcd8584f2 /crypto/asymmetric_keys | |
parent | ed1afac9145c4517a2c84f93e04a35046ea206e3 (diff) |
crypto: keys - Revert "convert public key to akcipher api"
This needs to go through the security tree so I'm reverting the
patches for now.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/asymmetric_keys')
-rw-r--r-- | crypto/asymmetric_keys/Kconfig | 2 | ||||
-rw-r--r-- | crypto/asymmetric_keys/Makefile | 7 | ||||
-rw-r--r-- | crypto/asymmetric_keys/pkcs7_parser.c | 12 | ||||
-rw-r--r-- | crypto/asymmetric_keys/pkcs7_trust.c | 2 | ||||
-rw-r--r-- | crypto/asymmetric_keys/pkcs7_verify.c | 2 | ||||
-rw-r--r-- | crypto/asymmetric_keys/public_key.c | 64 | ||||
-rw-r--r-- | crypto/asymmetric_keys/public_key.h | 36 | ||||
-rw-r--r-- | crypto/asymmetric_keys/rsa.c | 213 | ||||
-rw-r--r-- | crypto/asymmetric_keys/x509_cert_parser.c | 37 | ||||
-rw-r--r-- | crypto/asymmetric_keys/x509_public_key.c | 17 | ||||
-rw-r--r-- | crypto/asymmetric_keys/x509_rsakey.asn1 | 4 |
11 files changed, 272 insertions, 124 deletions
diff --git a/crypto/asymmetric_keys/Kconfig b/crypto/asymmetric_keys/Kconfig index 905d745c2f85..4870f28403f5 100644 --- a/crypto/asymmetric_keys/Kconfig +++ b/crypto/asymmetric_keys/Kconfig | |||
@@ -22,7 +22,7 @@ config ASYMMETRIC_PUBLIC_KEY_SUBTYPE | |||
22 | 22 | ||
23 | config PUBLIC_KEY_ALGO_RSA | 23 | config PUBLIC_KEY_ALGO_RSA |
24 | tristate "RSA public-key algorithm" | 24 | tristate "RSA public-key algorithm" |
25 | select CRYPTO_RSA | 25 | select MPILIB |
26 | help | 26 | help |
27 | This option enables support for the RSA algorithm (PKCS#1, RFC3447). | 27 | This option enables support for the RSA algorithm (PKCS#1, RFC3447). |
28 | 28 | ||
diff --git a/crypto/asymmetric_keys/Makefile b/crypto/asymmetric_keys/Makefile index b78a194ea014..cd1406f9b14a 100644 --- a/crypto/asymmetric_keys/Makefile +++ b/crypto/asymmetric_keys/Makefile | |||
@@ -16,18 +16,21 @@ obj-$(CONFIG_X509_CERTIFICATE_PARSER) += x509_key_parser.o | |||
16 | x509_key_parser-y := \ | 16 | x509_key_parser-y := \ |
17 | x509-asn1.o \ | 17 | x509-asn1.o \ |
18 | x509_akid-asn1.o \ | 18 | x509_akid-asn1.o \ |
19 | x509_rsakey-asn1.o \ | ||
19 | x509_cert_parser.o \ | 20 | x509_cert_parser.o \ |
20 | x509_public_key.o | 21 | x509_public_key.o |
21 | 22 | ||
22 | $(obj)/x509_cert_parser.o: \ | 23 | $(obj)/x509_cert_parser.o: \ |
23 | $(obj)/x509-asn1.h \ | 24 | $(obj)/x509-asn1.h \ |
24 | $(obj)/x509_akid-asn1.h | 25 | $(obj)/x509_akid-asn1.h \ |
25 | 26 | $(obj)/x509_rsakey-asn1.h | |
26 | $(obj)/x509-asn1.o: $(obj)/x509-asn1.c $(obj)/x509-asn1.h | 27 | $(obj)/x509-asn1.o: $(obj)/x509-asn1.c $(obj)/x509-asn1.h |
27 | $(obj)/x509_akid-asn1.o: $(obj)/x509_akid-asn1.c $(obj)/x509_akid-asn1.h | 28 | $(obj)/x509_akid-asn1.o: $(obj)/x509_akid-asn1.c $(obj)/x509_akid-asn1.h |
29 | $(obj)/x509_rsakey-asn1.o: $(obj)/x509_rsakey-asn1.c $(obj)/x509_rsakey-asn1.h | ||
28 | 30 | ||
29 | clean-files += x509-asn1.c x509-asn1.h | 31 | clean-files += x509-asn1.c x509-asn1.h |
30 | clean-files += x509_akid-asn1.c x509_akid-asn1.h | 32 | clean-files += x509_akid-asn1.c x509_akid-asn1.h |
33 | clean-files += x509_rsakey-asn1.c x509_rsakey-asn1.h | ||
31 | 34 | ||
32 | # | 35 | # |
33 | # PKCS#7 message handling | 36 | # PKCS#7 message handling |
diff --git a/crypto/asymmetric_keys/pkcs7_parser.c b/crypto/asymmetric_keys/pkcs7_parser.c index 3ef62dac9771..8f3056cd0399 100644 --- a/crypto/asymmetric_keys/pkcs7_parser.c +++ b/crypto/asymmetric_keys/pkcs7_parser.c | |||
@@ -15,7 +15,7 @@ | |||
15 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
16 | #include <linux/err.h> | 16 | #include <linux/err.h> |
17 | #include <linux/oid_registry.h> | 17 | #include <linux/oid_registry.h> |
18 | #include <crypto/public_key.h> | 18 | #include "public_key.h" |
19 | #include "pkcs7_parser.h" | 19 | #include "pkcs7_parser.h" |
20 | #include "pkcs7-asn1.h" | 20 | #include "pkcs7-asn1.h" |
21 | 21 | ||
@@ -44,7 +44,7 @@ struct pkcs7_parse_context { | |||
44 | static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo) | 44 | static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo) |
45 | { | 45 | { |
46 | if (sinfo) { | 46 | if (sinfo) { |
47 | kfree(sinfo->sig.s); | 47 | mpi_free(sinfo->sig.mpi[0]); |
48 | kfree(sinfo->sig.digest); | 48 | kfree(sinfo->sig.digest); |
49 | kfree(sinfo->signing_cert_id); | 49 | kfree(sinfo->signing_cert_id); |
50 | kfree(sinfo); | 50 | kfree(sinfo); |
@@ -614,14 +614,16 @@ int pkcs7_sig_note_signature(void *context, size_t hdrlen, | |||
614 | const void *value, size_t vlen) | 614 | const void *value, size_t vlen) |
615 | { | 615 | { |
616 | struct pkcs7_parse_context *ctx = context; | 616 | struct pkcs7_parse_context *ctx = context; |
617 | MPI mpi; | ||
617 | 618 | ||
618 | BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA); | 619 | BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA); |
619 | 620 | ||
620 | ctx->sinfo->sig.s = kmemdup(value, vlen, GFP_KERNEL); | 621 | mpi = mpi_read_raw_data(value, vlen); |
621 | if (!ctx->sinfo->sig.s) | 622 | if (!mpi) |
622 | return -ENOMEM; | 623 | return -ENOMEM; |
623 | 624 | ||
624 | ctx->sinfo->sig.s_size = vlen; | 625 | ctx->sinfo->sig.mpi[0] = mpi; |
626 | ctx->sinfo->sig.nr_mpi = 1; | ||
625 | return 0; | 627 | return 0; |
626 | } | 628 | } |
627 | 629 | ||
diff --git a/crypto/asymmetric_keys/pkcs7_trust.c b/crypto/asymmetric_keys/pkcs7_trust.c index 3bbdcc79a3d3..90d6d47965b0 100644 --- a/crypto/asymmetric_keys/pkcs7_trust.c +++ b/crypto/asymmetric_keys/pkcs7_trust.c | |||
@@ -17,7 +17,7 @@ | |||
17 | #include <linux/asn1.h> | 17 | #include <linux/asn1.h> |
18 | #include <linux/key.h> | 18 | #include <linux/key.h> |
19 | #include <keys/asymmetric-type.h> | 19 | #include <keys/asymmetric-type.h> |
20 | #include <crypto/public_key.h> | 20 | #include "public_key.h" |
21 | #include "pkcs7_parser.h" | 21 | #include "pkcs7_parser.h" |
22 | 22 | ||
23 | /** | 23 | /** |
diff --git a/crypto/asymmetric_keys/pkcs7_verify.c b/crypto/asymmetric_keys/pkcs7_verify.c index f5db1378c096..325575caf6b4 100644 --- a/crypto/asymmetric_keys/pkcs7_verify.c +++ b/crypto/asymmetric_keys/pkcs7_verify.c | |||
@@ -16,7 +16,7 @@ | |||
16 | #include <linux/err.h> | 16 | #include <linux/err.h> |
17 | #include <linux/asn1.h> | 17 | #include <linux/asn1.h> |
18 | #include <crypto/hash.h> | 18 | #include <crypto/hash.h> |
19 | #include <crypto/public_key.h> | 19 | #include "public_key.h" |
20 | #include "pkcs7_parser.h" | 20 | #include "pkcs7_parser.h" |
21 | 21 | ||
22 | /* | 22 | /* |
diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c index b383629b9e62..6db4c01c6503 100644 --- a/crypto/asymmetric_keys/public_key.c +++ b/crypto/asymmetric_keys/public_key.c | |||
@@ -18,16 +18,24 @@ | |||
18 | #include <linux/slab.h> | 18 | #include <linux/slab.h> |
19 | #include <linux/seq_file.h> | 19 | #include <linux/seq_file.h> |
20 | #include <keys/asymmetric-subtype.h> | 20 | #include <keys/asymmetric-subtype.h> |
21 | #include <crypto/public_key.h> | 21 | #include "public_key.h" |
22 | 22 | ||
23 | MODULE_LICENSE("GPL"); | 23 | MODULE_LICENSE("GPL"); |
24 | 24 | ||
25 | const char *const pkey_algo_name[PKEY_ALGO__LAST] = { | 25 | const char *const pkey_algo_name[PKEY_ALGO__LAST] = { |
26 | [PKEY_ALGO_DSA] = "dsa", | 26 | [PKEY_ALGO_DSA] = "DSA", |
27 | [PKEY_ALGO_RSA] = "rsa", | 27 | [PKEY_ALGO_RSA] = "RSA", |
28 | }; | 28 | }; |
29 | EXPORT_SYMBOL_GPL(pkey_algo_name); | 29 | EXPORT_SYMBOL_GPL(pkey_algo_name); |
30 | 30 | ||
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 | |||
31 | const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = { | 39 | const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = { |
32 | [PKEY_ID_PGP] = "PGP", | 40 | [PKEY_ID_PGP] = "PGP", |
33 | [PKEY_ID_X509] = "X509", | 41 | [PKEY_ID_X509] = "X509", |
@@ -35,12 +43,6 @@ const char *const pkey_id_type_name[PKEY_ID_TYPE__LAST] = { | |||
35 | }; | 43 | }; |
36 | EXPORT_SYMBOL_GPL(pkey_id_type_name); | 44 | EXPORT_SYMBOL_GPL(pkey_id_type_name); |
37 | 45 | ||
38 | static int (*alg_verify[PKEY_ALGO__LAST])(const struct public_key *pkey, | ||
39 | const struct public_key_signature *sig) = { | ||
40 | NULL, | ||
41 | rsa_verify_signature | ||
42 | }; | ||
43 | |||
44 | /* | 46 | /* |
45 | * Provide a part of a description of the key for /proc/keys. | 47 | * Provide a part of a description of the key for /proc/keys. |
46 | */ | 48 | */ |
@@ -51,8 +53,7 @@ static void public_key_describe(const struct key *asymmetric_key, | |||
51 | 53 | ||
52 | if (key) | 54 | if (key) |
53 | seq_printf(m, "%s.%s", | 55 | seq_printf(m, "%s.%s", |
54 | pkey_id_type_name[key->id_type], | 56 | pkey_id_type_name[key->id_type], key->algo->name); |
55 | pkey_algo_name[key->pkey_algo]); | ||
56 | } | 57 | } |
57 | 58 | ||
58 | /* | 59 | /* |
@@ -61,31 +62,50 @@ static void public_key_describe(const struct key *asymmetric_key, | |||
61 | void public_key_destroy(void *payload) | 62 | void public_key_destroy(void *payload) |
62 | { | 63 | { |
63 | struct public_key *key = payload; | 64 | struct public_key *key = payload; |
65 | int i; | ||
64 | 66 | ||
65 | if (key) | 67 | if (key) { |
66 | kfree(key->key); | 68 | for (i = 0; i < ARRAY_SIZE(key->mpi); i++) |
67 | kfree(key); | 69 | mpi_free(key->mpi[i]); |
70 | kfree(key); | ||
71 | } | ||
68 | } | 72 | } |
69 | EXPORT_SYMBOL_GPL(public_key_destroy); | 73 | EXPORT_SYMBOL_GPL(public_key_destroy); |
70 | 74 | ||
71 | /* | 75 | /* |
72 | * Verify a signature using a public key. | 76 | * Verify a signature using a public key. |
73 | */ | 77 | */ |
74 | int public_key_verify_signature(const struct public_key *pkey, | 78 | int public_key_verify_signature(const struct public_key *pk, |
75 | const struct public_key_signature *sig) | 79 | const struct public_key_signature *sig) |
76 | { | 80 | { |
77 | BUG_ON(!pkey); | 81 | const struct public_key_algorithm *algo; |
82 | |||
83 | BUG_ON(!pk); | ||
84 | BUG_ON(!pk->mpi[0]); | ||
85 | BUG_ON(!pk->mpi[1]); | ||
78 | BUG_ON(!sig); | 86 | BUG_ON(!sig); |
79 | BUG_ON(!sig->digest); | 87 | BUG_ON(!sig->digest); |
80 | BUG_ON(!sig->s); | 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 | } | ||
81 | 98 | ||
82 | if (pkey->pkey_algo >= PKEY_ALGO__LAST) | 99 | if (!algo->verify_signature) |
83 | return -ENOPKG; | 100 | return -ENOTSUPP; |
84 | 101 | ||
85 | if (!alg_verify[pkey->pkey_algo]) | 102 | if (sig->nr_mpi != algo->n_sig_mpi) { |
86 | return -ENOPKG; | 103 | pr_debug("Signature has %u MPI not %u\n", |
104 | sig->nr_mpi, algo->n_sig_mpi); | ||
105 | return -EINVAL; | ||
106 | } | ||
87 | 107 | ||
88 | return alg_verify[pkey->pkey_algo](pkey, sig); | 108 | return algo->verify_signature(pk, sig); |
89 | } | 109 | } |
90 | EXPORT_SYMBOL_GPL(public_key_verify_signature); | 110 | EXPORT_SYMBOL_GPL(public_key_verify_signature); |
91 | 111 | ||
diff --git a/crypto/asymmetric_keys/public_key.h b/crypto/asymmetric_keys/public_key.h new file mode 100644 index 000000000000..5c37a22a0637 --- /dev/null +++ b/crypto/asymmetric_keys/public_key.h | |||
@@ -0,0 +1,36 @@ | |||
1 | /* Public key algorithm internals | ||
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 | #include <crypto/public_key.h> | ||
15 | |||
16 | extern struct asymmetric_key_subtype public_key_subtype; | ||
17 | |||
18 | /* | ||
19 | * Public key algorithm definition. | ||
20 | */ | ||
21 | struct public_key_algorithm { | ||
22 | const char *name; | ||
23 | u8 n_pub_mpi; /* Number of MPIs in public key */ | ||
24 | u8 n_sec_mpi; /* Number of MPIs in secret key */ | ||
25 | u8 n_sig_mpi; /* Number of MPIs in a signature */ | ||
26 | int (*verify_signature)(const struct public_key *key, | ||
27 | const struct public_key_signature *sig); | ||
28 | }; | ||
29 | |||
30 | extern const struct public_key_algorithm RSA_public_key_algorithm; | ||
31 | |||
32 | /* | ||
33 | * public_key.c | ||
34 | */ | ||
35 | extern int public_key_verify_signature(const struct public_key *pk, | ||
36 | const struct public_key_signature *sig); | ||
diff --git a/crypto/asymmetric_keys/rsa.c b/crypto/asymmetric_keys/rsa.c index 8b08ffcdb809..508b57b77474 100644 --- a/crypto/asymmetric_keys/rsa.c +++ b/crypto/asymmetric_keys/rsa.c | |||
@@ -11,10 +11,10 @@ | |||
11 | 11 | ||
12 | #define pr_fmt(fmt) "RSA: "fmt | 12 | #define pr_fmt(fmt) "RSA: "fmt |
13 | #include <linux/module.h> | 13 | #include <linux/module.h> |
14 | #include <linux/kernel.h> | ||
14 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
15 | #include <crypto/akcipher.h> | ||
16 | #include <crypto/public_key.h> | ||
17 | #include <crypto/algapi.h> | 16 | #include <crypto/algapi.h> |
17 | #include "public_key.h" | ||
18 | 18 | ||
19 | MODULE_LICENSE("GPL"); | 19 | MODULE_LICENSE("GPL"); |
20 | MODULE_DESCRIPTION("RSA Public Key Algorithm"); | 20 | MODULE_DESCRIPTION("RSA Public Key Algorithm"); |
@@ -84,10 +84,72 @@ static const struct { | |||
84 | #undef _ | 84 | #undef _ |
85 | }; | 85 | }; |
86 | 86 | ||
87 | struct rsa_completion { | 87 | /* |
88 | struct completion completion; | 88 | * RSAVP1() function [RFC3447 sec 5.2.2] |
89 | int err; | 89 | */ |
90 | }; | 90 | static int RSAVP1(const struct public_key *key, MPI s, MPI *_m) |
91 | { | ||
92 | MPI m; | ||
93 | int ret; | ||
94 | |||
95 | /* (1) Validate 0 <= s < n */ | ||
96 | if (mpi_cmp_ui(s, 0) < 0) { | ||
97 | kleave(" = -EBADMSG [s < 0]"); | ||
98 | return -EBADMSG; | ||
99 | } | ||
100 | if (mpi_cmp(s, key->rsa.n) >= 0) { | ||
101 | kleave(" = -EBADMSG [s >= n]"); | ||
102 | return -EBADMSG; | ||
103 | } | ||
104 | |||
105 | m = mpi_alloc(0); | ||
106 | if (!m) | ||
107 | return -ENOMEM; | ||
108 | |||
109 | /* (2) m = s^e mod n */ | ||
110 | ret = mpi_powm(m, s, key->rsa.e, key->rsa.n); | ||
111 | if (ret < 0) { | ||
112 | mpi_free(m); | ||
113 | return ret; | ||
114 | } | ||
115 | |||
116 | *_m = m; | ||
117 | return 0; | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * Integer to Octet String conversion [RFC3447 sec 4.1] | ||
122 | */ | ||
123 | static int RSA_I2OSP(MPI x, size_t xLen, u8 **pX) | ||
124 | { | ||
125 | unsigned X_size, x_size; | ||
126 | int X_sign; | ||
127 | u8 *X; | ||
128 | |||
129 | /* Make sure the string is the right length. The number should begin | ||
130 | * with { 0x00, 0x01, ... } so we have to account for 15 leading zero | ||
131 | * bits not being reported by MPI. | ||
132 | */ | ||
133 | x_size = mpi_get_nbits(x); | ||
134 | pr_devel("size(x)=%u xLen*8=%zu\n", x_size, xLen * 8); | ||
135 | if (x_size != xLen * 8 - 15) | ||
136 | return -ERANGE; | ||
137 | |||
138 | X = mpi_get_buffer(x, &X_size, &X_sign); | ||
139 | if (!X) | ||
140 | return -ENOMEM; | ||
141 | if (X_sign < 0) { | ||
142 | kfree(X); | ||
143 | return -EBADMSG; | ||
144 | } | ||
145 | if (X_size != xLen - 1) { | ||
146 | kfree(X); | ||
147 | return -EBADMSG; | ||
148 | } | ||
149 | |||
150 | *pX = X; | ||
151 | return 0; | ||
152 | } | ||
91 | 153 | ||
92 | /* | 154 | /* |
93 | * Perform the RSA signature verification. | 155 | * Perform the RSA signature verification. |
@@ -98,7 +160,7 @@ struct rsa_completion { | |||
98 | * @asn1_template: The DigestInfo ASN.1 template | 160 | * @asn1_template: The DigestInfo ASN.1 template |
99 | * @asn1_size: Size of asm1_template[] | 161 | * @asn1_size: Size of asm1_template[] |
100 | */ | 162 | */ |
101 | static int rsa_verify(const u8 *H, const u8 *EM, size_t k, size_t hash_size, | 163 | static int RSA_verify(const u8 *H, const u8 *EM, size_t k, size_t hash_size, |
102 | const u8 *asn1_template, size_t asn1_size) | 164 | const u8 *asn1_template, size_t asn1_size) |
103 | { | 165 | { |
104 | unsigned PS_end, T_offset, i; | 166 | unsigned PS_end, T_offset, i; |
@@ -107,10 +169,10 @@ static int rsa_verify(const u8 *H, const u8 *EM, size_t k, size_t hash_size, | |||
107 | 169 | ||
108 | if (k < 2 + 1 + asn1_size + hash_size) | 170 | if (k < 2 + 1 + asn1_size + hash_size) |
109 | return -EBADMSG; | 171 | return -EBADMSG; |
110 | /* Decode the EMSA-PKCS1-v1_5 | 172 | |
111 | * note: leading zeros are stirpped by the RSA implementation */ | 173 | /* Decode the EMSA-PKCS1-v1_5 */ |
112 | if (EM[0] != 0x01) { | 174 | if (EM[1] != 0x01) { |
113 | kleave(" = -EBADMSG [EM[0] == %02u]", EM[0]); | 175 | kleave(" = -EBADMSG [EM[1] == %02u]", EM[1]); |
114 | return -EBADMSG; | 176 | return -EBADMSG; |
115 | } | 177 | } |
116 | 178 | ||
@@ -121,7 +183,7 @@ static int rsa_verify(const u8 *H, const u8 *EM, size_t k, size_t hash_size, | |||
121 | return -EBADMSG; | 183 | return -EBADMSG; |
122 | } | 184 | } |
123 | 185 | ||
124 | for (i = 1; i < PS_end; i++) { | 186 | for (i = 2; i < PS_end; i++) { |
125 | if (EM[i] != 0xff) { | 187 | if (EM[i] != 0xff) { |
126 | kleave(" = -EBADMSG [EM[PS%x] == %02u]", i - 2, EM[i]); | 188 | kleave(" = -EBADMSG [EM[PS%x] == %02u]", i - 2, EM[i]); |
127 | return -EBADMSG; | 189 | return -EBADMSG; |
@@ -142,82 +204,75 @@ static int rsa_verify(const u8 *H, const u8 *EM, size_t k, size_t hash_size, | |||
142 | return 0; | 204 | return 0; |
143 | } | 205 | } |
144 | 206 | ||
145 | static void public_key_verify_done(struct crypto_async_request *req, int err) | 207 | /* |
208 | * Perform the verification step [RFC3447 sec 8.2.2]. | ||
209 | */ | ||
210 | static int RSA_verify_signature(const struct public_key *key, | ||
211 | const struct public_key_signature *sig) | ||
146 | { | 212 | { |
147 | struct rsa_completion *compl = req->data; | 213 | size_t tsize; |
214 | int ret; | ||
148 | 215 | ||
149 | if (err == -EINPROGRESS) | 216 | /* Variables as per RFC3447 sec 8.2.2 */ |
150 | return; | 217 | const u8 *H = sig->digest; |
218 | u8 *EM = NULL; | ||
219 | MPI m = NULL; | ||
220 | size_t k; | ||
151 | 221 | ||
152 | compl->err = err; | 222 | kenter(""); |
153 | complete(&compl->completion); | ||
154 | } | ||
155 | 223 | ||
156 | int rsa_verify_signature(const struct public_key *pkey, | 224 | if (!RSA_ASN1_templates[sig->pkey_hash_algo].data) |
157 | const struct public_key_signature *sig) | 225 | return -ENOTSUPP; |
158 | { | 226 | |
159 | struct crypto_akcipher *tfm; | 227 | /* (1) Check the signature size against the public key modulus size */ |
160 | struct akcipher_request *req; | 228 | k = mpi_get_nbits(key->rsa.n); |
161 | struct rsa_completion compl; | 229 | tsize = mpi_get_nbits(sig->rsa.s); |
162 | struct scatterlist sig_sg, sg_out; | 230 | |
163 | void *outbuf = NULL; | 231 | /* According to RFC 4880 sec 3.2, length of MPI is computed starting |
164 | unsigned int outlen = 0; | 232 | * from most significant bit. So the RFC 3447 sec 8.2.2 size check |
165 | int ret = -ENOMEM; | 233 | * must be relaxed to conform with shorter signatures - so we fail here |
166 | 234 | * only if signature length is longer than modulus size. | |
167 | tfm = crypto_alloc_akcipher("rsa", 0, 0); | 235 | */ |
168 | if (IS_ERR(tfm)) | 236 | pr_devel("step 1: k=%zu size(S)=%zu\n", k, tsize); |
169 | goto error_out; | 237 | if (k < tsize) { |
170 | 238 | ret = -EBADMSG; | |
171 | req = akcipher_request_alloc(tfm, GFP_KERNEL); | 239 | goto error; |
172 | if (!req) | ||
173 | goto error_free_tfm; | ||
174 | |||
175 | ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen); | ||
176 | if (ret) | ||
177 | goto error_free_req; | ||
178 | |||
179 | ret = -EINVAL; | ||
180 | outlen = crypto_akcipher_maxsize(tfm); | ||
181 | if (!outlen) | ||
182 | goto error_free_req; | ||
183 | |||
184 | /* initlialzie out buf */ | ||
185 | ret = -ENOMEM; | ||
186 | outbuf = kmalloc(outlen, GFP_KERNEL); | ||
187 | if (!outbuf) | ||
188 | goto error_free_req; | ||
189 | |||
190 | sg_init_one(&sig_sg, sig->s, sig->s_size); | ||
191 | sg_init_one(&sg_out, outbuf, outlen); | ||
192 | akcipher_request_set_crypt(req, &sig_sg, &sg_out, sig->s_size, outlen); | ||
193 | init_completion(&compl.completion); | ||
194 | akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG | | ||
195 | CRYPTO_TFM_REQ_MAY_SLEEP, | ||
196 | public_key_verify_done, &compl); | ||
197 | |||
198 | ret = crypto_akcipher_verify(req); | ||
199 | if (ret == -EINPROGRESS) { | ||
200 | wait_for_completion(&compl.completion); | ||
201 | ret = compl.err; | ||
202 | } | 240 | } |
203 | 241 | ||
204 | if (ret) | 242 | /* Round up and convert to octets */ |
205 | goto error_free_req; | 243 | k = (k + 7) / 8; |
206 | 244 | ||
207 | /* | 245 | /* (2b) Apply the RSAVP1 verification primitive to the public key */ |
208 | * Output from the operation is an encoded message (EM) of | 246 | ret = RSAVP1(key, sig->rsa.s, &m); |
209 | * length k octets. | 247 | if (ret < 0) |
248 | goto error; | ||
249 | |||
250 | /* (2c) Convert the message representative (m) to an encoded message | ||
251 | * (EM) of length k octets. | ||
252 | * | ||
253 | * NOTE! The leading zero byte is suppressed by MPI, so we pass a | ||
254 | * pointer to the _preceding_ byte to RSA_verify()! | ||
210 | */ | 255 | */ |
211 | outlen = req->dst_len; | 256 | ret = RSA_I2OSP(m, k, &EM); |
212 | ret = rsa_verify(sig->digest, outbuf, outlen, sig->digest_size, | 257 | if (ret < 0) |
258 | goto error; | ||
259 | |||
260 | ret = RSA_verify(H, EM - 1, k, sig->digest_size, | ||
213 | RSA_ASN1_templates[sig->pkey_hash_algo].data, | 261 | RSA_ASN1_templates[sig->pkey_hash_algo].data, |
214 | RSA_ASN1_templates[sig->pkey_hash_algo].size); | 262 | RSA_ASN1_templates[sig->pkey_hash_algo].size); |
215 | error_free_req: | 263 | |
216 | akcipher_request_free(req); | 264 | error: |
217 | error_free_tfm: | 265 | kfree(EM); |
218 | crypto_free_akcipher(tfm); | 266 | mpi_free(m); |
219 | error_out: | 267 | kleave(" = %d", ret); |
220 | kfree(outbuf); | ||
221 | return ret; | 268 | return ret; |
222 | } | 269 | } |
223 | EXPORT_SYMBOL_GPL(rsa_verify_signature); | 270 | |
271 | const struct public_key_algorithm RSA_public_key_algorithm = { | ||
272 | .name = "RSA", | ||
273 | .n_pub_mpi = 2, | ||
274 | .n_sec_mpi = 3, | ||
275 | .n_sig_mpi = 1, | ||
276 | .verify_signature = RSA_verify_signature, | ||
277 | }; | ||
278 | EXPORT_SYMBOL_GPL(RSA_public_key_algorithm); | ||
diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c index 7502029e3385..021d39c0ba75 100644 --- a/crypto/asymmetric_keys/x509_cert_parser.c +++ b/crypto/asymmetric_keys/x509_cert_parser.c | |||
@@ -15,10 +15,11 @@ | |||
15 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
16 | #include <linux/err.h> | 16 | #include <linux/err.h> |
17 | #include <linux/oid_registry.h> | 17 | #include <linux/oid_registry.h> |
18 | #include <crypto/public_key.h> | 18 | #include "public_key.h" |
19 | #include "x509_parser.h" | 19 | #include "x509_parser.h" |
20 | #include "x509-asn1.h" | 20 | #include "x509-asn1.h" |
21 | #include "x509_akid-asn1.h" | 21 | #include "x509_akid-asn1.h" |
22 | #include "x509_rsakey-asn1.h" | ||
22 | 23 | ||
23 | struct x509_parse_context { | 24 | struct x509_parse_context { |
24 | struct x509_certificate *cert; /* Certificate being constructed */ | 25 | struct x509_certificate *cert; /* Certificate being constructed */ |
@@ -55,7 +56,7 @@ void x509_free_certificate(struct x509_certificate *cert) | |||
55 | kfree(cert->akid_id); | 56 | kfree(cert->akid_id); |
56 | kfree(cert->akid_skid); | 57 | kfree(cert->akid_skid); |
57 | kfree(cert->sig.digest); | 58 | kfree(cert->sig.digest); |
58 | kfree(cert->sig.s); | 59 | mpi_free(cert->sig.rsa.s); |
59 | kfree(cert); | 60 | kfree(cert); |
60 | } | 61 | } |
61 | } | 62 | } |
@@ -102,12 +103,12 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) | |||
102 | } | 103 | } |
103 | } | 104 | } |
104 | 105 | ||
105 | cert->pub->key = kmemdup(ctx->key, ctx->key_size, GFP_KERNEL); | 106 | /* Decode the public key */ |
106 | if (!cert->pub->key) | 107 | ret = asn1_ber_decoder(&x509_rsakey_decoder, ctx, |
108 | ctx->key, ctx->key_size); | ||
109 | if (ret < 0) | ||
107 | goto error_decode; | 110 | goto error_decode; |
108 | 111 | ||
109 | cert->pub->keylen = ctx->key_size; | ||
110 | |||
111 | /* Generate cert issuer + serial number key ID */ | 112 | /* Generate cert issuer + serial number key ID */ |
112 | kid = asymmetric_key_generate_id(cert->raw_serial, | 113 | kid = asymmetric_key_generate_id(cert->raw_serial, |
113 | cert->raw_serial_size, | 114 | cert->raw_serial_size, |
@@ -123,7 +124,6 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) | |||
123 | return cert; | 124 | return cert; |
124 | 125 | ||
125 | error_decode: | 126 | error_decode: |
126 | kfree(cert->pub->key); | ||
127 | kfree(ctx); | 127 | kfree(ctx); |
128 | error_no_ctx: | 128 | error_no_ctx: |
129 | x509_free_certificate(cert); | 129 | x509_free_certificate(cert); |
@@ -404,6 +404,29 @@ int x509_extract_key_data(void *context, size_t hdrlen, | |||
404 | return 0; | 404 | return 0; |
405 | } | 405 | } |
406 | 406 | ||
407 | /* | ||
408 | * Extract a RSA public key value | ||
409 | */ | ||
410 | int rsa_extract_mpi(void *context, size_t hdrlen, | ||
411 | unsigned char tag, | ||
412 | const void *value, size_t vlen) | ||
413 | { | ||
414 | struct x509_parse_context *ctx = context; | ||
415 | MPI mpi; | ||
416 | |||
417 | if (ctx->nr_mpi >= ARRAY_SIZE(ctx->cert->pub->mpi)) { | ||
418 | pr_err("Too many public key MPIs in certificate\n"); | ||
419 | return -EBADMSG; | ||
420 | } | ||
421 | |||
422 | mpi = mpi_read_raw_data(value, vlen); | ||
423 | if (!mpi) | ||
424 | return -ENOMEM; | ||
425 | |||
426 | ctx->cert->pub->mpi[ctx->nr_mpi++] = mpi; | ||
427 | return 0; | ||
428 | } | ||
429 | |||
407 | /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */ | 430 | /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */ |
408 | #define SEQ_TAG_KEYID (ASN1_CONT << 6) | 431 | #define SEQ_TAG_KEYID (ASN1_CONT << 6) |
409 | 432 | ||
diff --git a/crypto/asymmetric_keys/x509_public_key.c b/crypto/asymmetric_keys/x509_public_key.c index 7092d5cbb5d3..9e9e5a6a9ed6 100644 --- a/crypto/asymmetric_keys/x509_public_key.c +++ b/crypto/asymmetric_keys/x509_public_key.c | |||
@@ -13,11 +13,15 @@ | |||
13 | #include <linux/module.h> | 13 | #include <linux/module.h> |
14 | #include <linux/kernel.h> | 14 | #include <linux/kernel.h> |
15 | #include <linux/slab.h> | 15 | #include <linux/slab.h> |
16 | #include <linux/err.h> | ||
17 | #include <linux/mpi.h> | ||
18 | #include <linux/asn1_decoder.h> | ||
16 | #include <keys/asymmetric-subtype.h> | 19 | #include <keys/asymmetric-subtype.h> |
17 | #include <keys/asymmetric-parser.h> | 20 | #include <keys/asymmetric-parser.h> |
18 | #include <keys/system_keyring.h> | 21 | #include <keys/system_keyring.h> |
19 | #include <crypto/hash.h> | 22 | #include <crypto/hash.h> |
20 | #include "asymmetric_keys.h" | 23 | #include "asymmetric_keys.h" |
24 | #include "public_key.h" | ||
21 | #include "x509_parser.h" | 25 | #include "x509_parser.h" |
22 | 26 | ||
23 | static bool use_builtin_keys; | 27 | static bool use_builtin_keys; |
@@ -163,15 +167,13 @@ int x509_get_sig_params(struct x509_certificate *cert) | |||
163 | 167 | ||
164 | if (cert->unsupported_crypto) | 168 | if (cert->unsupported_crypto) |
165 | return -ENOPKG; | 169 | return -ENOPKG; |
166 | if (cert->sig.s) | 170 | if (cert->sig.rsa.s) |
167 | return 0; | 171 | return 0; |
168 | 172 | ||
169 | cert->sig.s = kmemdup(cert->raw_sig, cert->raw_sig_size, | 173 | cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size); |
170 | GFP_KERNEL); | 174 | if (!cert->sig.rsa.s) |
171 | if (!cert->sig.s) | ||
172 | return -ENOMEM; | 175 | return -ENOMEM; |
173 | 176 | cert->sig.nr_mpi = 1; | |
174 | cert->sig.s_size = cert->raw_sig_size; | ||
175 | 177 | ||
176 | /* Allocate the hashing algorithm we're going to need and find out how | 178 | /* Allocate the hashing algorithm we're going to need and find out how |
177 | * big the hash operational data will be. | 179 | * big the hash operational data will be. |
@@ -294,6 +296,8 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) | |||
294 | if (cert->pub->pkey_algo >= PKEY_ALGO__LAST || | 296 | if (cert->pub->pkey_algo >= PKEY_ALGO__LAST || |
295 | cert->sig.pkey_algo >= PKEY_ALGO__LAST || | 297 | cert->sig.pkey_algo >= PKEY_ALGO__LAST || |
296 | cert->sig.pkey_hash_algo >= PKEY_HASH__LAST || | 298 | cert->sig.pkey_hash_algo >= PKEY_HASH__LAST || |
299 | !pkey_algo[cert->pub->pkey_algo] || | ||
300 | !pkey_algo[cert->sig.pkey_algo] || | ||
297 | !hash_algo_name[cert->sig.pkey_hash_algo]) { | 301 | !hash_algo_name[cert->sig.pkey_hash_algo]) { |
298 | ret = -ENOPKG; | 302 | ret = -ENOPKG; |
299 | goto error_free_cert; | 303 | goto error_free_cert; |
@@ -305,6 +309,7 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) | |||
305 | pkey_algo_name[cert->sig.pkey_algo], | 309 | pkey_algo_name[cert->sig.pkey_algo], |
306 | hash_algo_name[cert->sig.pkey_hash_algo]); | 310 | hash_algo_name[cert->sig.pkey_hash_algo]); |
307 | 311 | ||
312 | cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; | ||
308 | cert->pub->id_type = PKEY_ID_X509; | 313 | cert->pub->id_type = PKEY_ID_X509; |
309 | 314 | ||
310 | /* Check the signature on the key if it appears to be self-signed */ | 315 | /* Check the signature on the key if it appears to be self-signed */ |
diff --git a/crypto/asymmetric_keys/x509_rsakey.asn1 b/crypto/asymmetric_keys/x509_rsakey.asn1 new file mode 100644 index 000000000000..4ec7cc6532c1 --- /dev/null +++ b/crypto/asymmetric_keys/x509_rsakey.asn1 | |||
@@ -0,0 +1,4 @@ | |||
1 | RSAPublicKey ::= SEQUENCE { | ||
2 | modulus INTEGER ({ rsa_extract_mpi }), -- n | ||
3 | publicExponent INTEGER ({ rsa_extract_mpi }) -- e | ||
4 | } | ||