VirtualBox

Changeset 84145 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
May 5, 2020 11:28:24 AM (5 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
137741
Message:

VBoxManage,manual: Adjusting the signova parameter parsing. bugref:9699

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxManage/VBoxManageAppliance.cpp

    r84047 r84145  
    17901790RTEXITCODE handleSignAppliance(HandlerArg *arg)
    17911791{
    1792     HRESULT hrc = S_OK;
    1793 
     1792    /*
     1793     * Parse arguments.
     1794     */
    17941795    static const RTGETOPTDEF s_aOptions[] =
    17951796    {
    1796         { "--private-key-form",         'k', RTGETOPT_REQ_STRING },
     1797        { "--certificate",              'c', RTGETOPT_REQ_STRING },
     1798        { "--private-key",              'k', RTGETOPT_REQ_STRING },
    17971799        { "--private-key-password",     'p', RTGETOPT_REQ_STRING },
    1798         { "--private-key-password-file",'f', RTGETOPT_REQ_STRING },
    1799         { "--cert-file",                'c', RTGETOPT_REQ_STRING },
     1800        { "--private-key-password-file",'P', RTGETOPT_REQ_STRING },
     1801        { "--pkcs7",                    '7', RTGETOPT_REQ_NOTHING },
     1802        { "--no-pkcs7",                 'n', RTGETOPT_REQ_NOTHING },
    18001803        { "--intermediate-cert-file",   'i', RTGETOPT_REQ_STRING },
    1801         { "--out-cert",                 'o', RTGETOPT_REQ_NOTHING },
    1802         { "--pkcs7",                    's', RTGETOPT_REQ_NOTHING },
    1803         { "--no-pkcs7",                 'S', RTGETOPT_REQ_NOTHING },
    1804         { "--force",                    'F', RTGETOPT_REQ_NOTHING },
    1805         { "--dry-run",                  'd', RTGETOPT_REQ_NOTHING },
    1806         { "help",                       1001, RTGETOPT_REQ_NOTHING },
    1807         { "--help",                     1002, RTGETOPT_REQ_NOTHING }
     1804        { "--force",                    'f', RTGETOPT_REQ_NOTHING },
     1805        { "--out-cert",                 'O', RTGETOPT_REQ_NOTHING },
     1806        { "--dry-run",                  'D', RTGETOPT_REQ_NOTHING },
    18081807    };
    18091808
    18101809    RTGETOPTSTATE GetState;
    1811     RTGETOPTUNION ValueUnion;
    1812 
    18131810    int rc = RTGetOptInit(&GetState, arg->argc, arg->argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0);
    18141811    AssertRCReturn(rc, RTEXITCODE_FAILURE);
    1815     if (arg->argc == 1)
    1816     {
    1817         RTPrintf("Empty command parameter list, show help.\n");
    1818         printHelp(g_pStdOut);
    1819         return RTEXITCODE_SUCCESS;
    1820     }
    1821 
    1822     Utf8Str strOvfFilename;
    1823     Utf8Str strPrivateKeyForm;
    1824     Utf8Str strPrivateKeyPassword;
    1825     Utf8Str strPrivateKeyPasswordFile;
    1826     Utf8Str strX509CertificateFile;
    1827     Utf8Str strInterimCertificateFile;
    1828     bool    fOutCert = false; // the default
    1829     bool    fPKCS7 = false; // the default
    1830     bool    fResign = false; // the default
    1831     bool    fDry = false; // the default
    1832     com::SafeArray<BSTR>  parameters;
     1812
     1813    const char *pszOva              = NULL;
     1814    const char *pszCertificate      = NULL;
     1815    const char *pszPrivateKey       = NULL;
     1816    Utf8Str     strPrivateKeyPassword;
     1817    bool        fPkcs7              = false;
     1818    unsigned    cIntermediateCerts  = 0;
     1819    const char *apszIntermediateCerts[32];
     1820    bool        fReSign             = false;
     1821
     1822    bool        fOutCert            = false;
     1823    bool        fDryRun             = false;
    18331824
    18341825    int c;
     1826    RTGETOPTUNION ValueUnion;
    18351827    while ((c = RTGetOpt(&GetState, &ValueUnion)) != 0)
    18361828    {
    18371829        switch (c)
    18381830        {
     1831            case 'c':
     1832                pszCertificate = ValueUnion.psz;
     1833                break;
     1834
    18391835            case 'k':
    1840                 strPrivateKeyForm=ValueUnion.psz;
     1836                pszPrivateKey = ValueUnion.psz;
    18411837                break;
    18421838
    18431839            case 'p':
    1844                 strPrivateKeyPassword=ValueUnion.psz;
     1840                if (strPrivateKeyPassword.isNotEmpty())
     1841                    RTMsgWarning("Password is given more than once.");
     1842                strPrivateKeyPassword = ValueUnion.psz;
     1843                break;
     1844
     1845            case 'P':
     1846            {
     1847                if (strPrivateKeyPassword.isNotEmpty())
     1848                    RTMsgWarning("Password is given more than once.");
     1849                RTEXITCODE rcExit = readPasswordFile(ValueUnion.psz, &strPrivateKeyPassword);
     1850                if (rcExit == RTEXITCODE_SUCCESS)
     1851                    break;
     1852                return rcExit;
     1853            }
     1854
     1855            case '7':
     1856                fPkcs7 = true;
     1857                break;
     1858
     1859            case 'n':
     1860                fPkcs7 = false;
     1861                break;
     1862
     1863            case 'i':
     1864                if (cIntermediateCerts >= RT_ELEMENTS(apszIntermediateCerts))
     1865                    return RTMsgErrorExitFailure("Too many intermediate certificates: max %zu",
     1866                                                 RT_ELEMENTS(apszIntermediateCerts));
     1867                apszIntermediateCerts[cIntermediateCerts++] = ValueUnion.psz;
    18451868                break;
    18461869
    18471870            case 'f':
    1848                 strPrivateKeyPasswordFile=ValueUnion.psz;
    1849                 break;
    1850 
    1851             case 'c':
    1852                 strX509CertificateFile=ValueUnion.psz;
    1853                 break;
    1854 
    1855             case 'i':
    1856                 strInterimCertificateFile=ValueUnion.psz;
    1857                 break;
    1858 
    1859             case 'o':
     1871                fReSign = true;
     1872                break;
     1873
     1874
     1875            case 'O':
    18601876                fOutCert = true;
    18611877                break;
    18621878
    1863             case 's':
    1864                 fPKCS7 = true;
    1865                 break;
    1866 
    1867             case 'F':
    1868                 fResign = true;
    1869                 break;
    1870 
    1871             case 'd':
    1872                 fDry = true;
    1873                 break;
    1874 
    1875             case 1001:
    1876             case 1002:
    1877                 printHelp(g_pStdOut);
    1878                 return RTEXITCODE_SUCCESS;
     1879            case 'D':
     1880                fDryRun = true;
     1881                break;
    18791882
    18801883            case VINF_GETOPT_NOT_OPTION:
    1881                 if (strOvfFilename.isEmpty())
    1882                     strOvfFilename = ValueUnion.psz;
    1883                 else
    1884                     return errorGetOpt(c, &ValueUnion);
    1885                 break;
    1886 
     1884                if (!pszOva)
     1885                {
     1886                    pszOva = ValueUnion.psz;
     1887                    break;
     1888                }
     1889                RT_FALL_THRU();
    18871890            default:
    18881891                return errorGetOpt(c, &ValueUnion);
     
    18901893    }
    18911894
     1895    /* Required paramaters: */
     1896    if (!pszOva || !*pszOva)
     1897        return RTMsgErrorExit(RTEXITCODE_FAILURE, "No OVA file was specified!");
     1898    if (!pszCertificate || !*pszCertificate)
     1899        return RTMsgErrorExit(RTEXITCODE_FAILURE, "No signing certificate (--certificate=<file>) was specified!");
     1900    if (!pszPrivateKey || !*pszPrivateKey)
     1901        return RTMsgErrorExit(RTEXITCODE_FAILURE, "No signing private key (--private-key=<file>) was specified!");
     1902
     1903    /* Check that input files exists before we commence: */
     1904    if (!RTFileExists(pszOva))
     1905        return RTMsgErrorExit(RTEXITCODE_FAILURE, "The specified OVA file was not found: %s", pszOva);
     1906    if (!RTFileExists(pszCertificate))
     1907        return RTMsgErrorExit(RTEXITCODE_FAILURE, "The specified certificate file was not found: %s", pszCertificate);
     1908    if (!RTFileExists(pszPrivateKey))
     1909        return RTMsgErrorExit(RTEXITCODE_FAILURE, "The specified private key file was not found: %s", pszPrivateKey);
     1910
     1911    /*
     1912     *
     1913     */
     1914    HRESULT hrc = S_OK;
     1915
    18921916    Utf8Str strManifestData;
    18931917    Utf8Str strManifestName;
     
    18951919    Utf8Str strApplianceFullPath;
    18961920
    1897     if (strOvfFilename.isEmpty())
    1898         return RTMsgErrorExit(RTEXITCODE_FAILURE, "The OVA package name is empty");
     1921    char *pszAbsFilePath = RTPathAbsDup(pszOva);
    18991922
    19001923    do
    19011924    {
    1902         char *pszAbsFilePath = RTPathAbsDup(strOvfFilename.c_str());
    19031925
    19041926        if (!RTFileExists(pszAbsFilePath))
     
    19461968
    19471969    /* Read the private key */
    1948     RTCRKEY hPrivateKey;
    1949     RTERRINFO ErrInfo;
    1950     uint32_t fFlags = 0;
    1951 
    1952     if (strPrivateKeyForm.equalsIgnoreCase("pem"))
    1953         fFlags = RTCRPEMREADFILE_F_VALID_MASK;//|RTCRKEYFROM_F_VALID_MASK;
    1954 
    1955     /* check the key file existence */
    1956     if (!RTFileExists(strPrivateKeyPasswordFile.c_str()))
    1957         return RTMsgErrorExit(RTEXITCODE_FAILURE, "The file %s with a private key wasn't found",
    1958                               strPrivateKeyPasswordFile.c_str());
    1959 
    1960     rc = RTCrKeyCreateFromFile(&hPrivateKey, 0, strPrivateKeyPasswordFile.c_str(), strPrivateKeyPassword.c_str(), &ErrInfo);
     1970    RTERRINFOSTATIC ErrInfo;
     1971    RTCRKEY         hPrivateKey = NIL_RTCRKEY;
     1972    rc = RTCrKeyCreateFromFile(&hPrivateKey, RTCRPEMREADFILE_F_SENSITIVE, pszPrivateKey,
     1973                               strPrivateKeyPassword.c_str(), RTErrInfoInitStatic(&ErrInfo));
    19611974    if (RT_SUCCESS(rc))
    19621975    {
    1963         RTPrintf("Reading the private key from %s was done.\n\n", strPrivateKeyPasswordFile.c_str());
    1964 
    1965         /* check the certificate file existence */
    1966         if (!RTFileExists(strX509CertificateFile.c_str()))
    1967         {
    1968             RTCrKeyRelease(hPrivateKey);
    1969             return RTMsgErrorExit(RTEXITCODE_FAILURE, "The file %s with a X509 certificate wasn't found",
    1970                                   strX509CertificateFile.c_str());
    1971         }
     1976        RTPrintf("Reading the private key from %s was done.\n\n", pszPrivateKey);
    19721977
    19731978        /* Read the certificate */
    19741979        RTCRX509CERTIFICATE     Certificate;
    1975         rc = RTCrX509Certificate_ReadFromFile(&Certificate, strX509CertificateFile.c_str(), 0, &g_RTAsn1DefaultAllocator,
    1976                                               &ErrInfo);
     1980        rc = RTCrX509Certificate_ReadFromFile(&Certificate, pszCertificate, 0, &g_RTAsn1DefaultAllocator,
     1981                                              RTErrInfoInitStatic(&ErrInfo));
    19771982        if (RT_FAILURE(rc))
    19781983        {
    19791984            RTCrKeyRelease(hPrivateKey);
    1980             return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error reading certificate from %s: %Rrc - %s",
    1981                                   strX509CertificateFile.c_str(), rc, ErrInfo.pszMsg);
     1985            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error reading certificate from %s: %Rrc%#RTeim",
     1986                                  pszCertificate, rc, &ErrInfo.Core);
    19821987        }
    19831988
    1984         RTPrintf("Reading the certificate from %s was done.\n\n", strX509CertificateFile.c_str());
     1989        RTPrintf("Reading the certificate from %s was done.\n\n", pszCertificate);
    19851990
    19861991        /*
     
    20262031                                              signatureBuf,
    20272032                                              &cbSignature,
    2028                                               &ErrInfo);
     2033                                              RTErrInfoInitStatic(&ErrInfo));
    20292034                if (RT_SUCCESS(rc))
    20302035                {
     
    20342039                    /* Verify the signature back using the public key information from the certificate */
    20352040                    rc = RTCrPkixPubKeyVerifySignedDigestByCertPubKeyInfo(&Certificate.TbsCertificate.SubjectPublicKeyInfo,
    2036                                                                           signatureBuf, cbSignature, hDigest, &ErrInfo);
     2041                                                                          signatureBuf, cbSignature, hDigest,
     2042                                                                          RTErrInfoInitStatic(&ErrInfo));
    20372043                    if (RT_FAILURE(rc))
    20382044                    {
     
    20432049                            return RTMsgErrorExit(RTEXITCODE_FAILURE, "The manifest signature does not match");
    20442050
    2045                         return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error validating the manifest signature (%Rrc, %s)",
    2046                                               rc, ErrInfo.pszMsg);
     2051                        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error validating the manifest signature (%Rrc%#RTeim)",
     2052                                              rc, &ErrInfo.Core);
    20472053                    }
    2048                     else
    2049                         RTPrintf("The manifest signature was validated successfully\n\n");
     2054                    RTPrintf("The manifest signature was validated successfully\n\n");
    20502055
    20512056                    /*
     
    20782083
    20792084                    /* Just stop here in the case of dry-run scenario */
    2080                     if (fDry)
     2085                    if (fDryRun)
    20812086                    {
    20822087                        /* Dont' forget */
     
    21052110                            /* Open and read the passed certificate file as a standard file */
    21062111                            RTVFSFILE     hVfsOriginalX509Certificate;
    2107                             rc = RTVfsFileOpenNormal(strX509CertificateFile.c_str(),
    2108                                                      RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE,
     2112                            rc = RTVfsFileOpenNormal(pszCertificate, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE,
    21092113                                                     &hVfsOriginalX509Certificate);
    21102114                            if (RT_SUCCESS(rc))
     
    21272131                            }
    21282132                            else
    2129                                 RTPrintf("Reading the certificate from the file %s failed (%Rrc)",
    2130                                          strX509CertificateFile.c_str(), rc);
     2133                                RTPrintf("Reading the certificate from the file %s failed (%Rrc)", pszCertificate, rc);
    21312134
    21322135                            /* Dont' forget */
     
    21442147                            }
    21452148                            else
    2146                                 RTPrintf("Reading the certificate from the file %s failed (%Rrc)",
    2147                                          strX509CertificateFile.c_str(), rc);
     2149                                RTPrintf("Reading the certificate from the file %s failed (%Rrc)", pszCertificate, rc);
    21482150                        }
    21492151                        else
     
    22312233                                            fCertPresence = true;//remember for later usage
    22322234                                            /* if the flag --force has been set just skip it and go further */
    2233                                             if (fResign)
     2235                                            if (fReSign)
    22342236                                                continue;
    22352237                                        }
     
    22662268                            if (RT_SUCCESS(rc))
    22672269                            {
    2268                                 /* Add only if no cetificate or the flag fResign was set and certificate is presented */
    2269                                 if ( !fCertPresence || (fCertPresence && fResign) )
     2270                                /* Add only if no cetificate or the flag fReSign was set and certificate is presented */
     2271                                if ( !fCertPresence || (fCertPresence && fReSign) )
    22702272                                {
    22712273                                    size_t cbWritten;
     
    23672369    }
    23682370    else
    2369         RTPrintf("Error reading the private key from %s: %Rrc - %s", strPrivateKeyPasswordFile.c_str(), rc, ErrInfo.pszMsg);
     2371        RTPrintf("Error reading the private key from %s: %Rrc%#RTeim", pszPrivateKey, rc, &ErrInfo.Core);
    23702372
    23712373    /* Dont' forget */
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