From 84498d97e27f79e7e919b42bbe54208a02856aaf Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Sun, 6 Dec 2020 21:23:48 +0900 Subject: Renaming of files in preparation for code style tools. Signed-off-by: Michele Calgaro (cherry picked from commit 4f99f868f09bbffa2e15733b8b7c78eba07a199e) --- tdewallet/backend/CMakeLists.txt | 6 +- tdewallet/backend/Makefile.am | 12 +- tdewallet/backend/blockcipher.cc | 37 -- tdewallet/backend/blockcipher.cpp | 37 ++ tdewallet/backend/blowfish.cc | 259 ---------- tdewallet/backend/blowfish.cpp | 259 ++++++++++ tdewallet/backend/blowfishtables.h | 2 +- tdewallet/backend/cbc.cc | 149 ------ tdewallet/backend/cbc.cpp | 149 ++++++ tdewallet/backend/sha1.cc | 343 -------------- tdewallet/backend/sha1.cpp | 343 ++++++++++++++ tdewallet/backend/tdewalletbackend.cc | 838 --------------------------------- tdewallet/backend/tdewalletbackend.cpp | 838 +++++++++++++++++++++++++++++++++ tdewallet/backend/tdewalletentry.cc | 89 ---- tdewallet/backend/tdewalletentry.cpp | 89 ++++ tdewallet/backend/tests/CMakeLists.txt | 4 +- tdewallet/client/CMakeLists.txt | 2 +- tdewallet/client/Makefile.am | 2 +- tdewallet/client/tdewallet.cc | 713 ---------------------------- tdewallet/client/tdewallet.cpp | 713 ++++++++++++++++++++++++++++ 20 files changed, 2442 insertions(+), 2442 deletions(-) delete mode 100644 tdewallet/backend/blockcipher.cc create mode 100644 tdewallet/backend/blockcipher.cpp delete mode 100644 tdewallet/backend/blowfish.cc create mode 100644 tdewallet/backend/blowfish.cpp delete mode 100644 tdewallet/backend/cbc.cc create mode 100644 tdewallet/backend/cbc.cpp delete mode 100644 tdewallet/backend/sha1.cc create mode 100644 tdewallet/backend/sha1.cpp delete mode 100644 tdewallet/backend/tdewalletbackend.cc create mode 100644 tdewallet/backend/tdewalletbackend.cpp delete mode 100644 tdewallet/backend/tdewalletentry.cc create mode 100644 tdewallet/backend/tdewalletentry.cpp delete mode 100644 tdewallet/client/tdewallet.cc create mode 100644 tdewallet/client/tdewallet.cpp (limited to 'tdewallet') diff --git a/tdewallet/backend/CMakeLists.txt b/tdewallet/backend/CMakeLists.txt index 57a1e77ac..db2f66994 100644 --- a/tdewallet/backend/CMakeLists.txt +++ b/tdewallet/backend/CMakeLists.txt @@ -30,9 +30,9 @@ link_directories( set( target tdewalletbackend ) set( ${target}_SRCS - blockcipher.cc blowfish.cc - cbc.cc sha1.cc tdewalletentry.cc - tdewalletbackend.cc + blockcipher.cpp blowfish.cpp + cbc.cpp sha1.cpp tdewalletentry.cpp + tdewalletbackend.cpp ) tde_add_library( ${target} SHARED diff --git a/tdewallet/backend/Makefile.am b/tdewallet/backend/Makefile.am index 79bfc0820..0db86f3e0 100644 --- a/tdewallet/backend/Makefile.am +++ b/tdewallet/backend/Makefile.am @@ -5,12 +5,12 @@ lib_LTLIBRARIES = libtdewalletbackend.la libtdewalletbackend_la_LDFLAGS = $(KDE_RPATH) $(all_libraries) -version-info 1:0 -no-undefined libtdewalletbackend_la_LIBADD = $(LIB_QT) ../../tdecore/libtdecore.la -libtdewalletbackend_la_SOURCES = blockcipher.cc \ - blowfish.cc \ - cbc.cc \ - sha1.cc \ - tdewalletentry.cc \ - tdewalletbackend.cc +libtdewalletbackend_la_SOURCES = blockcipher.cpp \ + blowfish.cpp \ + cbc.cpp \ + sha1.cpp \ + tdewalletentry.cpp \ + tdewalletbackend.cpp libtdewalletbackend_la_METASOURCES = AUTO diff --git a/tdewallet/backend/blockcipher.cc b/tdewallet/backend/blockcipher.cc deleted file mode 100644 index 9d770098e..000000000 --- a/tdewallet/backend/blockcipher.cc +++ /dev/null @@ -1,37 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2001 George Staikos - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include "blockcipher.h" - - - -BlockCipher::BlockCipher() { - _blksz = -1; -} - - -BlockCipher::~BlockCipher() { -} - - -int BlockCipher::blockSize() const { - return _blksz; -} - - diff --git a/tdewallet/backend/blockcipher.cpp b/tdewallet/backend/blockcipher.cpp new file mode 100644 index 000000000..9d770098e --- /dev/null +++ b/tdewallet/backend/blockcipher.cpp @@ -0,0 +1,37 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "blockcipher.h" + + + +BlockCipher::BlockCipher() { + _blksz = -1; +} + + +BlockCipher::~BlockCipher() { +} + + +int BlockCipher::blockSize() const { + return _blksz; +} + + diff --git a/tdewallet/backend/blowfish.cc b/tdewallet/backend/blowfish.cc deleted file mode 100644 index c708935de..000000000 --- a/tdewallet/backend/blowfish.cc +++ /dev/null @@ -1,259 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2001 George Staikos - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - - -// FIXME: should we unroll some loops? Optimization can be done here. - - -/* Implementation of 16 rounds blowfish as described in: - * _Applied_Cryptography_ (c) Bruce Schneier, 1996. - */ - -#include - -#include -#include -#include - -#include "blowfish.h" -#include "blowfishtables.h" - - -BlowFish::BlowFish() { - _blksz = 8; - _key = 0L; - _init = false; -} - - - -bool BlowFish::init() { - // Initialize the sboxes - for (int i = 0; i < 256; i++) { - _S[0][i] = ks0[i]; - _S[1][i] = ks1[i]; - _S[2][i] = ks2[i]; - _S[3][i] = ks3[i]; - } - - uint32_t datal = 0; - uint32_t datar = 0; - uint32_t data = 0; - int j = 0; - - - // Update the sboxes and pbox. - for (int i = 0; i < 18; i++) { - data = 0; - for (int k = 0; k < 4; ++k) { - data = (data << 8) | ((unsigned char *)_key)[j++]; - if (j >= _keylen / 8) { - j = 0; - } - } - _P[i] = P[i] ^ data; - } - - for (int i = 0; i < 18; i += 2) { - encipher(&datal, &datar); - _P[i] = datal; - _P[i+1] = datar; - } - - for (int j = 0; j < 4; j++) { - for (int i = 0; i < 256; i += 2) { - encipher(&datal, &datar); - _S[j][i] = datal; - _S[j][i+1] = datar; - } - } - - // Nice code from gpg's implementation... - // check to see if the key is weak and return error if so - for (int i = 0; i < 255; i++) { - for (int j = i + 1; j < 256; j++) { - if ((_S[0][i] == _S[0][j]) || (_S[1][i] == _S[1][j]) || - (_S[2][i] == _S[2][j]) || (_S[3][i] == _S[3][j])) { - return false; - } - } - } - - _init = true; - - return true; -} - - -BlowFish::~BlowFish() { - delete[] (unsigned char *)_key; - _key = 0L; -} - - -int BlowFish::keyLen() const { - return 448; -} - - -bool BlowFish::variableKeyLen() const { - return true; -} - - -bool BlowFish::readyToGo() const { - return _init; -} - - -bool BlowFish::setKey(void *key, int bitlength) { - if (bitlength <= 0 || bitlength > 448 || bitlength % 8 != 0) { - return false; - } - - delete[] (unsigned char *)_key; - - _key = new unsigned char[bitlength / 8]; - memcpy(_key, key, bitlength / 8); - _keylen = bitlength; - - return init(); -} - - -#ifdef WORDS_BIGENDIAN -#define shuffle(x) do { \ - uint32_t r = x; \ - x = (r & 0xff000000) >> 24; \ - x |= (r & 0x00ff0000) >> 8; \ - x |= (r & 0x0000ff00) << 8; \ - x |= (r & 0x000000ff) << 24; \ - } while (0) -#endif - -int BlowFish::encrypt(void *block, int len) { - uint32_t *d = (uint32_t *)block; - - if (!_init || len % _blksz != 0) { - return -1; - } - - for (int i = 0; i < len / _blksz; i++) { -#ifdef WORDS_BIGENDIAN - shuffle(*d); - shuffle(*(d + 1)); -#endif - encipher(d, d + 1); -#ifdef WORDS_BIGENDIAN - shuffle(*d); - shuffle(*(d + 1)); -#endif - d += 2; - } - - return len; -} - - -int BlowFish::decrypt(void *block, int len) { - uint32_t *d = (uint32_t *)block; - - if (!_init || len % _blksz != 0) { - return -1; - } - - for (int i = 0; i < len / _blksz; i++) { -#ifdef WORDS_BIGENDIAN - shuffle(*d); - shuffle(*(d + 1)); -#endif - decipher(d, d + 1); -#ifdef WORDS_BIGENDIAN - shuffle(*d); - shuffle(*(d + 1)); -#endif - d += 2; - } - - return len; -} - - -uint32_t BlowFish::F(uint32_t x) { - unsigned short a, b, c, d; - uint32_t y; - - d = x & 0x000000ff; - x >>= 8; - c = x & 0x000000ff; - x >>= 8; - b = x & 0x000000ff; - x >>= 8; - a = x & 0x000000ff; - - y = _S[0][a] + _S[1][b]; - y ^= _S[2][c]; - y += _S[3][d]; - - return y; -} - - -void BlowFish::encipher(uint32_t *xl, uint32_t *xr) { - uint32_t Xl = *xl, Xr = *xr, temp; - - for (int i = 0; i < 16; ++i) { - Xl ^= _P[i]; - Xr ^= F(Xl); - // Exchange - temp = Xl; Xl = Xr; Xr = temp; - } - - // Exchange - temp = Xl; Xl = Xr; Xr = temp; - - Xr ^= _P[16]; - Xl ^= _P[17]; - - *xl = Xl; - *xr = Xr; -} - - -void BlowFish::decipher(uint32_t *xl, uint32_t *xr) { - uint32_t Xl = *xl, Xr = *xr, temp; - - for (int i = 17; i > 1; --i) { - Xl ^= _P[i]; - Xr ^= F(Xl); - // Exchange - temp = Xl; Xl = Xr; Xr = temp; - } - - // Exchange - temp = Xl; Xl = Xr; Xr = temp; - - Xr ^= _P[1]; - Xl ^= _P[0]; - - *xl = Xl; - *xr = Xr; -} - - diff --git a/tdewallet/backend/blowfish.cpp b/tdewallet/backend/blowfish.cpp new file mode 100644 index 000000000..c708935de --- /dev/null +++ b/tdewallet/backend/blowfish.cpp @@ -0,0 +1,259 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +// FIXME: should we unroll some loops? Optimization can be done here. + + +/* Implementation of 16 rounds blowfish as described in: + * _Applied_Cryptography_ (c) Bruce Schneier, 1996. + */ + +#include + +#include +#include +#include + +#include "blowfish.h" +#include "blowfishtables.h" + + +BlowFish::BlowFish() { + _blksz = 8; + _key = 0L; + _init = false; +} + + + +bool BlowFish::init() { + // Initialize the sboxes + for (int i = 0; i < 256; i++) { + _S[0][i] = ks0[i]; + _S[1][i] = ks1[i]; + _S[2][i] = ks2[i]; + _S[3][i] = ks3[i]; + } + + uint32_t datal = 0; + uint32_t datar = 0; + uint32_t data = 0; + int j = 0; + + + // Update the sboxes and pbox. + for (int i = 0; i < 18; i++) { + data = 0; + for (int k = 0; k < 4; ++k) { + data = (data << 8) | ((unsigned char *)_key)[j++]; + if (j >= _keylen / 8) { + j = 0; + } + } + _P[i] = P[i] ^ data; + } + + for (int i = 0; i < 18; i += 2) { + encipher(&datal, &datar); + _P[i] = datal; + _P[i+1] = datar; + } + + for (int j = 0; j < 4; j++) { + for (int i = 0; i < 256; i += 2) { + encipher(&datal, &datar); + _S[j][i] = datal; + _S[j][i+1] = datar; + } + } + + // Nice code from gpg's implementation... + // check to see if the key is weak and return error if so + for (int i = 0; i < 255; i++) { + for (int j = i + 1; j < 256; j++) { + if ((_S[0][i] == _S[0][j]) || (_S[1][i] == _S[1][j]) || + (_S[2][i] == _S[2][j]) || (_S[3][i] == _S[3][j])) { + return false; + } + } + } + + _init = true; + + return true; +} + + +BlowFish::~BlowFish() { + delete[] (unsigned char *)_key; + _key = 0L; +} + + +int BlowFish::keyLen() const { + return 448; +} + + +bool BlowFish::variableKeyLen() const { + return true; +} + + +bool BlowFish::readyToGo() const { + return _init; +} + + +bool BlowFish::setKey(void *key, int bitlength) { + if (bitlength <= 0 || bitlength > 448 || bitlength % 8 != 0) { + return false; + } + + delete[] (unsigned char *)_key; + + _key = new unsigned char[bitlength / 8]; + memcpy(_key, key, bitlength / 8); + _keylen = bitlength; + + return init(); +} + + +#ifdef WORDS_BIGENDIAN +#define shuffle(x) do { \ + uint32_t r = x; \ + x = (r & 0xff000000) >> 24; \ + x |= (r & 0x00ff0000) >> 8; \ + x |= (r & 0x0000ff00) << 8; \ + x |= (r & 0x000000ff) << 24; \ + } while (0) +#endif + +int BlowFish::encrypt(void *block, int len) { + uint32_t *d = (uint32_t *)block; + + if (!_init || len % _blksz != 0) { + return -1; + } + + for (int i = 0; i < len / _blksz; i++) { +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + encipher(d, d + 1); +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + d += 2; + } + + return len; +} + + +int BlowFish::decrypt(void *block, int len) { + uint32_t *d = (uint32_t *)block; + + if (!_init || len % _blksz != 0) { + return -1; + } + + for (int i = 0; i < len / _blksz; i++) { +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + decipher(d, d + 1); +#ifdef WORDS_BIGENDIAN + shuffle(*d); + shuffle(*(d + 1)); +#endif + d += 2; + } + + return len; +} + + +uint32_t BlowFish::F(uint32_t x) { + unsigned short a, b, c, d; + uint32_t y; + + d = x & 0x000000ff; + x >>= 8; + c = x & 0x000000ff; + x >>= 8; + b = x & 0x000000ff; + x >>= 8; + a = x & 0x000000ff; + + y = _S[0][a] + _S[1][b]; + y ^= _S[2][c]; + y += _S[3][d]; + + return y; +} + + +void BlowFish::encipher(uint32_t *xl, uint32_t *xr) { + uint32_t Xl = *xl, Xr = *xr, temp; + + for (int i = 0; i < 16; ++i) { + Xl ^= _P[i]; + Xr ^= F(Xl); + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + } + + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + + Xr ^= _P[16]; + Xl ^= _P[17]; + + *xl = Xl; + *xr = Xr; +} + + +void BlowFish::decipher(uint32_t *xl, uint32_t *xr) { + uint32_t Xl = *xl, Xr = *xr, temp; + + for (int i = 17; i > 1; --i) { + Xl ^= _P[i]; + Xr ^= F(Xl); + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + } + + // Exchange + temp = Xl; Xl = Xr; Xr = temp; + + Xr ^= _P[1]; + Xl ^= _P[0]; + + *xl = Xl; + *xr = Xr; +} + + diff --git a/tdewallet/backend/blowfishtables.h b/tdewallet/backend/blowfishtables.h index dae448f96..a41d7a7e3 100644 --- a/tdewallet/backend/blowfishtables.h +++ b/tdewallet/backend/blowfishtables.h @@ -17,7 +17,7 @@ Boston, MA 02110-1301, USA. */ -// This is also from _Applied_Cryptography_. See blowfish.cc for more details. +// This is also from _Applied_Cryptography_. See blowfish.cpp for more details. #ifndef __bfdefs_h #define __bfdefs_h diff --git a/tdewallet/backend/cbc.cc b/tdewallet/backend/cbc.cc deleted file mode 100644 index 7bc5f3891..000000000 --- a/tdewallet/backend/cbc.cc +++ /dev/null @@ -1,149 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2001 George Staikos - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - - -#include "cbc.h" -#include - - - -CipherBlockChain::CipherBlockChain(BlockCipher *cipher) : _cipher(cipher) { - _next = 0L; - _register = 0L; - _len = -1; - _reader = _writer = 0L; - if (cipher) { - _blksz = cipher->blockSize(); - } -} - - -CipherBlockChain::~CipherBlockChain() { - delete[] (char *)_register; - _register = 0L; - delete[] (char *)_next; - _next = 0L; -} - - -bool CipherBlockChain::setKey(void *key, int bitlength) { - if (_cipher) { - return _cipher->setKey(key, bitlength); - } - return false; -} - - -int CipherBlockChain::keyLen() const { - if (_cipher) { - return _cipher->keyLen(); - } - return -1; -} - - -bool CipherBlockChain::variableKeyLen() const { - if (_cipher) { - return _cipher->variableKeyLen(); - } - return false; -} - - -bool CipherBlockChain::readyToGo() const { - if (_cipher) { - return _cipher->readyToGo(); - } - return false; -} - - -int CipherBlockChain::encrypt(void *block, int len) { - if (_cipher && !_reader) { - int rc; - - _writer |= 1; - - if (!_register) { - _register = new unsigned char[len]; - _len = len; - memset(_register, 0, len); - } else if (len > _len) { - return -1; - } - - // This might be optimizable - char *tb = (char *)block; - for (int i = 0; i < len; i++) { - tb[i] ^= ((char *)_register)[i]; - } - - rc = _cipher->encrypt(block, len); - - if (rc != -1) { - memcpy(_register, block, len); - } - - return rc; - } - return -1; -} - - -int CipherBlockChain::decrypt(void *block, int len) { - if (_cipher && !_writer) { - int rc; - - _reader |= 1; - - if (!_register) { - _register = new unsigned char[len]; - _len = len; - memset(_register, 0, len); - } else if (len > _len) { - return -1; - } - - if (!_next) - _next = new unsigned char[_len]; - memcpy(_next, block, _len); - - rc = _cipher->decrypt(block, len); - - if (rc != -1) { - // This might be optimizable - char *tb = (char *)block; - for (int i = 0; i < len; i++) { - tb[i] ^= ((char *)_register)[i]; - } - } - - void *temp; - temp = _next; - _next = _register; - _register = temp; - - return rc; - } - return -1; -} - - - - diff --git a/tdewallet/backend/cbc.cpp b/tdewallet/backend/cbc.cpp new file mode 100644 index 000000000..7bc5f3891 --- /dev/null +++ b/tdewallet/backend/cbc.cpp @@ -0,0 +1,149 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + + +#include "cbc.h" +#include + + + +CipherBlockChain::CipherBlockChain(BlockCipher *cipher) : _cipher(cipher) { + _next = 0L; + _register = 0L; + _len = -1; + _reader = _writer = 0L; + if (cipher) { + _blksz = cipher->blockSize(); + } +} + + +CipherBlockChain::~CipherBlockChain() { + delete[] (char *)_register; + _register = 0L; + delete[] (char *)_next; + _next = 0L; +} + + +bool CipherBlockChain::setKey(void *key, int bitlength) { + if (_cipher) { + return _cipher->setKey(key, bitlength); + } + return false; +} + + +int CipherBlockChain::keyLen() const { + if (_cipher) { + return _cipher->keyLen(); + } + return -1; +} + + +bool CipherBlockChain::variableKeyLen() const { + if (_cipher) { + return _cipher->variableKeyLen(); + } + return false; +} + + +bool CipherBlockChain::readyToGo() const { + if (_cipher) { + return _cipher->readyToGo(); + } + return false; +} + + +int CipherBlockChain::encrypt(void *block, int len) { + if (_cipher && !_reader) { + int rc; + + _writer |= 1; + + if (!_register) { + _register = new unsigned char[len]; + _len = len; + memset(_register, 0, len); + } else if (len > _len) { + return -1; + } + + // This might be optimizable + char *tb = (char *)block; + for (int i = 0; i < len; i++) { + tb[i] ^= ((char *)_register)[i]; + } + + rc = _cipher->encrypt(block, len); + + if (rc != -1) { + memcpy(_register, block, len); + } + + return rc; + } + return -1; +} + + +int CipherBlockChain::decrypt(void *block, int len) { + if (_cipher && !_writer) { + int rc; + + _reader |= 1; + + if (!_register) { + _register = new unsigned char[len]; + _len = len; + memset(_register, 0, len); + } else if (len > _len) { + return -1; + } + + if (!_next) + _next = new unsigned char[_len]; + memcpy(_next, block, _len); + + rc = _cipher->decrypt(block, len); + + if (rc != -1) { + // This might be optimizable + char *tb = (char *)block; + for (int i = 0; i < len; i++) { + tb[i] ^= ((char *)_register)[i]; + } + } + + void *temp; + temp = _next; + _next = _register; + _register = temp; + + return rc; + } + return -1; +} + + + + diff --git a/tdewallet/backend/sha1.cc b/tdewallet/backend/sha1.cc deleted file mode 100644 index 7420b5f16..000000000 --- a/tdewallet/backend/sha1.cc +++ /dev/null @@ -1,343 +0,0 @@ -/* This file is part of the KDE project - Copyright (C) 2001 George Staikos - Based heavily on SHA1 code from GPG 1.0.3 (C) 1998 FSF - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public - License as published by the Free Software Foundation; either - version 2 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; see the file COPYING.LIB. If not, write to - the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - Boston, MA 02110-1301, USA. -*/ - -#include - -#ifdef HAVE_SYS_TYPES_H -#include -#endif -#ifdef HAVE_STDINT_H -#include /* For uintXX_t on OSX */ -#endif -#ifdef HAVE_SYS_BITYPES_H -#include /* For uintXX_t on Tru64 */ -#endif -#ifdef HAVE_STDINT_H -#include -#endif - -#include "sha1.h" -#include - -// FIXME: this can be optimized to one instruction on most cpus. -#define rol(x,y) ((x << y) | (x >> (32-y))) - - -#define K1 0x5a827999L -#define K2 0x6ed9eba1L -#define K3 0x8f1bbcdcL -#define K4 0xca62c1d6L -#define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) -#define F2(x,y,z) ( x ^ y ^ z ) -#define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) -#define F4(x,y,z) ( x ^ y ^ z ) - -#define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \ - ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \ - , (x[i&0x0f] = (tm << 1) | (tm >> 31))) - -#define R(a,b,c,d,e,f,k,m) do { e += rol(a, 5) \ - + f(b, c, d) \ - + k \ - + m; \ - b = rol(b, 30); \ - } while(0) - - -SHA1::SHA1() { - _hashlen = 160; - _init = false; - reset(); -} - - -int SHA1::reset() { - _h0 = 0x67452301; - _h1 = 0xefcdab89; - _h2 = 0x98badcfe; - _h3 = 0x10325476; - _h4 = 0xc3d2e1f0; - _nblocks = 0; - _count = 0; - memset(_buf, 0, 56); // clear the buffer - - _init = true; - return 0; -} - - -int SHA1::size() const { - return _hashlen; -} - - -SHA1::~SHA1() { - -} - - -void SHA1::transform(void *data) { - unsigned int a, b, c, d, e, tm; - unsigned int x[16]; - unsigned char *_data = (unsigned char *)data; - - a = _h0; - b = _h1; - c = _h2; - d = _h3; - e = _h4; - -#ifdef WORDS_BIGENDIAN - memcpy(x, _data, 64); -#else - int i; - unsigned char *p2; - for (i = 0, p2 = (unsigned char *)x; - i < 16; i++, p2 += 4) { - p2[3] = *_data++; - p2[2] = *_data++; - p2[1] = *_data++; - p2[0] = *_data++; - } -#endif - - R(a, b, c, d, e, F1, K1, x[ 0]); - R(e, a, b, c, d, F1, K1, x[ 1]); - R(d, e, a, b, c, F1, K1, x[ 2]); - R(c, d, e, a, b, F1, K1, x[ 3]); - R(b, c, d, e, a, F1, K1, x[ 4]); - R(a, b, c, d, e, F1, K1, x[ 5]); - R(e, a, b, c, d, F1, K1, x[ 6]); - R(d, e, a, b, c, F1, K1, x[ 7]); - R(c, d, e, a, b, F1, K1, x[ 8]); - R(b, c, d, e, a, F1, K1, x[ 9]); - R(a, b, c, d, e, F1, K1, x[10]); - R(e, a, b, c, d, F1, K1, x[11]); - R(d, e, a, b, c, F1, K1, x[12]); - R(c, d, e, a, b, F1, K1, x[13]); - R(b, c, d, e, a, F1, K1, x[14]); - R(a, b, c, d, e, F1, K1, x[15]); - R(e, a, b, c, d, F1, K1, M(16)); - R(d, e, a, b, c, F1, K1, M(17)); - R(c, d, e, a, b, F1, K1, M(18)); - R(b, c, d, e, a, F1, K1, M(19)); - R(a, b, c, d, e, F2, K2, M(20)); - R(e, a, b, c, d, F2, K2, M(21)); - R(d, e, a, b, c, F2, K2, M(22)); - R(c, d, e, a, b, F2, K2, M(23)); - R(b, c, d, e, a, F2, K2, M(24)); - R(a, b, c, d, e, F2, K2, M(25)); - R(e, a, b, c, d, F2, K2, M(26)); - R(d, e, a, b, c, F2, K2, M(27)); - R(c, d, e, a, b, F2, K2, M(28)); - R(b, c, d, e, a, F2, K2, M(29)); - R(a, b, c, d, e, F2, K2, M(30)); - R(e, a, b, c, d, F2, K2, M(31)); - R(d, e, a, b, c, F2, K2, M(32)); - R(c, d, e, a, b, F2, K2, M(33)); - R(b, c, d, e, a, F2, K2, M(34)); - R(a, b, c, d, e, F2, K2, M(35)); - R(e, a, b, c, d, F2, K2, M(36)); - R(d, e, a, b, c, F2, K2, M(37)); - R(c, d, e, a, b, F2, K2, M(38)); - R(b, c, d, e, a, F2, K2, M(39)); - R(a, b, c, d, e, F3, K3, M(40)); - R(e, a, b, c, d, F3, K3, M(41)); - R(d, e, a, b, c, F3, K3, M(42)); - R(c, d, e, a, b, F3, K3, M(43)); - R(b, c, d, e, a, F3, K3, M(44)); - R(a, b, c, d, e, F3, K3, M(45)); - R(e, a, b, c, d, F3, K3, M(46)); - R(d, e, a, b, c, F3, K3, M(47)); - R(c, d, e, a, b, F3, K3, M(48)); - R(b, c, d, e, a, F3, K3, M(49)); - R(a, b, c, d, e, F3, K3, M(50)); - R(e, a, b, c, d, F3, K3, M(51)); - R(d, e, a, b, c, F3, K3, M(52)); - R(c, d, e, a, b, F3, K3, M(53)); - R(b, c, d, e, a, F3, K3, M(54)); - R(a, b, c, d, e, F3, K3, M(55)); - R(e, a, b, c, d, F3, K3, M(56)); - R(d, e, a, b, c, F3, K3, M(57)); - R(c, d, e, a, b, F3, K3, M(58)); - R(b, c, d, e, a, F3, K3, M(59)); - R(a, b, c, d, e, F4, K4, M(60)); - R(e, a, b, c, d, F4, K4, M(61)); - R(d, e, a, b, c, F4, K4, M(62)); - R(c, d, e, a, b, F4, K4, M(63)); - R(b, c, d, e, a, F4, K4, M(64)); - R(a, b, c, d, e, F4, K4, M(65)); - R(e, a, b, c, d, F4, K4, M(66)); - R(d, e, a, b, c, F4, K4, M(67)); - R(c, d, e, a, b, F4, K4, M(68)); - R(b, c, d, e, a, F4, K4, M(69)); - R(a, b, c, d, e, F4, K4, M(70)); - R(e, a, b, c, d, F4, K4, M(71)); - R(d, e, a, b, c, F4, K4, M(72)); - R(c, d, e, a, b, F4, K4, M(73)); - R(b, c, d, e, a, F4, K4, M(74)); - R(a, b, c, d, e, F4, K4, M(75)); - R(e, a, b, c, d, F4, K4, M(76)); - R(d, e, a, b, c, F4, K4, M(77)); - R(c, d, e, a, b, F4, K4, M(78)); - R(b, c, d, e, a, F4, K4, M(79)); - - _h0 += a; - _h1 += b; - _h2 += c; - _h3 += d; - _h4 += e; - -} - - -bool SHA1::readyToGo() const { - return _init; -} - - -int SHA1::process(const void *block, int len) { - if (!_init) { - return -1; - } - - unsigned char *_block = (unsigned char *)block; - - int cnt = 0; - // Flush the buffer before proceeding - if (_count == 64) { - transform(_buf); - _count = 0; - _nblocks++; - } - - if (!_block) { - return 0; - } - - if (_count) { - for (; len && _count < 64; len--, cnt++) { - _buf[_count++] = *_block++; - } - process(0, 0); // flush the buffer if necessary - if (!len) { - return cnt; - } - } - - while (len >= 64) { - transform(_block); - _count = 0; - _nblocks++; - len -= 64; - cnt += 64; - _block += 64; - } - - for (; len && _count < 64; len--, cnt++) { - _buf[_count++] = *_block++; - } - - return cnt; -} - - -const unsigned char *SHA1::hash() { - unsigned int t, msb, lsb; - unsigned char *p; - - - if (!_init) { - return (unsigned char *)_buf; - } - - process(0, 0); - - msb = 0; - t = _nblocks; - - if ((lsb = t << 6) < t) { - msb++; - } - - msb += t >> 26; - t = lsb; - - if ((lsb = t + _count) < t) { - msb++; - } - - t = lsb; - - if ((lsb = t << 3) < t) { - msb++; - } - - msb += t >> 29; - - _buf[_count++] = 0x80; - - if (_count < 56) { - while (_count < 56) { - _buf[_count++] = 0; - } - } else { - while (_count < 64) { - _buf[_count++] = 0; - } - process(0, 0); - memset(_buf, 0, 56); - } - - _buf[56] = msb >> 24; - _buf[57] = msb >> 16; - _buf[58] = msb >> 8; - _buf[59] = msb; - _buf[60] = lsb >> 24; - _buf[61] = lsb >> 16; - _buf[62] = lsb >> 8; - _buf[63] = lsb; - - transform(_buf); - - p = _buf; - -#ifdef WORDS_BIGENDIAN -#define X( a ) do { *( uint32_t * )p = _h##a; p += 4; } while ( 0 ) -#else -#define X(a) do { *p++ = _h##a >> 24; *p++ = _h##a >> 16; \ - *p++ = _h##a >> 8; *p++ = _h##a; } while (0) -#endif - - X(0); - X(1); - X(2); - X(3); - X(4); - -#undef X - - _init = false; - - return (unsigned char *)_buf; -} - - - diff --git a/tdewallet/backend/sha1.cpp b/tdewallet/backend/sha1.cpp new file mode 100644 index 000000000..7420b5f16 --- /dev/null +++ b/tdewallet/backend/sha1.cpp @@ -0,0 +1,343 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos + Based heavily on SHA1 code from GPG 1.0.3 (C) 1998 FSF + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include + +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_STDINT_H +#include /* For uintXX_t on OSX */ +#endif +#ifdef HAVE_SYS_BITYPES_H +#include /* For uintXX_t on Tru64 */ +#endif +#ifdef HAVE_STDINT_H +#include +#endif + +#include "sha1.h" +#include + +// FIXME: this can be optimized to one instruction on most cpus. +#define rol(x,y) ((x << y) | (x >> (32-y))) + + +#define K1 0x5a827999L +#define K2 0x6ed9eba1L +#define K3 0x8f1bbcdcL +#define K4 0xca62c1d6L +#define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) ) +#define F2(x,y,z) ( x ^ y ^ z ) +#define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) ) +#define F4(x,y,z) ( x ^ y ^ z ) + +#define M(i) ( tm = x[i&0x0f] ^ x[(i-14)&0x0f] \ + ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \ + , (x[i&0x0f] = (tm << 1) | (tm >> 31))) + +#define R(a,b,c,d,e,f,k,m) do { e += rol(a, 5) \ + + f(b, c, d) \ + + k \ + + m; \ + b = rol(b, 30); \ + } while(0) + + +SHA1::SHA1() { + _hashlen = 160; + _init = false; + reset(); +} + + +int SHA1::reset() { + _h0 = 0x67452301; + _h1 = 0xefcdab89; + _h2 = 0x98badcfe; + _h3 = 0x10325476; + _h4 = 0xc3d2e1f0; + _nblocks = 0; + _count = 0; + memset(_buf, 0, 56); // clear the buffer + + _init = true; + return 0; +} + + +int SHA1::size() const { + return _hashlen; +} + + +SHA1::~SHA1() { + +} + + +void SHA1::transform(void *data) { + unsigned int a, b, c, d, e, tm; + unsigned int x[16]; + unsigned char *_data = (unsigned char *)data; + + a = _h0; + b = _h1; + c = _h2; + d = _h3; + e = _h4; + +#ifdef WORDS_BIGENDIAN + memcpy(x, _data, 64); +#else + int i; + unsigned char *p2; + for (i = 0, p2 = (unsigned char *)x; + i < 16; i++, p2 += 4) { + p2[3] = *_data++; + p2[2] = *_data++; + p2[1] = *_data++; + p2[0] = *_data++; + } +#endif + + R(a, b, c, d, e, F1, K1, x[ 0]); + R(e, a, b, c, d, F1, K1, x[ 1]); + R(d, e, a, b, c, F1, K1, x[ 2]); + R(c, d, e, a, b, F1, K1, x[ 3]); + R(b, c, d, e, a, F1, K1, x[ 4]); + R(a, b, c, d, e, F1, K1, x[ 5]); + R(e, a, b, c, d, F1, K1, x[ 6]); + R(d, e, a, b, c, F1, K1, x[ 7]); + R(c, d, e, a, b, F1, K1, x[ 8]); + R(b, c, d, e, a, F1, K1, x[ 9]); + R(a, b, c, d, e, F1, K1, x[10]); + R(e, a, b, c, d, F1, K1, x[11]); + R(d, e, a, b, c, F1, K1, x[12]); + R(c, d, e, a, b, F1, K1, x[13]); + R(b, c, d, e, a, F1, K1, x[14]); + R(a, b, c, d, e, F1, K1, x[15]); + R(e, a, b, c, d, F1, K1, M(16)); + R(d, e, a, b, c, F1, K1, M(17)); + R(c, d, e, a, b, F1, K1, M(18)); + R(b, c, d, e, a, F1, K1, M(19)); + R(a, b, c, d, e, F2, K2, M(20)); + R(e, a, b, c, d, F2, K2, M(21)); + R(d, e, a, b, c, F2, K2, M(22)); + R(c, d, e, a, b, F2, K2, M(23)); + R(b, c, d, e, a, F2, K2, M(24)); + R(a, b, c, d, e, F2, K2, M(25)); + R(e, a, b, c, d, F2, K2, M(26)); + R(d, e, a, b, c, F2, K2, M(27)); + R(c, d, e, a, b, F2, K2, M(28)); + R(b, c, d, e, a, F2, K2, M(29)); + R(a, b, c, d, e, F2, K2, M(30)); + R(e, a, b, c, d, F2, K2, M(31)); + R(d, e, a, b, c, F2, K2, M(32)); + R(c, d, e, a, b, F2, K2, M(33)); + R(b, c, d, e, a, F2, K2, M(34)); + R(a, b, c, d, e, F2, K2, M(35)); + R(e, a, b, c, d, F2, K2, M(36)); + R(d, e, a, b, c, F2, K2, M(37)); + R(c, d, e, a, b, F2, K2, M(38)); + R(b, c, d, e, a, F2, K2, M(39)); + R(a, b, c, d, e, F3, K3, M(40)); + R(e, a, b, c, d, F3, K3, M(41)); + R(d, e, a, b, c, F3, K3, M(42)); + R(c, d, e, a, b, F3, K3, M(43)); + R(b, c, d, e, a, F3, K3, M(44)); + R(a, b, c, d, e, F3, K3, M(45)); + R(e, a, b, c, d, F3, K3, M(46)); + R(d, e, a, b, c, F3, K3, M(47)); + R(c, d, e, a, b, F3, K3, M(48)); + R(b, c, d, e, a, F3, K3, M(49)); + R(a, b, c, d, e, F3, K3, M(50)); + R(e, a, b, c, d, F3, K3, M(51)); + R(d, e, a, b, c, F3, K3, M(52)); + R(c, d, e, a, b, F3, K3, M(53)); + R(b, c, d, e, a, F3, K3, M(54)); + R(a, b, c, d, e, F3, K3, M(55)); + R(e, a, b, c, d, F3, K3, M(56)); + R(d, e, a, b, c, F3, K3, M(57)); + R(c, d, e, a, b, F3, K3, M(58)); + R(b, c, d, e, a, F3, K3, M(59)); + R(a, b, c, d, e, F4, K4, M(60)); + R(e, a, b, c, d, F4, K4, M(61)); + R(d, e, a, b, c, F4, K4, M(62)); + R(c, d, e, a, b, F4, K4, M(63)); + R(b, c, d, e, a, F4, K4, M(64)); + R(a, b, c, d, e, F4, K4, M(65)); + R(e, a, b, c, d, F4, K4, M(66)); + R(d, e, a, b, c, F4, K4, M(67)); + R(c, d, e, a, b, F4, K4, M(68)); + R(b, c, d, e, a, F4, K4, M(69)); + R(a, b, c, d, e, F4, K4, M(70)); + R(e, a, b, c, d, F4, K4, M(71)); + R(d, e, a, b, c, F4, K4, M(72)); + R(c, d, e, a, b, F4, K4, M(73)); + R(b, c, d, e, a, F4, K4, M(74)); + R(a, b, c, d, e, F4, K4, M(75)); + R(e, a, b, c, d, F4, K4, M(76)); + R(d, e, a, b, c, F4, K4, M(77)); + R(c, d, e, a, b, F4, K4, M(78)); + R(b, c, d, e, a, F4, K4, M(79)); + + _h0 += a; + _h1 += b; + _h2 += c; + _h3 += d; + _h4 += e; + +} + + +bool SHA1::readyToGo() const { + return _init; +} + + +int SHA1::process(const void *block, int len) { + if (!_init) { + return -1; + } + + unsigned char *_block = (unsigned char *)block; + + int cnt = 0; + // Flush the buffer before proceeding + if (_count == 64) { + transform(_buf); + _count = 0; + _nblocks++; + } + + if (!_block) { + return 0; + } + + if (_count) { + for (; len && _count < 64; len--, cnt++) { + _buf[_count++] = *_block++; + } + process(0, 0); // flush the buffer if necessary + if (!len) { + return cnt; + } + } + + while (len >= 64) { + transform(_block); + _count = 0; + _nblocks++; + len -= 64; + cnt += 64; + _block += 64; + } + + for (; len && _count < 64; len--, cnt++) { + _buf[_count++] = *_block++; + } + + return cnt; +} + + +const unsigned char *SHA1::hash() { + unsigned int t, msb, lsb; + unsigned char *p; + + + if (!_init) { + return (unsigned char *)_buf; + } + + process(0, 0); + + msb = 0; + t = _nblocks; + + if ((lsb = t << 6) < t) { + msb++; + } + + msb += t >> 26; + t = lsb; + + if ((lsb = t + _count) < t) { + msb++; + } + + t = lsb; + + if ((lsb = t << 3) < t) { + msb++; + } + + msb += t >> 29; + + _buf[_count++] = 0x80; + + if (_count < 56) { + while (_count < 56) { + _buf[_count++] = 0; + } + } else { + while (_count < 64) { + _buf[_count++] = 0; + } + process(0, 0); + memset(_buf, 0, 56); + } + + _buf[56] = msb >> 24; + _buf[57] = msb >> 16; + _buf[58] = msb >> 8; + _buf[59] = msb; + _buf[60] = lsb >> 24; + _buf[61] = lsb >> 16; + _buf[62] = lsb >> 8; + _buf[63] = lsb; + + transform(_buf); + + p = _buf; + +#ifdef WORDS_BIGENDIAN +#define X( a ) do { *( uint32_t * )p = _h##a; p += 4; } while ( 0 ) +#else +#define X(a) do { *p++ = _h##a >> 24; *p++ = _h##a >> 16; \ + *p++ = _h##a >> 8; *p++ = _h##a; } while (0) +#endif + + X(0); + X(1); + X(2); + X(3); + X(4); + +#undef X + + _init = false; + + return (unsigned char *)_buf; +} + + + diff --git a/tdewallet/backend/tdewalletbackend.cc b/tdewallet/backend/tdewalletbackend.cc deleted file mode 100644 index 83e37a49a..000000000 --- a/tdewallet/backend/tdewalletbackend.cc +++ /dev/null @@ -1,838 +0,0 @@ -/* This file is part of the KDE project - * - * Copyright (C) 2001-2004 George Staikos - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#include "tdewalletbackend.h" - -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "blowfish.h" -#include "sha1.h" -#include "cbc.h" - -#include - -#define TDEWALLET_VERSION_MAJOR 0 -#define TDEWALLET_VERSION_MINOR 0 - -#define TDEWALLET_CIPHER_BLOWFISH_CBC 0 -#define TDEWALLET_CIPHER_3DES_CBC 1 // unsupported - -#define TDEWALLET_HASH_SHA1 0 -#define TDEWALLET_HASH_MD5 1 // unsupported - - -using namespace TDEWallet; - -#define KWMAGIC "KWALLET\n\r\0\r\n" -#define KWMAGIC_LEN 12 - -static void initTDEWalletDir() -{ - TDEGlobal::dirs()->addResourceType("tdewallet", "share/apps/tdewallet"); -} - -Backend::Backend(const TQString& name, bool isPath) : _name(name), _ref(0) { - initTDEWalletDir(); - if (isPath) { - _path = name; - } else { - _path = TDEGlobal::dirs()->saveLocation("tdewallet") + "/" + _name + ".kwl"; - } - - _open = false; -} - - -Backend::~Backend() { - if (_open) { - close(); - } -} - - -int Backend::close() { - for (FolderMap::ConstIterator i = _entries.begin(); i != _entries.end(); ++i) { - for (EntryMap::ConstIterator j = i.data().begin(); j != i.data().end(); ++j) { - delete j.data(); - } - } - _entries.clear(); - -return 0; -} - - -static int getRandomBlock(TQByteArray& randBlock) { - // First try /dev/urandom - if (TQFile::exists("/dev/urandom")) { - TQFile devrand("/dev/urandom"); - if (devrand.open(IO_ReadOnly)) { - unsigned int rc = devrand.readBlock(randBlock.data(), randBlock.size()); - - if (rc != randBlock.size()) { - return -3; // not enough data read - } - - return 0; - } - } - - // If that failed, try /dev/random - // FIXME: open in noblocking mode! - if (TQFile::exists("/dev/random")) { - TQFile devrand("/dev/random"); - if (devrand.open(IO_ReadOnly)) { - unsigned int rc = 0; - unsigned int cnt = 0; - - do { - int rc2 = devrand.readBlock(randBlock.data() + rc, randBlock.size()); - - if (rc2 < 0) { - return -3; // read error - } - - rc += rc2; - cnt++; - if (cnt > randBlock.size()) { - return -4; // reading forever?! - } - } while(rc < randBlock.size()); - - return 0; - } - } - - // EGD method - char *randFilename; - if ((randFilename = getenv("RANDFILE"))) { - if (TQFile::exists(randFilename)) { - TQFile devrand(randFilename); - if (devrand.open(IO_ReadOnly)) { - unsigned int rc = devrand.readBlock(randBlock.data(), randBlock.size()); - if (rc != randBlock.size()) { - return -3; // not enough data read - } - return 0; - } - } - } - - // Couldn't get any random data!! - - return -1; -} - - -// this should be SHA-512 for release probably -static int password2hash(const TQByteArray& password, TQByteArray& hash) { - SHA1 sha; - int shasz = sha.size() / 8; - - assert(shasz >= 20); - - TQByteArray block1(shasz); - - sha.process(password.data(), TQMIN(password.size(), 16)); - - // To make brute force take longer - for (int i = 0; i < 2000; i++) { - memcpy(block1.data(), sha.hash(), shasz); - sha.reset(); - sha.process(block1.data(), shasz); - } - - sha.reset(); - - if (password.size() > 16) { - sha.process(password.data() + 16, TQMIN(password.size() - 16, 16)); - TQByteArray block2(shasz); - // To make brute force take longer - for (int i = 0; i < 2000; i++) { - memcpy(block2.data(), sha.hash(), shasz); - sha.reset(); - sha.process(block2.data(), shasz); - } - - sha.reset(); - - if (password.size() > 32) { - sha.process(password.data() + 32, TQMIN(password.size() - 32, 16)); - - TQByteArray block3(shasz); - // To make brute force take longer - for (int i = 0; i < 2000; i++) { - memcpy(block3.data(), sha.hash(), shasz); - sha.reset(); - sha.process(block3.data(), shasz); - } - - sha.reset(); - - if (password.size() > 48) { - sha.process(password.data() + 48, password.size() - 48); - - TQByteArray block4(shasz); - // To make brute force take longer - for (int i = 0; i < 2000; i++) { - memcpy(block4.data(), sha.hash(), shasz); - sha.reset(); - sha.process(block4.data(), shasz); - } - - sha.reset(); - // split 14/14/14/14 - hash.resize(56); - memcpy(hash.data(), block1.data(), 14); - memcpy(hash.data() + 14, block2.data(), 14); - memcpy(hash.data() + 28, block3.data(), 14); - memcpy(hash.data() + 42, block4.data(), 14); - block4.fill(0); - } else { - // split 20/20/16 - hash.resize(56); - memcpy(hash.data(), block1.data(), 20); - memcpy(hash.data() + 20, block2.data(), 20); - memcpy(hash.data() + 40, block3.data(), 16); - } - block3.fill(0); - } else { - // split 20/20 - hash.resize(40); - memcpy(hash.data(), block1.data(), 20); - memcpy(hash.data() + 20, block2.data(), 20); - } - block2.fill(0); - } else { - // entirely block1 - hash.resize(20); - memcpy(hash.data(), block1.data(), 20); - } - - block1.fill(0); - - return 0; -} - - -bool Backend::exists(const TQString& wallet) { - initTDEWalletDir(); - TQString path = TDEGlobal::dirs()->saveLocation("tdewallet") + "/" + wallet + ".kwl"; - // Note: 60 bytes is presently the minimum size of a wallet file. - // Anything smaller is junk. -return TQFile::exists(path) && TQFileInfo(path).size() >= 60; -} - - -TQString Backend::openRCToString(int rc) { - switch (rc) { - case -255: - return i18n("Already open."); - case -2: - return i18n("Error opening file."); - case -3: - return i18n("Not a wallet file."); - case -4: - return i18n("Unsupported file format revision."); - case -42: - return i18n("Unknown encryption scheme."); - case -43: - return i18n("Corrupt file?"); - case -8: - return i18n("Error validating wallet integrity. Possibly corrupted."); - case -5: - case -7: - case -9: - return i18n("Read error - possibly incorrect password."); - case -6: - return i18n("Decryption error."); - default: - return TQString::null; - } -} - - -int Backend::open(const TQByteArray& password) { - - if (_open) { - return -255; // already open - } - - TQByteArray passhash; - - // No wallet existed. Let's create it. - // Note: 60 bytes is presently the minimum size of a wallet file. - // Anything smaller is junk and should be deleted. - if (!TQFile::exists(_path) || TQFileInfo(_path).size() < 60) { - TQFile newfile(_path); - if (!newfile.open(IO_ReadWrite)) { - return -2; // error opening file - } - newfile.close(); - _open = true; - sync(password); - return 1; // new file opened, but OK - } - - TQFile db(_path); - - if (!db.open(IO_ReadOnly)) { - return -2; // error opening file - } - - char magicBuf[KWMAGIC_LEN]; - db.readBlock(magicBuf, KWMAGIC_LEN); - if (memcmp(magicBuf, KWMAGIC, KWMAGIC_LEN) != 0) { - return -3; // bad magic - } - - db.readBlock(magicBuf, 4); - - // First byte is major version, second byte is minor version - if (magicBuf[0] != TDEWALLET_VERSION_MAJOR) { - return -4; // unknown version - } - - if (magicBuf[1] != TDEWALLET_VERSION_MINOR) { - return -4; // unknown version - } - - if (magicBuf[2] != TDEWALLET_CIPHER_BLOWFISH_CBC) { - return -42; // unknown cipher - } - - if (magicBuf[3] != TDEWALLET_HASH_SHA1) { - return -42; // unknown hash - } - - _hashes.clear(); - // Read in the hashes - TQDataStream hds(&db); - TQ_UINT32 n; - hds >> n; - if (n > 0xffff) { // sanity check - return -43; - } - - for (size_t i = 0; i < n; ++i) { - KMD5::Digest d, d2; // judgment day - MD5Digest ba; - TQMap >::iterator it; - TQ_UINT32 fsz; - if (hds.atEnd()) return -43; - hds.readRawBytes(reinterpret_cast(d), 16); - hds >> fsz; - ba.duplicate(reinterpret_cast(d), 16); - it = _hashes.insert(ba, TQValueList()); - for (size_t j = 0; j < fsz; ++j) { - hds.readRawBytes(reinterpret_cast(d2), 16); - ba.duplicate(reinterpret_cast(d2), 16); - (*it).append(ba); - } - } - - // Read in the rest of the file. - TQByteArray encrypted = db.readAll(); - assert(encrypted.size() < db.size()); - - BlowFish _bf; - CipherBlockChain bf(&_bf); - int blksz = bf.blockSize(); - if ((encrypted.size() % blksz) != 0) { - return -5; // invalid file structure - } - - // Decrypt the encrypted data - passhash.resize(bf.keyLen()/8); - password2hash(password, passhash); - - bf.setKey((void *)passhash.data(), passhash.size()*8); - - if (!encrypted.data()) { - passhash.fill(0); - encrypted.fill(0); - return -7; // file structure error - } - - int rc = bf.decrypt(encrypted.data(), encrypted.size()); - if (rc < 0) { - passhash.fill(0); - encrypted.fill(0); - return -6; // decrypt error - } - - passhash.fill(0); // passhash is UNUSABLE NOW - - const char *t = encrypted.data(); - - // strip the leading data - t += blksz; // one block of random data - - // strip the file size off - long fsize = 0; - - fsize |= (long(*t) << 24) & 0xff000000; - t++; - fsize |= (long(*t) << 16) & 0x00ff0000; - t++; - fsize |= (long(*t) << 8) & 0x0000ff00; - t++; - fsize |= long(*t) & 0x000000ff; - t++; - - if (fsize < 0 || fsize > long(encrypted.size()) - blksz - 4) { - //kdDebug() << "fsize: " << fsize << " encrypted.size(): " << encrypted.size() << " blksz: " << blksz << endl; - encrypted.fill(0); - return -9; // file structure error. - } - - // compute the hash ourself - SHA1 sha; - sha.process(t, fsize); - const char *testhash = (const char *)sha.hash(); - - // compare hashes - int sz = encrypted.size(); - for (int i = 0; i < 20; i++) { - if (testhash[i] != static_cast(encrypted.at(sz - 20 + i))) { - encrypted.fill(0); - sha.reset(); - return -8; // hash error. - } - } - - sha.reset(); - - // chop off the leading blksz+4 bytes - TQByteArray tmpenc; - tmpenc.duplicate(encrypted.data()+blksz+4, fsize); - encrypted.fill(0); - encrypted.duplicate(tmpenc.data(), tmpenc.size()); - tmpenc.fill(0); - - // Load the data structures up - TQDataStream eStream(encrypted, IO_ReadOnly); - - while (!eStream.atEnd()) { - TQString folder; - TQ_UINT32 n; - - eStream >> folder; - eStream >> n; - - // Force initialisation - _entries[folder].clear(); - - for (size_t i = 0; i < n; i++) { - TQString key; - TDEWallet::Wallet::EntryType et = TDEWallet::Wallet::Unknown; - Entry *e = new Entry; - eStream >> key; - TQ_INT32 x = 0; // necessary to read properly - eStream >> x; - et = static_cast(x); - - switch (et) { - case TDEWallet::Wallet::Password: - case TDEWallet::Wallet::Stream: - case TDEWallet::Wallet::Map: - break; - default: // Unknown entry - delete e; - continue; - } - - TQByteArray a; - eStream >> a; - e->setValue(a); - e->setType(et); - e->setKey(key); - _entries[folder][key] = e; - } - } - - _open = true; - return 0; -} - - -int Backend::sync(const TQByteArray& password) { - if (!_open) { - return -255; // not open yet - } - - KSaveFile sf(_path, 0600); - TQFile *qf = sf.file(); - - if (!qf) { - sf.abort(); - return -1; // error opening file - } - - qf->writeBlock(KWMAGIC, KWMAGIC_LEN); - - // Write the version number - TQByteArray version(4); - version[0] = TDEWALLET_VERSION_MAJOR; - version[1] = TDEWALLET_VERSION_MINOR; - version[2] = TDEWALLET_CIPHER_BLOWFISH_CBC; - version[3] = TDEWALLET_HASH_SHA1; - qf->writeBlock(version, 4); - - // Holds the hashes we write out - TQByteArray hashes; - TQDataStream hashStream(hashes, IO_WriteOnly); - KMD5 md5; - hashStream << static_cast(_entries.count()); - - // Holds decrypted data prior to encryption - TQByteArray decrypted; - - // FIXME: we should estimate the amount of data we will write in each - // buffer and resize them approximately in order to avoid extra - // resizes. - - // populate decrypted - TQDataStream dStream(decrypted, IO_WriteOnly); - for (FolderMap::ConstIterator i = _entries.begin(); i != _entries.end(); ++i) { - dStream << i.key(); - dStream << static_cast(i.data().count()); - - md5.reset(); - md5.update(i.key().utf8()); - hashStream.writeRawBytes(reinterpret_cast(&(md5.rawDigest()[0])), 16); - hashStream << static_cast(i.data().count()); - - for (EntryMap::ConstIterator j = i.data().begin(); j != i.data().end(); ++j) { - dStream << j.key(); - dStream << static_cast(j.data()->type()); - dStream << j.data()->value(); - - md5.reset(); - md5.update(j.key().utf8()); - hashStream.writeRawBytes(reinterpret_cast(&(md5.rawDigest()[0])), 16); - } - } - - qf->writeBlock(hashes, hashes.size()); - - // calculate the hash of the file - SHA1 sha; - BlowFish _bf; - CipherBlockChain bf(&_bf); - - sha.process(decrypted.data(), decrypted.size()); - - // prepend and append the random data - TQByteArray wholeFile; - long blksz = bf.blockSize(); - long newsize = decrypted.size() + - blksz + // encrypted block - 4 + // file size - 20; // size of the SHA hash - - int delta = (blksz - (newsize % blksz)); - newsize += delta; - wholeFile.resize(newsize); - - TQByteArray randBlock; - randBlock.resize(blksz+delta); - if (getRandomBlock(randBlock) < 0) { - sha.reset(); - decrypted.fill(0); - sf.abort(); - return -3; // Fatal error: can't get random - } - - for (int i = 0; i < blksz; i++) { - wholeFile[i] = randBlock[i]; - } - - for (int i = 0; i < 4; i++) { - wholeFile[(int)(i+blksz)] = (decrypted.size() >> 8*(3-i))&0xff; - } - - for (unsigned int i = 0; i < decrypted.size(); i++) { - wholeFile[(int)(i+blksz+4)] = decrypted[i]; - } - - for (int i = 0; i < delta; i++) { - wholeFile[(int)(i+blksz+4+decrypted.size())] = randBlock[(int)(i+blksz)]; - } - - const char *hash = (const char *)sha.hash(); - for (int i = 0; i < 20; i++) { - wholeFile[(int)(newsize - 20 + i)] = hash[i]; - } - - sha.reset(); - decrypted.fill(0); - - // hash the passphrase - TQByteArray passhash; - password2hash(password, passhash); - - // encrypt the data - if (!bf.setKey(passhash.data(), passhash.size() * 8)) { - passhash.fill(0); - wholeFile.fill(0); - sf.abort(); - return -2; - } - - int rc = bf.encrypt(wholeFile.data(), wholeFile.size()); - if (rc < 0) { - passhash.fill(0); - wholeFile.fill(0); - sf.abort(); - return -2; // encrypt error - } - - passhash.fill(0); // passhash is UNUSABLE NOW - - // write the file - qf->writeBlock(wholeFile, wholeFile.size()); - if (!sf.close()) { - wholeFile.fill(0); - sf.abort(); - return -4; // write error - } - - wholeFile.fill(0); - -return 0; -} - - -int Backend::close(const TQByteArray& password) { - int rc = sync(password); - _open = false; - if (rc != 0) { - return rc; - } - return close(); -} - - -const TQString& Backend::walletName() const { - return _name; -} - - -bool Backend::isOpen() const { - return _open; -} - - -TQStringList Backend::folderList() const { - return _entries.keys(); -} - - -TQStringList Backend::entryList() const { - return _entries[_folder].keys(); -} - - -Entry *Backend::readEntry(const TQString& key) { -Entry *rc = 0L; - - if (_open && hasEntry(key)) { - rc = _entries[_folder][key]; - } - -return rc; -} - - -TQPtrList Backend::readEntryList(const TQString& key) { - TQPtrList rc; - - if (!_open) { - return rc; - } - - TQRegExp re(key, true, true); - - const EntryMap& map = _entries[_folder]; - for (EntryMap::ConstIterator i = map.begin(); i != map.end(); ++i) { - if (re.exactMatch(i.key())) { - rc.append(i.data()); - } - } - return rc; -} - - -bool Backend::createFolder(const TQString& f) { - if (_entries.contains(f)) { - return false; - } - - _entries.insert(f, EntryMap()); - - KMD5 folderMd5; - folderMd5.update(f.utf8()); - _hashes.insert(MD5Digest(folderMd5.rawDigest()), TQValueList()); - -return true; -} - - -int Backend::renameEntry(const TQString& oldName, const TQString& newName) { -EntryMap& emap = _entries[_folder]; -EntryMap::Iterator oi = emap.find(oldName); -EntryMap::Iterator ni = emap.find(newName); - - if (oi != emap.end() && ni == emap.end()) { - Entry *e = oi.data(); - emap.remove(oi); - emap[newName] = e; - - KMD5 folderMd5; - folderMd5.update(_folder.utf8()); - - HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); - if (i != _hashes.end()) { - KMD5 oldMd5, newMd5; - oldMd5.update(oldName.utf8()); - newMd5.update(newName.utf8()); - i.data().remove(MD5Digest(oldMd5.rawDigest())); - i.data().append(MD5Digest(newMd5.rawDigest())); - } - return 0; - } - -return -1; -} - - -void Backend::writeEntry(Entry *e) { - if (!_open) - return; - - if (!hasEntry(e->key())) { - _entries[_folder][e->key()] = new Entry; - } - _entries[_folder][e->key()]->copy(e); - - KMD5 folderMd5; - folderMd5.update(_folder.utf8()); - - HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); - if (i != _hashes.end()) { - KMD5 md5; - md5.update(e->key().utf8()); - i.data().append(MD5Digest(md5.rawDigest())); - } -} - - -bool Backend::hasEntry(const TQString& key) const { - return _entries.contains(_folder) && _entries[_folder].contains(key); -} - - -bool Backend::removeEntry(const TQString& key) { - if (!_open) { - return false; - } - - FolderMap::Iterator fi = _entries.find(_folder); - EntryMap::Iterator ei = fi.data().find(key); - - if (fi != _entries.end() && ei != fi.data().end()) { - delete ei.data(); - fi.data().remove(ei); - KMD5 folderMd5; - folderMd5.update(_folder.utf8()); - - HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); - if (i != _hashes.end()) { - KMD5 md5; - md5.update(key.utf8()); - i.data().remove(MD5Digest(md5.rawDigest())); - } - return true; - } - -return false; -} - - -bool Backend::removeFolder(const TQString& f) { - if (!_open) { - return false; - } - - FolderMap::Iterator fi = _entries.find(f); - - if (fi != _entries.end()) { - if (_folder == f) { - _folder = TQString::null; - } - - for (EntryMap::Iterator ei = fi.data().begin(); ei != fi.data().end(); ++ei) { - delete ei.data(); - } - - _entries.remove(fi); - - KMD5 folderMd5; - folderMd5.update(f.utf8()); - _hashes.erase(MD5Digest(folderMd5.rawDigest())); - return true; - } - -return false; -} - - -bool Backend::folderDoesNotExist(const TQString& folder) const { - KMD5 md5; - md5.update(folder.utf8()); - return !_hashes.contains(MD5Digest(md5.rawDigest())); -} - - -bool Backend::entryDoesNotExist(const TQString& folder, const TQString& entry) const { - KMD5 md5; - md5.update(folder.utf8()); - HashMap::const_iterator i = _hashes.find(MD5Digest(md5.rawDigest())); - if (i != _hashes.end()) { - md5.reset(); - md5.update(entry.utf8()); - return !i.data().contains(MD5Digest(md5.rawDigest())); - } - return true; -} - - diff --git a/tdewallet/backend/tdewalletbackend.cpp b/tdewallet/backend/tdewalletbackend.cpp new file mode 100644 index 000000000..83e37a49a --- /dev/null +++ b/tdewallet/backend/tdewalletbackend.cpp @@ -0,0 +1,838 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2004 George Staikos + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "tdewalletbackend.h" + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "blowfish.h" +#include "sha1.h" +#include "cbc.h" + +#include + +#define TDEWALLET_VERSION_MAJOR 0 +#define TDEWALLET_VERSION_MINOR 0 + +#define TDEWALLET_CIPHER_BLOWFISH_CBC 0 +#define TDEWALLET_CIPHER_3DES_CBC 1 // unsupported + +#define TDEWALLET_HASH_SHA1 0 +#define TDEWALLET_HASH_MD5 1 // unsupported + + +using namespace TDEWallet; + +#define KWMAGIC "KWALLET\n\r\0\r\n" +#define KWMAGIC_LEN 12 + +static void initTDEWalletDir() +{ + TDEGlobal::dirs()->addResourceType("tdewallet", "share/apps/tdewallet"); +} + +Backend::Backend(const TQString& name, bool isPath) : _name(name), _ref(0) { + initTDEWalletDir(); + if (isPath) { + _path = name; + } else { + _path = TDEGlobal::dirs()->saveLocation("tdewallet") + "/" + _name + ".kwl"; + } + + _open = false; +} + + +Backend::~Backend() { + if (_open) { + close(); + } +} + + +int Backend::close() { + for (FolderMap::ConstIterator i = _entries.begin(); i != _entries.end(); ++i) { + for (EntryMap::ConstIterator j = i.data().begin(); j != i.data().end(); ++j) { + delete j.data(); + } + } + _entries.clear(); + +return 0; +} + + +static int getRandomBlock(TQByteArray& randBlock) { + // First try /dev/urandom + if (TQFile::exists("/dev/urandom")) { + TQFile devrand("/dev/urandom"); + if (devrand.open(IO_ReadOnly)) { + unsigned int rc = devrand.readBlock(randBlock.data(), randBlock.size()); + + if (rc != randBlock.size()) { + return -3; // not enough data read + } + + return 0; + } + } + + // If that failed, try /dev/random + // FIXME: open in noblocking mode! + if (TQFile::exists("/dev/random")) { + TQFile devrand("/dev/random"); + if (devrand.open(IO_ReadOnly)) { + unsigned int rc = 0; + unsigned int cnt = 0; + + do { + int rc2 = devrand.readBlock(randBlock.data() + rc, randBlock.size()); + + if (rc2 < 0) { + return -3; // read error + } + + rc += rc2; + cnt++; + if (cnt > randBlock.size()) { + return -4; // reading forever?! + } + } while(rc < randBlock.size()); + + return 0; + } + } + + // EGD method + char *randFilename; + if ((randFilename = getenv("RANDFILE"))) { + if (TQFile::exists(randFilename)) { + TQFile devrand(randFilename); + if (devrand.open(IO_ReadOnly)) { + unsigned int rc = devrand.readBlock(randBlock.data(), randBlock.size()); + if (rc != randBlock.size()) { + return -3; // not enough data read + } + return 0; + } + } + } + + // Couldn't get any random data!! + + return -1; +} + + +// this should be SHA-512 for release probably +static int password2hash(const TQByteArray& password, TQByteArray& hash) { + SHA1 sha; + int shasz = sha.size() / 8; + + assert(shasz >= 20); + + TQByteArray block1(shasz); + + sha.process(password.data(), TQMIN(password.size(), 16)); + + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block1.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block1.data(), shasz); + } + + sha.reset(); + + if (password.size() > 16) { + sha.process(password.data() + 16, TQMIN(password.size() - 16, 16)); + TQByteArray block2(shasz); + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block2.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block2.data(), shasz); + } + + sha.reset(); + + if (password.size() > 32) { + sha.process(password.data() + 32, TQMIN(password.size() - 32, 16)); + + TQByteArray block3(shasz); + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block3.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block3.data(), shasz); + } + + sha.reset(); + + if (password.size() > 48) { + sha.process(password.data() + 48, password.size() - 48); + + TQByteArray block4(shasz); + // To make brute force take longer + for (int i = 0; i < 2000; i++) { + memcpy(block4.data(), sha.hash(), shasz); + sha.reset(); + sha.process(block4.data(), shasz); + } + + sha.reset(); + // split 14/14/14/14 + hash.resize(56); + memcpy(hash.data(), block1.data(), 14); + memcpy(hash.data() + 14, block2.data(), 14); + memcpy(hash.data() + 28, block3.data(), 14); + memcpy(hash.data() + 42, block4.data(), 14); + block4.fill(0); + } else { + // split 20/20/16 + hash.resize(56); + memcpy(hash.data(), block1.data(), 20); + memcpy(hash.data() + 20, block2.data(), 20); + memcpy(hash.data() + 40, block3.data(), 16); + } + block3.fill(0); + } else { + // split 20/20 + hash.resize(40); + memcpy(hash.data(), block1.data(), 20); + memcpy(hash.data() + 20, block2.data(), 20); + } + block2.fill(0); + } else { + // entirely block1 + hash.resize(20); + memcpy(hash.data(), block1.data(), 20); + } + + block1.fill(0); + + return 0; +} + + +bool Backend::exists(const TQString& wallet) { + initTDEWalletDir(); + TQString path = TDEGlobal::dirs()->saveLocation("tdewallet") + "/" + wallet + ".kwl"; + // Note: 60 bytes is presently the minimum size of a wallet file. + // Anything smaller is junk. +return TQFile::exists(path) && TQFileInfo(path).size() >= 60; +} + + +TQString Backend::openRCToString(int rc) { + switch (rc) { + case -255: + return i18n("Already open."); + case -2: + return i18n("Error opening file."); + case -3: + return i18n("Not a wallet file."); + case -4: + return i18n("Unsupported file format revision."); + case -42: + return i18n("Unknown encryption scheme."); + case -43: + return i18n("Corrupt file?"); + case -8: + return i18n("Error validating wallet integrity. Possibly corrupted."); + case -5: + case -7: + case -9: + return i18n("Read error - possibly incorrect password."); + case -6: + return i18n("Decryption error."); + default: + return TQString::null; + } +} + + +int Backend::open(const TQByteArray& password) { + + if (_open) { + return -255; // already open + } + + TQByteArray passhash; + + // No wallet existed. Let's create it. + // Note: 60 bytes is presently the minimum size of a wallet file. + // Anything smaller is junk and should be deleted. + if (!TQFile::exists(_path) || TQFileInfo(_path).size() < 60) { + TQFile newfile(_path); + if (!newfile.open(IO_ReadWrite)) { + return -2; // error opening file + } + newfile.close(); + _open = true; + sync(password); + return 1; // new file opened, but OK + } + + TQFile db(_path); + + if (!db.open(IO_ReadOnly)) { + return -2; // error opening file + } + + char magicBuf[KWMAGIC_LEN]; + db.readBlock(magicBuf, KWMAGIC_LEN); + if (memcmp(magicBuf, KWMAGIC, KWMAGIC_LEN) != 0) { + return -3; // bad magic + } + + db.readBlock(magicBuf, 4); + + // First byte is major version, second byte is minor version + if (magicBuf[0] != TDEWALLET_VERSION_MAJOR) { + return -4; // unknown version + } + + if (magicBuf[1] != TDEWALLET_VERSION_MINOR) { + return -4; // unknown version + } + + if (magicBuf[2] != TDEWALLET_CIPHER_BLOWFISH_CBC) { + return -42; // unknown cipher + } + + if (magicBuf[3] != TDEWALLET_HASH_SHA1) { + return -42; // unknown hash + } + + _hashes.clear(); + // Read in the hashes + TQDataStream hds(&db); + TQ_UINT32 n; + hds >> n; + if (n > 0xffff) { // sanity check + return -43; + } + + for (size_t i = 0; i < n; ++i) { + KMD5::Digest d, d2; // judgment day + MD5Digest ba; + TQMap >::iterator it; + TQ_UINT32 fsz; + if (hds.atEnd()) return -43; + hds.readRawBytes(reinterpret_cast(d), 16); + hds >> fsz; + ba.duplicate(reinterpret_cast(d), 16); + it = _hashes.insert(ba, TQValueList()); + for (size_t j = 0; j < fsz; ++j) { + hds.readRawBytes(reinterpret_cast(d2), 16); + ba.duplicate(reinterpret_cast(d2), 16); + (*it).append(ba); + } + } + + // Read in the rest of the file. + TQByteArray encrypted = db.readAll(); + assert(encrypted.size() < db.size()); + + BlowFish _bf; + CipherBlockChain bf(&_bf); + int blksz = bf.blockSize(); + if ((encrypted.size() % blksz) != 0) { + return -5; // invalid file structure + } + + // Decrypt the encrypted data + passhash.resize(bf.keyLen()/8); + password2hash(password, passhash); + + bf.setKey((void *)passhash.data(), passhash.size()*8); + + if (!encrypted.data()) { + passhash.fill(0); + encrypted.fill(0); + return -7; // file structure error + } + + int rc = bf.decrypt(encrypted.data(), encrypted.size()); + if (rc < 0) { + passhash.fill(0); + encrypted.fill(0); + return -6; // decrypt error + } + + passhash.fill(0); // passhash is UNUSABLE NOW + + const char *t = encrypted.data(); + + // strip the leading data + t += blksz; // one block of random data + + // strip the file size off + long fsize = 0; + + fsize |= (long(*t) << 24) & 0xff000000; + t++; + fsize |= (long(*t) << 16) & 0x00ff0000; + t++; + fsize |= (long(*t) << 8) & 0x0000ff00; + t++; + fsize |= long(*t) & 0x000000ff; + t++; + + if (fsize < 0 || fsize > long(encrypted.size()) - blksz - 4) { + //kdDebug() << "fsize: " << fsize << " encrypted.size(): " << encrypted.size() << " blksz: " << blksz << endl; + encrypted.fill(0); + return -9; // file structure error. + } + + // compute the hash ourself + SHA1 sha; + sha.process(t, fsize); + const char *testhash = (const char *)sha.hash(); + + // compare hashes + int sz = encrypted.size(); + for (int i = 0; i < 20; i++) { + if (testhash[i] != static_cast(encrypted.at(sz - 20 + i))) { + encrypted.fill(0); + sha.reset(); + return -8; // hash error. + } + } + + sha.reset(); + + // chop off the leading blksz+4 bytes + TQByteArray tmpenc; + tmpenc.duplicate(encrypted.data()+blksz+4, fsize); + encrypted.fill(0); + encrypted.duplicate(tmpenc.data(), tmpenc.size()); + tmpenc.fill(0); + + // Load the data structures up + TQDataStream eStream(encrypted, IO_ReadOnly); + + while (!eStream.atEnd()) { + TQString folder; + TQ_UINT32 n; + + eStream >> folder; + eStream >> n; + + // Force initialisation + _entries[folder].clear(); + + for (size_t i = 0; i < n; i++) { + TQString key; + TDEWallet::Wallet::EntryType et = TDEWallet::Wallet::Unknown; + Entry *e = new Entry; + eStream >> key; + TQ_INT32 x = 0; // necessary to read properly + eStream >> x; + et = static_cast(x); + + switch (et) { + case TDEWallet::Wallet::Password: + case TDEWallet::Wallet::Stream: + case TDEWallet::Wallet::Map: + break; + default: // Unknown entry + delete e; + continue; + } + + TQByteArray a; + eStream >> a; + e->setValue(a); + e->setType(et); + e->setKey(key); + _entries[folder][key] = e; + } + } + + _open = true; + return 0; +} + + +int Backend::sync(const TQByteArray& password) { + if (!_open) { + return -255; // not open yet + } + + KSaveFile sf(_path, 0600); + TQFile *qf = sf.file(); + + if (!qf) { + sf.abort(); + return -1; // error opening file + } + + qf->writeBlock(KWMAGIC, KWMAGIC_LEN); + + // Write the version number + TQByteArray version(4); + version[0] = TDEWALLET_VERSION_MAJOR; + version[1] = TDEWALLET_VERSION_MINOR; + version[2] = TDEWALLET_CIPHER_BLOWFISH_CBC; + version[3] = TDEWALLET_HASH_SHA1; + qf->writeBlock(version, 4); + + // Holds the hashes we write out + TQByteArray hashes; + TQDataStream hashStream(hashes, IO_WriteOnly); + KMD5 md5; + hashStream << static_cast(_entries.count()); + + // Holds decrypted data prior to encryption + TQByteArray decrypted; + + // FIXME: we should estimate the amount of data we will write in each + // buffer and resize them approximately in order to avoid extra + // resizes. + + // populate decrypted + TQDataStream dStream(decrypted, IO_WriteOnly); + for (FolderMap::ConstIterator i = _entries.begin(); i != _entries.end(); ++i) { + dStream << i.key(); + dStream << static_cast(i.data().count()); + + md5.reset(); + md5.update(i.key().utf8()); + hashStream.writeRawBytes(reinterpret_cast(&(md5.rawDigest()[0])), 16); + hashStream << static_cast(i.data().count()); + + for (EntryMap::ConstIterator j = i.data().begin(); j != i.data().end(); ++j) { + dStream << j.key(); + dStream << static_cast(j.data()->type()); + dStream << j.data()->value(); + + md5.reset(); + md5.update(j.key().utf8()); + hashStream.writeRawBytes(reinterpret_cast(&(md5.rawDigest()[0])), 16); + } + } + + qf->writeBlock(hashes, hashes.size()); + + // calculate the hash of the file + SHA1 sha; + BlowFish _bf; + CipherBlockChain bf(&_bf); + + sha.process(decrypted.data(), decrypted.size()); + + // prepend and append the random data + TQByteArray wholeFile; + long blksz = bf.blockSize(); + long newsize = decrypted.size() + + blksz + // encrypted block + 4 + // file size + 20; // size of the SHA hash + + int delta = (blksz - (newsize % blksz)); + newsize += delta; + wholeFile.resize(newsize); + + TQByteArray randBlock; + randBlock.resize(blksz+delta); + if (getRandomBlock(randBlock) < 0) { + sha.reset(); + decrypted.fill(0); + sf.abort(); + return -3; // Fatal error: can't get random + } + + for (int i = 0; i < blksz; i++) { + wholeFile[i] = randBlock[i]; + } + + for (int i = 0; i < 4; i++) { + wholeFile[(int)(i+blksz)] = (decrypted.size() >> 8*(3-i))&0xff; + } + + for (unsigned int i = 0; i < decrypted.size(); i++) { + wholeFile[(int)(i+blksz+4)] = decrypted[i]; + } + + for (int i = 0; i < delta; i++) { + wholeFile[(int)(i+blksz+4+decrypted.size())] = randBlock[(int)(i+blksz)]; + } + + const char *hash = (const char *)sha.hash(); + for (int i = 0; i < 20; i++) { + wholeFile[(int)(newsize - 20 + i)] = hash[i]; + } + + sha.reset(); + decrypted.fill(0); + + // hash the passphrase + TQByteArray passhash; + password2hash(password, passhash); + + // encrypt the data + if (!bf.setKey(passhash.data(), passhash.size() * 8)) { + passhash.fill(0); + wholeFile.fill(0); + sf.abort(); + return -2; + } + + int rc = bf.encrypt(wholeFile.data(), wholeFile.size()); + if (rc < 0) { + passhash.fill(0); + wholeFile.fill(0); + sf.abort(); + return -2; // encrypt error + } + + passhash.fill(0); // passhash is UNUSABLE NOW + + // write the file + qf->writeBlock(wholeFile, wholeFile.size()); + if (!sf.close()) { + wholeFile.fill(0); + sf.abort(); + return -4; // write error + } + + wholeFile.fill(0); + +return 0; +} + + +int Backend::close(const TQByteArray& password) { + int rc = sync(password); + _open = false; + if (rc != 0) { + return rc; + } + return close(); +} + + +const TQString& Backend::walletName() const { + return _name; +} + + +bool Backend::isOpen() const { + return _open; +} + + +TQStringList Backend::folderList() const { + return _entries.keys(); +} + + +TQStringList Backend::entryList() const { + return _entries[_folder].keys(); +} + + +Entry *Backend::readEntry(const TQString& key) { +Entry *rc = 0L; + + if (_open && hasEntry(key)) { + rc = _entries[_folder][key]; + } + +return rc; +} + + +TQPtrList Backend::readEntryList(const TQString& key) { + TQPtrList rc; + + if (!_open) { + return rc; + } + + TQRegExp re(key, true, true); + + const EntryMap& map = _entries[_folder]; + for (EntryMap::ConstIterator i = map.begin(); i != map.end(); ++i) { + if (re.exactMatch(i.key())) { + rc.append(i.data()); + } + } + return rc; +} + + +bool Backend::createFolder(const TQString& f) { + if (_entries.contains(f)) { + return false; + } + + _entries.insert(f, EntryMap()); + + KMD5 folderMd5; + folderMd5.update(f.utf8()); + _hashes.insert(MD5Digest(folderMd5.rawDigest()), TQValueList()); + +return true; +} + + +int Backend::renameEntry(const TQString& oldName, const TQString& newName) { +EntryMap& emap = _entries[_folder]; +EntryMap::Iterator oi = emap.find(oldName); +EntryMap::Iterator ni = emap.find(newName); + + if (oi != emap.end() && ni == emap.end()) { + Entry *e = oi.data(); + emap.remove(oi); + emap[newName] = e; + + KMD5 folderMd5; + folderMd5.update(_folder.utf8()); + + HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); + if (i != _hashes.end()) { + KMD5 oldMd5, newMd5; + oldMd5.update(oldName.utf8()); + newMd5.update(newName.utf8()); + i.data().remove(MD5Digest(oldMd5.rawDigest())); + i.data().append(MD5Digest(newMd5.rawDigest())); + } + return 0; + } + +return -1; +} + + +void Backend::writeEntry(Entry *e) { + if (!_open) + return; + + if (!hasEntry(e->key())) { + _entries[_folder][e->key()] = new Entry; + } + _entries[_folder][e->key()]->copy(e); + + KMD5 folderMd5; + folderMd5.update(_folder.utf8()); + + HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); + if (i != _hashes.end()) { + KMD5 md5; + md5.update(e->key().utf8()); + i.data().append(MD5Digest(md5.rawDigest())); + } +} + + +bool Backend::hasEntry(const TQString& key) const { + return _entries.contains(_folder) && _entries[_folder].contains(key); +} + + +bool Backend::removeEntry(const TQString& key) { + if (!_open) { + return false; + } + + FolderMap::Iterator fi = _entries.find(_folder); + EntryMap::Iterator ei = fi.data().find(key); + + if (fi != _entries.end() && ei != fi.data().end()) { + delete ei.data(); + fi.data().remove(ei); + KMD5 folderMd5; + folderMd5.update(_folder.utf8()); + + HashMap::iterator i = _hashes.find(MD5Digest(folderMd5.rawDigest())); + if (i != _hashes.end()) { + KMD5 md5; + md5.update(key.utf8()); + i.data().remove(MD5Digest(md5.rawDigest())); + } + return true; + } + +return false; +} + + +bool Backend::removeFolder(const TQString& f) { + if (!_open) { + return false; + } + + FolderMap::Iterator fi = _entries.find(f); + + if (fi != _entries.end()) { + if (_folder == f) { + _folder = TQString::null; + } + + for (EntryMap::Iterator ei = fi.data().begin(); ei != fi.data().end(); ++ei) { + delete ei.data(); + } + + _entries.remove(fi); + + KMD5 folderMd5; + folderMd5.update(f.utf8()); + _hashes.erase(MD5Digest(folderMd5.rawDigest())); + return true; + } + +return false; +} + + +bool Backend::folderDoesNotExist(const TQString& folder) const { + KMD5 md5; + md5.update(folder.utf8()); + return !_hashes.contains(MD5Digest(md5.rawDigest())); +} + + +bool Backend::entryDoesNotExist(const TQString& folder, const TQString& entry) const { + KMD5 md5; + md5.update(folder.utf8()); + HashMap::const_iterator i = _hashes.find(MD5Digest(md5.rawDigest())); + if (i != _hashes.end()) { + md5.reset(); + md5.update(entry.utf8()); + return !i.data().contains(MD5Digest(md5.rawDigest())); + } + return true; +} + + diff --git a/tdewallet/backend/tdewalletentry.cc b/tdewallet/backend/tdewalletentry.cc deleted file mode 100644 index d6b3aa84e..000000000 --- a/tdewallet/backend/tdewalletentry.cc +++ /dev/null @@ -1,89 +0,0 @@ -/* This file is part of the KDE project - * - * Copyright (C) 2001-2003 George Staikos - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#include "tdewalletentry.h" - - -using namespace TDEWallet; - - -Entry::Entry() { -} - -Entry::~Entry() { - _value.fill(0); -} - -const TQString& Entry::key() const { - return _key; -} - - -const TQByteArray& Entry::value() const { - return _value; -} - - -TQString Entry::password() const { -TQString x; - TQDataStream qds(_value, IO_ReadOnly); - qds >> x; - return x; -} - - -void Entry::setValue(const TQByteArray& val) { - // do a direct copy from one into the other without - // temporary variables - _value.fill(0); - _value.duplicate(val); -} - - -void Entry::setValue(const TQString& val) { - _value.fill(0); - TQDataStream qds(_value, IO_WriteOnly); - qds << val; -} - - -void Entry::setKey(const TQString& key) { - _key = key; -} - - -Wallet::EntryType Entry::type() const { - return _type; -} - - -void Entry::setType(Wallet::EntryType type) { - _type = type; -} - - -void Entry::copy(const Entry* x) { - _type = x->_type; - _key = x->_key; - _value.fill(0); - _value.duplicate(x->_value); -} - - diff --git a/tdewallet/backend/tdewalletentry.cpp b/tdewallet/backend/tdewalletentry.cpp new file mode 100644 index 000000000..d6b3aa84e --- /dev/null +++ b/tdewallet/backend/tdewalletentry.cpp @@ -0,0 +1,89 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2003 George Staikos + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "tdewalletentry.h" + + +using namespace TDEWallet; + + +Entry::Entry() { +} + +Entry::~Entry() { + _value.fill(0); +} + +const TQString& Entry::key() const { + return _key; +} + + +const TQByteArray& Entry::value() const { + return _value; +} + + +TQString Entry::password() const { +TQString x; + TQDataStream qds(_value, IO_ReadOnly); + qds >> x; + return x; +} + + +void Entry::setValue(const TQByteArray& val) { + // do a direct copy from one into the other without + // temporary variables + _value.fill(0); + _value.duplicate(val); +} + + +void Entry::setValue(const TQString& val) { + _value.fill(0); + TQDataStream qds(_value, IO_WriteOnly); + qds << val; +} + + +void Entry::setKey(const TQString& key) { + _key = key; +} + + +Wallet::EntryType Entry::type() const { + return _type; +} + + +void Entry::setType(Wallet::EntryType type) { + _type = type; +} + + +void Entry::copy(const Entry* x) { + _type = x->_type; + _key = x->_key; + _value.fill(0); + _value.duplicate(x->_value); +} + + diff --git a/tdewallet/backend/tests/CMakeLists.txt b/tdewallet/backend/tests/CMakeLists.txt index abc703a7c..129377791 100644 --- a/tdewallet/backend/tests/CMakeLists.txt +++ b/tdewallet/backend/tests/CMakeLists.txt @@ -31,8 +31,8 @@ add_test( NAME "tdewallet/backed/backendtest" COMMAND sh -x "${CMAKE_CURRENT_SOURCE_DIR}/backendtest.sh" ) set_tests_properties( "tdewallet/backed/backendtest" PROPERTIES TIMEOUT 30) tde_add_check_executable( testbf AUTOMOC - SOURCES testbf.cpp ../blockcipher.cc ../blowfish.cc + SOURCES testbf.cpp ../blockcipher.cpp ../blowfish.cpp LINK tdewalletbackend-shared tdewalletclient-shared TEST ) tde_add_check_executable( testsha AUTOMOC - SOURCES testsha.cpp ../sha1.cc + SOURCES testsha.cpp ../sha1.cpp LINK tdewalletbackend-shared tdewalletclient-shared TEST ) diff --git a/tdewallet/client/CMakeLists.txt b/tdewallet/client/CMakeLists.txt index ac56830e8..87c77132b 100644 --- a/tdewallet/client/CMakeLists.txt +++ b/tdewallet/client/CMakeLists.txt @@ -34,7 +34,7 @@ install( FILES set( target tdewalletclient ) set( ${target}_SRCS - tdewallet.skel tdewallet.cc + tdewallet.skel tdewallet.cpp ) tde_add_library( ${target} SHARED AUTOMOC diff --git a/tdewallet/client/Makefile.am b/tdewallet/client/Makefile.am index d10938a4f..3ba292002 100644 --- a/tdewallet/client/Makefile.am +++ b/tdewallet/client/Makefile.am @@ -6,7 +6,7 @@ lib_LTLIBRARIES = libtdewalletclient.la libtdewalletclient_la_LDFLAGS = $(KDE_RPATH) $(all_libraries) -version-info 1:1 -no-undefined libtdewalletclient_la_LIBADD = $(LIB_TDECORE) $(LIB_QT) $(top_builddir)/dcop/libDCOP.la libtdewalletclient_la_SOURCES = tdewallet.skel \ - tdewallet.cc + tdewallet.cpp libtdewalletclient_la_METASOURCES = AUTO diff --git a/tdewallet/client/tdewallet.cc b/tdewallet/client/tdewallet.cc deleted file mode 100644 index c730f10d4..000000000 --- a/tdewallet/client/tdewallet.cc +++ /dev/null @@ -1,713 +0,0 @@ -/* This file is part of the KDE project - * - * Copyright (C) 2002-2004 George Staikos - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; see the file COPYING.LIB. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#include "tdewallettypes.h" -#include "tdewallet.h" -#include -#include -#include -#include -#include -#include -#include - -#include - -using namespace TDEWallet; - - -const TQString Wallet::LocalWallet() { - TDEConfig cfg("tdewalletrc", true); - cfg.setGroup("Wallet"); - if (!cfg.readBoolEntry("Use One Wallet", true)) { - TQString tmp = cfg.readEntry("Local Wallet", "localwallet"); - if (tmp.isEmpty()) { - return "localwallet"; - } - return tmp; - } - - TQString tmp = cfg.readEntry("Default Wallet", "kdewallet"); - if (tmp.isEmpty()) { - return "kdewallet"; - } - return tmp; -} - -const TQString Wallet::NetworkWallet() { - TDEConfig cfg("tdewalletrc", true); - cfg.setGroup("Wallet"); - - TQString tmp = cfg.readEntry("Default Wallet", "kdewallet"); - if (tmp.isEmpty()) { - return "kdewallet"; - } - return tmp; -} - -const TQString Wallet::PasswordFolder() { - return "Passwords"; -} - -const TQString Wallet::FormDataFolder() { - return "Form Data"; -} - - - -Wallet::Wallet(int handle, const TQString& name) -: TQObject(0L), DCOPObject(), d(0L), _name(name), _handle(handle) { - - _dcopRef = new DCOPRef("kded", "tdewalletd"); - - _dcopRef->dcopClient()->setNotifications(true); - connect(_dcopRef->dcopClient(), - TQT_SIGNAL(applicationRemoved(const TQCString&)), - this, - TQT_SLOT(slotAppUnregistered(const TQCString&))); - - connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "walletClosed(int)", "slotWalletClosed(int)", false); - connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderListUpdated(TQString)", "slotFolderListUpdated(TQString)", false); - connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderUpdated(TQString, TQString)", "slotFolderUpdated(TQString, TQString)", false); - connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "applicationDisconnected(TQString, TQCString)", "slotApplicationDisconnected(TQString, TQCString)", false); - - // Verify that the wallet is still open - if (_handle != -1) { - DCOPReply r = _dcopRef->call("isOpen", _handle); - if (r.isValid()) { - bool rc = false; - r.get(rc); - if (!rc) { - _handle = -1; - _name = TQString::null; - } - } - } -} - - -Wallet::~Wallet() { - if (_handle != -1) { - _dcopRef->call("close", _handle, false); - _handle = -1; - _folder = TQString::null; - _name = TQString::null; - } - - delete _dcopRef; - _dcopRef = 0L; -} - - -TQStringList Wallet::walletList() { - DCOPReply r = DCOPRef("kded", "tdewalletd").call("wallets"); - TQStringList rc; - if (r.isValid()) { - r.get(rc); - } - return rc; -} - - -void Wallet::changePassword(const TQString& name, WId w) { - DCOPRef("kded", "tdewalletd").send("changePassword", name, uint(w)); -} - - -bool Wallet::isEnabled() { - DCOPReply r = DCOPRef("kded", "tdewalletd").call("isEnabled"); - bool rc = false; - if (r.isValid()) { - r.get(rc); - } - return rc; -} - - -bool Wallet::isOpen(const TQString& name) { - DCOPReply r = DCOPRef("kded", "tdewalletd").call("isOpen", name); - bool rc = false; - if (r.isValid()) { - r.get(rc); - } - return rc; -} - - -int Wallet::closeWallet(const TQString& name, bool force) { - DCOPReply r = DCOPRef("kded", "tdewalletd").call("close", name, force); - int rc = -1; - if (r.isValid()) { - r.get(rc); - } - return rc; -} - - -int Wallet::deleteWallet(const TQString& name) { - DCOPReply r = DCOPRef("kded", "tdewalletd").call("deleteWallet", name); - int rc = -1; - if (r.isValid()) { - r.get(rc); - } - return rc; -} - - -Wallet *Wallet::openWallet(const TQString& name, WId w, OpenType ot) { - if (ot == Asynchronous) { - Wallet *wallet = new Wallet(-1, name); - DCOPRef("kded", "tdewalletd").send("openAsynchronous", name, wallet->objId(), uint(w)); - return wallet; - } - - // avoid deadlock if the app has some popup open (#65978/#71048) - while( TQWidget* widget = TQT_TQWIDGET(tqApp->activePopupWidget())) - widget->close(); - - bool isPath = ot == Path; - DCOPReply r; - - if (isPath) { - r = DCOPRef("kded", "tdewalletd").call("openPath", name, uint(w)); - } else { - r = DCOPRef("kded", "tdewalletd").call("open", name, uint(w)); - } - - if (r.isValid()) { - int drc = -1; - r.get(drc); - if (drc != -1) { - return new Wallet(drc, name); - } - } - - return 0; -} - - -bool Wallet::disconnectApplication(const TQString& wallet, const TQCString& app) { - DCOPReply r = DCOPRef("kded", "tdewalletd").call("disconnectApplication", wallet, app); - bool rc = false; - if (r.isValid()) { - r.get(rc); - } - return rc; -} - - -TQStringList Wallet::users(const TQString& name) { - DCOPReply r = DCOPRef("kded", "tdewalletd").call("users", name); - TQStringList drc; - if (r.isValid()) { - r.get(drc); - } - return drc; -} - - -int Wallet::sync() { - if (_handle == -1) { - return -1; - } - - _dcopRef->call("sync", _handle); - return 0; -} - - -int Wallet::lockWallet() { - if (_handle == -1) { - return -1; - } - - DCOPReply r = _dcopRef->call("close", _handle, true); - _handle = -1; - _folder = TQString::null; - _name = TQString::null; - if (r.isValid()) { - int drc = -1; - r.get(drc); - return drc; - } - return -1; -} - - -const TQString& Wallet::walletName() const { - return _name; -} - - -bool Wallet::isOpen() const { - return _handle != -1; -} - - -void Wallet::requestChangePassword(WId w) { - if (_handle == -1) { - return; - } - - _dcopRef->send("changePassword", _name, uint(w)); -} - - -void Wallet::slotWalletClosed(int handle) { - if (_handle == handle) { - _handle = -1; - _folder = TQString::null; - _name = TQString::null; - emit walletClosed(); - } -} - - -TQStringList Wallet::folderList() { - TQStringList rc; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("folderList", _handle); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -TQStringList Wallet::entryList() { - TQStringList rc; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("entryList", _handle, _folder); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -bool Wallet::hasFolder(const TQString& f) { - bool rc = false; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("hasFolder", _handle, f); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -bool Wallet::createFolder(const TQString& f) { - bool rc = true; - - if (_handle == -1) { - return false; - } - - if (!hasFolder(f)) { - DCOPReply r = _dcopRef->call("createFolder", _handle, f); - if (r.isValid()) { - r.get(rc); - } - } - - return rc; -} - - -bool Wallet::setFolder(const TQString& f) { - bool rc = false; - - if (_handle == -1) { - return rc; - } - - // Don't do this - the folder could have disappeared? -#if 0 - if (f == _folder) { - return true; - } -#endif - - if (hasFolder(f)) { - _folder = f; - rc = true; - } - - return rc; -} - - -bool Wallet::removeFolder(const TQString& f) { - bool rc = false; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("removeFolder", _handle, f); - if (r.isValid()) { - r.get(rc); - } - - if (_folder == f) { - setFolder(TQString::null); - } - - return rc; -} - - -const TQString& Wallet::currentFolder() const { - return _folder; -} - - -int Wallet::readEntry(const TQString& key, TQByteArray& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("readEntry", _handle, _folder, key); - if (r.isValid()) { - r.get(value); - rc = 0; - } - - return rc; -} - - -int Wallet::readEntryList(const TQString& key, TQMap& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("readEntryList", _handle, _folder, key); - if (r.isValid()) { - r.get(value); - rc = 0; - } - - return rc; -} - - -int Wallet::renameEntry(const TQString& oldName, const TQString& newName) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("renameEntry", _handle, _folder, oldName, newName); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -int Wallet::readMap(const TQString& key, TQMap& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("readMap", _handle, _folder, key); - if (r.isValid()) { - TQByteArray v; - r.get(v); - if (!v.isEmpty()) { - TQDataStream ds(v, IO_ReadOnly); - ds >> value; - } - rc = 0; - } - - return rc; -} - - -int Wallet::readMapList(const TQString& key, TQMap >& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("readMapList", _handle, _folder, key); - if (r.isValid()) { - TQMap unparsed; - r.get(unparsed); - for (TQMap::ConstIterator i = unparsed.begin(); i != unparsed.end(); ++i) { - if (!i.data().isEmpty()) { - TQDataStream ds(i.data(), IO_ReadOnly); - TQMap v; - ds >> v; - value.insert(i.key(), v); - } - } - rc = 0; - } - - return rc; -} - - -int Wallet::readPassword(const TQString& key, TQString& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("readPassword", _handle, _folder, key); - if (r.isValid()) { - r.get(value); - rc = 0; - } - - return rc; -} - - -int Wallet::readPasswordList(const TQString& key, TQMap& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("readPasswordList", _handle, _folder, key); - if (r.isValid()) { - r.get(value); - rc = 0; - } - - return rc; -} - - -int Wallet::writeEntry(const TQString& key, const TQByteArray& value, EntryType entryType) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value, int(entryType)); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -int Wallet::writeEntry(const TQString& key, const TQByteArray& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -int Wallet::writeMap(const TQString& key, const TQMap& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - TQByteArray a; - TQDataStream ds(a, IO_WriteOnly); - ds << value; - DCOPReply r = _dcopRef->call("writeMap", _handle, _folder, key, a); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -int Wallet::writePassword(const TQString& key, const TQString& value) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("writePassword", _handle, _folder, key, value); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -bool Wallet::hasEntry(const TQString& key) { - bool rc = false; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("hasEntry", _handle, _folder, key); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -int Wallet::removeEntry(const TQString& key) { - int rc = -1; - - if (_handle == -1) { - return rc; - } - - DCOPReply r = _dcopRef->call("removeEntry", _handle, _folder, key); - if (r.isValid()) { - r.get(rc); - } - - return rc; -} - - -Wallet::EntryType Wallet::entryType(const TQString& key) { - int rc = 0; - - if (_handle == -1) { - return Wallet::Unknown; - } - - DCOPReply r = _dcopRef->call("entryType", _handle, _folder, key); - if (r.isValid()) { - r.get(rc); - } - - return static_cast(rc); -} - - -void Wallet::slotAppUnregistered(const TQCString& app) { - if (_handle >= 0 && app == "kded") { - slotWalletClosed(_handle); - } -} - - -void Wallet::slotFolderUpdated(const TQString& wallet, const TQString& folder) { - if (_name == wallet) { - emit folderUpdated(folder); - } -} - - -void Wallet::slotFolderListUpdated(const TQString& wallet) { - if (_name == wallet) { - emit folderListUpdated(); - } -} - - -void Wallet::slotApplicationDisconnected(const TQString& wallet, const TQCString& application) { - if (_handle >= 0 - && _name == wallet - && application == _dcopRef->dcopClient()->appId()) { - slotWalletClosed(_handle); - } -} - - -void Wallet::walletOpenResult(int id) { - if (_handle != -1) { - // This is BAD. - return; - } - - if (id > 0) { - _handle = id; - emit walletOpened(true); - } else if (id < 0) { - emit walletOpened(false); - } // id == 0 => wait -} - - -bool Wallet::folderDoesNotExist(const TQString& wallet, const TQString& folder) { -DCOPReply r = DCOPRef("kded", "tdewalletd").call("folderDoesNotExist", wallet, folder); -bool rc = true; - if (r.isValid()) { - r.get(rc); - } -return rc; -} - - -bool Wallet::keyDoesNotExist(const TQString& wallet, const TQString& folder, const TQString& key) { -DCOPReply r = DCOPRef("kded", "tdewalletd").call("keyDoesNotExist", wallet, folder, key); -bool rc = true; - if (r.isValid()) { - r.get(rc); - } -return rc; -} - - -void Wallet::virtual_hook(int, void*) { - //BASE::virtual_hook( id, data ); -} - -#include "tdewallet.moc" diff --git a/tdewallet/client/tdewallet.cpp b/tdewallet/client/tdewallet.cpp new file mode 100644 index 000000000..c730f10d4 --- /dev/null +++ b/tdewallet/client/tdewallet.cpp @@ -0,0 +1,713 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2002-2004 George Staikos + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "tdewallettypes.h" +#include "tdewallet.h" +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace TDEWallet; + + +const TQString Wallet::LocalWallet() { + TDEConfig cfg("tdewalletrc", true); + cfg.setGroup("Wallet"); + if (!cfg.readBoolEntry("Use One Wallet", true)) { + TQString tmp = cfg.readEntry("Local Wallet", "localwallet"); + if (tmp.isEmpty()) { + return "localwallet"; + } + return tmp; + } + + TQString tmp = cfg.readEntry("Default Wallet", "kdewallet"); + if (tmp.isEmpty()) { + return "kdewallet"; + } + return tmp; +} + +const TQString Wallet::NetworkWallet() { + TDEConfig cfg("tdewalletrc", true); + cfg.setGroup("Wallet"); + + TQString tmp = cfg.readEntry("Default Wallet", "kdewallet"); + if (tmp.isEmpty()) { + return "kdewallet"; + } + return tmp; +} + +const TQString Wallet::PasswordFolder() { + return "Passwords"; +} + +const TQString Wallet::FormDataFolder() { + return "Form Data"; +} + + + +Wallet::Wallet(int handle, const TQString& name) +: TQObject(0L), DCOPObject(), d(0L), _name(name), _handle(handle) { + + _dcopRef = new DCOPRef("kded", "tdewalletd"); + + _dcopRef->dcopClient()->setNotifications(true); + connect(_dcopRef->dcopClient(), + TQT_SIGNAL(applicationRemoved(const TQCString&)), + this, + TQT_SLOT(slotAppUnregistered(const TQCString&))); + + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "walletClosed(int)", "slotWalletClosed(int)", false); + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderListUpdated(TQString)", "slotFolderListUpdated(TQString)", false); + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderUpdated(TQString, TQString)", "slotFolderUpdated(TQString, TQString)", false); + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "applicationDisconnected(TQString, TQCString)", "slotApplicationDisconnected(TQString, TQCString)", false); + + // Verify that the wallet is still open + if (_handle != -1) { + DCOPReply r = _dcopRef->call("isOpen", _handle); + if (r.isValid()) { + bool rc = false; + r.get(rc); + if (!rc) { + _handle = -1; + _name = TQString::null; + } + } + } +} + + +Wallet::~Wallet() { + if (_handle != -1) { + _dcopRef->call("close", _handle, false); + _handle = -1; + _folder = TQString::null; + _name = TQString::null; + } + + delete _dcopRef; + _dcopRef = 0L; +} + + +TQStringList Wallet::walletList() { + DCOPReply r = DCOPRef("kded", "tdewalletd").call("wallets"); + TQStringList rc; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +void Wallet::changePassword(const TQString& name, WId w) { + DCOPRef("kded", "tdewalletd").send("changePassword", name, uint(w)); +} + + +bool Wallet::isEnabled() { + DCOPReply r = DCOPRef("kded", "tdewalletd").call("isEnabled"); + bool rc = false; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +bool Wallet::isOpen(const TQString& name) { + DCOPReply r = DCOPRef("kded", "tdewalletd").call("isOpen", name); + bool rc = false; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +int Wallet::closeWallet(const TQString& name, bool force) { + DCOPReply r = DCOPRef("kded", "tdewalletd").call("close", name, force); + int rc = -1; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +int Wallet::deleteWallet(const TQString& name) { + DCOPReply r = DCOPRef("kded", "tdewalletd").call("deleteWallet", name); + int rc = -1; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +Wallet *Wallet::openWallet(const TQString& name, WId w, OpenType ot) { + if (ot == Asynchronous) { + Wallet *wallet = new Wallet(-1, name); + DCOPRef("kded", "tdewalletd").send("openAsynchronous", name, wallet->objId(), uint(w)); + return wallet; + } + + // avoid deadlock if the app has some popup open (#65978/#71048) + while( TQWidget* widget = TQT_TQWIDGET(tqApp->activePopupWidget())) + widget->close(); + + bool isPath = ot == Path; + DCOPReply r; + + if (isPath) { + r = DCOPRef("kded", "tdewalletd").call("openPath", name, uint(w)); + } else { + r = DCOPRef("kded", "tdewalletd").call("open", name, uint(w)); + } + + if (r.isValid()) { + int drc = -1; + r.get(drc); + if (drc != -1) { + return new Wallet(drc, name); + } + } + + return 0; +} + + +bool Wallet::disconnectApplication(const TQString& wallet, const TQCString& app) { + DCOPReply r = DCOPRef("kded", "tdewalletd").call("disconnectApplication", wallet, app); + bool rc = false; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +TQStringList Wallet::users(const TQString& name) { + DCOPReply r = DCOPRef("kded", "tdewalletd").call("users", name); + TQStringList drc; + if (r.isValid()) { + r.get(drc); + } + return drc; +} + + +int Wallet::sync() { + if (_handle == -1) { + return -1; + } + + _dcopRef->call("sync", _handle); + return 0; +} + + +int Wallet::lockWallet() { + if (_handle == -1) { + return -1; + } + + DCOPReply r = _dcopRef->call("close", _handle, true); + _handle = -1; + _folder = TQString::null; + _name = TQString::null; + if (r.isValid()) { + int drc = -1; + r.get(drc); + return drc; + } + return -1; +} + + +const TQString& Wallet::walletName() const { + return _name; +} + + +bool Wallet::isOpen() const { + return _handle != -1; +} + + +void Wallet::requestChangePassword(WId w) { + if (_handle == -1) { + return; + } + + _dcopRef->send("changePassword", _name, uint(w)); +} + + +void Wallet::slotWalletClosed(int handle) { + if (_handle == handle) { + _handle = -1; + _folder = TQString::null; + _name = TQString::null; + emit walletClosed(); + } +} + + +TQStringList Wallet::folderList() { + TQStringList rc; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("folderList", _handle); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +TQStringList Wallet::entryList() { + TQStringList rc; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("entryList", _handle, _folder); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +bool Wallet::hasFolder(const TQString& f) { + bool rc = false; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("hasFolder", _handle, f); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +bool Wallet::createFolder(const TQString& f) { + bool rc = true; + + if (_handle == -1) { + return false; + } + + if (!hasFolder(f)) { + DCOPReply r = _dcopRef->call("createFolder", _handle, f); + if (r.isValid()) { + r.get(rc); + } + } + + return rc; +} + + +bool Wallet::setFolder(const TQString& f) { + bool rc = false; + + if (_handle == -1) { + return rc; + } + + // Don't do this - the folder could have disappeared? +#if 0 + if (f == _folder) { + return true; + } +#endif + + if (hasFolder(f)) { + _folder = f; + rc = true; + } + + return rc; +} + + +bool Wallet::removeFolder(const TQString& f) { + bool rc = false; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("removeFolder", _handle, f); + if (r.isValid()) { + r.get(rc); + } + + if (_folder == f) { + setFolder(TQString::null); + } + + return rc; +} + + +const TQString& Wallet::currentFolder() const { + return _folder; +} + + +int Wallet::readEntry(const TQString& key, TQByteArray& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readEntry", _handle, _folder, key); + if (r.isValid()) { + r.get(value); + rc = 0; + } + + return rc; +} + + +int Wallet::readEntryList(const TQString& key, TQMap& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readEntryList", _handle, _folder, key); + if (r.isValid()) { + r.get(value); + rc = 0; + } + + return rc; +} + + +int Wallet::renameEntry(const TQString& oldName, const TQString& newName) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("renameEntry", _handle, _folder, oldName, newName); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +int Wallet::readMap(const TQString& key, TQMap& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readMap", _handle, _folder, key); + if (r.isValid()) { + TQByteArray v; + r.get(v); + if (!v.isEmpty()) { + TQDataStream ds(v, IO_ReadOnly); + ds >> value; + } + rc = 0; + } + + return rc; +} + + +int Wallet::readMapList(const TQString& key, TQMap >& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readMapList", _handle, _folder, key); + if (r.isValid()) { + TQMap unparsed; + r.get(unparsed); + for (TQMap::ConstIterator i = unparsed.begin(); i != unparsed.end(); ++i) { + if (!i.data().isEmpty()) { + TQDataStream ds(i.data(), IO_ReadOnly); + TQMap v; + ds >> v; + value.insert(i.key(), v); + } + } + rc = 0; + } + + return rc; +} + + +int Wallet::readPassword(const TQString& key, TQString& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readPassword", _handle, _folder, key); + if (r.isValid()) { + r.get(value); + rc = 0; + } + + return rc; +} + + +int Wallet::readPasswordList(const TQString& key, TQMap& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readPasswordList", _handle, _folder, key); + if (r.isValid()) { + r.get(value); + rc = 0; + } + + return rc; +} + + +int Wallet::writeEntry(const TQString& key, const TQByteArray& value, EntryType entryType) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value, int(entryType)); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +int Wallet::writeEntry(const TQString& key, const TQByteArray& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("writeEntry", _handle, _folder, key, value); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +int Wallet::writeMap(const TQString& key, const TQMap& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + TQByteArray a; + TQDataStream ds(a, IO_WriteOnly); + ds << value; + DCOPReply r = _dcopRef->call("writeMap", _handle, _folder, key, a); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +int Wallet::writePassword(const TQString& key, const TQString& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("writePassword", _handle, _folder, key, value); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +bool Wallet::hasEntry(const TQString& key) { + bool rc = false; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("hasEntry", _handle, _folder, key); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +int Wallet::removeEntry(const TQString& key) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("removeEntry", _handle, _folder, key); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +Wallet::EntryType Wallet::entryType(const TQString& key) { + int rc = 0; + + if (_handle == -1) { + return Wallet::Unknown; + } + + DCOPReply r = _dcopRef->call("entryType", _handle, _folder, key); + if (r.isValid()) { + r.get(rc); + } + + return static_cast(rc); +} + + +void Wallet::slotAppUnregistered(const TQCString& app) { + if (_handle >= 0 && app == "kded") { + slotWalletClosed(_handle); + } +} + + +void Wallet::slotFolderUpdated(const TQString& wallet, const TQString& folder) { + if (_name == wallet) { + emit folderUpdated(folder); + } +} + + +void Wallet::slotFolderListUpdated(const TQString& wallet) { + if (_name == wallet) { + emit folderListUpdated(); + } +} + + +void Wallet::slotApplicationDisconnected(const TQString& wallet, const TQCString& application) { + if (_handle >= 0 + && _name == wallet + && application == _dcopRef->dcopClient()->appId()) { + slotWalletClosed(_handle); + } +} + + +void Wallet::walletOpenResult(int id) { + if (_handle != -1) { + // This is BAD. + return; + } + + if (id > 0) { + _handle = id; + emit walletOpened(true); + } else if (id < 0) { + emit walletOpened(false); + } // id == 0 => wait +} + + +bool Wallet::folderDoesNotExist(const TQString& wallet, const TQString& folder) { +DCOPReply r = DCOPRef("kded", "tdewalletd").call("folderDoesNotExist", wallet, folder); +bool rc = true; + if (r.isValid()) { + r.get(rc); + } +return rc; +} + + +bool Wallet::keyDoesNotExist(const TQString& wallet, const TQString& folder, const TQString& key) { +DCOPReply r = DCOPRef("kded", "tdewalletd").call("keyDoesNotExist", wallet, folder, key); +bool rc = true; + if (r.isValid()) { + r.get(rc); + } +return rc; +} + + +void Wallet::virtual_hook(int, void*) { + //BASE::virtual_hook( id, data ); +} + +#include "tdewallet.moc" -- cgit v1.2.3