2 * Copyright 2006-2021 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 * DH & DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/evp.h>
22 #include <openssl/bn.h>
23 #include <openssl/dsa.h>
24 #include <openssl/objects.h>
25 #include "crypto/evp.h"
27 /* DH pkey context structure */
30 /* Parameter gen parameters */
36 /* message digest used for parameter generation */
39 /* Keygen callback info */
41 /* KDF (if any) to use for DH */
43 /* OID to use for KDF */
45 /* Message digest to use for key derivation */
47 /* User key material */
48 unsigned char *kdf_ukm
;
50 /* KDF output length */
54 static int pkey_dh_init(EVP_PKEY_CTX
*ctx
)
58 if ((dctx
= OPENSSL_zalloc(sizeof(*dctx
))) == NULL
) {
59 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
62 dctx
->prime_len
= 2048;
63 dctx
->subprime_len
= -1;
65 dctx
->kdf_type
= EVP_PKEY_DH_KDF_NONE
;
68 ctx
->keygen_info
= dctx
->gentmp
;
69 ctx
->keygen_info_count
= 2;
74 static void pkey_dh_cleanup(EVP_PKEY_CTX
*ctx
)
76 DH_PKEY_CTX
*dctx
= ctx
->data
;
79 OPENSSL_free(dctx
->kdf_ukm
);
80 ASN1_OBJECT_free(dctx
->kdf_oid
);
86 static int pkey_dh_copy(EVP_PKEY_CTX
*dst
, const EVP_PKEY_CTX
*src
)
88 DH_PKEY_CTX
*dctx
, *sctx
;
90 if (!pkey_dh_init(dst
))
94 dctx
->prime_len
= sctx
->prime_len
;
95 dctx
->subprime_len
= sctx
->subprime_len
;
96 dctx
->generator
= sctx
->generator
;
97 dctx
->paramgen_type
= sctx
->paramgen_type
;
98 dctx
->pad
= sctx
->pad
;
100 dctx
->param_nid
= sctx
->param_nid
;
102 dctx
->kdf_type
= sctx
->kdf_type
;
103 dctx
->kdf_oid
= OBJ_dup(sctx
->kdf_oid
);
104 if (dctx
->kdf_oid
== NULL
)
106 dctx
->kdf_md
= sctx
->kdf_md
;
107 if (sctx
->kdf_ukm
!= NULL
) {
108 dctx
->kdf_ukm
= OPENSSL_memdup(sctx
->kdf_ukm
, sctx
->kdf_ukmlen
);
109 if (dctx
->kdf_ukm
== NULL
)
111 dctx
->kdf_ukmlen
= sctx
->kdf_ukmlen
;
113 dctx
->kdf_outlen
= sctx
->kdf_outlen
;
117 static int pkey_dh_ctrl(EVP_PKEY_CTX
*ctx
, int type
, int p1
, void *p2
)
119 DH_PKEY_CTX
*dctx
= ctx
->data
;
121 case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN
:
124 dctx
->prime_len
= p1
;
127 case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN
:
128 if (dctx
->paramgen_type
== DH_PARAMGEN_TYPE_GENERATOR
)
130 dctx
->subprime_len
= p1
;
133 case EVP_PKEY_CTRL_DH_PAD
:
137 case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR
:
138 if (dctx
->paramgen_type
!= DH_PARAMGEN_TYPE_GENERATOR
)
140 dctx
->generator
= p1
;
143 case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE
:
144 #ifdef OPENSSL_NO_DSA
145 if (p1
!= DH_PARAMGEN_TYPE_GENERATOR
)
148 if (p1
< 0 || p1
> 2)
151 dctx
->paramgen_type
= p1
;
154 case EVP_PKEY_CTRL_DH_RFC5114
:
155 if (p1
< 1 || p1
> 3 || dctx
->param_nid
!= NID_undef
)
157 dctx
->param_nid
= p1
;
160 case EVP_PKEY_CTRL_DH_NID
:
161 if (p1
<= 0 || dctx
->param_nid
!= NID_undef
)
163 dctx
->param_nid
= p1
;
166 case EVP_PKEY_CTRL_PEER_KEY
:
167 /* Default behaviour is OK */
170 case EVP_PKEY_CTRL_DH_KDF_TYPE
:
172 return dctx
->kdf_type
;
173 if (p1
!= EVP_PKEY_DH_KDF_NONE
&& p1
!= EVP_PKEY_DH_KDF_X9_42
)
178 case EVP_PKEY_CTRL_DH_KDF_MD
:
182 case EVP_PKEY_CTRL_GET_DH_KDF_MD
:
183 *(const EVP_MD
**)p2
= dctx
->kdf_md
;
186 case EVP_PKEY_CTRL_DH_KDF_OUTLEN
:
189 dctx
->kdf_outlen
= (size_t)p1
;
192 case EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN
:
193 *(int *)p2
= dctx
->kdf_outlen
;
196 case EVP_PKEY_CTRL_DH_KDF_UKM
:
197 OPENSSL_free(dctx
->kdf_ukm
);
200 dctx
->kdf_ukmlen
= p1
;
202 dctx
->kdf_ukmlen
= 0;
205 case EVP_PKEY_CTRL_GET_DH_KDF_UKM
:
206 *(unsigned char **)p2
= dctx
->kdf_ukm
;
207 return dctx
->kdf_ukmlen
;
209 case EVP_PKEY_CTRL_DH_KDF_OID
:
210 ASN1_OBJECT_free(dctx
->kdf_oid
);
214 case EVP_PKEY_CTRL_GET_DH_KDF_OID
:
215 *(ASN1_OBJECT
**)p2
= dctx
->kdf_oid
;
224 static int pkey_dh_ctrl_str(EVP_PKEY_CTX
*ctx
,
225 const char *type
, const char *value
)
227 if (strcmp(type
, "dh_paramgen_prime_len") == 0) {
230 return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx
, len
);
232 if (strcmp(type
, "dh_rfc5114") == 0) {
233 DH_PKEY_CTX
*dctx
= ctx
->data
;
237 if (id
< 0 || id
> 3)
239 dctx
->param_nid
= id
;
242 if (strcmp(type
, "dh_param") == 0) {
243 DH_PKEY_CTX
*dctx
= ctx
->data
;
244 int nid
= OBJ_sn2nid(value
);
246 if (nid
== NID_undef
) {
247 ERR_raise(ERR_LIB_DH
, DH_R_INVALID_PARAMETER_NAME
);
250 dctx
->param_nid
= nid
;
253 if (strcmp(type
, "dh_paramgen_generator") == 0) {
256 return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx
, len
);
258 if (strcmp(type
, "dh_paramgen_subprime_len") == 0) {
261 return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx
, len
);
263 if (strcmp(type
, "dh_paramgen_type") == 0) {
266 return EVP_PKEY_CTX_set_dh_paramgen_type(ctx
, typ
);
268 if (strcmp(type
, "dh_pad") == 0) {
271 return EVP_PKEY_CTX_set_dh_pad(ctx
, pad
);
276 static DH
*ffc_params_generate(OSSL_LIB_CTX
*libctx
, DH_PKEY_CTX
*dctx
,
282 int prime_len
= dctx
->prime_len
;
283 int subprime_len
= dctx
->subprime_len
;
285 if (dctx
->paramgen_type
> DH_PARAMGEN_TYPE_FIPS_186_4
)
291 if (subprime_len
== -1) {
292 if (prime_len
>= 2048)
298 if (dctx
->md
!= NULL
)
299 ossl_ffc_set_digest(&ret
->params
, EVP_MD_get0_name(dctx
->md
), NULL
);
302 if (dctx
->paramgen_type
== DH_PARAMGEN_TYPE_FIPS_186_2
)
303 rv
= ossl_ffc_params_FIPS186_2_generate(libctx
, &ret
->params
,
305 prime_len
, subprime_len
, &res
,
309 /* For FIPS we always use the DH_PARAMGEN_TYPE_FIPS_186_4 generator */
310 if (dctx
->paramgen_type
>= DH_PARAMGEN_TYPE_FIPS_186_2
)
311 rv
= ossl_ffc_params_FIPS186_4_generate(libctx
, &ret
->params
,
313 prime_len
, subprime_len
, &res
,
322 static int pkey_dh_paramgen(EVP_PKEY_CTX
*ctx
,
326 DH_PKEY_CTX
*dctx
= ctx
->data
;
327 BN_GENCB
*pcb
= NULL
;
331 * Look for a safe prime group for key establishment. Which uses
332 * either RFC_3526 (modp_XXXX) or RFC_7919 (ffdheXXXX).
333 * RFC_5114 is also handled here for param_nid = (1..3)
335 if (dctx
->param_nid
!= NID_undef
) {
336 int type
= dctx
->param_nid
<= 3 ? EVP_PKEY_DHX
: EVP_PKEY_DH
;
338 if ((dh
= DH_new_by_nid(dctx
->param_nid
)) == NULL
)
340 EVP_PKEY_assign(pkey
, type
, dh
);
344 if (ctx
->pkey_gencb
!= NULL
) {
345 pcb
= BN_GENCB_new();
348 evp_pkey_set_cb_translate(pcb
, ctx
);
351 dctx
->paramgen_type
= DH_PARAMGEN_TYPE_FIPS_186_4
;
352 # endif /* FIPS_MODULE */
353 if (dctx
->paramgen_type
>= DH_PARAMGEN_TYPE_FIPS_186_2
) {
354 dh
= ffc_params_generate(NULL
, dctx
, pcb
);
358 EVP_PKEY_assign(pkey
, EVP_PKEY_DHX
, dh
);
366 ret
= DH_generate_parameters_ex(dh
,
367 dctx
->prime_len
, dctx
->generator
, pcb
);
370 EVP_PKEY_assign_DH(pkey
, dh
);
376 static int pkey_dh_keygen(EVP_PKEY_CTX
*ctx
, EVP_PKEY
*pkey
)
378 DH_PKEY_CTX
*dctx
= ctx
->data
;
381 if (ctx
->pkey
== NULL
&& dctx
->param_nid
== NID_undef
) {
382 ERR_raise(ERR_LIB_DH
, DH_R_NO_PARAMETERS_SET
);
385 if (dctx
->param_nid
!= NID_undef
)
386 dh
= DH_new_by_nid(dctx
->param_nid
);
391 EVP_PKEY_assign(pkey
, ctx
->pmeth
->pkey_id
, dh
);
392 /* Note: if error return, pkey is freed by parent routine */
393 if (ctx
->pkey
!= NULL
&& !EVP_PKEY_copy_parameters(pkey
, ctx
->pkey
))
395 return DH_generate_key((DH
*)EVP_PKEY_get0_DH(pkey
));
398 static int pkey_dh_derive(EVP_PKEY_CTX
*ctx
, unsigned char *key
,
404 DH_PKEY_CTX
*dctx
= ctx
->data
;
407 if (ctx
->pkey
== NULL
|| ctx
->peerkey
== NULL
) {
408 ERR_raise(ERR_LIB_DH
, DH_R_KEYS_NOT_SET
);
411 dh
= (DH
*)EVP_PKEY_get0_DH(ctx
->pkey
);
412 dhpub
= EVP_PKEY_get0_DH(ctx
->peerkey
);
414 ERR_raise(ERR_LIB_DH
, DH_R_KEYS_NOT_SET
);
417 dhpubbn
= dhpub
->pub_key
;
418 if (dctx
->kdf_type
== EVP_PKEY_DH_KDF_NONE
) {
420 *keylen
= DH_size(dh
);
424 ret
= DH_compute_key_padded(key
, dhpubbn
, dh
);
426 ret
= DH_compute_key(key
, dhpubbn
, dh
);
432 else if (dctx
->kdf_type
== EVP_PKEY_DH_KDF_X9_42
) {
434 unsigned char *Z
= NULL
;
437 if (!dctx
->kdf_outlen
|| !dctx
->kdf_oid
)
440 *keylen
= dctx
->kdf_outlen
;
443 if (*keylen
!= dctx
->kdf_outlen
)
446 if ((Zlen
= DH_size(dh
)) <= 0)
448 if ((Z
= OPENSSL_malloc(Zlen
)) == NULL
) {
449 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
452 if (DH_compute_key_padded(Z
, dhpubbn
, dh
) <= 0)
454 if (!DH_KDF_X9_42(key
, *keylen
, Z
, Zlen
, dctx
->kdf_oid
,
455 dctx
->kdf_ukm
, dctx
->kdf_ukmlen
, dctx
->kdf_md
))
457 *keylen
= dctx
->kdf_outlen
;
460 OPENSSL_clear_free(Z
, Zlen
);
466 static const EVP_PKEY_METHOD dh_pkey_meth
= {
500 const EVP_PKEY_METHOD
*ossl_dh_pkey_method(void)
502 return &dh_pkey_meth
;
505 static const EVP_PKEY_METHOD dhx_pkey_meth
= {
539 const EVP_PKEY_METHOD
*ossl_dhx_pkey_method(void)
541 return &dhx_pkey_meth
;