Changeset 94082 in vbox for trunk/src/libs/openssl-3.0.1/crypto/dsa/dsa_ameth.c
- Timestamp:
- Mar 3, 2022 7:17:34 PM (3 years ago)
- svn:sync-xref-src-repo-rev:
- 150325
- Location:
- trunk/src/libs/openssl-3.0.1
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libs/openssl-3.0.1
- Property svn:mergeinfo
-
old new 12 12 /vendor/openssl/1.1.1c:131722-131725 13 13 /vendor/openssl/1.1.1k:145841-145843 14 /vendor/openssl/3.0.1:150323-150324 15 /vendor/openssl/current:147554-150322
-
- Property svn:mergeinfo
-
trunk/src/libs/openssl-3.0.1/crypto/dsa/dsa_ameth.c
r91772 r94082 1 1 /* 2 * Copyright 2006-20 19The OpenSSL Project Authors. All Rights Reserved.2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. 3 3 * 4 * Licensed under the OpenSSL license(the "License"). You may not use4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 5 * this file except in compliance with the License. You can obtain a copy 6 6 * in the file LICENSE in the source distribution or at … … 8 8 */ 9 9 10 /* 11 * DSA low level APIs are deprecated for public use, but still ok for 12 * internal use. 13 */ 14 #include "internal/deprecated.h" 15 10 16 #include <stdio.h> 11 #include "internal/cryptlib.h"12 17 #include <openssl/x509.h> 13 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/cryptlib.h" 23 #include "crypto/asn1.h" 24 #include "crypto/dsa.h" 25 #include "crypto/evp.h" 26 #include "internal/ffc.h" 14 27 #include "dsa_local.h" 15 #include <openssl/bn.h> 16 #include <openssl/cms.h> 17 #include "crypto/asn1.h" 18 #include "crypto/evp.h" 19 20 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) 28 29 static int dsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey) 21 30 { 22 31 const unsigned char *p, *pm; … … 40 49 41 50 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) { 42 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);51 ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR); 43 52 goto err; 44 53 } … … 46 55 } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) { 47 56 if ((dsa = DSA_new()) == NULL) { 48 DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);57 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 49 58 goto err; 50 59 } 51 60 } else { 52 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);61 ERR_raise(ERR_LIB_DSA, DSA_R_PARAMETER_ENCODING_ERROR); 53 62 goto err; 54 63 } 55 64 56 65 if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) { 57 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);66 ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR); 58 67 goto err; 59 68 } 60 69 61 70 if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) { 62 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR); 63 goto err; 64 } 65 71 ERR_raise(ERR_LIB_DSA, DSA_R_BN_DECODE_ERROR); 72 goto err; 73 } 74 75 dsa->dirty_cnt++; 66 76 ASN1_INTEGER_free(public_key); 67 77 EVP_PKEY_assign_DSA(pkey, dsa); … … 86 96 87 97 dsa = pkey->pkey.dsa; 88 if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) { 98 if (pkey->save_parameters 99 && dsa->params.p != NULL 100 && dsa->params.q != NULL 101 && dsa->params.g != NULL) { 89 102 str = ASN1_STRING_new(); 90 103 if (str == NULL) { 91 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);104 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 92 105 goto err; 93 106 } 94 107 str->length = i2d_DSAparams(dsa, &str->data); 95 108 if (str->length <= 0) { 96 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);109 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 97 110 goto err; 98 111 } … … 104 117 105 118 if (pubint == NULL) { 106 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);119 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 107 120 goto err; 108 121 } … … 112 125 113 126 if (penclen <= 0) { 114 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);127 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 115 128 goto err; 116 129 } … … 137 150 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) 138 151 { 139 const unsigned char *p, *pm;140 int pklen, pmlen;141 int ptype;142 const void *pval;143 const ASN1_STRING *pstr;144 const X509_ALGOR *palg;145 ASN1_INTEGER *privkey = NULL;146 BN_CTX *ctx = NULL;147 148 DSA *dsa = NULL;149 150 152 int ret = 0; 151 152 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) 153 return 0; 154 X509_ALGOR_get0(NULL, &ptype, &pval, palg); 155 156 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) 157 goto decerr; 158 if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE) 159 goto decerr; 160 161 pstr = pval; 162 pm = pstr->data; 163 pmlen = pstr->length; 164 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) 165 goto decerr; 166 /* We have parameters now set private key */ 167 if ((dsa->priv_key = BN_secure_new()) == NULL 168 || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) { 169 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR); 170 goto dsaerr; 171 } 172 /* Calculate public key */ 173 if ((dsa->pub_key = BN_new()) == NULL) { 174 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); 175 goto dsaerr; 176 } 177 if ((ctx = BN_CTX_new()) == NULL) { 178 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE); 179 goto dsaerr; 180 } 181 182 BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME); 183 if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) { 184 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR); 185 goto dsaerr; 186 } 187 188 EVP_PKEY_assign_DSA(pkey, dsa); 189 190 ret = 1; 191 goto done; 192 193 decerr: 194 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR); 195 dsaerr: 196 DSA_free(dsa); 197 done: 198 BN_CTX_free(ctx); 199 ASN1_STRING_clear_free(privkey); 153 DSA *dsa = ossl_dsa_key_from_pkcs8(p8, NULL, NULL); 154 155 if (dsa != NULL) { 156 ret = 1; 157 EVP_PKEY_assign_DSA(pkey, dsa); 158 } 159 200 160 return ret; 201 161 } … … 208 168 int dplen; 209 169 210 if ( !pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {211 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);170 if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) { 171 ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS); 212 172 goto err; 213 173 } … … 216 176 217 177 if (params == NULL) { 218 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);178 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 219 179 goto err; 220 180 } … … 222 182 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data); 223 183 if (params->length <= 0) { 224 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);184 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 225 185 goto err; 226 186 } … … 230 190 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL); 231 191 232 if ( !prkey) {233 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);192 if (prkey == NULL) { 193 ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR); 234 194 goto err; 235 195 } … … 272 232 DSA *dsa; 273 233 dsa = pkey->pkey.dsa; 274 if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) 275 return 1; 276 return 0; 234 return dsa == NULL 235 || dsa->params.p == NULL 236 || dsa->params.q == NULL 237 || dsa->params.g == NULL; 277 238 } 278 239 279 240 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) 280 241 { 281 BIGNUM *a;282 283 242 if (to->pkey.dsa == NULL) { 284 243 to->pkey.dsa = DSA_new(); … … 286 245 return 0; 287 246 } 288 289 if ((a = BN_dup(from->pkey.dsa->p)) == NULL) 290 return 0; 291 BN_free(to->pkey.dsa->p); 292 to->pkey.dsa->p = a; 293 294 if ((a = BN_dup(from->pkey.dsa->q)) == NULL) 295 return 0; 296 BN_free(to->pkey.dsa->q); 297 to->pkey.dsa->q = a; 298 299 if ((a = BN_dup(from->pkey.dsa->g)) == NULL) 300 return 0; 301 BN_free(to->pkey.dsa->g); 302 to->pkey.dsa->g = a; 247 if (!ossl_ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params)) 248 return 0; 249 250 to->pkey.dsa->dirty_cnt++; 303 251 return 1; 304 252 } … … 306 254 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) 307 255 { 308 if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) || 309 BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) || 310 BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g)) 311 return 0; 312 else 313 return 1; 256 return ossl_ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1); 314 257 } 315 258 316 259 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) 317 260 { 318 if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0) 319 return 0; 320 else 321 return 1; 261 return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0; 322 262 } 323 263 … … 332 272 const char *ktype = NULL; 333 273 const BIGNUM *priv_key, *pub_key; 274 int mod_len = 0; 275 276 if (x->params.p != NULL) 277 mod_len = DSA_bits(x); 334 278 335 279 if (ptype == 2) … … 350 294 ktype = "DSA-Parameters"; 351 295 352 if (priv_key ) {296 if (priv_key != NULL) { 353 297 if (!BIO_indent(bp, off, 128)) 354 298 goto err; 355 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) 356 <= 0) 299 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0) 300 goto err; 301 } else { 302 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0) 357 303 goto err; 358 304 } … … 362 308 if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off)) 363 309 goto err; 364 if (!ASN1_bn_print(bp, "P: ", x->p, NULL, off)) 365 goto err; 366 if (!ASN1_bn_print(bp, "Q: ", x->q, NULL, off)) 367 goto err; 368 if (!ASN1_bn_print(bp, "G: ", x->g, NULL, off)) 310 if (!ossl_ffc_params_print(bp, &x->params, off)) 369 311 goto err; 370 312 ret = 1; … … 378 320 DSA *dsa; 379 321 380 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {381 DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);382 return 0; 383 }322 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) 323 return 0; 324 325 dsa->dirty_cnt++; 384 326 EVP_PKEY_assign_DSA(pkey, dsa); 385 327 return 1; … … 415 357 416 358 if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) { 417 DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB); 418 return 0; 419 } 359 ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB); 360 return 0; 361 } 362 dsa->dirty_cnt++; 420 363 EVP_PKEY_assign_DSA(pkey, dsa); 421 364 return 1; … … 433 376 const unsigned char *p; 434 377 435 if ( !sig) {378 if (sig == NULL) { 436 379 if (BIO_puts(bp, "\n") <= 0) 437 380 return 0; … … 441 384 p = sig->data; 442 385 dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length); 443 if (dsa_sig ) {386 if (dsa_sig != NULL) { 444 387 int rv = 0; 445 388 const BIGNUM *r, *s; … … 459 402 return rv; 460 403 } 404 if (BIO_puts(bp, "\n") <= 0) 405 return 0; 461 406 return X509_signature_dump(bp, sig, indent); 462 407 } … … 465 410 { 466 411 switch (op) { 467 case ASN1_PKEY_CTRL_PKCS7_SIGN:468 if (arg1 == 0) {469 int snid, hnid;470 X509_ALGOR *alg1, *alg2;471 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);472 if (alg1 == NULL || alg1->algorithm == NULL)473 return -1;474 hnid = OBJ_obj2nid(alg1->algorithm);475 if (hnid == NID_undef)476 return -1;477 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))478 return -1;479 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);480 }481 return 1;482 #ifndef OPENSSL_NO_CMS483 case ASN1_PKEY_CTRL_CMS_SIGN:484 if (arg1 == 0) {485 int snid, hnid;486 X509_ALGOR *alg1, *alg2;487 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);488 if (alg1 == NULL || alg1->algorithm == NULL)489 return -1;490 hnid = OBJ_obj2nid(alg1->algorithm);491 if (hnid == NID_undef)492 return -1;493 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))494 return -1;495 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);496 }497 return 1;498 499 case ASN1_PKEY_CTRL_CMS_RI_TYPE:500 *(int *)arg2 = CMS_RECIPINFO_NONE;501 return 1;502 #endif503 504 412 case ASN1_PKEY_CTRL_DEFAULT_MD_NID: 505 413 *(int *)arg2 = NID_sha256; … … 508 416 default: 509 417 return -2; 510 511 } 512 418 } 419 } 420 421 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey) 422 { 423 return pkey->pkey.dsa->dirty_cnt; 424 } 425 426 static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata, 427 OSSL_FUNC_keymgmt_import_fn *importer, 428 OSSL_LIB_CTX *libctx, const char *propq) 429 { 430 DSA *dsa = from->pkey.dsa; 431 OSSL_PARAM_BLD *tmpl; 432 const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa); 433 const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa); 434 const BIGNUM *priv_key = DSA_get0_priv_key(dsa); 435 OSSL_PARAM *params; 436 int selection = 0; 437 int rv = 0; 438 439 if (p == NULL || q == NULL || g == NULL) 440 return 0; 441 442 tmpl = OSSL_PARAM_BLD_new(); 443 if (tmpl == NULL) 444 return 0; 445 446 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) 447 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q) 448 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)) 449 goto err; 450 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; 451 if (pub_key != NULL) { 452 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, 453 pub_key)) 454 goto err; 455 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; 456 } 457 if (priv_key != NULL) { 458 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, 459 priv_key)) 460 goto err; 461 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY; 462 } 463 464 if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) 465 goto err; 466 467 /* We export, the provider imports */ 468 rv = importer(to_keydata, selection, params); 469 470 OSSL_PARAM_free(params); 471 err: 472 OSSL_PARAM_BLD_free(tmpl); 473 return rv; 474 } 475 476 static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx) 477 { 478 EVP_PKEY_CTX *pctx = vpctx; 479 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx); 480 DSA *dsa = ossl_dsa_new(pctx->libctx); 481 482 if (dsa == NULL) { 483 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); 484 return 0; 485 } 486 487 if (!ossl_dsa_ffc_params_fromdata(dsa, params) 488 || !ossl_dsa_key_fromdata(dsa, params) 489 || !EVP_PKEY_assign_DSA(pkey, dsa)) { 490 DSA_free(dsa); 491 return 0; 492 } 493 return 1; 494 } 495 496 static int dsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from) 497 { 498 DSA *dsa = from->pkey.dsa; 499 DSA *dupkey = NULL; 500 int ret; 501 502 if (dsa != NULL) { 503 dupkey = ossl_dsa_dup(dsa, OSSL_KEYMGMT_SELECT_ALL); 504 if (dupkey == NULL) 505 return 0; 506 } 507 508 ret = EVP_PKEY_assign_DSA(to, dupkey); 509 if (!ret) 510 DSA_free(dupkey); 511 return ret; 513 512 } 514 513 515 514 /* NB these are sorted in pkey_id order, lowest first */ 516 515 517 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {516 const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5] = { 518 517 519 518 { … … 569 568 dsa_pkey_ctrl, 570 569 old_dsa_priv_decode, 571 old_dsa_priv_encode} 570 old_dsa_priv_encode, 571 572 NULL, NULL, NULL, 573 NULL, NULL, NULL, 574 NULL, NULL, NULL, NULL, 575 576 dsa_pkey_dirty_cnt, 577 dsa_pkey_export_to, 578 dsa_pkey_import_from, 579 dsa_pkey_copy 580 } 572 581 };
Note:
See TracChangeset
for help on using the changeset viewer.