basic impl for windows
This commit is contained in:
parent
ed718884cc
commit
d6ecb94c8f
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
Keychain::Error Keychain::Private::writeEntryImpl( const QString& account,
|
||||
const QByteArray& data,
|
||||
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& key,
|
||||
const QByteArray& data_,
|
||||
QString* err ) {
|
||||
Q_ASSERT( err );
|
||||
err->clear();
|
||||
*err = tr("Not implemented");
|
||||
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;
|
||||
}
|
||||
|
||||
Keychain::Error Keychain::Private::deleteEntryImpl( const QString& account,
|
||||
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& key,
|
||||
QString* err ) {
|
||||
Q_ASSERT( err );
|
||||
err->clear();
|
||||
*err = tr("Not implemented");
|
||||
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");
|
||||
*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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue