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