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 "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 if ((dctx
= OPENSSL_zalloc(sizeof(*dctx
))) == NULL
) {
54 DHerr(DH_F_PKEY_DH_INIT
, ERR_R_MALLOC_FAILURE
);
57 dctx
->prime_len
= 1024;
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
, EVP_PKEY_CTX
*src
)
82 DH_PKEY_CTX
*dctx
, *sctx
;
83 if (!pkey_dh_init(dst
))
87 dctx
->prime_len
= sctx
->prime_len
;
88 dctx
->subprime_len
= sctx
->subprime_len
;
89 dctx
->generator
= sctx
->generator
;
90 dctx
->use_dsa
= sctx
->use_dsa
;
91 dctx
->pad
= sctx
->pad
;
93 dctx
->rfc5114_param
= sctx
->rfc5114_param
;
94 dctx
->param_nid
= sctx
->param_nid
;
96 dctx
->kdf_type
= sctx
->kdf_type
;
97 dctx
->kdf_oid
= OBJ_dup(sctx
->kdf_oid
);
98 if (dctx
->kdf_oid
== NULL
)
100 dctx
->kdf_md
= sctx
->kdf_md
;
101 if (sctx
->kdf_ukm
!= NULL
) {
102 dctx
->kdf_ukm
= OPENSSL_memdup(sctx
->kdf_ukm
, sctx
->kdf_ukmlen
);
103 if (dctx
->kdf_ukm
== NULL
)
105 dctx
->kdf_ukmlen
= sctx
->kdf_ukmlen
;
107 dctx
->kdf_outlen
= sctx
->kdf_outlen
;
111 static int pkey_dh_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
113 DH_PKEY_CTX
*dctx
= ctx
->data
;
115 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN
:
118 dctx
->prime_len
= p1
;
121 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN
:
122 if (dctx
->use_dsa
== 0)
124 dctx
->subprime_len
= p1
;
127 case EVP_PKEY_CTRL_DH_PAD
:
131 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR
:
134 dctx
->generator
= p1
;
137 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE
:
138 #ifdef OPENSSL_NO_DSA
142 if (p1
< 0 || p1
> 2)
148 case EVP_PKEY_CTRL_DH_RFC5114
:
149 if (p1
< 1 || p1
> 3 || dctx
->param_nid
!= NID_undef
)
151 dctx
->rfc5114_param
= p1
;
154 case EVP_PKEY_CTRL_DH_NID
:
155 if (p1
<= 0 || dctx
->rfc5114_param
!= 0)
157 dctx
->param_nid
= p1
;
160 case EVP_PKEY_CTRL_PEER_KEY
:
161 /* Default behaviour is OK */
164 case EVP_PKEY_CTRL_DH_KDF_TYPE
:
166 return dctx
->kdf_type
;
167 #ifdef OPENSSL_NO_CMS
168 if (p1
!= EVP_PKEY_DH_KDF_NONE
)
170 if (p1
!= EVP_PKEY_DH_KDF_NONE
&& p1
!= EVP_PKEY_DH_KDF_X9_42
)
176 case EVP_PKEY_CTRL_DH_KDF_MD
:
180 case EVP_PKEY_CTRL_GET_DH_KDF_MD
:
181 *(const EVP_MD
**)p2
= dctx
->kdf_md
;
184 case EVP_PKEY_CTRL_DH_KDF_OUTLEN
:
187 dctx
->kdf_outlen
= (size_t)p1
;
190 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN
:
191 *(int *)p2
= dctx
->kdf_outlen
;
194 case EVP_PKEY_CTRL_DH_KDF_UKM
:
195 OPENSSL_free(dctx
->kdf_ukm
);
198 dctx
->kdf_ukmlen
= p1
;
200 dctx
->kdf_ukmlen
= 0;
203 case EVP_PKEY_CTRL_GET_DH_KDF_UKM
:
204 *(unsigned char **)p2
= dctx
->kdf_ukm
;
205 return dctx
->kdf_ukmlen
;
207 case EVP_PKEY_CTRL_DH_KDF_OID
:
208 ASN1_OBJECT_free(dctx
->kdf_oid
);
212 case EVP_PKEY_CTRL_GET_DH_KDF_OID
:
213 *(ASN1_OBJECT
**)p2
= dctx
->kdf_oid
;
222 static int pkey_dh_ctrl_str(EVP_PKEY_CTX
*ctx
,
223 const char *type
, const char *value
)
225 if (strcmp(type
, "dh_paramgen_prime_len") == 0) {
228 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx
, len
);
230 if (strcmp(type
, "dh_rfc5114") == 0) {
231 DH_PKEY_CTX
*dctx
= ctx
->data
;
234 if (len
< 0 || len
> 3)
236 dctx
->rfc5114_param
= len
;
239 if (strcmp(type
, "dh_param") == 0) {
240 DH_PKEY_CTX
*dctx
= ctx
->data
;
241 int nid
= OBJ_sn2nid(value
);
243 if (nid
== NID_undef
) {
244 DHerr(DH_F_PKEY_DH_CTRL_STR
, DH_R_INVALID_PARAMETER_NAME
);
247 dctx
->param_nid
= nid
;
250 if (strcmp(type
, "dh_paramgen_generator") == 0) {
253 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx
, len
);
255 if (strcmp(type
, "dh_paramgen_subprime_len") == 0) {
258 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx
, len
);
260 if (strcmp(type
, "dh_paramgen_type") == 0) {
263 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx
, typ
);
265 if (strcmp(type
, "dh_pad") == 0) {
268 return EVP_PKEY_CTX_set_dh_pad(ctx
, pad
);
273 #ifndef OPENSSL_NO_DSA
275 extern int dsa_builtin_paramgen(DSA
*ret
, size_t bits
, size_t qbits
,
277 const unsigned char *seed_in
, size_t seed_len
,
278 unsigned char *seed_out
, int *counter_ret
,
279 unsigned long *h_ret
, BN_GENCB
*cb
);
281 extern int dsa_builtin_paramgen2(DSA
*ret
, size_t L
, size_t N
,
283 const unsigned char *seed_in
,
284 size_t seed_len
, int idx
,
285 unsigned char *seed_out
, int *counter_ret
,
286 unsigned long *h_ret
, BN_GENCB
*cb
);
288 static DSA
*dsa_dh_generate(DH_PKEY_CTX
*dctx
, BN_GENCB
*pcb
)
292 int prime_len
= dctx
->prime_len
;
293 int subprime_len
= dctx
->subprime_len
;
294 const EVP_MD
*md
= dctx
->md
;
295 if (dctx
->use_dsa
> 2)
300 if (subprime_len
== -1) {
301 if (prime_len
>= 2048)
307 if (prime_len
>= 2048)
312 if (dctx
->use_dsa
== 1)
313 rv
= dsa_builtin_paramgen(ret
, prime_len
, subprime_len
, md
,
314 NULL
, 0, NULL
, NULL
, NULL
, pcb
);
315 else if (dctx
->use_dsa
== 2)
316 rv
= dsa_builtin_paramgen2(ret
, prime_len
, subprime_len
, md
,
317 NULL
, 0, -1, NULL
, NULL
, NULL
, pcb
);
327 static int pkey_dh_paramgen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
330 DH_PKEY_CTX
*dctx
= ctx
->data
;
333 if (dctx
->rfc5114_param
) {
334 switch (dctx
->rfc5114_param
) {
336 dh
= DH_get_1024_160();
340 dh
= DH_get_2048_224();
344 dh
= DH_get_2048_256();
350 EVP_PKEY_assign(pkey
, EVP_PKEY_DHX
, dh
);
354 if (dctx
->param_nid
!= 0) {
355 if ((dh
= DH_new_by_nid(dctx
->param_nid
)) == NULL
)
357 EVP_PKEY_assign(pkey
, EVP_PKEY_DH
, dh
);
361 if (ctx
->pkey_gencb
) {
362 pcb
= BN_GENCB_new();
365 evp_pkey_set_cb_translate(pcb
, ctx
);
368 #ifndef OPENSSL_NO_DSA
371 dsa_dh
= dsa_dh_generate(dctx
, pcb
);
375 dh
= DSA_dup_DH(dsa_dh
);
379 EVP_PKEY_assign(pkey
, EVP_PKEY_DHX
, dh
);
388 ret
= DH_generate_parameters_ex(dh
,
389 dctx
->prime_len
, dctx
->generator
, pcb
);
392 EVP_PKEY_assign_DH(pkey
, dh
);
398 static int pkey_dh_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
400 DH_PKEY_CTX
*dctx
= ctx
->data
;
403 if (ctx
->pkey
== NULL
&& dctx
->param_nid
== 0) {
404 DHerr(DH_F_PKEY_DH_KEYGEN
, DH_R_NO_PARAMETERS_SET
);
407 if (dctx
->param_nid
!= 0)
408 dh
= DH_new_by_nid(dctx
->param_nid
);
413 EVP_PKEY_assign(pkey
, ctx
->pmeth
->pkey_id
, dh
);
414 /* Note: if error return, pkey is freed by parent routine */
415 if (ctx
->pkey
!= NULL
&& !EVP_PKEY_copy_parameters(pkey
, ctx
->pkey
))
417 return DH_generate_key(pkey
->pkey
.dh
);
420 static int pkey_dh_derive(EVP_PKEY_CTX
*ctx
, unsigned char *key
,
425 DH_PKEY_CTX
*dctx
= ctx
->data
;
427 if (!ctx
->pkey
|| !ctx
->peerkey
) {
428 DHerr(DH_F_PKEY_DH_DERIVE
, DH_R_KEYS_NOT_SET
);
431 dh
= ctx
->pkey
->pkey
.dh
;
432 dhpub
= ctx
->peerkey
->pkey
.dh
->pub_key
;
433 if (dctx
->kdf_type
== EVP_PKEY_DH_KDF_NONE
) {
435 *keylen
= DH_size(dh
);
439 ret
= DH_compute_key_padded(key
, dhpub
, dh
);
441 ret
= DH_compute_key(key
, dhpub
, dh
);
447 #ifndef OPENSSL_NO_CMS
448 else if (dctx
->kdf_type
== EVP_PKEY_DH_KDF_X9_42
) {
450 unsigned char *Z
= NULL
;
452 if (!dctx
->kdf_outlen
|| !dctx
->kdf_oid
)
455 *keylen
= dctx
->kdf_outlen
;
458 if (*keylen
!= dctx
->kdf_outlen
)
462 Z
= OPENSSL_malloc(Zlen
);
466 if (DH_compute_key_padded(Z
, dhpub
, dh
) <= 0)
468 if (!DH_KDF_X9_42(key
, *keylen
, Z
, Zlen
, dctx
->kdf_oid
,
469 dctx
->kdf_ukm
, dctx
->kdf_ukmlen
, dctx
->kdf_md
))
471 *keylen
= dctx
->kdf_outlen
;
474 OPENSSL_clear_free(Z
, Zlen
);
481 const EVP_PKEY_METHOD dh_pkey_meth
= {
515 const EVP_PKEY_METHOD dhx_pkey_meth
= {