VirtualBox

Changeset 95657 in vbox


Ignore:
Timestamp:
Jul 15, 2022 1:05:33 AM (2 years ago)
Author:
vboxsync
Message:

RTSignTool: Added a sign-cat command. Output PKCS#7 V1 w/o extra octet string wrappers. bugref:8691

File:
1 edited

Legend:

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

    r95637 r95657  
    5454#include <iprt/crypto/store.h>
    5555#include <iprt/crypto/spc.h>
     56#include <iprt/crypto/tsp.h>
    5657#ifdef VBOX
    5758# include <VBox/sup.h> /* Certificates */
     
    10121013static RTEXITCODE SignToolPkcs7_AuthAttribsAddSpcOpusInfo(PRTCRPKCS7ATTRIBUTES pAuthAttribs, void *pvInfo)
    10131014{
     1015    /** @todo The OpusInfo is a structure with an optional SpcString and an
     1016     * optional SpcLink (url). The two attributes can be set using the /d and /du
     1017     * options of MS signtool.exe, I think.  We shouldn't be using them atm. */
     1018
    10141019    PRTCRPKCS7ATTRIBUTE pAttr = SignToolPkcs7_AuthAttribAppend(pAuthAttribs);
    10151020    if (!pAttr)
     
    10651070}
    10661071
     1072
    10671073static RTEXITCODE SignToolPkcs7_AuthAttribsAddContentType(PRTCRPKCS7ATTRIBUTES pAuthAttribs, const char *pszContentTypeId)
    10681074{
     
    10941100{
    10951101    /*
    1096      * Add signing time.
    1097      */
    1098     RTEXITCODE rcExit = SignToolPkcs7_AuthAttribsAddSigningTime(pAuthAttribs, SigningTime);
     1102     * Add content type.
     1103     */
     1104    RTEXITCODE rcExit = SignToolPkcs7_AuthAttribsAddContentType(pAuthAttribs,
     1105                                                                fTimestampTypeOld ? RTCR_PKCS7_DATA_OID : RTCRTSPTSTINFO_OID);
    10991106    if (rcExit != RTEXITCODE_SUCCESS)
    11001107        return rcExit;
    11011108
    11021109    /*
     1110     * Add signing time.
     1111     */
     1112    rcExit = SignToolPkcs7_AuthAttribsAddSigningTime(pAuthAttribs, SigningTime);
     1113    if (rcExit != RTEXITCODE_SUCCESS)
     1114        return rcExit;
     1115
     1116    /*
    11031117     * More later if we want to support fTimestampTypeOld = false perhaps?
    11041118     */
     
    11101124
    11111125
    1112 static RTEXITCODE SignToolPkcs7_AddAuthAttribsForImageSignature(PRTCRPKCS7ATTRIBUTES pAuthAttribs, RTTIMESPEC SigningTime)
     1126static RTEXITCODE SignToolPkcs7_AddAuthAttribsForImageOrCatSignature(PRTCRPKCS7ATTRIBUTES pAuthAttribs, RTTIMESPEC SigningTime,
     1127                                                                     const char *pszContentTypeId)
    11131128{
    11141129    /*
     
    11311146     *                              ^- Set Of
    11321147     */
    1133     rcExit = SignToolPkcs7_AuthAttribsAddContentType(pAuthAttribs, RTCRSPCINDIRECTDATACONTENT_OID);
     1148    rcExit = SignToolPkcs7_AuthAttribsAddContentType(pAuthAttribs, pszContentTypeId);
    11341149    if (rcExit != RTEXITCODE_SUCCESS)
    11351150        return rcExit;
     
    11811196
    11821197    /* Create the attrib and its sub-set of counter signatures. */
    1183 #if 1
    11841198    rc = RTCrPkcs7Attribute_SetCounterSignatures(pAttr, NULL, pAttr->Allocation.pAllocator);
    1185 #else
    1186     rc = RTAsn1ObjId_InitFromString(&pAttr->Type, RTCR_PKCS9_ID_COUNTER_SIGNATURE_OID, pAttr->Allocation.pAllocator);
    11871199    if (RT_FAILURE(rc))
    1188         return RTMsgErrorExitFailure("RTAsn1ObjId_InitFromString failed: %Rrc", rc);
    1189 
    1190     /** @todo Generalize the Type + enmType DYN stuff and generate setters. */
    1191     Assert(pAttr->enmType == RTCRPKCS7ATTRIBUTETYPE_NOT_PRESENT);
    1192     Assert(pAttr->uValues.pContentInfos == NULL);
    1193     pAttr->enmType = RTCRPKCS7ATTRIBUTETYPE_COUNTER_SIGNATURES;
    1194     rc = RTAsn1MemAllocZ(&pAttr->Allocation, (void **)&pAttr->uValues.pCounterSignatures,
    1195                          sizeof(*pAttr->uValues.pCounterSignatures));
    1196     if (RT_FAILURE(rc))
    1197         return RTMsgErrorExitFailure("RTAsn1MemAllocZ failed: %Rrc", rc);
    1198 
    1199     rc = RTCrPkcs7SignerInfos_Init(pAttr->uValues.pCounterSignatures, pAttr->Allocation.pAllocator);
    1200     if (RT_FAILURE(rc))
    1201         return RTMsgErrorExitFailure("RTCrPkcs7SignerInfos_Init failed: %Rrc", rc);
    1202 #endif
     1200        return RTMsgErrorExitFailure("RTCrPkcs7Attribute_SetCounterSignatures failed: %Rrc", rc);
    12031201
    12041202    /* Insert the counter signature. */
     
    12401238
    12411239
    1242 static RTEXITCODE Pkcs7SignStuff(const char *pszWhat, const void *pvToDataToSign, size_t cbToDataToSign,
    1243                                  PCRTCRPKCS7ATTRIBUTES pAuthAttribs, RTCRSTORE hAdditionalCerts, uint32_t fExtraFlags,
    1244                                  RTDIGESTTYPE enmDigestType, SIGNTOOLKEYPAIR *pCertKeyPair,
    1245                                  unsigned cVerbosity, void **ppvSigned, size_t *pcbSigned,
    1246                                  PRTCRPKCS7CONTENTINFO pContentInfo, PRTCRPKCS7SIGNEDDATA *ppSignedData)
     1240static RTEXITCODE SignToolPkcs7_Pkcs7SignStuff(const char *pszWhat, const void *pvToDataToSign, size_t cbToDataToSign,
     1241                                               PCRTCRPKCS7ATTRIBUTES pAuthAttribs, RTCRSTORE hAdditionalCerts,
     1242                                               uint32_t fExtraFlags, RTDIGESTTYPE enmDigestType, SIGNTOOLKEYPAIR *pCertKeyPair,
     1243                                               unsigned cVerbosity, void **ppvSigned, size_t *pcbSigned,
     1244                                               PRTCRPKCS7CONTENTINFO pContentInfo, PRTCRPKCS7SIGNEDDATA *ppSignedData)
    12471245{
    12481246    *ppvSigned = NULL;
     
    12531251
    12541252    /* Figure out how large the signature will be. */
     1253    uint32_t const  fSignFlags = RTCRPKCS7SIGN_SD_F_USE_V1 | RTCRPKCS7SIGN_SD_F_NO_SMIME_CAP | fExtraFlags;
     1254    size_t          cbSigned   = 1024;
    12551255    RTERRINFOSTATIC ErrInfo;
    1256     size_t cbSigned = 1024;
    1257     int rc = RTCrPkcs7SimpleSignSignedData(RTCRPKCS7SIGN_SD_F_NO_SMIME_CAP | fExtraFlags,
    1258                                            pCertKeyPair->pCertificate, pCertKeyPair->hPrivateKey, pvToDataToSign, cbToDataToSign,
    1259                                            enmDigestType, hAdditionalCerts, pAuthAttribs,
     1256    int rc = RTCrPkcs7SimpleSignSignedData(fSignFlags, pCertKeyPair->pCertificate, pCertKeyPair->hPrivateKey,
     1257                                           pvToDataToSign, cbToDataToSign,enmDigestType, hAdditionalCerts, pAuthAttribs,
    12601258                                           NULL, &cbSigned, RTErrInfoInitStatic(&ErrInfo));
    12611259    if (rc != VERR_BUFFER_OVERFLOW)
     
    12661264    if (!pvSigned)
    12671265        return RTMsgErrorExitFailure("Failed to allocate %#zx bytes for %s signature", cbSigned, pszWhat);
    1268     rc = RTCrPkcs7SimpleSignSignedData(RTCRPKCS7SIGN_SD_F_NO_SMIME_CAP | fExtraFlags,
    1269                                        pCertKeyPair->pCertificate, pCertKeyPair->hPrivateKey, pvToDataToSign, cbToDataToSign,
    1270                                        enmDigestType, hAdditionalCerts, pAuthAttribs,
     1266    rc = RTCrPkcs7SimpleSignSignedData(fSignFlags, pCertKeyPair->pCertificate, pCertKeyPair->hPrivateKey,
     1267                                       pvToDataToSign, cbToDataToSign, enmDigestType, hAdditionalCerts, pAuthAttribs,
    12711268                                       pvSigned, &cbSigned, RTErrInfoInitStatic(&ErrInfo));
    12721269    if (RT_SUCCESS(rc))
     
    13381335        PRTCRPKCS7SIGNEDDATA pTsSignedData = NULL;
    13391336        RTCRPKCS7CONTENTINFO TsContentInfo;
    1340         rcExit = Pkcs7SignStuff("timestamp", pSignerInfo->EncryptedDigest.Asn1Core.uData.pv,
    1341                                 pSignerInfo->EncryptedDigest.Asn1Core.cb, &AuthAttribs, NIL_RTCRSTORE /*hAdditionalCerts*/,
    1342                                 RTCRPKCS7SIGN_SD_F_DEATCHED, RTDIGESTTYPE_SHA1, pTimestampPair, cVerbosity,
    1343                                 &pvSigned, NULL /*pcbSigned*/, &TsContentInfo, &pTsSignedData);
     1337        rcExit = SignToolPkcs7_Pkcs7SignStuff("timestamp", pSignerInfo->EncryptedDigest.Asn1Core.uData.pv,
     1338                                              pSignerInfo->EncryptedDigest.Asn1Core.cb, &AuthAttribs,
     1339                                              NIL_RTCRSTORE /*hAdditionalCerts*/, RTCRPKCS7SIGN_SD_F_DEATCHED,
     1340                                              RTDIGESTTYPE_SHA1, pTimestampPair, cVerbosity,
     1341                                              &pvSigned, NULL /*pcbSigned*/, &TsContentInfo, &pTsSignedData);
    13441342        if (rcExit == RTEXITCODE_SUCCESS)
    13451343        {
     
    14091407
    14101408
    1411 static RTEXITCODE SignToolPkcs7_SignSpcIndData(SIGNTOOLPKCS7EXE *pThis, RTCRSPCINDIRECTDATACONTENT *pSpcIndData,
    1412                                                unsigned cVerbosity,  RTDIGESTTYPE enmSigType, bool fReplaceExisting,
    1413                                                SIGNTOOLKEYPAIR *pSigningCertKey, RTCRSTORE hAddCerts,
    1414                                                bool fTimestampTypeOld, RTTIMESPEC SigningTime, SIGNTOOLKEYPAIR *pTimestampCertKey)
     1409static RTEXITCODE SignToolPkcs7_SignData(SIGNTOOLPKCS7 *pThis, PRTASN1CORE pToSignRoot, bool fIsRootsParent,
     1410                                         const char *pszContentTypeId, unsigned cVerbosity,  RTDIGESTTYPE enmSigType,
     1411                                         bool fReplaceExisting, SIGNTOOLKEYPAIR *pSigningCertKey, RTCRSTORE hAddCerts,
     1412                                         bool fTimestampTypeOld, RTTIMESPEC SigningTime, SIGNTOOLKEYPAIR *pTimestampCertKey)
    14151413{
    14161414    /*
     
    14181416     */
    14191417    RTERRINFOSTATIC ErrInfo;
    1420     PRTASN1CORE     pSpcRoot     = RTCrSpcIndirectDataContent_GetAsn1Core(pSpcIndData);
    1421     uint32_t        cbSpcEncoded = 0;
    1422     int rc = RTAsn1EncodePrepare(pSpcRoot, RTASN1ENCODE_F_DER, &cbSpcEncoded, RTErrInfoInitStatic(&ErrInfo));
     1418    uint32_t        cbEncoded = 0;
     1419    int rc = RTAsn1EncodePrepare(pToSignRoot, RTASN1ENCODE_F_DER, &cbEncoded, RTErrInfoInitStatic(&ErrInfo));
    14231420    if (RT_FAILURE(rc))
    14241421        return RTMsgErrorExitFailure("RTAsn1EncodePrepare failed: %Rrc%RTeim", rc, &ErrInfo.Core);
    14251422
    14261423    if (cVerbosity >= 4)
    1427         RTAsn1Dump(pSpcRoot, 0, 0, RTStrmDumpPrintfV, g_pStdOut);
    1428 
    1429     void *pvSpcEncoded = (uint8_t *)RTMemTmpAllocZ(cbSpcEncoded);
    1430     if (!pvSpcEncoded)
    1431         return RTMsgErrorExitFailure("Failed to allocate %#z bytes for SpcIndirectData", cbSpcEncoded);
     1424        RTAsn1Dump(pToSignRoot, 0, 0, RTStrmDumpPrintfV, g_pStdOut);
     1425
     1426    void *pvEncoded = (uint8_t *)RTMemTmpAllocZ(cbEncoded);
     1427    if (!pvEncoded)
     1428        return RTMsgErrorExitFailure("Failed to allocate %#z bytes for encoding data we're signing (%s)",
     1429                                     cbEncoded, pszContentTypeId);
    14321430
    14331431    RTEXITCODE rcExit = RTEXITCODE_FAILURE;
    1434     rc = RTAsn1EncodeToBuffer(pSpcRoot, RTASN1ENCODE_F_DER, pvSpcEncoded, cbSpcEncoded, RTErrInfoInitStatic(&ErrInfo));
     1432    rc = RTAsn1EncodeToBuffer(pToSignRoot, RTASN1ENCODE_F_DER, pvEncoded, cbEncoded, RTErrInfoInitStatic(&ErrInfo));
    14351433    if (RT_SUCCESS(rc))
    14361434    {
     1435        size_t const cbToSign = cbEncoded            - (fIsRootsParent ? pToSignRoot->cbHdr : 0);
     1436        void const  *pvToSign = (uint8_t *)pvEncoded + (fIsRootsParent ? pToSignRoot->cbHdr : 0);
     1437
    14371438        /*
    14381439         * Create additional authenticated attributes.
     
    14421443        if (RT_SUCCESS(rc))
    14431444        {
    1444             rcExit = SignToolPkcs7_AddAuthAttribsForImageSignature(&AuthAttribs, SigningTime);
     1445            rcExit = SignToolPkcs7_AddAuthAttribsForImageOrCatSignature(&AuthAttribs, SigningTime, pszContentTypeId);
    14451446            if (rcExit == RTEXITCODE_SUCCESS)
    14461447            {
     
    14621463                SIGNTOOLPKCS7  Src     = { NULL, 0, NULL };
    14631464                PSIGNTOOLPKCS7 pSigDst = !pThis->pSignedData ? pThis : &Src;
    1464                 rcExit = Pkcs7SignStuff("image", pvSpcEncoded, cbSpcEncoded, &AuthAttribs, hAddCerts, 0 /*fExtraFlags*/,
    1465                                         enmSigType /** @todo ?? */, pSigningCertKey, cVerbosity,
    1466                                         (void **)&pSigDst->pbBuf, &pSigDst->cbBuf, &pSigDst->ContentInfo, &pSigDst->pSignedData);
     1465                rcExit = SignToolPkcs7_Pkcs7SignStuff("image", pvToSign, cbToSign, &AuthAttribs, hAddCerts,
     1466                                                      RTCRPKCS7SIGN_SD_F_NO_DATA_ENCAP, enmSigType /** @todo ?? */,
     1467                                                      pSigningCertKey, cVerbosity,
     1468                                                      (void **)&pSigDst->pbBuf, &pSigDst->cbBuf,
     1469                                                      &pSigDst->ContentInfo, &pSigDst->pSignedData);
    14671470                if (rcExit == RTEXITCODE_SUCCESS)
    14681471                {
     
    14861489                        SignToolPkcs7_Delete(&Src);
    14871490                }
     1491
    14881492            }
    14891493            RTCrPkcs7Attributes_Delete(&AuthAttribs);
     
    14941498    else
    14951499        RTMsgError("RTAsn1EncodeToBuffer failed: %Rrc", rc);
    1496     RTMemTmpFree(pvSpcEncoded);
     1500    RTMemTmpFree(pvEncoded);
    14971501    return rcExit;
    14981502}
     
    17721776                     */
    17731777                    if (rcExit == RTEXITCODE_SUCCESS)
    1774                         rcExit = SignToolPkcs7_SignSpcIndData(pThis, &SpcIndData, cVerbosity,
    1775                                                               enmSigType, fReplaceExisting, pSigningCertKey, hAddCerts,
    1776                                                               fTimestampTypeOld, SigningTime, pTimestampCertKey);
     1778                        rcExit = SignToolPkcs7_SignData(pThis, RTCrSpcIndirectDataContent_GetAsn1Core(&SpcIndData), false,
     1779                                                        RTCRSPCINDIRECTDATACONTENT_OID, cVerbosity,
     1780                                                        enmSigType, fReplaceExisting, pSigningCertKey, hAddCerts,
     1781                                                        fTimestampTypeOld, SigningTime, pTimestampCertKey);
    17771782                }
    17781783            }
     
    17871792
    17881793    RTCrSpcIndirectDataContent_Delete(&SpcIndData);
     1794    return rcExit;
     1795}
     1796
     1797
     1798static RTEXITCODE SignToolPkcs7_AddOrReplaceCatSignature(SIGNTOOLPKCS7 *pThis, unsigned cVerbosity, RTDIGESTTYPE enmSigType,
     1799                                                         bool fReplaceExisting, SIGNTOOLKEYPAIR *pSigningCertKey,
     1800                                                         RTCRSTORE hAddCerts, bool fTimestampTypeOld,
     1801                                                         RTTIMESPEC SigningTime, SIGNTOOLKEYPAIR *pTimestampCertKey)
     1802{
     1803    AssertReturn(fTimestampTypeOld || pTimestampCertKey->isNull(),
     1804                 RTMsgErrorExitFailure("New style signatures not supported yet"));
     1805    AssertReturn(pThis->pSignedData, RTMsgErrorExitFailure("pSignedData is NULL!"));
     1806
     1807    /*
     1808     * Figure out what to sign first.
     1809     */
     1810    PRTASN1CORE pToSign = &pThis->pSignedData->ContentInfo.Content.Asn1Core;
     1811    const char *pszType = pThis->pSignedData->ContentInfo.ContentType.szObjId;
     1812    if (!fReplaceExisting)
     1813    {
     1814        if (pThis->pSignedData->SignerInfos.cItems == 0)
     1815            fReplaceExisting = false;
     1816        else
     1817        {
     1818            /** @todo figure out nested catalog signatures... It's marked as
     1819             * pkcs7-data and seems to be empty, i.e. it's detached. */
     1820            //pszType = RTCR_PKCS7_SIGNED_DATA_OID;
     1821            AssertFailedReturn(RTMsgErrorExitFailure("nested cat signing not implemented"));
     1822        }
     1823    }
     1824
     1825    /*
     1826     * Do the signing.
     1827     */
     1828    RTEXITCODE rcExit = SignToolPkcs7_SignData(pThis, pToSign, true, pszType, cVerbosity, enmSigType, fReplaceExisting,
     1829                                               pSigningCertKey, hAddCerts, fTimestampTypeOld, SigningTime, pTimestampCertKey);
     1830
     1831    /* probably need to clean up stuff related to nested signatures here later... */
    17891832    return rcExit;
    17901833}
     
    25282571                            rcExit2 = SignToolPkcs7Exe_WriteSignatureToFile(&Exe, cVerbosity);
    25292572                        SignToolPkcs7Exe_Delete(&Exe);
     2573                    }
     2574                    if (rcExit2 != RTEXITCODE_SUCCESS && rcExit == RTEXITCODE_SUCCESS)
     2575                        rcExit = rcExit2;
     2576                    rcExit2 = RTEXITCODE_SUCCESS;
     2577                }
     2578                else
     2579                {
     2580                    if (!TimestampCertKey.pCertificate)
     2581                        RTMsgError("No signing certificate was specified");
     2582                    if (TimestampCertKey.hPrivateKey == NIL_RTCRKEY)
     2583                        RTMsgError("No signing private key was specified");
     2584
     2585                    if (!TimestampCertKey.pCertificate && !TimestampCertKey.isNull())
     2586                        RTMsgError("No timestamp certificate was specified");
     2587                    if (TimestampCertKey.hPrivateKey == NIL_RTCRKEY && !TimestampCertKey.isNull())
     2588                        RTMsgError("No timestamp private key was specified");
     2589                    rcExit2 = RTEXITCODE_SYNTAX;
     2590                }
     2591                break;
     2592
     2593            default:
     2594                return RTGetOptPrintError(ch, &ValueUnion);
     2595        }
     2596        if (rcExit2 != RTEXITCODE_SUCCESS)
     2597        {
     2598            rcExit = rcExit2;
     2599            break;
     2600        }
     2601    }
     2602
     2603    if (hAddCerts != NIL_RTCRSTORE)
     2604        RTCrStoreRelease(hAddCerts);
     2605    return rcExit;
     2606}
     2607
     2608#endif /*!IPRT_IN_BUILD_TOOL */
     2609
     2610
     2611/*********************************************************************************************************************************
     2612*   The 'sign-cat' command.                                                                                   *
     2613*********************************************************************************************************************************/
     2614#ifndef IPRT_IN_BUILD_TOOL
     2615
     2616static RTEXITCODE HelpSignCat(PRTSTREAM pStrm, RTSIGNTOOLHELP enmLevel)
     2617{
     2618    RT_NOREF_PV(enmLevel);
     2619
     2620    RTStrmWrappedPrintf(pStrm, RTSTRMWRAPPED_F_HANGING_INDENT,
     2621                        "sign-exe [-v|--verbose] "
     2622                        "[--type sha1|sha256] "
     2623                        "[--append] "
     2624                        "[--cert-file <file>] "
     2625                        "[--cert-key <file>] "
     2626                        "[--add-cert <file>] "
     2627                        "[--timestamp-cert-file <file>] "
     2628                        "[--timestamp-key-file <file>] "
     2629                        "[--timestamp-type old|new] "
     2630                        "[--timestamp-date <fake-isots>] "
     2631                        "[--timestamp-year <fake-year>] "
     2632                        "[--replace-existing|-r] "
     2633                        "<exe>\n");
     2634    if (enmLevel == RTSIGNTOOLHELP_FULL)
     2635        RTStrmWrappedPrintf(pStrm, 0,
     2636                            "Sign a catalog file.\n"
     2637                            "\n"
     2638                            "The --timestamp-override option can take a partial or full ISO timestamp.  It is merged "
     2639                            "with the current time if partial.\n"
     2640                            "\n");
     2641    return RTEXITCODE_SUCCESS;
     2642}
     2643
     2644
     2645static RTEXITCODE HandleSignCat(int cArgs, char **papszArgs)
     2646{
     2647    /*
     2648     * Parse arguments.
     2649     */
     2650    static const RTGETOPTDEF s_aOptions[] =
     2651    {
     2652        { "--append",               'a',                        RTGETOPT_REQ_NOTHING },
     2653        { "/as",                    'a',                        RTGETOPT_REQ_NOTHING },
     2654        { "--type",                 't',                        RTGETOPT_REQ_STRING },
     2655        { "/fd",                    't',                        RTGETOPT_REQ_STRING },
     2656        { "--add-cert",             OPT_ADD_CERT,               RTGETOPT_REQ_STRING },
     2657        { "/ac",                    OPT_ADD_CERT,               RTGETOPT_REQ_STRING },
     2658        { "--cert-file",            OPT_CERT_FILE,              RTGETOPT_REQ_STRING },
     2659        { "--key-file",             OPT_KEY_FILE,               RTGETOPT_REQ_STRING },
     2660        { "--timestamp-cert-file",  OPT_TIMESTAMP_CERT_FILE,    RTGETOPT_REQ_STRING },
     2661        { "--timestamp-key-file",   OPT_TIMESTAMP_KEY_FILE,     RTGETOPT_REQ_STRING },
     2662        { "--timestamp-type",       OPT_TIMESTAMP_TYPE,         RTGETOPT_REQ_STRING },
     2663        { "--timestamp-override",   OPT_TIMESTAMP_OVERRIDE,     RTGETOPT_REQ_STRING },
     2664        { "--verbose",              'v',                        RTGETOPT_REQ_NOTHING },
     2665        { "/v",                     'v',                        RTGETOPT_REQ_NOTHING },
     2666        { "/debug",                 'v',                        RTGETOPT_REQ_NOTHING },
     2667    };
     2668
     2669    unsigned                cVerbosity              = 0;
     2670    RTDIGESTTYPE            enmSigType              = RTDIGESTTYPE_SHA1;
     2671    bool                    fReplaceExisting        = true;
     2672    SIGNTOOLKEYPAIR         SigningCertKey;
     2673    RTCRSTORE               hAddCerts               = NIL_RTCRSTORE; /* leaked if returning directly (--help, --version) */
     2674    bool                    fTimestampTypeOld       = true;
     2675    SIGNTOOLKEYPAIR         TimestampCertKey;
     2676    RTTIMESPEC              SigningTime;
     2677    RTTimeNow(&SigningTime);
     2678
     2679    RTGETOPTSTATE   GetState;
     2680    int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
     2681    AssertRCReturn(rc, RTEXITCODE_FAILURE);
     2682
     2683    RTEXITCODE      rcExit = RTEXITCODE_SUCCESS;
     2684    RTGETOPTUNION   ValueUnion;
     2685    int             ch;
     2686    while ((ch = RTGetOpt(&GetState, &ValueUnion)))
     2687    {
     2688        RTEXITCODE rcExit2 = RTEXITCODE_SUCCESS;
     2689        switch (ch)
     2690        {
     2691            case 't':                       rcExit2 = HandleOptSignatureType(&enmSigType, ValueUnion.psz); break;
     2692            case 'a':                       fReplaceExisting = false; break;
     2693            case OPT_CERT_FILE:             rcExit2 = HandleOptCertFile(&SigningCertKey, ValueUnion.psz); break;
     2694            case OPT_KEY_FILE:              rcExit2 = HandleOptKeyFile(&SigningCertKey, ValueUnion.psz); break;
     2695            case OPT_ADD_CERT:              rcExit2 = HandleOptAddCert(&hAddCerts, ValueUnion.psz); break;
     2696            case OPT_TIMESTAMP_CERT_FILE:   rcExit2 = HandleOptCertFile(&TimestampCertKey, ValueUnion.psz); break;
     2697            case OPT_TIMESTAMP_KEY_FILE:    rcExit2 = HandleOptKeyFile(&TimestampCertKey, ValueUnion.psz); break;
     2698            case OPT_TIMESTAMP_TYPE:        rcExit2 = HandleOptTimestampType(&fTimestampTypeOld, ValueUnion.psz); break;
     2699            case OPT_TIMESTAMP_OVERRIDE:    rcExit2 = HandleOptTimestampOverride(&SigningTime, ValueUnion.psz); break;
     2700            case 'v':                       cVerbosity++; break;
     2701            case 'V':                       return HandleVersion(cArgs, papszArgs);
     2702            case 'h':                       return HelpSignExe(g_pStdOut, RTSIGNTOOLHELP_FULL);
     2703
     2704            case VINF_GETOPT_NOT_OPTION:
     2705                /* check that we've got all the info we need: */
     2706                if (   SigningCertKey.isComplete()
     2707                    && (TimestampCertKey.isNull() || TimestampCertKey.isComplete()))
     2708                {
     2709                    /* Do the work: */
     2710                    SIGNTOOLPKCS7 Cat;
     2711                    rcExit2 = SignToolPkcs7_InitFromFile(&Cat, ValueUnion.psz, cVerbosity);
     2712                    if (rcExit2 == RTEXITCODE_SUCCESS)
     2713                    {
     2714                        rcExit2 = SignToolPkcs7_AddOrReplaceCatSignature(&Cat, cVerbosity, enmSigType, fReplaceExisting,
     2715                                                                         &SigningCertKey, hAddCerts,
     2716                                                                         fTimestampTypeOld, SigningTime, &TimestampCertKey);
     2717                        if (rcExit2 == RTEXITCODE_SUCCESS)
     2718                            rcExit2 = SignToolPkcs7_Encode(&Cat, cVerbosity);
     2719                        if (rcExit2 == RTEXITCODE_SUCCESS)
     2720                            rcExit2 = SignToolPkcs7_WriteSignatureToFile(&Cat, ValueUnion.psz, cVerbosity);
     2721                        SignToolPkcs7_Delete(&Cat);
    25302722                    }
    25312723                    if (rcExit2 != RTEXITCODE_SUCCESS && rcExit == RTEXITCODE_SUCCESS)
     
    36173809    }
    36183810    else
     3811    {
    36193812        HandleShowExeWorkerDisplayObjId(pThis, &pSignedData->ContentInfo.ContentType, "     ContentType: ", " - not implemented.\n");
     3813        RTPrintf("%s                  %u (%#x) bytes\n", pThis->szPrefix,
     3814                 pSignedData->ContentInfo.Content.Asn1Core.cb, pSignedData->ContentInfo.Content.Asn1Core.cb);
     3815    }
    36203816
    36213817    /*
     
    41234319    { "add-timestamp-exe-signature",    HandleAddTimestampExeSignature,     HelpAddTimestampExeSignature },
    41244320    { "sign-exe",                       HandleSignExe,                      HelpSignExe  },
     4321    { "sign-cat",                       HandleSignCat,                      HelpSignCat  },
    41254322#endif
    41264323#ifndef IPRT_IN_BUILD_TOOL
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