VirtualBox

Changeset 61891 in vbox for trunk


Ignore:
Timestamp:
Jun 27, 2016 10:55:28 AM (9 years ago)
Author:
vboxsync
Message:

Runtime/crypto: some fixes for OpenSSL 1.1, work in progress

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/crypto/digest.h

    r59689 r61891  
    6161
    6262    /**
     63     * Allocates the digest data.
     64     */
     65    DECLCALLBACKMEMBER(void *, pfnNew)(void);
     66   
     67    /**
     68     * Frees the digest data.
     69     *
     70     * @param   pvState     The opaque message digest state.
     71     */
     72    DECLCALLBACKMEMBER(void, pfnFree)(void *pvState);
     73
     74    /**
    6375     * Updates the digest with more data.
    6476     *
  • trunk/src/VBox/Runtime/common/crypto/digest-builtin.cpp

    r57358 r61891  
    8787    sizeof(RTMD2CONTEXT),
    8888    0,
     89    NULL,
     90    NULL,
    8991    rtCrDigestMd2_Update,
    9092    rtCrDigestMd2_Final,
     
    139141    sizeof(RTMD5CONTEXT),
    140142    0,
     143    NULL,
     144    NULL,
    141145    rtCrDigestMd5_Update,
    142146    rtCrDigestMd5_Final,
     
    191195    sizeof(RTSHA1CONTEXT),
    192196    0,
     197    NULL,
     198    NULL,
    193199    rtCrDigestSha1_Update,
    194200    rtCrDigestSha1_Final,
     
    242248    sizeof(RTSHA256CONTEXT),
    243249    0,
     250    NULL,
     251    NULL,
    244252    rtCrDigestSha256_Update,
    245253    rtCrDigestSha256_Final,
     
    293301    sizeof(RTSHA512CONTEXT),
    294302    0,
     303    NULL,
     304    NULL,
    295305    rtCrDigestSha512_Update,
    296306    rtCrDigestSha512_Final,
     
    344354    sizeof(RTSHA224CONTEXT),
    345355    0,
     356    NULL,
     357    NULL,
    346358    rtCrDigestSha224_Update,
    347359    rtCrDigestSha224_Final,
     
    395407    sizeof(RTSHA384CONTEXT),
    396408    0,
     409    NULL,
     410    NULL,
    397411    rtCrDigestSha384_Update,
    398412    rtCrDigestSha384_Final,
     
    446460    sizeof(RTSHA512T224CONTEXT),
    447461    0,
     462    NULL,
     463    NULL,
    448464    rtCrDigestSha512t224_Update,
    449465    rtCrDigestSha512t224_Final,
     
    498514    sizeof(RTSHA512T256CONTEXT),
    499515    0,
     516    NULL,
     517    NULL,
    500518    rtCrDigestSha512t256_Update,
    501519    rtCrDigestSha512t256_Final,
     
    534552 * OpenSSL EVP.
    535553 */
     554   
     555# if OPENSSL_VERSION_NUMBER >= 0x10100000
     556/** @impl_interface_method{RTCRDIGESTDESC::pfnNew} */
     557static DECLCALLBACK(void*) rtCrDigestOsslEvp_New(void)
     558{
     559    return EVP_MD_CTX_new();
     560}
     561
     562static DECLCALLBACK(void) rtCrDigestOsslEvp_Free(void *pvState)
     563{
     564    EVP_MD_CTX_free((EVP_MD_CTX*)pvState);
     565}
     566
     567# endif
    536568
    537569/** @impl_interface_method{RTCRDIGESTDESC::pfnUpdate} */
     
    540572    EVP_DigestUpdate((EVP_MD_CTX *)pvState, pvData, cbData);
    541573}
    542 
    543574
    544575/** @impl_interface_method{RTCRDIGESTDESC::pfnFinal} */
     
    558589    {
    559590        pEvpType = EVP_MD_CTX_md(pThis);
     591# if OPENSSL_VERSION_NUMBER >= 0x10100000
     592        EVP_MD_CTX_reset(pThis);
     593# else
    560594        EVP_MD_CTX_cleanup(pThis);
     595# endif
    561596    }
    562597
     
    573608{
    574609    EVP_MD_CTX *pThis = (EVP_MD_CTX *)pvState;
     610# if OPENSSL_VERSION_NUMBER >= 0x10100000
     611    EVP_MD_CTX_reset(pThis);
     612# else
    575613    EVP_MD_CTX_cleanup(pThis);
     614# endif
    576615}
    577616
     
    614653    RTDIGESTTYPE_UNKNOWN,
    615654    EVP_MAX_MD_SIZE,
    616     sizeof(EVP_MD_CTX),
    617     0,
     655    0,
     656    0,
     657# if OPENSSL_VERSION_NUMBER >= 0x10100000
     658    rtCrDigestOsslEvp_New,
     659    rtCrDigestOsslEvp_Free,
     660# else
     661    NULL,
     662    NULL,
     663# endif
    618664    rtCrDigestOsslEvp_Update,
    619665    rtCrDigestOsslEvp_Final,
  • trunk/src/VBox/Runtime/common/crypto/digest-core.cpp

    r59689 r61891  
    6060    /** The number of bytes consumed. */
    6161    uint64_t            cbConsumed;
     62    /** Pointer to the data specific to the message digest algorithm. Points
     63     * either to &abState[0] or to memory allocated with pDesc->pfnNew. */
     64    void               *pvState;
    6265    /** Opaque data specific to the message digest algorithm, size given by
    63      * RTCRDIGESTDESC::cbState.  This is followed by space for the final hash at
    64      * offHash with size RTCRDIGESTDESC::cbHash. */
     66     * RTCRDIGESTDESC::cbState.  This is followed by space for the final hash
     67     * at offHash with size RTCRDIGESTDESC::cbHash.  The data specific to the
     68     * message digest algorithm can also be 0. In this case, pDesc->pfnNew()
     69     * and pDesc->pfnFree() must not be NULL. */
    6570    uint8_t             abState[1];
    6671} RTCRDIGESTINT;
     
    8994
    9095    int rc = VINF_SUCCESS;
    91     uint32_t offHash = RT_ALIGN_32(pDesc->cbState, 8);
     96    uint32_t const offHash = RT_ALIGN_32(pDesc->cbState, 8);
     97    AssertReturn(pDesc->pfnNew || offHash, VERR_INVALID_PARAMETER);
     98    AssertReturn(!pDesc->pfnNew || (pDesc->pfnFree && pDesc->pfnInit && pDesc->pfnClone), VERR_INVALID_PARAMETER);
    9299    PRTCRDIGESTINT pThis = (PRTCRDIGESTINT)RTMemAllocZ(RT_OFFSETOF(RTCRDIGESTINT, abState[offHash + pDesc->cbHash]));
    93100    if (pThis)
    94101    {
    95         pThis->u32Magic = RTCRDIGESTINT_MAGIC;
    96         pThis->cRefs    = 1;
    97         pThis->offHash  = offHash;
    98         pThis->pDesc    = pDesc;
    99         pThis->uState   = RTCRDIGEST_STATE_READY;
    100         if (pDesc->pfnInit)
    101             rc = pDesc->pfnInit(pThis->abState, pvOpaque, false /*fReInit*/);
    102         if (RT_SUCCESS(rc))
     102        if (pDesc->pfnNew)
     103            pThis->pvState = pDesc->pfnNew();
     104        else
     105            pThis->pvState = &pThis->abState[0];
     106        if (pThis->pvState)
    103107        {
    104             *phDigest = pThis;
    105             return VINF_SUCCESS;
     108            pThis->u32Magic = RTCRDIGESTINT_MAGIC;
     109            pThis->cRefs    = 1;
     110            pThis->offHash  = offHash;
     111            pThis->pDesc    = pDesc;
     112            pThis->uState   = RTCRDIGEST_STATE_READY;
     113            if (pDesc->pfnInit)
     114                rc = pDesc->pfnInit(pThis->pvState, pvOpaque, false /*fReInit*/);
     115            if (RT_SUCCESS(rc))
     116            {
     117                *phDigest = pThis;
     118                return VINF_SUCCESS;
     119            }
     120            if (pDesc->pfnFree)
     121                pDesc->pfnFree(pThis->pvState);
    106122        }
     123        else
     124            rc = VERR_NO_MEMORY;
    107125        pThis->u32Magic = 0;
    108126        RTMemFree(pThis);
     
    125143    if (pThis)
    126144    {
    127         pThis->u32Magic = RTCRDIGESTINT_MAGIC;
    128         pThis->cRefs    = 1;
    129         pThis->offHash  = offHash;
    130         pThis->pDesc    = hSrc->pDesc;
    131         if (hSrc->pDesc->pfnClone)
    132             rc = hSrc->pDesc->pfnClone(pThis->abState, hSrc->abState);
    133         else
    134             memcpy(pThis->abState, hSrc->abState, offHash);
    135         memcpy(&pThis->abState[offHash], &hSrc->abState[offHash], hSrc->pDesc->cbHash);
    136         pThis->uState = hSrc->uState;
    137 
    138         if (RT_SUCCESS(rc))
     145        if (hSrc->pDesc->pfnNew)
     146            pThis->pvState = hSrc->pDesc->pfnNew();
     147        else
     148            pThis->pvState = &pThis->abState[0];
     149        if (pThis->pvState)
    139150        {
    140             *phDigest = pThis;
    141             return VINF_SUCCESS;
     151            pThis->u32Magic = RTCRDIGESTINT_MAGIC;
     152            pThis->cRefs    = 1;
     153            pThis->offHash  = offHash;
     154            pThis->pDesc    = hSrc->pDesc;
     155            if (hSrc->pDesc->pfnClone)
     156                rc = hSrc->pDesc->pfnClone(pThis->pvState, hSrc->pvState);
     157            else
     158            {
     159                Assert(!hSrc->pDesc->pfnNew);
     160                memcpy(pThis->pvState, hSrc->pvState, offHash);
     161            }
     162            memcpy(&pThis->abState[offHash], &hSrc->abState[offHash], hSrc->pDesc->cbHash);
     163            pThis->uState = hSrc->uState;
     164
     165            if (RT_SUCCESS(rc))
     166            {
     167                *phDigest = pThis;
     168                return VINF_SUCCESS;
     169            }
     170            if (hSrc->pDesc->pfnFree)
     171                hSrc->pDesc->pfnFree(pThis->pvState);
    142172        }
     173        else
     174            rc = VERR_NO_MEMORY;
    143175        pThis->u32Magic = 0;
    144176        RTMemFree(pThis);
     
    162194    if (pThis->pDesc->pfnInit)
    163195    {
    164         rc = pThis->pDesc->pfnInit(pThis->abState, NULL, true /*fReInit*/);
     196        rc = pThis->pDesc->pfnInit(pThis->pvState, NULL, true /*fReInit*/);
    165197        if (RT_FAILURE(rc))
    166198            pThis->uState = RTCRDIGEST_STATE_BUSTED;
     
    168200    }
    169201    else
    170         RT_BZERO(pThis->abState, pThis->offHash + pThis->pDesc->cbHash);
     202    {
     203        Assert(!pThis->pDesc->pfnNew);
     204        RT_BZERO(pThis->pvState, pThis->offHash + pThis->pDesc->cbHash);
     205    }
    171206    return rc;
    172207}
     
    198233        pThis->u32Magic = ~RTCRDIGESTINT_MAGIC;
    199234        if (pThis->pDesc->pfnDelete)
    200             pThis->pDesc->pfnDelete(pThis->abState);
     235            pThis->pDesc->pfnDelete(pThis->pvState);
     236        if (pThis->pDesc->pfnFree)
     237            pThis->pDesc->pfnFree(pThis->pvState);
    201238        RTMemFree(pThis);
    202239    }
     
    213250    AssertReturn(pThis->uState == RTCRDIGEST_STATE_READY, VERR_INVALID_STATE);
    214251
    215     pThis->pDesc->pfnUpdate(pThis->abState, pvData, cbData);
     252    pThis->pDesc->pfnUpdate(pThis->pvState, pvData, cbData);
    216253    pThis->cbConsumed += cbData;
    217254    return VINF_SUCCESS;
     
    232269    if (pThis->uState == RTCRDIGEST_STATE_READY)
    233270    {
    234         pThis->pDesc->pfnFinal(pThis->abState, &pThis->abState[pThis->offHash]);
     271        pThis->pDesc->pfnFinal(pThis->pvState, &pThis->abState[pThis->offHash]);
    235272        pThis->uState = RTCRDIGEST_STATE_FINAL;
    236273    }
     
    245282        uint32_t cbNeeded = pThis->pDesc->cbHash;
    246283        if (pThis->pDesc->pfnGetHashSize)
    247             cbNeeded = pThis->pDesc->pfnGetHashSize(pThis->abState);
     284            cbNeeded = pThis->pDesc->pfnGetHashSize(pThis->pvState);
    248285        Assert(cbNeeded > 0);
    249286
     
    300337    if (pThis->pDesc->pfnGetHashSize)
    301338    {
    302         uint32_t cbHash = pThis->pDesc->pfnGetHashSize(pThis->abState);
     339        uint32_t cbHash = pThis->pDesc->pfnGetHashSize(pThis->pvState);
    303340        Assert(cbHash <= pThis->pDesc->cbHash);
    304341        return cbHash;
     
    334371    RTDIGESTTYPE enmType = pThis->pDesc->enmType;
    335372    if (pThis->pDesc->pfnGetDigestType)
    336         enmType = pThis->pDesc->pfnGetDigestType(pThis->abState);
     373        enmType = pThis->pDesc->pfnGetDigestType(pThis->pvState);
    337374    return enmType;
    338375}
Note: See TracChangeset for help on using the changeset viewer.

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