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