VirtualBox

Ignore:
Timestamp:
Jul 14, 2022 2:12:29 AM (2 years ago)
Author:
vboxsync
Message:

RTSignTool: Initial implementation of page hashes. bugref:8691

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/tools/RTSignTool.cpp

    r95633 r95637  
    15661566                                                      RTDIGESTTYPE enmSigType)
    15671567{
    1568     RT_NOREF(pThis, pSpcIndData, enmSigType);
     1568    PCRTASN1ALLOCATORVTABLE const pAllocator = &g_RTAsn1DefaultAllocator;
     1569    PRTCRSPCPEIMAGEDATA const     pPeImage   = pSpcIndData->Data.uValue.pPeImage;
     1570    Assert(pPeImage);
     1571
     1572    /*
     1573     * The hashes are stored in the 'Moniker' attribute.
     1574     */
     1575    /* Create a temporary SpcLink with a default moniker. */
     1576    RTCRSPCLINK SpcLink;
     1577    int rc = RTCrSpcLink_Init(&SpcLink, pAllocator);
     1578    if (RT_FAILURE(rc))
     1579        return RTMsgErrorExitFailure("RTCrSpcLink_Init failed: %Rrc", rc);
     1580    rc = RTCrSpcLink_SetMoniker(&SpcLink, NULL, pAllocator);
     1581    if (RT_SUCCESS(rc))
     1582    {
     1583        /* Use the setter to copy SpcLink to the PeImage structure. */
     1584        rc = RTCrSpcPeImageData_SetFile(pPeImage, &SpcLink, pAllocator);
     1585        if (RT_FAILURE(rc))
     1586            RTMsgError("RTCrSpcLink_SetFile failed: %Rrc", rc);
     1587    }
     1588    else
     1589        RTMsgError("RTCrSpcLink_SetMoniker failed: %Rrc", rc);
     1590    RTCrSpcLink_Delete(&SpcLink);
     1591    if (RT_FAILURE(rc))
     1592        return RTEXITCODE_FAILURE;
     1593
     1594    /*
     1595     * Now go to work on the moniker.  It doesn't have any autogenerated
     1596     * setters, so we must do stuff manually.
     1597     */
     1598    PRTCRSPCSERIALIZEDOBJECT pMoniker = pPeImage->T0.File.u.pMoniker;
     1599    RTUUID                   Uuid;
     1600    rc = RTUuidFromStr(&Uuid, RTCRSPCSERIALIZEDOBJECT_UUID_STR);
     1601    if (RT_FAILURE(rc))
     1602        return RTMsgErrorExitFailure("RTUuidFromStr failed: %Rrc", rc);
     1603
     1604    rc = RTAsn1OctetString_AllocContent(&pMoniker->Uuid, &Uuid, sizeof(Uuid), pAllocator);
     1605    if (RT_FAILURE(rc))
     1606        return RTMsgErrorExitFailure("RTAsn1String_InitWithValue/UUID failed: %Rrc", rc);
     1607
     1608    /* Create a new set of attributes and associate this with the SerializedData member. */
     1609    PRTCRSPCSERIALIZEDOBJECTATTRIBUTES pSpcAttribs;
     1610    rc = RTAsn1MemAllocZ(&pMoniker->SerializedData.EncapsulatedAllocation,
     1611                         (void **)&pSpcAttribs, sizeof(*pSpcAttribs));
     1612    if (RT_FAILURE(rc))
     1613        return RTMsgErrorExitFailure("RTAsn1MemAllocZ/pSpcAttribs failed: %Rrc", rc);
     1614    pMoniker->SerializedData.pEncapsulated = RTCrSpcSerializedObjectAttributes_GetAsn1Core(pSpcAttribs);
     1615    pMoniker->enmType                      = RTCRSPCSERIALIZEDOBJECTTYPE_ATTRIBUTES;
     1616    pMoniker->u.pData                      = pSpcAttribs;
     1617
     1618    rc = RTCrSpcSerializedObjectAttributes_Init(pSpcAttribs, pAllocator);
     1619    if (RT_FAILURE(rc))
     1620        return RTMsgErrorExitFailure("RTCrSpcSerializedObjectAttributes_Init failed: %Rrc", rc);
     1621
     1622    /*
     1623     * Add a single attribute to the set that we'll use for page hashes.
     1624     */
     1625    int32_t iPos = RTCrSpcSerializedObjectAttributes_Append(pSpcAttribs);
     1626    if (iPos < 0)
     1627        return RTMsgErrorExitFailure("RTCrSpcSerializedObjectAttributes_Append failed: %Rrc", iPos);
     1628    PRTCRSPCSERIALIZEDOBJECTATTRIBUTE pSpcObjAttr = pSpcAttribs->papItems[iPos];
     1629
     1630    if (enmSigType == RTDIGESTTYPE_SHA1)
     1631        rc = RTCrSpcSerializedObjectAttribute_SetV1Hashes(pSpcObjAttr, NULL, pAllocator);
     1632    else if (enmSigType == RTDIGESTTYPE_SHA256)
     1633        rc = RTCrSpcSerializedObjectAttribute_SetV2Hashes(pSpcObjAttr, NULL, pAllocator);
     1634    else
     1635        rc = VERR_CR_DIGEST_NOT_SUPPORTED;
     1636    if (RT_FAILURE(rc))
     1637        return RTMsgErrorExitFailure("RTCrSpcSerializedObjectAttribute_SetV1Hashes/SetV2Hashes failed: %Rrc", rc);
     1638    PRTCRSPCSERIALIZEDPAGEHASHES pSpcPageHashes = pSpcObjAttr->u.pPageHashes;
     1639    Assert(pSpcPageHashes);
     1640
     1641    /*
     1642     * Now ask the loader for the number of pages in the page hash table
     1643     * and calculate its size.
     1644     */
     1645    uint32_t cPages = 0;
     1646    rc = RTLdrQueryPropEx(pThis->hLdrMod, RTLDRPROP_HASHABLE_PAGES, NULL, &cPages, sizeof(cPages), NULL);
     1647    if (RT_FAILURE(rc))
     1648        return RTMsgErrorExitFailure("RTLdrQueryPropEx/RTLDRPROP_HASHABLE_PAGES failed: %Rrc", rc);
     1649
     1650    uint32_t const cbHash  = RTCrDigestTypeToHashSize(enmSigType);
     1651    AssertReturn(cbHash > 0, RTMsgErrorExitFailure("Invalid value: enmSigType=%d", enmSigType));
     1652    uint32_t const cbTable = (sizeof(uint32_t) + cbHash) * cPages;
     1653
     1654    /*
     1655     * Allocate memory in the octect string.
     1656     */
     1657    rc = RTAsn1ContentAllocZ(&pSpcPageHashes->RawData.Asn1Core, cbTable, pAllocator);
     1658    if (RT_FAILURE(rc))
     1659        return RTMsgErrorExitFailure("RTAsn1ContentAllocZ failed to allocate %#x bytes for page hashes: %Rrc", cbTable, rc);
     1660    pSpcPageHashes->pData = (PCRTCRSPCPEIMAGEPAGEHASHES)pSpcPageHashes->RawData.Asn1Core.uData.pu8;
     1661
     1662    RTLDRPROP enmLdrProp;
     1663    switch (enmSigType)
     1664    {
     1665        case RTDIGESTTYPE_SHA1:     enmLdrProp = RTLDRPROP_SHA1_PAGE_HASHES; break;
     1666        case RTDIGESTTYPE_SHA256:   enmLdrProp = RTLDRPROP_SHA256_PAGE_HASHES; break;
     1667        default: AssertFailedReturn(RTMsgErrorExitFailure("Invalid value: enmSigType=%d", enmSigType));
     1668
     1669    }
     1670    rc = RTLdrQueryPropEx(pThis->hLdrMod, enmLdrProp, NULL, (void *)pSpcPageHashes->RawData.Asn1Core.uData.pv, cbTable, NULL);
     1671    if (RT_FAILURE(rc))
     1672        return RTMsgErrorExitFailure("RTLdrQueryPropEx/RTLDRPROP_SHA?_PAGE_HASHES/%#x failed: %Rrc", cbTable, rc);
     1673
    15691674    return RTEXITCODE_SUCCESS;
    15701675}
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