VirtualBox

Changeset 95625 in vbox


Ignore:
Timestamp:
Jul 13, 2022 8:32:39 PM (2 years ago)
Author:
vboxsync
Message:

RTSignTool/sign-exe: More work, output looks better now. bugref:8691

File:
1 edited

Legend:

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

    r95616 r95625  
    963963#ifndef IPRT_IN_BUILD_TOOL
    964964
     965static PRTCRPKCS7ATTRIBUTE SignToolPkcs7_AuthAttribAppend(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId)
     966{
     967    int32_t iPos = RTCrPkcs7Attributes_Append(pAuthAttribs);
     968    if (iPos >= 0)
     969    {
     970        /* Create the attrib and its sub-set of timestamps. */
     971        PRTCRPKCS7ATTRIBUTE pAttr = pAuthAttribs->papItems[iPos];
     972        int rc = RTAsn1ObjId_InitFromString(&pAttr->Type, pszAttrId, pAttr->Allocation.pAllocator);
     973        if (RT_SUCCESS(rc))
     974            return pAttr;
     975        RTMsgError("RTAsn1ObjId_InitFromString/%s failed: %Rrc", pszAttrId, rc);
     976    }
     977    else
     978        RTMsgError("RTCrPkcs7Attributes_Append failed: %Rrc", iPos);
     979
     980    return NULL;
     981}
     982
     983
    965984static RTEXITCODE SignToolPkcs7_AuthAttribsAddSigningTime(PRTCRPKCS7ATTRIBUTES pAuthAttribs, RTTIMESPEC SigningTime)
    966985{
     
    972991     *                                     ^^- end 2016-10-05T07:50:30.000000000Z (161005075030Z)
    973992     */
    974     int32_t iPos = RTCrPkcs7Attributes_Append(pAuthAttribs);
    975     if (iPos < 0)
    976         return RTMsgErrorExitFailure("RTCrPkcs7Attributes_Append failed: %Rrc", iPos);
    977 
    978     /* Create the attrib and its sub-set of timestamps. */
    979     PRTCRPKCS7ATTRIBUTE pAttr = pAuthAttribs->papItems[iPos];
    980     int rc = RTAsn1ObjId_InitFromString(&pAttr->Type, RTCR_PKCS9_ID_SIGNING_TIME_OID, pAttr->Allocation.pAllocator);
    981     if (RT_FAILURE(rc))
    982         return RTMsgErrorExitFailure("RTAsn1ObjId_InitFromString/RTCR_PKCS9_ID_SIGNING_TIME_OID failed: %Rrc", rc);
     993    PRTCRPKCS7ATTRIBUTE pAttr = SignToolPkcs7_AuthAttribAppend(pAuthAttribs, RTCR_PKCS9_ID_SIGNING_TIME_OID);
     994    if (!pAttr)
     995        return RTEXITCODE_FAILURE;
    983996
    984997    /** @todo Generalize the Type + enmType DYN stuff and generate setters. */
     
    986999    Assert(pAttr->uValues.pContentInfos == NULL);
    9871000    pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_SIGNING_TIME;
    988     rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pSigningTime, sizeof(*pAttr->uValues.pSigningTime));
     1001    int rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pSigningTime, sizeof(*pAttr->uValues.pSigningTime));
    9891002    if (RT_FAILURE(rc))
    9901003        return RTMsgErrorExitFailure("RTAsn1MemAllocZ failed: %Rrc", rc);
     
    9951008
    9961009    /* Create the timestamp. */
    997     iPos = RTAsn1SetOfTimes_Append(pAttr->uValues.pSigningTime);
     1010    int32_t iPos = RTAsn1SetOfTimes_Append(pAttr->uValues.pSigningTime);
    9981011    if (iPos < 0)
    9991012        return RTMsgErrorExitFailure("RTAsn1SetOfTimes_Append failed: %Rrc", iPos);
     
    10071020}
    10081021
     1022
    10091023static RTEXITCODE SignToolPkcs7_AuthAttribsAddObjIdSeqsEmpty(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId)
    10101024{
    1011     int32_t iPos = RTCrPkcs7Attributes_Append(pAuthAttribs);
    1012     if (iPos < 0)
    1013         return RTMsgErrorExitFailure("RTCrPkcs7Attributes_Append failed: %Rrc", iPos);
    1014 
    1015     /* Create the attrib and its sub-set of timestamps. */
    1016     PRTCRPKCS7ATTRIBUTE pAttr = pAuthAttribs->papItems[iPos];
    1017     int rc = RTAsn1ObjId_InitFromString(&pAttr->Type, pszAttrId, pAttr->Allocation.pAllocator);
    1018     if (RT_FAILURE(rc))
    1019         return RTMsgErrorExitFailure("RTAsn1ObjId_InitFromString/%s failed: %Rrc", pszAttrId, rc);
     1025    PRTCRPKCS7ATTRIBUTE pAttr = SignToolPkcs7_AuthAttribAppend(pAuthAttribs, pszAttrId);
     1026    if (!pAttr)
     1027        return RTEXITCODE_FAILURE;
    10201028
    10211029    /** @todo Generalize the Type + enmType DYN stuff and generate setters. */
     
    10231031    Assert(pAttr->uValues.pContentInfos == NULL);
    10241032    pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_MS_STATEMENT_TYPE;
    1025     rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pObjIdSeqs, sizeof(*pAttr->uValues.pObjIdSeqs));
     1033    int rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pObjIdSeqs, sizeof(*pAttr->uValues.pObjIdSeqs));
    10261034    if (RT_FAILURE(rc))
    10271035        return RTMsgErrorExitFailure("RTAsn1MemAllocZ failed: %Rrc", rc);
     
    10331041    return RTEXITCODE_SUCCESS;
    10341042}
     1043
    10351044
    10361045static RTEXITCODE SignToolPkcs7_AuthAttribsAddObjIdSeqsValue(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId,
     
    10571066    RTAsn1ObjId_Delete(&ObjIdValue);
    10581067    if (RT_FAILURE(rc))
    1059         return RTMsgErrorExitFailure("RTAsn1SeqOfObjIds_InsertEx failed: %Rrc", iPos);
     1068        return RTMsgErrorExitFailure("RTAsn1SeqOfObjIds_InsertEx failed: %Rrc", rc);
    10601069
    10611070    return RTEXITCODE_SUCCESS;
    10621071}
     1072
     1073
     1074static RTEXITCODE SignToolPkcs7_AuthAttribsAddObjIdValue(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszAttrId,
     1075                                                         const char *pszValueId)
     1076{
     1077    /* Create the attrib and its sub-set of object IDs. */
     1078    PRTCRPKCS7ATTRIBUTE pAttr = SignToolPkcs7_AuthAttribAppend(pAuthAttribs, pszAttrId);
     1079    if (!pAttr)
     1080        return RTEXITCODE_FAILURE;
     1081
     1082    /** @todo Generalize the Type + enmType DYN stuff and generate setters. */
     1083    Assert(pAttr->enmType == RTCRPKCS7ATTRIBUTETYPE_NOT_PRESENT);
     1084    Assert(pAttr->uValues.pContentInfos == NULL);
     1085    pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS;
     1086    int rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pObjIds, sizeof(*pAttr->uValues.pObjIds));
     1087    if (RT_FAILURE(rc))
     1088        return RTMsgErrorExitFailure("RTAsn1MemAllocZ failed: %Rrc", rc);
     1089
     1090    rc = RTAsn1SetOfObjIds_Init(pAttr->uValues.pObjIds, pAttr->Allocation.pAllocator);
     1091    if (RT_FAILURE(rc))
     1092        return RTMsgErrorExitFailure("RTAsn1SetOfObjIdSeqs_Init failed: %Rrc", rc);
     1093
     1094    /* Add a object id to the value. */
     1095    RTASN1OBJID ObjIdValue;
     1096    rc = RTAsn1ObjId_InitFromString(&ObjIdValue, pszValueId, &g_RTAsn1DefaultAllocator);
     1097    if (RT_FAILURE(rc))
     1098        return RTMsgErrorExitFailure("RTAsn1ObjId_InitFromString/%s failed: %Rrc", pszAttrId, rc);
     1099
     1100    rc = RTAsn1SetOfObjIds_InsertEx(pAttr->uValues.pObjIds, 0 /*iPos*/, &ObjIdValue, &g_RTAsn1DefaultAllocator, NULL);
     1101    RTAsn1ObjId_Delete(&ObjIdValue);
     1102    if (RT_FAILURE(rc))
     1103        return RTMsgErrorExitFailure("RTAsn1SetOfObjIds_InsertEx failed: %Rrc", rc);
     1104
     1105    return RTEXITCODE_SUCCESS;
     1106}
     1107
    10631108
    10641109static RTEXITCODE SignToolPkcs7_AddAuthAttribsForTimestamp(PRTCRPKCS7ATTRIBUTES pAuthAttribs, bool fTimestampTypeOld,
     
    10801125    return RTEXITCODE_SUCCESS;
    10811126}
     1127
    10821128
    10831129static RTEXITCODE SignToolPkcs7_AddAuthAttribsForImageSignature(PRTCRPKCS7ATTRIBUTES pAuthAttribs, RTTIMESPEC SigningTime)
     
    10901136     *                   Set Of -^^    ^^- Empty Sequence.
    10911137     */
     1138    /** @todo ends up with zero byte instead of two.   */
    10921139    RTEXITCODE rcExit = SignToolPkcs7_AuthAttribsAddObjIdSeqsEmpty(pAuthAttribs, RTCR_PKCS9_ID_MS_SP_OPUS_INFO);
    10931140    if (rcExit != RTEXITCODE_SUCCESS)
     
    11021149     *                              ^- Set Of
    11031150     */
     1151    rcExit = SignToolPkcs7_AuthAttribsAddObjIdValue(pAuthAttribs, RTCR_PKCS9_ID_CONTENT_TYPE_OID,
     1152                                                    RTCRSPCINDIRECTDATACONTENT_OID);
     1153    if (rcExit != RTEXITCODE_SUCCESS)
     1154        return rcExit;
    11041155
    11051156    /*
     
    14721523}
    14731524
    1474 static RTEXITCODE SignToolPkcs7_SpcCompleteWithoutPageHashes(SIGNTOOLPKCS7EXE *pThis, RTCRSPCINDIRECTDATACONTENT *pSpcIndData)
    1475 {
    1476     RT_NOREF(pThis);
    1477     PRTCRSPCPEIMAGEDATA pPeImage = pSpcIndData->Data.uValue.pPeImage;
     1525static RTEXITCODE SignToolPkcs7_SpcCompleteWithoutPageHashes(RTCRSPCINDIRECTDATACONTENT *pSpcIndData)
     1526{
     1527    PCRTASN1ALLOCATORVTABLE const pAllocator = &g_RTAsn1DefaultAllocator;
     1528    PRTCRSPCPEIMAGEDATA const     pPeImage  = pSpcIndData->Data.uValue.pPeImage;
    14781529    Assert(pPeImage);
    1479     //pPeImage->Flags
    1480     RT_NOREF(pPeImage);
    1481 
    1482     return RTEXITCODE_SUCCESS;
     1530
     1531    /*
     1532     * Set it to File with an empty name.
     1533     *         RTCRSPCPEIMAGEDATA::Flags -vv
     1534     * RTCRSPCPEIMAGEDATA::SeqCore -vv         T0 -vv    vv- pT2/CtxTag2
     1535     *   0040: 04 01 82 37 02 01 0f 30-09 03 01 00 a0 04 a2 02 ...7...0........
     1536     *   0050: 80 00 30 21 30 09 06 05-2b 0e 03 02 1a 05 00 04 ..0!0...+.......
     1537     *         ^^- pUcs2 / empty string
     1538     */
     1539
     1540    /* Create an empty BMP string. */
     1541    RTASN1STRING EmptyStr;
     1542    int rc = RTAsn1BmpString_Init(&EmptyStr, pAllocator);
     1543    if (RT_FAILURE(rc))
     1544        return RTMsgErrorExitFailure("RTAsn1BmpString_Init/Ucs2 failed: %Rrc", rc);
     1545
     1546    /* Create an SPC string and use the above empty string with the Ucs2 setter. */
     1547    RTEXITCODE    rcExit = RTEXITCODE_FAILURE;
     1548    RTCRSPCSTRING SpcString;
     1549    rc = RTCrSpcString_Init(&SpcString, pAllocator);
     1550    if (RT_SUCCESS(rc))
     1551    {
     1552        rc = RTCrSpcString_SetUcs2(&SpcString, &EmptyStr, pAllocator);
     1553        if (RT_SUCCESS(rc))
     1554        {
     1555            /* Create a temporary SpcLink with the empty SpcString. */
     1556            RTCRSPCLINK SpcLink;
     1557            rc = RTCrSpcLink_Init(&SpcLink, pAllocator);
     1558            if (RT_SUCCESS(rc))
     1559            {
     1560                /* Use the setter on the SpcLink object to copy the SpcString to it. */
     1561                rc = RTCrSpcLink_SetFile(&SpcLink, &SpcString, pAllocator);
     1562                if (RT_SUCCESS(rc))
     1563                {
     1564                    /* Use the setter to copy SpcLink to the PeImage structure. */
     1565                    rc = RTCrSpcPeImageData_SetFile(pPeImage, &SpcLink, pAllocator);
     1566                    if (RT_SUCCESS(rc))
     1567                        rcExit = RTEXITCODE_SUCCESS;
     1568                    else
     1569                        RTMsgError("RTCrSpcPeImageData_SetFile failed: %Rrc", rc);
     1570                }
     1571                else
     1572                    RTMsgError("RTCrSpcLink_SetFile failed: %Rrc", rc);
     1573                RTCrSpcLink_Delete(&SpcLink);
     1574            }
     1575            else
     1576                RTMsgError("RTCrSpcLink_Init failed: %Rrc", rc);
     1577        }
     1578        else
     1579            RTMsgError("RTCrSpcString_SetUcs2 failed: %Rrc", rc);
     1580        RTCrSpcString_Delete(&SpcString);
     1581    }
     1582    else
     1583        RTMsgError("RTCrSpcString_Init failed: %Rrc", rc);
     1584    RTAsn1BmpString_Delete(&EmptyStr);
     1585    return rcExit;
    14831586}
    14841587
     
    15321635     * and signed.
    15331636     */
    1534     RTCRSPCINDIRECTDATACONTENT SpcIndData;
    1535     int rc = RTCrSpcIndirectDataContent_Init(&SpcIndData, &g_RTAsn1DefaultAllocator);
     1637    PCRTASN1ALLOCATORVTABLE const   pAllocator = &g_RTAsn1DefaultAllocator;
     1638    RTCRSPCINDIRECTDATACONTENT      SpcIndData;
     1639    int rc = RTCrSpcIndirectDataContent_Init(&SpcIndData, pAllocator);
    15361640    if (RT_FAILURE(rc))
    15371641        return RTMsgErrorExitFailure("RTCrSpcIndirectDataContent_Init failed: %Rrc", rc);
     
    15411645    Assert(SpcIndData.Data.enmType == RTCRSPCAAOVTYPE_NOT_PRESENT);
    15421646    Assert(SpcIndData.Data.uValue.pPeImage == NULL);
    1543     SpcIndData.Data.enmType = RTCRSPCAAOVTYPE_PE_IMAGE_DATA;
    1544     rc = RTAsn1MemAllocZ(&SpcIndData.Data.Allocation, (void **)&SpcIndData.Data.uValue.pPeImage,
    1545                          sizeof(*SpcIndData.Data.uValue.pPeImage));
    15461647    RTEXITCODE rcExit;
     1648    rc = RTAsn1ObjId_SetFromString(&SpcIndData.Data.Type, RTCRSPCPEIMAGEDATA_OID, pAllocator);
    15471649    if (RT_SUCCESS(rc))
    15481650    {
    1549         rc = RTCrSpcPeImageData_Init(SpcIndData.Data.uValue.pPeImage, SpcIndData.Data.Allocation.pAllocator);
     1651        SpcIndData.Data.enmType = RTCRSPCAAOVTYPE_PE_IMAGE_DATA;
     1652        rc = RTAsn1MemAllocZ(&SpcIndData.Data.Allocation, (void **)&SpcIndData.Data.uValue.pPeImage,
     1653                             sizeof(*SpcIndData.Data.uValue.pPeImage));
    15501654        if (RT_SUCCESS(rc))
    15511655        {
    1552             /* Add the hashes. */
    1553             rcExit = SignToolPkcs7_SpcAddImageHash(pThis, &SpcIndData, enmSigType);
    1554             if (rcExit == RTEXITCODE_SUCCESS)
     1656            rc = RTCrSpcPeImageData_Init(SpcIndData.Data.uValue.pPeImage, pAllocator);
     1657            if (RT_SUCCESS(rc))
    15551658            {
    1556                 if (fHashPages)
    1557                     rcExit = SignToolPkcs7_SpcAddImagePageHashes(pThis, &SpcIndData, enmSigType);
    1558                 else
    1559                     rcExit = SignToolPkcs7_SpcCompleteWithoutPageHashes(pThis, &SpcIndData);
     1659                /* Old (SHA1) signatures has a Flags member, it's zero bits, though. */
     1660                if (enmSigType == RTDIGESTTYPE_SHA1)
     1661                {
     1662                    uint8_t         bFlags = 0;
     1663                    RTASN1BITSTRING Flags;
     1664                    rc = RTAsn1BitString_InitWithData(&Flags, &bFlags, 0, pAllocator);
     1665                    if (RT_SUCCESS(rc))
     1666                    {
     1667                        rc = RTCrSpcPeImageData_SetFlags(SpcIndData.Data.uValue.pPeImage, &Flags, pAllocator);
     1668                        RTAsn1BitString_Delete(&Flags);
     1669                        if (RT_FAILURE(rc))
     1670                            rcExit = RTMsgErrorExitFailure("RTCrSpcPeImageData_SetFlags failed: %Rrc", rc);
     1671                    }
     1672                    else
     1673                        rcExit = RTMsgErrorExitFailure("RTAsn1BitString_InitWithData failed: %Rrc", rc);
     1674                }
    15601675
    15611676                /*
    1562                  * Encode and sign the SPC data, timestamp it, and line it up for adding to the executable.
     1677                 * Add the hashes.
    15631678                 */
     1679                rcExit = SignToolPkcs7_SpcAddImageHash(pThis, &SpcIndData, enmSigType);
    15641680                if (rcExit == RTEXITCODE_SUCCESS)
    1565                     rcExit = SignToolPkcs7_SignSpcIndData(pThis, &SpcIndData, cVerbosity, fReplaceExisting, pSigningCertKey,
    1566                                                           hAddCerts, fTimestampTypeOld, SigningTime, pTimestampCertKey);
     1681                {
     1682                    if (fHashPages)
     1683                        rcExit = SignToolPkcs7_SpcAddImagePageHashes(pThis, &SpcIndData, enmSigType);
     1684                    else
     1685                        rcExit = SignToolPkcs7_SpcCompleteWithoutPageHashes(&SpcIndData);
     1686
     1687                    /*
     1688                     * Encode and sign the SPC data, timestamp it, and line it up for adding to the executable.
     1689                     */
     1690                    if (rcExit == RTEXITCODE_SUCCESS)
     1691                        rcExit = SignToolPkcs7_SignSpcIndData(pThis, &SpcIndData, cVerbosity, fReplaceExisting, pSigningCertKey,
     1692                                                              hAddCerts, fTimestampTypeOld, SigningTime, pTimestampCertKey);
     1693                }
    15671694            }
     1695            else
     1696                rcExit = RTMsgErrorExitFailure("RTCrPkcs7SignerInfos_Init failed: %Rrc", rc);
    15681697        }
    15691698        else
    1570             rcExit = RTMsgErrorExitFailure("RTCrPkcs7SignerInfos_Init failed: %Rrc", rc);
     1699            rcExit = RTMsgErrorExitFailure("RTAsn1MemAllocZ failed for RTCRSPCPEIMAGEDATA: %Rrc", rc);
    15711700    }
    15721701    else
    1573         rcExit = RTMsgErrorExitFailure("RTAsn1MemAllocZ failed for RTCRSPCPEIMAGEDATA: %Rrc", rc);
     1702        rcExit = RTMsgErrorExitFailure("RTAsn1ObjId_SetWithString/SpcPeImageData failed: %Rrc", rc);
    15741703
    15751704    RTCrSpcIndirectDataContent_Delete(&SpcIndData);
     
    33073436                    else
    33083437                        RTPrintf("%s        File: rc=%Rrc\n", pThis->szPrefix, rc);
     3438                    if (pThis->cVerbosity > 4 && pPeImage->T0.File.u.pT2 == NULL)
     3439                        RTPrintf("%s        pT2=NULL\n", pThis->szPrefix);
     3440                    else if (pThis->cVerbosity > 4)
     3441                    {
     3442                        PCRTASN1STRING pStr = pPeImage->T0.File.u.pT2->File.u.pAscii;
     3443                        RTPrintf("%s        pT2=%p/%p LB %#x fFlags=%#x pOps=%p (%s)\n"
     3444                                 "%s        enmChoice=%d pStr=%p/%p LB %#x fFlags=%#x\n",
     3445                                 pThis->szPrefix,
     3446                                 pPeImage->T0.File.u.pT2,
     3447                                 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.uData.pu8,
     3448                                 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.cb,
     3449                                 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.fFlags,
     3450                                 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.pOps,
     3451                                 pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.pOps
     3452                                 ? pPeImage->T0.File.u.pT2->CtxTag2.Asn1Core.pOps->pszName : "",
     3453                                 pThis->szPrefix,
     3454                                 pPeImage->T0.File.u.pT2->File.enmChoice,
     3455                                 pStr,
     3456                                 pStr ? pStr->Asn1Core.uData.pu8 : NULL,
     3457                                 pStr ? pStr->Asn1Core.cb : 0,
     3458                                 pStr ? pStr->Asn1Core.fFlags : 0);
     3459                    }
    33093460                    break;
    33103461                }
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