2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/evp.h>
16 #include <openssl/bn.h>
17 #include <openssl/dsa.h>
18 #include <openssl/objects.h>
19 #include "crypto/evp.h"
21 /* DH pkey context structure */
24 /* Parameter gen parameters */
30 /* message digest used for parameter generation */
34 /* Keygen callback info */
36 /* KDF (if any) to use for DH */
38 /* OID to use for KDF */
40 /* Message digest to use for key derivation */
42 /* User key material */
43 unsigned char *kdf_ukm;
45 /* KDF output length */
49 static int pkey_dh_init(EVP_PKEY_CTX *ctx)
53 if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL) {
54 DHerr(DH_F_PKEY_DH_INIT, ERR_R_MALLOC_FAILURE);
57 dctx->prime_len = 2048;
58 dctx->subprime_len = -1;
60 dctx->kdf_type = EVP_PKEY_DH_KDF_NONE;
63 ctx->keygen_info = dctx->gentmp;
64 ctx->keygen_info_count = 2;
69 static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
71 DH_PKEY_CTX *dctx = ctx->data;
73 OPENSSL_free(dctx->kdf_ukm);
74 ASN1_OBJECT_free(dctx->kdf_oid);
80 static int pkey_dh_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
82 DH_PKEY_CTX *dctx, *sctx;
84 if (!pkey_dh_init(dst))
88 dctx->prime_len = sctx->prime_len;
89 dctx->subprime_len = sctx->subprime_len;
90 dctx->generator = sctx->generator;
91 dctx->use_dsa = sctx->use_dsa;
92 dctx->pad = sctx->pad;
94 dctx->rfc5114_param = sctx->rfc5114_param;
95 dctx->param_nid = sctx->param_nid;
97 dctx->kdf_type = sctx->kdf_type;
98 dctx->kdf_oid = OBJ_dup(sctx->kdf_oid);
99 if (dctx->kdf_oid == NULL)
101 dctx->kdf_md = sctx->kdf_md;
102 if (sctx->kdf_ukm != NULL) {
103 dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
104 if (dctx->kdf_ukm == NULL)
106 dctx->kdf_ukmlen = sctx->kdf_ukmlen;
108 dctx->kdf_outlen = sctx->kdf_outlen;
112 static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
114 DH_PKEY_CTX *dctx = ctx->data;
116 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
119 dctx->prime_len = p1;
122 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
123 if (dctx->use_dsa == 0)
125 dctx->subprime_len = p1;
128 case EVP_PKEY_CTRL_DH_PAD:
132 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
135 dctx->generator = p1;
138 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
139 #ifdef OPENSSL_NO_DSA
143 if (p1 < 0 || p1 > 2)
149 case EVP_PKEY_CTRL_DH_RFC5114:
150 if (p1 < 1 || p1 > 3 || dctx->param_nid != NID_undef)
152 dctx->rfc5114_param = p1;
155 case EVP_PKEY_CTRL_DH_NID:
156 if (p1 <= 0 || dctx->rfc5114_param != 0)
158 dctx->param_nid = p1;
161 case EVP_PKEY_CTRL_PEER_KEY:
162 /* Default behaviour is OK */
165 case EVP_PKEY_CTRL_DH_KDF_TYPE:
167 return dctx->kdf_type;
168 #ifdef OPENSSL_NO_CMS
169 if (p1 != EVP_PKEY_DH_KDF_NONE)
171 if (p1 != EVP_PKEY_DH_KDF_NONE && p1 != EVP_PKEY_DH_KDF_X9_42)
177 case EVP_PKEY_CTRL_DH_KDF_MD:
181 case EVP_PKEY_CTRL_GET_DH_KDF_MD:
182 *(const EVP_MD **)p2 = dctx->kdf_md;
185 case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
188 dctx->kdf_outlen = (size_t)p1;
191 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
192 *(int *)p2 = dctx->kdf_outlen;
195 case EVP_PKEY_CTRL_DH_KDF_UKM:
196 OPENSSL_free(dctx->kdf_ukm);
199 dctx->kdf_ukmlen = p1;
201 dctx->kdf_ukmlen = 0;
204 case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
205 *(unsigned char **)p2 = dctx->kdf_ukm;
206 return dctx->kdf_ukmlen;
208 case EVP_PKEY_CTRL_DH_KDF_OID:
209 ASN1_OBJECT_free(dctx->kdf_oid);
213 case EVP_PKEY_CTRL_GET_DH_KDF_OID:
214 *(ASN1_OBJECT **)p2 = dctx->kdf_oid;
223 static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
224 const char *type, const char *value)
226 if (strcmp(type, "dh_paramgen_prime_len") == 0) {
229 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
231 if (strcmp(type, "dh_rfc5114") == 0) {
232 DH_PKEY_CTX *dctx = ctx->data;
235 if (len < 0 || len > 3)
237 dctx->rfc5114_param = len;
240 if (strcmp(type, "dh_param") == 0) {
241 DH_PKEY_CTX *dctx = ctx->data;
242 int nid = OBJ_sn2nid(value);
244 if (nid == NID_undef) {
245 DHerr(DH_F_PKEY_DH_CTRL_STR, DH_R_INVALID_PARAMETER_NAME);
248 dctx->param_nid = nid;
251 if (strcmp(type, "dh_paramgen_generator") == 0) {
254 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
256 if (strcmp(type, "dh_paramgen_subprime_len") == 0) {
259 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len);
261 if (strcmp(type, "dh_paramgen_type") == 0) {
264 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ);
266 if (strcmp(type, "dh_pad") == 0) {
269 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
274 #ifndef OPENSSL_NO_DSA
276 extern int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
278 const unsigned char *seed_in, size_t seed_len,
279 unsigned char *seed_out, int *counter_ret,
280 unsigned long *h_ret, BN_GENCB *cb);
282 extern int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
284 const unsigned char *seed_in,
285 size_t seed_len, int idx,
286 unsigned char *seed_out, int *counter_ret,
287 unsigned long *h_ret, BN_GENCB *cb);
289 static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)
293 int prime_len = dctx->prime_len;
294 int subprime_len = dctx->subprime_len;
295 const EVP_MD *md = dctx->md;
296 if (dctx->use_dsa > 2)
301 if (subprime_len == -1) {
302 if (prime_len >= 2048)
308 if (prime_len >= 2048)
313 if (dctx->use_dsa == 1)
314 rv = dsa_builtin_paramgen(ret, prime_len, subprime_len, md,
315 NULL, 0, NULL, NULL, NULL, pcb);
316 else if (dctx->use_dsa == 2)
317 rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,
318 NULL, 0, -1, NULL, NULL, NULL, pcb);
328 static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
331 DH_PKEY_CTX *dctx = ctx->data;
334 if (dctx->rfc5114_param) {
335 switch (dctx->rfc5114_param) {
337 dh = DH_get_1024_160();
341 dh = DH_get_2048_224();
345 dh = DH_get_2048_256();
351 EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
355 if (dctx->param_nid != 0) {
356 if ((dh = DH_new_by_nid(dctx->param_nid)) == NULL)
358 EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh);
362 if (ctx->pkey_gencb) {
363 pcb = BN_GENCB_new();
366 evp_pkey_set_cb_translate(pcb, ctx);
369 #ifndef OPENSSL_NO_DSA
372 dsa_dh = dsa_dh_generate(dctx, pcb);
376 dh = DSA_dup_DH(dsa_dh);
380 EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
389 ret = DH_generate_parameters_ex(dh,
390 dctx->prime_len, dctx->generator, pcb);
393 EVP_PKEY_assign_DH(pkey, dh);
399 static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
401 DH_PKEY_CTX *dctx = ctx->data;
404 if (ctx->pkey == NULL && dctx->param_nid == 0) {
405 DHerr(DH_F_PKEY_DH_KEYGEN, DH_R_NO_PARAMETERS_SET);
408 if (dctx->param_nid != 0)
409 dh = DH_new_by_nid(dctx->param_nid);
414 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, dh);
415 /* Note: if error return, pkey is freed by parent routine */
416 if (ctx->pkey != NULL && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
418 return DH_generate_key(pkey->pkey.dh);
421 static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
426 DH_PKEY_CTX *dctx = ctx->data;
428 if (!ctx->pkey || !ctx->peerkey) {
429 DHerr(DH_F_PKEY_DH_DERIVE, DH_R_KEYS_NOT_SET);
432 dh = ctx->pkey->pkey.dh;
433 dhpub = ctx->peerkey->pkey.dh->pub_key;
434 if (dctx->kdf_type == EVP_PKEY_DH_KDF_NONE) {
436 *keylen = DH_size(dh);
440 ret = DH_compute_key_padded(key, dhpub, dh);
442 ret = DH_compute_key(key, dhpub, dh);
448 #ifndef OPENSSL_NO_CMS
449 else if (dctx->kdf_type == EVP_PKEY_DH_KDF_X9_42) {
451 unsigned char *Z = NULL;
453 if (!dctx->kdf_outlen || !dctx->kdf_oid)
456 *keylen = dctx->kdf_outlen;
459 if (*keylen != dctx->kdf_outlen)
463 Z = OPENSSL_malloc(Zlen);
467 if (DH_compute_key_padded(Z, dhpub, dh) <= 0)
469 if (!DH_KDF_X9_42(key, *keylen, Z, Zlen, dctx->kdf_oid,
470 dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
472 *keylen = dctx->kdf_outlen;
475 OPENSSL_clear_free(Z, Zlen);
482 static const EVP_PKEY_METHOD dh_pkey_meth = {
516 const EVP_PKEY_METHOD *dh_pkey_method(void)
518 return &dh_pkey_meth;
521 static const EVP_PKEY_METHOD dhx_pkey_meth = {
555 const EVP_PKEY_METHOD *dhx_pkey_method(void)
557 return &dhx_pkey_meth;