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