Skip to content

Commit

Permalink
Windows: Improve CredWrite() error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ckamm committed Feb 4, 2019
1 parent fa90685 commit 22236d6
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion keychain_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using namespace QKeychain;

#if defined(USE_CREDENTIAL_STORE)
#include <wincred.h>
#include <comdef.h>

void ReadPasswordJobPrivate::scheduledStart() {
LPCWSTR name = (LPCWSTR)key.utf16();
Expand Down Expand Up @@ -60,8 +61,27 @@ void WritePasswordJobPrivate::scheduledStart() {
cred.CredentialBlob = (LPBYTE)pwd;
cred.Persist = CRED_PERSIST_ENTERPRISE;

const size_t maxBlob = CRED_MAX_CREDENTIAL_BLOB_SIZE;
if (cred.CredentialBlobSize > maxBlob) {
q->emitFinishedWithError(
OtherError,
tr("Credential size exceeds maximum size of %1").arg(maxBlob));
return;
}

const size_t maxTargetName = CRED_MAX_GENERIC_TARGET_NAME_LENGTH;
if (key.size() > maxTargetName) {
q->emitFinishedWithError(
OtherError,
tr("Credential key exceeds maximum size of %1").arg(maxTargetName));
return;
}

if (!CredWriteW(&cred, 0)) {
q->emitFinishedWithError( OtherError, tr("Encryption failed") ); //TODO more details available?
DWORD err = GetLastError();
QString msg = QString::fromUtf16(
(const ushort *)_com_error(HRESULT_FROM_WIN32(err)).ErrorMessage());
q->emitFinishedWithError( OtherError, tr("Writing credentials failed: %1").arg(msg) );
} else {
q->emitFinished();
}
Expand Down

0 comments on commit 22236d6

Please sign in to comment.