VirtualBox

Ignore:
Timestamp:
Mar 26, 2015 7:32:11 PM (10 years ago)
Author:
vboxsync
Message:

Main/Console: Add method to add multiple disk encryption passwords with a single call

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-client/ConsoleImpl.cpp

    r54809 r54976  
    34123412    else
    34133413        return setError(E_FAIL, tr("Failed to allocate secure memory for the password (%Rrc)"), rc);
     3414
     3415    return hrc;
     3416}
     3417
     3418HRESULT Console::addDiskEncryptionPasswords(const std::vector<com::Utf8Str> &aIds, const std::vector<com::Utf8Str> &aPasswords,
     3419                                            BOOL aClearOnSuspend)
     3420{
     3421    HRESULT hrc = S_OK;
     3422
     3423    if (   !aIds.size()
     3424        || !aPasswords.size())
     3425        return setError(E_FAIL, tr("IDs and passwords must not be empty"));
     3426
     3427    if (aIds.size() != aPasswords.size())
     3428        return setError(E_FAIL, tr("The number of entries in the id and password arguments must match"));
     3429
     3430    AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
     3431
     3432    /* Check that the IDs do not exist already before changing anything. */
     3433    for (unsigned i = 0; i < aIds.size(); i++)
     3434    {
     3435        SecretKeyMap::const_iterator it = m_mapSecretKeys.find(aIds[i]);
     3436        if (it != m_mapSecretKeys.end())
     3437            return setError(VBOX_E_OBJECT_IN_USE, tr("A password with the given ID already exists"));
     3438    }
     3439
     3440    for (unsigned i = 0; i < aIds.size(); i++)
     3441    {
     3442        size_t cbKey = aPasswords[i].length() + 1; /* Include terminator */
     3443        uint8_t *pbKey = NULL;
     3444        int rc = RTMemSaferAllocZEx((void **)&pbKey, cbKey, RTMEMSAFER_F_REQUIRE_NOT_PAGABLE);
     3445        if (RT_SUCCESS(rc))
     3446        {
     3447            memcpy(pbKey, aPasswords[i].c_str(), cbKey);
     3448
     3449            /* Scramble content to make retrieving the key more difficult. */
     3450            rc = RTMemSaferScramble(pbKey, cbKey);
     3451            AssertRC(rc);
     3452            SecretKey *pKey = new SecretKey(pbKey, cbKey, !!aClearOnSuspend);
     3453            /* Add the key to the map */
     3454            m_mapSecretKeys.insert(std::make_pair(aIds[i], pKey));
     3455            hrc = i_configureEncryptionForDisk(aIds[i]);
     3456            if (FAILED(hrc))
     3457                m_mapSecretKeys.erase(aIds[i]);
     3458        }
     3459        else
     3460            hrc = setError(E_FAIL, tr("Failed to allocate secure memory for the password (%Rrc)"), rc);
     3461
     3462        if (FAILED(hrc))
     3463        {
     3464            /*
     3465             * Try to remove already successfully added passwords from the map to not
     3466             * change the state of the Console object.
     3467             */
     3468            for (unsigned ii = 0; ii < i; ii++)
     3469                removeDiskEncryptionPassword(aIds[ii]);
     3470
     3471            break;
     3472        }
     3473    }
     3474
     3475    if (   SUCCEEDED(hrc)
     3476        && m_mapSecretKeys.size() == m_cDisksEncrypted
     3477        && mMachineState == MachineState_Paused)
     3478    {
     3479        /* get the VM handle. */
     3480        SafeVMPtr ptrVM(this);
     3481        if (!ptrVM.isOk())
     3482            return ptrVM.rc();
     3483
     3484        alock.release();
     3485        int vrc = VMR3Resume(ptrVM.rawUVM(), VMRESUMEREASON_RECONFIG);
     3486
     3487        hrc = RT_SUCCESS(vrc) ? S_OK :
     3488                setError(VBOX_E_VM_ERROR,
     3489                         tr("Could not resume the machine execution (%Rrc)"), vrc);
     3490    }
    34143491
    34153492    return hrc;
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