-rw-r--r-- | pwmanager/pwmanager/libgcryptif.cpp | 420 | ||||
-rw-r--r-- | pwmanager/pwmanager/libgcryptif.h | 5 | ||||
-rw-r--r-- | pwmanager/pwmanager/pwmanagerE.pro | 10 | ||||
-rw-r--r-- | pwmanager/pwmanager/pwmdoc.cpp | 77 |
4 files changed, 446 insertions, 66 deletions
diff --git a/pwmanager/pwmanager/libgcryptif.cpp b/pwmanager/pwmanager/libgcryptif.cpp index 8e55144..6f3a994 100644 --- a/pwmanager/pwmanager/libgcryptif.cpp +++ b/pwmanager/pwmanager/libgcryptif.cpp | |||
@@ -1,435 +1,855 @@ | |||
1 | /*************************************************************************** | 1 | /*************************************************************************** |
2 | * * | 2 | * * |
3 | * copyright (C) 2004 by Michael Buesch * | 3 | * copyright (C) 2004 by Michael Buesch * |
4 | * email: mbuesch@freenet.de * | 4 | * email: mbuesch@freenet.de * |
5 | * * | 5 | * * |
6 | * hashPassphrase() is derived from GnuPG and is * | 6 | * hashPassphrase() is derived from GnuPG and is * |
7 | * Copyright (C) 1998, 1999, 2000, 2001, 2003 * | 7 | * Copyright (C) 1998, 1999, 2000, 2001, 2003 * |
8 | * Free Software Foundation, Inc. * | 8 | * Free Software Foundation, Inc. * |
9 | * * | 9 | * * |
10 | * This program is free software; you can redistribute it and/or modify * | 10 | * This program is free software; you can redistribute it and/or modify * |
11 | * it under the terms of the GNU General Public License version 2 * | 11 | * it under the terms of the GNU General Public License version 2 * |
12 | * as published by the Free Software Foundation. * | 12 | * as published by the Free Software Foundation. * |
13 | * * | 13 | * * |
14 | ***************************************************************************/ | 14 | ***************************************************************************/ |
15 | 15 | ||
16 | #include "libgcryptif.h" | 16 | #include "libgcryptif.h" |
17 | 17 | ||
18 | #ifdef CONFIG_PWMANAGER_GCRY | 18 | #ifdef CONFIG_PWMANAGER_GCRY |
19 | 19 | ||
20 | #include "pwmdoc.h" | 20 | #include "pwmdoc.h" |
21 | #include "randomizer.h" | 21 | #include "randomizer.h" |
22 | 22 | ||
23 | #include <gcrypt.h> | 23 | #include <gcrypt.h> |
24 | 24 | ||
25 | PwMerror LibGCryptIf::encrypt(unsigned char **outBuf, | 25 | PwMerror LibGCryptIf::encrypt(unsigned char **outBuf, |
26 | size_t *outBufLen, | 26 | size_t *outBufLen, |
27 | unsigned char *inBuf, | 27 | unsigned char *inBuf, |
28 | size_t inBufLen, | 28 | size_t inBufLen, |
29 | const unsigned char *key, | 29 | const unsigned char *key, |
30 | size_t keylen, | 30 | size_t keylen, |
31 | char _algo) | 31 | char _algo) |
32 | { | 32 | { |
33 | PwMerror ret = e_success; | 33 | PwMerror ret = e_success; |
34 | gcry_error_t err; | 34 | gcry_error_t err; |
35 | gcry_cipher_hd_t handle; | 35 | gcry_cipher_hd_t handle; |
36 | size_t blklen; | 36 | size_t blklen; |
37 | size_t unpaddedLen = inBufLen; | 37 | size_t unpaddedLen = inBufLen; |
38 | size_t cipherKeylen; | 38 | size_t cipherKeylen; |
39 | unsigned char *hashedKey; | 39 | unsigned char *hashedKey; |
40 | unsigned char salt[STRING2KEY_SALTLEN]; | 40 | unsigned char salt[STRING2KEY_SALTLEN]; |
41 | int algo = mapCipherId(_algo); | 41 | int algo = mapCipherId(_algo); |
42 | 42 | ||
43 | if (!inBufLen || !keylen) | 43 | if (!inBufLen || !keylen) |
44 | return e_invalidArg; | 44 | return e_invalidArg; |
45 | 45 | ||
46 | // test if algo is ready for encryption | 46 | // test if algo is ready for encryption |
47 | err = gcry_cipher_algo_info(algo, | 47 | err = gcry_cipher_algo_info(algo, |
48 | GCRYCTL_TEST_ALGO, | 48 | GCRYCTL_TEST_ALGO, |
49 | 0, 0); | 49 | 0, 0); |
50 | if (err != GPG_ERR_NO_ERROR) { | 50 | if (err != GPG_ERR_NO_ERROR) { |
51 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_TEST_ALGO failed: ") | 51 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_TEST_ALGO failed: ") |
52 | + gcry_strerror(err)); | 52 | + gcry_strerror(err)); |
53 | ret = e_cryptNotImpl; | 53 | ret = e_cryptNotImpl; |
54 | goto out; | 54 | goto out; |
55 | } | 55 | } |
56 | // get the algo block length | 56 | // get the algo block length |
57 | err = gcry_cipher_algo_info(algo, | 57 | err = gcry_cipher_algo_info(algo, |
58 | GCRYCTL_GET_BLKLEN, | 58 | GCRYCTL_GET_BLKLEN, |
59 | 0, | 59 | 0, |
60 | &blklen); | 60 | &blklen); |
61 | if (err != GPG_ERR_NO_ERROR) { | 61 | if (err != GPG_ERR_NO_ERROR) { |
62 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_BLKLEN failed: ") | 62 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_BLKLEN failed: ") |
63 | + gcry_strerror(err)); | 63 | + gcry_strerror(err)); |
64 | ret = e_cryptNotImpl; | 64 | ret = e_cryptNotImpl; |
65 | goto out; | 65 | goto out; |
66 | } | 66 | } |
67 | /* double check if we have enough space. | 67 | /* double check if we have enough space. |
68 | * We have only 1024 extra bytes for padding and salt. | 68 | * We have only 1024 extra bytes for padding and salt. |
69 | */ | 69 | */ |
70 | BUG_ON(blklen > 1024 - STRING2KEY_SALTLEN); | 70 | BUG_ON(blklen > 1024 - STRING2KEY_SALTLEN); |
71 | // get the algo key length | 71 | // get the algo key length |
72 | err = gcry_cipher_algo_info(algo, | 72 | err = gcry_cipher_algo_info(algo, |
73 | GCRYCTL_GET_KEYLEN, | 73 | GCRYCTL_GET_KEYLEN, |
74 | 0, | 74 | 0, |
75 | &cipherKeylen); | 75 | &cipherKeylen); |
76 | if (err != GPG_ERR_NO_ERROR) { | 76 | if (err != GPG_ERR_NO_ERROR) { |
77 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_KEYLEN failed: ") | 77 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_KEYLEN failed: ") |
78 | + gcry_strerror(err)); | 78 | + gcry_strerror(err)); |
79 | ret = e_cryptNotImpl; | 79 | ret = e_cryptNotImpl; |
80 | goto out; | 80 | goto out; |
81 | } | 81 | } |
82 | // now open the algo and get a handle | 82 | // now open the algo and get a handle |
83 | err = gcry_cipher_open(&handle, | 83 | err = gcry_cipher_open(&handle, |
84 | algo, | 84 | algo, |
85 | GCRY_CIPHER_MODE_CBC, | 85 | GCRY_CIPHER_MODE_CBC, |
86 | 0); | 86 | 0); |
87 | if (err != GPG_ERR_NO_ERROR) { | 87 | if (err != GPG_ERR_NO_ERROR) { |
88 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_open() failed: ") | 88 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_open() failed: ") |
89 | + gcry_strerror(err)); | 89 | + gcry_strerror(err)); |
90 | ret = e_cryptNotImpl; | 90 | ret = e_cryptNotImpl; |
91 | goto out; | 91 | goto out; |
92 | } | 92 | } |
93 | // hash the "key" to a fixed size hash matching "cipherKeylen" | 93 | // hash the "key" to a fixed size hash matching "cipherKeylen" |
94 | hashedKey = new unsigned char[cipherKeylen]; | 94 | hashedKey = new unsigned char[cipherKeylen]; |
95 | hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, true); | 95 | hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, true); |
96 | // so now set the hashed key | 96 | // so now set the hashed key |
97 | err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); | 97 | err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); |
98 | if (err != GPG_ERR_NO_ERROR) { | 98 | if (err != GPG_ERR_NO_ERROR) { |
99 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_setkey() failed: ") | 99 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_setkey() failed: ") |
100 | + gcry_strerror(err)); | 100 | + gcry_strerror(err)); |
101 | ret = e_cryptNotImpl; | 101 | ret = e_cryptNotImpl; |
102 | delete [] hashedKey; | 102 | delete [] hashedKey; |
103 | goto out_close; | 103 | goto out_close; |
104 | } | 104 | } |
105 | delete [] hashedKey; | 105 | delete [] hashedKey; |
106 | /* allocate a buffer for the encrypted data. | 106 | /* allocate a buffer for the encrypted data. |
107 | * The size of the buffer is the inBuf length, but blklen | 107 | * The size of the buffer is the inBuf length, but blklen |
108 | * aligned and plus the length of the salt, that is appended. | 108 | * aligned and plus the length of the salt, that is appended. |
109 | */ | 109 | */ |
110 | *outBufLen = getBufLen(unpaddedLen, blklen) + STRING2KEY_SALTLEN; | 110 | *outBufLen = getBufLen(unpaddedLen, blklen) + STRING2KEY_SALTLEN; |
111 | *outBuf = new unsigned char[*outBufLen]; | 111 | *outBuf = new unsigned char[*outBufLen]; |
112 | padData(inBuf, unpaddedLen, blklen); | 112 | padData(inBuf, unpaddedLen, blklen); |
113 | // encrypt the padded data | 113 | // encrypt the padded data |
114 | err = gcry_cipher_encrypt(handle, | 114 | err = gcry_cipher_encrypt(handle, |
115 | *outBuf, | 115 | *outBuf, |
116 | *outBufLen - STRING2KEY_SALTLEN, | 116 | *outBufLen - STRING2KEY_SALTLEN, |
117 | inBuf, | 117 | inBuf, |
118 | *outBufLen - STRING2KEY_SALTLEN); | 118 | *outBufLen - STRING2KEY_SALTLEN); |
119 | if (err != GPG_ERR_NO_ERROR) { | 119 | if (err != GPG_ERR_NO_ERROR) { |
120 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") | 120 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") |
121 | + gcry_strerror(err)); | 121 | + gcry_strerror(err)); |
122 | ret = e_cryptNotImpl; | 122 | ret = e_cryptNotImpl; |
123 | goto out_delete; | 123 | goto out_delete; |
124 | } | 124 | } |
125 | // append the salt to the encrypted data | 125 | // append the salt to the encrypted data |
126 | memcpy(*outBuf + *outBufLen - STRING2KEY_SALTLEN, salt, STRING2KEY_SALTLEN); | 126 | memcpy(*outBuf + *outBufLen - STRING2KEY_SALTLEN, salt, STRING2KEY_SALTLEN); |
127 | goto out_close; | 127 | goto out_close; |
128 | out_delete: | 128 | out_delete: |
129 | delete [] *outBuf; | 129 | delete [] *outBuf; |
130 | out_close: | 130 | out_close: |
131 | gcry_cipher_close(handle); | 131 | gcry_cipher_close(handle); |
132 | out: | 132 | out: |
133 | return ret; | 133 | return ret; |
134 | } | 134 | } |
135 | 135 | ||
136 | PwMerror LibGCryptIf::decrypt(unsigned char **outBuf, | 136 | PwMerror LibGCryptIf::decrypt(unsigned char **outBuf, |
137 | size_t *outBufLen, | 137 | size_t *outBufLen, |
138 | const unsigned char *inBuf, | 138 | const unsigned char *inBuf, |
139 | size_t inBufLen, | 139 | size_t inBufLen, |
140 | const unsigned char *key, | 140 | const unsigned char *key, |
141 | size_t keylen, | 141 | size_t keylen, |
142 | char _algo) | 142 | char _algo) |
143 | { | 143 | { |
144 | PwMerror ret = e_success; | 144 | PwMerror ret = e_success; |
145 | gcry_error_t err; | 145 | gcry_error_t err; |
146 | gcry_cipher_hd_t handle; | 146 | gcry_cipher_hd_t handle; |
147 | size_t cipherKeylen; | 147 | size_t cipherKeylen; |
148 | unsigned char *hashedKey; | 148 | unsigned char *hashedKey; |
149 | unsigned char salt[STRING2KEY_SALTLEN]; | 149 | unsigned char salt[STRING2KEY_SALTLEN]; |
150 | int algo = mapCipherId(_algo); | 150 | int algo = mapCipherId(_algo); |
151 | 151 | ||
152 | if (!inBufLen || !keylen) | 152 | if (!inBufLen || !keylen) |
153 | return e_invalidArg; | 153 | return e_invalidArg; |
154 | 154 | ||
155 | // test if algo is ready for encryption | 155 | // test if algo is ready for encryption |
156 | err = gcry_cipher_algo_info(algo, | 156 | err = gcry_cipher_algo_info(algo, |
157 | GCRYCTL_TEST_ALGO, | 157 | GCRYCTL_TEST_ALGO, |
158 | 0, 0); | 158 | 0, 0); |
159 | if (err != GPG_ERR_NO_ERROR) { | 159 | if (err != GPG_ERR_NO_ERROR) { |
160 | printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_TEST_ALGO failed: ") | 160 | printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_TEST_ALGO failed: ") |
161 | + gcry_strerror(err)); | 161 | + gcry_strerror(err)); |
162 | ret = e_cryptNotImpl; | 162 | ret = e_cryptNotImpl; |
163 | goto out; | 163 | goto out; |
164 | } | 164 | } |
165 | // get algo key length | 165 | // get algo key length |
166 | err = gcry_cipher_algo_info(algo, | 166 | err = gcry_cipher_algo_info(algo, |
167 | GCRYCTL_GET_KEYLEN, | 167 | GCRYCTL_GET_KEYLEN, |
168 | 0, | 168 | 0, |
169 | &cipherKeylen); | 169 | &cipherKeylen); |
170 | if (err != GPG_ERR_NO_ERROR) { | 170 | if (err != GPG_ERR_NO_ERROR) { |
171 | printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_GET_KEYLEN failed: ") | 171 | printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_GET_KEYLEN failed: ") |
172 | + gcry_strerror(err)); | 172 | + gcry_strerror(err)); |
173 | ret = e_cryptNotImpl; | 173 | ret = e_cryptNotImpl; |
174 | goto out; | 174 | goto out; |
175 | } | 175 | } |
176 | // extract the salt of the encrypted data buffer | 176 | // extract the salt of the encrypted data buffer |
177 | memcpy(salt, inBuf + inBufLen - STRING2KEY_SALTLEN, STRING2KEY_SALTLEN); | 177 | memcpy(salt, inBuf + inBufLen - STRING2KEY_SALTLEN, STRING2KEY_SALTLEN); |
178 | // open the algo and get a handle | 178 | // open the algo and get a handle |
179 | err = gcry_cipher_open(&handle, | 179 | err = gcry_cipher_open(&handle, |
180 | algo, | 180 | algo, |
181 | GCRY_CIPHER_MODE_CBC, | 181 | GCRY_CIPHER_MODE_CBC, |
182 | 0); | 182 | 0); |
183 | if (err != GPG_ERR_NO_ERROR) { | 183 | if (err != GPG_ERR_NO_ERROR) { |
184 | printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_open() failed: ") | 184 | printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_open() failed: ") |
185 | + gcry_strerror(err)); | 185 | + gcry_strerror(err)); |
186 | ret = e_cryptNotImpl; | 186 | ret = e_cryptNotImpl; |
187 | goto out; | 187 | goto out; |
188 | } | 188 | } |
189 | // hash the "key" to a fixed size hash matching "cipherKeylen" | 189 | // hash the "key" to a fixed size hash matching "cipherKeylen" |
190 | hashedKey = new unsigned char[cipherKeylen]; | 190 | hashedKey = new unsigned char[cipherKeylen]; |
191 | hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, false); | 191 | hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, false); |
192 | // so now set the hashed key | 192 | // so now set the hashed key |
193 | err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); | 193 | err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); |
194 | if (err != GPG_ERR_NO_ERROR) { | 194 | if (err != GPG_ERR_NO_ERROR) { |
195 | printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_setkey() failed: ") | 195 | printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_setkey() failed: ") |
196 | + gcry_strerror(err)); | 196 | + gcry_strerror(err)); |
197 | ret = e_cryptNotImpl; | 197 | ret = e_cryptNotImpl; |
198 | delete [] hashedKey; | 198 | delete [] hashedKey; |
199 | goto out_close; | 199 | goto out_close; |
200 | } | 200 | } |
201 | delete [] hashedKey; | 201 | delete [] hashedKey; |
202 | *outBufLen = inBufLen - STRING2KEY_SALTLEN; | 202 | *outBufLen = inBufLen - STRING2KEY_SALTLEN; |
203 | *outBuf = new unsigned char[*outBufLen]; | 203 | *outBuf = new unsigned char[*outBufLen]; |
204 | // decrypt the data | 204 | // decrypt the data |
205 | err = gcry_cipher_decrypt(handle, | 205 | err = gcry_cipher_decrypt(handle, |
206 | *outBuf, | 206 | *outBuf, |
207 | *outBufLen, | 207 | *outBufLen, |
208 | inBuf, | 208 | inBuf, |
209 | *outBufLen); | 209 | *outBufLen); |
210 | if (err != GPG_ERR_NO_ERROR) { | 210 | if (err != GPG_ERR_NO_ERROR) { |
211 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") | 211 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") |
212 | + gcry_strerror(err)); | 212 | + gcry_strerror(err)); |
213 | ret = e_cryptNotImpl; | 213 | ret = e_cryptNotImpl; |
214 | goto out_delete; | 214 | goto out_delete; |
215 | } | 215 | } |
216 | // remove all random padding | 216 | // remove all random padding |
217 | unpadData(*outBuf, outBufLen); | 217 | unpadData(*outBuf, outBufLen); |
218 | goto out_close; | 218 | goto out_close; |
219 | out_delete: | 219 | out_delete: |
220 | delete [] *outBuf; | 220 | delete [] *outBuf; |
221 | out_close: | 221 | out_close: |
222 | gcry_cipher_close(handle); | 222 | gcry_cipher_close(handle); |
223 | out: | 223 | out: |
224 | return ret; | 224 | return ret; |
225 | } | 225 | } |
226 | 226 | ||
227 | PwMerror LibGCryptIf::hash(unsigned char **outBuf, | 227 | PwMerror LibGCryptIf::hash(unsigned char **outBuf, |
228 | size_t *outBufLen, | 228 | size_t *outBufLen, |
229 | const unsigned char *inBuf, | 229 | const unsigned char *inBuf, |
230 | size_t inBufLen, | 230 | size_t inBufLen, |
231 | char _algo) | 231 | char _algo) |
232 | { | 232 | { |
233 | PwMerror ret = e_success; | 233 | PwMerror ret = e_success; |
234 | unsigned int hashLen; | 234 | unsigned int hashLen; |
235 | int algo = mapHashId(_algo); | 235 | int algo = mapHashId(_algo); |
236 | 236 | ||
237 | hashLen = gcry_md_get_algo_dlen(algo); | 237 | hashLen = gcry_md_get_algo_dlen(algo); |
238 | *outBufLen = hashLen; | 238 | *outBufLen = hashLen; |
239 | *outBuf = new unsigned char[*outBufLen]; | 239 | *outBuf = new unsigned char[*outBufLen]; |
240 | gcry_md_hash_buffer(algo, | 240 | gcry_md_hash_buffer(algo, |
241 | *outBuf, | 241 | *outBuf, |
242 | inBuf, | 242 | inBuf, |
243 | inBufLen); | 243 | inBufLen); |
244 | return ret; | 244 | return ret; |
245 | } | 245 | } |
246 | 246 | ||
247 | unsigned int LibGCryptIf::hashLength(char _algo) | 247 | unsigned int LibGCryptIf::hashLength(char _algo) |
248 | { | 248 | { |
249 | unsigned int ret; | 249 | unsigned int ret; |
250 | int algo = mapHashId(_algo); | 250 | int algo = mapHashId(_algo); |
251 | ret = gcry_md_get_algo_dlen(algo); | 251 | ret = gcry_md_get_algo_dlen(algo); |
252 | return ret; | 252 | return ret; |
253 | } | 253 | } |
254 | 254 | ||
255 | int LibGCryptIf::mapCipherId(char algo) | 255 | int LibGCryptIf::mapCipherId(char algo) |
256 | { | 256 | { |
257 | switch (algo) { | 257 | switch (algo) { |
258 | case PWM_CRYPT_AES128: | 258 | case PWM_CRYPT_AES128: |
259 | return GCRY_CIPHER_AES; | 259 | return GCRY_CIPHER_AES; |
260 | case PWM_CRYPT_AES192: | 260 | case PWM_CRYPT_AES192: |
261 | return GCRY_CIPHER_AES192; | 261 | return GCRY_CIPHER_AES192; |
262 | case PWM_CRYPT_AES256: | 262 | case PWM_CRYPT_AES256: |
263 | return GCRY_CIPHER_AES256; | 263 | return GCRY_CIPHER_AES256; |
264 | case PWM_CRYPT_3DES: | 264 | case PWM_CRYPT_3DES: |
265 | return GCRY_CIPHER_3DES; | 265 | return GCRY_CIPHER_3DES; |
266 | case PWM_CRYPT_TWOFISH: | 266 | case PWM_CRYPT_TWOFISH: |
267 | return GCRY_CIPHER_TWOFISH; | 267 | return GCRY_CIPHER_TWOFISH; |
268 | case PWM_CRYPT_TWOFISH128: | 268 | case PWM_CRYPT_TWOFISH128: |
269 | return GCRY_CIPHER_TWOFISH128; | 269 | return GCRY_CIPHER_TWOFISH128; |
270 | default: | 270 | default: |
271 | BUG(); | 271 | BUG(); |
272 | } | 272 | } |
273 | return GCRY_CIPHER_NONE; | 273 | return GCRY_CIPHER_NONE; |
274 | } | 274 | } |
275 | 275 | ||
276 | int LibGCryptIf::mapHashId(char algo) | 276 | int LibGCryptIf::mapHashId(char algo) |
277 | { | 277 | { |
278 | switch (algo) { | 278 | switch (algo) { |
279 | case PWM_HASH_SHA1: | 279 | case PWM_HASH_SHA1: |
280 | return GCRY_MD_SHA1; | 280 | return GCRY_MD_SHA1; |
281 | case PWM_HASH_SHA256: | 281 | case PWM_HASH_SHA256: |
282 | return GCRY_MD_SHA256; | 282 | return GCRY_MD_SHA256; |
283 | case PWM_HASH_SHA384: | 283 | case PWM_HASH_SHA384: |
284 | return GCRY_MD_SHA384; | 284 | return GCRY_MD_SHA384; |
285 | case PWM_HASH_SHA512: | 285 | case PWM_HASH_SHA512: |
286 | return GCRY_MD_SHA512; | 286 | return GCRY_MD_SHA512; |
287 | case PWM_HASH_MD5: | 287 | case PWM_HASH_MD5: |
288 | return GCRY_MD_MD5; | 288 | return GCRY_MD_MD5; |
289 | case PWM_HASH_RMD160: | 289 | case PWM_HASH_RMD160: |
290 | return GCRY_MD_RMD160; | 290 | return GCRY_MD_RMD160; |
291 | case PWM_HASH_TIGER: | 291 | case PWM_HASH_TIGER: |
292 | return GCRY_MD_TIGER; | 292 | return GCRY_MD_TIGER; |
293 | default: | 293 | default: |
294 | BUG(); | 294 | BUG(); |
295 | } | 295 | } |
296 | return GCRY_MD_NONE; | 296 | return GCRY_MD_NONE; |
297 | } | 297 | } |
298 | 298 | ||
299 | bool LibGCryptIf::hashPassphrase(const unsigned char *pw, | 299 | bool LibGCryptIf::hashPassphrase(const unsigned char *pw, |
300 | size_t pwlen, | 300 | size_t pwlen, |
301 | unsigned char *salt, | 301 | unsigned char *salt, |
302 | unsigned char *key, | 302 | unsigned char *key, |
303 | size_t keylen, | 303 | size_t keylen, |
304 | bool create) | 304 | bool create) |
305 | { | 305 | { |
306 | DEK dek; | 306 | DEK dek; |
307 | STRING2KEY s2k; | 307 | STRING2KEY s2k; |
308 | bool ret; | 308 | bool ret; |
309 | 309 | ||
310 | dek.keylen = keylen; | 310 | dek.keylen = keylen; |
311 | s2k.mode = 1; | 311 | s2k.mode = 1; |
312 | s2k.hash_algo = mapHashId(conf()->confGlobHashAlgo()); | 312 | s2k.hash_algo = mapHashId(conf()->confGlobHashAlgo()); |
313 | s2k.count = 0; | 313 | s2k.count = 0; |
314 | if (!create) | 314 | if (!create) |
315 | memcpy(s2k.salt, salt, STRING2KEY_SALTLEN); | 315 | memcpy(s2k.salt, salt, STRING2KEY_SALTLEN); |
316 | ret = doHashPassphrase(&dek, | 316 | ret = doHashPassphrase(&dek, |
317 | pw, | 317 | pw, |
318 | pwlen, | 318 | pwlen, |
319 | &s2k, | 319 | &s2k, |
320 | create); | 320 | create); |
321 | if (!ret) | 321 | if (!ret) |
322 | goto out; | 322 | goto out; |
323 | memcpy(key, dek.key, dek.keylen); | 323 | memcpy(key, dek.key, dek.keylen); |
324 | if (create) | 324 | if (create) |
325 | memcpy(salt, s2k.salt, STRING2KEY_SALTLEN); | 325 | memcpy(salt, s2k.salt, STRING2KEY_SALTLEN); |
326 | out: | 326 | out: |
327 | return ret; | 327 | return ret; |
328 | } | 328 | } |
329 | 329 | ||
330 | 330 | ||
331 | bool LibGCryptIf::doHashPassphrase(DEK *dek, | 331 | bool LibGCryptIf::doHashPassphrase(DEK *dek, |
332 | const unsigned char *pw, | 332 | const unsigned char *pw, |
333 | size_t pwlen, | 333 | size_t pwlen, |
334 | STRING2KEY *s2k, | 334 | STRING2KEY *s2k, |
335 | bool create) | 335 | bool create) |
336 | { | 336 | { |
337 | // This function is derived from GnuPG-1.2.5-rc2 | 337 | // This function is derived from GnuPG-1.2.5-rc2 |
338 | gcry_md_hd_t md; | 338 | gcry_md_hd_t md; |
339 | gcry_error_t err; | 339 | gcry_error_t err; |
340 | bool ret = true; | 340 | bool ret = true; |
341 | size_t pass, i; | 341 | size_t pass, i; |
342 | size_t used = 0; | 342 | size_t used = 0; |
343 | 343 | ||
344 | PWM_ASSERT(s2k->hash_algo); | 344 | PWM_ASSERT(s2k->hash_algo); |
345 | BUG_ON(!(dek->keylen > 0 && dek->keylen <= array_size(dek->key))); | 345 | BUG_ON(!(dek->keylen > 0 && dek->keylen <= array_size(dek->key))); |
346 | 346 | ||
347 | err = gcry_md_open(&md, s2k->hash_algo, 0); | 347 | err = gcry_md_open(&md, s2k->hash_algo, 0); |
348 | if (err != GPG_ERR_NO_ERROR) { | 348 | if (err != GPG_ERR_NO_ERROR) { |
349 | ret = false; | 349 | ret = false; |
350 | goto out; | 350 | goto out; |
351 | } | 351 | } |
352 | for (pass = 0; used < dek->keylen; pass++) { | 352 | for (pass = 0; used < dek->keylen; pass++) { |
353 | if (pass) { | 353 | if (pass) { |
354 | gcry_md_reset(md); | 354 | gcry_md_reset(md); |
355 | for (i = 0; i < pass; i++) // preset the hash context | 355 | for (i = 0; i < pass; i++) // preset the hash context |
356 | gcry_md_putc(md, 0); | 356 | gcry_md_putc(md, 0); |
357 | } | 357 | } |
358 | if (s2k->mode == 1 || s2k->mode == 3) { | 358 | if (s2k->mode == 1 || s2k->mode == 3) { |
359 | size_t len2 = pwlen + 8; | 359 | size_t len2 = pwlen + 8; |
360 | size_t count = len2; | 360 | size_t count = len2; |
361 | 361 | ||
362 | if (create && !pass) { | 362 | if (create && !pass) { |
363 | Randomizer *rnd = Randomizer::obj(); | 363 | Randomizer *rnd = Randomizer::obj(); |
364 | const unsigned int salt_len = 8; | 364 | const unsigned int salt_len = 8; |
365 | string rndBuf(rnd->genRndBuf(salt_len)); | 365 | string rndBuf(rnd->genRndBuf(salt_len)); |
366 | memcpy(s2k->salt, rndBuf.c_str(), salt_len); | 366 | memcpy(s2k->salt, rndBuf.c_str(), salt_len); |
367 | if (s2k->mode == 3) | 367 | if (s2k->mode == 3) |
368 | s2k->count = 96; // 65536 iterations | 368 | s2k->count = 96; // 65536 iterations |
369 | } | 369 | } |
370 | if (s2k->mode == 3) { | 370 | if (s2k->mode == 3) { |
371 | count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6); | 371 | count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6); |
372 | if (count < len2) | 372 | if (count < len2) |
373 | count = len2; | 373 | count = len2; |
374 | } | 374 | } |
375 | // a little bit complicated because we need a ulong for count | 375 | // a little bit complicated because we need a ulong for count |
376 | while (count > len2) { // maybe iterated+salted | 376 | while (count > len2) { // maybe iterated+salted |
377 | gcry_md_write(md, s2k->salt, 8); | 377 | gcry_md_write(md, s2k->salt, 8); |
378 | gcry_md_write(md, pw, pwlen); | 378 | gcry_md_write(md, pw, pwlen); |
379 | count -= len2; | 379 | count -= len2; |
380 | } | 380 | } |
381 | if (count < 8) { | 381 | if (count < 8) { |
382 | gcry_md_write(md, s2k->salt, count); | 382 | gcry_md_write(md, s2k->salt, count); |
383 | } else { | 383 | } else { |
384 | gcry_md_write(md, s2k->salt, 8); | 384 | gcry_md_write(md, s2k->salt, 8); |
385 | count -= 8; | 385 | count -= 8; |
386 | gcry_md_write(md, pw, count); | 386 | gcry_md_write(md, pw, count); |
387 | } | 387 | } |
388 | } else | 388 | } else |
389 | gcry_md_write(md, pw, pwlen); | 389 | gcry_md_write(md, pw, pwlen); |
390 | gcry_md_final(md); | 390 | gcry_md_final(md); |
391 | i = gcry_md_get_algo_dlen(s2k->hash_algo); | 391 | i = gcry_md_get_algo_dlen(s2k->hash_algo); |
392 | if (i > dek->keylen - used) | 392 | if (i > dek->keylen - used) |
393 | i = dek->keylen - used; | 393 | i = dek->keylen - used; |
394 | memcpy(dek->key+used, gcry_md_read(md, s2k->hash_algo), i); | 394 | memcpy(dek->key+used, gcry_md_read(md, s2k->hash_algo), i); |
395 | used += i; | 395 | used += i; |
396 | } | 396 | } |
397 | gcry_md_close(md); | 397 | gcry_md_close(md); |
398 | out: | 398 | out: |
399 | return ret; | 399 | return ret; |
400 | } | 400 | } |
401 | 401 | ||
402 | void LibGCryptIf::padData(unsigned char *buf, | 402 | void LibGCryptIf::padData(unsigned char *buf, |
403 | size_t bufLen, | 403 | size_t bufLen, |
404 | size_t boundary) | 404 | size_t boundary) |
405 | { | 405 | { |
406 | size_t numPadBytes = boundary - ((bufLen + 1) % boundary); | 406 | size_t numPadBytes = boundary - ((bufLen + 1) % boundary); |
407 | buf[bufLen] = static_cast<char>(0x01); | 407 | buf[bufLen] = static_cast<char>(0x01); |
408 | size_t i = 0; | 408 | size_t i = 0; |
409 | Randomizer *rnd = Randomizer::obj(); | 409 | Randomizer *rnd = Randomizer::obj(); |
410 | char c; | 410 | char c; |
411 | unsigned char *b; | 411 | unsigned char *b; |
412 | while (i < numPadBytes) { | 412 | while (i < numPadBytes) { |
413 | c = rnd->genRndChar(); | 413 | c = rnd->genRndChar(); |
414 | if (c == static_cast<char>(0x01)) | 414 | if (c == static_cast<char>(0x01)) |
415 | continue; | 415 | continue; |
416 | b = buf + bufLen + 1 + i; | 416 | b = buf + bufLen + 1 + i; |
417 | *b = c; | 417 | *b = c; |
418 | ++i; | 418 | ++i; |
419 | } | 419 | } |
420 | } | 420 | } |
421 | 421 | ||
422 | void LibGCryptIf::unpadData(const unsigned char *buf, | 422 | void LibGCryptIf::unpadData(const unsigned char *buf, |
423 | size_t *bufLen) | 423 | size_t *bufLen) |
424 | { | 424 | { |
425 | size_t pos; | 425 | size_t pos; |
426 | BUG_ON(*bufLen % 8); | 426 | BUG_ON(*bufLen % 8); |
427 | pos = *bufLen - 1; | 427 | pos = *bufLen - 1; |
428 | while (buf[pos] != static_cast<char>(0x01)) { | 428 | while (buf[pos] != static_cast<char>(0x01)) { |
429 | BUG_ON(!pos); | 429 | BUG_ON(!pos); |
430 | --pos; | 430 | --pos; |
431 | } | 431 | } |
432 | *bufLen = pos; | 432 | *bufLen = pos; |
433 | } | 433 | } |
434 | 434 | ||
435 | #endif // CONFIG_PWMANAGER_GCRY | 435 | #endif // CONFIG_PWMANAGER_GCRY |
436 | |||
437 | #ifdef CONFIG_PWMANAGER_CRYPTO | ||
438 | |||
439 | #include "pwmdoc.h" | ||
440 | #include "randomizer.h" | ||
441 | |||
442 | #include <openssl/crypto.h> | ||
443 | |||
444 | PwMerror LibGCryptIf::encrypt(unsigned char **outBuf, | ||
445 | size_t *outBufLen, | ||
446 | unsigned char *inBuf, | ||
447 | size_t inBufLen, | ||
448 | const unsigned char *key, | ||
449 | size_t keylen, | ||
450 | char _algo) | ||
451 | { | ||
452 | PwMerror ret = e_success; | ||
453 | gcry_error_t err; | ||
454 | gcry_cipher_hd_t handle; | ||
455 | size_t blklen; | ||
456 | size_t unpaddedLen = inBufLen; | ||
457 | size_t cipherKeylen; | ||
458 | unsigned char *hashedKey; | ||
459 | unsigned char salt[STRING2KEY_SALTLEN]; | ||
460 | int algo = mapCipherId(_algo); | ||
461 | |||
462 | if (!inBufLen || !keylen) | ||
463 | return e_invalidArg; | ||
464 | |||
465 | // test if algo is ready for encryption | ||
466 | err = gcry_cipher_algo_info(algo, | ||
467 | GCRYCTL_TEST_ALGO, | ||
468 | 0, 0); | ||
469 | if (err != GPG_ERR_NO_ERROR) { | ||
470 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_TEST_ALGO failed: ") | ||
471 | + gcry_strerror(err)); | ||
472 | ret = e_cryptNotImpl; | ||
473 | goto out; | ||
474 | } | ||
475 | // get the algo block length | ||
476 | err = gcry_cipher_algo_info(algo, | ||
477 | GCRYCTL_GET_BLKLEN, | ||
478 | 0, | ||
479 | &blklen); | ||
480 | if (err != GPG_ERR_NO_ERROR) { | ||
481 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_BLKLEN failed: ") | ||
482 | + gcry_strerror(err)); | ||
483 | ret = e_cryptNotImpl; | ||
484 | goto out; | ||
485 | } | ||
486 | /* double check if we have enough space. | ||
487 | * We have only 1024 extra bytes for padding and salt. | ||
488 | */ | ||
489 | BUG_ON(blklen > 1024 - STRING2KEY_SALTLEN); | ||
490 | // get the algo key length | ||
491 | err = gcry_cipher_algo_info(algo, | ||
492 | GCRYCTL_GET_KEYLEN, | ||
493 | 0, | ||
494 | &cipherKeylen); | ||
495 | if (err != GPG_ERR_NO_ERROR) { | ||
496 | printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_KEYLEN failed: ") | ||
497 | + gcry_strerror(err)); | ||
498 | ret = e_cryptNotImpl; | ||
499 | goto out; | ||
500 | } | ||
501 | // now open the algo and get a handle | ||
502 | err = gcry_cipher_open(&handle, | ||
503 | algo, | ||
504 | GCRY_CIPHER_MODE_CBC, | ||
505 | 0); | ||
506 | if (err != GPG_ERR_NO_ERROR) { | ||
507 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_open() failed: ") | ||
508 | + gcry_strerror(err)); | ||
509 | ret = e_cryptNotImpl; | ||
510 | goto out; | ||
511 | } | ||
512 | // hash the "key" to a fixed size hash matching "cipherKeylen" | ||
513 | hashedKey = new unsigned char[cipherKeylen]; | ||
514 | hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, true); | ||
515 | // so now set the hashed key | ||
516 | err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); | ||
517 | if (err != GPG_ERR_NO_ERROR) { | ||
518 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_setkey() failed: ") | ||
519 | + gcry_strerror(err)); | ||
520 | ret = e_cryptNotImpl; | ||
521 | delete [] hashedKey; | ||
522 | goto out_close; | ||
523 | } | ||
524 | delete [] hashedKey; | ||
525 | /* allocate a buffer for the encrypted data. | ||
526 | * The size of the buffer is the inBuf length, but blklen | ||
527 | * aligned and plus the length of the salt, that is appended. | ||
528 | */ | ||
529 | *outBufLen = getBufLen(unpaddedLen, blklen) + STRING2KEY_SALTLEN; | ||
530 | *outBuf = new unsigned char[*outBufLen]; | ||
531 | padData(inBuf, unpaddedLen, blklen); | ||
532 | // encrypt the padded data | ||
533 | err = gcry_cipher_encrypt(handle, | ||
534 | *outBuf, | ||
535 | *outBufLen - STRING2KEY_SALTLEN, | ||
536 | inBuf, | ||
537 | *outBufLen - STRING2KEY_SALTLEN); | ||
538 | if (err != GPG_ERR_NO_ERROR) { | ||
539 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") | ||
540 | + gcry_strerror(err)); | ||
541 | ret = e_cryptNotImpl; | ||
542 | goto out_delete; | ||
543 | } | ||
544 | // append the salt to the encrypted data | ||
545 | memcpy(*outBuf + *outBufLen - STRING2KEY_SALTLEN, salt, STRING2KEY_SALTLEN); | ||
546 | goto out_close; | ||
547 | out_delete: | ||
548 | delete [] *outBuf; | ||
549 | out_close: | ||
550 | gcry_cipher_close(handle); | ||
551 | out: | ||
552 | return ret; | ||
553 | } | ||
554 | |||
555 | PwMerror LibGCryptIf::decrypt(unsigned char **outBuf, | ||
556 | size_t *outBufLen, | ||
557 | const unsigned char *inBuf, | ||
558 | size_t inBufLen, | ||
559 | const unsigned char *key, | ||
560 | size_t keylen, | ||
561 | char _algo) | ||
562 | { | ||
563 | PwMerror ret = e_success; | ||
564 | gcry_error_t err; | ||
565 | gcry_cipher_hd_t handle; | ||
566 | size_t cipherKeylen; | ||
567 | unsigned char *hashedKey; | ||
568 | unsigned char salt[STRING2KEY_SALTLEN]; | ||
569 | int algo = mapCipherId(_algo); | ||
570 | |||
571 | if (!inBufLen || !keylen) | ||
572 | return e_invalidArg; | ||
573 | |||
574 | // test if algo is ready for encryption | ||
575 | err = gcry_cipher_algo_info(algo, | ||
576 | GCRYCTL_TEST_ALGO, | ||
577 | 0, 0); | ||
578 | if (err != GPG_ERR_NO_ERROR) { | ||
579 | printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_TEST_ALGO failed: ") | ||
580 | + gcry_strerror(err)); | ||
581 | ret = e_cryptNotImpl; | ||
582 | goto out; | ||
583 | } | ||
584 | // get algo key length | ||
585 | err = gcry_cipher_algo_info(algo, | ||
586 | GCRYCTL_GET_KEYLEN, | ||
587 | 0, | ||
588 | &cipherKeylen); | ||
589 | if (err != GPG_ERR_NO_ERROR) { | ||
590 | printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_GET_KEYLEN failed: ") | ||
591 | + gcry_strerror(err)); | ||
592 | ret = e_cryptNotImpl; | ||
593 | goto out; | ||
594 | } | ||
595 | // extract the salt of the encrypted data buffer | ||
596 | memcpy(salt, inBuf + inBufLen - STRING2KEY_SALTLEN, STRING2KEY_SALTLEN); | ||
597 | // open the algo and get a handle | ||
598 | err = gcry_cipher_open(&handle, | ||
599 | algo, | ||
600 | GCRY_CIPHER_MODE_CBC, | ||
601 | 0); | ||
602 | if (err != GPG_ERR_NO_ERROR) { | ||
603 | printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_open() failed: ") | ||
604 | + gcry_strerror(err)); | ||
605 | ret = e_cryptNotImpl; | ||
606 | goto out; | ||
607 | } | ||
608 | // hash the "key" to a fixed size hash matching "cipherKeylen" | ||
609 | hashedKey = new unsigned char[cipherKeylen]; | ||
610 | hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, false); | ||
611 | // so now set the hashed key | ||
612 | err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen); | ||
613 | if (err != GPG_ERR_NO_ERROR) { | ||
614 | printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_setkey() failed: ") | ||
615 | + gcry_strerror(err)); | ||
616 | ret = e_cryptNotImpl; | ||
617 | delete [] hashedKey; | ||
618 | goto out_close; | ||
619 | } | ||
620 | delete [] hashedKey; | ||
621 | *outBufLen = inBufLen - STRING2KEY_SALTLEN; | ||
622 | *outBuf = new unsigned char[*outBufLen]; | ||
623 | // decrypt the data | ||
624 | err = gcry_cipher_decrypt(handle, | ||
625 | *outBuf, | ||
626 | *outBufLen, | ||
627 | inBuf, | ||
628 | *outBufLen); | ||
629 | if (err != GPG_ERR_NO_ERROR) { | ||
630 | printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ") | ||
631 | + gcry_strerror(err)); | ||
632 | ret = e_cryptNotImpl; | ||
633 | goto out_delete; | ||
634 | } | ||
635 | // remove all random padding | ||
636 | unpadData(*outBuf, outBufLen); | ||
637 | goto out_close; | ||
638 | out_delete: | ||
639 | delete [] *outBuf; | ||
640 | out_close: | ||
641 | gcry_cipher_close(handle); | ||
642 | out: | ||
643 | return ret; | ||
644 | } | ||
645 | |||
646 | PwMerror LibGCryptIf::hash(unsigned char **outBuf, | ||
647 | size_t *outBufLen, | ||
648 | const unsigned char *inBuf, | ||
649 | size_t inBufLen, | ||
650 | char _algo) | ||
651 | { | ||
652 | PwMerror ret = e_success; | ||
653 | unsigned int hashLen; | ||
654 | int algo = mapHashId(_algo); | ||
655 | |||
656 | hashLen = gcry_md_get_algo_dlen(algo); | ||
657 | *outBufLen = hashLen; | ||
658 | *outBuf = new unsigned char[*outBufLen]; | ||
659 | gcry_md_hash_buffer(algo, | ||
660 | *outBuf, | ||
661 | inBuf, | ||
662 | inBufLen); | ||
663 | return ret; | ||
664 | } | ||
665 | |||
666 | unsigned int LibGCryptIf::hashLength(char _algo) | ||
667 | { | ||
668 | unsigned int ret; | ||
669 | int algo = mapHashId(_algo); | ||
670 | ret = gcry_md_get_algo_dlen(algo); | ||
671 | return ret; | ||
672 | } | ||
673 | |||
674 | int LibGCryptIf::mapCipherId(char algo) | ||
675 | { | ||
676 | switch (algo) { | ||
677 | case PWM_CRYPT_AES128: | ||
678 | return GCRY_CIPHER_AES; | ||
679 | case PWM_CRYPT_AES192: | ||
680 | return GCRY_CIPHER_AES192; | ||
681 | case PWM_CRYPT_AES256: | ||
682 | return GCRY_CIPHER_AES256; | ||
683 | case PWM_CRYPT_3DES: | ||
684 | return GCRY_CIPHER_3DES; | ||
685 | case PWM_CRYPT_TWOFISH: | ||
686 | return GCRY_CIPHER_TWOFISH; | ||
687 | case PWM_CRYPT_TWOFISH128: | ||
688 | return GCRY_CIPHER_TWOFISH128; | ||
689 | default: | ||
690 | BUG(); | ||
691 | } | ||
692 | return GCRY_CIPHER_NONE; | ||
693 | } | ||
694 | |||
695 | int LibGCryptIf::mapHashId(char algo) | ||
696 | { | ||
697 | switch (algo) { | ||
698 | case PWM_HASH_SHA1: | ||
699 | return GCRY_MD_SHA1; | ||
700 | case PWM_HASH_SHA256: | ||
701 | return GCRY_MD_SHA256; | ||
702 | case PWM_HASH_SHA384: | ||
703 | return GCRY_MD_SHA384; | ||
704 | case PWM_HASH_SHA512: | ||
705 | return GCRY_MD_SHA512; | ||
706 | case PWM_HASH_MD5: | ||
707 | return GCRY_MD_MD5; | ||
708 | case PWM_HASH_RMD160: | ||
709 | return GCRY_MD_RMD160; | ||
710 | case PWM_HASH_TIGER: | ||
711 | return GCRY_MD_TIGER; | ||
712 | default: | ||
713 | BUG(); | ||
714 | } | ||
715 | return GCRY_MD_NONE; | ||
716 | } | ||
717 | |||
718 | bool LibGCryptIf::hashPassphrase(const unsigned char *pw, | ||
719 | size_t pwlen, | ||
720 | unsigned char *salt, | ||
721 | unsigned char *key, | ||
722 | size_t keylen, | ||
723 | bool create) | ||
724 | { | ||
725 | DEK dek; | ||
726 | STRING2KEY s2k; | ||
727 | bool ret; | ||
728 | |||
729 | dek.keylen = keylen; | ||
730 | s2k.mode = 1; | ||
731 | s2k.hash_algo = mapHashId(conf()->confGlobHashAlgo()); | ||
732 | s2k.count = 0; | ||
733 | if (!create) | ||
734 | memcpy(s2k.salt, salt, STRING2KEY_SALTLEN); | ||
735 | ret = doHashPassphrase(&dek, | ||
736 | pw, | ||
737 | pwlen, | ||
738 | &s2k, | ||
739 | create); | ||
740 | if (!ret) | ||
741 | goto out; | ||
742 | memcpy(key, dek.key, dek.keylen); | ||
743 | if (create) | ||
744 | memcpy(salt, s2k.salt, STRING2KEY_SALTLEN); | ||
745 | out: | ||
746 | return ret; | ||
747 | } | ||
748 | |||
749 | |||
750 | bool LibGCryptIf::doHashPassphrase(DEK *dek, | ||
751 | const unsigned char *pw, | ||
752 | size_t pwlen, | ||
753 | STRING2KEY *s2k, | ||
754 | bool create) | ||
755 | { | ||
756 | // This function is derived from GnuPG-1.2.5-rc2 | ||
757 | gcry_md_hd_t md; | ||
758 | gcry_error_t err; | ||
759 | bool ret = true; | ||
760 | size_t pass, i; | ||
761 | size_t used = 0; | ||
762 | |||
763 | PWM_ASSERT(s2k->hash_algo); | ||
764 | BUG_ON(!(dek->keylen > 0 && dek->keylen <= array_size(dek->key))); | ||
765 | |||
766 | err = gcry_md_open(&md, s2k->hash_algo, 0); | ||
767 | if (err != GPG_ERR_NO_ERROR) { | ||
768 | ret = false; | ||
769 | goto out; | ||
770 | } | ||
771 | for (pass = 0; used < dek->keylen; pass++) { | ||
772 | if (pass) { | ||
773 | gcry_md_reset(md); | ||
774 | for (i = 0; i < pass; i++) // preset the hash context | ||
775 | gcry_md_putc(md, 0); | ||
776 | } | ||
777 | if (s2k->mode == 1 || s2k->mode == 3) { | ||
778 | size_t len2 = pwlen + 8; | ||
779 | size_t count = len2; | ||
780 | |||
781 | if (create && !pass) { | ||
782 | Randomizer *rnd = Randomizer::obj(); | ||
783 | const unsigned int salt_len = 8; | ||
784 | string rndBuf(rnd->genRndBuf(salt_len)); | ||
785 | memcpy(s2k->salt, rndBuf.c_str(), salt_len); | ||
786 | if (s2k->mode == 3) | ||
787 | s2k->count = 96; // 65536 iterations | ||
788 | } | ||
789 | if (s2k->mode == 3) { | ||
790 | count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6); | ||
791 | if (count < len2) | ||
792 | count = len2; | ||
793 | } | ||
794 | // a little bit complicated because we need a ulong for count | ||
795 | while (count > len2) { // maybe iterated+salted | ||
796 | gcry_md_write(md, s2k->salt, 8); | ||
797 | gcry_md_write(md, pw, pwlen); | ||
798 | count -= len2; | ||
799 | } | ||
800 | if (count < 8) { | ||
801 | gcry_md_write(md, s2k->salt, count); | ||
802 | } else { | ||
803 | gcry_md_write(md, s2k->salt, 8); | ||
804 | count -= 8; | ||
805 | gcry_md_write(md, pw, count); | ||
806 | } | ||
807 | } else | ||
808 | gcry_md_write(md, pw, pwlen); | ||
809 | gcry_md_final(md); | ||
810 | i = gcry_md_get_algo_dlen(s2k->hash_algo); | ||
811 | if (i > dek->keylen - used) | ||
812 | i = dek->keylen - used; | ||
813 | memcpy(dek->key+used, gcry_md_read(md, s2k->hash_algo), i); | ||
814 | used += i; | ||
815 | } | ||
816 | gcry_md_close(md); | ||
817 | out: | ||
818 | return ret; | ||
819 | } | ||
820 | |||
821 | void LibGCryptIf::padData(unsigned char *buf, | ||
822 | size_t bufLen, | ||
823 | size_t boundary) | ||
824 | { | ||
825 | size_t numPadBytes = boundary - ((bufLen + 1) % boundary); | ||
826 | buf[bufLen] = static_cast<char>(0x01); | ||
827 | size_t i = 0; | ||
828 | Randomizer *rnd = Randomizer::obj(); | ||
829 | char c; | ||
830 | unsigned char *b; | ||
831 | while (i < numPadBytes) { | ||
832 | c = rnd->genRndChar(); | ||
833 | if (c == static_cast<char>(0x01)) | ||
834 | continue; | ||
835 | b = buf + bufLen + 1 + i; | ||
836 | *b = c; | ||
837 | ++i; | ||
838 | } | ||
839 | } | ||
840 | |||
841 | void LibGCryptIf::unpadData(const unsigned char *buf, | ||
842 | size_t *bufLen) | ||
843 | { | ||
844 | size_t pos; | ||
845 | BUG_ON(*bufLen % 8); | ||
846 | pos = *bufLen - 1; | ||
847 | while (buf[pos] != static_cast<char>(0x01)) { | ||
848 | BUG_ON(!pos); | ||
849 | --pos; | ||
850 | } | ||
851 | *bufLen = pos; | ||
852 | } | ||
853 | |||
854 | #endif // CONFIG_PWMANAGER_CRYPTO | ||
855 | |||
diff --git a/pwmanager/pwmanager/libgcryptif.h b/pwmanager/pwmanager/libgcryptif.h index e86d638..7390827 100644 --- a/pwmanager/pwmanager/libgcryptif.h +++ b/pwmanager/pwmanager/libgcryptif.h | |||
@@ -1,158 +1,161 @@ | |||
1 | /*************************************************************************** | 1 | /*************************************************************************** |
2 | * * | 2 | * * |
3 | * copyright (C) 2004 by Michael Buesch * | 3 | * copyright (C) 2004 by Michael Buesch * |
4 | * email: mbuesch@freenet.de * | 4 | * email: mbuesch@freenet.de * |
5 | * * | 5 | * * |
6 | * hashPassphrase() is derived from GnuPG and is * | 6 | * hashPassphrase() is derived from GnuPG and is * |
7 | * Copyright (C) 1998, 1999, 2000, 2001, 2003 * | 7 | * Copyright (C) 1998, 1999, 2000, 2001, 2003 * |
8 | * Free Software Foundation, Inc. * | 8 | * Free Software Foundation, Inc. * |
9 | * * | 9 | * * |
10 | * This program is free software; you can redistribute it and/or modify * | 10 | * This program is free software; you can redistribute it and/or modify * |
11 | * it under the terms of the GNU General Public License version 2 * | 11 | * it under the terms of the GNU General Public License version 2 * |
12 | * as published by the Free Software Foundation. * | 12 | * as published by the Free Software Foundation. * |
13 | * * | 13 | * * |
14 | ***************************************************************************/ | 14 | ***************************************************************************/ |
15 | 15 | ||
16 | #ifndef __LIBGCRYPTIF_H | 16 | #ifndef __LIBGCRYPTIF_H |
17 | #define __LIBGCRYPTIF_H | 17 | #define __LIBGCRYPTIF_H |
18 | 18 | ||
19 | #include "pwmexception.h" | 19 | #include "pwmexception.h" |
20 | 20 | ||
21 | //US ENH: should we put this better into globalstuff.h? | ||
22 | #define CONFIG_PWMANAGER_CRYPTO | ||
23 | |||
21 | //#undef CONFIG_PWMANAGER_GCRY // for debugging only. | 24 | //#undef CONFIG_PWMANAGER_GCRY // for debugging only. |
22 | #ifdef CONFIG_PWMANAGER_GCRY | 25 | #if defined CONFIG_PWMANAGER_GCRY || defined CONFIG_PWMANAGER_CRYPTO |
23 | 26 | ||
24 | #include <stddef.h> | 27 | #include <stddef.h> |
25 | #include <sys/types.h> | 28 | #include <sys/types.h> |
26 | #include <stdint.h> | 29 | #include <stdint.h> |
27 | 30 | ||
28 | #define STRING2KEY_SALTLEN8 | 31 | #define STRING2KEY_SALTLEN8 |
29 | 32 | ||
30 | /** interface class for the libgcrypt cipher and hash algorithms | 33 | /** interface class for the libgcrypt cipher and hash algorithms |
31 | * NOTE: Always allocate 1024 extra bytes for the inBuf (for padding) | 34 | * NOTE: Always allocate 1024 extra bytes for the inBuf (for padding) |
32 | */ | 35 | */ |
33 | class LibGCryptIf | 36 | class LibGCryptIf |
34 | { | 37 | { |
35 | protected: | 38 | protected: |
36 | struct STRING2KEY | 39 | struct STRING2KEY |
37 | { | 40 | { |
38 | int mode; | 41 | int mode; |
39 | int hash_algo; | 42 | int hash_algo; |
40 | uint8_t salt[STRING2KEY_SALTLEN]; | 43 | uint8_t salt[STRING2KEY_SALTLEN]; |
41 | uint32_t count; | 44 | uint32_t count; |
42 | }; | 45 | }; |
43 | struct DEK | 46 | struct DEK |
44 | { | 47 | { |
45 | size_t keylen; | 48 | size_t keylen; |
46 | uint8_t key[32]; // this is the largest used keylen (256 bit) | 49 | uint8_t key[32]; // this is the largest used keylen (256 bit) |
47 | }; | 50 | }; |
48 | 51 | ||
49 | public: | 52 | public: |
50 | LibGCryptIf() { } | 53 | LibGCryptIf() { } |
51 | /** is libgcrypt available? */ | 54 | /** is libgcrypt available? */ |
52 | static bool available() | 55 | static bool available() |
53 | { return true; } | 56 | { return true; } |
54 | /** encrypt data. _algo is the PWM_CRYPT_* ID | 57 | /** encrypt data. _algo is the PWM_CRYPT_* ID |
55 | * of the algorithm. | 58 | * of the algorithm. |
56 | */ | 59 | */ |
57 | PwMerror encrypt(unsigned char **outBuf, | 60 | PwMerror encrypt(unsigned char **outBuf, |
58 | size_t *outBufLen, | 61 | size_t *outBufLen, |
59 | unsigned char *inBuf, | 62 | unsigned char *inBuf, |
60 | size_t inBufLen, | 63 | size_t inBufLen, |
61 | const unsigned char *key, | 64 | const unsigned char *key, |
62 | size_t keylen, | 65 | size_t keylen, |
63 | char _algo); | 66 | char _algo); |
64 | /** decrypt data. _algo is the PWM_CRYPT_* ID | 67 | /** decrypt data. _algo is the PWM_CRYPT_* ID |
65 | * of the algorithm. | 68 | * of the algorithm. |
66 | */ | 69 | */ |
67 | PwMerror decrypt(unsigned char **outBuf, | 70 | PwMerror decrypt(unsigned char **outBuf, |
68 | size_t *outBufLen, | 71 | size_t *outBufLen, |
69 | const unsigned char *inBuf, | 72 | const unsigned char *inBuf, |
70 | size_t inBufLen, | 73 | size_t inBufLen, |
71 | const unsigned char *key, | 74 | const unsigned char *key, |
72 | size_t keylen, | 75 | size_t keylen, |
73 | char _algo); | 76 | char _algo); |
74 | /** hash data. _algo is the PWM_HASH_* ID of the hash */ | 77 | /** hash data. _algo is the PWM_HASH_* ID of the hash */ |
75 | PwMerror hash(unsigned char **outBuf, | 78 | PwMerror hash(unsigned char **outBuf, |
76 | size_t *outBufLen, | 79 | size_t *outBufLen, |
77 | const unsigned char *inBuf, | 80 | const unsigned char *inBuf, |
78 | size_t inBufLen, | 81 | size_t inBufLen, |
79 | char _algo); | 82 | char _algo); |
80 | /** returns the length of the hash. _algo is the PWM_HASH_* | 83 | /** returns the length of the hash. _algo is the PWM_HASH_* |
81 | * id of the hash. returns 0 on error. | 84 | * id of the hash. returns 0 on error. |
82 | */ | 85 | */ |
83 | unsigned int hashLength(char _algo); | 86 | unsigned int hashLength(char _algo); |
84 | 87 | ||
85 | protected: | 88 | protected: |
86 | /** returns the total buffer length */ | 89 | /** returns the total buffer length */ |
87 | size_t getBufLen(size_t inBufLen, size_t boundary) | 90 | size_t getBufLen(size_t inBufLen, size_t boundary) |
88 | { | 91 | { |
89 | return ((boundary - (inBufLen % boundary)) + inBufLen); | 92 | return ((boundary - (inBufLen % boundary)) + inBufLen); |
90 | } | 93 | } |
91 | /** pad the data up to the given boundary. | 94 | /** pad the data up to the given boundary. |
92 | * "buf" has to be big enough! | 95 | * "buf" has to be big enough! |
93 | */ | 96 | */ |
94 | void padData(unsigned char *buf, | 97 | void padData(unsigned char *buf, |
95 | size_t bufLen, | 98 | size_t bufLen, |
96 | size_t boundary); | 99 | size_t boundary); |
97 | /** unpad the data */ | 100 | /** unpad the data */ |
98 | void unpadData(const unsigned char *buf, | 101 | void unpadData(const unsigned char *buf, |
99 | size_t *bufLen); | 102 | size_t *bufLen); |
100 | /** maps the PWM_CRYPT_* ID of an algorithm | 103 | /** maps the PWM_CRYPT_* ID of an algorithm |
101 | * to the libgcrypt GCRY_CIPHER_* ID | 104 | * to the libgcrypt GCRY_CIPHER_* ID |
102 | */ | 105 | */ |
103 | int mapCipherId(char algo); | 106 | int mapCipherId(char algo); |
104 | /** maps the PWM_HASH_* ID of an algorithm | 107 | /** maps the PWM_HASH_* ID of an algorithm |
105 | * to the libgcrypt GCRY_MD_* ID | 108 | * to the libgcrypt GCRY_MD_* ID |
106 | */ | 109 | */ |
107 | int mapHashId(char algo); | 110 | int mapHashId(char algo); |
108 | /** hash a passphrase to a cipher key */ | 111 | /** hash a passphrase to a cipher key */ |
109 | bool hashPassphrase(const unsigned char *pw, | 112 | bool hashPassphrase(const unsigned char *pw, |
110 | size_t pwlen, | 113 | size_t pwlen, |
111 | unsigned char *salt, | 114 | unsigned char *salt, |
112 | unsigned char *key, | 115 | unsigned char *key, |
113 | size_t keylen, | 116 | size_t keylen, |
114 | bool create); | 117 | bool create); |
115 | /** hash a passphrase to a cipher key */ | 118 | /** hash a passphrase to a cipher key */ |
116 | bool doHashPassphrase(DEK *dek, | 119 | bool doHashPassphrase(DEK *dek, |
117 | const unsigned char *pw, | 120 | const unsigned char *pw, |
118 | size_t pwlen, | 121 | size_t pwlen, |
119 | STRING2KEY *s2k, | 122 | STRING2KEY *s2k, |
120 | bool create); | 123 | bool create); |
121 | }; | 124 | }; |
122 | 125 | ||
123 | 126 | ||
124 | #else // CONFIG_PWMANAGER_GCRY | 127 | #else // CONFIG_PWMANAGER_GCRY |
125 | /** libgcrypt is not installed. This is a NOP wrapper. */ | 128 | /** libgcrypt is not installed. This is a NOP wrapper. */ |
126 | class LibGCryptIf | 129 | class LibGCryptIf |
127 | { | 130 | { |
128 | public: | 131 | public: |
129 | LibGCryptIf() { } | 132 | LibGCryptIf() { } |
130 | static bool available() | 133 | static bool available() |
131 | { return false; } | 134 | { return false; } |
132 | PwMerror encrypt(unsigned char **, | 135 | PwMerror encrypt(unsigned char **, |
133 | size_t *, | 136 | size_t *, |
134 | unsigned char *, | 137 | unsigned char *, |
135 | size_t, | 138 | size_t, |
136 | const unsigned char *, | 139 | const unsigned char *, |
137 | size_t, | 140 | size_t, |
138 | char) | 141 | char) |
139 | { return e_cryptNotImpl; } | 142 | { return e_cryptNotImpl; } |
140 | PwMerror decrypt(unsigned char **, | 143 | PwMerror decrypt(unsigned char **, |
141 | size_t *, | 144 | size_t *, |
142 | const unsigned char *, | 145 | const unsigned char *, |
143 | size_t, | 146 | size_t, |
144 | const unsigned char *, | 147 | const unsigned char *, |
145 | size_t, | 148 | size_t, |
146 | char) | 149 | char) |
147 | { return e_cryptNotImpl; } | 150 | { return e_cryptNotImpl; } |
148 | PwMerror hash(unsigned char **, | 151 | PwMerror hash(unsigned char **, |
149 | size_t *, | 152 | size_t *, |
150 | const unsigned char *, | 153 | const unsigned char *, |
151 | size_t, | 154 | size_t, |
152 | char) | 155 | char) |
153 | { return e_hashNotImpl; } | 156 | { return e_hashNotImpl; } |
154 | unsigned int hashLength(char) | 157 | unsigned int hashLength(char) |
155 | { return 0; } | 158 | { return 0; } |
156 | }; | 159 | }; |
157 | #endif // CONFIG_PWMANAGER_GCRY | 160 | #endif // CONFIG_PWMANAGER_GCRY |
158 | #endif // __LIBGCRYPTIF_H | 161 | #endif // __LIBGCRYPTIF_H |
diff --git a/pwmanager/pwmanager/pwmanagerE.pro b/pwmanager/pwmanager/pwmanagerE.pro index 52d7586..294f549 100644 --- a/pwmanager/pwmanager/pwmanagerE.pro +++ b/pwmanager/pwmanager/pwmanagerE.pro | |||
@@ -1,158 +1,160 @@ | |||
1 | TEMPLATE= app | 1 | TEMPLATE= app |
2 | CONFIG += qt warn_on | 2 | CONFIG += qt warn_on |
3 | 3 | ||
4 | 4 | ||
5 | TARGET = pwmpi | 5 | TARGET = pwmpi |
6 | OBJECTS_DIR = obj/$(PLATFORM) | 6 | OBJECTS_DIR = obj/$(PLATFORM) |
7 | MOC_DIR = moc/$(PLATFORM) | 7 | MOC_DIR = moc/$(PLATFORM) |
8 | DESTDIR=$(QPEDIR)/bin | 8 | DESTDIR=$(QPEDIR)/bin |
9 | 9 | ||
10 | INCLUDEPATH += . ../../ ../../qtcompat ../../qtcompat/xml ../../libkdepim ../../microkde ../../microkde/kdecore ../../microkde/kdeui ../../microkde/kutils $(QPEDIR)/include | 10 | INCLUDEPATH += . ../../ ../../qtcompat ../../qtcompat/xml ../../libkdepim ../../microkde ../../microkde/kdecore ../../microkde/kdeui ../../microkde/kutils $(QPEDIR)/include |
11 | DEFINES += PWM_EMBEDDED | 11 | DEFINES += PWM_EMBEDDED |
12 | #enable this setting if you want debugoutput for pwmanager | 12 | #enable this setting if you want debugoutput for pwmanager |
13 | #DEFINES += CONFIG_DEBUG | 13 | DEFINES += CONFIG_DEBUG |
14 | 14 | ||
15 | LIBS += -lmicrokde | 15 | LIBS += -lmicrokde |
16 | LIBS += -lmicroqtcompat | 16 | LIBS += -lmicroqtcompat |
17 | LIBS += -lmicrokdepim | 17 | LIBS += -lmicrokdepim |
18 | LIBS += -L$(QPEDIR)/lib | 18 | LIBS += -L$(QPEDIR)/lib |
19 | LIBS += -lqpe | 19 | LIBS += -lqpe |
20 | LIBS += -lz | 20 | LIBS += -lz |
21 | LIBS += -lbz2 | 21 | #LIBS += -lbz2 |
22 | LIBS += -lcrypto | 22 | LIBS += -lcrypto |
23 | LIBS += $(QTOPIALIB) | 23 | LIBS += $(QTOPIALIB) |
24 | 24 | ||
25 | #INTERFACES = \ | 25 | #INTERFACES = \ |
26 | #addentrywnd.ui \ | 26 | #addentrywnd.ui \ |
27 | #configwnd.ui \ | 27 | #configwnd.ui \ |
28 | #findwnd.ui \ | 28 | #findwnd.ui \ |
29 | #getmasterpwwnd.ui \ | 29 | #getmasterpwwnd.ui \ |
30 | #pwgenwnd.ui \ | 30 | #pwgenwnd.ui \ |
31 | #setmasterpwwnd.ui \ | 31 | #setmasterpwwnd.ui \ |
32 | #subtbledit.ui | 32 | #subtbledit.ui |
33 | 33 | ||
34 | #INTERFACES = \ | 34 | #INTERFACES = \ |
35 | #subtbledit.ui \ | 35 | #subtbledit.ui \ |
36 | 36 | ||
37 | 37 | ||
38 | 38 | ||
39 | #HEADERS = \ | 39 | #HEADERS = \ |
40 | #configuration_31compat.h \ | 40 | #configuration_31compat.h \ |
41 | #configuration.h \ | 41 | #configuration.h \ |
42 | #configwnd.h \ | 42 | #configwnd.h \ |
43 | #configwndimpl.h \ | 43 | #configwndimpl.h \ |
44 | #selftest.h | 44 | #selftest.h |
45 | #subtbledit.h \ | 45 | #subtbledit.h \ |
46 | #subtbleditimpl.h \ | 46 | #subtbleditimpl.h \ |
47 | #compressbzip2.h \ | ||
47 | 48 | ||
48 | HEADERS = \ | 49 | HEADERS = \ |
49 | addentrywnd_emb.h \ | 50 | addentrywnd_emb.h \ |
50 | addentrywndimpl.h \ | 51 | addentrywndimpl.h \ |
51 | base64.h \ | 52 | base64.h \ |
52 | binentrygen.h \ | 53 | binentrygen.h \ |
53 | blowfish.h \ | 54 | blowfish.h \ |
54 | commentbox.h \ | 55 | commentbox.h \ |
55 | compiler.h \ | 56 | compiler.h \ |
56 | compressbzip2.h \ | ||
57 | compressgzip.h \ | 57 | compressgzip.h \ |
58 | findwnd_emb.h \ | 58 | findwnd_emb.h \ |
59 | findwndimpl.h \ | 59 | findwndimpl.h \ |
60 | genpasswd.h \ | 60 | genpasswd.h \ |
61 | getkeycardwnd.h \ | 61 | getkeycardwnd.h \ |
62 | getmasterpwwnd_emb.h \ | 62 | getmasterpwwnd_emb.h \ |
63 | getmasterpwwndimpl.h \ | 63 | getmasterpwwndimpl.h \ |
64 | globalstuff.h \ | 64 | globalstuff.h \ |
65 | gpasmanfile.h \ | 65 | gpasmanfile.h \ |
66 | htmlgen.h \ | 66 | htmlgen.h \ |
67 | htmlparse.h \ | 67 | htmlparse.h \ |
68 | ipc.h \ | 68 | ipc.h \ |
69 | libgcryptif.h \ | ||
69 | listobjselectwnd.h \ | 70 | listobjselectwnd.h \ |
70 | listviewpwm.h \ | 71 | listviewpwm.h \ |
71 | printtext.h \ | 72 | printtext.h \ |
72 | pwgenwnd_emb.h \ | 73 | pwgenwnd_emb.h \ |
73 | pwgenwndimpl.h \ | 74 | pwgenwndimpl.h \ |
74 | pwmdoc.h \ | 75 | pwmdoc.h \ |
75 | pwmdocui.h \ | 76 | pwmdocui.h \ |
76 | pwmexception.h \ | 77 | pwmexception.h \ |
77 | pwm.h \ | 78 | pwm.h \ |
78 | pwminit.h \ | 79 | pwminit.h \ |
79 | pwmprefs.h \ | 80 | pwmprefs.h \ |
80 | pwmprint.h \ | 81 | pwmprint.h \ |
81 | pwmtray.h \ | 82 | pwmtray.h \ |
82 | pwmview.h \ | 83 | pwmview.h \ |
83 | pwmviewstyle_0.h \ | 84 | pwmviewstyle_0.h \ |
84 | pwmviewstyle_1.h \ | 85 | pwmviewstyle_1.h \ |
85 | pwmviewstyle.h \ | 86 | pwmviewstyle.h \ |
86 | randomizer.h \ | 87 | randomizer.h \ |
87 | rc2.h \ | 88 | rc2.h \ |
88 | rencatwnd.h \ | 89 | rencatwnd.h \ |
89 | serializer.h \ | 90 | serializer.h \ |
90 | setmasterpwwnd_emb.h \ | 91 | setmasterpwwnd_emb.h \ |
91 | setmasterpwwndimpl.h \ | 92 | setmasterpwwndimpl.h \ |
92 | sha1.h \ | 93 | sha1.h \ |
93 | waitwnd.h \ | 94 | waitwnd.h \ |
94 | kcmconfigs/kcmpwmconfig.h \ | 95 | kcmconfigs/kcmpwmconfig.h \ |
95 | kcmconfigs/pwmconfigwidget.h \ | 96 | kcmconfigs/pwmconfigwidget.h \ |
96 | 97 | ||
97 | #sources that need not be build | 98 | #sources that need not be build |
98 | #SOURCES = \ | 99 | #SOURCES = \ |
99 | #advcommeditimpl.cpp \ | 100 | #advcommeditimpl.cpp \ |
100 | #configuration.cpp \ | 101 | #configuration.cpp \ |
101 | #configwnd.cpp \ | 102 | #configwnd.cpp \ |
102 | #configwndimpl.cpp \ | 103 | #configwndimpl.cpp \ |
103 | #configuration_31compat.cpp \ | 104 | #configuration_31compat.cpp \ |
104 | #htmlparse.cpp \ | 105 | #htmlparse.cpp \ |
105 | #printtext.cpp \ | 106 | #printtext.cpp \ |
106 | #selftest.cpp \ | 107 | #selftest.cpp \ |
107 | #pwmprint.cpp \ | 108 | #pwmprint.cpp \ |
108 | #spinforsignal.cpp | 109 | #spinforsignal.cpp |
109 | #subtbledit.cpp \ | 110 | #subtbledit.cpp \ |
110 | #subtbleditimpl.cpp \ | 111 | #subtbleditimpl.cpp \ |
112 | #compressbzip2.cpp \ | ||
111 | 113 | ||
112 | SOURCES = \ | 114 | SOURCES = \ |
113 | addentrywnd_emb.cpp \ | 115 | addentrywnd_emb.cpp \ |
114 | addentrywndimpl.cpp \ | 116 | addentrywndimpl.cpp \ |
115 | base64.cpp \ | 117 | base64.cpp \ |
116 | binentrygen.cpp \ | 118 | binentrygen.cpp \ |
117 | blowfish.cpp \ | 119 | blowfish.cpp \ |
118 | commentbox.cpp \ | 120 | commentbox.cpp \ |
119 | compressbzip2.cpp \ | ||
120 | compressgzip.cpp \ | 121 | compressgzip.cpp \ |
121 | findwnd_emb.cpp \ | 122 | findwnd_emb.cpp \ |
122 | findwndimpl.cpp \ | 123 | findwndimpl.cpp \ |
123 | genpasswd.cpp \ | 124 | genpasswd.cpp \ |
124 | getkeycardwnd.cpp \ | 125 | getkeycardwnd.cpp \ |
125 | getmasterpwwnd_emb.cpp \ | 126 | getmasterpwwnd_emb.cpp \ |
126 | getmasterpwwndimpl.cpp \ | 127 | getmasterpwwndimpl.cpp \ |
127 | globalstuff.cpp \ | 128 | globalstuff.cpp \ |
128 | gpasmanfile.cpp \ | 129 | gpasmanfile.cpp \ |
129 | htmlgen.cpp \ | 130 | htmlgen.cpp \ |
130 | ipc.cpp \ | 131 | ipc.cpp \ |
132 | libgcryptif.cpp \ | ||
131 | listobjselectwnd.cpp \ | 133 | listobjselectwnd.cpp \ |
132 | listviewpwm.cpp \ | 134 | listviewpwm.cpp \ |
133 | main.cpp \ | 135 | main.cpp \ |
134 | pwgenwnd_emb.cpp \ | 136 | pwgenwnd_emb.cpp \ |
135 | pwgenwndimpl.cpp \ | 137 | pwgenwndimpl.cpp \ |
136 | pwm.cpp \ | 138 | pwm.cpp \ |
137 | pwmdoc.cpp \ | 139 | pwmdoc.cpp \ |
138 | pwmdocui.cpp \ | 140 | pwmdocui.cpp \ |
139 | pwmexception.cpp \ | 141 | pwmexception.cpp \ |
140 | pwminit.cpp \ | 142 | pwminit.cpp \ |
141 | pwmprefs.cpp \ | 143 | pwmprefs.cpp \ |
142 | pwmtray.cpp \ | 144 | pwmtray.cpp \ |
143 | pwmview.cpp \ | 145 | pwmview.cpp \ |
144 | pwmviewstyle_0.cpp \ | 146 | pwmviewstyle_0.cpp \ |
145 | pwmviewstyle_1.cpp \ | 147 | pwmviewstyle_1.cpp \ |
146 | pwmviewstyle.cpp \ | 148 | pwmviewstyle.cpp \ |
147 | randomizer.cpp \ | 149 | randomizer.cpp \ |
148 | rc2.cpp \ | 150 | rc2.cpp \ |
149 | rencatwnd.cpp \ | 151 | rencatwnd.cpp \ |
150 | serializer.cpp \ | 152 | serializer.cpp \ |
151 | setmasterpwwnd_emb.cpp \ | 153 | setmasterpwwnd_emb.cpp \ |
152 | setmasterpwwndimpl.cpp \ | 154 | setmasterpwwndimpl.cpp \ |
153 | sha1.cpp \ | 155 | sha1.cpp \ |
154 | waitwnd.cpp \ | 156 | waitwnd.cpp \ |
155 | kcmconfigs/kcmpwmconfig.cpp \ | 157 | kcmconfigs/kcmpwmconfig.cpp \ |
156 | kcmconfigs/pwmconfigwidget.cpp \ | 158 | kcmconfigs/pwmconfigwidget.cpp \ |
157 | 159 | ||
158 | 160 | ||
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp index 4ad392e..a5df8f0 100644 --- a/pwmanager/pwmanager/pwmdoc.cpp +++ b/pwmanager/pwmanager/pwmdoc.cpp | |||
@@ -1,2251 +1,2206 @@ | |||
1 | /*************************************************************************** | 1 | /*************************************************************************** |
2 | * * | 2 | * * |
3 | * copyright (C) 2003, 2004 by Michael Buesch * | 3 | * copyright (C) 2003, 2004 by Michael Buesch * |
4 | * email: mbuesch@freenet.de * | 4 | * email: mbuesch@freenet.de * |
5 | * * | 5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * | 6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License version 2 * | 7 | * it under the terms of the GNU General Public License version 2 * |
8 | * as published by the Free Software Foundation. * | 8 | * as published by the Free Software Foundation. * |
9 | * * | 9 | * * |
10 | ***************************************************************************/ | 10 | ***************************************************************************/ |
11 | 11 | ||
12 | /*************************************************************************** | 12 | /*************************************************************************** |
13 | * copyright (C) 2004 by Ulf Schenk | 13 | * copyright (C) 2004 by Ulf Schenk |
14 | * This file is originaly based on version 2.0 of pwmanager | 14 | * This file is originaly based on version 2.0 of pwmanager |
15 | * and was modified to run on embedded devices that run microkde | 15 | * and was modified to run on embedded devices that run microkde |
16 | * | 16 | * |
17 | * $Id$ | 17 | * $Id$ |
18 | **************************************************************************/ | 18 | **************************************************************************/ |
19 | 19 | ||
20 | #include "pwmdoc.h" | 20 | #include "pwmdoc.h" |
21 | #include "pwmview.h" | 21 | #include "pwmview.h" |
22 | #include "blowfish.h" | 22 | #include "blowfish.h" |
23 | #include "sha1.h" | 23 | #include "sha1.h" |
24 | #include "globalstuff.h" | 24 | #include "globalstuff.h" |
25 | #include "gpasmanfile.h" | 25 | #include "gpasmanfile.h" |
26 | #include "serializer.h" | 26 | #include "serializer.h" |
27 | #include "compressgzip.h" | 27 | #include "compressgzip.h" |
28 | #include "compressbzip2.h" | 28 | #include "compressbzip2.h" |
29 | #include "randomizer.h" | 29 | #include "randomizer.h" |
30 | #include "pwminit.h" | 30 | #include "pwminit.h" |
31 | #ifndef PWM_EMBEDDED | 31 | #include "libgcryptif.h" |
32 | //US #include "libgryptif.h" | 32 | #ifdef PWM_EMBEDDED |
33 | #else | ||
34 | #include "pwmprefs.h" | 33 | #include "pwmprefs.h" |
35 | #include "kglobal.h" | 34 | #include "kglobal.h" |
36 | #endif | 35 | #endif |
37 | 36 | ||
38 | #include <kmessagebox.h> | 37 | #include <kmessagebox.h> |
39 | #include <libkcal/syncdefines.h> | 38 | #include <libkcal/syncdefines.h> |
40 | 39 | ||
41 | 40 | ||
42 | #ifdef CONFIG_KWALLETIF | 41 | #ifdef CONFIG_KWALLETIF |
43 | # include "kwalletemu.h" | 42 | # include "kwalletemu.h" |
44 | #endif // CONFIG_KWALLETIF | 43 | #endif // CONFIG_KWALLETIF |
45 | 44 | ||
46 | #include <qdatetime.h> | 45 | #include <qdatetime.h> |
47 | #include <qsize.h> | 46 | #include <qsize.h> |
48 | #include <qfileinfo.h> | 47 | #include <qfileinfo.h> |
49 | #include <qfile.h> | 48 | #include <qfile.h> |
50 | 49 | ||
51 | #include <stdio.h> | 50 | #include <stdio.h> |
52 | #include <stdlib.h> | 51 | #include <stdlib.h> |
53 | #include <errno.h> | 52 | #include <errno.h> |
54 | #include <string.h> | 53 | #include <string.h> |
55 | //US#include <iostream> | 54 | //US#include <iostream> |
56 | #include <algorithm> | 55 | #include <algorithm> |
57 | #include <sys/types.h> | 56 | #include <sys/types.h> |
58 | #include <sys/stat.h> | 57 | #include <sys/stat.h> |
59 | #include <unistd.h> | 58 | #include <unistd.h> |
60 | #include <stdint.h> | 59 | #include <stdint.h> |
61 | 60 | ||
61 | |||
62 | #ifdef PWM_EMBEDDED | ||
63 | #ifndef Q_LONG | ||
64 | #define Q_LONG long | ||
65 | #endif | ||
66 | |||
67 | #ifndef Q_ULONG | ||
68 | #define Q_ULONG unsigned long | ||
69 | #endif | ||
70 | #endif //PWM_EMBEDDED | ||
71 | |||
72 | |||
62 | //TODO: reset to its normal value. | 73 | //TODO: reset to its normal value. |
63 | #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */ | 74 | #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */ |
64 | 75 | ||
65 | using namespace std; | 76 | using namespace std; |
66 | 77 | ||
67 | 78 | ||
68 | void PwMDocList::add(PwMDoc *doc, const string &id) | 79 | void PwMDocList::add(PwMDoc *doc, const string &id) |
69 | { | 80 | { |
70 | #ifdef PWM_DEBUG | 81 | #ifdef PWM_DEBUG |
71 | // check for existance of object in debug mode only. | 82 | // check for existance of object in debug mode only. |
72 | vector<listItem>::iterator begin = docList.begin(), | 83 | vector<listItem>::iterator begin = docList.begin(), |
73 | end = docList.end(), | 84 | end = docList.end(), |
74 | i = begin; | 85 | i = begin; |
75 | while (i != end) { | 86 | while (i != end) { |
76 | if (i->doc == doc) { | 87 | if (i->doc == doc) { |
77 | BUG(); | 88 | BUG(); |
78 | return; | 89 | return; |
79 | } | 90 | } |
80 | ++i; | 91 | ++i; |
81 | } | 92 | } |
82 | #endif | 93 | #endif |
83 | listItem newItem; | 94 | listItem newItem; |
84 | newItem.doc = doc; | 95 | newItem.doc = doc; |
85 | newItem.docId = id; | 96 | newItem.docId = id; |
86 | docList.push_back(newItem); | 97 | docList.push_back(newItem); |
87 | } | 98 | } |
88 | 99 | ||
89 | void PwMDocList::edit(PwMDoc *doc, const string &newId) | 100 | void PwMDocList::edit(PwMDoc *doc, const string &newId) |
90 | { | 101 | { |
91 | vector<listItem>::iterator begin = docList.begin(), | 102 | vector<listItem>::iterator begin = docList.begin(), |
92 | end = docList.end(), | 103 | end = docList.end(), |
93 | i = begin; | 104 | i = begin; |
94 | while (i != end) { | 105 | while (i != end) { |
95 | if (i->doc == doc) { | 106 | if (i->doc == doc) { |
96 | i->docId = newId; | 107 | i->docId = newId; |
97 | return; | 108 | return; |
98 | } | 109 | } |
99 | ++i; | 110 | ++i; |
100 | } | 111 | } |
101 | } | 112 | } |
102 | 113 | ||
103 | void PwMDocList::del(PwMDoc *doc) | 114 | void PwMDocList::del(PwMDoc *doc) |
104 | { | 115 | { |
105 | vector<listItem>::iterator begin = docList.begin(), | 116 | vector<listItem>::iterator begin = docList.begin(), |
106 | end = docList.end(), | 117 | end = docList.end(), |
107 | i = begin; | 118 | i = begin; |
108 | while (i != end) { | 119 | while (i != end) { |
109 | if (i->doc == doc) { | 120 | if (i->doc == doc) { |
110 | docList.erase(i); | 121 | docList.erase(i); |
111 | return; | 122 | return; |
112 | } | 123 | } |
113 | ++i; | 124 | ++i; |
114 | } | 125 | } |
115 | } | 126 | } |
116 | 127 | ||
117 | bool PwMDocList::find(const string &id, listItem *ret) | 128 | bool PwMDocList::find(const string &id, listItem *ret) |
118 | { | 129 | { |
119 | vector<listItem>::iterator begin = docList.begin(), | 130 | vector<listItem>::iterator begin = docList.begin(), |
120 | end = docList.end(), | 131 | end = docList.end(), |
121 | i = begin; | 132 | i = begin; |
122 | while (i != end) { | 133 | while (i != end) { |
123 | if (i->docId == id) { | 134 | if (i->docId == id) { |
124 | if (ret) | 135 | if (ret) |
125 | *ret = *i; | 136 | *ret = *i; |
126 | return true; | 137 | return true; |
127 | } | 138 | } |
128 | ++i; | 139 | ++i; |
129 | } | 140 | } |
130 | return false; | 141 | return false; |
131 | } | 142 | } |
132 | 143 | ||
133 | 144 | ||
134 | 145 | ||
135 | DocTimer::DocTimer(PwMDoc *_doc) | 146 | DocTimer::DocTimer(PwMDoc *_doc) |
136 | : doc (_doc) | 147 | : doc (_doc) |
137 | , mpwLock (0) | 148 | , mpwLock (0) |
138 | , autoLockLock (0) | 149 | , autoLockLock (0) |
139 | , metaCheckLock (0) | 150 | , metaCheckLock (0) |
140 | { | 151 | { |
141 | mpwTimer = new QTimer; | 152 | mpwTimer = new QTimer; |
142 | autoLockTimer = new QTimer; | 153 | autoLockTimer = new QTimer; |
143 | metaCheckTimer = new QTimer; | 154 | metaCheckTimer = new QTimer; |
144 | connect(mpwTimer, SIGNAL(timeout()), | 155 | connect(mpwTimer, SIGNAL(timeout()), |
145 | this, SLOT(mpwTimeout())); | 156 | this, SLOT(mpwTimeout())); |
146 | connect(autoLockTimer, SIGNAL(timeout()), | 157 | connect(autoLockTimer, SIGNAL(timeout()), |
147 | this, SLOT(autoLockTimeout())); | 158 | this, SLOT(autoLockTimeout())); |
148 | connect(metaCheckTimer, SIGNAL(timeout()), | 159 | connect(metaCheckTimer, SIGNAL(timeout()), |
149 | this, SLOT(metaCheckTimeout())); | 160 | this, SLOT(metaCheckTimeout())); |
150 | } | 161 | } |
151 | 162 | ||
152 | DocTimer::~DocTimer() | 163 | DocTimer::~DocTimer() |
153 | { | 164 | { |
154 | delete mpwTimer; | 165 | delete mpwTimer; |
155 | delete autoLockTimer; | 166 | delete autoLockTimer; |
156 | delete metaCheckTimer; | 167 | delete metaCheckTimer; |
157 | } | 168 | } |
158 | 169 | ||
159 | void DocTimer::start(TimerIDs timer) | 170 | void DocTimer::start(TimerIDs timer) |
160 | { | 171 | { |
161 | switch (timer) { | 172 | switch (timer) { |
162 | case id_mpwTimer: | 173 | case id_mpwTimer: |
163 | if (mpwTimer->isActive()) | 174 | if (mpwTimer->isActive()) |
164 | mpwTimer->stop(); | 175 | mpwTimer->stop(); |
165 | doc->setDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); | 176 | doc->setDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); |
166 | mpwTimer->start(conf()->confGlobPwTimeout() * 1000, true); | 177 | mpwTimer->start(conf()->confGlobPwTimeout() * 1000, true); |
167 | break; | 178 | break; |
168 | case id_autoLockTimer: | 179 | case id_autoLockTimer: |
169 | if (autoLockTimer->isActive()) | 180 | if (autoLockTimer->isActive()) |
170 | autoLockTimer->stop(); | 181 | autoLockTimer->stop(); |
171 | if (conf()->confGlobLockTimeout() > 0) | 182 | if (conf()->confGlobLockTimeout() > 0) |
172 | autoLockTimer->start(conf()->confGlobLockTimeout() * 1000, true); | 183 | autoLockTimer->start(conf()->confGlobLockTimeout() * 1000, true); |
173 | break; | 184 | break; |
174 | case id_metaCheckTimer: | 185 | case id_metaCheckTimer: |
175 | if (metaCheckTimer->isActive()) | 186 | if (metaCheckTimer->isActive()) |
176 | metaCheckTimer->stop(); | 187 | metaCheckTimer->stop(); |
177 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); | 188 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); |
178 | break; | 189 | break; |
179 | } | 190 | } |
180 | } | 191 | } |
181 | 192 | ||
182 | void DocTimer::stop(TimerIDs timer) | 193 | void DocTimer::stop(TimerIDs timer) |
183 | { | 194 | { |
184 | switch (timer) { | 195 | switch (timer) { |
185 | case id_mpwTimer: | 196 | case id_mpwTimer: |
186 | mpwTimer->stop(); | 197 | mpwTimer->stop(); |
187 | break; | 198 | break; |
188 | case id_autoLockTimer: | 199 | case id_autoLockTimer: |
189 | autoLockTimer->stop(); | 200 | autoLockTimer->stop(); |
190 | break; | 201 | break; |
191 | case id_metaCheckTimer: | 202 | case id_metaCheckTimer: |
192 | metaCheckTimer->stop(); | 203 | metaCheckTimer->stop(); |
193 | break; | 204 | break; |
194 | } | 205 | } |
195 | } | 206 | } |
196 | 207 | ||
197 | void DocTimer::getLock(TimerIDs timer) | 208 | void DocTimer::getLock(TimerIDs timer) |
198 | { | 209 | { |
199 | switch (timer) { | 210 | switch (timer) { |
200 | case id_mpwTimer: | 211 | case id_mpwTimer: |
201 | ++mpwLock; | 212 | ++mpwLock; |
202 | break; | 213 | break; |
203 | case id_autoLockTimer: | 214 | case id_autoLockTimer: |
204 | ++autoLockLock; | 215 | ++autoLockLock; |
205 | break; | 216 | break; |
206 | case id_metaCheckTimer: | 217 | case id_metaCheckTimer: |
207 | ++metaCheckLock; | 218 | ++metaCheckLock; |
208 | break; | 219 | break; |
209 | } | 220 | } |
210 | } | 221 | } |
211 | 222 | ||
212 | void DocTimer::putLock(TimerIDs timer) | 223 | void DocTimer::putLock(TimerIDs timer) |
213 | { | 224 | { |
214 | switch (timer) { | 225 | switch (timer) { |
215 | case id_mpwTimer: | 226 | case id_mpwTimer: |
216 | if (mpwLock) | 227 | if (mpwLock) |
217 | --mpwLock; | 228 | --mpwLock; |
218 | break; | 229 | break; |
219 | case id_autoLockTimer: | 230 | case id_autoLockTimer: |
220 | if (autoLockLock) | 231 | if (autoLockLock) |
221 | --autoLockLock; | 232 | --autoLockLock; |
222 | break; | 233 | break; |
223 | case id_metaCheckTimer: | 234 | case id_metaCheckTimer: |
224 | if (metaCheckLock) | 235 | if (metaCheckLock) |
225 | --metaCheckLock; | 236 | --metaCheckLock; |
226 | break; | 237 | break; |
227 | } | 238 | } |
228 | } | 239 | } |
229 | 240 | ||
230 | void DocTimer::mpwTimeout() | 241 | void DocTimer::mpwTimeout() |
231 | { | 242 | { |
232 | if (mpwLock) { | 243 | if (mpwLock) { |
233 | mpwTimer->start(1000, true); | 244 | mpwTimer->start(1000, true); |
234 | return; | 245 | return; |
235 | } | 246 | } |
236 | doc->unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); | 247 | doc->unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); |
237 | } | 248 | } |
238 | 249 | ||
239 | void DocTimer::autoLockTimeout() | 250 | void DocTimer::autoLockTimeout() |
240 | { | 251 | { |
241 | if (autoLockLock) { | 252 | if (autoLockLock) { |
242 | autoLockTimer->start(1000, true); | 253 | autoLockTimer->start(1000, true); |
243 | return; | 254 | return; |
244 | } | 255 | } |
245 | if (conf()->confGlobAutoDeepLock() && | 256 | if (conf()->confGlobAutoDeepLock() && |
246 | doc->filename != QString::null && | 257 | doc->filename != QString::null && |
247 | doc->filename != "") { | 258 | doc->filename != "") { |
248 | doc->deepLock(true); | 259 | doc->deepLock(true); |
249 | } else { | 260 | } else { |
250 | doc->lockAll(true); | 261 | doc->lockAll(true); |
251 | } | 262 | } |
252 | } | 263 | } |
253 | 264 | ||
254 | void DocTimer::metaCheckTimeout() | 265 | void DocTimer::metaCheckTimeout() |
255 | { | 266 | { |
256 | if (metaCheckLock) { | 267 | if (metaCheckLock) { |
257 | // check again in one second. | 268 | // check again in one second. |
258 | metaCheckTimer->start(1000, true); | 269 | metaCheckTimer->start(1000, true); |
259 | return; | 270 | return; |
260 | } | 271 | } |
261 | if (doc->isDeepLocked()) { | 272 | if (doc->isDeepLocked()) { |
262 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); | 273 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); |
263 | return; | 274 | return; |
264 | } | 275 | } |
265 | if (doc->isDocEmpty()) { | 276 | if (doc->isDocEmpty()) { |
266 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); | 277 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); |
267 | return; | 278 | return; |
268 | } | 279 | } |
269 | #ifdef CONFIG_KWALLETIF | 280 | #ifdef CONFIG_KWALLETIF |
270 | KWalletEmu *kwlEmu = doc->init->kwalletEmu(); | 281 | KWalletEmu *kwlEmu = doc->init->kwalletEmu(); |
271 | if (kwlEmu) | 282 | if (kwlEmu) |
272 | kwlEmu->suspendDocSignals(); | 283 | kwlEmu->suspendDocSignals(); |
273 | #endif // CONFIG_KWALLETIF | 284 | #endif // CONFIG_KWALLETIF |
274 | /* We simply trigger all views to update their | 285 | /* We simply trigger all views to update their |
275 | * displayed values. This way they have a chance | 286 | * displayed values. This way they have a chance |
276 | * to get notified when some meta changes over time. | 287 | * to get notified when some meta changes over time. |
277 | * (for example an entry expired). | 288 | * (for example an entry expired). |
278 | * The _view_ is responsive for not updating its | 289 | * The _view_ is responsive for not updating its |
279 | * contents if nothing really changed! | 290 | * contents if nothing really changed! |
280 | */ | 291 | */ |
281 | emit doc->dataChanged(doc); | 292 | emit doc->dataChanged(doc); |
282 | #ifdef CONFIG_KWALLETIF | 293 | #ifdef CONFIG_KWALLETIF |
283 | if (kwlEmu) | 294 | if (kwlEmu) |
284 | kwlEmu->resumeDocSignals(); | 295 | kwlEmu->resumeDocSignals(); |
285 | #endif // CONFIG_KWALLETIF | 296 | #endif // CONFIG_KWALLETIF |
286 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); | 297 | metaCheckTimer->start(META_CHECK_TIMER_INTERVAL * 1000, true); |
287 | } | 298 | } |
288 | 299 | ||
289 | 300 | ||
290 | 301 | ||
291 | PwMDocList PwMDoc::openDocList; | 302 | PwMDocList PwMDoc::openDocList; |
292 | unsigned int PwMDocList::unnamedDocCnt = 1; | 303 | unsigned int PwMDocList::unnamedDocCnt = 1; |
293 | 304 | ||
294 | PwMDoc::PwMDoc(QObject *parent, const char *name) | 305 | PwMDoc::PwMDoc(QObject *parent, const char *name) |
295 | : PwMDocUi(parent, name) | 306 | : PwMDocUi(parent, name) |
296 | , dataChangedLock (0) | 307 | , dataChangedLock (0) |
297 | { | 308 | { |
298 | deleted = false; | 309 | deleted = false; |
299 | unnamedNum = 0; | 310 | unnamedNum = 0; |
300 | getOpenDocList()->add(this, getTitle().latin1()); | 311 | getOpenDocList()->add(this, getTitle().latin1()); |
301 | curDocStat = 0; | 312 | curDocStat = 0; |
302 | setMaxNumEntries(); | 313 | setMaxNumEntries(); |
303 | _timer = new DocTimer(this); | 314 | _timer = new DocTimer(this); |
304 | timer()->start(DocTimer::id_mpwTimer); | 315 | timer()->start(DocTimer::id_mpwTimer); |
305 | timer()->start(DocTimer::id_autoLockTimer); | 316 | timer()->start(DocTimer::id_autoLockTimer); |
306 | timer()->start(DocTimer::id_metaCheckTimer); | 317 | timer()->start(DocTimer::id_metaCheckTimer); |
307 | addCategory(DEFAULT_CATEGORY, 0, false); | 318 | addCategory(DEFAULT_CATEGORY, 0, false); |
308 | listView = 0; | 319 | listView = 0; |
309 | emit docCreated(this); | 320 | emit docCreated(this); |
310 | } | 321 | } |
311 | 322 | ||
312 | PwMDoc::~PwMDoc() | 323 | PwMDoc::~PwMDoc() |
313 | { | 324 | { |
314 | emit docClosed(this); | 325 | emit docClosed(this); |
315 | getOpenDocList()->del(this); | 326 | getOpenDocList()->del(this); |
316 | delete _timer; | 327 | delete _timer; |
317 | } | 328 | } |
318 | 329 | ||
319 | PwMerror PwMDoc::saveDoc(char compress, const QString *file) | 330 | PwMerror PwMDoc::saveDoc(char compress, const QString *file) |
320 | { | 331 | { |
321 | PwMerror ret, e; | 332 | PwMerror ret, e; |
322 | if (!file) { | 333 | if (!file) { |
323 | if (filename == "") | 334 | if (filename == "") |
324 | return e_filename; | 335 | return e_filename; |
325 | } else { | 336 | } else { |
326 | if (*file == "" && filename == "") | 337 | if (*file == "" && filename == "") |
327 | return e_filename; | 338 | return e_filename; |
328 | if (*file != "") | 339 | if (*file != "") |
329 | filename = *file; | 340 | filename = *file; |
330 | } | 341 | } |
331 | 342 | ||
332 | bool wasDeepLocked = isDeepLocked(); | 343 | bool wasDeepLocked = isDeepLocked(); |
333 | if (wasDeepLocked) { | 344 | if (wasDeepLocked) { |
334 | if (deepLock(false) != e_success) | 345 | if (deepLock(false) != e_success) |
335 | return e_noPw; | 346 | return e_noPw; |
336 | } | 347 | } |
337 | 348 | ||
338 | if (!isPwAvailable()) { | 349 | if (!isPwAvailable()) { |
339 | /* password is not available. This means, the | 350 | /* password is not available. This means, the |
340 | * document wasn't saved, yet. | 351 | * document wasn't saved, yet. |
341 | */ | 352 | */ |
342 | bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); | 353 | bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); |
343 | QString pw(requestNewMpw(&useChipcard)); | 354 | QString pw(requestNewMpw(&useChipcard)); |
344 | if (pw != "") { | 355 | if (pw != "") { |
345 | currentPw = pw; | 356 | currentPw = pw; |
346 | } else { | 357 | } else { |
347 | return e_noPw; | 358 | return e_noPw; |
348 | } | 359 | } |
349 | if (useChipcard) { | 360 | if (useChipcard) { |
350 | setDocStatFlag(DOC_STAT_USE_CHIPCARD); | 361 | setDocStatFlag(DOC_STAT_USE_CHIPCARD); |
351 | } else { | 362 | } else { |
352 | unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); | 363 | unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); |
353 | } | 364 | } |
354 | } | 365 | } |
355 | #ifndef PWM_EMBEDDED | 366 | |
356 | int _cryptAlgo = conf()->confGlobCryptAlgo(); | 367 | int _cryptAlgo = conf()->confGlobCryptAlgo(); |
357 | int _hashAlgo = conf()->confGlobHashAlgo(); | 368 | int _hashAlgo = conf()->confGlobHashAlgo(); |
358 | #else | ||
359 | int _cryptAlgo = PWM_CRYPT_BLOWFISH; | ||
360 | int _hashAlgo = PWM_HASH_SHA1; | ||
361 | #endif | ||
362 | 369 | ||
363 | // sanity check for the selected algorithms | 370 | // sanity check for the selected algorithms |
364 | if (_cryptAlgo < PWM_CRYPT_BLOWFISH || | 371 | if (_cryptAlgo < PWM_CRYPT_BLOWFISH || |
365 | _cryptAlgo > PWM_CRYPT_TWOFISH128) { | 372 | _cryptAlgo > PWM_CRYPT_TWOFISH128) { |
366 | printWarn("Invalid Crypto-Algorithm selected! " | 373 | printWarn("Invalid Crypto-Algorithm selected! " |
367 | "Config-file seems to be corrupt. " | 374 | "Config-file seems to be corrupt. " |
368 | "Falling back to Blowfish."); | 375 | "Falling back to Blowfish."); |
369 | _cryptAlgo = PWM_CRYPT_BLOWFISH; | 376 | _cryptAlgo = PWM_CRYPT_BLOWFISH; |
370 | } | 377 | } |
371 | if (_hashAlgo < PWM_HASH_SHA1 || | 378 | if (_hashAlgo < PWM_HASH_SHA1 || |
372 | _hashAlgo > PWM_HASH_TIGER) { | 379 | _hashAlgo > PWM_HASH_TIGER) { |
373 | printWarn("Invalid Hash-Algorithm selected! " | 380 | printWarn("Invalid Hash-Algorithm selected! " |
374 | "Config-file seems to be corrupt. " | 381 | "Config-file seems to be corrupt. " |
375 | "Falling back to SHA1."); | 382 | "Falling back to SHA1."); |
376 | _hashAlgo = PWM_HASH_SHA1; | 383 | _hashAlgo = PWM_HASH_SHA1; |
377 | } | 384 | } |
378 | char cryptAlgo = static_cast<char>(_cryptAlgo); | 385 | char cryptAlgo = static_cast<char>(_cryptAlgo); |
379 | char hashAlgo = static_cast<char>(_hashAlgo); | 386 | char hashAlgo = static_cast<char>(_hashAlgo); |
380 | 387 | ||
381 | if (conf()->confGlobMakeFileBackup()) { | 388 | if (conf()->confGlobMakeFileBackup()) { |
382 | if (!backupFile(filename)) | 389 | if (!backupFile(filename)) |
383 | return e_fileBackup; | 390 | return e_fileBackup; |
384 | } | 391 | } |
385 | QString tmpFileMoved(QString::null); | 392 | QString tmpFileMoved(QString::null); |
386 | if (QFile::exists(filename)) { | 393 | if (QFile::exists(filename)) { |
387 | /* Move the existing file to some tmp file. | 394 | /* Move the existing file to some tmp file. |
388 | * When saving file succeeds, delete tmp file. Otherwise | 395 | * When saving file succeeds, delete tmp file. Otherwise |
389 | * move tmp file back. See below. | 396 | * move tmp file back. See below. |
390 | */ | 397 | */ |
391 | Randomizer *rnd = Randomizer::obj(); | 398 | Randomizer *rnd = Randomizer::obj(); |
392 | char rnd_buf[5]; | 399 | char rnd_buf[5]; |
393 | sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF, | 400 | sprintf(rnd_buf, "%X%X%X%X", rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF, |
394 | rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF); | 401 | rnd->genRndChar() & 0xFF, rnd->genRndChar() & 0xFF); |
395 | tmpFileMoved = filename + "." + rnd_buf + ".mv"; | 402 | tmpFileMoved = filename + "." + rnd_buf + ".mv"; |
396 | if (!copyFile(filename, tmpFileMoved)) | 403 | if (!copyFile(filename, tmpFileMoved)) |
397 | return e_openFile; | 404 | return e_openFile; |
398 | if (!QFile::remove(filename)) { | 405 | if (!QFile::remove(filename)) { |
399 | printWarn(string("removing orig file ") | 406 | printWarn(string("removing orig file ") |
400 | + filename.latin1() | 407 | + filename.latin1() |
401 | + " failed!"); | 408 | + " failed!"); |
402 | } | 409 | } |
403 | } | 410 | } |
404 | QFile f(filename); | 411 | QFile f(filename); |
405 | string serialized; | 412 | string serialized; |
406 | if (!f.open(IO_ReadWrite)) { | 413 | if (!f.open(IO_ReadWrite)) { |
407 | ret = e_openFile; | 414 | ret = e_openFile; |
408 | goto out_moveback; | 415 | goto out_moveback; |
409 | } | 416 | } |
410 | e = writeFileHeader(hashAlgo, hashAlgo, | 417 | e = writeFileHeader(hashAlgo, hashAlgo, |
411 | cryptAlgo, compress, | 418 | cryptAlgo, compress, |
412 | ¤tPw, &f); | 419 | ¤tPw, &f); |
413 | if (e == e_hashNotImpl) { | 420 | if (e == e_hashNotImpl) { |
414 | printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl"); | 421 | printDebug("PwMDoc::saveDoc(): writeFileHeader() failed: e_hashNotImpl"); |
415 | f.close(); | 422 | f.close(); |
416 | ret = e_hashNotImpl; | 423 | ret = e_hashNotImpl; |
417 | goto out_moveback; | 424 | goto out_moveback; |
418 | } else if (e != e_success) { | 425 | } else if (e != e_success) { |
419 | printDebug("PwMDoc::saveDoc(): writeFileHeader() failed"); | 426 | printDebug("PwMDoc::saveDoc(): writeFileHeader() failed"); |
420 | f.close(); | 427 | f.close(); |
421 | ret = e_writeHeader; | 428 | ret = e_writeHeader; |
422 | goto out_moveback; | 429 | goto out_moveback; |
423 | } | 430 | } |
424 | if (!serializeDta(&serialized)) { | 431 | if (!serializeDta(&serialized)) { |
425 | printDebug("PwMDoc::saveDoc(): serializeDta() failed"); | 432 | printDebug("PwMDoc::saveDoc(): serializeDta() failed"); |
426 | f.close(); | 433 | f.close(); |
427 | ret = e_serializeDta; | 434 | ret = e_serializeDta; |
428 | goto out_moveback; | 435 | goto out_moveback; |
429 | } | 436 | } |
430 | e = writeDataHash(hashAlgo, &serialized, &f); | 437 | e = writeDataHash(hashAlgo, &serialized, &f); |
431 | if (e == e_hashNotImpl) { | 438 | if (e == e_hashNotImpl) { |
432 | printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl"); | 439 | printDebug("PwMDoc::saveDoc(): writeDataHash() failed: e_hashNotImpl"); |
433 | f.close(); | 440 | f.close(); |
434 | ret = e_hashNotImpl; | 441 | ret = e_hashNotImpl; |
435 | goto out_moveback; | 442 | goto out_moveback; |
436 | } else if (e != e_success) { | 443 | } else if (e != e_success) { |
437 | printDebug("PwMDoc::saveDoc(): writeDataHash() failed"); | 444 | printDebug("PwMDoc::saveDoc(): writeDataHash() failed"); |
438 | f.close(); | 445 | f.close(); |
439 | ret = e_writeHeader; | 446 | ret = e_writeHeader; |
440 | goto out_moveback; | 447 | goto out_moveback; |
441 | } | 448 | } |
442 | if (!compressDta(&serialized, compress)) { | 449 | if (!compressDta(&serialized, compress)) { |
443 | printDebug("PwMDoc::saveDoc(): compressDta() failed"); | 450 | printDebug("PwMDoc::saveDoc(): compressDta() failed"); |
444 | f.close(); | 451 | f.close(); |
445 | ret = e_enc; | 452 | ret = e_enc; |
446 | goto out_moveback; | 453 | goto out_moveback; |
447 | } | 454 | } |
448 | e = encrypt(&serialized, ¤tPw, &f, cryptAlgo); | 455 | e = encrypt(&serialized, ¤tPw, &f, cryptAlgo); |
449 | if (e == e_weakPw) { | 456 | if (e == e_weakPw) { |
450 | printDebug("PwMDoc::saveDoc(): encrypt() failed: e_weakPw"); | 457 | printDebug("PwMDoc::saveDoc(): encrypt() failed: e_weakPw"); |
451 | f.close(); | 458 | f.close(); |
452 | ret = e_weakPw; | 459 | ret = e_weakPw; |
453 | goto out_moveback; | 460 | goto out_moveback; |
454 | } else if (e == e_cryptNotImpl) { | 461 | } else if (e == e_cryptNotImpl) { |
455 | printDebug("PwMDoc::saveDoc(): encrypt() failed: e_cryptNotImpl"); | 462 | printDebug("PwMDoc::saveDoc(): encrypt() failed: e_cryptNotImpl"); |
456 | f.close(); | 463 | f.close(); |
457 | ret = e_cryptNotImpl; | 464 | ret = e_cryptNotImpl; |
458 | goto out_moveback; | 465 | goto out_moveback; |
459 | } else if (e != e_success) { | 466 | } else if (e != e_success) { |
460 | printDebug("PwMDoc::saveDoc(): encrypt() failed"); | 467 | printDebug("PwMDoc::saveDoc(): encrypt() failed"); |
461 | f.close(); | 468 | f.close(); |
462 | ret = e_enc; | 469 | ret = e_enc; |
463 | goto out_moveback; | 470 | goto out_moveback; |
464 | } | 471 | } |
465 | unsetDocStatFlag(DOC_STAT_DISK_DIRTY); | 472 | unsetDocStatFlag(DOC_STAT_DISK_DIRTY); |
466 | f.close(); | 473 | f.close(); |
467 | if (chmod(filename.latin1(), | 474 | if (chmod(filename.latin1(), |
468 | conf()->confGlobFilePermissions())) { | 475 | conf()->confGlobFilePermissions())) { |
469 | printWarn(string("chmod failed: ") + strerror(errno)); | 476 | printWarn(string("chmod failed: ") + strerror(errno)); |
470 | } | 477 | } |
471 | openDocList.edit(this, getTitle().latin1()); | 478 | openDocList.edit(this, getTitle().latin1()); |
472 | if (wasDeepLocked) | 479 | if (wasDeepLocked) |
473 | deepLock(true); | 480 | deepLock(true); |
474 | if (tmpFileMoved != QString::null) { | 481 | if (tmpFileMoved != QString::null) { |
475 | // now remove the moved file. | 482 | // now remove the moved file. |
476 | if (!QFile::remove(tmpFileMoved)) { | 483 | if (!QFile::remove(tmpFileMoved)) { |
477 | printWarn(string("removing file ") | 484 | printWarn(string("removing file ") |
478 | + tmpFileMoved.latin1() | 485 | + tmpFileMoved.latin1() |
479 | + " failed!"); | 486 | + " failed!"); |
480 | } | 487 | } |
481 | } | 488 | } |
482 | ret = e_success; | 489 | ret = e_success; |
483 | printDebug(string("writing file { compress: ") | 490 | printDebug(string("writing file { compress: ") |
484 | + tostr(static_cast<int>(compress)) + " cryptAlgo: " | 491 | + tostr(static_cast<int>(compress)) + " cryptAlgo: " |
485 | + tostr(static_cast<int>(cryptAlgo)) + " hashAlgo: " | 492 | + tostr(static_cast<int>(cryptAlgo)) + " hashAlgo: " |
486 | + tostr(static_cast<int>(hashAlgo)) | 493 | + tostr(static_cast<int>(hashAlgo)) |
487 | + " }"); | 494 | + " }"); |
488 | goto out; | 495 | goto out; |
489 | out_moveback: | 496 | out_moveback: |
490 | if (tmpFileMoved != QString::null) { | 497 | if (tmpFileMoved != QString::null) { |
491 | if (copyFile(tmpFileMoved, filename)) { | 498 | if (copyFile(tmpFileMoved, filename)) { |
492 | if (!QFile::remove(tmpFileMoved)) { | 499 | if (!QFile::remove(tmpFileMoved)) { |
493 | printWarn(string("removing tmp file ") | 500 | printWarn(string("removing tmp file ") |
494 | + filename.latin1() | 501 | + filename.latin1() |
495 | + " failed!"); | 502 | + " failed!"); |
496 | } | 503 | } |
497 | } else { | 504 | } else { |
498 | printWarn(string("couldn't copy file ") | 505 | printWarn(string("couldn't copy file ") |
499 | + tmpFileMoved.latin1() | 506 | + tmpFileMoved.latin1() |
500 | + " back to " | 507 | + " back to " |
501 | + filename.latin1()); | 508 | + filename.latin1()); |
502 | } | 509 | } |
503 | } | 510 | } |
504 | out: | 511 | out: |
505 | return ret; | 512 | return ret; |
506 | } | 513 | } |
507 | 514 | ||
508 | PwMerror PwMDoc::openDoc(const QString *file, int openLocked) | 515 | PwMerror PwMDoc::openDoc(const QString *file, int openLocked) |
509 | { | 516 | { |
510 | PWM_ASSERT(file); | 517 | PWM_ASSERT(file); |
511 | PWM_ASSERT(openLocked == 0 || openLocked == 1 || openLocked == 2); | 518 | PWM_ASSERT(openLocked == 0 || openLocked == 1 || openLocked == 2); |
512 | string decrypted, dataHash; | 519 | string decrypted, dataHash; |
513 | PwMerror ret; | 520 | PwMerror ret; |
514 | char cryptAlgo, dataHashType, compress; | 521 | char cryptAlgo, dataHashType, compress; |
515 | unsigned int headerLen; | 522 | unsigned int headerLen; |
516 | 523 | ||
517 | if (*file == "") | 524 | if (*file == "") |
518 | return e_readFile; | 525 | return e_readFile; |
519 | filename = *file; | 526 | filename = *file; |
520 | /* check if this file is already open. | 527 | /* check if this file is already open. |
521 | * This does not catch symlinks! | 528 | * This does not catch symlinks! |
522 | */ | 529 | */ |
523 | if (!isDeepLocked()) { | 530 | if (!isDeepLocked()) { |
524 | if (getOpenDocList()->find(filename.latin1())) | 531 | if (getOpenDocList()->find(filename.latin1())) |
525 | return e_alreadyOpen; | 532 | return e_alreadyOpen; |
526 | } | 533 | } |
527 | QFile f(filename); | 534 | QFile f(filename); |
528 | 535 | ||
529 | if (openLocked == 2) { | 536 | if (openLocked == 2) { |
530 | // open deep-locked | 537 | // open deep-locked |
531 | if (!QFile::exists(filename)) | 538 | if (!QFile::exists(filename)) |
532 | return e_openFile; | 539 | return e_openFile; |
533 | if (deepLock(true, false) != e_success) | 540 | if (deepLock(true, false) != e_success) |
534 | return e_openFile; | 541 | return e_openFile; |
535 | goto out_success; | 542 | goto out_success; |
536 | } | 543 | } |
537 | 544 | ||
538 | if (!f.open(IO_ReadOnly)) | 545 | if (!f.open(IO_ReadOnly)) |
539 | return e_openFile; | 546 | return e_openFile; |
540 | 547 | ||
541 | ret = checkHeader(&cryptAlgo, ¤tPw, &compress, &headerLen, | 548 | ret = checkHeader(&cryptAlgo, ¤tPw, &compress, &headerLen, |
542 | &dataHashType, &dataHash, &f); | 549 | &dataHashType, &dataHash, &f); |
543 | if (ret != e_success) { | 550 | if (ret != e_success) { |
544 | printDebug("PwMDoc::openDoc(): checkHeader() failed"); | 551 | printDebug("PwMDoc::openDoc(): checkHeader() failed"); |
545 | f.close(); | 552 | f.close(); |
546 | if (ret == e_wrongPw) { | 553 | if (ret == e_wrongPw) { |
547 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 554 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
548 | return ret; | 555 | return ret; |
549 | } else if (ret == e_noPw || | 556 | } else if (ret == e_noPw || |
550 | ret == e_fileVer || | 557 | ret == e_fileVer || |
551 | ret == e_fileFormat || | 558 | ret == e_fileFormat || |
552 | ret == e_hashNotImpl) { | 559 | ret == e_hashNotImpl) { |
553 | return ret; | 560 | return ret; |
554 | } else | 561 | } else |
555 | return e_readFile; | 562 | return e_readFile; |
556 | } | 563 | } |
557 | ret = decrypt(&decrypted, headerLen, ¤tPw, cryptAlgo, &f); | 564 | ret = decrypt(&decrypted, headerLen, ¤tPw, cryptAlgo, &f); |
558 | if (ret == e_cryptNotImpl) { | 565 | if (ret == e_cryptNotImpl) { |
559 | printDebug("PwMDoc::openDoc(): decrypt() failed: e_cryptNotImpl"); | 566 | printDebug("PwMDoc::openDoc(): decrypt() failed: e_cryptNotImpl"); |
560 | f.close(); | 567 | f.close(); |
561 | return e_cryptNotImpl; | 568 | return e_cryptNotImpl; |
562 | } else if (ret != e_success) { | 569 | } else if (ret != e_success) { |
563 | printDebug("PwMDoc::openDoc(): decrypt() failed"); | 570 | printDebug("PwMDoc::openDoc(): decrypt() failed"); |
564 | f.close(); | 571 | f.close(); |
565 | return e_readFile; | 572 | return e_readFile; |
566 | } | 573 | } |
567 | if (!decompressDta(&decrypted, compress)) { | 574 | if (!decompressDta(&decrypted, compress)) { |
568 | printDebug("PwMDoc::openDoc(): decompressDta() failed"); | 575 | printDebug("PwMDoc::openDoc(): decompressDta() failed"); |
569 | f.close(); | 576 | f.close(); |
570 | return e_fileCorrupt; | 577 | return e_fileCorrupt; |
571 | } | 578 | } |
572 | ret = checkDataHash(dataHashType, &dataHash, &decrypted); | 579 | ret = checkDataHash(dataHashType, &dataHash, &decrypted); |
573 | if (ret == e_hashNotImpl) { | 580 | if (ret == e_hashNotImpl) { |
574 | printDebug("PwMDoc::openDoc(): checkDataHash() failed: e_hashNotImpl"); | 581 | printDebug("PwMDoc::openDoc(): checkDataHash() failed: e_hashNotImpl"); |
575 | f.close(); | 582 | f.close(); |
576 | return e_hashNotImpl; | 583 | return e_hashNotImpl; |
577 | } else if (ret != e_success) { | 584 | } else if (ret != e_success) { |
578 | printDebug("PwMDoc::openDoc(): checkDataHash() failed"); | 585 | printDebug("PwMDoc::openDoc(): checkDataHash() failed"); |
579 | f.close(); | 586 | f.close(); |
580 | return e_fileCorrupt; | 587 | return e_fileCorrupt; |
581 | } | 588 | } |
582 | if (!deSerializeDta(&decrypted, openLocked == 1)) { | 589 | if (!deSerializeDta(&decrypted, openLocked == 1)) { |
583 | printDebug("PwMDoc::openDoc(): deSerializeDta() failed"); | 590 | printDebug("PwMDoc::openDoc(): deSerializeDta() failed"); |
584 | f.close(); | 591 | f.close(); |
585 | return e_readFile; | 592 | return e_readFile; |
586 | } | 593 | } |
587 | f.close(); | 594 | f.close(); |
588 | timer()->start(DocTimer::id_mpwTimer); | 595 | timer()->start(DocTimer::id_mpwTimer); |
589 | timer()->start(DocTimer::id_autoLockTimer); | 596 | timer()->start(DocTimer::id_autoLockTimer); |
590 | out_success: | 597 | out_success: |
591 | openDocList.edit(this, getTitle().latin1()); | 598 | openDocList.edit(this, getTitle().latin1()); |
592 | emit docOpened(this); | 599 | emit docOpened(this); |
593 | return e_success; | 600 | return e_success; |
594 | } | 601 | } |
595 | 602 | ||
596 | PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char compress, | 603 | PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char compress, |
597 | QString *pw, QFile *f) | 604 | QString *pw, QFile *f) |
598 | { | 605 | { |
599 | PWM_ASSERT(pw); | 606 | PWM_ASSERT(pw); |
600 | PWM_ASSERT(f); | 607 | PWM_ASSERT(f); |
601 | PWM_ASSERT(listView); | 608 | PWM_ASSERT(listView); |
602 | #ifndef PWM_EMBEDDED | ||
603 | if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) != | 609 | if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) != |
604 | static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) { | 610 | static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) { |
605 | return e_writeFile; | 611 | return e_writeFile; |
606 | } | 612 | } |
607 | if (f->putch(PWM_FILE_VER) == -1 || | 613 | if (f->putch(PWM_FILE_VER) == -1 || |
608 | f->putch(keyHash) == -1 || | 614 | f->putch(keyHash) == -1 || |
609 | f->putch(dataHash) == -1 || | 615 | f->putch(dataHash) == -1 || |
610 | f->putch(crypt) == -1 || | 616 | f->putch(crypt) == -1 || |
611 | f->putch(compress) == -1 || | 617 | f->putch(compress) == -1 || |
612 | f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ? | 618 | f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ? |
613 | (static_cast<char>(0x01)) : (static_cast<char>(0x00))) == -1) { | 619 | (static_cast<char>(0x01)) : (static_cast<char>(0x00))) == -1) { |
614 | return e_writeFile; | 620 | return e_writeFile; |
615 | } | 621 | } |
616 | 622 | ||
617 | #else | ||
618 | if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) != | ||
619 | (long)(strlen(FILE_ID_HEADER))) { | ||
620 | return e_writeFile; | ||
621 | } | ||
622 | if (f->putch(PWM_FILE_VER) == -1 || | ||
623 | f->putch(keyHash) == -1 || | ||
624 | f->putch(dataHash) == -1 || | ||
625 | f->putch(crypt) == -1 || | ||
626 | f->putch(compress) == -1 || | ||
627 | f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ? | ||
628 | ((char)(0x01)) : ((char)(0x00))) == -1) { | ||
629 | return e_writeFile; | ||
630 | } | ||
631 | #endif | ||
632 | // write bytes of NUL-data. These bytes are reserved for future-use. | 623 | // write bytes of NUL-data. These bytes are reserved for future-use. |
633 | const int bufSize = 64; | 624 | const int bufSize = 64; |
634 | char tmp_buf[bufSize]; | 625 | char tmp_buf[bufSize]; |
635 | memset(tmp_buf, 0x00, bufSize); | 626 | memset(tmp_buf, 0x00, bufSize); |
636 | if (f->writeBlock(tmp_buf, bufSize) != bufSize) | 627 | if (f->writeBlock(tmp_buf, bufSize) != bufSize) |
637 | return e_writeFile; | 628 | return e_writeFile; |
638 | 629 | ||
639 | switch (keyHash) { | 630 | switch (keyHash) { |
640 | case PWM_HASH_SHA1: { | 631 | case PWM_HASH_SHA1: { |
641 | const int hashlen = SHA1_HASH_LEN_BYTE; | 632 | const int hashlen = SHA1_HASH_LEN_BYTE; |
642 | Sha1 hash; | 633 | Sha1 hash; |
643 | hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); | 634 | hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); |
644 | string ret = hash.sha1_read(); | 635 | string ret = hash.sha1_read(); |
645 | if (f->writeBlock(ret.c_str(), hashlen) != hashlen) | 636 | if (f->writeBlock(ret.c_str(), hashlen) != hashlen) |
646 | return e_writeFile; | 637 | return e_writeFile; |
647 | break; | 638 | break; |
648 | } | 639 | } |
649 | #ifndef PWM_EMBEDDED | ||
650 | case PWM_HASH_SHA256: | 640 | case PWM_HASH_SHA256: |
651 | /*... fall through */ | 641 | /*... fall through */ |
652 | case PWM_HASH_SHA384: | 642 | case PWM_HASH_SHA384: |
653 | case PWM_HASH_SHA512: | 643 | case PWM_HASH_SHA512: |
654 | case PWM_HASH_MD5: | 644 | case PWM_HASH_MD5: |
655 | case PWM_HASH_RMD160: | 645 | case PWM_HASH_RMD160: |
656 | case PWM_HASH_TIGER: | 646 | case PWM_HASH_TIGER: |
657 | { | 647 | { |
658 | if (!LibGCryptIf::available()) | 648 | if (!LibGCryptIf::available()) |
659 | return e_hashNotImpl; | 649 | return e_hashNotImpl; |
660 | LibGCryptIf gc; | 650 | LibGCryptIf gc; |
661 | PwMerror err; | 651 | PwMerror err; |
662 | unsigned char *buf; | 652 | unsigned char *buf; |
663 | size_t hashLen; | 653 | size_t hashLen; |
664 | err = gc.hash(&buf, | 654 | err = gc.hash(&buf, |
665 | &hashLen, | 655 | &hashLen, |
666 | reinterpret_cast<const unsigned char *>(pw->latin1()), | 656 | reinterpret_cast<const unsigned char *>(pw->latin1()), |
667 | pw->length(), | 657 | pw->length(), |
668 | keyHash); | 658 | keyHash); |
669 | if (err != e_success) | 659 | if (err != e_success) |
670 | return e_hashNotImpl; | 660 | return e_hashNotImpl; |
671 | if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen) | 661 | if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen) |
672 | != static_cast<Q_LONG>(hashLen)) { | 662 | != static_cast<Q_LONG>(hashLen)) { |
673 | delete [] buf; | 663 | delete [] buf; |
674 | return e_hashNotImpl; | 664 | return e_hashNotImpl; |
675 | } | 665 | } |
676 | delete [] buf; | 666 | delete [] buf; |
677 | break; | 667 | break; |
678 | } | 668 | } |
679 | #endif | ||
680 | default: { | 669 | default: { |
681 | return e_hashNotImpl; | 670 | return e_hashNotImpl; |
682 | } } | 671 | } } |
683 | return e_success; | 672 | return e_success; |
684 | } | 673 | } |
685 | 674 | ||
686 | PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress, | 675 | PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress, |
687 | unsigned int *headerLength, char *dataHashType, | 676 | unsigned int *headerLength, char *dataHashType, |
688 | string *dataHash, QFile *f) | 677 | string *dataHash, QFile *f) |
689 | { | 678 | { |
690 | PWM_ASSERT(cryptAlgo); | 679 | PWM_ASSERT(cryptAlgo); |
691 | PWM_ASSERT(pw); | 680 | PWM_ASSERT(pw); |
692 | PWM_ASSERT(headerLength); | 681 | PWM_ASSERT(headerLength); |
693 | PWM_ASSERT(dataHashType); | 682 | PWM_ASSERT(dataHashType); |
694 | PWM_ASSERT(dataHash); | 683 | PWM_ASSERT(dataHash); |
695 | PWM_ASSERT(f); | 684 | PWM_ASSERT(f); |
696 | int tmpRet; | 685 | int tmpRet; |
697 | // check "magic" header | 686 | // check "magic" header |
698 | const char magicHdr[] = FILE_ID_HEADER; | 687 | const char magicHdr[] = FILE_ID_HEADER; |
699 | const int hdrLen = array_size(magicHdr) - 1; | 688 | const int hdrLen = array_size(magicHdr) - 1; |
700 | char tmp[hdrLen]; | 689 | char tmp[hdrLen]; |
701 | if (f->readBlock(tmp, hdrLen) != hdrLen) | 690 | if (f->readBlock(tmp, hdrLen) != hdrLen) |
702 | return e_readFile; | 691 | return e_readFile; |
703 | if (memcmp(tmp, magicHdr, hdrLen) != 0) | 692 | if (memcmp(tmp, magicHdr, hdrLen) != 0) |
704 | return e_fileFormat; | 693 | return e_fileFormat; |
705 | // read and check file ver | 694 | // read and check file ver |
706 | int fileV = f->getch(); | 695 | int fileV = f->getch(); |
707 | if (fileV == -1) | 696 | if (fileV == -1) |
708 | return e_fileFormat; | 697 | return e_fileFormat; |
709 | if (fileV != PWM_FILE_VER) | 698 | if (fileV != PWM_FILE_VER) |
710 | return e_fileVer; | 699 | return e_fileVer; |
711 | // read hash hash type | 700 | // read hash hash type |
712 | int keyHash = f->getch(); | 701 | int keyHash = f->getch(); |
713 | if (keyHash == -1) | 702 | if (keyHash == -1) |
714 | return e_fileFormat; | 703 | return e_fileFormat; |
715 | // read data hash type | 704 | // read data hash type |
716 | tmpRet = f->getch(); | 705 | tmpRet = f->getch(); |
717 | if (tmpRet == -1) | 706 | if (tmpRet == -1) |
718 | return e_fileFormat; | 707 | return e_fileFormat; |
719 | *dataHashType = tmpRet; | 708 | *dataHashType = tmpRet; |
720 | // read crypt algo | 709 | // read crypt algo |
721 | tmpRet = f->getch(); | 710 | tmpRet = f->getch(); |
722 | if (tmpRet == -1) | 711 | if (tmpRet == -1) |
723 | return e_fileFormat; | 712 | return e_fileFormat; |
724 | *cryptAlgo = tmpRet; | 713 | *cryptAlgo = tmpRet; |
725 | // get compression-algo | 714 | // get compression-algo |
726 | tmpRet = f->getch(); | 715 | tmpRet = f->getch(); |
727 | if (tmpRet == -1) | 716 | if (tmpRet == -1) |
728 | return e_fileFormat; | 717 | return e_fileFormat; |
729 | *compress = tmpRet; | 718 | *compress = tmpRet; |
730 | // get the MPW-flag | 719 | // get the MPW-flag |
731 | int mpw_flag = f->getch(); | 720 | int mpw_flag = f->getch(); |
732 | if (mpw_flag == -1) | 721 | if (mpw_flag == -1) |
733 | return e_fileFormat; | 722 | return e_fileFormat; |
734 | if (mpw_flag == 0x01) | 723 | if (mpw_flag == 0x01) |
735 | setDocStatFlag(DOC_STAT_USE_CHIPCARD); | 724 | setDocStatFlag(DOC_STAT_USE_CHIPCARD); |
736 | else | 725 | else |
737 | unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); | 726 | unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); |
738 | // skip the "RESERVED"-bytes | 727 | // skip the "RESERVED"-bytes |
739 | if (!(f->at(f->at() + 64))) | 728 | if (!(f->at(f->at() + 64))) |
740 | return e_fileFormat; | 729 | return e_fileFormat; |
741 | 730 | ||
742 | *pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 731 | *pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
743 | if (*pw == "") { | 732 | if (*pw == "") { |
744 | /* the user didn't give a master-password | 733 | /* the user didn't give a master-password |
745 | * or didn't insert a chipcard | 734 | * or didn't insert a chipcard |
746 | */ | 735 | */ |
747 | return e_noPw; | 736 | return e_noPw; |
748 | } | 737 | } |
749 | // verify key-hash | 738 | // verify key-hash |
750 | switch (keyHash) { | 739 | switch (keyHash) { |
751 | case PWM_HASH_SHA1: { | 740 | case PWM_HASH_SHA1: { |
752 | // read hash from header | 741 | // read hash from header |
753 | const int hashLen = SHA1_HASH_LEN_BYTE; | 742 | const int hashLen = SHA1_HASH_LEN_BYTE; |
754 | string readHash; | 743 | string readHash; |
755 | int i; | 744 | int i; |
756 | for (i = 0; i < hashLen; ++i) | 745 | for (i = 0; i < hashLen; ++i) |
757 | readHash.push_back(f->getch()); | 746 | readHash.push_back(f->getch()); |
758 | Sha1 hash; | 747 | Sha1 hash; |
759 | hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); | 748 | hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); |
760 | string ret = hash.sha1_read(); | 749 | string ret = hash.sha1_read(); |
761 | if (ret != readHash) | 750 | if (ret != readHash) |
762 | return e_wrongPw;// hash doesn't match (wrong key) | 751 | return e_wrongPw;// hash doesn't match (wrong key) |
763 | break; | 752 | break; |
764 | } | 753 | } |
765 | #ifndef PWM_EMBEDDED | ||
766 | case PWM_HASH_SHA256: | 754 | case PWM_HASH_SHA256: |
767 | /*... fall through */ | 755 | /*... fall through */ |
768 | case PWM_HASH_SHA384: | 756 | case PWM_HASH_SHA384: |
769 | case PWM_HASH_SHA512: | 757 | case PWM_HASH_SHA512: |
770 | case PWM_HASH_MD5: | 758 | case PWM_HASH_MD5: |
771 | case PWM_HASH_RMD160: | 759 | case PWM_HASH_RMD160: |
772 | case PWM_HASH_TIGER: { | 760 | case PWM_HASH_TIGER: { |
773 | if (!LibGCryptIf::available()) | 761 | if (!LibGCryptIf::available()) |
774 | return e_hashNotImpl; | 762 | return e_hashNotImpl; |
775 | LibGCryptIf gc; | 763 | LibGCryptIf gc; |
776 | PwMerror err; | 764 | PwMerror err; |
777 | unsigned char *buf; | 765 | unsigned char *buf; |
778 | size_t hashLen; | 766 | size_t hashLen; |
779 | err = gc.hash(&buf, | 767 | err = gc.hash(&buf, |
780 | &hashLen, | 768 | &hashLen, |
781 | reinterpret_cast<const unsigned char *>(pw->latin1()), | 769 | reinterpret_cast<const unsigned char *>(pw->latin1()), |
782 | pw->length(), | 770 | pw->length(), |
783 | keyHash); | 771 | keyHash); |
784 | if (err != e_success) | 772 | if (err != e_success) |
785 | return e_hashNotImpl; | 773 | return e_hashNotImpl; |
786 | string calcHash(reinterpret_cast<const char *>(buf), | 774 | string calcHash(reinterpret_cast<const char *>(buf), |
787 | static_cast<string::size_type>(hashLen)); | 775 | static_cast<string::size_type>(hashLen)); |
788 | delete [] buf; | 776 | delete [] buf; |
789 | // read hash from header | 777 | // read hash from header |
790 | string readHash; | 778 | string readHash; |
791 | size_t i; | 779 | size_t i; |
792 | for (i = 0; i < hashLen; ++i) | 780 | for (i = 0; i < hashLen; ++i) |
793 | readHash.push_back(f->getch()); | 781 | readHash.push_back(f->getch()); |
794 | if (calcHash != readHash) | 782 | if (calcHash != readHash) |
795 | return e_wrongPw;// hash doesn't match (wrong key) | 783 | return e_wrongPw;// hash doesn't match (wrong key) |
796 | break; | 784 | break; |
797 | } | 785 | } |
798 | #endif | ||
799 | default: { | 786 | default: { |
800 | return e_hashNotImpl; | 787 | return e_hashNotImpl; |
801 | } } | 788 | } } |
802 | // read the data-hash from the file | 789 | // read the data-hash from the file |
803 | unsigned int hashLen, i; | 790 | unsigned int hashLen, i; |
804 | switch (*dataHashType) { | 791 | switch (*dataHashType) { |
805 | case PWM_HASH_SHA1: | 792 | case PWM_HASH_SHA1: |
806 | hashLen = SHA1_HASH_LEN_BYTE; | 793 | hashLen = SHA1_HASH_LEN_BYTE; |
807 | break; | 794 | break; |
808 | #ifndef PWM_EMBEDDED | ||
809 | case PWM_HASH_SHA256: | 795 | case PWM_HASH_SHA256: |
810 | /*... fall through */ | 796 | /*... fall through */ |
811 | case PWM_HASH_SHA384: | 797 | case PWM_HASH_SHA384: |
812 | case PWM_HASH_SHA512: | 798 | case PWM_HASH_SHA512: |
813 | case PWM_HASH_MD5: | 799 | case PWM_HASH_MD5: |
814 | case PWM_HASH_RMD160: | 800 | case PWM_HASH_RMD160: |
815 | case PWM_HASH_TIGER: { | 801 | case PWM_HASH_TIGER: { |
816 | if (!LibGCryptIf::available()) | 802 | if (!LibGCryptIf::available()) |
817 | return e_hashNotImpl; | 803 | return e_hashNotImpl; |
818 | LibGCryptIf gc; | 804 | LibGCryptIf gc; |
819 | hashLen = gc.hashLength(*dataHashType); | 805 | hashLen = gc.hashLength(*dataHashType); |
820 | if (hashLen == 0) | 806 | if (hashLen == 0) |
821 | return e_hashNotImpl; | 807 | return e_hashNotImpl; |
822 | break; | 808 | break; |
823 | } | 809 | } |
824 | #endif | ||
825 | default: | 810 | default: |
826 | return e_hashNotImpl; | 811 | return e_hashNotImpl; |
827 | } | 812 | } |
828 | *dataHash = ""; | 813 | *dataHash = ""; |
829 | for (i = 0; i < hashLen; ++i) { | 814 | for (i = 0; i < hashLen; ++i) { |
830 | tmpRet = f->getch(); | 815 | tmpRet = f->getch(); |
831 | if (tmpRet == -1) | 816 | if (tmpRet == -1) |
832 | return e_fileFormat; | 817 | return e_fileFormat; |
833 | dataHash->push_back(static_cast<char>(tmpRet)); | 818 | dataHash->push_back(static_cast<char>(tmpRet)); |
834 | } | 819 | } |
835 | *headerLength = f->at(); | 820 | *headerLength = f->at(); |
836 | #ifndef PWM_EMBEDDED | 821 | #ifndef PWM_EMBEDDED |
837 | printDebug(string("opening file { compress: ") | 822 | printDebug(string("opening file { compress: ") |
838 | + tostr(static_cast<int>(*compress)) + " cryptAlgo: " | 823 | + tostr(static_cast<int>(*compress)) + " cryptAlgo: " |
839 | + tostr(static_cast<int>(*cryptAlgo)) + " keyHashAlgo: " | 824 | + tostr(static_cast<int>(*cryptAlgo)) + " keyHashAlgo: " |
840 | + tostr(static_cast<int>(keyHash)) | 825 | + tostr(static_cast<int>(keyHash)) |
841 | + " }"); | 826 | + " }"); |
842 | #else | 827 | #else |
843 | printDebug(string("opening file { compress: ") | 828 | printDebug(string("opening file { compress: ") |
844 | + tostr((int)(*compress)) + " cryptAlgo: " | 829 | + tostr((int)(*compress)) + " cryptAlgo: " |
845 | + tostr((int)(*cryptAlgo)) + " keyHashAlgo: " | 830 | + tostr((int)(*cryptAlgo)) + " keyHashAlgo: " |
846 | + tostr((int)(keyHash)) | 831 | + tostr((int)(keyHash)) |
847 | + " }"); | 832 | + " }"); |
848 | #endif | 833 | #endif |
849 | 834 | ||
850 | return e_success; | 835 | return e_success; |
851 | } | 836 | } |
852 | 837 | ||
853 | PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f) | 838 | PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f) |
854 | { | 839 | { |
855 | PWM_ASSERT(d); | 840 | PWM_ASSERT(d); |
856 | PWM_ASSERT(f); | 841 | PWM_ASSERT(f); |
857 | 842 | ||
858 | switch (dataHash) { | 843 | switch (dataHash) { |
859 | case PWM_HASH_SHA1: { | 844 | case PWM_HASH_SHA1: { |
860 | const int hashLen = SHA1_HASH_LEN_BYTE; | 845 | const int hashLen = SHA1_HASH_LEN_BYTE; |
861 | Sha1 h; | 846 | Sha1 h; |
862 | h.sha1_write(reinterpret_cast<const byte *>(d->c_str()), d->size()); | 847 | h.sha1_write(reinterpret_cast<const byte *>(d->c_str()), d->size()); |
863 | string hRet = h.sha1_read(); | 848 | string hRet = h.sha1_read(); |
864 | if (f->writeBlock(hRet.c_str(), hashLen) != hashLen) | 849 | if (f->writeBlock(hRet.c_str(), hashLen) != hashLen) |
865 | return e_writeFile; | 850 | return e_writeFile; |
866 | break; | 851 | break; |
867 | } | 852 | } |
868 | #ifndef PWM_EMBEDDED | ||
869 | case PWM_HASH_SHA256: | 853 | case PWM_HASH_SHA256: |
870 | /*... fall through */ | 854 | /*... fall through */ |
871 | case PWM_HASH_SHA384: | 855 | case PWM_HASH_SHA384: |
872 | case PWM_HASH_SHA512: | 856 | case PWM_HASH_SHA512: |
873 | case PWM_HASH_MD5: | 857 | case PWM_HASH_MD5: |
874 | case PWM_HASH_RMD160: | 858 | case PWM_HASH_RMD160: |
875 | case PWM_HASH_TIGER: { | 859 | case PWM_HASH_TIGER: { |
876 | if (!LibGCryptIf::available()) | 860 | if (!LibGCryptIf::available()) |
877 | return e_hashNotImpl; | 861 | return e_hashNotImpl; |
878 | LibGCryptIf gc; | 862 | LibGCryptIf gc; |
879 | PwMerror err; | 863 | PwMerror err; |
880 | unsigned char *buf; | 864 | unsigned char *buf; |
881 | size_t hashLen; | 865 | size_t hashLen; |
882 | err = gc.hash(&buf, | 866 | err = gc.hash(&buf, |
883 | &hashLen, | 867 | &hashLen, |
884 | reinterpret_cast<const unsigned char *>(d->c_str()), | 868 | reinterpret_cast<const unsigned char *>(d->c_str()), |
885 | d->size(), | 869 | d->size(), |
886 | dataHash); | 870 | dataHash); |
887 | if (err != e_success) | 871 | if (err != e_success) |
888 | return e_hashNotImpl; | 872 | return e_hashNotImpl; |
889 | if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen) | 873 | if (f->writeBlock(reinterpret_cast<const char *>(buf), hashLen) |
890 | != static_cast<Q_LONG>(hashLen)) { | 874 | != static_cast<Q_LONG>(hashLen)) { |
891 | delete [] buf; | 875 | delete [] buf; |
892 | return e_hashNotImpl; | 876 | return e_hashNotImpl; |
893 | } | 877 | } |
894 | delete [] buf; | 878 | delete [] buf; |
895 | break; | 879 | break; |
896 | } | 880 | } |
897 | #endif | ||
898 | default: { | 881 | default: { |
899 | return e_hashNotImpl; | 882 | return e_hashNotImpl; |
900 | } } | 883 | } } |
901 | 884 | ||
902 | return e_success; | 885 | return e_success; |
903 | } | 886 | } |
904 | 887 | ||
905 | bool PwMDoc::backupFile(const QString &filePath) | 888 | bool PwMDoc::backupFile(const QString &filePath) |
906 | { | 889 | { |
907 | QFileInfo fi(filePath); | 890 | QFileInfo fi(filePath); |
908 | if (!fi.exists()) | 891 | if (!fi.exists()) |
909 | return true; // Yes, true is correct. | 892 | return true; // Yes, true is correct. |
910 | QString pathOnly(fi.dirPath(true)); | 893 | QString pathOnly(fi.dirPath(true)); |
911 | QString nameOnly(fi.fileName()); | 894 | QString nameOnly(fi.fileName()); |
912 | QString backupPath = pathOnly | 895 | QString backupPath = pathOnly |
913 | + "/~" | 896 | + "/~" |
914 | + nameOnly | 897 | + nameOnly |
915 | + ".backup"; | 898 | + ".backup"; |
916 | return copyFile(filePath, backupPath); | 899 | return copyFile(filePath, backupPath); |
917 | } | 900 | } |
918 | 901 | ||
919 | bool PwMDoc::copyFile(const QString &src, const QString &dst) | 902 | bool PwMDoc::copyFile(const QString &src, const QString &dst) |
920 | { | 903 | { |
921 | QFileInfo fi(src); | 904 | QFileInfo fi(src); |
922 | if (!fi.exists()) | 905 | if (!fi.exists()) |
923 | return false; | 906 | return false; |
924 | if (QFile::exists(dst)) { | 907 | if (QFile::exists(dst)) { |
925 | if (!QFile::remove(dst)) | 908 | if (!QFile::remove(dst)) |
926 | return false; | 909 | return false; |
927 | } | 910 | } |
928 | QFile srcFd(src); | 911 | QFile srcFd(src); |
929 | if (!srcFd.open(IO_ReadOnly)) | 912 | if (!srcFd.open(IO_ReadOnly)) |
930 | return false; | 913 | return false; |
931 | QFile dstFd(dst); | 914 | QFile dstFd(dst); |
932 | if (!dstFd.open(IO_ReadWrite)) { | 915 | if (!dstFd.open(IO_ReadWrite)) { |
933 | srcFd.close(); | 916 | srcFd.close(); |
934 | return false; | 917 | return false; |
935 | } | 918 | } |
936 | const int tmpBuf_size = 512; | 919 | const int tmpBuf_size = 512; |
937 | char tmpBuf[tmpBuf_size]; | 920 | char tmpBuf[tmpBuf_size]; |
938 | #ifndef PWM_EMBEDDED | ||
939 | Q_LONG bytesRead, bytesWritten; | 921 | Q_LONG bytesRead, bytesWritten; |
940 | #else | 922 | |
941 | long bytesRead, bytesWritten; | ||
942 | #endif | ||
943 | while (!srcFd.atEnd()) { | 923 | while (!srcFd.atEnd()) { |
944 | #ifndef PWM_EMBEDDED | ||
945 | bytesRead = srcFd.readBlock(tmpBuf, | 924 | bytesRead = srcFd.readBlock(tmpBuf, |
946 | static_cast<Q_ULONG>(tmpBuf_size)); | 925 | static_cast<Q_ULONG>(tmpBuf_size)); |
947 | #else | ||
948 | bytesRead = srcFd.readBlock(tmpBuf, | ||
949 | (unsigned long)(tmpBuf_size)); | ||
950 | #endif | ||
951 | if (bytesRead == -1) { | 926 | if (bytesRead == -1) { |
952 | srcFd.close(); | 927 | srcFd.close(); |
953 | dstFd.close(); | 928 | dstFd.close(); |
954 | return false; | 929 | return false; |
955 | } | 930 | } |
956 | #ifndef PWM_EMBEDDED | ||
957 | bytesWritten = dstFd.writeBlock(tmpBuf, | 931 | bytesWritten = dstFd.writeBlock(tmpBuf, |
958 | static_cast<Q_ULONG>(bytesRead)); | 932 | static_cast<Q_ULONG>(bytesRead)); |
959 | #else | ||
960 | bytesWritten = dstFd.writeBlock(tmpBuf, | ||
961 | (unsigned long)(bytesRead)); | ||
962 | #endif | ||
963 | if (bytesWritten != bytesRead) { | 933 | if (bytesWritten != bytesRead) { |
964 | srcFd.close(); | 934 | srcFd.close(); |
965 | dstFd.close(); | 935 | dstFd.close(); |
966 | return false; | 936 | return false; |
967 | } | 937 | } |
968 | } | 938 | } |
969 | srcFd.close(); | 939 | srcFd.close(); |
970 | dstFd.close(); | 940 | dstFd.close(); |
971 | return true; | 941 | return true; |
972 | } | 942 | } |
973 | 943 | ||
974 | PwMerror PwMDoc::addEntry(const QString &category, PwMDataItem *d, | 944 | PwMerror PwMDoc::addEntry(const QString &category, PwMDataItem *d, |
975 | bool dontFlagDirty, bool updateMeta) | 945 | bool dontFlagDirty, bool updateMeta) |
976 | { | 946 | { |
977 | PWM_ASSERT(d); | 947 | PWM_ASSERT(d); |
978 | unsigned int cat = 0; | 948 | unsigned int cat = 0; |
979 | 949 | ||
980 | if (isDeepLocked()) { | 950 | if (isDeepLocked()) { |
981 | PwMerror ret; | 951 | PwMerror ret; |
982 | ret = deepLock(false); | 952 | ret = deepLock(false); |
983 | if (ret != e_success) | 953 | if (ret != e_success) |
984 | return e_lock; | 954 | return e_lock; |
985 | } | 955 | } |
986 | 956 | ||
987 | addCategory(category, &cat); | 957 | addCategory(category, &cat); |
988 | 958 | ||
989 | if (numEntries(category) >= maxEntries) | 959 | if (numEntries(category) >= maxEntries) |
990 | return e_maxAllowedEntr; | 960 | return e_maxAllowedEntr; |
991 | 961 | ||
992 | vector<unsigned int> foundPositions; | 962 | vector<unsigned int> foundPositions; |
993 | /* historically this was: | 963 | /* historically this was: |
994 | *const int searchIn = SEARCH_IN_DESC | SEARCH_IN_NAME | | 964 | *const int searchIn = SEARCH_IN_DESC | SEARCH_IN_NAME | |
995 | * SEARCH_IN_URL | SEARCH_IN_LAUNCHER; | 965 | * SEARCH_IN_URL | SEARCH_IN_LAUNCHER; |
996 | * But for now we only search in desc. | 966 | * But for now we only search in desc. |
997 | * That's a tweak to be KWallet compatible. But it should not add | 967 | * That's a tweak to be KWallet compatible. But it should not add |
998 | * usability-drop onto PwManager, does it? | 968 | * usability-drop onto PwManager, does it? |
999 | * (And yes, "int" was a bug. Correct is "unsigned int") | 969 | * (And yes, "int" was a bug. Correct is "unsigned int") |
1000 | */ | 970 | */ |
1001 | const unsigned int searchIn = SEARCH_IN_DESC; | 971 | const unsigned int searchIn = SEARCH_IN_DESC; |
1002 | findEntry(cat, *d, searchIn, &foundPositions, true); | 972 | findEntry(cat, *d, searchIn, &foundPositions, true); |
1003 | if (foundPositions.size()) { | 973 | if (foundPositions.size()) { |
1004 | // DOH! We found this entry. | 974 | // DOH! We found this entry. |
1005 | return e_entryExists; | 975 | return e_entryExists; |
1006 | } | 976 | } |
1007 | 977 | ||
1008 | d->listViewPos = -1; | 978 | d->listViewPos = -1; |
1009 | d->lockStat = conf()->confGlobNewEntrLockStat(); | 979 | d->lockStat = conf()->confGlobNewEntrLockStat(); |
1010 | if (updateMeta) { | 980 | if (updateMeta) { |
1011 | d->meta.create = QDateTime::currentDateTime(); | 981 | d->meta.create = QDateTime::currentDateTime(); |
1012 | d->meta.update = d->meta.create; | 982 | d->meta.update = d->meta.create; |
1013 | } | 983 | } |
1014 | dti.dta[cat].d.push_back(*d); | 984 | dti.dta[cat].d.push_back(*d); |
1015 | 985 | ||
1016 | delAllEmptyCat(true); | 986 | delAllEmptyCat(true); |
1017 | 987 | ||
1018 | if (!dontFlagDirty) | 988 | if (!dontFlagDirty) |
1019 | flagDirty(); | 989 | flagDirty(); |
1020 | return e_success; | 990 | return e_success; |
1021 | } | 991 | } |
1022 | 992 | ||
1023 | PwMerror PwMDoc::addCategory(const QString &category, unsigned int *categoryIndex, | 993 | PwMerror PwMDoc::addCategory(const QString &category, unsigned int *categoryIndex, |
1024 | bool checkIfExist) | 994 | bool checkIfExist) |
1025 | { | 995 | { |
1026 | if (isDeepLocked()) { | 996 | if (isDeepLocked()) { |
1027 | PwMerror ret; | 997 | PwMerror ret; |
1028 | ret = deepLock(false); | 998 | ret = deepLock(false); |
1029 | if (ret != e_success) | 999 | if (ret != e_success) |
1030 | return e_lock; | 1000 | return e_lock; |
1031 | } | 1001 | } |
1032 | if (checkIfExist) { | 1002 | if (checkIfExist) { |
1033 | if (findCategory(category, categoryIndex)) | 1003 | if (findCategory(category, categoryIndex)) |
1034 | return e_categoryExists; | 1004 | return e_categoryExists; |
1035 | } | 1005 | } |
1036 | PwMCategoryItem item; | 1006 | PwMCategoryItem item; |
1037 | item.name = category.latin1(); | 1007 | item.name = category.latin1(); |
1038 | dti.dta.push_back(item); | 1008 | dti.dta.push_back(item); |
1039 | if (categoryIndex) | 1009 | if (categoryIndex) |
1040 | *categoryIndex = dti.dta.size() - 1; | 1010 | *categoryIndex = dti.dta.size() - 1; |
1041 | return e_success; | 1011 | return e_success; |
1042 | } | 1012 | } |
1043 | 1013 | ||
1044 | bool PwMDoc::delEntry(const QString &category, unsigned int index, bool dontFlagDirty) | 1014 | bool PwMDoc::delEntry(const QString &category, unsigned int index, bool dontFlagDirty) |
1045 | { | 1015 | { |
1046 | unsigned int cat = 0; | 1016 | unsigned int cat = 0; |
1047 | 1017 | ||
1048 | if (!findCategory(category, &cat)) { | 1018 | if (!findCategory(category, &cat)) { |
1049 | BUG(); | 1019 | BUG(); |
1050 | return false; | 1020 | return false; |
1051 | } | 1021 | } |
1052 | 1022 | ||
1053 | return delEntry(cat, index, dontFlagDirty); | 1023 | return delEntry(cat, index, dontFlagDirty); |
1054 | } | 1024 | } |
1055 | 1025 | ||
1056 | bool PwMDoc::delEntry(unsigned int category, unsigned int index, bool dontFlagDirty) | 1026 | bool PwMDoc::delEntry(unsigned int category, unsigned int index, bool dontFlagDirty) |
1057 | { | 1027 | { |
1058 | if (isDeepLocked()) | 1028 | if (isDeepLocked()) |
1059 | return false; | 1029 | return false; |
1060 | if (index > dti.dta[category].d.size() - 1) | 1030 | if (index > dti.dta[category].d.size() - 1) |
1061 | return false; | 1031 | return false; |
1062 | getDataChangedLock(); | 1032 | getDataChangedLock(); |
1063 | if (!lockAt(category, index, false)) { | 1033 | if (!lockAt(category, index, false)) { |
1064 | putDataChangedLock(); | 1034 | putDataChangedLock(); |
1065 | return false; | 1035 | return false; |
1066 | } | 1036 | } |
1067 | putDataChangedLock(); | 1037 | putDataChangedLock(); |
1068 | int lvPos = dti.dta[category].d[index].listViewPos; | 1038 | int lvPos = dti.dta[category].d[index].listViewPos; |
1069 | 1039 | ||
1070 | // delete entry | 1040 | // delete entry |
1071 | dti.dta[category].d.erase(dti.dta[category].d.begin() + index); | 1041 | dti.dta[category].d.erase(dti.dta[category].d.begin() + index); |
1072 | 1042 | ||
1073 | unsigned int i, entries = numEntries(category); | 1043 | unsigned int i, entries = numEntries(category); |
1074 | if (!entries) { | 1044 | if (!entries) { |
1075 | // no more entries in this category, so | 1045 | // no more entries in this category, so |
1076 | // we can delete it, too. | 1046 | // we can delete it, too. |
1077 | BUG_ON(!delCategory(category)); | 1047 | BUG_ON(!delCategory(category)); |
1078 | // delCategory() flags it dirty, so we need not to do so. | 1048 | // delCategory() flags it dirty, so we need not to do so. |
1079 | return true; | 1049 | return true; |
1080 | } | 1050 | } |
1081 | for (i = 0; i < entries; ++i) { | 1051 | for (i = 0; i < entries; ++i) { |
1082 | // decrement all listViewPositions that are greater than the deleted. | 1052 | // decrement all listViewPositions that are greater than the deleted. |
1083 | if (dti.dta[category].d[i].listViewPos > lvPos) | 1053 | if (dti.dta[category].d[i].listViewPos > lvPos) |
1084 | --dti.dta[category].d[i].listViewPos; | 1054 | --dti.dta[category].d[i].listViewPos; |
1085 | } | 1055 | } |
1086 | 1056 | ||
1087 | if (!dontFlagDirty) | 1057 | if (!dontFlagDirty) |
1088 | flagDirty(); | 1058 | flagDirty(); |
1089 | return true; | 1059 | return true; |
1090 | } | 1060 | } |
1091 | 1061 | ||
1092 | bool PwMDoc::editEntry(const QString &oldCategory, const QString &newCategory, | 1062 | bool PwMDoc::editEntry(const QString &oldCategory, const QString &newCategory, |
1093 | unsigned int index, PwMDataItem *d, bool updateMeta) | 1063 | unsigned int index, PwMDataItem *d, bool updateMeta) |
1094 | { | 1064 | { |
1095 | PWM_ASSERT(d); | 1065 | PWM_ASSERT(d); |
1096 | unsigned int oldCat = 0; | 1066 | unsigned int oldCat = 0; |
1097 | 1067 | ||
1098 | if (!findCategory(oldCategory, &oldCat)) { | 1068 | if (!findCategory(oldCategory, &oldCat)) { |
1099 | BUG(); | 1069 | BUG(); |
1100 | return false; | 1070 | return false; |
1101 | } | 1071 | } |
1102 | 1072 | ||
1103 | return editEntry(oldCat, newCategory, index, d, updateMeta); | 1073 | return editEntry(oldCat, newCategory, index, d, updateMeta); |
1104 | } | 1074 | } |
1105 | 1075 | ||
1106 | bool PwMDoc::editEntry(unsigned int oldCategory, const QString &newCategory, | 1076 | bool PwMDoc::editEntry(unsigned int oldCategory, const QString &newCategory, |
1107 | unsigned int index, PwMDataItem *d, bool updateMeta) | 1077 | unsigned int index, PwMDataItem *d, bool updateMeta) |
1108 | { | 1078 | { |
1109 | if (isDeepLocked()) | 1079 | if (isDeepLocked()) |
1110 | return false; | 1080 | return false; |
1111 | if (updateMeta) { | 1081 | if (updateMeta) { |
1112 | d->meta.update = QDateTime::currentDateTime(); | 1082 | d->meta.update = QDateTime::currentDateTime(); |
1113 | if (d->meta.create.isNull()) { | 1083 | if (d->meta.create.isNull()) { |
1114 | d->meta.create = d->meta.update; | 1084 | d->meta.create = d->meta.update; |
1115 | } | 1085 | } |
1116 | } | 1086 | } |
1117 | if (dti.dta[oldCategory].name != newCategory.latin1()) { | 1087 | if (dti.dta[oldCategory].name != newCategory.latin1()) { |
1118 | // the user changed the category. | 1088 | // the user changed the category. |
1119 | PwMerror ret; | 1089 | PwMerror ret; |
1120 | d->rev = 0; | 1090 | d->rev = 0; |
1121 | ret = addEntry(newCategory, d, true, false); | 1091 | ret = addEntry(newCategory, d, true, false); |
1122 | if (ret != e_success) | 1092 | if (ret != e_success) |
1123 | return false; | 1093 | return false; |
1124 | if (!delEntry(oldCategory, index, true)) | 1094 | if (!delEntry(oldCategory, index, true)) |
1125 | return false; | 1095 | return false; |
1126 | } else { | 1096 | } else { |
1127 | d->rev = dti.dta[oldCategory].d[index].rev + 1; // increment revision counter. | 1097 | d->rev = dti.dta[oldCategory].d[index].rev + 1; // increment revision counter. |
1128 | dti.dta[oldCategory].d[index] = *d; | 1098 | dti.dta[oldCategory].d[index] = *d; |
1129 | } | 1099 | } |
1130 | flagDirty(); | 1100 | flagDirty(); |
1131 | return true; | 1101 | return true; |
1132 | } | 1102 | } |
1133 | 1103 | ||
1134 | unsigned int PwMDoc::numEntries(const QString &category) | 1104 | unsigned int PwMDoc::numEntries(const QString &category) |
1135 | { | 1105 | { |
1136 | unsigned int cat = 0; | 1106 | unsigned int cat = 0; |
1137 | 1107 | ||
1138 | if (!findCategory(category, &cat)) { | 1108 | if (!findCategory(category, &cat)) { |
1139 | BUG(); | 1109 | BUG(); |
1140 | return 0; | 1110 | return 0; |
1141 | } | 1111 | } |
1142 | 1112 | ||
1143 | return numEntries(cat); | 1113 | return numEntries(cat); |
1144 | } | 1114 | } |
1145 | 1115 | ||
1146 | bool PwMDoc::serializeDta(string *d) | 1116 | bool PwMDoc::serializeDta(string *d) |
1147 | { | 1117 | { |
1148 | PWM_ASSERT(d); | 1118 | PWM_ASSERT(d); |
1149 | Serializer ser; | 1119 | Serializer ser; |
1150 | if (!ser.serialize(dti)) | 1120 | if (!ser.serialize(dti)) |
1151 | return false; | 1121 | return false; |
1152 | d->assign(ser.getXml()); | 1122 | d->assign(ser.getXml()); |
1153 | if (!d->size()) | 1123 | if (!d->size()) |
1154 | return false; | 1124 | return false; |
1155 | return true; | 1125 | return true; |
1156 | } | 1126 | } |
1157 | 1127 | ||
1158 | bool PwMDoc::deSerializeDta(const string *d, bool entriesLocked) | 1128 | bool PwMDoc::deSerializeDta(const string *d, bool entriesLocked) |
1159 | { | 1129 | { |
1160 | PWM_ASSERT(d); | 1130 | PWM_ASSERT(d); |
1161 | #ifndef PWM_EMBEDDED | 1131 | #ifndef PWM_EMBEDDED |
1162 | try { | 1132 | try { |
1163 | 1133 | ||
1164 | Serializer ser(d->c_str()); | 1134 | Serializer ser(d->c_str()); |
1165 | ser.setDefaultLockStat(entriesLocked); | 1135 | ser.setDefaultLockStat(entriesLocked); |
1166 | if (!ser.deSerialize(&dti)) | 1136 | if (!ser.deSerialize(&dti)) |
1167 | return false; | 1137 | return false; |
1168 | } catch (PwMException) { | 1138 | } catch (PwMException) { |
1169 | return false; | 1139 | return false; |
1170 | } | 1140 | } |
1171 | #else | 1141 | #else |
1172 | Serializer ser(d->c_str()); | 1142 | Serializer ser(d->c_str()); |
1173 | ser.setDefaultLockStat(entriesLocked); | 1143 | ser.setDefaultLockStat(entriesLocked); |
1174 | if (!ser.deSerialize(&dti)) | 1144 | if (!ser.deSerialize(&dti)) |
1175 | return false; | 1145 | return false; |
1176 | #endif | 1146 | #endif |
1177 | 1147 | ||
1178 | emitDataChanged(this); | 1148 | emitDataChanged(this); |
1179 | return true; | 1149 | return true; |
1180 | } | 1150 | } |
1181 | 1151 | ||
1182 | bool PwMDoc::getEntry(const QString &category, unsigned int index, | 1152 | bool PwMDoc::getEntry(const QString &category, unsigned int index, |
1183 | PwMDataItem * d, bool unlockIfLocked) | 1153 | PwMDataItem * d, bool unlockIfLocked) |
1184 | { | 1154 | { |
1185 | PWM_ASSERT(d); | 1155 | PWM_ASSERT(d); |
1186 | unsigned int cat = 0; | 1156 | unsigned int cat = 0; |
1187 | 1157 | ||
1188 | if (!findCategory(category, &cat)) { | 1158 | if (!findCategory(category, &cat)) { |
1189 | BUG(); | 1159 | BUG(); |
1190 | return false; | 1160 | return false; |
1191 | } | 1161 | } |
1192 | 1162 | ||
1193 | return getEntry(cat, index, d, unlockIfLocked); | 1163 | return getEntry(cat, index, d, unlockIfLocked); |
1194 | } | 1164 | } |
1195 | 1165 | ||
1196 | bool PwMDoc::getEntry(unsigned int category, unsigned int index, | 1166 | bool PwMDoc::getEntry(unsigned int category, unsigned int index, |
1197 | PwMDataItem *d, bool unlockIfLocked) | 1167 | PwMDataItem *d, bool unlockIfLocked) |
1198 | { | 1168 | { |
1199 | if (index > dti.dta[category].d.size() - 1) | 1169 | if (index > dti.dta[category].d.size() - 1) |
1200 | return false; | 1170 | return false; |
1201 | 1171 | ||
1202 | bool locked = isLocked(category, index); | 1172 | bool locked = isLocked(category, index); |
1203 | if (locked) { | 1173 | if (locked) { |
1204 | /* this entry is locked. We don't return a password, | 1174 | /* this entry is locked. We don't return a password, |
1205 | * until it's unlocked by the user by inserting | 1175 | * until it's unlocked by the user by inserting |
1206 | * chipcard or entering the mpw | 1176 | * chipcard or entering the mpw |
1207 | */ | 1177 | */ |
1208 | if (unlockIfLocked) { | 1178 | if (unlockIfLocked) { |
1209 | if (!lockAt(category, index, false)) { | 1179 | if (!lockAt(category, index, false)) { |
1210 | return false; | 1180 | return false; |
1211 | } | 1181 | } |
1212 | locked = false; | 1182 | locked = false; |
1213 | } | 1183 | } |
1214 | } | 1184 | } |
1215 | 1185 | ||
1216 | *d = dti.dta[category].d[index]; | 1186 | *d = dti.dta[category].d[index]; |
1217 | if (locked) | 1187 | if (locked) |
1218 | d->pw = LOCKED_STRING.latin1(); | 1188 | d->pw = LOCKED_STRING.latin1(); |
1219 | 1189 | ||
1220 | return true; | 1190 | return true; |
1221 | } | 1191 | } |
1222 | 1192 | ||
1223 | PwMerror PwMDoc::getCommentByLvp(const QString &category, int listViewPos, | 1193 | PwMerror PwMDoc::getCommentByLvp(const QString &category, int listViewPos, |
1224 | string *foundComment) | 1194 | string *foundComment) |
1225 | { | 1195 | { |
1226 | PWM_ASSERT(foundComment); | 1196 | PWM_ASSERT(foundComment); |
1227 | unsigned int cat = 0; | 1197 | unsigned int cat = 0; |
1228 | 1198 | ||
1229 | if (!findCategory(category, &cat)) | 1199 | if (!findCategory(category, &cat)) |
1230 | return e_invalidArg; | 1200 | return e_invalidArg; |
1231 | 1201 | ||
1232 | unsigned int i, entries = numEntries(cat); | 1202 | unsigned int i, entries = numEntries(cat); |
1233 | for (i = 0; i < entries; ++i) { | 1203 | for (i = 0; i < entries; ++i) { |
1234 | if (dti.dta[cat].d[i].listViewPos == listViewPos) { | 1204 | if (dti.dta[cat].d[i].listViewPos == listViewPos) { |
1235 | *foundComment = dti.dta[cat].d[i].comment; | 1205 | *foundComment = dti.dta[cat].d[i].comment; |
1236 | if (dti.dta[cat].d[i].binary) | 1206 | if (dti.dta[cat].d[i].binary) |
1237 | return e_binEntry; | 1207 | return e_binEntry; |
1238 | return e_normalEntry; | 1208 | return e_normalEntry; |
1239 | } | 1209 | } |
1240 | } | 1210 | } |
1241 | BUG(); | 1211 | BUG(); |
1242 | return e_generic; | 1212 | return e_generic; |
1243 | } | 1213 | } |
1244 | 1214 | ||
1245 | bool PwMDoc::compressDta(string *d, char algo) | 1215 | bool PwMDoc::compressDta(string *d, char algo) |
1246 | { | 1216 | { |
1247 | PWM_ASSERT(d); | 1217 | PWM_ASSERT(d); |
1248 | switch (algo) { | 1218 | switch (algo) { |
1249 | case PWM_COMPRESS_GZIP: { | 1219 | case PWM_COMPRESS_GZIP: { |
1250 | CompressGzip comp; | 1220 | CompressGzip comp; |
1251 | return comp.compress(d); | 1221 | return comp.compress(d); |
1252 | } case PWM_COMPRESS_BZIP2: { | 1222 | } case PWM_COMPRESS_BZIP2: { |
1253 | CompressBzip2 comp; | 1223 | CompressBzip2 comp; |
1254 | return comp.compress(d); | 1224 | return comp.compress(d); |
1255 | } case PWM_COMPRESS_NONE: { | 1225 | } case PWM_COMPRESS_NONE: { |
1256 | return true; | 1226 | return true; |
1257 | } default: { | 1227 | } default: { |
1258 | BUG(); | 1228 | BUG(); |
1259 | } | 1229 | } |
1260 | } | 1230 | } |
1261 | return false; | 1231 | return false; |
1262 | } | 1232 | } |
1263 | 1233 | ||
1264 | bool PwMDoc::decompressDta(string *d, char algo) | 1234 | bool PwMDoc::decompressDta(string *d, char algo) |
1265 | { | 1235 | { |
1266 | PWM_ASSERT(d); | 1236 | PWM_ASSERT(d); |
1267 | switch (algo) { | 1237 | switch (algo) { |
1268 | case PWM_COMPRESS_GZIP: { | 1238 | case PWM_COMPRESS_GZIP: { |
1269 | CompressGzip comp; | 1239 | CompressGzip comp; |
1270 | return comp.decompress(d); | 1240 | return comp.decompress(d); |
1271 | } case PWM_COMPRESS_BZIP2: { | 1241 | } case PWM_COMPRESS_BZIP2: { |
1272 | CompressBzip2 comp; | 1242 | CompressBzip2 comp; |
1273 | return comp.decompress(d); | 1243 | return comp.decompress(d); |
1274 | } case PWM_COMPRESS_NONE: { | 1244 | } case PWM_COMPRESS_NONE: { |
1275 | return true; | 1245 | return true; |
1276 | } | 1246 | } |
1277 | } | 1247 | } |
1278 | return false; | 1248 | return false; |
1279 | } | 1249 | } |
1280 | 1250 | ||
1281 | PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo) | 1251 | PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo) |
1282 | { | 1252 | { |
1283 | PWM_ASSERT(d); | 1253 | PWM_ASSERT(d); |
1284 | PWM_ASSERT(pw); | 1254 | PWM_ASSERT(pw); |
1285 | PWM_ASSERT(f); | 1255 | PWM_ASSERT(f); |
1286 | 1256 | ||
1287 | size_t encSize; | 1257 | size_t encSize; |
1288 | byte *encrypted = 0; | 1258 | byte *encrypted = 0; |
1289 | 1259 | ||
1290 | switch (algo) { | 1260 | switch (algo) { |
1291 | case PWM_CRYPT_BLOWFISH: { | 1261 | case PWM_CRYPT_BLOWFISH: { |
1292 | Blowfish::padNull(d); | 1262 | Blowfish::padNull(d); |
1293 | encSize = d->length(); | 1263 | encSize = d->length(); |
1294 | encrypted = new byte[encSize]; | 1264 | encrypted = new byte[encSize]; |
1295 | Blowfish bf; | 1265 | Blowfish bf; |
1296 | if (bf.bf_setkey((byte *) pw->latin1(), pw->length())) { | 1266 | if (bf.bf_setkey((byte *) pw->latin1(), pw->length())) { |
1297 | delete [] encrypted; | 1267 | delete [] encrypted; |
1298 | return e_weakPw; | 1268 | return e_weakPw; |
1299 | } | 1269 | } |
1300 | bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize); | 1270 | bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize); |
1301 | break; | 1271 | break; |
1302 | } | 1272 | } |
1303 | #ifndef PWM_EMBEDDED | ||
1304 | case PWM_CRYPT_AES128: | 1273 | case PWM_CRYPT_AES128: |
1305 | /*... fall through */ | 1274 | /*... fall through */ |
1306 | case PWM_CRYPT_AES192: | 1275 | case PWM_CRYPT_AES192: |
1307 | case PWM_CRYPT_AES256: | 1276 | case PWM_CRYPT_AES256: |
1308 | case PWM_CRYPT_3DES: | 1277 | case PWM_CRYPT_3DES: |
1309 | case PWM_CRYPT_TWOFISH: | 1278 | case PWM_CRYPT_TWOFISH: |
1310 | case PWM_CRYPT_TWOFISH128: { | 1279 | case PWM_CRYPT_TWOFISH128: { |
1311 | if (!LibGCryptIf::available()) | 1280 | if (!LibGCryptIf::available()) |
1312 | return e_cryptNotImpl; | 1281 | return e_cryptNotImpl; |
1313 | LibGCryptIf gc; | 1282 | LibGCryptIf gc; |
1314 | PwMerror err; | 1283 | PwMerror err; |
1315 | unsigned char *plain = new unsigned char[d->length() + 1024]; | 1284 | unsigned char *plain = new unsigned char[d->length() + 1024]; |
1316 | memcpy(plain, d->c_str(), d->length()); | 1285 | memcpy(plain, d->c_str(), d->length()); |
1317 | err = gc.encrypt(&encrypted, | 1286 | err = gc.encrypt(&encrypted, |
1318 | &encSize, | 1287 | &encSize, |
1319 | plain, | 1288 | plain, |
1320 | d->length(), | 1289 | d->length(), |
1321 | reinterpret_cast<const unsigned char *>(pw->latin1()), | 1290 | reinterpret_cast<const unsigned char *>(pw->latin1()), |
1322 | pw->length(), | 1291 | pw->length(), |
1323 | algo); | 1292 | algo); |
1324 | delete [] plain; | 1293 | delete [] plain; |
1325 | if (err != e_success) | 1294 | if (err != e_success) |
1326 | return e_cryptNotImpl; | 1295 | return e_cryptNotImpl; |
1327 | break; | 1296 | break; |
1328 | } | 1297 | } |
1329 | #endif | ||
1330 | default: { | 1298 | default: { |
1331 | delete_ifnot_null_array(encrypted); | 1299 | delete_ifnot_null_array(encrypted); |
1332 | return e_cryptNotImpl; | 1300 | return e_cryptNotImpl; |
1333 | } } | 1301 | } } |
1334 | 1302 | ||
1335 | // write encrypted data to file | 1303 | // write encrypted data to file |
1336 | #ifndef PWM_EMBEDDED | ||
1337 | if (f->writeBlock(reinterpret_cast<const char *>(encrypted), | 1304 | if (f->writeBlock(reinterpret_cast<const char *>(encrypted), |
1338 | static_cast<Q_ULONG>(encSize)) | 1305 | static_cast<Q_ULONG>(encSize)) |
1339 | != static_cast<Q_LONG>(encSize)) { | 1306 | != static_cast<Q_LONG>(encSize)) { |
1340 | delete_ifnot_null_array(encrypted); | 1307 | delete_ifnot_null_array(encrypted); |
1341 | return e_writeFile; | 1308 | return e_writeFile; |
1342 | } | 1309 | } |
1343 | #else | ||
1344 | if (f->writeBlock((const char *)(encrypted), | ||
1345 | (unsigned long)(encSize)) | ||
1346 | != (long)(encSize)) { | ||
1347 | delete_ifnot_null_array(encrypted); | ||
1348 | return e_writeFile; | ||
1349 | } | ||
1350 | #endif | ||
1351 | delete_ifnot_null_array(encrypted); | 1310 | delete_ifnot_null_array(encrypted); |
1352 | return e_success; | 1311 | return e_success; |
1353 | } | 1312 | } |
1354 | 1313 | ||
1355 | PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw, | 1314 | PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw, |
1356 | char algo, QFile *f) | 1315 | char algo, QFile *f) |
1357 | { | 1316 | { |
1358 | PWM_ASSERT(d); | 1317 | PWM_ASSERT(d); |
1359 | PWM_ASSERT(pw); | 1318 | PWM_ASSERT(pw); |
1360 | PWM_ASSERT(f); | 1319 | PWM_ASSERT(f); |
1361 | 1320 | ||
1362 | unsigned int cryptLen = f->size() - pos; | 1321 | unsigned int cryptLen = f->size() - pos; |
1363 | byte *encrypted = new byte[cryptLen]; | 1322 | byte *encrypted = new byte[cryptLen]; |
1364 | byte *decrypted = new byte[cryptLen]; | 1323 | byte *decrypted = new byte[cryptLen]; |
1365 | 1324 | ||
1366 | f->at(pos); | 1325 | f->at(pos); |
1367 | #ifndef PWM_EMBEDDED | 1326 | #ifndef PWM_EMBEDDED |
1368 | if (f->readBlock(reinterpret_cast<char *>(encrypted), | 1327 | if (f->readBlock(reinterpret_cast<char *>(encrypted), |
1369 | static_cast<Q_ULONG>(cryptLen)) | 1328 | static_cast<Q_ULONG>(cryptLen)) |
1370 | != static_cast<Q_LONG>(cryptLen)) { | 1329 | != static_cast<Q_LONG>(cryptLen)) { |
1371 | delete [] encrypted; | 1330 | delete [] encrypted; |
1372 | delete [] decrypted; | 1331 | delete [] decrypted; |
1373 | return e_readFile; | 1332 | return e_readFile; |
1374 | } | 1333 | } |
1375 | #else | 1334 | #else |
1376 | if (f->readBlock((char *)(encrypted), | 1335 | if (f->readBlock((char *)(encrypted), |
1377 | (unsigned long)(cryptLen)) | 1336 | (unsigned long)(cryptLen)) |
1378 | != (long)(cryptLen)) { | 1337 | != (long)(cryptLen)) { |
1379 | delete [] encrypted; | 1338 | delete [] encrypted; |
1380 | delete [] decrypted; | 1339 | delete [] decrypted; |
1381 | return e_readFile; | 1340 | return e_readFile; |
1382 | } | 1341 | } |
1383 | #endif | 1342 | #endif |
1384 | switch (algo) { | 1343 | switch (algo) { |
1385 | case PWM_CRYPT_BLOWFISH: { | 1344 | case PWM_CRYPT_BLOWFISH: { |
1386 | Blowfish bf; | 1345 | Blowfish bf; |
1387 | bf.bf_setkey((byte *) pw->latin1(), pw->length()); | 1346 | bf.bf_setkey((byte *) pw->latin1(), pw->length()); |
1388 | bf.bf_decrypt(decrypted, encrypted, cryptLen); | 1347 | bf.bf_decrypt(decrypted, encrypted, cryptLen); |
1389 | break; | 1348 | break; |
1390 | } | 1349 | } |
1391 | #ifndef PWM_EMBEDDED | ||
1392 | case PWM_CRYPT_AES128: | 1350 | case PWM_CRYPT_AES128: |
1393 | /*... fall through */ | 1351 | /*... fall through */ |
1394 | case PWM_CRYPT_AES192: | 1352 | case PWM_CRYPT_AES192: |
1395 | case PWM_CRYPT_AES256: | 1353 | case PWM_CRYPT_AES256: |
1396 | case PWM_CRYPT_3DES: | 1354 | case PWM_CRYPT_3DES: |
1397 | case PWM_CRYPT_TWOFISH: | 1355 | case PWM_CRYPT_TWOFISH: |
1398 | case PWM_CRYPT_TWOFISH128: { | 1356 | case PWM_CRYPT_TWOFISH128: { |
1399 | if (!LibGCryptIf::available()) | 1357 | if (!LibGCryptIf::available()) |
1400 | return e_cryptNotImpl; | 1358 | return e_cryptNotImpl; |
1401 | LibGCryptIf gc; | 1359 | LibGCryptIf gc; |
1402 | PwMerror err; | 1360 | PwMerror err; |
1403 | err = gc.decrypt(&decrypted, | 1361 | err = gc.decrypt(&decrypted, |
1404 | &cryptLen, | 1362 | &cryptLen, |
1405 | encrypted, | 1363 | encrypted, |
1406 | cryptLen, | 1364 | cryptLen, |
1407 | reinterpret_cast<const unsigned char *>(pw->latin1()), | 1365 | reinterpret_cast<const unsigned char *>(pw->latin1()), |
1408 | pw->length(), | 1366 | pw->length(), |
1409 | algo); | 1367 | algo); |
1410 | if (err != e_success) { | 1368 | if (err != e_success) { |
1411 | delete [] encrypted; | 1369 | delete [] encrypted; |
1412 | delete [] decrypted; | 1370 | delete [] decrypted; |
1413 | return e_cryptNotImpl; | 1371 | return e_cryptNotImpl; |
1414 | } | 1372 | } |
1415 | break; | 1373 | break; |
1416 | } | 1374 | } |
1417 | #endif | ||
1418 | default: { | 1375 | default: { |
1419 | delete [] encrypted; | 1376 | delete [] encrypted; |
1420 | delete [] decrypted; | 1377 | delete [] decrypted; |
1421 | return e_cryptNotImpl; | 1378 | return e_cryptNotImpl; |
1422 | } } | 1379 | } } |
1423 | delete [] encrypted; | 1380 | delete [] encrypted; |
1424 | #ifndef PWM_EMBEDDED | 1381 | #ifndef PWM_EMBEDDED |
1425 | d->assign(reinterpret_cast<const char *>(decrypted), | 1382 | d->assign(reinterpret_cast<const char *>(decrypted), |
1426 | static_cast<string::size_type>(cryptLen)); | 1383 | static_cast<string::size_type>(cryptLen)); |
1427 | #else | 1384 | #else |
1428 | d->assign((const char *)(decrypted), | 1385 | d->assign((const char *)(decrypted), |
1429 | (string::size_type)(cryptLen)); | 1386 | (string::size_type)(cryptLen)); |
1430 | #endif | 1387 | #endif |
1431 | delete [] decrypted; | 1388 | delete [] decrypted; |
1432 | if (algo == PWM_CRYPT_BLOWFISH) { | 1389 | if (algo == PWM_CRYPT_BLOWFISH) { |
1433 | if (!Blowfish::unpadNull(d)) { | 1390 | if (!Blowfish::unpadNull(d)) { |
1434 | BUG(); | 1391 | BUG(); |
1435 | return e_readFile; | 1392 | return e_readFile; |
1436 | } | 1393 | } |
1437 | } | 1394 | } |
1438 | return e_success; | 1395 | return e_success; |
1439 | } | 1396 | } |
1440 | 1397 | ||
1441 | PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash, | 1398 | PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash, |
1442 | const string *dataStream) | 1399 | const string *dataStream) |
1443 | { | 1400 | { |
1444 | PWM_ASSERT(dataHash); | 1401 | PWM_ASSERT(dataHash); |
1445 | PWM_ASSERT(dataStream); | 1402 | PWM_ASSERT(dataStream); |
1446 | switch(dataHashType) { | 1403 | switch(dataHashType) { |
1447 | case PWM_HASH_SHA1: { | 1404 | case PWM_HASH_SHA1: { |
1448 | Sha1 hash; | 1405 | Sha1 hash; |
1449 | hash.sha1_write((byte*)dataStream->c_str(), dataStream->length()); | 1406 | hash.sha1_write((byte*)dataStream->c_str(), dataStream->length()); |
1450 | string ret = hash.sha1_read(); | 1407 | string ret = hash.sha1_read(); |
1451 | if (ret != *dataHash) | 1408 | if (ret != *dataHash) |
1452 | return e_fileCorrupt; | 1409 | return e_fileCorrupt; |
1453 | break; | 1410 | break; |
1454 | } | 1411 | } |
1455 | #ifndef PWM_EMBEDDED | ||
1456 | case PWM_HASH_SHA256: | 1412 | case PWM_HASH_SHA256: |
1457 | /*... fall through */ | 1413 | /*... fall through */ |
1458 | case PWM_HASH_SHA384: | 1414 | case PWM_HASH_SHA384: |
1459 | case PWM_HASH_SHA512: | 1415 | case PWM_HASH_SHA512: |
1460 | case PWM_HASH_MD5: | 1416 | case PWM_HASH_MD5: |
1461 | case PWM_HASH_RMD160: | 1417 | case PWM_HASH_RMD160: |
1462 | case PWM_HASH_TIGER: { | 1418 | case PWM_HASH_TIGER: { |
1463 | if (!LibGCryptIf::available()) | 1419 | if (!LibGCryptIf::available()) |
1464 | return e_hashNotImpl; | 1420 | return e_hashNotImpl; |
1465 | LibGCryptIf gc; | 1421 | LibGCryptIf gc; |
1466 | PwMerror err; | 1422 | PwMerror err; |
1467 | unsigned char *buf; | 1423 | unsigned char *buf; |
1468 | size_t hashLen; | 1424 | size_t hashLen; |
1469 | err = gc.hash(&buf, | 1425 | err = gc.hash(&buf, |
1470 | &hashLen, | 1426 | &hashLen, |
1471 | reinterpret_cast<const unsigned char *>(dataStream->c_str()), | 1427 | reinterpret_cast<const unsigned char *>(dataStream->c_str()), |
1472 | dataStream->length(), | 1428 | dataStream->length(), |
1473 | dataHashType); | 1429 | dataHashType); |
1474 | if (err != e_success) | 1430 | if (err != e_success) |
1475 | return e_hashNotImpl; | 1431 | return e_hashNotImpl; |
1476 | string calcHash(reinterpret_cast<const char *>(buf), | 1432 | string calcHash(reinterpret_cast<const char *>(buf), |
1477 | static_cast<string::size_type>(hashLen)); | 1433 | static_cast<string::size_type>(hashLen)); |
1478 | delete [] buf; | 1434 | delete [] buf; |
1479 | if (calcHash != *dataHash) | 1435 | if (calcHash != *dataHash) |
1480 | return e_fileCorrupt; | 1436 | return e_fileCorrupt; |
1481 | break; | 1437 | break; |
1482 | } | 1438 | } |
1483 | #endif | ||
1484 | default: | 1439 | default: |
1485 | return e_hashNotImpl; | 1440 | return e_hashNotImpl; |
1486 | } | 1441 | } |
1487 | return e_success; | 1442 | return e_success; |
1488 | } | 1443 | } |
1489 | 1444 | ||
1490 | bool PwMDoc::lockAt(unsigned int category, unsigned int index, | 1445 | bool PwMDoc::lockAt(unsigned int category, unsigned int index, |
1491 | bool lock) | 1446 | bool lock) |
1492 | { | 1447 | { |
1493 | if (index >= numEntries(category)) { | 1448 | if (index >= numEntries(category)) { |
1494 | BUG(); | 1449 | BUG(); |
1495 | return false; | 1450 | return false; |
1496 | } | 1451 | } |
1497 | if (lock == dti.dta[category].d[index].lockStat) | 1452 | if (lock == dti.dta[category].d[index].lockStat) |
1498 | return true; | 1453 | return true; |
1499 | 1454 | ||
1500 | if (!lock && currentPw != "") { | 1455 | if (!lock && currentPw != "") { |
1501 | // "unlocking" and "password is already set" | 1456 | // "unlocking" and "password is already set" |
1502 | if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) { | 1457 | if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) { |
1503 | // unlocking without pw not allowed | 1458 | // unlocking without pw not allowed |
1504 | QString pw; | 1459 | QString pw; |
1505 | pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 1460 | pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
1506 | if (pw != "") { | 1461 | if (pw != "") { |
1507 | if (pw != currentPw) { | 1462 | if (pw != currentPw) { |
1508 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 1463 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
1509 | return false; | 1464 | return false; |
1510 | } else { | 1465 | } else { |
1511 | timer()->start(DocTimer::id_mpwTimer); | 1466 | timer()->start(DocTimer::id_mpwTimer); |
1512 | } | 1467 | } |
1513 | } else { | 1468 | } else { |
1514 | return false; | 1469 | return false; |
1515 | } | 1470 | } |
1516 | } else { | 1471 | } else { |
1517 | timer()->start(DocTimer::id_mpwTimer); | 1472 | timer()->start(DocTimer::id_mpwTimer); |
1518 | } | 1473 | } |
1519 | } | 1474 | } |
1520 | 1475 | ||
1521 | dti.dta[category].d[index].lockStat = lock; | 1476 | dti.dta[category].d[index].lockStat = lock; |
1522 | dti.dta[category].d[index].rev++; // increment revision counter. | 1477 | dti.dta[category].d[index].rev++; // increment revision counter. |
1523 | 1478 | ||
1524 | emitDataChanged(this); | 1479 | emitDataChanged(this); |
1525 | if (!lock) | 1480 | if (!lock) |
1526 | timer()->start(DocTimer::id_autoLockTimer); | 1481 | timer()->start(DocTimer::id_autoLockTimer); |
1527 | 1482 | ||
1528 | return true; | 1483 | return true; |
1529 | 1484 | ||
1530 | } | 1485 | } |
1531 | 1486 | ||
1532 | bool PwMDoc::lockAt(const QString &category,unsigned int index, | 1487 | bool PwMDoc::lockAt(const QString &category,unsigned int index, |
1533 | bool lock) | 1488 | bool lock) |
1534 | { | 1489 | { |
1535 | unsigned int cat = 0; | 1490 | unsigned int cat = 0; |
1536 | 1491 | ||
1537 | if (!findCategory(category, &cat)) { | 1492 | if (!findCategory(category, &cat)) { |
1538 | BUG(); | 1493 | BUG(); |
1539 | return false; | 1494 | return false; |
1540 | } | 1495 | } |
1541 | 1496 | ||
1542 | return lockAt(cat, index, lock); | 1497 | return lockAt(cat, index, lock); |
1543 | } | 1498 | } |
1544 | 1499 | ||
1545 | bool PwMDoc::lockAll(bool lock) | 1500 | bool PwMDoc::lockAll(bool lock) |
1546 | { | 1501 | { |
1547 | if (!lock && isDeepLocked()) { | 1502 | if (!lock && isDeepLocked()) { |
1548 | PwMerror ret; | 1503 | PwMerror ret; |
1549 | ret = deepLock(false); | 1504 | ret = deepLock(false); |
1550 | if (ret != e_success) | 1505 | if (ret != e_success) |
1551 | return false; | 1506 | return false; |
1552 | return true; | 1507 | return true; |
1553 | } | 1508 | } |
1554 | if (isDocEmpty()) { | 1509 | if (isDocEmpty()) { |
1555 | return true; | 1510 | return true; |
1556 | } | 1511 | } |
1557 | if (!lock && currentPw != "") { | 1512 | if (!lock && currentPw != "") { |
1558 | // unlocking and password is already set | 1513 | // unlocking and password is already set |
1559 | if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) { | 1514 | if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW)) { |
1560 | // unlocking without pw not allowed | 1515 | // unlocking without pw not allowed |
1561 | QString pw; | 1516 | QString pw; |
1562 | pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 1517 | pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
1563 | if (pw != "") { | 1518 | if (pw != "") { |
1564 | if (pw != currentPw) { | 1519 | if (pw != currentPw) { |
1565 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 1520 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
1566 | return false; | 1521 | return false; |
1567 | } else { | 1522 | } else { |
1568 | timer()->start(DocTimer::id_mpwTimer); | 1523 | timer()->start(DocTimer::id_mpwTimer); |
1569 | } | 1524 | } |
1570 | } else { | 1525 | } else { |
1571 | return false; | 1526 | return false; |
1572 | } | 1527 | } |
1573 | } else { | 1528 | } else { |
1574 | timer()->start(DocTimer::id_mpwTimer); | 1529 | timer()->start(DocTimer::id_mpwTimer); |
1575 | } | 1530 | } |
1576 | } | 1531 | } |
1577 | 1532 | ||
1578 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), | 1533 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), |
1579 | catEnd = dti.dta.end(), | 1534 | catEnd = dti.dta.end(), |
1580 | catI = catBegin; | 1535 | catI = catBegin; |
1581 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; | 1536 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; |
1582 | while (catI != catEnd) { | 1537 | while (catI != catEnd) { |
1583 | entrBegin = catI->d.begin(); | 1538 | entrBegin = catI->d.begin(); |
1584 | entrEnd = catI->d.end(); | 1539 | entrEnd = catI->d.end(); |
1585 | entrI = entrBegin; | 1540 | entrI = entrBegin; |
1586 | while (entrI != entrEnd) { | 1541 | while (entrI != entrEnd) { |
1587 | entrI->lockStat = lock; | 1542 | entrI->lockStat = lock; |
1588 | entrI->rev++; // increment revision counter. | 1543 | entrI->rev++; // increment revision counter. |
1589 | ++entrI; | 1544 | ++entrI; |
1590 | } | 1545 | } |
1591 | ++catI; | 1546 | ++catI; |
1592 | } | 1547 | } |
1593 | 1548 | ||
1594 | emitDataChanged(this); | 1549 | emitDataChanged(this); |
1595 | if (lock) | 1550 | if (lock) |
1596 | timer()->stop(DocTimer::id_autoLockTimer); | 1551 | timer()->stop(DocTimer::id_autoLockTimer); |
1597 | else | 1552 | else |
1598 | timer()->start(DocTimer::id_autoLockTimer); | 1553 | timer()->start(DocTimer::id_autoLockTimer); |
1599 | 1554 | ||
1600 | return true; | 1555 | return true; |
1601 | } | 1556 | } |
1602 | 1557 | ||
1603 | bool PwMDoc::isLocked(const QString &category, unsigned int index) | 1558 | bool PwMDoc::isLocked(const QString &category, unsigned int index) |
1604 | { | 1559 | { |
1605 | unsigned int cat = 0; | 1560 | unsigned int cat = 0; |
1606 | 1561 | ||
1607 | if (!findCategory(category, &cat)) { | 1562 | if (!findCategory(category, &cat)) { |
1608 | BUG(); | 1563 | BUG(); |
1609 | return false; | 1564 | return false; |
1610 | } | 1565 | } |
1611 | 1566 | ||
1612 | return isLocked(cat, index); | 1567 | return isLocked(cat, index); |
1613 | } | 1568 | } |
1614 | 1569 | ||
1615 | bool PwMDoc::unlockAll_tempoary(bool revert) | 1570 | bool PwMDoc::unlockAll_tempoary(bool revert) |
1616 | { | 1571 | { |
1617 | static vector< vector<bool> > *oldLockStates = 0; | 1572 | static vector< vector<bool> > *oldLockStates = 0; |
1618 | static bool wasDeepLocked; | 1573 | static bool wasDeepLocked; |
1619 | 1574 | ||
1620 | if (revert) {// revert the unlocking | 1575 | if (revert) {// revert the unlocking |
1621 | if (oldLockStates) { | 1576 | if (oldLockStates) { |
1622 | /* we actually _have_ unlocked something, because | 1577 | /* we actually _have_ unlocked something, because |
1623 | * we have allocated space for the oldLockStates. | 1578 | * we have allocated space for the oldLockStates. |
1624 | * So, go on and revert them! | 1579 | * So, go on and revert them! |
1625 | */ | 1580 | */ |
1626 | if (wasDeepLocked) { | 1581 | if (wasDeepLocked) { |
1627 | PwMerror ret = deepLock(true); | 1582 | PwMerror ret = deepLock(true); |
1628 | if (ret == e_success) { | 1583 | if (ret == e_success) { |
1629 | /* deep-lock succeed. We are save. | 1584 | /* deep-lock succeed. We are save. |
1630 | * (but if it failed, just go on | 1585 | * (but if it failed, just go on |
1631 | * lock them normally) | 1586 | * lock them normally) |
1632 | */ | 1587 | */ |
1633 | delete_and_null(oldLockStates); | 1588 | delete_and_null(oldLockStates); |
1634 | timer()->start(DocTimer::id_autoLockTimer); | 1589 | timer()->start(DocTimer::id_autoLockTimer); |
1635 | printDebug("tempoary unlocking of dta " | 1590 | printDebug("tempoary unlocking of dta " |
1636 | "reverted by deep-locking."); | 1591 | "reverted by deep-locking."); |
1637 | return true; | 1592 | return true; |
1638 | } | 1593 | } |
1639 | printDebug("deep-lock failed while reverting! " | 1594 | printDebug("deep-lock failed while reverting! " |
1640 | "Falling back to normal-lock."); | 1595 | "Falling back to normal-lock."); |
1641 | } | 1596 | } |
1642 | if (unlikely(!wasDeepLocked && | 1597 | if (unlikely(!wasDeepLocked && |
1643 | numCategories() != oldLockStates->size())) { | 1598 | numCategories() != oldLockStates->size())) { |
1644 | /* DOH! We have modified "dta" while | 1599 | /* DOH! We have modified "dta" while |
1645 | * it was unlocked tempoary. DON'T DO THIS! | 1600 | * it was unlocked tempoary. DON'T DO THIS! |
1646 | */ | 1601 | */ |
1647 | BUG(); | 1602 | BUG(); |
1648 | delete_and_null(oldLockStates); | 1603 | delete_and_null(oldLockStates); |
1649 | timer()->start(DocTimer::id_autoLockTimer); | 1604 | timer()->start(DocTimer::id_autoLockTimer); |
1650 | return false; | 1605 | return false; |
1651 | } | 1606 | } |
1652 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), | 1607 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), |
1653 | catEnd = dti.dta.end(), | 1608 | catEnd = dti.dta.end(), |
1654 | catI = catBegin; | 1609 | catI = catBegin; |
1655 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; | 1610 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; |
1656 | vector< vector<bool> >::iterator oldCatStatI = oldLockStates->begin(); | 1611 | vector< vector<bool> >::iterator oldCatStatI = oldLockStates->begin(); |
1657 | vector<bool>::iterator oldEntrStatBegin, | 1612 | vector<bool>::iterator oldEntrStatBegin, |
1658 | oldEntrStatEnd, | 1613 | oldEntrStatEnd, |
1659 | oldEntrStatI; | 1614 | oldEntrStatI; |
1660 | while (catI != catEnd) { | 1615 | while (catI != catEnd) { |
1661 | entrBegin = catI->d.begin(); | 1616 | entrBegin = catI->d.begin(); |
1662 | entrEnd = catI->d.end(); | 1617 | entrEnd = catI->d.end(); |
1663 | entrI = entrBegin; | 1618 | entrI = entrBegin; |
1664 | if (likely(!wasDeepLocked)) { | 1619 | if (likely(!wasDeepLocked)) { |
1665 | oldEntrStatBegin = oldCatStatI->begin(); | 1620 | oldEntrStatBegin = oldCatStatI->begin(); |
1666 | oldEntrStatEnd = oldCatStatI->end(); | 1621 | oldEntrStatEnd = oldCatStatI->end(); |
1667 | oldEntrStatI = oldEntrStatBegin; | 1622 | oldEntrStatI = oldEntrStatBegin; |
1668 | if (unlikely(catI->d.size() != oldCatStatI->size())) { | 1623 | if (unlikely(catI->d.size() != oldCatStatI->size())) { |
1669 | /* DOH! We have modified "dta" while | 1624 | /* DOH! We have modified "dta" while |
1670 | * it was unlocked tempoary. DON'T DO THIS! | 1625 | * it was unlocked tempoary. DON'T DO THIS! |
1671 | */ | 1626 | */ |
1672 | BUG(); | 1627 | BUG(); |
1673 | delete_and_null(oldLockStates); | 1628 | delete_and_null(oldLockStates); |
1674 | timer()->start(DocTimer::id_autoLockTimer); | 1629 | timer()->start(DocTimer::id_autoLockTimer); |
1675 | return false; | 1630 | return false; |
1676 | } | 1631 | } |
1677 | } | 1632 | } |
1678 | while (entrI != entrEnd) { | 1633 | while (entrI != entrEnd) { |
1679 | if (wasDeepLocked) { | 1634 | if (wasDeepLocked) { |
1680 | /* this is an error-fallback if | 1635 | /* this is an error-fallback if |
1681 | * deeplock didn't succeed | 1636 | * deeplock didn't succeed |
1682 | */ | 1637 | */ |
1683 | entrI->lockStat = true; | 1638 | entrI->lockStat = true; |
1684 | } else { | 1639 | } else { |
1685 | entrI->lockStat = *oldEntrStatI; | 1640 | entrI->lockStat = *oldEntrStatI; |
1686 | } | 1641 | } |
1687 | ++entrI; | 1642 | ++entrI; |
1688 | if (likely(!wasDeepLocked)) | 1643 | if (likely(!wasDeepLocked)) |
1689 | ++oldEntrStatI; | 1644 | ++oldEntrStatI; |
1690 | } | 1645 | } |
1691 | ++catI; | 1646 | ++catI; |
1692 | if (likely(!wasDeepLocked)) | 1647 | if (likely(!wasDeepLocked)) |
1693 | ++oldCatStatI; | 1648 | ++oldCatStatI; |
1694 | } | 1649 | } |
1695 | delete_and_null(oldLockStates); | 1650 | delete_and_null(oldLockStates); |
1696 | if (unlikely(wasDeepLocked)) { | 1651 | if (unlikely(wasDeepLocked)) { |
1697 | /* error fallback... */ | 1652 | /* error fallback... */ |
1698 | unsetDocStatFlag(DOC_STAT_DEEPLOCKED); | 1653 | unsetDocStatFlag(DOC_STAT_DEEPLOCKED); |
1699 | emitDataChanged(this); | 1654 | emitDataChanged(this); |
1700 | printDebug("WARNING: unlockAll_tempoary(true) " | 1655 | printDebug("WARNING: unlockAll_tempoary(true) " |
1701 | "deeplock fallback!"); | 1656 | "deeplock fallback!"); |
1702 | } | 1657 | } |
1703 | printDebug("tempoary unlocking of dta reverted."); | 1658 | printDebug("tempoary unlocking of dta reverted."); |
1704 | } else { | 1659 | } else { |
1705 | printDebug("unlockAll_tempoary(true): nothing to do."); | 1660 | printDebug("unlockAll_tempoary(true): nothing to do."); |
1706 | } | 1661 | } |
1707 | timer()->start(DocTimer::id_autoLockTimer); | 1662 | timer()->start(DocTimer::id_autoLockTimer); |
1708 | } else {// unlock all data tempoary | 1663 | } else {// unlock all data tempoary |
1709 | if (unlikely(oldLockStates != 0)) { | 1664 | if (unlikely(oldLockStates != 0)) { |
1710 | /* DOH! We have already unlocked the data tempoarly. | 1665 | /* DOH! We have already unlocked the data tempoarly. |
1711 | * No need to do it twice. ;) | 1666 | * No need to do it twice. ;) |
1712 | */ | 1667 | */ |
1713 | BUG(); | 1668 | BUG(); |
1714 | return false; | 1669 | return false; |
1715 | } | 1670 | } |
1716 | wasDeepLocked = false; | 1671 | wasDeepLocked = false; |
1717 | bool mustUnlock = false; | 1672 | bool mustUnlock = false; |
1718 | if (isDeepLocked()) { | 1673 | if (isDeepLocked()) { |
1719 | PwMerror ret; | 1674 | PwMerror ret; |
1720 | while (1) { | 1675 | while (1) { |
1721 | ret = deepLock(false); | 1676 | ret = deepLock(false); |
1722 | if (ret == e_success) { | 1677 | if (ret == e_success) { |
1723 | break; | 1678 | break; |
1724 | } else if (ret == e_wrongPw) { | 1679 | } else if (ret == e_wrongPw) { |
1725 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 1680 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
1726 | } else { | 1681 | } else { |
1727 | printDebug("deep-unlocking failed while " | 1682 | printDebug("deep-unlocking failed while " |
1728 | "tempoary unlocking!"); | 1683 | "tempoary unlocking!"); |
1729 | return false; | 1684 | return false; |
1730 | } | 1685 | } |
1731 | } | 1686 | } |
1732 | wasDeepLocked = true; | 1687 | wasDeepLocked = true; |
1733 | mustUnlock = true; | 1688 | mustUnlock = true; |
1734 | } else { | 1689 | } else { |
1735 | // first check if it's needed to unlock some entries | 1690 | // first check if it's needed to unlock some entries |
1736 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), | 1691 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), |
1737 | catEnd = dti.dta.end(), | 1692 | catEnd = dti.dta.end(), |
1738 | catI = catBegin; | 1693 | catI = catBegin; |
1739 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; | 1694 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; |
1740 | while (catI != catEnd) { | 1695 | while (catI != catEnd) { |
1741 | entrBegin = catI->d.begin(); | 1696 | entrBegin = catI->d.begin(); |
1742 | entrEnd = catI->d.end(); | 1697 | entrEnd = catI->d.end(); |
1743 | entrI = entrBegin; | 1698 | entrI = entrBegin; |
1744 | while (entrI != entrEnd) { | 1699 | while (entrI != entrEnd) { |
1745 | if (entrI->lockStat == true) { | 1700 | if (entrI->lockStat == true) { |
1746 | mustUnlock = true; | 1701 | mustUnlock = true; |
1747 | break; | 1702 | break; |
1748 | } | 1703 | } |
1749 | ++entrI; | 1704 | ++entrI; |
1750 | } | 1705 | } |
1751 | if (mustUnlock) | 1706 | if (mustUnlock) |
1752 | break; | 1707 | break; |
1753 | ++catI; | 1708 | ++catI; |
1754 | } | 1709 | } |
1755 | } | 1710 | } |
1756 | if (!mustUnlock) { | 1711 | if (!mustUnlock) { |
1757 | // nothing to do. | 1712 | // nothing to do. |
1758 | timer()->stop(DocTimer::id_autoLockTimer); | 1713 | timer()->stop(DocTimer::id_autoLockTimer); |
1759 | printDebug("unlockAll_tempoary(): nothing to do."); | 1714 | printDebug("unlockAll_tempoary(): nothing to do."); |
1760 | return true; | 1715 | return true; |
1761 | } else if (!wasDeepLocked) { | 1716 | } else if (!wasDeepLocked) { |
1762 | if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW) && | 1717 | if (!getDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW) && |
1763 | currentPw != "") { | 1718 | currentPw != "") { |
1764 | /* we can't unlock without mpw, so | 1719 | /* we can't unlock without mpw, so |
1765 | * we need to ask for it. | 1720 | * we need to ask for it. |
1766 | */ | 1721 | */ |
1767 | QString pw; | 1722 | QString pw; |
1768 | while (1) { | 1723 | while (1) { |
1769 | pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 1724 | pw = requestMpw(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
1770 | if (pw == "") { | 1725 | if (pw == "") { |
1771 | return false; | 1726 | return false; |
1772 | } else if (pw == currentPw) { | 1727 | } else if (pw == currentPw) { |
1773 | break; | 1728 | break; |
1774 | } | 1729 | } |
1775 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); | 1730 | wrongMpwMsgBox(getDocStatFlag(DOC_STAT_USE_CHIPCARD)); |
1776 | } | 1731 | } |
1777 | } | 1732 | } |
1778 | } | 1733 | } |
1779 | timer()->stop(DocTimer::id_autoLockTimer); | 1734 | timer()->stop(DocTimer::id_autoLockTimer); |
1780 | oldLockStates = new vector< vector<bool> >; | 1735 | oldLockStates = new vector< vector<bool> >; |
1781 | vector<bool> tmp_vec; | 1736 | vector<bool> tmp_vec; |
1782 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), | 1737 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), |
1783 | catEnd = dti.dta.end(), | 1738 | catEnd = dti.dta.end(), |
1784 | catI = catBegin; | 1739 | catI = catBegin; |
1785 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; | 1740 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; |
1786 | while (catI != catEnd) { | 1741 | while (catI != catEnd) { |
1787 | entrBegin = catI->d.begin(); | 1742 | entrBegin = catI->d.begin(); |
1788 | entrEnd = catI->d.end(); | 1743 | entrEnd = catI->d.end(); |
1789 | entrI = entrBegin; | 1744 | entrI = entrBegin; |
1790 | while (entrI != entrEnd) { | 1745 | while (entrI != entrEnd) { |
1791 | if (!wasDeepLocked) { | 1746 | if (!wasDeepLocked) { |
1792 | tmp_vec.push_back(entrI->lockStat); | 1747 | tmp_vec.push_back(entrI->lockStat); |
1793 | } | 1748 | } |
1794 | entrI->lockStat = false; | 1749 | entrI->lockStat = false; |
1795 | ++entrI; | 1750 | ++entrI; |
1796 | } | 1751 | } |
1797 | if (!wasDeepLocked) { | 1752 | if (!wasDeepLocked) { |
1798 | oldLockStates->push_back(tmp_vec); | 1753 | oldLockStates->push_back(tmp_vec); |
1799 | tmp_vec.clear(); | 1754 | tmp_vec.clear(); |
1800 | } | 1755 | } |
1801 | ++catI; | 1756 | ++catI; |
1802 | } | 1757 | } |
1803 | printDebug("tempoary unlocked dta."); | 1758 | printDebug("tempoary unlocked dta."); |
1804 | } | 1759 | } |
1805 | 1760 | ||
1806 | return true; | 1761 | return true; |
1807 | } | 1762 | } |
1808 | 1763 | ||
1809 | PwMerror PwMDoc::deepLock(bool lock, bool saveToFile) | 1764 | PwMerror PwMDoc::deepLock(bool lock, bool saveToFile) |
1810 | { | 1765 | { |
1811 | PwMerror ret; | 1766 | PwMerror ret; |
1812 | 1767 | ||
1813 | if (lock) { | 1768 | if (lock) { |
1814 | if (isDeepLocked()) | 1769 | if (isDeepLocked()) |
1815 | return e_lock; | 1770 | return e_lock; |
1816 | if (saveToFile) { | 1771 | if (saveToFile) { |
1817 | if (isDocEmpty()) | 1772 | if (isDocEmpty()) |
1818 | return e_docIsEmpty; | 1773 | return e_docIsEmpty; |
1819 | ret = saveDoc(conf()->confGlobCompression()); | 1774 | ret = saveDoc(conf()->confGlobCompression()); |
1820 | if (ret == e_filename) { | 1775 | if (ret == e_filename) { |
1821 | /* the doc wasn't saved to a file | 1776 | /* the doc wasn't saved to a file |
1822 | * by the user, yet. | 1777 | * by the user, yet. |
1823 | */ | 1778 | */ |
1824 | cantDeeplock_notSavedMsgBox(); | 1779 | cantDeeplock_notSavedMsgBox(); |
1825 | return e_docNotSaved; | 1780 | return e_docNotSaved; |
1826 | } else if (ret != e_success) { | 1781 | } else if (ret != e_success) { |
1827 | return e_lock; | 1782 | return e_lock; |
1828 | } | 1783 | } |
1829 | } | 1784 | } |
1830 | timer()->stop(DocTimer::id_autoLockTimer); | 1785 | timer()->stop(DocTimer::id_autoLockTimer); |
1831 | clearDoc(); | 1786 | clearDoc(); |
1832 | PwMDataItem d; | 1787 | PwMDataItem d; |
1833 | d.desc = IS_DEEPLOCKED_SHORTMSG.latin1(); | 1788 | d.desc = IS_DEEPLOCKED_SHORTMSG.latin1(); |
1834 | d.comment = IS_DEEPLOCKED_MSG.latin1(); | 1789 | d.comment = IS_DEEPLOCKED_MSG.latin1(); |
1835 | d.listViewPos = 0; | 1790 | d.listViewPos = 0; |
1836 | addEntry(DEFAULT_CATEGORY, &d, true); | 1791 | addEntry(DEFAULT_CATEGORY, &d, true); |
1837 | lockAt(DEFAULT_CATEGORY, 0, true); | 1792 | lockAt(DEFAULT_CATEGORY, 0, true); |
1838 | unsetDocStatFlag(DOC_STAT_DISK_DIRTY); | 1793 | unsetDocStatFlag(DOC_STAT_DISK_DIRTY); |
1839 | setDocStatFlag(DOC_STAT_DEEPLOCKED); | 1794 | setDocStatFlag(DOC_STAT_DEEPLOCKED); |
1840 | } else { | 1795 | } else { |
1841 | if (!isDeepLocked()) | 1796 | if (!isDeepLocked()) |
1842 | return e_lock; | 1797 | return e_lock; |
1843 | ret = openDoc(&filename, (conf()->confGlobUnlockOnOpen()) | 1798 | ret = openDoc(&filename, (conf()->confGlobUnlockOnOpen()) |
1844 | ? 0 : 1); | 1799 | ? 0 : 1); |
1845 | if (ret == e_wrongPw) { | 1800 | if (ret == e_wrongPw) { |
1846 | return e_wrongPw; | 1801 | return e_wrongPw; |
1847 | } else if (ret != e_success) { | 1802 | } else if (ret != e_success) { |
1848 | printDebug(string("PwMDoc::deepLock(false): ERR! openDoc() == ") | 1803 | printDebug(string("PwMDoc::deepLock(false): ERR! openDoc() == ") |
1849 | + tostr(static_cast<int>(ret))); | 1804 | + tostr(static_cast<int>(ret))); |
1850 | return e_lock; | 1805 | return e_lock; |
1851 | } | 1806 | } |
1852 | unsetDocStatFlag(DOC_STAT_DEEPLOCKED); | 1807 | unsetDocStatFlag(DOC_STAT_DEEPLOCKED); |
1853 | timer()->start(DocTimer::id_autoLockTimer); | 1808 | timer()->start(DocTimer::id_autoLockTimer); |
1854 | } | 1809 | } |
1855 | 1810 | ||
1856 | emitDataChanged(this); | 1811 | emitDataChanged(this); |
1857 | return e_success; | 1812 | return e_success; |
1858 | } | 1813 | } |
1859 | 1814 | ||
1860 | void PwMDoc::_deepUnlock() | 1815 | void PwMDoc::_deepUnlock() |
1861 | { | 1816 | { |
1862 | deepLock(false); | 1817 | deepLock(false); |
1863 | } | 1818 | } |
1864 | 1819 | ||
1865 | void PwMDoc::clearDoc() | 1820 | void PwMDoc::clearDoc() |
1866 | { | 1821 | { |
1867 | dti.clear(); | 1822 | dti.clear(); |
1868 | PwMCategoryItem d; | 1823 | PwMCategoryItem d; |
1869 | d.name = DEFAULT_CATEGORY.latin1(); | 1824 | d.name = DEFAULT_CATEGORY.latin1(); |
1870 | dti.dta.push_back(d); | 1825 | dti.dta.push_back(d); |
1871 | currentPw = ""; | 1826 | currentPw = ""; |
1872 | unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); | 1827 | unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW); |
1873 | } | 1828 | } |
1874 | 1829 | ||
1875 | void PwMDoc::changeCurrentPw() | 1830 | void PwMDoc::changeCurrentPw() |
1876 | { | 1831 | { |
1877 | if (currentPw == "") | 1832 | if (currentPw == "") |
1878 | return; // doc hasn't been saved. No mpw available. | 1833 | return; // doc hasn't been saved. No mpw available. |
1879 | bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); | 1834 | bool useChipcard = getDocStatFlag(DOC_STAT_USE_CHIPCARD); |
1880 | QString pw = requestMpwChange(¤tPw, &useChipcard); | 1835 | QString pw = requestMpwChange(¤tPw, &useChipcard); |
1881 | if (pw == "") | 1836 | if (pw == "") |
1882 | return; | 1837 | return; |
1883 | if (useChipcard) | 1838 | if (useChipcard) |
1884 | setDocStatFlag(DOC_STAT_USE_CHIPCARD); | 1839 | setDocStatFlag(DOC_STAT_USE_CHIPCARD); |
1885 | else | 1840 | else |
1886 | unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); | 1841 | unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); |
1887 | setCurrentPw(pw); | 1842 | setCurrentPw(pw); |
1888 | } | 1843 | } |
1889 | 1844 | ||
1890 | void PwMDoc::setListViewPos(const QString &category, unsigned int index, | 1845 | void PwMDoc::setListViewPos(const QString &category, unsigned int index, |
1891 | int pos) | 1846 | int pos) |
1892 | { | 1847 | { |
1893 | unsigned int cat = 0; | 1848 | unsigned int cat = 0; |
1894 | 1849 | ||
1895 | if (!findCategory(category, &cat)) { | 1850 | if (!findCategory(category, &cat)) { |
1896 | BUG(); | 1851 | BUG(); |
1897 | return; | 1852 | return; |
1898 | } | 1853 | } |
1899 | setListViewPos(cat, index, pos); | 1854 | setListViewPos(cat, index, pos); |
1900 | } | 1855 | } |
1901 | 1856 | ||
1902 | void PwMDoc::setListViewPos(unsigned int category, unsigned int index, | 1857 | void PwMDoc::setListViewPos(unsigned int category, unsigned int index, |
1903 | int pos) | 1858 | int pos) |
1904 | { | 1859 | { |
1905 | dti.dta[category].d[index].listViewPos = pos; | 1860 | dti.dta[category].d[index].listViewPos = pos; |
1906 | 1861 | ||
1907 | /* FIXME workaround: don't flag dirty, because this function sometimes | 1862 | /* FIXME workaround: don't flag dirty, because this function sometimes |
1908 | * get's called when it shouldn't. It's because PwMView assumes | 1863 | * get's called when it shouldn't. It's because PwMView assumes |
1909 | * the user resorted the UI on behalf of signal layoutChanged(). | 1864 | * the user resorted the UI on behalf of signal layoutChanged(). |
1910 | * This is somewhat broken and incorrect, but I've no other | 1865 | * This is somewhat broken and incorrect, but I've no other |
1911 | * solution for now. | 1866 | * solution for now. |
1912 | */ | 1867 | */ |
1913 | //setDocStatFlag(DOC_STAT_DISK_DIRTY); | 1868 | //setDocStatFlag(DOC_STAT_DISK_DIRTY); |
1914 | } | 1869 | } |
1915 | 1870 | ||
1916 | int PwMDoc::getListViewPos(const QString &category, unsigned int index) | 1871 | int PwMDoc::getListViewPos(const QString &category, unsigned int index) |
1917 | { | 1872 | { |
1918 | unsigned int cat = 0; | 1873 | unsigned int cat = 0; |
1919 | 1874 | ||
1920 | if (!findCategory(category, &cat)) { | 1875 | if (!findCategory(category, &cat)) { |
1921 | BUG(); | 1876 | BUG(); |
1922 | return -1; | 1877 | return -1; |
1923 | } | 1878 | } |
1924 | 1879 | ||
1925 | return dti.dta[cat].d[index].listViewPos; | 1880 | return dti.dta[cat].d[index].listViewPos; |
1926 | } | 1881 | } |
1927 | 1882 | ||
1928 | void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int searchIn, | 1883 | void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int searchIn, |
1929 | vector<unsigned int> *foundPositions, bool breakAfterFound, | 1884 | vector<unsigned int> *foundPositions, bool breakAfterFound, |
1930 | bool caseSensitive, bool exactWordMatch, bool sortByLvp) | 1885 | bool caseSensitive, bool exactWordMatch, bool sortByLvp) |
1931 | { | 1886 | { |
1932 | PWM_ASSERT(foundPositions); | 1887 | PWM_ASSERT(foundPositions); |
1933 | PWM_ASSERT(searchIn); | 1888 | PWM_ASSERT(searchIn); |
1934 | foundPositions->clear(); | 1889 | foundPositions->clear(); |
1935 | 1890 | ||
1936 | unsigned int i, entries = numEntries(category); | 1891 | unsigned int i, entries = numEntries(category); |
1937 | for (i = 0; i < entries; ++i) { | 1892 | for (i = 0; i < entries; ++i) { |
1938 | if (searchIn & SEARCH_IN_DESC) { | 1893 | if (searchIn & SEARCH_IN_DESC) { |
1939 | if (!compareString(find.desc, dti.dta[category].d[i].desc, | 1894 | if (!compareString(find.desc, dti.dta[category].d[i].desc, |
1940 | caseSensitive, exactWordMatch)) { | 1895 | caseSensitive, exactWordMatch)) { |
1941 | continue; | 1896 | continue; |
1942 | } | 1897 | } |
1943 | } | 1898 | } |
1944 | if (searchIn & SEARCH_IN_NAME) { | 1899 | if (searchIn & SEARCH_IN_NAME) { |
1945 | if (!compareString(find.name, dti.dta[category].d[i].name, | 1900 | if (!compareString(find.name, dti.dta[category].d[i].name, |
1946 | caseSensitive, exactWordMatch)) { | 1901 | caseSensitive, exactWordMatch)) { |
1947 | continue; | 1902 | continue; |
1948 | } | 1903 | } |
1949 | } | 1904 | } |
1950 | if (searchIn & SEARCH_IN_PW) { | 1905 | if (searchIn & SEARCH_IN_PW) { |
1951 | bool wasLocked = isLocked(category, i); | 1906 | bool wasLocked = isLocked(category, i); |
1952 | getDataChangedLock(); | 1907 | getDataChangedLock(); |
1953 | lockAt(category, i, false); | 1908 | lockAt(category, i, false); |
1954 | if (!compareString(find.pw, dti.dta[category].d[i].pw, | 1909 | if (!compareString(find.pw, dti.dta[category].d[i].pw, |
1955 | caseSensitive, exactWordMatch)) { | 1910 | caseSensitive, exactWordMatch)) { |
1956 | lockAt(category, i, wasLocked); | 1911 | lockAt(category, i, wasLocked); |
1957 | putDataChangedLock(); | 1912 | putDataChangedLock(); |
1958 | continue; | 1913 | continue; |
1959 | } | 1914 | } |
1960 | lockAt(category, i, wasLocked); | 1915 | lockAt(category, i, wasLocked); |
1961 | putDataChangedLock(); | 1916 | putDataChangedLock(); |
1962 | } | 1917 | } |
1963 | if (searchIn & SEARCH_IN_COMMENT) { | 1918 | if (searchIn & SEARCH_IN_COMMENT) { |
1964 | if (!compareString(find.comment, dti.dta[category].d[i].comment, | 1919 | if (!compareString(find.comment, dti.dta[category].d[i].comment, |
1965 | caseSensitive, exactWordMatch)) { | 1920 | caseSensitive, exactWordMatch)) { |
1966 | continue; | 1921 | continue; |
1967 | } | 1922 | } |
1968 | } | 1923 | } |
1969 | if (searchIn & SEARCH_IN_URL) { | 1924 | if (searchIn & SEARCH_IN_URL) { |
1970 | if (!compareString(find.url, dti.dta[category].d[i].url, | 1925 | if (!compareString(find.url, dti.dta[category].d[i].url, |
1971 | caseSensitive, exactWordMatch)) { | 1926 | caseSensitive, exactWordMatch)) { |
1972 | continue; | 1927 | continue; |
1973 | } | 1928 | } |
1974 | } | 1929 | } |
1975 | if (searchIn & SEARCH_IN_LAUNCHER) { | 1930 | if (searchIn & SEARCH_IN_LAUNCHER) { |
1976 | if (!compareString(find.launcher, dti.dta[category].d[i].launcher, | 1931 | if (!compareString(find.launcher, dti.dta[category].d[i].launcher, |
1977 | caseSensitive, exactWordMatch)) { | 1932 | caseSensitive, exactWordMatch)) { |
1978 | continue; | 1933 | continue; |
1979 | } | 1934 | } |
1980 | } | 1935 | } |
1981 | 1936 | ||
1982 | // all selected "searchIn" matched. | 1937 | // all selected "searchIn" matched. |
1983 | foundPositions->push_back(i); | 1938 | foundPositions->push_back(i); |
1984 | if (breakAfterFound) | 1939 | if (breakAfterFound) |
1985 | break; | 1940 | break; |
1986 | } | 1941 | } |
1987 | 1942 | ||
1988 | if (sortByLvp && foundPositions->size() > 1) { | 1943 | if (sortByLvp && foundPositions->size() > 1) { |
1989 | vector< pair<unsigned int /* foundPosition (real doc pos) */, | 1944 | vector< pair<unsigned int /* foundPosition (real doc pos) */, |
1990 | unsigned int /* lvp-pos */> > tmp_vec; | 1945 | unsigned int /* lvp-pos */> > tmp_vec; |
1991 | 1946 | ||
1992 | unsigned int i, items = foundPositions->size(); | 1947 | unsigned int i, items = foundPositions->size(); |
1993 | pair<unsigned int, unsigned int> tmp_pair; | 1948 | pair<unsigned int, unsigned int> tmp_pair; |
1994 | for (i = 0; i < items; ++i) { | 1949 | for (i = 0; i < items; ++i) { |
1995 | tmp_pair.first = (*foundPositions)[i]; | 1950 | tmp_pair.first = (*foundPositions)[i]; |
1996 | tmp_pair.second = dti.dta[category].d[(*foundPositions)[i]].listViewPos; | 1951 | tmp_pair.second = dti.dta[category].d[(*foundPositions)[i]].listViewPos; |
1997 | tmp_vec.push_back(tmp_pair); | 1952 | tmp_vec.push_back(tmp_pair); |
1998 | } | 1953 | } |
1999 | sort(tmp_vec.begin(), tmp_vec.end(), dta_lvp_greater()); | 1954 | sort(tmp_vec.begin(), tmp_vec.end(), dta_lvp_greater()); |
2000 | foundPositions->clear(); | 1955 | foundPositions->clear(); |
2001 | for (i = 0; i < items; ++i) { | 1956 | for (i = 0; i < items; ++i) { |
2002 | foundPositions->push_back(tmp_vec[i].first); | 1957 | foundPositions->push_back(tmp_vec[i].first); |
2003 | } | 1958 | } |
2004 | } | 1959 | } |
2005 | } | 1960 | } |
2006 | 1961 | ||
2007 | void PwMDoc::findEntry(const QString &category, PwMDataItem find, unsigned int searchIn, | 1962 | void PwMDoc::findEntry(const QString &category, PwMDataItem find, unsigned int searchIn, |
2008 | vector<unsigned int> *foundPositions, bool breakAfterFound, | 1963 | vector<unsigned int> *foundPositions, bool breakAfterFound, |
2009 | bool caseSensitive, bool exactWordMatch, bool sortByLvp) | 1964 | bool caseSensitive, bool exactWordMatch, bool sortByLvp) |
2010 | { | 1965 | { |
2011 | PWM_ASSERT(foundPositions); | 1966 | PWM_ASSERT(foundPositions); |
2012 | unsigned int cat = 0; | 1967 | unsigned int cat = 0; |
2013 | 1968 | ||
2014 | if (!findCategory(category, &cat)) { | 1969 | if (!findCategory(category, &cat)) { |
2015 | foundPositions->clear(); | 1970 | foundPositions->clear(); |
2016 | return; | 1971 | return; |
2017 | } | 1972 | } |
2018 | 1973 | ||
2019 | findEntry(cat, find, searchIn, foundPositions, breakAfterFound, | 1974 | findEntry(cat, find, searchIn, foundPositions, breakAfterFound, |
2020 | caseSensitive, exactWordMatch, sortByLvp); | 1975 | caseSensitive, exactWordMatch, sortByLvp); |
2021 | } | 1976 | } |
2022 | 1977 | ||
2023 | bool PwMDoc::compareString(const string &s1, const string &s2, bool caseSensitive, | 1978 | bool PwMDoc::compareString(const string &s1, const string &s2, bool caseSensitive, |
2024 | bool exactWordMatch) | 1979 | bool exactWordMatch) |
2025 | { | 1980 | { |
2026 | QString _s1(s1.c_str()); | 1981 | QString _s1(s1.c_str()); |
2027 | QString _s2(s2.c_str()); | 1982 | QString _s2(s2.c_str()); |
2028 | if (!caseSensitive) { | 1983 | if (!caseSensitive) { |
2029 | _s1 = _s1.lower(); | 1984 | _s1 = _s1.lower(); |
2030 | _s2 = _s2.lower(); | 1985 | _s2 = _s2.lower(); |
2031 | } | 1986 | } |
2032 | if (exactWordMatch ? (_s1 == _s2) : (_s2.find(_s1) != -1)) | 1987 | if (exactWordMatch ? (_s1 == _s2) : (_s2.find(_s1) != -1)) |
2033 | return true; | 1988 | return true; |
2034 | return false; | 1989 | return false; |
2035 | } | 1990 | } |
2036 | 1991 | ||
2037 | bool PwMDoc::findCategory(const QString &name, unsigned int *index) | 1992 | bool PwMDoc::findCategory(const QString &name, unsigned int *index) |
2038 | { | 1993 | { |
2039 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), | 1994 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), |
2040 | end = dti.dta.end(); | 1995 | end = dti.dta.end(); |
2041 | while (i != end) { | 1996 | while (i != end) { |
2042 | if ((*i).name == name.latin1()) { | 1997 | if ((*i).name == name.latin1()) { |
2043 | if (index) { | 1998 | if (index) { |
2044 | *index = i - dti.dta.begin(); | 1999 | *index = i - dti.dta.begin(); |
2045 | } | 2000 | } |
2046 | return true; | 2001 | return true; |
2047 | } | 2002 | } |
2048 | ++i; | 2003 | ++i; |
2049 | } | 2004 | } |
2050 | return false; | 2005 | return false; |
2051 | } | 2006 | } |
2052 | 2007 | ||
2053 | bool PwMDoc::renameCategory(const QString &category, const QString &newName) | 2008 | bool PwMDoc::renameCategory(const QString &category, const QString &newName) |
2054 | { | 2009 | { |
2055 | unsigned int cat = 0; | 2010 | unsigned int cat = 0; |
2056 | 2011 | ||
2057 | if (!findCategory(category, &cat)) | 2012 | if (!findCategory(category, &cat)) |
2058 | return false; | 2013 | return false; |
2059 | 2014 | ||
2060 | return renameCategory(cat, newName); | 2015 | return renameCategory(cat, newName); |
2061 | } | 2016 | } |
2062 | 2017 | ||
2063 | bool PwMDoc::renameCategory(unsigned int category, const QString &newName, | 2018 | bool PwMDoc::renameCategory(unsigned int category, const QString &newName, |
2064 | bool dontFlagDirty) | 2019 | bool dontFlagDirty) |
2065 | { | 2020 | { |
2066 | if (category > numCategories() - 1) | 2021 | if (category > numCategories() - 1) |
2067 | return false; | 2022 | return false; |
2068 | 2023 | ||
2069 | dti.dta[category].name = newName.latin1(); | 2024 | dti.dta[category].name = newName.latin1(); |
2070 | if (!dontFlagDirty) | 2025 | if (!dontFlagDirty) |
2071 | flagDirty(); | 2026 | flagDirty(); |
2072 | 2027 | ||
2073 | return true; | 2028 | return true; |
2074 | } | 2029 | } |
2075 | 2030 | ||
2076 | bool PwMDoc::delCategory(const QString &category) | 2031 | bool PwMDoc::delCategory(const QString &category) |
2077 | { | 2032 | { |
2078 | unsigned int cat = 0; | 2033 | unsigned int cat = 0; |
2079 | 2034 | ||
2080 | if (!findCategory(category, &cat)) | 2035 | if (!findCategory(category, &cat)) |
2081 | return false; | 2036 | return false; |
2082 | 2037 | ||
2083 | return delCategory(cat); | 2038 | return delCategory(cat); |
2084 | } | 2039 | } |
2085 | 2040 | ||
2086 | bool PwMDoc::delCategory(unsigned int category, bool dontFlagDirty) | 2041 | bool PwMDoc::delCategory(unsigned int category, bool dontFlagDirty) |
2087 | { | 2042 | { |
2088 | if (category > numCategories() - 1) | 2043 | if (category > numCategories() - 1) |
2089 | return false; | 2044 | return false; |
2090 | 2045 | ||
2091 | // We don't delete it, if it is the last existing | 2046 | // We don't delete it, if it is the last existing |
2092 | // category! Instead we rename it to "Default". | 2047 | // category! Instead we rename it to "Default". |
2093 | if (numCategories() > 1) { | 2048 | if (numCategories() > 1) { |
2094 | dti.dta.erase(dti.dta.begin() + category); | 2049 | dti.dta.erase(dti.dta.begin() + category); |
2095 | } else { | 2050 | } else { |
2096 | renameCategory(category, DEFAULT_CATEGORY, dontFlagDirty); | 2051 | renameCategory(category, DEFAULT_CATEGORY, dontFlagDirty); |
2097 | return true; | 2052 | return true; |
2098 | } | 2053 | } |
2099 | if (!dontFlagDirty) | 2054 | if (!dontFlagDirty) |
2100 | flagDirty(); | 2055 | flagDirty(); |
2101 | 2056 | ||
2102 | return true; | 2057 | return true; |
2103 | } | 2058 | } |
2104 | 2059 | ||
2105 | void PwMDoc::delAllEmptyCat(bool dontFlagDirty) | 2060 | void PwMDoc::delAllEmptyCat(bool dontFlagDirty) |
2106 | { | 2061 | { |
2107 | vector<PwMCategoryItem>::iterator begin = dti.dta.begin(), | 2062 | vector<PwMCategoryItem>::iterator begin = dti.dta.begin(), |
2108 | end = dti.dta.end(), | 2063 | end = dti.dta.end(), |
2109 | i = begin; | 2064 | i = begin; |
2110 | while (i != end) { | 2065 | while (i != end) { |
2111 | if (i->d.empty()) { | 2066 | if (i->d.empty()) { |
2112 | delCategory(begin - i, dontFlagDirty); | 2067 | delCategory(begin - i, dontFlagDirty); |
2113 | } | 2068 | } |
2114 | ++i; | 2069 | ++i; |
2115 | } | 2070 | } |
2116 | } | 2071 | } |
2117 | 2072 | ||
2118 | void PwMDoc::getCategoryList(vector<string> *list) | 2073 | void PwMDoc::getCategoryList(vector<string> *list) |
2119 | { | 2074 | { |
2120 | PWM_ASSERT(list); | 2075 | PWM_ASSERT(list); |
2121 | list->clear(); | 2076 | list->clear(); |
2122 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), | 2077 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), |
2123 | end = dti.dta.end(); | 2078 | end = dti.dta.end(); |
2124 | while (i != end) { | 2079 | while (i != end) { |
2125 | list->push_back(i->name); | 2080 | list->push_back(i->name); |
2126 | ++i; | 2081 | ++i; |
2127 | } | 2082 | } |
2128 | } | 2083 | } |
2129 | 2084 | ||
2130 | void PwMDoc::getCategoryList(QStringList *list) | 2085 | void PwMDoc::getCategoryList(QStringList *list) |
2131 | { | 2086 | { |
2132 | PWM_ASSERT(list); | 2087 | PWM_ASSERT(list); |
2133 | list->clear(); | 2088 | list->clear(); |
2134 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), | 2089 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), |
2135 | end = dti.dta.end(); | 2090 | end = dti.dta.end(); |
2136 | while (i != end) { | 2091 | while (i != end) { |
2137 | #ifndef PWM_EMBEDDED | 2092 | #ifndef PWM_EMBEDDED |
2138 | list->push_back(i->name.c_str()); | 2093 | list->push_back(i->name.c_str()); |
2139 | #else | 2094 | #else |
2140 | list->append(i->name.c_str()); | 2095 | list->append(i->name.c_str()); |
2141 | #endif | 2096 | #endif |
2142 | ++i; | 2097 | ++i; |
2143 | } | 2098 | } |
2144 | } | 2099 | } |
2145 | 2100 | ||
2146 | void PwMDoc::getEntryList(const QString &category, QStringList *list) | 2101 | void PwMDoc::getEntryList(const QString &category, QStringList *list) |
2147 | { | 2102 | { |
2148 | PWM_ASSERT(list); | 2103 | PWM_ASSERT(list); |
2149 | unsigned int cat = 0; | 2104 | unsigned int cat = 0; |
2150 | if (!findCategory(category, &cat)) { | 2105 | if (!findCategory(category, &cat)) { |
2151 | list->clear(); | 2106 | list->clear(); |
2152 | return; | 2107 | return; |
2153 | } | 2108 | } |
2154 | getEntryList(cat, list); | 2109 | getEntryList(cat, list); |
2155 | } | 2110 | } |
2156 | 2111 | ||
2157 | void PwMDoc::getEntryList(const QString &category, vector<string> *list) | 2112 | void PwMDoc::getEntryList(const QString &category, vector<string> *list) |
2158 | { | 2113 | { |
2159 | PWM_ASSERT(list); | 2114 | PWM_ASSERT(list); |
2160 | unsigned int cat = 0; | 2115 | unsigned int cat = 0; |
2161 | if (!findCategory(category, &cat)) { | 2116 | if (!findCategory(category, &cat)) { |
2162 | list->clear(); | 2117 | list->clear(); |
2163 | return; | 2118 | return; |
2164 | } | 2119 | } |
2165 | getEntryList(cat, list); | 2120 | getEntryList(cat, list); |
2166 | } | 2121 | } |
2167 | 2122 | ||
2168 | void PwMDoc::getEntryList(unsigned int category, vector<string> *list) | 2123 | void PwMDoc::getEntryList(unsigned int category, vector<string> *list) |
2169 | { | 2124 | { |
2170 | PWM_ASSERT(list); | 2125 | PWM_ASSERT(list); |
2171 | list->clear(); | 2126 | list->clear(); |
2172 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), | 2127 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), |
2173 | end = dti.dta[category].d.end(), | 2128 | end = dti.dta[category].d.end(), |
2174 | i = begin; | 2129 | i = begin; |
2175 | while (i != end) { | 2130 | while (i != end) { |
2176 | list->push_back(i->desc); | 2131 | list->push_back(i->desc); |
2177 | ++i; | 2132 | ++i; |
2178 | } | 2133 | } |
2179 | } | 2134 | } |
2180 | 2135 | ||
2181 | void PwMDoc::getEntryList(unsigned int category, QStringList *list) | 2136 | void PwMDoc::getEntryList(unsigned int category, QStringList *list) |
2182 | { | 2137 | { |
2183 | PWM_ASSERT(list); | 2138 | PWM_ASSERT(list); |
2184 | list->clear(); | 2139 | list->clear(); |
2185 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), | 2140 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), |
2186 | end = dti.dta[category].d.end(), | 2141 | end = dti.dta[category].d.end(), |
2187 | i = begin; | 2142 | i = begin; |
2188 | while (i != end) { | 2143 | while (i != end) { |
2189 | #ifndef PWM_EMBEDDED | 2144 | #ifndef PWM_EMBEDDED |
2190 | list->push_back(i->desc.c_str()); | 2145 | list->push_back(i->desc.c_str()); |
2191 | #else | 2146 | #else |
2192 | list->append(i->desc.c_str()); | 2147 | list->append(i->desc.c_str()); |
2193 | #endif | 2148 | #endif |
2194 | ++i; | 2149 | ++i; |
2195 | } | 2150 | } |
2196 | } | 2151 | } |
2197 | 2152 | ||
2198 | bool PwMDoc::execLauncher(const QString &category, unsigned int entryIndex) | 2153 | bool PwMDoc::execLauncher(const QString &category, unsigned int entryIndex) |
2199 | { | 2154 | { |
2200 | unsigned int cat = 0; | 2155 | unsigned int cat = 0; |
2201 | 2156 | ||
2202 | if (!findCategory(category, &cat)) | 2157 | if (!findCategory(category, &cat)) |
2203 | return false; | 2158 | return false; |
2204 | 2159 | ||
2205 | return execLauncher(cat, entryIndex); | 2160 | return execLauncher(cat, entryIndex); |
2206 | } | 2161 | } |
2207 | 2162 | ||
2208 | bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex) | 2163 | bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex) |
2209 | { | 2164 | { |
2210 | if (geteuid() == 0) { | 2165 | if (geteuid() == 0) { |
2211 | rootAlertMsgBox(); | 2166 | rootAlertMsgBox(); |
2212 | return false; | 2167 | return false; |
2213 | } | 2168 | } |
2214 | QString command(dti.dta[category].d[entryIndex].launcher.c_str()); | 2169 | QString command(dti.dta[category].d[entryIndex].launcher.c_str()); |
2215 | bool wasLocked = isLocked(category, entryIndex); | 2170 | bool wasLocked = isLocked(category, entryIndex); |
2216 | 2171 | ||
2217 | if (command.find("$p") != -1) { | 2172 | if (command.find("$p") != -1) { |
2218 | /* the user requested the password to be included | 2173 | /* the user requested the password to be included |
2219 | * into the command. We have to ask for the password, | 2174 | * into the command. We have to ask for the password, |
2220 | * if it's locked. We do that by unlocking the entry | 2175 | * if it's locked. We do that by unlocking the entry |
2221 | */ | 2176 | */ |
2222 | if (!lockAt(category, entryIndex, false)) | 2177 | if (!lockAt(category, entryIndex, false)) |
2223 | return false; | 2178 | return false; |
2224 | } | 2179 | } |
2225 | #ifndef PWM_EMBEDDED | 2180 | #ifndef PWM_EMBEDDED |
2226 | command.replace("$d", dti.dta[category].d[entryIndex].desc.c_str()); | 2181 | command.replace("$d", dti.dta[category].d[entryIndex].desc.c_str()); |
2227 | command.replace("$n", dti.dta[category].d[entryIndex].name.c_str()); | 2182 | command.replace("$n", dti.dta[category].d[entryIndex].name.c_str()); |
2228 | command.replace("$p", dti.dta[category].d[entryIndex].pw.c_str()); | 2183 | command.replace("$p", dti.dta[category].d[entryIndex].pw.c_str()); |
2229 | command.replace("$u", dti.dta[category].d[entryIndex].url.c_str()); | 2184 | command.replace("$u", dti.dta[category].d[entryIndex].url.c_str()); |
2230 | command.replace("$c", dti.dta[category].d[entryIndex].comment.c_str()); | 2185 | command.replace("$c", dti.dta[category].d[entryIndex].comment.c_str()); |
2231 | #else | 2186 | #else |
2232 | command.replace(QRegExp("$d"), dti.dta[category].d[entryIndex].desc.c_str()); | 2187 | command.replace(QRegExp("$d"), dti.dta[category].d[entryIndex].desc.c_str()); |
2233 | command.replace(QRegExp("$n"), dti.dta[category].d[entryIndex].name.c_str()); | 2188 | command.replace(QRegExp("$n"), dti.dta[category].d[entryIndex].name.c_str()); |
2234 | command.replace(QRegExp("$p"), dti.dta[category].d[entryIndex].pw.c_str()); | 2189 | command.replace(QRegExp("$p"), dti.dta[category].d[entryIndex].pw.c_str()); |
2235 | command.replace(QRegExp("$u"), dti.dta[category].d[entryIndex].url.c_str()); | 2190 | command.replace(QRegExp("$u"), dti.dta[category].d[entryIndex].url.c_str()); |
2236 | command.replace(QRegExp("$c"), dti.dta[category].d[entryIndex].comment.c_str()); | 2191 | command.replace(QRegExp("$c"), dti.dta[category].d[entryIndex].comment.c_str()); |
2237 | #endif | 2192 | #endif |
2238 | command.append(" &"); | 2193 | command.append(" &"); |
2239 | 2194 | ||
2240 | QString customXterm(conf()->confGlobXtermCommand()); | 2195 | QString customXterm(conf()->confGlobXtermCommand()); |
2241 | if (!customXterm.isEmpty()) | 2196 | if (!customXterm.isEmpty()) |
2242 | command = customXterm + " " + command; | 2197 | command = customXterm + " " + command; |
2243 | 2198 | ||
2244 | system(command.latin1()); | 2199 | system(command.latin1()); |
2245 | 2200 | ||
2246 | lockAt(category, entryIndex, wasLocked); | 2201 | lockAt(category, entryIndex, wasLocked); |
2247 | return true; | 2202 | return true; |
2248 | } | 2203 | } |
2249 | 2204 | ||
2250 | bool PwMDoc::goToURL(const QString &category, unsigned int entryIndex) | 2205 | bool PwMDoc::goToURL(const QString &category, unsigned int entryIndex) |
2251 | { | 2206 | { |