VirtualBox

Changeset 96765 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Sep 16, 2022 11:58:36 AM (2 years ago)
Author:
vboxsync
Message:

Add/Nt/CertUtil: Pass up error when RTPathGlob fails. Added --add-if-new (default) / --force options to the -add-root and --add-trusted-publisher commands. Display different message if the certificate is already present in the store and --force wasn't specified. bugref:8691

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/WINNT/tools/VBoxCertUtil.cpp

    r96685 r96765  
    488488 * @param   dwDisposition   The disposition towards existing certificates when
    489489 *                          adding it.  CERT_STORE_ADD_NEW is a safe one.
    490  */
    491 static bool addCertToStoreByFile(DWORD dwDst, const char *pszStoreNm, const char *pszCertFile, DWORD dwDisposition)
    492 {
     490 * @param   pfAlreadyExists Where to indicate whether the certificate was
     491 *                          already present and not replaced.
     492 */
     493static bool addCertToStoreByFile(DWORD dwDst, const char *pszStoreNm, const char *pszCertFile, DWORD dwDisposition,
     494                                 bool *pfAlreadyExists)
     495{
     496    *pfAlreadyExists = false;
     497
    493498    /*
    494499     * Read the certificate file first.
     
    514519                fRc = true;
    515520            else
    516                 RTMsgError("CertAddCertificateContextToStore returned %s", errorToString(GetLastError()));
     521            {
     522                DWORD const dwErr = GetLastError();
     523                *pfAlreadyExists = fRc = dwErr == CRYPT_E_EXISTS;
     524                if (!fRc)
     525                    RTMsgError("CertAddCertificateContextToStore returned %s", errorToString(dwErr));
     526            }
    517527        }
    518528        else
     
    533543 */
    534544static RTEXITCODE addCertToStoreByFilePattern(DWORD dwDst, const char *pszStoreNm, const char *pszStoreDesc,
    535                                               const char *pszFilePattern, RTEXITCODE rcExit, uint32_t *pcImports)
     545                                              const char *pszFilePattern, bool fForce, RTEXITCODE rcExit, uint32_t *pcImports)
    536546{
    537547    PCRTPATHGLOBENTRY pResultHead;
     
    541551        for (PCRTPATHGLOBENTRY pCur = pResultHead; pCur; pCur = pCur->pNext)
    542552        {
    543             if (addCertToStoreByFile(dwDst, pszStoreNm, pCur->szPath, CERT_STORE_ADD_NEW))
    544                 RTMsgInfo("Successfully added '%s' as %s", pCur->szPath, pszStoreDesc);
     553            bool fAlreadyExists = false;
     554            if (addCertToStoreByFile(dwDst, pszStoreNm, pCur->szPath,
     555                                     !fForce ? CERT_STORE_ADD_NEW : CERT_STORE_ADD_REPLACE_EXISTING,
     556                                     &fAlreadyExists))
     557            {
     558                if (!fAlreadyExists)
     559                    RTMsgInfo("Successfully added '%s' to the %s store", pCur->szPath, pszStoreDesc);
     560                else
     561                    RTMsgInfo("Certificate '%s' is already present in the %s store and was not re-added or updated.",
     562                              pCur->szPath, pszStoreNm);
     563            }
    545564            else
    546565                rcExit = RTEXITCODE_FAILURE;
     
    551570    else
    552571    {
    553         rcExit = RTMsgErrorExit(RTEXITCODE_SUCCESS, "glob failed on '%s': %Rrc", pszFilePattern, rc);
     572        rcExit = RTMsgErrorExitFailure("glob failed on '%s': %Rrc", pszFilePattern, rc);
    554573        *pcImports += 1;
    555574    }
     
    678697
    679698            case 'h':
    680                 RTPrintf("Usage: VBoxCertUtil display-all [-v|--verbose] [-q|--quiet]\n");
     699                RTStrmWrappedPrintf(g_pStdOut, RTSTRMWRAPPED_F_HANGING_INDENT,
     700                                    "Usage: VBoxCertUtil display-all [-v|--verbose] [-q|--quiet]\n");
    681701                return RTEXITCODE_SUCCESS;
    682702
     
    723743
    724744            case 'h':
    725                 RTPrintf("Usage: VBoxCertUtil root-exists <full-subject-name> [alternative-subject-name [...]]\n"
    726                          "\n"
    727                          "Exit code: 10 if not found, 0 if found.\n"
    728                          "\n"
    729                          "The names are on the form 'C=US; O=Company; OU=some unit; CN=a cert name'\n"
    730                          "where semi-colon is the X.500 attribute separator and spaces surrounding it\n"
    731                          "the type (CN, OU, ) and '=' are generally ignored.\n"
    732                          "\n"
    733                          "At verbosity level 2, the full subject name of each certificate in the store\n"
    734                          "will be listed as the search progresses.  These can be used as search input.\n"
    735                          );
     745                RTStrmWrappedPrintf(g_pStdOut, RTSTRMWRAPPED_F_HANGING_INDENT,
     746                                    "Usage: VBoxCertUtil root-exists <full-subject-name> [alternative-subject-name [...]]\n");
     747                RTStrmWrappedPrintf(g_pStdOut, 0,
     748                                    "\n"
     749                                    "Exit code: 10 if not found, 0 if found.\n"
     750                                    "\n"
     751                                    "The names are on the form 'C=US; O=Company; OU=some unit; CN=a cert name' "
     752                                    "where semi-colon is the X.500 attribute separator and spaces surrounding it "
     753                                    "the type (CN, OU, ) and '=' are generally ignored.\n"
     754                                    "\n"
     755                                    "At verbosity level 2, the full subject name of each certificate in the store "
     756                                    "will be listed as the search progresses.  These can be used as search input.\n"
     757                                    );
    736758                return RTEXITCODE_SUCCESS;
    737759
     
    781803
    782804            case 'h':
    783                 RTPrintf("Usage: VBoxCertUtil remove-root <root-cert-file>\n");
     805                RTStrmWrappedPrintf(g_pStdOut, RTSTRMWRAPPED_F_HANGING_INDENT,
     806                                    "Usage: VBoxCertUtil remove-root <root-cert-file>\n");
    784807                return RTEXITCODE_SUCCESS;
    785808
     
    827850
    828851            case 'h':
    829                 RTPrintf("Usage: VBoxCertUtil remove-trusted-publisher [--root <root-cert>] <trusted-cert>\n");
     852                RTStrmWrappedPrintf(g_pStdOut, RTSTRMWRAPPED_F_HANGING_INDENT,
     853                                    "Usage: VBoxCertUtil remove-trusted-publisher [--root <root-cert>] <trusted-cert>\n");
    830854                return RTEXITCODE_SUCCESS;
    831855
     
    861885    static const RTGETOPTDEF s_aOptions[] =
    862886    {
     887        { "--add-if-new",   'a',    RTGETOPT_REQ_NOTHING },
     888        { "--force",        'f',    RTGETOPT_REQ_NOTHING },
    863889        VCU_COMMON_OPTION_DEFINITIONS(),
    864890    };
    865891
    866     RTEXITCODE          rcExit = RTEXITCODE_SUCCESS;
    867     unsigned            cImports = 0;
     892    RTEXITCODE          rcExit    = RTEXITCODE_SUCCESS;
     893    unsigned            cImports  = 0;
     894    bool                fForce    = false;
    868895    RTGETOPTUNION       ValueUnion;
    869896    RTGETOPTSTATE       GetState;
     
    876903            VCU_COMMON_OPTION_HANDLING();
    877904
     905            case 'a':
     906                fForce = false;
     907                break;
     908
     909            case 'f':
     910                fForce = false;
     911                break;
     912
    878913            case 'h':
    879                 RTPrintf("Usage: VBoxCertUtil add-root <root-cert>\n");
     914                RTStrmWrappedPrintf(g_pStdOut, RTSTRMWRAPPED_F_HANGING_INDENT,
     915                                    "Usage: VBoxCertUtil add-root [--force|--add-if-new] <root-cert>\n");
    880916                return RTEXITCODE_SUCCESS;
    881917
    882918            case VINF_GETOPT_NOT_OPTION:
    883919                rcExit = addCertToStoreByFilePattern(CERT_SYSTEM_STORE_LOCAL_MACHINE, "Root", "root",
    884                                                      ValueUnion.psz, rcExit, &cImports);
     920                                                     ValueUnion.psz, fForce, rcExit, &cImports);
    885921                break;
    886922
     
    905941    static const RTGETOPTDEF s_aOptions[] =
    906942    {
    907         { "--root",     'r',    RTGETOPT_REQ_STRING },
     943        { "--root",         'r',    RTGETOPT_REQ_STRING },
     944        { "--add-if-new",   'a',    RTGETOPT_REQ_NOTHING },
     945        { "--force",        'f',    RTGETOPT_REQ_NOTHING },
    908946        VCU_COMMON_OPTION_DEFINITIONS(),
    909947    };
    910948
    911     RTEXITCODE          rcExit = RTEXITCODE_SUCCESS;
    912     unsigned            cImports = 0;
     949    RTEXITCODE          rcExit    = RTEXITCODE_SUCCESS;
     950    bool                fForce    = false;
     951    unsigned            cImports  = 0;
    913952    RTGETOPTUNION       ValueUnion;
    914953    RTGETOPTSTATE       GetState;
     
    921960            VCU_COMMON_OPTION_HANDLING();
    922961
     962            case 'a':
     963                fForce = false;
     964                break;
     965
     966            case 'f':
     967                fForce = false;
     968                break;
     969
    923970            case 'h':
    924                 RTPrintf("Usage: VBoxCertUtil add-trusted-publisher [--root <root-cert>] <trusted-cert>\n");
     971                RTStrmWrappedPrintf(g_pStdOut, RTSTRMWRAPPED_F_HANGING_INDENT,
     972                                    "Usage: VBoxCertUtil add-trusted-publisher [--force|--add-if-new] "
     973                                    "[--root <root-cert>] <trusted-cert>\n");
    925974                return RTEXITCODE_SUCCESS;
    926975
    927976            case 'r':
    928977                rcExit = addCertToStoreByFilePattern(CERT_SYSTEM_STORE_LOCAL_MACHINE, "Root", "root",
    929                                                      ValueUnion.psz, rcExit, &cImports);
     978                                                     ValueUnion.psz, fForce, rcExit, &cImports);
    930979                break;
    931980
    932981            case VINF_GETOPT_NOT_OPTION:
    933982                rcExit = addCertToStoreByFilePattern(CERT_SYSTEM_STORE_LOCAL_MACHINE, "TrustedPublisher", "trusted publisher",
    934                                                      ValueUnion.psz, rcExit, &cImports);
     983                                                     ValueUnion.psz, fForce, rcExit, &cImports);
    935984                break;
    936985
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