diff options
Diffstat (limited to 'kwallet')
31 files changed, 4157 insertions, 0 deletions
diff --git a/kwallet/Makefile.am b/kwallet/Makefile.am new file mode 100644 index 000000000..18a2cc6e7 --- /dev/null +++ b/kwallet/Makefile.am @@ -0,0 +1,7 @@ + +SUBDIRS=client backend tests + +DOXYGEN_REFERENCES=dcop +DOXYGEN_EXCLUDE=backend +include ../admin/Doxyfile.am + diff --git a/kwallet/README b/kwallet/README new file mode 100644 index 000000000..9d662c0a9 --- /dev/null +++ b/kwallet/README @@ -0,0 +1,22 @@ + +Please read the paper at http://www.staikos.net/~staikos/papers/2003/ +It has been updated to 10/22/2003. + +----------- + +This directory consists of two libraries: kwalletbackend and kwalletclient + +KWallet::Backend is used inside kded to manage the actual files and +encryption. On the other hand, KWallet::Wallet is used to represent +wallet entries and to encapsulate communication to and from the +wallet server inside kded. + + +Application level: + +KSystemTray notifier for wallet accesses + +Konqueror plugin + +KControl module + diff --git a/kwallet/backend/Makefile.am b/kwallet/backend/Makefile.am new file mode 100644 index 000000000..9727718b5 --- /dev/null +++ b/kwallet/backend/Makefile.am @@ -0,0 +1,25 @@ + +INCLUDES = -I$(srcdir)/../client $(all_includes) + +lib_LTLIBRARIES = libkwalletbackend.la + +libkwalletbackend_la_LDFLAGS = $(KDE_RPATH) $(all_libraries) -version-info 1:0 -no-undefined +libkwalletbackend_la_LIBADD = $(LIB_QT) ../../kdecore/libkdecore.la +libkwalletbackend_la_SOURCES = blockcipher.cc \ + blowfish.cc \ + cbc.cc \ + sha1.cc \ + kwalletentry.cc \ + kwalletbackend.cc + + +libkwalletbackend_la_METASOURCES = AUTO + +noinst_HEADERS = blowfishtables.h \ + cbc.h \ + sha1.h \ + blockcipher.h \ + kwalletentry.h \ + kwalletbackend.h \ + blowfish.h + diff --git a/kwallet/backend/blockcipher.cc b/kwallet/backend/blockcipher.cc new file mode 100644 index 000000000..9d770098e --- /dev/null +++ b/kwallet/backend/blockcipher.cc @@ -0,0 +1,37 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + 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/kwallet/backend/blockcipher.h b/kwallet/backend/blockcipher.h new file mode 100644 index 000000000..aadff6e70 --- /dev/null +++ b/kwallet/backend/blockcipher.h @@ -0,0 +1,76 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + 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. +*/ + + +#ifndef __bcipher__ko__h +#define __bcipher__ko__h + + +/* @internal + */ +class BlockCipher { + public: + BlockCipher(); + virtual ~BlockCipher(); + + /* + * Return the current blocksize in bytes. + */ + int blockSize() const; + + /* + * Set the encryption key to key. Return true on success. + */ + virtual bool setKey(void *key, int bitlength) = 0; + + /* + * Get the required (or if it's variable, then the maximum) key + * length for this cipher in bits. + */ + virtual int keyLen() const = 0; + + /* + * True if the key is of a variable length. In this case, + * getKeyLen() will return the maximum length. + */ + virtual bool variableKeyLen() const = 0; + + /* + * True if all settings are good and we are ready to encrypt. + */ + virtual bool readyToGo() const = 0; + + /* + * Encrypt the block. Returns the number of bytes successfully + * encrypted. Can return -1 on error. + */ + virtual int encrypt(void *block, int len) = 0; + + /* + * Decrypt the block. Returns as does encrypt(); + */ + virtual int decrypt(void *block, int len) = 0; + + protected: + int _blksz; + int _keylen; // in bits +}; + + +#endif diff --git a/kwallet/backend/blowfish.cc b/kwallet/backend/blowfish.cc new file mode 100644 index 000000000..c708935de --- /dev/null +++ b/kwallet/backend/blowfish.cc @@ -0,0 +1,259 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + 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 <config.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#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/kwallet/backend/blowfish.h b/kwallet/backend/blowfish.h new file mode 100644 index 000000000..581ac31c0 --- /dev/null +++ b/kwallet/backend/blowfish.h @@ -0,0 +1,73 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + 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. +*/ + +#ifndef _BLOWFISH_H +#define _BLOWFISH_H + +#include <config.h> + +#ifdef HAVE_STDINT_H +#include <stdint.h> +#endif +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#ifdef HAVE_SYS_BITYPES_H +#include <sys/bitypes.h> /* For uintXX_t on Tru64 */ +#endif + +#include "blockcipher.h" + + +/* @internal + */ +class BlowFish : public BlockCipher { + public: + BlowFish(); + virtual ~BlowFish(); + + virtual bool setKey(void *key, int bitlength); + + virtual int keyLen() const; + + virtual bool variableKeyLen() const; + + virtual bool readyToGo() const; + + virtual int encrypt(void *block, int len); + + virtual int decrypt(void *block, int len); + + private: + uint32_t _S[4][256]; + uint32_t _P[18]; + + void *_key; + int _keylen; // in bits + + bool _init; + + bool init(); + uint32_t F(uint32_t x); + void encipher(uint32_t *xl, uint32_t *xr); + void decipher(uint32_t *xl, uint32_t *xr); +}; + +#endif + diff --git a/kwallet/backend/blowfishtables.h b/kwallet/backend/blowfishtables.h new file mode 100644 index 000000000..dae448f96 --- /dev/null +++ b/kwallet/backend/blowfishtables.h @@ -0,0 +1,214 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + 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. +*/ + +// This is also from _Applied_Cryptography_. See blowfish.cc for more details. + +#ifndef __bfdefs_h +#define __bfdefs_h + + +const unsigned long ks0[256] = { + 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, + 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658, + 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, + 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6, + 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, + 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, + 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, + 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176, + 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, + 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b, + 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, + 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, + 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, + 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8, + 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, + 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0, + 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, + 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, + 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, + 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9, + 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, + 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a }; + +const unsigned long ks1[256] = { + 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, + 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, + 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, + 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, + 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, + 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908, + 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, + 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, + 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, + 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, + 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, + 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5, + 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, + 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, + 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, + 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, + 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, + 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646, + 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, + 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, + 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, + 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7 }; + +const unsigned long ks2[256] = { + 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, + 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af, + 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, + 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, + 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, + 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, + 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, + 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, + 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, + 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, + 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, + 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, + 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, + 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, + 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, + 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, + 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, + 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, + 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, + 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188, + 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, + 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0 }; + +const unsigned long ks3[256] = { + 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, + 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, + 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, + 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, + 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, + 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, + 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, + 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, + 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, + 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, + 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, + 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, + 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, + 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, + 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, + 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, + 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, + 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, + 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, + 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, + 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, + 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 }; + +const unsigned long P[18] = { + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, + 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b }; + + +#endif + + diff --git a/kwallet/backend/cbc.cc b/kwallet/backend/cbc.cc new file mode 100644 index 000000000..7bc5f3891 --- /dev/null +++ b/kwallet/backend/cbc.cc @@ -0,0 +1,149 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + 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 <string.h> + + + +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/kwallet/backend/cbc.h b/kwallet/backend/cbc.h new file mode 100644 index 000000000..1ce971ab3 --- /dev/null +++ b/kwallet/backend/cbc.h @@ -0,0 +1,62 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + + 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. +*/ + + + +#ifndef __CBC__KO__H +#define __CBC__KO__H + +#include "blockcipher.h" + +/* @internal + * Initialize this class with a pointer to a valid, uninitialized BlockCipher + * and it will apply that cipher using CBC. You may want to make the + * initial block a full block of random data. Do not change the block size + * at any time!! You must pad it yourself. Also, you can only encrypt or + * decrypt. You can't do both with a given instance. After you call one, + * calls to the other will fail in this instance. + */ + +class CipherBlockChain : public BlockCipher { + public: + CipherBlockChain(BlockCipher *cipher); + virtual ~CipherBlockChain(); + + virtual bool setKey(void *key, int bitlength); + + virtual int keyLen() const; + + virtual bool variableKeyLen() const; + + virtual bool readyToGo() const; + + virtual int encrypt(void *block, int len); + + virtual int decrypt(void *block, int len); + + private: + BlockCipher *_cipher; + void *_register; + void *_next; + int _len; + int _reader, _writer; + +}; + +#endif diff --git a/kwallet/backend/kwalletbackend.cc b/kwallet/backend/kwalletbackend.cc new file mode 100644 index 000000000..5b9c29299 --- /dev/null +++ b/kwallet/backend/kwalletbackend.cc @@ -0,0 +1,838 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2004 George Staikos <staikos@kde.org> + * + * 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 "kwalletbackend.h" + +#include <stdlib.h> + +#include <kdebug.h> +#include <kglobal.h> +#include <klocale.h> +#include <kmdcodec.h> +#include <ksavefile.h> +#include <kstandarddirs.h> + +#include <qfile.h> +#include <qfileinfo.h> +#include <qregexp.h> + +#include "blowfish.h" +#include "sha1.h" +#include "cbc.h" + +#include <assert.h> + +#define KWALLET_VERSION_MAJOR 0 +#define KWALLET_VERSION_MINOR 0 + +#define KWALLET_CIPHER_BLOWFISH_CBC 0 +#define KWALLET_CIPHER_3DES_CBC 1 // unsupported + +#define KWALLET_HASH_SHA1 0 +#define KWALLET_HASH_MD5 1 // unsupported + + +using namespace KWallet; + +#define KWMAGIC "KWALLET\n\r\0\r\n" +#define KWMAGIC_LEN 12 + +static void initKWalletDir() +{ + KGlobal::dirs()->addResourceType("kwallet", "share/apps/kwallet"); +} + +Backend::Backend(const QString& name, bool isPath) : _name(name), _ref(0) { + initKWalletDir(); + if (isPath) { + _path = name; + } else { + _path = KGlobal::dirs()->saveLocation("kwallet") + "/" + _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(QByteArray& randBlock) { + // First try /dev/urandom + if (QFile::exists("/dev/urandom")) { + QFile 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 (QFile::exists("/dev/random")) { + QFile 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 (QFile::exists(randFilename)) { + QFile 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 QByteArray& password, QByteArray& hash) { + SHA1 sha; + int shasz = sha.size() / 8; + + assert(shasz >= 20); + + QByteArray block1(shasz); + + sha.process(password.data(), QMIN(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, QMIN(password.size() - 16, 16)); + QByteArray 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, QMIN(password.size() - 32, 16)); + + QByteArray 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); + + QByteArray 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 QString& wallet) { + initKWalletDir(); + QString path = KGlobal::dirs()->saveLocation("kwallet") + "/" + wallet + ".kwl"; + // Note: 60 bytes is presently the minimum size of a wallet file. + // Anything smaller is junk. +return QFile::exists(path) && QFileInfo(path).size() >= 60; +} + + +QString 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 QString::null; + } +} + + +int Backend::open(const QByteArray& password) { + + if (_open) { + return -255; // already open + } + + QByteArray 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 (!QFile::exists(_path) || QFileInfo(_path).size() < 60) { + QFile 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 + } + + QFile 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] != KWALLET_VERSION_MAJOR) { + return -4; // unknown version + } + + if (magicBuf[1] != KWALLET_VERSION_MINOR) { + return -4; // unknown version + } + + if (magicBuf[2] != KWALLET_CIPHER_BLOWFISH_CBC) { + return -42; // unknown cipher + } + + if (magicBuf[3] != KWALLET_HASH_SHA1) { + return -42; // unknown hash + } + + _hashes.clear(); + // Read in the hashes + QDataStream hds(&db); + Q_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; + QMap<MD5Digest,QValueList<MD5Digest> >::iterator it; + Q_UINT32 fsz; + if (hds.atEnd()) return -43; + hds.readRawBytes(reinterpret_cast<char *>(d), 16); + hds >> fsz; + ba.duplicate(reinterpret_cast<char *>(d), 16); + it = _hashes.insert(ba, QValueList<MD5Digest>()); + for (size_t j = 0; j < fsz; ++j) { + hds.readRawBytes(reinterpret_cast<char *>(d2), 16); + ba.duplicate(reinterpret_cast<char *>(d2), 16); + (*it).append(ba); + } + } + + // Read in the rest of the file. + QByteArray 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] != encrypted[sz - 20 + i]) { + encrypted.fill(0); + sha.reset(); + return -8; // hash error. + } + } + + sha.reset(); + + // chop off the leading blksz+4 bytes + QByteArray 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 + QDataStream eStream(encrypted, IO_ReadOnly); + + while (!eStream.atEnd()) { + QString folder; + Q_UINT32 n; + + eStream >> folder; + eStream >> n; + + // Force initialisation + _entries[folder].clear(); + + for (size_t i = 0; i < n; i++) { + QString key; + KWallet::Wallet::EntryType et = KWallet::Wallet::Unknown; + Entry *e = new Entry; + eStream >> key; + Q_INT32 x = 0; // necessary to read properly + eStream >> x; + et = static_cast<KWallet::Wallet::EntryType>(x); + + switch (et) { + case KWallet::Wallet::Password: + case KWallet::Wallet::Stream: + case KWallet::Wallet::Map: + break; + default: // Unknown entry + delete e; + continue; + } + + QByteArray a; + eStream >> a; + e->setValue(a); + e->setType(et); + e->setKey(key); + _entries[folder][key] = e; + } + } + + _open = true; + return 0; +} + + +int Backend::sync(const QByteArray& password) { + if (!_open) { + return -255; // not open yet + } + + KSaveFile sf(_path, 0600); + QFile *qf = sf.file(); + + if (!qf) { + sf.abort(); + return -1; // error opening file + } + + qf->writeBlock(KWMAGIC, KWMAGIC_LEN); + + // Write the version number + QByteArray version(4); + version[0] = KWALLET_VERSION_MAJOR; + version[1] = KWALLET_VERSION_MINOR; + version[2] = KWALLET_CIPHER_BLOWFISH_CBC; + version[3] = KWALLET_HASH_SHA1; + qf->writeBlock(version, 4); + + // Holds the hashes we write out + QByteArray hashes; + QDataStream hashStream(hashes, IO_WriteOnly); + KMD5 md5; + hashStream << static_cast<Q_UINT32>(_entries.count()); + + // Holds decrypted data prior to encryption + QByteArray 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 + QDataStream dStream(decrypted, IO_WriteOnly); + for (FolderMap::ConstIterator i = _entries.begin(); i != _entries.end(); ++i) { + dStream << i.key(); + dStream << static_cast<Q_UINT32>(i.data().count()); + + md5.reset(); + md5.update(i.key().utf8()); + hashStream.writeRawBytes(reinterpret_cast<const char*>(&(md5.rawDigest()[0])), 16); + hashStream << static_cast<Q_UINT32>(i.data().count()); + + for (EntryMap::ConstIterator j = i.data().begin(); j != i.data().end(); ++j) { + dStream << j.key(); + dStream << static_cast<Q_INT32>(j.data()->type()); + dStream << j.data()->value(); + + md5.reset(); + md5.update(j.key().utf8()); + hashStream.writeRawBytes(reinterpret_cast<const char*>(&(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 + QByteArray 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); + + QByteArray 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 + QByteArray 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 QByteArray& password) { + int rc = sync(password); + _open = false; + if (rc != 0) { + return rc; + } + return close(); +} + + +const QString& Backend::walletName() const { + return _name; +} + + +bool Backend::isOpen() const { + return _open; +} + + +QStringList Backend::folderList() const { + return _entries.keys(); +} + + +QStringList Backend::entryList() const { + return _entries[_folder].keys(); +} + + +Entry *Backend::readEntry(const QString& key) { +Entry *rc = 0L; + + if (_open && hasEntry(key)) { + rc = _entries[_folder][key]; + } + +return rc; +} + + +QPtrList<Entry> Backend::readEntryList(const QString& key) { + QPtrList<Entry> rc; + + if (!_open) { + return rc; + } + + QRegExp 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 QString& f) { + if (_entries.contains(f)) { + return false; + } + + _entries.insert(f, EntryMap()); + + KMD5 folderMd5; + folderMd5.update(f.utf8()); + _hashes.insert(MD5Digest(folderMd5.rawDigest()), QValueList<MD5Digest>()); + +return true; +} + + +int Backend::renameEntry(const QString& oldName, const QString& 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 QString& key) const { + return _entries.contains(_folder) && _entries[_folder].contains(key); +} + + +bool Backend::removeEntry(const QString& 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 QString& f) { + if (!_open) { + return false; + } + + FolderMap::Iterator fi = _entries.find(f); + + if (fi != _entries.end()) { + if (_folder == f) { + _folder = QString::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 QString& folder) const { + KMD5 md5; + md5.update(folder.utf8()); + return !_hashes.contains(MD5Digest(md5.rawDigest())); +} + + +bool Backend::entryDoesNotExist(const QString& folder, const QString& 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/kwallet/backend/kwalletbackend.h b/kwallet/backend/kwalletbackend.h new file mode 100644 index 000000000..f1e9e521d --- /dev/null +++ b/kwallet/backend/kwalletbackend.h @@ -0,0 +1,161 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2004 George Staikos <staikos@kde.org> + * + * 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. + */ + + +#ifndef _KWALLETBACKEND_H +#define _KWALLETBACKEND_H + +#include <kmdcodec.h> + +#include <qstring.h> +#include <qstringlist.h> +#include <qmap.h> +#include "kwalletentry.h" + + +namespace KWallet { + +class MD5Digest; + +/* @internal + */ +class KDE_EXPORT Backend { + public: + Backend(const QString& name = "kdewallet", bool isPath = false); + ~Backend(); + + // Open and unlock the wallet. + int open(const QByteArray& password); + + // Close and lock the wallet (saving changes). + int close(const QByteArray& password); + + // Close the wallet, losing any changes. + int close(); + + // Write the wallet to disk + int sync(const QByteArray& password); + + // Returns true if the current wallet is open. + bool isOpen() const; + + // Returns the current wallet name. + const QString& walletName() const; + + // The list of folders. + QStringList folderList() const; + + // Force creation of a folder. + bool createFolder(const QString& f); + + // Change the folder. + void setFolder(const QString& f) { _folder = f; } + + // Current folder. If empty, it's the global folder. + const QString& folder() const { return _folder; } + + // Does it have this folder? + bool hasFolder(const QString& f) const { return _entries.contains(f); } + + // Look up an entry. Returns null if it doesn't exist. + Entry *readEntry(const QString& key); + + // Look up a list of entries. Supports wildcards. + // You delete the list. + QPtrList<Entry> readEntryList(const QString& key); + + // Store an entry. + void writeEntry(Entry *e); + + // Does this folder contain this entry? + bool hasEntry(const QString& key) const; + + // Returns true if the entry was removed + bool removeEntry(const QString& key); + + // Returns true if the folder was removed + bool removeFolder(const QString& f); + + // The list of entries in this folder. + QStringList entryList() const; + + // Rename an entry in this folder. + int renameEntry(const QString& oldName, const QString& newName); + + int ref() { return ++_ref; } + + int deref() { return --_ref; } + + int refCount() const { return _ref; } + + static bool exists(const QString& wallet); + + bool folderDoesNotExist(const QString& folder) const; + + bool entryDoesNotExist(const QString& folder, const QString& entry) const; + + static QString openRCToString(int rc); + + private: + class BackendPrivate; + BackendPrivate *d; + QString _name; + QString _path; + bool _open; + QString _folder; + int _ref; + // Map Folder->Entries + typedef QMap< QString, Entry* > EntryMap; + typedef QMap< QString, EntryMap > FolderMap; + FolderMap _entries; + typedef QMap<MD5Digest, QValueList<MD5Digest> > HashMap; + HashMap _hashes; +}; + +/** + * @internal + */ +class MD5Digest : public QByteArray { + public: + MD5Digest() : QByteArray(16) {} + MD5Digest(const KMD5::Digest d) : QByteArray() { duplicate(reinterpret_cast<const char *>(d), 16); } + virtual ~MD5Digest() {} + + int operator<(const MD5Digest& r) const { + int i = 0; + char x, y; + for (; i < 16; ++i) { + x = at(i); + y = r.at(i); + if (x != y) { + break; + } + } + if (i < 16 && x < y) { + return 1; + } + return 0; + } +}; + +} + +#endif + diff --git a/kwallet/backend/kwalletentry.cc b/kwallet/backend/kwalletentry.cc new file mode 100644 index 000000000..da88610b1 --- /dev/null +++ b/kwallet/backend/kwalletentry.cc @@ -0,0 +1,89 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2003 George Staikos <staikos@kde.org> + * + * 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 "kwalletentry.h" + + +using namespace KWallet; + + +Entry::Entry() { +} + +Entry::~Entry() { + _value.fill(0); +} + +const QString& Entry::key() const { + return _key; +} + + +const QByteArray& Entry::value() const { + return _value; +} + + +QString Entry::password() const { +QString x; + QDataStream qds(_value, IO_ReadOnly); + qds >> x; + return x; +} + + +void Entry::setValue(const QByteArray& val) { + // do a direct copy from one into the other without + // temporary variables + _value.fill(0); + _value.duplicate(val); +} + + +void Entry::setValue(const QString& val) { + _value.fill(0); + QDataStream qds(_value, IO_WriteOnly); + qds << val; +} + + +void Entry::setKey(const QString& 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/kwallet/backend/kwalletentry.h b/kwallet/backend/kwalletentry.h new file mode 100644 index 000000000..7f8c88448 --- /dev/null +++ b/kwallet/backend/kwalletentry.h @@ -0,0 +1,62 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2001-2003 George Staikos <staikos@kde.org> + * + * 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. + */ + + +#ifndef _KWALLETENTRY_H +#define _KWALLETENTRY_H + +#include <qstring.h> +#include <qdatastream.h> + +#include "kwallet.h" + +namespace KWallet { + +/* @internal + */ +class KDE_EXPORT Entry { + public: + Entry(); + ~Entry(); + + const QString& key() const; + const QByteArray& value() const; + QString password() const; + const QByteArray& map() const { return value(); } + + void setValue(const QByteArray& val); + void setValue(const QString& val); + void setKey(const QString& key); + + Wallet::EntryType type() const; + void setType(Wallet::EntryType type); + + void copy(const Entry* x); + + private: + QString _key; + QByteArray _value; + Wallet::EntryType _type; +}; + +} + +#endif + diff --git a/kwallet/backend/sha1.cc b/kwallet/backend/sha1.cc new file mode 100644 index 000000000..7420b5f16 --- /dev/null +++ b/kwallet/backend/sha1.cc @@ -0,0 +1,343 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + 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 <config.h> + +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif +#ifdef HAVE_STDINT_H +#include <stdint.h> /* For uintXX_t on OSX */ +#endif +#ifdef HAVE_SYS_BITYPES_H +#include <sys/bitypes.h> /* For uintXX_t on Tru64 */ +#endif +#ifdef HAVE_STDINT_H +#include <stdint.h> +#endif + +#include "sha1.h" +#include <string.h> + +// 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/kwallet/backend/sha1.h b/kwallet/backend/sha1.h new file mode 100644 index 000000000..de40cc692 --- /dev/null +++ b/kwallet/backend/sha1.h @@ -0,0 +1,71 @@ +/* This file is part of the KDE project + Copyright (C) 2001 George Staikos <staikos@kde.org> + 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. +*/ + + +#ifndef __sha1__ko__h +#define __sha1__ko__h + + +/* @internal + */ +class SHA1 { + public: + SHA1(); + virtual ~SHA1(); + + /* + * The number of bits in the hash generated. + */ + virtual int size() const; + + /* + * True if all settings are good and we are ready to hash. + */ + virtual bool readyToGo() const; + + /* + * Process a block of data for the hash function. + */ + virtual int process(const void *block, int len); + + /* + * Return the digest as a 20 byte array reference. + * Calling this makes readyToGo() == false. + */ + virtual const unsigned char *hash(); + + /* + * Reset the digest so a new one can be calculated. + */ + virtual int reset(); + + protected: + int _hashlen; + bool _init; + + long _h0, _h1, _h2, _h3, _h4; + long _nblocks; + int _count; + unsigned char _buf[64]; + void transform(void *data); +}; + + +#endif diff --git a/kwallet/backend/tests/Makefile.am b/kwallet/backend/tests/Makefile.am new file mode 100644 index 000000000..3191b06a8 --- /dev/null +++ b/kwallet/backend/tests/Makefile.am @@ -0,0 +1,14 @@ +INCLUDES = -I$(top_srcdir)/kwallet/backend -I$(top_srcdir)/kwallet/client $(all_includes) + +AM_LDFLAGS = $(QT_LDFLAGS) $(X_LDFLAGS) $(KDE_RPATH) + +check_PROGRAMS = backendtest testbf testsha + +METASOURCES = AUTO + +LDADD = ../libkwalletbackend.la ../../client/libkwalletclient.la +backendtest_SOURCES = backendtest.cpp + +testbf_SOURCES = testbf.cpp + +testsha_SOURCES = testsha.cpp diff --git a/kwallet/backend/tests/backendtest.cpp b/kwallet/backend/tests/backendtest.cpp new file mode 100644 index 000000000..a38c8cdb3 --- /dev/null +++ b/kwallet/backend/tests/backendtest.cpp @@ -0,0 +1,45 @@ +#include <stdlib.h> +#include <stdio.h> + +#include <kapplication.h> +#include <qstring.h> + +#include "kwalletbackend.h" + +int main(int argc, char **argv) { + KApplication a(argc, argv, "kwalletbackendtest"); + + KWallet::Backend be("ktestwallet"); + printf("KWalletBackend constructed\n"); + + QByteArray apass, bpass, cpass; + + apass.duplicate("apassword", 9); + bpass.duplicate("bpassword", 9); + cpass.duplicate("cpassword", 9); + + printf("Passwords initialised.\n"); + int rc = be.close(apass); + + printf("be.close(apass) returned %d (should be -255)\n", rc); + + rc = be.open(bpass); + + printf("be.open(bpass) returned %d (should be 0 or 1)\n", rc); + + rc = be.close(bpass); + + printf("be.close(bpass) returned %d (should be 0)\n", rc); + + rc = be.open(apass); + + printf("be.open(apass) returned %d (should be negative)\n", rc); + + rc = be.open(bpass); + + printf("be.open(bpass) returned %d (should be 0)\n", rc); + + return 0; +} + + diff --git a/kwallet/backend/tests/testbf.cpp b/kwallet/backend/tests/testbf.cpp new file mode 100644 index 000000000..12dc74630 --- /dev/null +++ b/kwallet/backend/tests/testbf.cpp @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "blowfish.h" +#include "cbc.h" + + +int main() { +BlockCipher *bf; +char data[] = "This is a test."; +char expect[] = "\x22\x30\x7e\x2f\x42\x28\x44\x01\xda\xdf\x5a\x81\xd7\xe5\x7c\xd0"; +char key[] = "testkey"; +unsigned long et[] = {0x11223344}; + + printf("%d: 0x11 == %d and 0x44 == %d\n", ((unsigned char *)et)[0], + 0x11, 0x44); + bf = new BlowFish(); +// bf = new CipherBlockChain(new BlowFish()); + + bf->setKey((void *)key, 7*8); + + if (!bf->readyToGo()) { + printf("Error: not ready to go!\n"); + return -1; + } + + printf("About to encrypt...\n"); fflush(stdout); + if (-1 == bf->encrypt((void *)data, 8)) { + printf("Error: encrypt failed!\n"); + return -1; + } + printf("About to encrypt part 2...\n"); fflush(stdout); + bf->encrypt((void *)(data+8), 8); + + printf("Encryption done. data[] is now: "); + for (int i = 0; i < 16; i++) { + printf("0x%x ", data[i]&0xff); + if ((data[i]&0xff) != (expect[i]&0xff)) { + printf("Error. This byte failed the comparison. It should have been 0x%x.\n", expect[i]&0xff); + return -1; + } + } + printf("\n"); + + delete bf; + bf = new BlowFish(); +// bf = new CipherBlockChain(new BlowFish()); + bf->setKey((void *)key, 7*8); + + printf("About to decrypt...\n"); fflush(stdout); + if (-1 == bf->decrypt((void *)data, 16)) { + printf("Error: decrypt failed!\n"); + return -1; + } + //bf->decrypt((void *)(data+8), 8); + + printf("All done! Result... data[] = \"%s\"\n", data); + if (strcmp(data, "This is a test.")) { + printf("ERROR. Decryption failed.\n"); + return -1; + } + + delete bf; +} + + + diff --git a/kwallet/backend/tests/testsha.cpp b/kwallet/backend/tests/testsha.cpp new file mode 100644 index 000000000..70879f015 --- /dev/null +++ b/kwallet/backend/tests/testsha.cpp @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "sha1.h" + + +int main() { +SHA1 *sha1; +unsigned char data[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; +unsigned long et[] = {0x11223344}; +int rc; + + printf("%d: 0x11 == %d and 0x44 == %d\n", ((unsigned char *)et)[0], + 0x11, 0x44); + sha1 = new SHA1(); + + if (!sha1->readyToGo()) { + printf("Error: not ready to go!\n"); + return -1; + } + + printf("About to process [%s]\n", data); + rc = sha1->process(data, strlen((char *)data)); + + if (rc != strlen((char *)data)) { + printf("Error processing the data. rc=%d\n", rc); + } else printf("Done.\n"); + +const unsigned char *res = sha1->getHash(); + + if (res) { + for (int i = 0; i < 20; i++) { + printf("%.2X", *res++); + if (i>0 && (i-1)%2 == 0) printf(" "); + } + printf("\n"); + } else printf("Error - getHash() returned NULL!\n"); + + delete sha1; +} + + + diff --git a/kwallet/client/Makefile.am b/kwallet/client/Makefile.am new file mode 100644 index 000000000..7158aae31 --- /dev/null +++ b/kwallet/client/Makefile.am @@ -0,0 +1,14 @@ + +INCLUDES= -I$(srcdir) $(all_includes) + +lib_LTLIBRARIES = libkwalletclient.la + +libkwalletclient_la_LDFLAGS = $(KDE_RPATH) $(all_libraries) -version-info 1:1 -no-undefined +libkwalletclient_la_LIBADD = $(LIB_KDECORE) +libkwalletclient_la_SOURCES = kwallet.skel \ + kwallet.cc + +libkwalletclient_la_METASOURCES = AUTO + +include_HEADERS = kwallet.h kwallettypes.h + diff --git a/kwallet/client/kwallet.cc b/kwallet/client/kwallet.cc new file mode 100644 index 000000000..266135f85 --- /dev/null +++ b/kwallet/client/kwallet.cc @@ -0,0 +1,713 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2002-2004 George Staikos <staikos@kde.org> + * + * 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 "kwallettypes.h" +#include "kwallet.h" +#include <kconfig.h> +#include <kdebug.h> +#include <kdeversion.h> +#include <dcopclient.h> +#include <dcopref.h> +#include <qpopupmenu.h> +#include <qapplication.h> + +#include <assert.h> + +using namespace KWallet; + + +const QString Wallet::LocalWallet() { + KConfig cfg("kwalletrc", true); + cfg.setGroup("Wallet"); + if (!cfg.readBoolEntry("Use One Wallet", true)) { + QString tmp = cfg.readEntry("Local Wallet", "localwallet"); + if (tmp.isEmpty()) { + return "localwallet"; + } + return tmp; + } + + QString tmp = cfg.readEntry("Default Wallet", "kdewallet"); + if (tmp.isEmpty()) { + return "kdewallet"; + } + return tmp; +} + +const QString Wallet::NetworkWallet() { + KConfig cfg("kwalletrc", true); + cfg.setGroup("Wallet"); + + QString tmp = cfg.readEntry("Default Wallet", "kdewallet"); + if (tmp.isEmpty()) { + return "kdewallet"; + } + return tmp; +} + +const QString Wallet::PasswordFolder() { + return "Passwords"; +} + +const QString Wallet::FormDataFolder() { + return "Form Data"; +} + + + +Wallet::Wallet(int handle, const QString& name) +: QObject(0L), DCOPObject(), d(0L), _name(name), _handle(handle) { + + _dcopRef = new DCOPRef("kded", "kwalletd"); + + _dcopRef->dcopClient()->setNotifications(true); + connect(_dcopRef->dcopClient(), + SIGNAL(applicationRemoved(const QCString&)), + this, + SLOT(slotAppUnregistered(const QCString&))); + + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "walletClosed(int)", "slotWalletClosed(int)", false); + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderListUpdated(QString)", "slotFolderListUpdated(QString)", false); + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "folderUpdated(QString, QString)", "slotFolderUpdated(QString, QString)", false); + connectDCOPSignal(_dcopRef->app(), _dcopRef->obj(), "applicationDisconnected(QString, QCString)", "slotApplicationDisconnected(QString, QCString)", 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 = QString::null; + } + } + } +} + + +Wallet::~Wallet() { + if (_handle != -1) { + _dcopRef->call("close", _handle, false); + _handle = -1; + _folder = QString::null; + _name = QString::null; + } + + delete _dcopRef; + _dcopRef = 0L; +} + + +QStringList Wallet::walletList() { + DCOPReply r = DCOPRef("kded", "kwalletd").call("wallets"); + QStringList rc; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +void Wallet::changePassword(const QString& name, WId w) { + DCOPRef("kded", "kwalletd").send("changePassword", name, uint(w)); +} + + +bool Wallet::isEnabled() { + DCOPReply r = DCOPRef("kded", "kwalletd").call("isEnabled"); + bool rc = false; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +bool Wallet::isOpen(const QString& name) { + DCOPReply r = DCOPRef("kded", "kwalletd").call("isOpen", name); + bool rc = false; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +int Wallet::closeWallet(const QString& name, bool force) { + DCOPReply r = DCOPRef("kded", "kwalletd").call("close", name, force); + int rc = -1; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +int Wallet::deleteWallet(const QString& name) { + DCOPReply r = DCOPRef("kded", "kwalletd").call("deleteWallet", name); + int rc = -1; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +Wallet *Wallet::openWallet(const QString& name, WId w, OpenType ot) { + if (ot == Asynchronous) { + Wallet *wallet = new Wallet(-1, name); + DCOPRef("kded", "kwalletd").send("openAsynchronous", name, wallet->objId(), uint(w)); + return wallet; + } + + // avoid deadlock if the app has some popup open (#65978/#71048) + while( QWidget* widget = qApp->activePopupWidget()) + widget->close(); + + bool isPath = ot == Path; + DCOPReply r; + + if (isPath) { + r = DCOPRef("kded", "kwalletd").call("openPath", name, uint(w)); + } else { + r = DCOPRef("kded", "kwalletd").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 QString& wallet, const QCString& app) { + DCOPReply r = DCOPRef("kded", "kwalletd").call("disconnectApplication", wallet, app); + bool rc = false; + if (r.isValid()) { + r.get(rc); + } + return rc; +} + + +QStringList Wallet::users(const QString& name) { + DCOPReply r = DCOPRef("kded", "kwalletd").call("users", name); + QStringList 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 = QString::null; + _name = QString::null; + if (r.isValid()) { + int drc = -1; + r.get(drc); + return drc; + } + return -1; +} + + +const QString& 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 = QString::null; + _name = QString::null; + emit walletClosed(); + } +} + + +QStringList Wallet::folderList() { + QStringList rc; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("folderList", _handle); + if (r.isValid()) { + r.get(rc); + } + + return rc; +} + + +QStringList Wallet::entryList() { + QStringList 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 QString& 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 QString& 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 QString& 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 QString& 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(QString::null); + } + + return rc; +} + + +const QString& Wallet::currentFolder() const { + return _folder; +} + + +int Wallet::readEntry(const QString& key, QByteArray& 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 QString& key, QMap<QString, QByteArray>& 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 QString& oldName, const QString& 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 QString& key, QMap<QString,QString>& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readMap", _handle, _folder, key); + if (r.isValid()) { + QByteArray v; + r.get(v); + if (!v.isEmpty()) { + QDataStream ds(v, IO_ReadOnly); + ds >> value; + } + rc = 0; + } + + return rc; +} + + +int Wallet::readMapList(const QString& key, QMap<QString, QMap<QString, QString> >& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + DCOPReply r = _dcopRef->call("readMapList", _handle, _folder, key); + if (r.isValid()) { + QMap<QString,QByteArray> unparsed; + r.get(unparsed); + for (QMap<QString,QByteArray>::ConstIterator i = unparsed.begin(); i != unparsed.end(); ++i) { + if (!i.data().isEmpty()) { + QDataStream ds(i.data(), IO_ReadOnly); + QMap<QString,QString> v; + ds >> v; + value.insert(i.key(), v); + } + } + rc = 0; + } + + return rc; +} + + +int Wallet::readPassword(const QString& key, QString& 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 QString& key, QMap<QString, QString>& 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 QString& key, const QByteArray& 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 QString& key, const QByteArray& 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 QString& key, const QMap<QString,QString>& value) { + int rc = -1; + + if (_handle == -1) { + return rc; + } + + QByteArray a; + QDataStream 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 QString& key, const QString& 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 QString& 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 QString& 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 QString& 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<EntryType>(rc); +} + + +void Wallet::slotAppUnregistered(const QCString& app) { + if (_handle >= 0 && app == "kded") { + slotWalletClosed(_handle); + } +} + + +void Wallet::slotFolderUpdated(const QString& wallet, const QString& folder) { + if (_name == wallet) { + emit folderUpdated(folder); + } +} + + +void Wallet::slotFolderListUpdated(const QString& wallet) { + if (_name == wallet) { + emit folderListUpdated(); + } +} + + +void Wallet::slotApplicationDisconnected(const QString& wallet, const QCString& 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 QString& wallet, const QString& folder) { +DCOPReply r = DCOPRef("kded", "kwalletd").call("folderDoesNotExist", wallet, folder); +bool rc = true; + if (r.isValid()) { + r.get(rc); + } +return rc; +} + + +bool Wallet::keyDoesNotExist(const QString& wallet, const QString& folder, const QString& key) { +DCOPReply r = DCOPRef("kded", "kwalletd").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 "kwallet.moc" diff --git a/kwallet/client/kwallet.h b/kwallet/client/kwallet.h new file mode 100644 index 000000000..e49839ddc --- /dev/null +++ b/kwallet/client/kwallet.h @@ -0,0 +1,520 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2002-2004 George Staikos <staikos@kde.org> + * + * 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. + */ + + +#ifndef _KWALLET_H +#define _KWALLET_H + +#include <qglobal.h> +#ifdef Q_OS_UNIX + +#include <qstring.h> +#include <qstringlist.h> +#include <qobject.h> +#include <dcopobject.h> + +class DCOPRef; + +/** Namespace collecting all the Wallet-related classes. */ +namespace KWallet { + +/** + * KDE Wallet + * + * This class implements a generic system-wide Wallet for KDE. This is the + * ONLY public interface. The DCOP client is unsupported and considered to be + * private. + * + * @author George Staikos <staikos@kde.org> + * @short KDE Wallet Class + */ +class KIO_EXPORT Wallet : public QObject, public DCOPObject { + K_DCOP + Q_OBJECT + protected: + /** + * Construct a KWallet object. + * @internal + * @param handle The handle for the wallet. + * @param name The name of the wallet. + */ + Wallet(int handle, const QString& name); + /** + * Copy a KWallet object. + * @internal + */ + Wallet(const Wallet&); + + public: + enum EntryType { Unknown=0, Password, Stream, Map, Unused=0xffff }; + + /** + * Destroy a KWallet object. Closes the wallet. + */ + virtual ~Wallet(); + + /** + * List all the wallets available. + * @return Returns a list of the names of all wallets that are + * open. + */ + static QStringList walletList(); + + /** + * Determine if the KDE wallet is enabled. Normally you do + * not need to use this because open() will just fail. + * @return Returns true if the wallet enabled, else false. + */ + static bool isEnabled(); + + /** + * Determine if the wallet @p name is open by any application. + * @param name The name of the wallet to check. + * @return Returns true if the wallet is open, else false. + */ + static bool isOpen(const QString& name); + + /** + * Close the wallet @p name. The wallet will only be closed + * if it is open but not in use (rare), or if it is forced + * closed. + * @param name The name of the wallet to close. + * @param force Set true to force the wallet closed even if it + * is in use by others. + * @return Returns 0 on success, non-zero on error. + */ + static int closeWallet(const QString& name, bool force); + + /** + * Delete the wallet @p name. The wallet will be forced closed + * first. + * @param name The name of the wallet to delete. + * @return Returns 0 on success, non-zero on error. + */ + static int deleteWallet(const QString& name); + + /** + * Disconnect the application @p app from @p wallet. + * @param wallet The name of the wallet to disconnect. + * @param app The name of the application to disconnect. + * @return Returns true on success, false on error. + */ + static bool disconnectApplication(const QString& wallet, const QCString& app); + + enum OpenType { Synchronous=0, Asynchronous, Path, OpenTypeUnused=0xff }; + + /** + * Open the wallet @p name. The user will be prompted to + * allow your application to open the wallet, and may be + * prompted for a password. You are responsible for deleting + * this object when you are done with it. + * @param name The name of the wallet to open. + * @param ot If Asynchronous, the call will return + * immediately with a non-null pointer to an + * invalid wallet. You must immediately connect + * the walletOpened() signal to a slot so that + * you will know when it is opened, or when it + * fails. + * @param w The window id to associate any dialogs with. + * @return Returns a pointer to the wallet if successful, + * or a null pointer on error or if rejected. + */ + static Wallet* openWallet(const QString& name, WId w = 0, OpenType ot = Synchronous); + + /** + * List the applications that are using the wallet @p wallet. + * @param wallet The wallet to query. + * @return Returns a list of all DCOP application IDs using + * the wallet. + */ + static QStringList users(const QString& wallet); + + /** + * The name of the wallet used to store local passwords. + */ + static const QString LocalWallet(); + + /** + * The name of the wallet used to store network passwords. + */ + static const QString NetworkWallet(); + + /** + * The standardized name of the password folder. + * It is automatically created when a wallet is created, but + * the user may still delete it so you should check for its + * existence and recreate it if necessary and desired. + */ + static const QString PasswordFolder(); + + /** + * The standardized name of the form data folder. + * It is automatically created when a wallet is created, but + * the user may still delete it so you should check for its + * existence and recreate it if necessary and desired. + */ + static const QString FormDataFolder(); + + /** + * Request to the wallet service to change the password of + * the wallet @p name. + * @param name The the wallet to change the password of. + * @param w The window id to associate any dialogs with. + */ + static void changePassword(const QString& name, WId w = 0); + + /** + * This syncs the wallet file on disk with what is in memory. + * You don't normally need to use this. It happens + * automatically on close. + * @return Returns 0 on success, non-zero on error. + */ + virtual int sync(); + + /** + * This closes and locks the current wallet. It will + * disconnect all applications using the wallet. + * @return Returns 0 on success, non-zero on error. + */ + virtual int lockWallet(); + + /** + * The name of the current wallet. + */ + virtual const QString& walletName() const; + + /** + * Determine if the current wallet is open, and is a valid + * wallet handle. + * @return Returns true if the wallet handle is valid and open. + */ + virtual bool isOpen() const; + + /** + * Request to the wallet service to change the password of + * the current wallet. + * @param w The window id to associate any dialogs with. + */ + virtual void requestChangePassword(WId w = 0); + + /** + * Obtain the list of all folders contained in the wallet. + * @return Returns an empty list if the wallet is not open. + */ + virtual QStringList folderList(); + + /** + * Determine if the folder @p f exists in the wallet. + * @param f the name of the folder to check for + * @return Returns true if the folder exists in the wallet. + */ + virtual bool hasFolder(const QString& f); + + /** + * Set the current working folder to @p f. The folder must + * exist, or this call will fail. Create a folder with + * createFolder(). + * @param f the name of the folder to make the working folder + * @return Returns true if the folder was successfully set. + */ + virtual bool setFolder(const QString& f); + + /** + * Remove the folder @p f and all its entries from the wallet. + * @param f the name of the folder to remove + * @return Returns true if the folder was successfully removed. + */ + virtual bool removeFolder(const QString& f); + + /** + * Created the folder @p f. + * @param f the name of the folder to create + * @return Returns true if the folder was successfully created. + */ + virtual bool createFolder(const QString& f); + + /** + * Determine the current working folder in the wallet. + * If the folder name is empty, it is working in the global + * folder, which is valid but discouraged. + * @return Returns the current working folder. + */ + virtual const QString& currentFolder() const; + + /** + * Return the list of keys of all entries in this folder. + * @return Returns an empty list if the wallet is not open, or + * if the folder is empty. + */ + virtual QStringList entryList(); + + /** + * Rename the entry @p oldName to @p newName. + * @param oldName The original key of the entry. + * @param newName The new key of the entry. + * @return Returns 0 on success, non-zero on error. + */ + virtual int renameEntry(const QString& oldName, const QString& newName); + + /** + * Read the entry @p key from the current folder. + * The entry format is unknown except that it is either a + * QByteArray or a QDataStream, which effectively means that + * it is anything. + * @param key The key of the entry to read. + * @param value A buffer to fill with the value. + * @return Returns 0 on success, non-zero on error. + */ + virtual int readEntry(const QString& key, QByteArray& value); + + /** + * Read the map entry @p key from the current folder. + * @param key The key of the entry to read. + * @param value A map buffer to fill with the value. + * @return Returns 0 on success, non-zero on error. Will + * return an error if the key was not originally + * written as a map. + */ + virtual int readMap(const QString& key, QMap<QString,QString>& value); + + /** + * Read the password entry @p key from the current folder. + * @param key The key of the entry to read. + * @param value A password buffer to fill with the value. + * @return Returns 0 on success, non-zero on error. Will + * return an error if the key was not originally + * written as a password. + */ + virtual int readPassword(const QString& key, QString& value); + + /** + * Read the entries matching @p key from the current folder. + * The entry format is unknown except that it is either a + * QByteArray or a QDataStream, which effectively means that + * it is anything. + * @param key The key of the entry to read. Wildcards + * are supported. + * @param value A buffer to fill with the value. The key in + * the map is the entry key. + * @return Returns 0 on success, non-zero on error. + * @since 3.4 + */ + int readEntryList(const QString& key, QMap<QString, QByteArray>& value); + + /** + * Read the map entry @p key from the current folder. + * @param key The key of the entry to read. Wildcards + * are supported. + * @param value A buffer to fill with the value. The key in + * the map is the entry key. + * @return Returns 0 on success, non-zero on error. Will + * return an error if the key was not originally + * written as a map. + * @since 3.4 + */ + int readMapList(const QString& key, QMap<QString, QMap<QString, QString> >& value); + + /** + * Read the password entry @p key from the current folder. + * @param key The key of the entry to read. Wildcards + * are supported. + * @param value A buffer to fill with the value. The key in + * the map is the entry key. + * @return Returns 0 on success, non-zero on error. Will + * return an error if the key was not originally + * written as a password. + * @since 3.4 + */ + int readPasswordList(const QString& key, QMap<QString, QString>& value); + + /** + * Write @p key = @p value as a binary entry to the current + * folder. Be careful with this, it could cause inconsistency + * in the future since you can put an arbitrary entry type in + * place. + * @param key The key of the new entry. + * @param value The value of the entry. + * @param entryType The type of the entry. + * @return Returns 0 on success, non-zero on error. + */ + virtual int writeEntry(const QString& key, const QByteArray& value, EntryType entryType); + + /** + * Write @p key = @p value as a binary entry to the current + * folder. + * @param key The key of the new entry. + * @param value The value of the entry. + * @return Returns 0 on success, non-zero on error. + */ + virtual int writeEntry(const QString& key, const QByteArray& value); + + /** + * Write @p key = @p value as a map to the current folder. + * @param key The key of the new entry. + * @param value The value of the map. + * @return Returns 0 on success, non-zero on error. + */ + virtual int writeMap(const QString& key, const QMap<QString,QString>& value); + + /** + * Write @p key = @p value as a password to the current folder. + * @param key The key of the new entry. + * @param value The value of the password. + * @return Returns 0 on success, non-zero on error. + */ + virtual int writePassword(const QString& key, const QString& value); + + /** + * Determine if the current folder has they entry @p key. + * @param key The key to search for. + * @return Returns true if the folder contains @p key. + */ + virtual bool hasEntry(const QString& key); + + /** + * Remove the entry @p key from the current folder. + * @param key The key to remove. + * @return Returns 0 on success, non-zero on error. + */ + virtual int removeEntry(const QString& key); + + /** + * Determine the type of the entry @p key in this folder. + * @param key The key to look up. + * @return Returns an enumerated type representing the type + * of the entry. + */ + virtual EntryType entryType(const QString& key); + + /** + * Determine if a folder does not exist in a wallet. This + * does not require decryption of the wallet. + * This is a handy optimization to avoid prompting the user + * if your data is certainly not in the wallet. + * @param wallet The wallet to look in. + * @param folder The folder to look up. + * @return Returns true if the folder does NOT exist in the + * wallet, or the wallet does not exist. + */ + static bool folderDoesNotExist(const QString& wallet, const QString& folder); + + /** + * Determine if an entry in a folder does not exist in a + * wallet. This does not require decryption of the wallet. + * This is a handy optimization to avoid prompting the user + * if your data is certainly not in the wallet. + * @param wallet The wallet to look in. + * @param folder The folder to look in. + * @param key The key to look up. + * @return Returns true if the key does NOT exist in the + * wallet, or the folder or wallet does not exist. + */ + static bool keyDoesNotExist(const QString& wallet, const QString& folder, + const QString& key); + + signals: + /** + * Emitted when this wallet is closed. + */ + void walletClosed(); + + /** + * Emitted when a folder in this wallet is updated. + * @param folder The folder that was updated. + */ + void folderUpdated(const QString& folder); + + /** + * Emitted when the folder list is changed in this wallet. + */ + void folderListUpdated(); + + /** + * Emitted when a folder in this wallet is removed. + * @param folder The folder that was removed. + */ + void folderRemoved(const QString& folder); + + /** + * Emitted when a wallet is opened in asynchronous mode. + * @param success True if the wallet was opened successfully. + */ + void walletOpened(bool success); + + private: + k_dcop: + /** + * @internal + * DCOP slot for signals emitted by the wallet service. + */ + ASYNC slotWalletClosed(int handle); + + /** + * @internal + * DCOP slot for signals emitted by the wallet service. + */ + ASYNC slotFolderUpdated(const QString& wallet, const QString& folder); + + /** + * @internal + * DCOP slot for signals emitted by the wallet service. + */ + ASYNC slotFolderListUpdated(const QString& wallet); + + /** + * @internal + * DCOP slot for signals emitted by the wallet service. + */ + ASYNC slotApplicationDisconnected(const QString& wallet, const QCString& application); + + /** + * @internal + * Callback for kwalletd + */ + ASYNC walletOpenResult(int rc); + + private slots: + /** + * @internal + * Used to detect when the wallet service dies. + */ + void slotAppUnregistered(const QCString&); + + private: + class WalletPrivate; + WalletPrivate *d; + QString _name; + QString _folder; + int _handle; + DCOPRef *_dcopRef; + + protected: + /** + * @internal + */ + virtual void virtual_hook(int id, void *data); +}; + +} + +#endif //Q_OS_UNIX + +#endif //_KWALLET_H + diff --git a/kwallet/client/kwallettypes.h b/kwallet/client/kwallettypes.h new file mode 100644 index 000000000..b98d3d17a --- /dev/null +++ b/kwallet/client/kwallettypes.h @@ -0,0 +1,32 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2004 George Staikos <staikos@kde.org> + * + * 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. + */ + +#ifndef KWALLET_TYPES_H +#define KWALLET_TYPES_H + +#include <qmap.h> + +class QString; + +inline const char* dcopTypeName(const QByteArray&) { return "QByteArray"; } +inline const char* dcopTypeName(const QMap<QString,QString>&) { return "QMap<QString,QString>"; } +inline const char* dcopTypeName(const QMap<QString,QByteArray>&) { return "QMap<QString,QByteArray>"; } + +#endif diff --git a/kwallet/tests/Makefile.am b/kwallet/tests/Makefile.am new file mode 100644 index 000000000..cdcec5859 --- /dev/null +++ b/kwallet/tests/Makefile.am @@ -0,0 +1,20 @@ +AM_CPPFLAGS = -DKDE_NO_COMPAT -DQT_NO_COMPAT -DQT_NO_ASCII_CAST $(all_includes) + +METASOURCES = AUTO +check_PROGRAMS = kwalletasync kwalletsync kwalletboth + +kwalletasync_SOURCES = kwalletasync.cpp kwallettest.cpp +kwalletasync_LDFLAGS = -no-undefined $(KDE_RPATH) $(all_libraries) +kwalletasync_LDADD = $(top_builddir)/kwallet/client/libkwalletclient.la \ + $(LIB_KDECORE) $(LIB_QT) + +kwalletsync_SOURCES = kwalletsync.cpp +kwalletsync_LDFLAGS = -no-undefined $(KDE_RPATH) $(all_libraries) +kwalletsync_LDADD = $(top_builddir)/kwallet/client/libkwalletclient.la \ + $(LIB_KDECORE) $(LIB_QT) + +kwalletboth_SOURCES = kwalletboth.cpp kwallettest.cpp +kwalletboth_LDFLAGS = -no-undefined $(KDE_RPATH) $(all_libraries) +kwalletboth_LDADD = $(top_builddir)/kwallet/client/libkwalletclient.la \ + $(LIB_KDECORE) $(LIB_QT) +INCLUDES = -I$(top_srcdir)/kwallet/client diff --git a/kwallet/tests/README b/kwallet/tests/README new file mode 100644 index 000000000..73c3ad1db --- /dev/null +++ b/kwallet/tests/README @@ -0,0 +1,5 @@ +Tests for opening the wallet synchronously and asynchronously + +kwalletsync - open synchronously +kwalletasync - open asynchronously +kwalletboth - start opening asynchronously, then, during the async call, open synchronously diff --git a/kwallet/tests/kwalletasync.cpp b/kwallet/tests/kwalletasync.cpp new file mode 100644 index 000000000..0992c6e7d --- /dev/null +++ b/kwallet/tests/kwalletasync.cpp @@ -0,0 +1,59 @@ +#include <qtextstream.h> +#include <qtimer.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kcmdlineargs.h> +#include <kdebug.h> +#include <kglobal.h> +#include <kstandarddirs.h> +#include <dcopclient.h> +#include <kwallet.h> + +#include "kwallettest.h" + +static QTextStream _out( stdout, IO_WriteOnly ); + +void openWallet() +{ + _out << "About to ask for wallet async" << endl; + + // we have no wallet: ask for one. + KWallet::Wallet *wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(), 0, KWallet::Wallet::Asynchronous ); + + WalletReceiver r; + r.connect( wallet, SIGNAL( walletOpened(bool) ), SLOT( walletOpened(bool) ) ); + + _out << "About to start 30 second event loop" << endl; + + QTimer::singleShot( 30000, qApp, SLOT( quit() ) ); + int ret = qApp->exec(); + + if ( ret == 0 ) + _out << "Timed out!" << endl; + else + _out << "Success!" << endl; +} + +void WalletReceiver::walletOpened( bool got ) +{ + _out << "Got async wallet: " << got << endl; + qApp->exit( 1 ); +} + +int main( int argc, char *argv[] ) +{ + KAboutData aboutData( "kwalletasync", "kwalletasync", "version" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app( "kwalletasync" ); + + // register with DCOP + _out << "DCOP registration returned " << app.dcopClient()->registerAs(app.name()) << endl; + + openWallet(); + + return 0; +} + +// vim: set noet ts=4 sts=4 sw=4: + diff --git a/kwallet/tests/kwalletboth.cpp b/kwallet/tests/kwalletboth.cpp new file mode 100644 index 000000000..736be72cb --- /dev/null +++ b/kwallet/tests/kwalletboth.cpp @@ -0,0 +1,81 @@ +#include <qtextstream.h> +#include <qtimer.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kcmdlineargs.h> +#include <kdebug.h> +#include <kglobal.h> +#include <kstandarddirs.h> +#include <dcopclient.h> +#include <kwallet.h> + +#include "kwallettest.h" + +static QTextStream _out( stdout, IO_WriteOnly ); + +void openWallet() +{ + _out << "About to ask for wallet async" << endl; + + // we have no wallet: ask for one. + KWallet::Wallet *wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(), 0, KWallet::Wallet::Asynchronous ); + + WalletReceiver r; + r.connect( wallet, SIGNAL( walletOpened(bool) ), SLOT( walletOpened(bool) ) ); + + _out << "About to ask for wallet sync" << endl; + + wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(), 0, KWallet::Wallet::Synchronous ); + + _out << "Got sync wallet: " << (wallet != 0) << endl; + _out << "About to start 30 second event loop" << endl; + + QTimer::singleShot( 30000, qApp, SLOT( quit() ) ); + int ret = qApp->exec(); + + + if ( ret == 0 ) + _out << "Timed out!" << endl; + else + _out << "Success!" << endl; + + QMap<QString,QString> p; + ret = wallet->readPasswordList("*", p); + _out << "readPasswordList returned: " << ret << endl; + _out << "readPasswordList returned " << p.keys().count() << " entries" << endl; + QMap<QString, QMap<QString, QString> > q; + ret = wallet->readMapList("*", q); + _out << "readMapList returned: " << ret << endl; + _out << "readMapList returned " << q.keys().count() << " entries" << endl; + + QMap<QString, QByteArray> s; + ret = wallet->readEntryList("*", s); + _out << "readEntryList returned: " << ret << endl; + _out << "readEntryList returned " << s.keys().count() << " entries" << endl; + + delete wallet; +} + +void WalletReceiver::walletOpened( bool got ) +{ + _out << "Got async wallet: " << got << endl; + qApp->exit( 1 ); +} + +int main( int argc, char *argv[] ) +{ + KAboutData aboutData( "kwalletboth", "kwalletboth", "version" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app( "kwalletboth" ); + + // register with DCOP + _out << "DCOP registration returned " << app.dcopClient()->registerAs(app.name()) << endl; + + openWallet(); + + return 0; +} + +// vim: set noet ts=4 sts=4 sw=4: + diff --git a/kwallet/tests/kwalletsync.cpp b/kwallet/tests/kwalletsync.cpp new file mode 100644 index 000000000..d5eae13a3 --- /dev/null +++ b/kwallet/tests/kwalletsync.cpp @@ -0,0 +1,39 @@ +#include <qtextstream.h> +#include <qtimer.h> + +#include <kaboutdata.h> +#include <kapplication.h> +#include <kcmdlineargs.h> +#include <kdebug.h> +#include <kglobal.h> +#include <kstandarddirs.h> +#include <dcopclient.h> +#include <kwallet.h> + +static QTextStream _out( stdout, IO_WriteOnly ); + +void openWallet() +{ + _out << "About to ask for wallet sync" << endl; + + KWallet::Wallet *w = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(), 0, KWallet::Wallet::Synchronous ); + + _out << "Got sync wallet: " << (w != 0) << endl; +} + +int main( int argc, char *argv[] ) +{ + KAboutData aboutData( "kwalletsync", "kwalletsync", "version" ); + KCmdLineArgs::init( argc, argv, &aboutData ); + KApplication app( "kwalletsync" ); + + // register with DCOP + _out << "DCOP registration returned " << app.dcopClient()->registerAs(app.name()) << endl; + + openWallet(); + + return 0; +} + +// vim: set noet ts=4 sts=4 sw=4: + diff --git a/kwallet/tests/kwallettest.cpp b/kwallet/tests/kwallettest.cpp new file mode 100644 index 000000000..a17644cfb --- /dev/null +++ b/kwallet/tests/kwallettest.cpp @@ -0,0 +1,2 @@ +#include "kwallettest.h" +#include "kwallettest.moc" diff --git a/kwallet/tests/kwallettest.h b/kwallet/tests/kwallettest.h new file mode 100644 index 000000000..e3fe834f2 --- /dev/null +++ b/kwallet/tests/kwallettest.h @@ -0,0 +1,15 @@ +#ifndef KWALLETASYNC_H +#define KWALLETASYNC_H + +#include <qobject.h> + +namespace KWallet { class Wallet; } + +class WalletReceiver : public QObject +{ + Q_OBJECT +public slots: + void walletOpened( bool ); +}; + +#endif |