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 low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include <openssl/x509.h>
18 #include <openssl/asn1.h>
19 #include <openssl/bn.h>
20 #include <openssl/core_names.h>
21 #include <openssl/param_build.h>
22 #include "internal/ffc.h"
23 #include "internal/cryptlib.h"
24 #include "crypto/asn1.h"
25 #include "crypto/dh.h"
26 #include "crypto/evp.h"
30 * i2d/d2i like DH parameter functions which use the appropriate routine for
31 * PKCS#3 DH or X9.42 DH.
34 static DH
*d2i_dhp(const EVP_PKEY
*pkey
, const unsigned char **pp
,
38 int is_dhx
= (pkey
->ameth
== &ossl_dhx_asn1_meth
);
41 dh
= d2i_DHxparams(NULL
, pp
, length
);
43 dh
= d2i_DHparams(NULL
, pp
, length
);
48 static int i2d_dhp(const EVP_PKEY
*pkey
, const DH
*a
, unsigned char **pp
)
50 if (pkey
->ameth
== &ossl_dhx_asn1_meth
)
51 return i2d_DHxparams(a
, pp
);
52 return i2d_DHparams(a
, pp
);
55 static void int_dh_free(EVP_PKEY
*pkey
)
57 DH_free(pkey
->pkey
.dh
);
60 static int dh_pub_decode(EVP_PKEY
*pkey
, const X509_PUBKEY
*pubkey
)
62 const unsigned char *p
, *pm
;
66 const ASN1_STRING
*pstr
;
68 ASN1_INTEGER
*public_key
= NULL
;
72 if (!X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &palg
, pubkey
))
74 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
76 if (ptype
!= V_ASN1_SEQUENCE
) {
77 ERR_raise(ERR_LIB_DH
, DH_R_PARAMETER_ENCODING_ERROR
);
85 if ((dh
= d2i_dhp(pkey
, &pm
, pmlen
)) == NULL
) {
86 ERR_raise(ERR_LIB_DH
, DH_R_DECODE_ERROR
);
90 if ((public_key
= d2i_ASN1_INTEGER(NULL
, &p
, pklen
)) == NULL
) {
91 ERR_raise(ERR_LIB_DH
, DH_R_DECODE_ERROR
);
95 /* We have parameters now set public key */
96 if ((dh
->pub_key
= ASN1_INTEGER_to_BN(public_key
, NULL
)) == NULL
) {
97 ERR_raise(ERR_LIB_DH
, DH_R_BN_DECODE_ERROR
);
101 ASN1_INTEGER_free(public_key
);
102 EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, dh
);
106 ASN1_INTEGER_free(public_key
);
111 static int dh_pub_encode(X509_PUBKEY
*pk
, const EVP_PKEY
*pkey
)
115 unsigned char *penc
= NULL
;
118 ASN1_INTEGER
*pub_key
= NULL
;
122 str
= ASN1_STRING_new();
124 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
127 str
->length
= i2d_dhp(pkey
, dh
, &str
->data
);
128 if (str
->length
<= 0) {
129 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
132 ptype
= V_ASN1_SEQUENCE
;
134 pub_key
= BN_to_ASN1_INTEGER(dh
->pub_key
, NULL
);
138 penclen
= i2d_ASN1_INTEGER(pub_key
, &penc
);
140 ASN1_INTEGER_free(pub_key
);
143 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
147 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(pkey
->ameth
->pkey_id
),
148 ptype
, str
, penc
, penclen
))
153 ASN1_STRING_free(str
);
159 * PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in that
160 * the AlgorithmIdentifier contains the parameters, the private key is
161 * explicitly included and the pubkey must be recalculated.
164 static int dh_priv_decode(EVP_PKEY
*pkey
, const PKCS8_PRIV_KEY_INFO
*p8
)
167 DH
*dh
= ossl_dh_key_from_pkcs8(p8
, NULL
, NULL
);
171 EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, dh
);
177 static int dh_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
179 ASN1_STRING
*params
= NULL
;
180 ASN1_INTEGER
*prkey
= NULL
;
181 unsigned char *dp
= NULL
;
184 params
= ASN1_STRING_new();
186 if (params
== NULL
) {
187 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
191 params
->length
= i2d_dhp(pkey
, pkey
->pkey
.dh
, ¶ms
->data
);
192 if (params
->length
<= 0) {
193 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
196 params
->type
= V_ASN1_SEQUENCE
;
198 /* Get private key into integer */
199 prkey
= BN_to_ASN1_INTEGER(pkey
->pkey
.dh
->priv_key
, NULL
);
202 ERR_raise(ERR_LIB_DH
, DH_R_BN_ERROR
);
206 dplen
= i2d_ASN1_INTEGER(prkey
, &dp
);
208 ASN1_STRING_clear_free(prkey
);
211 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(pkey
->ameth
->pkey_id
), 0,
212 V_ASN1_SEQUENCE
, params
, dp
, dplen
))
219 ASN1_STRING_free(params
);
220 ASN1_STRING_clear_free(prkey
);
224 static int dh_param_decode(EVP_PKEY
*pkey
,
225 const unsigned char **pder
, int derlen
)
229 if ((dh
= d2i_dhp(pkey
, pder
, derlen
)) == NULL
)
232 EVP_PKEY_assign(pkey
, pkey
->ameth
->pkey_id
, dh
);
236 static int dh_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
238 return i2d_dhp(pkey
, pkey
->pkey
.dh
, pder
);
241 static int do_dh_print(BIO
*bp
, const DH
*x
, int indent
, int ptype
)
243 int reason
= ERR_R_BUF_LIB
;
244 const char *ktype
= NULL
;
245 BIGNUM
*priv_key
, *pub_key
;
248 priv_key
= x
->priv_key
;
253 pub_key
= x
->pub_key
;
257 if (x
->params
.p
== NULL
|| (ptype
== 2 && priv_key
== NULL
)
258 || (ptype
> 0 && pub_key
== NULL
)) {
259 reason
= ERR_R_PASSED_NULL_PARAMETER
;
264 ktype
= "DH Private-Key";
266 ktype
= "DH Public-Key";
268 ktype
= "DH Parameters";
270 if (!BIO_indent(bp
, indent
, 128)
271 || BIO_printf(bp
, "%s: (%d bit)\n", ktype
, DH_bits(x
)) <= 0)
275 if (!ASN1_bn_print(bp
, "private-key:", priv_key
, NULL
, indent
))
277 if (!ASN1_bn_print(bp
, "public-key:", pub_key
, NULL
, indent
))
280 if (!ossl_ffc_params_print(bp
, &x
->params
, indent
))
283 if (x
->length
!= 0) {
284 if (!BIO_indent(bp
, indent
, 128)
285 || BIO_printf(bp
, "recommended-private-length: %d bits\n",
286 (int)x
->length
) <= 0)
293 ERR_raise(ERR_LIB_DH
, reason
);
297 static int int_dh_size(const EVP_PKEY
*pkey
)
299 return DH_size(pkey
->pkey
.dh
);
302 static int dh_bits(const EVP_PKEY
*pkey
)
304 return DH_bits(pkey
->pkey
.dh
);
307 static int dh_security_bits(const EVP_PKEY
*pkey
)
309 return DH_security_bits(pkey
->pkey
.dh
);
312 static int dh_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
314 return ossl_ffc_params_cmp(&a
->pkey
.dh
->params
, &a
->pkey
.dh
->params
,
315 a
->ameth
!= &ossl_dhx_asn1_meth
);
318 static int int_dh_param_copy(DH
*to
, const DH
*from
, int is_x942
)
321 is_x942
= (from
->params
.q
!= NULL
);
322 if (!ossl_ffc_params_copy(&to
->params
, &from
->params
))
325 to
->length
= from
->length
;
330 DH
*DHparams_dup(const DH
*dh
)
336 if (!int_dh_param_copy(ret
, dh
, -1)) {
343 static int dh_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
345 if (to
->pkey
.dh
== NULL
) {
346 to
->pkey
.dh
= DH_new();
347 if (to
->pkey
.dh
== NULL
)
350 return int_dh_param_copy(to
->pkey
.dh
, from
->pkey
.dh
,
351 from
->ameth
== &ossl_dhx_asn1_meth
);
354 static int dh_missing_parameters(const EVP_PKEY
*a
)
356 return a
->pkey
.dh
== NULL
357 || a
->pkey
.dh
->params
.p
== NULL
358 || a
->pkey
.dh
->params
.g
== NULL
;
361 static int dh_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
363 if (dh_cmp_parameters(a
, b
) == 0)
365 if (BN_cmp(b
->pkey
.dh
->pub_key
, a
->pkey
.dh
->pub_key
) != 0)
371 static int dh_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
374 return do_dh_print(bp
, pkey
->pkey
.dh
, indent
, 0);
377 static int dh_public_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
380 return do_dh_print(bp
, pkey
->pkey
.dh
, indent
, 1);
383 static int dh_private_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
386 return do_dh_print(bp
, pkey
->pkey
.dh
, indent
, 2);
389 int DHparams_print(BIO
*bp
, const DH
*x
)
391 return do_dh_print(bp
, x
, 4, 0);
394 static int dh_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
397 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT
:
398 /* We should only be here if we have a legacy key */
399 if (!ossl_assert(evp_pkey_is_legacy(pkey
)))
401 return ossl_dh_buf2key(evp_pkey_get0_DH_int(pkey
), arg2
, arg1
);
402 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT
:
403 return ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey
), arg2
, 0, 1);
409 static int dhx_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
418 static int dh_pkey_public_check(const EVP_PKEY
*pkey
)
420 DH
*dh
= pkey
->pkey
.dh
;
422 if (dh
->pub_key
== NULL
) {
423 ERR_raise(ERR_LIB_DH
, DH_R_MISSING_PUBKEY
);
427 return DH_check_pub_key_ex(dh
, dh
->pub_key
);
430 static int dh_pkey_param_check(const EVP_PKEY
*pkey
)
432 DH
*dh
= pkey
->pkey
.dh
;
434 return DH_check_ex(dh
);
437 static size_t dh_pkey_dirty_cnt(const EVP_PKEY
*pkey
)
439 return pkey
->pkey
.dh
->dirty_cnt
;
442 static int dh_pkey_export_to(const EVP_PKEY
*from
, void *to_keydata
,
443 EVP_KEYMGMT
*to_keymgmt
, OSSL_LIB_CTX
*libctx
,
446 DH
*dh
= from
->pkey
.dh
;
447 OSSL_PARAM_BLD
*tmpl
;
448 const BIGNUM
*p
= DH_get0_p(dh
), *g
= DH_get0_g(dh
), *q
= DH_get0_q(dh
);
449 long l
= DH_get_length(dh
);
450 const BIGNUM
*pub_key
= DH_get0_pub_key(dh
);
451 const BIGNUM
*priv_key
= DH_get0_priv_key(dh
);
452 OSSL_PARAM
*params
= NULL
;
457 * If the DH method is foreign, then we can't be sure of anything, and
458 * can therefore not export or pretend to export.
460 if (ossl_dh_get_method(dh
) != DH_OpenSSL())
463 if (p
== NULL
|| g
== NULL
)
466 tmpl
= OSSL_PARAM_BLD_new();
469 if (!OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_P
, p
)
470 || !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_G
, g
))
473 if (!OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_Q
, q
))
476 selection
|= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
;
478 if (!OSSL_PARAM_BLD_push_long(tmpl
, OSSL_PKEY_PARAM_DH_PRIV_LEN
, l
))
480 selection
|= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
;
482 if (pub_key
!= NULL
) {
483 if (!OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_PUB_KEY
, pub_key
))
485 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
487 if (priv_key
!= NULL
) {
488 if (!OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_PRIV_KEY
,
491 selection
|= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
494 if ((params
= OSSL_PARAM_BLD_to_param(tmpl
)) == NULL
)
497 /* We export, the provider imports */
498 rv
= evp_keymgmt_import(to_keymgmt
, to_keydata
, selection
, params
);
500 OSSL_PARAM_free(params
);
502 OSSL_PARAM_BLD_free(tmpl
);
506 static int dh_pkey_import_from_type(const OSSL_PARAM params
[], void *vpctx
,
509 EVP_PKEY_CTX
*pctx
= vpctx
;
510 EVP_PKEY
*pkey
= EVP_PKEY_CTX_get0_pkey(pctx
);
511 DH
*dh
= ossl_dh_new_ex(pctx
->libctx
);
514 ERR_raise(ERR_LIB_DH
, ERR_R_MALLOC_FAILURE
);
517 DH_clear_flags(dh
, DH_FLAG_TYPE_MASK
);
518 DH_set_flags(dh
, type
== EVP_PKEY_DH
? DH_FLAG_TYPE_DH
: DH_FLAG_TYPE_DHX
);
520 if (!ossl_dh_params_fromdata(dh
, params
)
521 || !ossl_dh_key_fromdata(dh
, params
)
522 || !EVP_PKEY_assign(pkey
, type
, dh
)) {
529 static int dh_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
531 return dh_pkey_import_from_type(params
, vpctx
, EVP_PKEY_DH
);
534 static int dhx_pkey_import_from(const OSSL_PARAM params
[], void *vpctx
)
536 return dh_pkey_import_from_type(params
, vpctx
, EVP_PKEY_DHX
);
539 static ossl_inline
int dh_bn_dup_check(BIGNUM
**out
, const BIGNUM
*f
)
541 if (f
!= NULL
&& (*out
= BN_dup(f
)) == NULL
)
546 static DH
*dh_dup(const DH
*dh
)
550 /* Do not try to duplicate foreign DH keys */
551 if (ossl_dh_get_method(dh
) != DH_OpenSSL())
554 if ((dupkey
= ossl_dh_new_ex(dh
->libctx
)) == NULL
)
557 dupkey
->length
= DH_get_length(dh
);
558 if (!ossl_ffc_params_copy(&dupkey
->params
, &dh
->params
))
561 dupkey
->flags
= dh
->flags
;
563 if (!dh_bn_dup_check(&dupkey
->pub_key
, dh
->pub_key
))
565 if (!dh_bn_dup_check(&dupkey
->priv_key
, dh
->priv_key
))
568 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DH
,
569 &dupkey
->ex_data
, &dh
->ex_data
))
579 static int dh_pkey_copy(EVP_PKEY
*to
, EVP_PKEY
*from
)
581 DH
*dh
= from
->pkey
.dh
;
591 ret
= EVP_PKEY_assign(to
, from
->type
, dupkey
);
597 const EVP_PKEY_ASN1_METHOD ossl_dh_asn1_meth
= {
603 "OpenSSL PKCS#3 DH method",
620 dh_missing_parameters
,
632 dh_pkey_public_check
,
643 const EVP_PKEY_ASN1_METHOD ossl_dhx_asn1_meth
= {
649 "OpenSSL X9.42 DH method",
666 dh_missing_parameters
,
678 dh_pkey_public_check
,
683 dhx_pkey_import_from
,