2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 "internal/evp_int.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 dctx = OPENSSL_zalloc(sizeof(*dctx));
56 dctx->prime_len = 1024;
57 dctx->subprime_len = -1;
59 dctx->kdf_type = EVP_PKEY_DH_KDF_NONE;
62 ctx->keygen_info = dctx->gentmp;
63 ctx->keygen_info_count = 2;
68 static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
70 DH_PKEY_CTX *dctx = ctx->data;
72 OPENSSL_free(dctx->kdf_ukm);
73 ASN1_OBJECT_free(dctx->kdf_oid);
79 static int pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
81 DH_PKEY_CTX *dctx, *sctx;
82 if (!pkey_dh_init(dst))
86 dctx->prime_len = sctx->prime_len;
87 dctx->subprime_len = sctx->subprime_len;
88 dctx->generator = sctx->generator;
89 dctx->use_dsa = sctx->use_dsa;
90 dctx->pad = sctx->pad;
92 dctx->rfc5114_param = sctx->rfc5114_param;
93 dctx->param_nid = sctx->param_nid;
95 dctx->kdf_type = sctx->kdf_type;
96 dctx->kdf_oid = OBJ_dup(sctx->kdf_oid);
97 if (dctx->kdf_oid == NULL)
99 dctx->kdf_md = sctx->kdf_md;
100 if (sctx->kdf_ukm != NULL) {
101 dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
102 if (dctx->kdf_ukm == NULL)
104 dctx->kdf_ukmlen = sctx->kdf_ukmlen;
106 dctx->kdf_outlen = sctx->kdf_outlen;
110 static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
112 DH_PKEY_CTX *dctx = ctx->data;
114 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
117 dctx->prime_len = p1;
120 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN:
121 if (dctx->use_dsa == 0)
123 dctx->subprime_len = p1;
126 case EVP_PKEY_CTRL_DH_PAD:
130 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
133 dctx->generator = p1;
136 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE:
137 #ifdef OPENSSL_NO_DSA
141 if (p1 < 0 || p1 > 2)
147 case EVP_PKEY_CTRL_DH_RFC5114:
148 if (p1 < 1 || p1 > 3 || dctx->param_nid != NID_undef)
150 dctx->rfc5114_param = p1;
153 case EVP_PKEY_CTRL_DH_NID:
154 if (p1 <= 0 || dctx->rfc5114_param != 0)
156 dctx->param_nid = p1;
159 case EVP_PKEY_CTRL_PEER_KEY:
160 /* Default behaviour is OK */
163 case EVP_PKEY_CTRL_DH_KDF_TYPE:
165 return dctx->kdf_type;
166 #ifdef OPENSSL_NO_CMS
167 if (p1 != EVP_PKEY_DH_KDF_NONE)
169 if (p1 != EVP_PKEY_DH_KDF_NONE && p1 != EVP_PKEY_DH_KDF_X9_42)
175 case EVP_PKEY_CTRL_DH_KDF_MD:
179 case EVP_PKEY_CTRL_GET_DH_KDF_MD:
180 *(const EVP_MD **)p2 = dctx->kdf_md;
183 case EVP_PKEY_CTRL_DH_KDF_OUTLEN:
186 dctx->kdf_outlen = (size_t)p1;
189 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN:
190 *(int *)p2 = dctx->kdf_outlen;
193 case EVP_PKEY_CTRL_DH_KDF_UKM:
194 OPENSSL_free(dctx->kdf_ukm);
197 dctx->kdf_ukmlen = p1;
199 dctx->kdf_ukmlen = 0;
202 case EVP_PKEY_CTRL_GET_DH_KDF_UKM:
203 *(unsigned char **)p2 = dctx->kdf_ukm;
204 return dctx->kdf_ukmlen;
206 case EVP_PKEY_CTRL_DH_KDF_OID:
207 ASN1_OBJECT_free(dctx->kdf_oid);
211 case EVP_PKEY_CTRL_GET_DH_KDF_OID:
212 *(ASN1_OBJECT **)p2 = dctx->kdf_oid;
221 static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
222 const char *type, const char *value)
224 if (strcmp(type, "dh_paramgen_prime_len") == 0) {
227 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
229 if (strcmp(type, "dh_rfc5114") == 0) {
230 DH_PKEY_CTX *dctx = ctx->data;
233 if (len < 0 || len > 3)
235 dctx->rfc5114_param = len;
238 if (strcmp(type, "dh_param") == 0) {
239 DH_PKEY_CTX *dctx = ctx->data;
240 int nid = OBJ_sn2nid(value);
242 if (nid == NID_undef) {
243 DHerr(DH_F_PKEY_DH_CTRL_STR, DH_R_INVALID_PARAMETER_NAME);
246 dctx->param_nid = nid;
249 if (strcmp(type, "dh_paramgen_generator") == 0) {
252 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
254 if (strcmp(type, "dh_paramgen_subprime_len") == 0) {
257 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, len);
259 if (strcmp(type, "dh_paramgen_type") == 0) {
262 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, typ);
264 if (strcmp(type, "dh_pad") == 0) {
267 return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
272 #ifndef OPENSSL_NO_DSA
274 extern int dsa_builtin_paramgen(DSA *ret, size_t bits, size_t qbits,
276 const unsigned char *seed_in, size_t seed_len,
277 unsigned char *seed_out, int *counter_ret,
278 unsigned long *h_ret, BN_GENCB *cb);
280 extern int dsa_builtin_paramgen2(DSA *ret, size_t L, size_t N,
282 const unsigned char *seed_in,
283 size_t seed_len, int idx,
284 unsigned char *seed_out, int *counter_ret,
285 unsigned long *h_ret, BN_GENCB *cb);
287 static DSA *dsa_dh_generate(DH_PKEY_CTX *dctx, BN_GENCB *pcb)
291 int prime_len = dctx->prime_len;
292 int subprime_len = dctx->subprime_len;
293 const EVP_MD *md = dctx->md;
294 if (dctx->use_dsa > 2)
299 if (subprime_len == -1) {
300 if (prime_len >= 2048)
306 if (prime_len >= 2048)
311 if (dctx->use_dsa == 1)
312 rv = dsa_builtin_paramgen(ret, prime_len, subprime_len, md,
313 NULL, 0, NULL, NULL, NULL, pcb);
314 else if (dctx->use_dsa == 2)
315 rv = dsa_builtin_paramgen2(ret, prime_len, subprime_len, md,
316 NULL, 0, -1, NULL, NULL, NULL, pcb);
326 static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
329 DH_PKEY_CTX *dctx = ctx->data;
332 if (dctx->rfc5114_param) {
333 switch (dctx->rfc5114_param) {
335 dh = DH_get_1024_160();
339 dh = DH_get_2048_224();
343 dh = DH_get_2048_256();
349 EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
353 if (dctx->param_nid != 0) {
354 if ((dh = DH_new_by_nid(dctx->param_nid)) == NULL)
356 EVP_PKEY_assign(pkey, EVP_PKEY_DH, dh);
360 if (ctx->pkey_gencb) {
361 pcb = BN_GENCB_new();
364 evp_pkey_set_cb_translate(pcb, ctx);
367 #ifndef OPENSSL_NO_DSA
370 dsa_dh = dsa_dh_generate(dctx, pcb);
374 dh = DSA_dup_DH(dsa_dh);
378 EVP_PKEY_assign(pkey, EVP_PKEY_DHX, dh);
387 ret = DH_generate_parameters_ex(dh,
388 dctx->prime_len, dctx->generator, pcb);
391 EVP_PKEY_assign_DH(pkey, dh);
397 static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
399 DH_PKEY_CTX *dctx = ctx->data;
402 if (ctx->pkey == NULL && dctx->param_nid == 0) {
403 DHerr(DH_F_PKEY_DH_KEYGEN, DH_R_NO_PARAMETERS_SET);
406 if (dctx->param_nid != 0)
407 dh = DH_new_by_nid(dctx->param_nid);
412 EVP_PKEY_assign(pkey, ctx->pmeth->pkey_id, dh);
413 /* Note: if error return, pkey is freed by parent routine */
414 if (ctx->pkey != NULL && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
416 return DH_generate_key(pkey->pkey.dh);
419 static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
424 DH_PKEY_CTX *dctx = ctx->data;
426 if (!ctx->pkey || !ctx->peerkey) {
427 DHerr(DH_F_PKEY_DH_DERIVE, DH_R_KEYS_NOT_SET);
430 dh = ctx->pkey->pkey.dh;
431 dhpub = ctx->peerkey->pkey.dh->pub_key;
432 if (dctx->kdf_type == EVP_PKEY_DH_KDF_NONE) {
434 *keylen = DH_size(dh);
438 ret = DH_compute_key_padded(key, dhpub, dh);
440 ret = DH_compute_key(key, dhpub, dh);
446 #ifndef OPENSSL_NO_CMS
447 else if (dctx->kdf_type == EVP_PKEY_DH_KDF_X9_42) {
449 unsigned char *Z = NULL;
451 if (!dctx->kdf_outlen || !dctx->kdf_oid)
454 *keylen = dctx->kdf_outlen;
457 if (*keylen != dctx->kdf_outlen)
461 Z = OPENSSL_malloc(Zlen);
465 if (DH_compute_key_padded(Z, dhpub, dh) <= 0)
467 if (!DH_KDF_X9_42(key, *keylen, Z, Zlen, dctx->kdf_oid,
468 dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
470 *keylen = dctx->kdf_outlen;
473 OPENSSL_clear_free(Z, Zlen);
480 const EVP_PKEY_METHOD dh_pkey_meth = {
514 const EVP_PKEY_METHOD dhx_pkey_meth = {