basic impl for windows

This commit is contained in:
Frank Osterfeld 2011-10-29 01:21:07 +02:00
parent ed718884cc
commit d6ecb94c8f
3 changed files with 93 additions and 14 deletions

View File

@ -17,7 +17,7 @@ namespace QKeychain {
class Keychain::Private {
Q_DECLARE_TR_FUNCTIONS(Keychain::Private)
public:
explicit Private( const QString& s ) : service( s ) {}
explicit Private( const QString& s ) : service( s ), error( NoError ) {}
Keychain::Error writeEntryImpl( const QString& account,
const QByteArray& data,

View File

@ -8,42 +8,120 @@
*****************************************************************************/
#include "keychain_p.h"
#include <QSettings>
#include <Windows.h>
#include <WinCrypt.h>
using namespace QKeychain;
Keychain::Error Keychain::Private::readEntryImpl( QByteArray* pw,
const QString& account,
const QString& key,
QString* err ) {
Q_ASSERT( pw );
Q_ASSERT( err );
err->clear();
*err = tr("Not implemented");
return OtherError;
QSettings settings( service );
QByteArray encrypted = settings.value( key ).toByteArray();
if ( encrypted.isNull() ) {
*err = tr("Entry not found");
return EntryNotFound;
}
DATA_BLOB blob_in, blob_out;
blob_in.pbData = reinterpret_cast<BYTE*>( encrypted.data() );
blob_in.cbData = encrypted.size();
const BOOL ret = CryptUnprotectData( &blob_in,
NULL,
NULL,
NULL,
NULL,
0,
&blob_out );
if ( !ret ) {
*err = tr("Could not decrypt data");
return OtherError;
}
*pw = QByteArray( reinterpret_cast<char*>( blob_out.pbData ), blob_out.cbData );
SecureZeroMemory( blob_out.pbData, blob_out.cbData );
LocalFree( blob_out.pbData );
return NoError;
}
Keychain::Error Keychain::Private::writeEntryImpl( const QString& account,
const QByteArray& data,
Keychain::Error Keychain::Private::writeEntryImpl( const QString& key,
const QByteArray& data_,
QString* err ) {
Q_ASSERT( err );
err->clear();
*err = tr("Not implemented");
return OtherError;
QByteArray data = data_;
DATA_BLOB blob_in, blob_out;
blob_in.pbData = reinterpret_cast<BYTE*>( data.data() );
blob_in.cbData = data.size();
const BOOL res = CryptProtectData( &blob_in,
L"QKeychain-encrypted data",
NULL,
NULL,
NULL,
0,
&blob_out );
if ( !res ) {
*err = tr("Encryption failed"); //TODO more details available?
return OtherError;
}
const QByteArray encrypted( reinterpret_cast<char*>( blob_out.pbData ), blob_out.cbData );
LocalFree( blob_out.pbData );
QSettings settings( service );
settings.setValue( key, encrypted );
settings.sync();
if ( settings.status() != QSettings::NoError ) {
*err = settings.status() == QSettings::AccessError
? tr("Could not store encrypted data in settings: access error")
: tr("Could not store encrypted data in settings: format error");
return OtherError;
}
return NoError;
}
Keychain::Error Keychain::Private::deleteEntryImpl( const QString& account,
Keychain::Error Keychain::Private::deleteEntryImpl( const QString& key,
QString* err ) {
Q_ASSERT( err );
err->clear();
*err = tr("Not implemented");
return OtherError;
QSettings settings( service );
settings.remove( key );
settings.sync();
if ( settings.status() != QSettings::NoError ) {
*err = settings.status() == QSettings::AccessError
? tr("Could not delete encrypted data from settings: access error")
: tr("Could not delete encrypted data from settings: format error");
return OtherError;
}
return NoError;
}
Keychain::Error Keychain::Private::entryExistsImpl( bool* exists,
const QString& account,
const QString& key,
QString* err ) {
Q_ASSERT( exists );
Q_ASSERT( err );
err->clear();
*err = tr("Not implemented");
return OtherError;
*exists = false;
QSettings settings( service );
const bool ex = settings.contains( key );
if ( settings.status() != QSettings::NoError ) {
*err = settings.status() == QSettings::AccessError
? tr("Could not read settings: access error")
: tr("Could not read settings: format error");
return OtherError;
}
*exists = ex;
return NoError;
}

View File

@ -16,5 +16,6 @@ macx {
win32 {
DESTDIR = lib
DLLDESTDIR = lib
LIBS += -lCrypt32
SOURCES += keychain_win.cpp
}