| 1 |
From 9af4db9205484f5a30bbf360d045175f38137e8d Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephan Mueller <[email protected]> |
| 3 |
Date: Fri, 29 Sep 2017 01:27:53 +0200 |
| 4 |
Subject: [PATCH v2 6/8] crypto: DH - add PKCS#3 parameter handling |
| 5 |
|
| 6 |
DH parameters are commonly handled in PKCS#3 ASN.1 DER encoded files. |
| 7 |
The addition adds support for the parsing of such DER encoded parameter |
| 8 |
sets. After parsing, the data is added to the DH context data structure. |
| 9 |
|
| 10 |
This support allows using of parameter sets generated with the openssl |
| 11 |
dhparam command. |
| 12 |
|
| 13 |
Note, the privateValueLength parameter defined in PKCS#3 is not parsed |
| 14 |
as it is not required for the DH operation. |
| 15 |
|
| 16 |
The PKCS#3 parser is able to parse data created by other tools, such as: |
| 17 |
|
| 18 |
openssl dhparam -outform DER -out dhparam.der 2048 |
| 19 |
|
| 20 |
Signed-off-by: Stephan Mueller <[email protected]> |
| 21 |
--- |
| 22 |
crypto/Makefile | 7 ++++++- |
| 23 |
crypto/dh.c | 30 ++++++++++++++++++++++++---- |
| 24 |
crypto/dh_helper.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++--- |
| 25 |
crypto/dhparameter.asn1 | 4 ++++ |
| 26 |
include/crypto/dh.h | 21 +++++++++++++++++--- |
| 27 |
5 files changed, 104 insertions(+), 11 deletions(-) |
| 28 |
create mode 100644 crypto/dhparameter.asn1 |
| 29 |
|
| 30 |
diff --git a/crypto/Makefile b/crypto/Makefile |
| 31 |
index b9b86934fd3e..d0e72df0f2d1 100644 |
| 32 |
--- a/crypto/Makefile |
| 33 |
+++ b/crypto/Makefile |
| 34 |
@@ -30,7 +30,12 @@ obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o |
| 35 |
obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o |
| 36 |
obj-$(CONFIG_CRYPTO_KPP2) += kpp.o |
| 37 |
|
| 38 |
-dh_generic-y := dh.o |
| 39 |
+$(obj)/dhparameter-asn1.o: $(obj)/dhparameter-asn1.c $(obj)/dhparameter-asn1.h |
| 40 |
+$(obj)/dh_helper.o: $(obj)/dhparameter-asn1.h |
| 41 |
+clean-files += dhparameter-asn1.c dhparameter-asn1.h |
| 42 |
+ |
| 43 |
+dh_generic-y := dhparameter-asn1.o |
| 44 |
+dh_generic-y += dh.o |
| 45 |
dh_generic-y += dh_helper.o |
| 46 |
obj-$(CONFIG_CRYPTO_DH) += dh_generic.o |
| 47 |
|
| 48 |
diff --git a/crypto/dh.c b/crypto/dh.c |
| 49 |
index 02abaad3f668..9aa77cac7927 100644 |
| 50 |
--- a/crypto/dh.c |
| 51 |
+++ b/crypto/dh.c |
| 52 |
@@ -29,13 +29,18 @@ static inline void dh_clear_params(struct dh_ctx *ctx) |
| 53 |
ctx->g = NULL; |
| 54 |
} |
| 55 |
|
| 56 |
-static void dh_free_ctx(struct dh_ctx *ctx) |
| 57 |
+static inline void dh_clear_key(struct dh_ctx *ctx) |
| 58 |
{ |
| 59 |
- dh_clear_params(ctx); |
| 60 |
mpi_free(ctx->xa); |
| 61 |
ctx->xa = NULL; |
| 62 |
} |
| 63 |
|
| 64 |
+static void dh_free_ctx(struct dh_ctx *ctx) |
| 65 |
+{ |
| 66 |
+ dh_clear_params(ctx); |
| 67 |
+ dh_clear_key(ctx); |
| 68 |
+} |
| 69 |
+ |
| 70 |
/* |
| 71 |
* If base is g we compute the public key |
| 72 |
* ya = g^xa mod p; [RFC2631 sec 2.1.1] |
| 73 |
@@ -83,6 +88,23 @@ static int dh_set_params(struct dh_ctx *ctx, struct dh *params) |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
+static int dh_set_params_pkcs3(struct crypto_kpp *tfm, const void *param, |
| 78 |
+ unsigned int param_len) |
| 79 |
+{ |
| 80 |
+ struct dh_ctx *ctx = dh_get_ctx(tfm); |
| 81 |
+ struct dh parsed_params; |
| 82 |
+ int ret; |
| 83 |
+ |
| 84 |
+ /* Free the old parameter if any */ |
| 85 |
+ dh_clear_params(ctx); |
| 86 |
+ |
| 87 |
+ ret = dh_parse_params_pkcs3(&parsed_params, param, param_len); |
| 88 |
+ if (ret) |
| 89 |
+ return ret; |
| 90 |
+ |
| 91 |
+ return dh_set_params(ctx, &parsed_params); |
| 92 |
+} |
| 93 |
+ |
| 94 |
static int dh_set_secret(struct crypto_kpp *tfm, const void *buf, |
| 95 |
unsigned int len) |
| 96 |
{ |
| 97 |
@@ -90,7 +112,7 @@ static int dh_set_secret(struct crypto_kpp *tfm, const void *buf, |
| 98 |
struct dh params; |
| 99 |
|
| 100 |
/* Free the old MPI key if any */ |
| 101 |
- dh_free_ctx(ctx); |
| 102 |
+ dh_clear_key(ctx); |
| 103 |
|
| 104 |
if (crypto_dh_decode_key(buf, len, ¶ms) < 0) |
| 105 |
return -EINVAL; |
| 106 |
@@ -114,7 +136,6 @@ static int dh_compute_value(struct kpp_request *req) |
| 107 |
MPI base, val = mpi_alloc(0); |
| 108 |
int ret = 0; |
| 109 |
int sign; |
| 110 |
- |
| 111 |
if (!val) |
| 112 |
return -ENOMEM; |
| 113 |
|
| 114 |
@@ -166,6 +187,7 @@ static void dh_exit_tfm(struct crypto_kpp *tfm) |
| 115 |
} |
| 116 |
|
| 117 |
static struct kpp_alg dh = { |
| 118 |
+ .set_params = dh_set_params_pkcs3, |
| 119 |
.set_secret = dh_set_secret, |
| 120 |
.generate_public_key = dh_compute_value, |
| 121 |
.compute_shared_secret = dh_compute_value, |
| 122 |
diff --git a/crypto/dh_helper.c b/crypto/dh_helper.c |
| 123 |
index 8ba8a3f82620..ecc87a9f589e 100644 |
| 124 |
--- a/crypto/dh_helper.c |
| 125 |
+++ b/crypto/dh_helper.c |
| 126 |
@@ -13,6 +13,7 @@ |
| 127 |
#include <linux/string.h> |
| 128 |
#include <crypto/dh.h> |
| 129 |
#include <crypto/kpp.h> |
| 130 |
+#include "dhparameter-asn1.h" |
| 131 |
|
| 132 |
#define DH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 3 * sizeof(int)) |
| 133 |
|
| 134 |
@@ -53,13 +54,22 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params) |
| 135 |
if (len != crypto_dh_key_len(params)) |
| 136 |
return -EINVAL; |
| 137 |
|
| 138 |
+ /* Prevention of out-of-bounds access in decode code path */ |
| 139 |
+ if ((!params->key && params->key_size) || |
| 140 |
+ (!params->p && params->p_size) || |
| 141 |
+ (!params->g && params->g_size)) |
| 142 |
+ return -EINVAL; |
| 143 |
+ |
| 144 |
ptr = dh_pack_data(ptr, &secret, sizeof(secret)); |
| 145 |
ptr = dh_pack_data(ptr, ¶ms->key_size, sizeof(params->key_size)); |
| 146 |
ptr = dh_pack_data(ptr, ¶ms->p_size, sizeof(params->p_size)); |
| 147 |
ptr = dh_pack_data(ptr, ¶ms->g_size, sizeof(params->g_size)); |
| 148 |
- ptr = dh_pack_data(ptr, params->key, params->key_size); |
| 149 |
- ptr = dh_pack_data(ptr, params->p, params->p_size); |
| 150 |
- dh_pack_data(ptr, params->g, params->g_size); |
| 151 |
+ if (params->key) |
| 152 |
+ ptr = dh_pack_data(ptr, params->key, params->key_size); |
| 153 |
+ if (params->p) |
| 154 |
+ ptr = dh_pack_data(ptr, params->p, params->p_size); |
| 155 |
+ if (params->g) |
| 156 |
+ dh_pack_data(ptr, params->g, params->g_size); |
| 157 |
|
| 158 |
return 0; |
| 159 |
} |
| 160 |
@@ -93,3 +103,40 @@ int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params) |
| 161 |
return 0; |
| 162 |
} |
| 163 |
EXPORT_SYMBOL_GPL(crypto_dh_decode_key); |
| 164 |
+ |
| 165 |
+int dh_get_p(void *context, size_t hdrlen, unsigned char tag, const void *value, |
| 166 |
+ size_t vlen) |
| 167 |
+{ |
| 168 |
+ struct dh *dh = context; |
| 169 |
+ |
| 170 |
+ /* invalid key provided */ |
| 171 |
+ if (!value || !vlen) |
| 172 |
+ return -EINVAL; |
| 173 |
+ |
| 174 |
+ dh->p = value; |
| 175 |
+ dh->p_size = vlen; |
| 176 |
+ |
| 177 |
+ return 0; |
| 178 |
+} |
| 179 |
+ |
| 180 |
+int dh_get_g(void *context, size_t hdrlen, unsigned char tag, |
| 181 |
+ const void *value, size_t vlen) |
| 182 |
+{ |
| 183 |
+ struct dh *dh = context; |
| 184 |
+ |
| 185 |
+ /* invalid base provided */ |
| 186 |
+ if (!value || !dh->p_size || !vlen || vlen > dh->p_size) |
| 187 |
+ return -EINVAL; |
| 188 |
+ |
| 189 |
+ dh->g = value; |
| 190 |
+ dh->g_size = vlen; |
| 191 |
+ |
| 192 |
+ return 0; |
| 193 |
+} |
| 194 |
+ |
| 195 |
+int dh_parse_params_pkcs3(struct dh *dh, const void *param, |
| 196 |
+ unsigned int param_len) |
| 197 |
+{ |
| 198 |
+ return asn1_ber_decoder(&dhparameter_decoder, dh, param, param_len); |
| 199 |
+} |
| 200 |
+EXPORT_SYMBOL_GPL(dh_parse_params_pkcs3); |
| 201 |
diff --git a/crypto/dhparameter.asn1 b/crypto/dhparameter.asn1 |
| 202 |
new file mode 100644 |
| 203 |
index 000000000000..e9107c80478d |
| 204 |
--- /dev/null |
| 205 |
+++ b/crypto/dhparameter.asn1 |
| 206 |
@@ -0,0 +1,4 @@ |
| 207 |
+DHParameter ::= SEQUENCE { |
| 208 |
+ prime INTEGER ({ dh_get_p }), |
| 209 |
+ base INTEGER ({ dh_get_g }) |
| 210 |
+} |
| 211 |
diff --git a/include/crypto/dh.h b/include/crypto/dh.h |
| 212 |
index f638998fb6d0..ea22783323e9 100644 |
| 213 |
--- a/include/crypto/dh.h |
| 214 |
+++ b/include/crypto/dh.h |
| 215 |
@@ -35,9 +35,9 @@ |
| 216 |
* @g_size: Size of DH generator G |
| 217 |
*/ |
| 218 |
struct dh { |
| 219 |
- void *key; |
| 220 |
- void *p; |
| 221 |
- void *g; |
| 222 |
+ const void *key; |
| 223 |
+ const void *p; |
| 224 |
+ const void *g; |
| 225 |
unsigned int key_size; |
| 226 |
unsigned int p_size; |
| 227 |
unsigned int g_size; |
| 228 |
@@ -84,4 +84,19 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params); |
| 229 |
*/ |
| 230 |
int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params); |
| 231 |
|
| 232 |
+/** |
| 233 |
+ * dh_parse_params_pkcs3() - decodes the PKCS#3 BER encoded buffer of the DH |
| 234 |
+ * parameters and stores in the provided struct dh, |
| 235 |
+ * pointers to the raw p and g parameters as is, so |
| 236 |
+ * that the caller can copy it or MPI parse it, etc. |
| 237 |
+ * |
| 238 |
+ * @dh: struct dh representation |
| 239 |
+ * @param: DH parameters in BER format following PKCS#3 |
| 240 |
+ * @param_len: length of parameter buffer |
| 241 |
+ * |
| 242 |
+ * Return: 0 on success or error code in case of error |
| 243 |
+ */ |
| 244 |
+int dh_parse_params_pkcs3(struct dh *dh, const void *param, |
| 245 |
+ unsigned int param_len); |
| 246 |
+ |
| 247 |
#endif |
| 248 |
-- |
| 249 |
2.13.5 |
| 250 |
|