VirtualBox

Ignore:
Timestamp:
Mar 3, 2022 7:17:34 PM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
150325
Message:

libs/openssl-3.0.1: started applying and adjusting our OpenSSL changes to 3.0.1. bugref:10128

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  
        1212/vendor/openssl/1.1.1c:131722-131725
        1313/vendor/openssl/1.1.1k:145841-145843
         14/vendor/openssl/3.0.1:150323-150324
         15/vendor/openssl/current:147554-150322
  • trunk/src/libs/openssl-3.0.1/crypto/dsa/dsa_ameth.c

    r91772 r94082  
    11/*
    2  * Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
     2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
    33 *
    4  * Licensed under the OpenSSL license (the "License").  You may not use
     4 * Licensed under the Apache License 2.0 (the "License").  You may not use
    55 * this file except in compliance with the License.  You can obtain a copy
    66 * in the file LICENSE in the source distribution or at
     
    88 */
    99
     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
    1016#include <stdio.h>
    11 #include "internal/cryptlib.h"
    1217#include <openssl/x509.h>
    1318#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"
    1427#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
     29static int dsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
    2130{
    2231    const unsigned char *p, *pm;
     
    4049
    4150        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);
    4352            goto err;
    4453        }
     
    4655    } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
    4756        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);
    4958            goto err;
    5059        }
    5160    } else {
    52         DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
     61        ERR_raise(ERR_LIB_DSA, DSA_R_PARAMETER_ENCODING_ERROR);
    5362        goto err;
    5463    }
    5564
    5665    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);
    5867        goto err;
    5968    }
    6069
    6170    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++;
    6676    ASN1_INTEGER_free(public_key);
    6777    EVP_PKEY_assign_DSA(pkey, dsa);
     
    8696
    8797    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) {
    89102        str = ASN1_STRING_new();
    90103        if (str == NULL) {
    91             DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
     104            ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
    92105            goto err;
    93106        }
    94107        str->length = i2d_DSAparams(dsa, &str->data);
    95108        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);
    97110            goto err;
    98111        }
     
    104117
    105118    if (pubint == NULL) {
    106         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
     119        ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
    107120        goto err;
    108121    }
     
    112125
    113126    if (penclen <= 0) {
    114         DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
     127        ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
    115128        goto err;
    116129    }
     
    137150static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
    138151{
    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 
    150152    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
    200160    return ret;
    201161}
     
    208168    int dplen;
    209169
    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);
    212172        goto err;
    213173    }
     
    216176
    217177    if (params == NULL) {
    218         DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
     178        ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
    219179        goto err;
    220180    }
     
    222182    params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
    223183    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);
    225185        goto err;
    226186    }
     
    230190    prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
    231191
    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);
    234194        goto err;
    235195    }
     
    272232    DSA *dsa;
    273233    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;
    277238}
    278239
    279240static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
    280241{
    281     BIGNUM *a;
    282 
    283242    if (to->pkey.dsa == NULL) {
    284243        to->pkey.dsa = DSA_new();
     
    286245            return 0;
    287246    }
    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++;
    303251    return 1;
    304252}
     
    306254static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
    307255{
    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);
    314257}
    315258
    316259static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
    317260{
    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;
    322262}
    323263
     
    332272    const char *ktype = NULL;
    333273    const BIGNUM *priv_key, *pub_key;
     274    int mod_len = 0;
     275
     276    if (x->params.p != NULL)
     277        mod_len = DSA_bits(x);
    334278
    335279    if (ptype == 2)
     
    350294        ktype = "DSA-Parameters";
    351295
    352     if (priv_key) {
     296    if (priv_key != NULL) {
    353297        if (!BIO_indent(bp, off, 128))
    354298            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)
    357303            goto err;
    358304    }
     
    362308    if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
    363309        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))
    369311        goto err;
    370312    ret = 1;
     
    378320    DSA *dsa;
    379321
    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++;
    384326    EVP_PKEY_assign_DSA(pkey, dsa);
    385327    return 1;
     
    415357
    416358    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++;
    420363    EVP_PKEY_assign_DSA(pkey, dsa);
    421364    return 1;
     
    433376    const unsigned char *p;
    434377
    435     if (!sig) {
     378    if (sig == NULL) {
    436379        if (BIO_puts(bp, "\n") <= 0)
    437380            return 0;
     
    441384    p = sig->data;
    442385    dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
    443     if (dsa_sig) {
     386    if (dsa_sig != NULL) {
    444387        int rv = 0;
    445388        const BIGNUM *r, *s;
     
    459402        return rv;
    460403    }
     404    if (BIO_puts(bp, "\n") <= 0)
     405        return 0;
    461406    return X509_signature_dump(bp, sig, indent);
    462407}
     
    465410{
    466411    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_CMS
    483     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 #endif
    503 
    504412    case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
    505413        *(int *)arg2 = NID_sha256;
     
    508416    default:
    509417        return -2;
    510 
    511     }
    512 
     418    }
     419}
     420
     421static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
     422{
     423    return pkey->pkey.dsa->dirty_cnt;
     424}
     425
     426static 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
     476static 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
     496static 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;
    513512}
    514513
    515514/* NB these are sorted in pkey_id order, lowest first */
    516515
    517 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
     516const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5] = {
    518517
    519518    {
     
    569568     dsa_pkey_ctrl,
    570569     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    }
    572581};
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette