2 * Copyright 2019 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"
16 #include <openssl/core_numbers.h>
17 #include <openssl/core_names.h>
18 #include <openssl/bn.h>
19 #include <openssl/dh.h>
20 #include <openssl/params.h>
21 #include "prov/implementations.h"
22 #include "prov/providercommon.h"
23 #include "prov/provider_ctx.h"
24 #include "crypto/dh.h"
25 #include "openssl/param_build.h"
27 static OSSL_OP_keymgmt_new_fn dh_newdata
;
28 static OSSL_OP_keymgmt_free_fn dh_freedata
;
29 static OSSL_OP_keymgmt_get_params_fn dh_get_params
;
30 static OSSL_OP_keymgmt_gettable_params_fn dh_gettable_params
;
31 static OSSL_OP_keymgmt_has_fn dh_has
;
32 static OSSL_OP_keymgmt_match_fn dh_match
;
33 static OSSL_OP_keymgmt_validate_fn dh_validate
;
34 static OSSL_OP_keymgmt_import_fn dh_import
;
35 static OSSL_OP_keymgmt_import_types_fn dh_import_types
;
36 static OSSL_OP_keymgmt_export_fn dh_export
;
37 static OSSL_OP_keymgmt_export_types_fn dh_export_types
;
39 #define DH_POSSIBLE_SELECTIONS \
40 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
42 static int domparams_to_params(DH
*dh
, OSSL_PARAM_BLD
*tmpl
)
44 const BIGNUM
*dh_p
= NULL
, *dh_g
= NULL
;
49 DH_get0_pqg(dh
, &dh_p
, NULL
, &dh_g
);
51 && !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_P
, dh_p
))
54 && !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_FFC_G
, dh_g
))
60 static int key_to_params(DH
*dh
, OSSL_PARAM_BLD
*tmpl
)
62 const BIGNUM
*priv_key
= NULL
, *pub_key
= NULL
;
66 if (!domparams_to_params(dh
, tmpl
))
69 DH_get0_key(dh
, &pub_key
, &priv_key
);
71 && !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_PRIV_KEY
, priv_key
))
74 && !OSSL_PARAM_BLD_push_BN(tmpl
, OSSL_PKEY_PARAM_PUB_KEY
, pub_key
))
80 static void *dh_newdata(void *provctx
)
82 return dh_new_with_ctx(PROV_LIBRARY_CONTEXT_OF(provctx
));
85 static void dh_freedata(void *keydata
)
90 static int dh_has(void *keydata
, int selection
)
96 if ((selection
& DH_POSSIBLE_SELECTIONS
) != 0)
99 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
100 ok
= ok
&& (DH_get0_pub_key(dh
) != NULL
);
101 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
102 ok
= ok
&& (DH_get0_priv_key(dh
) != NULL
);
103 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
104 ok
= ok
&& (DH_get0_p(dh
) != NULL
&& DH_get0_g(dh
) != NULL
);
109 static int dh_match(const void *keydata1
, const void *keydata2
, int selection
)
111 const DH
*dh1
= keydata1
;
112 const DH
*dh2
= keydata2
;
115 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
116 ok
= ok
&& BN_cmp(DH_get0_pub_key(dh1
), DH_get0_pub_key(dh2
)) == 0;
117 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
118 ok
= ok
&& BN_cmp(DH_get0_priv_key(dh1
), DH_get0_priv_key(dh2
)) == 0;
119 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0) {
120 FFC_PARAMS
*dhparams1
= dh_get0_params((DH
*)dh1
);
121 FFC_PARAMS
*dhparams2
= dh_get0_params((DH
*)dh2
);
123 ok
= ok
&& ffc_params_cmp(dhparams1
, dhparams2
, 1);
128 static int dh_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
136 if ((selection
& DH_POSSIBLE_SELECTIONS
) != 0)
139 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
140 ok
= ok
&& ffc_fromdata(dh_get0_params(dh
), params
);
141 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
142 ok
= ok
&& dh_key_fromdata(dh
, params
);
147 static int dh_export(void *keydata
, int selection
, OSSL_CALLBACK
*param_cb
,
151 OSSL_PARAM_BLD
*tmpl
;
152 OSSL_PARAM
*params
= NULL
;
158 tmpl
= OSSL_PARAM_BLD_new();
162 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
163 ok
= ok
&& domparams_to_params(dh
, tmpl
);
164 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
165 ok
= ok
&& key_to_params(dh
, tmpl
);
168 || (params
= OSSL_PARAM_BLD_to_param(tmpl
)) == NULL
) {
169 OSSL_PARAM_BLD_free(tmpl
);
172 OSSL_PARAM_BLD_free(tmpl
);
174 ok
= param_cb(params
, cbarg
);
175 OSSL_PARAM_BLD_free_params(params
);
179 /* IMEXPORT = IMPORT + EXPORT */
181 # define DH_IMEXPORTABLE_PARAMETERS \
182 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
183 OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0)
184 # define DH_IMEXPORTABLE_PUBLIC_KEY \
185 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
186 # define DH_IMEXPORTABLE_PRIVATE_KEY \
187 OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
188 static const OSSL_PARAM dh_all_types
[] = {
189 DH_IMEXPORTABLE_PARAMETERS
,
190 DH_IMEXPORTABLE_PUBLIC_KEY
,
191 DH_IMEXPORTABLE_PRIVATE_KEY
,
194 static const OSSL_PARAM dh_parameter_types
[] = {
195 DH_IMEXPORTABLE_PARAMETERS
,
198 static const OSSL_PARAM dh_key_types
[] = {
199 DH_IMEXPORTABLE_PUBLIC_KEY
,
200 DH_IMEXPORTABLE_PRIVATE_KEY
,
203 static const OSSL_PARAM
*dh_types
[] = {
204 NULL
, /* Index 0 = none of them */
205 dh_parameter_types
, /* Index 1 = parameter types */
206 dh_key_types
, /* Index 2 = key types */
207 dh_all_types
/* Index 3 = 1 + 2 */
210 static const OSSL_PARAM
*dh_imexport_types(int selection
)
214 if ((selection
& OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
) != 0)
216 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
218 return dh_types
[type_select
];
221 static const OSSL_PARAM
*dh_import_types(int selection
)
223 return dh_imexport_types(selection
);
226 static const OSSL_PARAM
*dh_export_types(int selection
)
228 return dh_imexport_types(selection
);
231 static ossl_inline
int dh_get_params(void *key
, OSSL_PARAM params
[])
236 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
237 && !OSSL_PARAM_set_int(p
, DH_bits(dh
)))
239 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
240 && !OSSL_PARAM_set_int(p
, DH_security_bits(dh
)))
242 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
243 && !OSSL_PARAM_set_int(p
, DH_size(dh
)))
248 static const OSSL_PARAM dh_params
[] = {
249 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
250 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
251 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
255 static const OSSL_PARAM
*dh_gettable_params(void)
260 static int dh_validate_public(DH
*dh
)
262 const BIGNUM
*pub_key
= NULL
;
264 DH_get0_key(dh
, &pub_key
, NULL
);
265 return DH_check_pub_key_ex(dh
, pub_key
);
268 static int dh_validate_private(DH
*dh
)
271 const BIGNUM
*priv_key
= NULL
;
273 DH_get0_key(dh
, NULL
, &priv_key
);
274 return dh_check_priv_key(dh
, priv_key
, &status
);;
277 static int dh_validate(void *keydata
, int selection
)
282 if ((selection
& DH_POSSIBLE_SELECTIONS
) != 0)
285 if ((selection
& OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) != 0)
286 ok
= ok
&& DH_check_params_ex(dh
);
288 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
289 ok
= ok
&& dh_validate_public(dh
);
291 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
292 ok
= ok
&& dh_validate_private(dh
);
294 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
)
295 == OSSL_KEYMGMT_SELECT_KEYPAIR
)
296 ok
= ok
&& dh_check_pairwise(dh
);
300 const OSSL_DISPATCH dh_keymgmt_functions
[] = {
301 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))dh_newdata
},
302 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))dh_freedata
},
303 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))dh_get_params
},
304 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))dh_gettable_params
},
305 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))dh_has
},
306 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))dh_match
},
307 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))dh_validate
},
308 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))dh_import
},
309 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))dh_import_types
},
310 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))dh_export
},
311 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))dh_export_types
},