]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
fecb3aae | 2 | * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved. |
adbc603d | 3 | * |
e38873f5 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
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 | |
adbc603d DSH |
8 | */ |
9 | ||
ada66e78 P |
10 | /* |
11 | * DH low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
adbc603d | 16 | #include <stdio.h> |
adbc603d DSH |
17 | #include <openssl/x509.h> |
18 | #include <openssl/asn1.h> | |
1e26a8ba | 19 | #include <openssl/bn.h> |
8b84b075 | 20 | #include <openssl/core_names.h> |
b03ec3b5 | 21 | #include <openssl/param_build.h> |
0abae163 | 22 | #include "internal/ffc.h" |
0b3a4ef2 MC |
23 | #include "internal/cryptlib.h" |
24 | #include "crypto/asn1.h" | |
25 | #include "crypto/dh.h" | |
26 | #include "crypto/evp.h" | |
27 | #include "dh_local.h" | |
adbc603d | 28 | |
0f113f3e MC |
29 | /* |
30 | * i2d/d2i like DH parameter functions which use the appropriate routine for | |
31 | * PKCS#3 DH or X9.42 DH. | |
afb14cda DSH |
32 | */ |
33 | ||
0f113f3e MC |
34 | static DH *d2i_dhp(const EVP_PKEY *pkey, const unsigned char **pp, |
35 | long length) | |
36 | { | |
31d2daec | 37 | DH *dh = NULL; |
adf7e6d1 | 38 | int is_dhx = (pkey->ameth == &ossl_dhx_asn1_meth); |
31d2daec SL |
39 | |
40 | if (is_dhx) | |
41 | dh = d2i_DHxparams(NULL, pp, length); | |
42 | else | |
43 | dh = d2i_DHparams(NULL, pp, length); | |
44 | ||
31d2daec | 45 | return dh; |
0f113f3e | 46 | } |
afb14cda DSH |
47 | |
48 | static int i2d_dhp(const EVP_PKEY *pkey, const DH *a, unsigned char **pp) | |
0f113f3e | 49 | { |
adf7e6d1 | 50 | if (pkey->ameth == &ossl_dhx_asn1_meth) |
0f113f3e MC |
51 | return i2d_DHxparams(a, pp); |
52 | return i2d_DHparams(a, pp); | |
53 | } | |
afb14cda | 54 | |
adbc603d | 55 | static void int_dh_free(EVP_PKEY *pkey) |
0f113f3e MC |
56 | { |
57 | DH_free(pkey->pkey.dh); | |
58 | } | |
adbc603d | 59 | |
7674e923 | 60 | static int dh_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey) |
0f113f3e MC |
61 | { |
62 | const unsigned char *p, *pm; | |
63 | int pklen, pmlen; | |
64 | int ptype; | |
ac4e2577 DSH |
65 | const void *pval; |
66 | const ASN1_STRING *pstr; | |
0f113f3e MC |
67 | X509_ALGOR *palg; |
68 | ASN1_INTEGER *public_key = NULL; | |
69 | ||
70 | DH *dh = NULL; | |
71 | ||
72 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | |
73 | return 0; | |
74 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
75 | ||
76 | if (ptype != V_ASN1_SEQUENCE) { | |
9311d0c4 | 77 | ERR_raise(ERR_LIB_DH, DH_R_PARAMETER_ENCODING_ERROR); |
0f113f3e MC |
78 | goto err; |
79 | } | |
80 | ||
81 | pstr = pval; | |
82 | pm = pstr->data; | |
83 | pmlen = pstr->length; | |
84 | ||
75ebbd9a | 85 | if ((dh = d2i_dhp(pkey, &pm, pmlen)) == NULL) { |
9311d0c4 | 86 | ERR_raise(ERR_LIB_DH, DH_R_DECODE_ERROR); |
0f113f3e MC |
87 | goto err; |
88 | } | |
89 | ||
75ebbd9a | 90 | if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) { |
9311d0c4 | 91 | ERR_raise(ERR_LIB_DH, DH_R_DECODE_ERROR); |
0f113f3e MC |
92 | goto err; |
93 | } | |
94 | ||
95 | /* We have parameters now set public key */ | |
75ebbd9a | 96 | if ((dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) { |
9311d0c4 | 97 | ERR_raise(ERR_LIB_DH, DH_R_BN_DECODE_ERROR); |
0f113f3e MC |
98 | goto err; |
99 | } | |
100 | ||
101 | ASN1_INTEGER_free(public_key); | |
102 | EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh); | |
103 | return 1; | |
104 | ||
105 | err: | |
2ace7450 | 106 | ASN1_INTEGER_free(public_key); |
d6407083 | 107 | DH_free(dh); |
0f113f3e | 108 | return 0; |
0f113f3e | 109 | } |
4c97a04e | 110 | |
0f113f3e MC |
111 | static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
112 | { | |
113 | DH *dh; | |
0f113f3e MC |
114 | int ptype; |
115 | unsigned char *penc = NULL; | |
116 | int penclen; | |
117 | ASN1_STRING *str; | |
118 | ASN1_INTEGER *pub_key = NULL; | |
119 | ||
120 | dh = pkey->pkey.dh; | |
121 | ||
122 | str = ASN1_STRING_new(); | |
90945fa3 | 123 | if (str == NULL) { |
9311d0c4 | 124 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
6aa8dab2 MC |
125 | goto err; |
126 | } | |
0f113f3e MC |
127 | str->length = i2d_dhp(pkey, dh, &str->data); |
128 | if (str->length <= 0) { | |
9311d0c4 | 129 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
130 | goto err; |
131 | } | |
0f113f3e MC |
132 | ptype = V_ASN1_SEQUENCE; |
133 | ||
134 | pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL); | |
12a765a5 | 135 | if (pub_key == NULL) |
0f113f3e MC |
136 | goto err; |
137 | ||
138 | penclen = i2d_ASN1_INTEGER(pub_key, &penc); | |
139 | ||
140 | ASN1_INTEGER_free(pub_key); | |
141 | ||
142 | if (penclen <= 0) { | |
9311d0c4 | 143 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
144 | goto err; |
145 | } | |
146 | ||
147 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id), | |
6aa8dab2 | 148 | ptype, str, penc, penclen)) |
0f113f3e MC |
149 | return 1; |
150 | ||
151 | err: | |
b548a1f1 | 152 | OPENSSL_free(penc); |
0dfb9398 | 153 | ASN1_STRING_free(str); |
0f113f3e MC |
154 | |
155 | return 0; | |
156 | } | |
4c97a04e | 157 | |
0f113f3e MC |
158 | /* |
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 | |
0d4fb843 | 161 | * explicitly included and the pubkey must be recalculated. |
0f113f3e | 162 | */ |
4c97a04e | 163 | |
245c6bc3 | 164 | static int dh_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) |
0f113f3e | 165 | { |
cf333799 RL |
166 | int ret = 0; |
167 | DH *dh = ossl_dh_key_from_pkcs8(p8, NULL, NULL); | |
0f113f3e | 168 | |
cf333799 RL |
169 | if (dh != NULL) { |
170 | ret = 1; | |
171 | EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh); | |
0f113f3e | 172 | } |
0f113f3e | 173 | |
cf333799 | 174 | return ret; |
0f113f3e | 175 | } |
4c97a04e DSH |
176 | |
177 | static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) | |
178 | { | |
0f113f3e MC |
179 | ASN1_STRING *params = NULL; |
180 | ASN1_INTEGER *prkey = NULL; | |
181 | unsigned char *dp = NULL; | |
182 | int dplen; | |
183 | ||
184 | params = ASN1_STRING_new(); | |
185 | ||
90945fa3 | 186 | if (params == NULL) { |
9311d0c4 | 187 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
188 | goto err; |
189 | } | |
190 | ||
191 | params->length = i2d_dhp(pkey, pkey->pkey.dh, ¶ms->data); | |
192 | if (params->length <= 0) { | |
9311d0c4 | 193 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
194 | goto err; |
195 | } | |
196 | params->type = V_ASN1_SEQUENCE; | |
197 | ||
198 | /* Get private key into integer */ | |
199 | prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL); | |
200 | ||
12a765a5 | 201 | if (prkey == NULL) { |
9311d0c4 | 202 | ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR); |
0f113f3e MC |
203 | goto err; |
204 | } | |
205 | ||
206 | dplen = i2d_ASN1_INTEGER(prkey, &dp); | |
207 | ||
a8ae0891 | 208 | ASN1_STRING_clear_free(prkey); |
1549a265 | 209 | prkey = NULL; |
0f113f3e MC |
210 | |
211 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0, | |
212 | V_ASN1_SEQUENCE, params, dp, dplen)) | |
213 | goto err; | |
214 | ||
215 | return 1; | |
216 | ||
217 | err: | |
b548a1f1 | 218 | OPENSSL_free(dp); |
0dfb9398 | 219 | ASN1_STRING_free(params); |
2ace7450 | 220 | ASN1_STRING_clear_free(prkey); |
0f113f3e | 221 | return 0; |
4c97a04e DSH |
222 | } |
223 | ||
3e4585c8 | 224 | static int dh_param_decode(EVP_PKEY *pkey, |
0f113f3e MC |
225 | const unsigned char **pder, int derlen) |
226 | { | |
227 | DH *dh; | |
75ebbd9a | 228 | |
29844ea5 | 229 | if ((dh = d2i_dhp(pkey, pder, derlen)) == NULL) |
0f113f3e | 230 | return 0; |
8b84b075 | 231 | dh->dirty_cnt++; |
0f113f3e MC |
232 | EVP_PKEY_assign(pkey, pkey->ameth->pkey_id, dh); |
233 | return 1; | |
234 | } | |
3e4585c8 DSH |
235 | |
236 | static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
237 | { |
238 | return i2d_dhp(pkey, pkey->pkey.dh, pder); | |
239 | } | |
3e4585c8 | 240 | |
a773b52a | 241 | static int do_dh_print(BIO *bp, const DH *x, int indent, int ptype) |
0f113f3e | 242 | { |
66696478 | 243 | int reason = ERR_R_BUF_LIB; |
0f113f3e | 244 | const char *ktype = NULL; |
0f113f3e MC |
245 | BIGNUM *priv_key, *pub_key; |
246 | ||
247 | if (ptype == 2) | |
248 | priv_key = x->priv_key; | |
249 | else | |
250 | priv_key = NULL; | |
251 | ||
252 | if (ptype > 0) | |
253 | pub_key = x->pub_key; | |
254 | else | |
255 | pub_key = NULL; | |
256 | ||
dc8de3e6 | 257 | if (x->params.p == NULL || (ptype == 2 && priv_key == NULL) |
1d54ef34 | 258 | || (ptype > 0 && pub_key == NULL)) { |
0f113f3e MC |
259 | reason = ERR_R_PASSED_NULL_PARAMETER; |
260 | goto err; | |
261 | } | |
262 | ||
0f113f3e MC |
263 | if (ptype == 2) |
264 | ktype = "DH Private-Key"; | |
265 | else if (ptype == 1) | |
266 | ktype = "DH Public-Key"; | |
267 | else | |
268 | ktype = "DH Parameters"; | |
269 | ||
756f5c6c | 270 | if (!BIO_indent(bp, indent, 128) |
dc8de3e6 | 271 | || BIO_printf(bp, "%s: (%d bit)\n", ktype, DH_bits(x)) <= 0) |
0f113f3e MC |
272 | goto err; |
273 | indent += 4; | |
274 | ||
a773b52a | 275 | if (!ASN1_bn_print(bp, "private-key:", priv_key, NULL, indent)) |
0f113f3e | 276 | goto err; |
a773b52a | 277 | if (!ASN1_bn_print(bp, "public-key:", pub_key, NULL, indent)) |
0f113f3e MC |
278 | goto err; |
279 | ||
5357c106 | 280 | if (!ossl_ffc_params_print(bp, &x->params, indent)) |
0f113f3e | 281 | goto err; |
756f5c6c | 282 | |
0f113f3e | 283 | if (x->length != 0) { |
756f5c6c P |
284 | if (!BIO_indent(bp, indent, 128) |
285 | || BIO_printf(bp, "recommended-private-length: %d bits\n", | |
286 | (int)x->length) <= 0) | |
0f113f3e MC |
287 | goto err; |
288 | } | |
289 | ||
66696478 RS |
290 | return 1; |
291 | ||
0f113f3e | 292 | err: |
9311d0c4 | 293 | ERR_raise(ERR_LIB_DH, reason); |
66696478 | 294 | return 0; |
0f113f3e | 295 | } |
3e4585c8 | 296 | |
ceb46789 | 297 | static int int_dh_size(const EVP_PKEY *pkey) |
0f113f3e | 298 | { |
26a7d938 | 299 | return DH_size(pkey->pkey.dh); |
0f113f3e | 300 | } |
ceb46789 DSH |
301 | |
302 | static int dh_bits(const EVP_PKEY *pkey) | |
0f113f3e | 303 | { |
dc8de3e6 | 304 | return DH_bits(pkey->pkey.dh); |
0f113f3e | 305 | } |
ceb46789 | 306 | |
2514fa79 | 307 | static int dh_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
308 | { |
309 | return DH_security_bits(pkey->pkey.dh); | |
310 | } | |
2514fa79 | 311 | |
ffb1ac67 | 312 | static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e | 313 | { |
cf1a231d | 314 | return ossl_ffc_params_cmp(&a->pkey.dh->params, &b->pkey.dh->params, |
adf7e6d1 | 315 | a->ameth != &ossl_dhx_asn1_meth); |
0f113f3e | 316 | } |
ceb46789 | 317 | |
d3cc91ee | 318 | static int int_dh_param_copy(DH *to, const DH *from, int is_x942) |
0f113f3e MC |
319 | { |
320 | if (is_x942 == -1) | |
dc8de3e6 | 321 | is_x942 = (from->params.q != NULL); |
5357c106 | 322 | if (!ossl_ffc_params_copy(&to->params, &from->params)) |
0f113f3e | 323 | return 0; |
dc8de3e6 | 324 | if (!is_x942) |
0f113f3e | 325 | to->length = from->length; |
8b84b075 | 326 | to->dirty_cnt++; |
0f113f3e MC |
327 | return 1; |
328 | } | |
d3cc91ee | 329 | |
9fdcc21f | 330 | DH *DHparams_dup(const DH *dh) |
0f113f3e MC |
331 | { |
332 | DH *ret; | |
333 | ret = DH_new(); | |
90945fa3 | 334 | if (ret == NULL) |
0f113f3e MC |
335 | return NULL; |
336 | if (!int_dh_param_copy(ret, dh, -1)) { | |
337 | DH_free(ret); | |
338 | return NULL; | |
339 | } | |
340 | return ret; | |
341 | } | |
d3cc91ee DSH |
342 | |
343 | static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) | |
0f113f3e | 344 | { |
2986ecdc DSH |
345 | if (to->pkey.dh == NULL) { |
346 | to->pkey.dh = DH_new(); | |
347 | if (to->pkey.dh == NULL) | |
348 | return 0; | |
349 | } | |
0f113f3e | 350 | return int_dh_param_copy(to->pkey.dh, from->pkey.dh, |
adf7e6d1 | 351 | from->ameth == &ossl_dhx_asn1_meth); |
0f113f3e | 352 | } |
d3cc91ee | 353 | |
ffb1ac67 | 354 | static int dh_missing_parameters(const EVP_PKEY *a) |
0f113f3e | 355 | { |
dc8de3e6 SL |
356 | return a->pkey.dh == NULL |
357 | || a->pkey.dh->params.p == NULL | |
358 | || a->pkey.dh->params.g == NULL; | |
0f113f3e | 359 | } |
ceb46789 DSH |
360 | |
361 | static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) | |
0f113f3e MC |
362 | { |
363 | if (dh_cmp_parameters(a, b) == 0) | |
364 | return 0; | |
365 | if (BN_cmp(b->pkey.dh->pub_key, a->pkey.dh->pub_key) != 0) | |
366 | return 0; | |
367 | else | |
368 | return 1; | |
369 | } | |
ceb46789 | 370 | |
3e4585c8 | 371 | static int dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, |
0f113f3e MC |
372 | ASN1_PCTX *ctx) |
373 | { | |
a773b52a | 374 | return do_dh_print(bp, pkey->pkey.dh, indent, 0); |
0f113f3e | 375 | } |
ceb46789 DSH |
376 | |
377 | static int dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
378 | ASN1_PCTX *ctx) |
379 | { | |
a773b52a | 380 | return do_dh_print(bp, pkey->pkey.dh, indent, 1); |
0f113f3e | 381 | } |
ceb46789 DSH |
382 | |
383 | static int dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
384 | ASN1_PCTX *ctx) |
385 | { | |
a773b52a | 386 | return do_dh_print(bp, pkey->pkey.dh, indent, 2); |
0f113f3e | 387 | } |
3e4585c8 DSH |
388 | |
389 | int DHparams_print(BIO *bp, const DH *x) | |
0f113f3e | 390 | { |
a773b52a | 391 | return do_dh_print(bp, x, 4, 0); |
0f113f3e | 392 | } |
3e4585c8 | 393 | |
bd59f2b9 | 394 | static int dh_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
9aaecbfc | 395 | { |
396 | switch (op) { | |
397 | case ASN1_PKEY_CTRL_SET1_TLS_ENCPT: | |
7bc0fdd3 MC |
398 | /* We should only be here if we have a legacy key */ |
399 | if (!ossl_assert(evp_pkey_is_legacy(pkey))) | |
400 | return 0; | |
401 | return ossl_dh_buf2key(evp_pkey_get0_DH_int(pkey), arg2, arg1); | |
9aaecbfc | 402 | case ASN1_PKEY_CTRL_GET1_TLS_ENCPT: |
19dbb742 | 403 | return ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey), arg2, 0, 1); |
9aaecbfc | 404 | default: |
405 | return -2; | |
406 | } | |
407 | } | |
408 | ||
409 | static int dhx_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) | |
0f113f3e MC |
410 | { |
411 | switch (op) { | |
0f113f3e MC |
412 | default: |
413 | return -2; | |
414 | } | |
415 | ||
416 | } | |
417 | ||
b0004708 PY |
418 | static int dh_pkey_public_check(const EVP_PKEY *pkey) |
419 | { | |
420 | DH *dh = pkey->pkey.dh; | |
421 | ||
422 | if (dh->pub_key == NULL) { | |
9311d0c4 | 423 | ERR_raise(ERR_LIB_DH, DH_R_MISSING_PUBKEY); |
b0004708 PY |
424 | return 0; |
425 | } | |
426 | ||
427 | return DH_check_pub_key_ex(dh, dh->pub_key); | |
428 | } | |
429 | ||
430 | static int dh_pkey_param_check(const EVP_PKEY *pkey) | |
431 | { | |
432 | DH *dh = pkey->pkey.dh; | |
433 | ||
434 | return DH_check_ex(dh); | |
435 | } | |
436 | ||
8b84b075 RL |
437 | static size_t dh_pkey_dirty_cnt(const EVP_PKEY *pkey) |
438 | { | |
439 | return pkey->pkey.dh->dirty_cnt; | |
440 | } | |
441 | ||
b305452f | 442 | static int dh_pkey_export_to(const EVP_PKEY *from, void *to_keydata, |
bed7437b RL |
443 | OSSL_FUNC_keymgmt_import_fn *importer, |
444 | OSSL_LIB_CTX *libctx, const char *propq) | |
8b84b075 | 445 | { |
b305452f | 446 | DH *dh = from->pkey.dh; |
6d4e6009 | 447 | OSSL_PARAM_BLD *tmpl; |
8b84b075 | 448 | const BIGNUM *p = DH_get0_p(dh), *g = DH_get0_g(dh), *q = DH_get0_q(dh); |
0ba71d6a | 449 | long l = DH_get_length(dh); |
8b84b075 RL |
450 | const BIGNUM *pub_key = DH_get0_pub_key(dh); |
451 | const BIGNUM *priv_key = DH_get0_priv_key(dh); | |
6d4e6009 | 452 | OSSL_PARAM *params = NULL; |
0996cff9 | 453 | int selection = 0; |
6d4e6009 | 454 | int rv = 0; |
8b84b075 | 455 | |
21fb7067 | 456 | if (p == NULL || g == NULL) |
b305452f | 457 | return 0; |
8b84b075 | 458 | |
6d4e6009 P |
459 | tmpl = OSSL_PARAM_BLD_new(); |
460 | if (tmpl == NULL) | |
b305452f | 461 | return 0; |
6d4e6009 P |
462 | if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
463 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)) | |
464 | goto err; | |
8b84b075 | 465 | if (q != NULL) { |
6d4e6009 P |
466 | if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q)) |
467 | goto err; | |
8b84b075 | 468 | } |
0996cff9 | 469 | selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; |
0ba71d6a RL |
470 | if (l > 0) { |
471 | if (!OSSL_PARAM_BLD_push_long(tmpl, OSSL_PKEY_PARAM_DH_PRIV_LEN, l)) | |
472 | goto err; | |
473 | selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS; | |
474 | } | |
0996cff9 | 475 | if (pub_key != NULL) { |
6d4e6009 P |
476 | if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key)) |
477 | goto err; | |
0996cff9 RL |
478 | selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
479 | } | |
b305452f | 480 | if (priv_key != NULL) { |
6d4e6009 | 481 | if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, |
b305452f | 482 | priv_key)) |
6d4e6009 | 483 | goto err; |
0996cff9 | 484 | selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
8b84b075 RL |
485 | } |
486 | ||
6d4e6009 P |
487 | if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) |
488 | goto err; | |
8b84b075 RL |
489 | |
490 | /* We export, the provider imports */ | |
bed7437b | 491 | rv = importer(to_keydata, selection, params); |
8b84b075 | 492 | |
3f883c7c | 493 | OSSL_PARAM_free(params); |
6d4e6009 P |
494 | err: |
495 | OSSL_PARAM_BLD_free(tmpl); | |
b305452f | 496 | return rv; |
8b84b075 RL |
497 | } |
498 | ||
31d2daec SL |
499 | static int dh_pkey_import_from_type(const OSSL_PARAM params[], void *vpctx, |
500 | int type) | |
0abae163 | 501 | { |
629c72db MC |
502 | EVP_PKEY_CTX *pctx = vpctx; |
503 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx); | |
19dbb742 | 504 | DH *dh = ossl_dh_new_ex(pctx->libctx); |
0abae163 RL |
505 | |
506 | if (dh == NULL) { | |
507 | ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE); | |
508 | return 0; | |
509 | } | |
d135774e SL |
510 | DH_clear_flags(dh, DH_FLAG_TYPE_MASK); |
511 | DH_set_flags(dh, type == EVP_PKEY_DH ? DH_FLAG_TYPE_DH : DH_FLAG_TYPE_DHX); | |
0abae163 | 512 | |
19dbb742 | 513 | if (!ossl_dh_params_fromdata(dh, params) |
944f822a | 514 | || !ossl_dh_key_fromdata(dh, params, 1) |
31d2daec | 515 | || !EVP_PKEY_assign(pkey, type, dh)) { |
0abae163 RL |
516 | DH_free(dh); |
517 | return 0; | |
518 | } | |
519 | return 1; | |
520 | } | |
521 | ||
31d2daec SL |
522 | static int dh_pkey_import_from(const OSSL_PARAM params[], void *vpctx) |
523 | { | |
524 | return dh_pkey_import_from_type(params, vpctx, EVP_PKEY_DH); | |
525 | } | |
526 | ||
527 | static int dhx_pkey_import_from(const OSSL_PARAM params[], void *vpctx) | |
528 | { | |
529 | return dh_pkey_import_from_type(params, vpctx, EVP_PKEY_DHX); | |
530 | } | |
531 | ||
2145ba5e TM |
532 | static int dh_pkey_copy(EVP_PKEY *to, EVP_PKEY *from) |
533 | { | |
534 | DH *dh = from->pkey.dh; | |
535 | DH *dupkey = NULL; | |
536 | int ret; | |
537 | ||
538 | if (dh != NULL) { | |
b4f447c0 | 539 | dupkey = ossl_dh_dup(dh, OSSL_KEYMGMT_SELECT_ALL); |
2145ba5e TM |
540 | if (dupkey == NULL) |
541 | return 0; | |
542 | } | |
543 | ||
544 | ret = EVP_PKEY_assign(to, from->type, dupkey); | |
545 | if (!ret) | |
546 | DH_free(dupkey); | |
547 | return ret; | |
548 | } | |
549 | ||
adf7e6d1 | 550 | const EVP_PKEY_ASN1_METHOD ossl_dh_asn1_meth = { |
0f113f3e MC |
551 | EVP_PKEY_DH, |
552 | EVP_PKEY_DH, | |
553 | 0, | |
554 | ||
555 | "DH", | |
556 | "OpenSSL PKCS#3 DH method", | |
557 | ||
558 | dh_pub_decode, | |
559 | dh_pub_encode, | |
560 | dh_pub_cmp, | |
561 | dh_public_print, | |
562 | ||
563 | dh_priv_decode, | |
564 | dh_priv_encode, | |
565 | dh_private_print, | |
566 | ||
567 | int_dh_size, | |
568 | dh_bits, | |
569 | dh_security_bits, | |
570 | ||
571 | dh_param_decode, | |
572 | dh_param_encode, | |
573 | dh_missing_parameters, | |
574 | dh_copy_parameters, | |
575 | dh_cmp_parameters, | |
576 | dh_param_print, | |
577 | 0, | |
578 | ||
579 | int_dh_free, | |
9aaecbfc | 580 | dh_pkey_ctrl, |
b0004708 PY |
581 | |
582 | 0, 0, 0, 0, 0, | |
583 | ||
584 | 0, | |
585 | dh_pkey_public_check, | |
8b84b075 RL |
586 | dh_pkey_param_check, |
587 | ||
588 | 0, 0, 0, 0, | |
589 | ||
590 | dh_pkey_dirty_cnt, | |
591 | dh_pkey_export_to, | |
0abae163 | 592 | dh_pkey_import_from, |
2145ba5e | 593 | dh_pkey_copy |
0f113f3e MC |
594 | }; |
595 | ||
adf7e6d1 | 596 | const EVP_PKEY_ASN1_METHOD ossl_dhx_asn1_meth = { |
0f113f3e MC |
597 | EVP_PKEY_DHX, |
598 | EVP_PKEY_DHX, | |
599 | 0, | |
600 | ||
601 | "X9.42 DH", | |
602 | "OpenSSL X9.42 DH method", | |
603 | ||
604 | dh_pub_decode, | |
605 | dh_pub_encode, | |
606 | dh_pub_cmp, | |
607 | dh_public_print, | |
608 | ||
609 | dh_priv_decode, | |
610 | dh_priv_encode, | |
611 | dh_private_print, | |
612 | ||
613 | int_dh_size, | |
614 | dh_bits, | |
615 | dh_security_bits, | |
616 | ||
617 | dh_param_decode, | |
618 | dh_param_encode, | |
619 | dh_missing_parameters, | |
620 | dh_copy_parameters, | |
621 | dh_cmp_parameters, | |
622 | dh_param_print, | |
623 | 0, | |
624 | ||
625 | int_dh_free, | |
9aaecbfc | 626 | dhx_pkey_ctrl, |
b0004708 PY |
627 | |
628 | 0, 0, 0, 0, 0, | |
629 | ||
630 | 0, | |
631 | dh_pkey_public_check, | |
31d2daec SL |
632 | dh_pkey_param_check, |
633 | 0, 0, 0, 0, | |
634 | dh_pkey_dirty_cnt, | |
635 | dh_pkey_export_to, | |
636 | dhx_pkey_import_from, | |
2145ba5e | 637 | dh_pkey_copy |
0f113f3e | 638 | }; |