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