VirtualBox

Changeset 64926 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Dec 16, 2016 9:43:08 PM (8 years ago)
Author:
vboxsync
Message:

RTSignTool: Added an --signature-index to the extract-exe-signer-cert command.

File:
1 edited

Legend:

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

    r64916 r64926  
    473473    return RTEXITCODE_FAILURE;
    474474}
     475
     476
     477
     478/**
     479 * Worker for recursively searching for MS nested signatures and signer infos.
     480 *
     481 * @returns Pointer to the signer info corresponding to @a iSignature.  NULL if
     482 *          not found.
     483 * @param   pSignedData     The signature to search.
     484 * @param   piNextSignature Pointer to the variable keeping track of the next
     485 *                          signature number.
     486 * @param   iReqSignature   The request signature number.
     487 * @param   ppSignedData    Where to return the signature data structure.
     488 */
     489static PRTCRPKCS7SIGNERINFO SignToolPkcs7_FindNestedSignatureByIndexWorker(PRTCRPKCS7SIGNEDDATA pSignedData,
     490                                                                           uint32_t *piNextSignature,
     491                                                                           uint32_t iReqSignature,
     492                                                                           PRTCRPKCS7SIGNEDDATA *ppSignedData)
     493{
     494    for (uint32_t iSignerInfo = 0; iSignerInfo < pSignedData->SignerInfos.cItems; iSignerInfo++)
     495    {
     496        /* Match?*/
     497        PRTCRPKCS7SIGNERINFO pSignerInfo = pSignedData->SignerInfos.papItems[iSignerInfo];
     498        if (*piNextSignature == iReqSignature)
     499        {
     500            *ppSignedData = pSignedData;
     501            return pSignerInfo;
     502        }
     503        *piNextSignature += 1;
     504
     505        /* Look for nested signatures. */
     506        for (uint32_t iAttrib = 0; iAttrib < pSignerInfo->UnauthenticatedAttributes.cItems; iAttrib++)
     507            if (pSignerInfo->UnauthenticatedAttributes.papItems[iAttrib]->enmType == RTCRPKCS7ATTRIBUTETYPE_MS_NESTED_SIGNATURE)
     508            {
     509                PRTCRPKCS7SETOFCONTENTINFOS pCntInfos;
     510                pCntInfos = pSignerInfo->UnauthenticatedAttributes.papItems[iAttrib]->uValues.pContentInfos;
     511                for (uint32_t iCntInfo = 0; iCntInfo < pCntInfos->cItems; iCntInfo++)
     512                {
     513                    PRTCRPKCS7CONTENTINFO pCntInfo = pCntInfos->papItems[iCntInfo];
     514                    if (RTCrPkcs7ContentInfo_IsSignedData(pCntInfo))
     515                    {
     516                        PRTCRPKCS7SIGNERINFO pRet;
     517                        pRet = SignToolPkcs7_FindNestedSignatureByIndexWorker(pCntInfo->u.pSignedData, piNextSignature,
     518                                                                              iReqSignature, ppSignedData);
     519                        if (pRet)
     520                            return pRet;
     521                    }
     522                }
     523            }
     524    }
     525    return NULL;
     526}
     527
     528
     529/**
     530 * Locates the given nested signature.
     531 *
     532 * @returns Pointer to the signer info corresponding to @a iSignature.  NULL if
     533 *          not found.
     534 * @param   pThis           The PKCS\#7 structure to search.
     535 * @param   iReqSignature   The requested signature number.
     536 * @param   ppSignedData    Where to return the pointer to the signed data that
     537 *                          the returned signer info belongs to.
     538 *
     539 * @todo    Move into SPC or PKCS\#7.
     540 */
     541static PRTCRPKCS7SIGNERINFO SignToolPkcs7_FindNestedSignatureByIndex(PSIGNTOOLPKCS7 pThis, uint32_t iReqSignature,
     542                                                                     PRTCRPKCS7SIGNEDDATA *ppSignedData)
     543{
     544    uint32_t iNextSignature = 0;
     545    return SignToolPkcs7_FindNestedSignatureByIndexWorker(pThis->pSignedData, &iNextSignature, iReqSignature, ppSignedData);
     546}
     547
    475548
    476549
     
    797870{
    798871    RT_NOREF_PV(enmLevel);
    799     RTStrmPrintf(pStrm, "extract-exe-signer-cert [--ber|--cer|--der] [--exe|-e] <exe> [--output|-o] <outfile.cer>\n");
     872    RTStrmPrintf(pStrm, "extract-exe-signer-cert [--ber|--cer|--der] [--signature-index|-i <num>] [--exe|-e] <exe> [--output|-o] <outfile.cer>\n");
    800873    return RTEXITCODE_SUCCESS;
    801874}
     
    808881    static const RTGETOPTDEF s_aOptions[] =
    809882    {
    810         { "--ber",    'b', RTGETOPT_REQ_NOTHING },
    811         { "--cer",    'c', RTGETOPT_REQ_NOTHING },
    812         { "--der",    'd', RTGETOPT_REQ_NOTHING },
    813         { "--exe",    'e', RTGETOPT_REQ_STRING },
    814         { "--output", 'o', RTGETOPT_REQ_STRING },
     883        { "--ber",              'b', RTGETOPT_REQ_NOTHING },
     884        { "--cer",              'c', RTGETOPT_REQ_NOTHING },
     885        { "--der",              'd', RTGETOPT_REQ_NOTHING },
     886        { "--exe",              'e', RTGETOPT_REQ_STRING  },
     887        { "--output",           'o', RTGETOPT_REQ_STRING  },
     888        { "--signature-index",  'i', RTGETOPT_REQ_UINT32  },
    815889    };
    816890
     
    820894    unsigned    cVerbosity   = 0;
    821895    uint32_t    fCursorFlags = RTASN1CURSOR_FLAGS_DER;
     896    uint32_t    iSignature   = 0;
    822897
    823898    RTGETOPTSTATE GetState;
     
    835910            case 'c':   fCursorFlags = RTASN1CURSOR_FLAGS_CER; break;
    836911            case 'd':   fCursorFlags = RTASN1CURSOR_FLAGS_DER; break;
     912            case 'i':   iSignature = ValueUnion.u32; break;
    837913            case 'V':   return HandleVersion(cArgs, papszArgs);
    838914            case 'h':   return HelpExtractExeSignerCert(g_pStdOut, RTSIGNTOOLHELP_FULL);
     
    866942    if (rcExit == RTEXITCODE_SUCCESS)
    867943    {
    868         /* Find the signing certificate (ASSUMING there's only one signer and that
    869            the certificate used is shipped in the set of certificates). */
     944        /* Find the signing certificate (ASSUMING that the certificate used is shipped in the set of certificates). */
     945        PRTCRPKCS7SIGNEDDATA  pSignedData;
     946        PCRTCRPKCS7SIGNERINFO pSignerInfo = SignToolPkcs7_FindNestedSignatureByIndex(&This, iSignature, &pSignedData);
    870947        rcExit = RTEXITCODE_FAILURE;
    871         if (This.pSignedData->SignerInfos.cItems == 1)
    872         {
    873             PCRTCRPKCS7ISSUERANDSERIALNUMBER pISN = &This.pSignedData->SignerInfos.papItems[0]->IssuerAndSerialNumber;
     948        if (pSignerInfo)
     949        {
     950            PCRTCRPKCS7ISSUERANDSERIALNUMBER pISN = &pSignedData->SignerInfos.papItems[0]->IssuerAndSerialNumber;
    874951            PCRTCRX509CERTIFICATE pCert;
    875             pCert = RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(&This.pSignedData->Certificates,
     952            pCert = RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(&pSignedData->Certificates,
    876953                                                                        &pISN->Name, &pISN->SerialNumber);
    877954            if (pCert)
     
    910987        }
    911988        else
    912             RTMsgError("SignerInfo count: %u", This.pSignedData->SignerInfos.cItems);
    913 
     989            RTMsgError("Could not locate signature #%u!", iSignature);
    914990
    915991        /* Delete the signature data. */
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