VirtualBox

Changeset 96778 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Sep 17, 2022 1:33:38 AM (2 years ago)
Author:
vboxsync
Message:

RTSignTool: Added a hash-exe command.

File:
1 edited

Legend:

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

    r96777 r96778  
    58245824
    58255825
    5826 /*
    5827  * The 'make-tainfo' command.
    5828  */
     5826/*********************************************************************************************************************************
     5827*   The 'hash-exe' command.                                                                                                      *
     5828*********************************************************************************************************************************/
     5829static RTEXITCODE HelpHashExe(PRTSTREAM pStrm, RTSIGNTOOLHELP enmLevel)
     5830{
     5831    RT_NOREF_PV(enmLevel);
     5832    RTStrmWrappedPrintf(pStrm, RTSTRMWRAPPED_F_HANGING_INDENT, "show-exe [--verbose|-v] [--quiet|-q] <exe1> [exe2 [..]]\n");
     5833    return RTEXITCODE_SUCCESS;
     5834}
     5835
     5836
     5837static RTEXITCODE HandleHashExe(int cArgs, char **papszArgs)
     5838{
     5839    /*
     5840     * Parse arguments.
     5841     */
     5842    static const RTGETOPTDEF s_aOptions[] =
     5843    {
     5844        { "--verbose",      'v', RTGETOPT_REQ_NOTHING },
     5845        { "--quiet",        'q', RTGETOPT_REQ_NOTHING },
     5846    };
     5847
     5848    unsigned  cVerbosity = 0;
     5849    RTLDRARCH enmLdrArch = RTLDRARCH_WHATEVER;
     5850
     5851    RTGETOPTSTATE GetState;
     5852    int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
     5853    AssertRCReturn(rc, RTEXITCODE_FAILURE);
     5854    RTGETOPTUNION ValueUnion;
     5855    int ch;
     5856    while ((ch = RTGetOpt(&GetState, &ValueUnion)) && ch != VINF_GETOPT_NOT_OPTION)
     5857    {
     5858        switch (ch)
     5859        {
     5860            case 'v': cVerbosity++; break;
     5861            case 'q': cVerbosity = 0; break;
     5862            case 'V': return HandleVersion(cArgs, papszArgs);
     5863            case 'h': return HelpHashExe(g_pStdOut, RTSIGNTOOLHELP_FULL);
     5864            default:  return RTGetOptPrintError(ch, &ValueUnion);
     5865        }
     5866    }
     5867    if (ch != VINF_GETOPT_NOT_OPTION)
     5868        return RTMsgErrorExit(RTEXITCODE_FAILURE, "No executable given.");
     5869
     5870    /*
     5871     * Do it.
     5872     */
     5873    unsigned   iFile  = 0;
     5874    RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
     5875    do
     5876    {
     5877        RTPrintf(iFile == 0 ? "%s:\n" : "\n%s:\n", ValueUnion.psz);
     5878
     5879        RTERRINFOSTATIC ErrInfo;
     5880        RTLDRMOD        hLdrMod;
     5881        rc = RTLdrOpenEx(ValueUnion.psz, RTLDR_O_FOR_VALIDATION, enmLdrArch, &hLdrMod, RTErrInfoInitStatic(&ErrInfo));
     5882        if (RT_SUCCESS(rc))
     5883        {
     5884            uint8_t abHash[RTSHA512_HASH_SIZE];
     5885            char    szDigest[RTSHA512_DIGEST_LEN + 1];
     5886
     5887            /* SHA-1: */
     5888            rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA1, abHash, sizeof(abHash));
     5889            if (RT_SUCCESS(rc))
     5890                RTSha1ToString(abHash, szDigest, sizeof(szDigest));
     5891            else
     5892                RTStrPrintf(szDigest, sizeof(szDigest), "%Rrc", rc);
     5893            RTPrintf("  SHA-1:   %s\n", szDigest);
     5894
     5895            /* SHA-256: */
     5896            rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA256, abHash, sizeof(abHash));
     5897            if (RT_SUCCESS(rc))
     5898                RTSha256ToString(abHash, szDigest, sizeof(szDigest));
     5899            else
     5900                RTStrPrintf(szDigest, sizeof(szDigest), "%Rrc", rc);
     5901            RTPrintf("  SHA-256: %s\n", szDigest);
     5902
     5903            /* SHA-512: */
     5904            rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA512, abHash, sizeof(abHash));
     5905            if (RT_SUCCESS(rc))
     5906                RTSha512ToString(abHash, szDigest, sizeof(szDigest));
     5907            else
     5908                RTStrPrintf(szDigest, sizeof(szDigest), "%Rrc", rc);
     5909            RTPrintf("  SHA-512: %s\n", szDigest);
     5910
     5911            RTLdrClose(hLdrMod);
     5912        }
     5913        else
     5914            rcExit = RTMsgErrorExitFailure("Failed to open '%s': %Rrc%#RTeim", ValueUnion.psz, rc, &ErrInfo.Core);
     5915
     5916    } while ((ch = RTGetOpt(&GetState, &ValueUnion)) == VINF_GETOPT_NOT_OPTION);
     5917    if (ch != 0)
     5918        return RTGetOptPrintError(ch, &ValueUnion);
     5919
     5920    return rcExit;
     5921}
     5922
     5923
     5924/*********************************************************************************************************************************
     5925*   The 'make-tainfo' command.                                                                                                   *
     5926*********************************************************************************************************************************/
    58295927static RTEXITCODE HelpMakeTaInfo(PRTSTREAM pStrm, RTSIGNTOOLHELP enmLevel)
    58305928{
     
    60986196    { "show-exe",                       HandleShowExe,                      HelpShowExe },
    60996197    { "show-cat",                       HandleShowCat,                      HelpShowCat },
     6198    { "hash-exe",                       HandleHashExe,                      HelpHashExe },
    61006199    { "make-tainfo",                    HandleMakeTaInfo,                   HelpMakeTaInfo },
    61016200    { "help",                           HandleHelp,                         HelpHelp },
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