/
Processor.cs
405 lines (371 loc) · 14.8 KB
/
Processor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Text;
using DESKTOPAPILib;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using System.Threading;
namespace KeePassBB2AddIn
{
[Guid(Register.GUID), ProgId("KeePassBB2AddIn.Processor"), ComVisible(true)]
public class Processor : IRimExtension
{
public const int RECORD_DATABASE = 100;
public const int RECORD_MESSAGE = 120;
public const int RECORD_CREDENTIALS = 130;
public const int FIELD_RECORD_TYPE = 0;
public const int FIELD_TYPE_DATA = 101;
public const int FIELD_TYPE_CHECKSUM = 102;
public const int FIELD_RC = 121;
public const int FIELD_MSG = 122;
public const int FIELD_PASSWORD = 131;
public const int FIELD_KEYFILE = 132;
public static String password;
public static String keyfile;
private const int RC_NO_CHANGES_MADE = 1;
private const int RC_CHANGES_MADE = 2;
private const int RC_RECONCILE_EXCEPTION = 3;
private const int RC_DESKTOP_DECRYPT_FAILURE = 4;
private const int RC_DEVICE_DECRYPT_FAILURE = 5;
private const int RC_CONVERT_OUT_ERROR = 6;
private const int RC_CONVERT_IN_ERROR = 7;
private const int RC_RESTORE = 8;
private const int RC_BACKUP = 9;
private const int RC_LOADED_TO_DEVICE = 10;
private const int RC_DEVICE_FILE_OPEN_EXCEPTION = 11;
private const int RC_DEVICE_FILE_SERIALIZE_EXCEPTION = 12;
private const int RC_NOT_SUPPORTED = 13;
ProgressForm progressForm = null;
IRimLogger logger;
int progressCount = 0;
public void Process(IRimUtility pRimUtility, IRimDatabaseAccess pRimDeviceAccess)
{
logger = (IRimLogger)pRimUtility;
logger.LogLevel = eRIM_LogLevel.RIM_Logger_Verbose;
logger.LogDebug(eRIM_LogLevel.RIM_Logger_Verbose, 100, "KeePassBB2 Sync Starting");
progressForm = new ProgressForm();
progressForm.Show();
IRimTable dboTable = null;
IRimRecords records = null;
try
{
dboTable = getTable(pRimDeviceAccess);
records = dboTable.LoadRecords(eRIM_Mode.RIM_Mode_Write);
}
catch (Exception e)
{
logger.LogStatus("Couldn't retrieve the sync object. KeePass for BlackBerry v2 either isn't loaded on the device or is configured for External File Mode. " + e.ToString());
MessageBox.Show("KeePass for BlackBerry v2 either isn't loaded on the device or is configured for External File Mode.");
closeProgress();
return;
}
String dbFilename = getDBPath();
if (dbFilename == null)
{
Configure(null, 0);
dbFilename = getDBPath();
if (dbFilename == null || dbFilename.Length == 0)
{
logger.LogStatus("No database path was specified.");
MessageBox.Show("You must specify a database file.");
closeProgress();
return;
}
}
logger.LogStatus("Using " + dbFilename);
FileInfo fi = new FileInfo(dbFilename);
if (!fi.Exists)
{
logger.LogStatus("The database file does not exist.");
MessageBox.Show("The database file specified in the configuration does not exist.");
closeProgress();
return;
}
if (!checkPassword())
{
logger.LogStatus("A password/keyfile was not specified.");
MessageBox.Show("You must specify a password and/or keyfile.");
closeProgress();
return;
}
try
{
sendCredentials(records);
}
catch (Exception e)
{
logger.LogStatus("There was an exception transmitting the credentials. " + e.ToString());
MessageBox.Show("There was an exception transmitting the credentials. " + e.ToString());
closeProgress();
return;
}
try
{
sendFile(records, dbFilename);
}
catch (Exception e)
{
logger.LogStatus("There was an exception sending the file. " + e.ToString());
MessageBox.Show("There was an exception sending the file. " + e.ToString());
closeProgress();
return;
}
try
{
logger.LogStatus("Receiving data from device");
setProgress("Receiving data from device");
records = dboTable.LoadRecords(eRIM_Mode.RIM_Mode_Read);
}
catch (Exception e)
{
logger.LogStatus("There was an exception receiving the file. " + e.ToString());
MessageBox.Show("There was an exception receiving the file. " + e.ToString());
closeProgress();
return;
}
// if (GetValue(dbFilename, records, out displayMsg)) return;
MessageBox.Show("testing sync api", "DevTrac Sync Add-In");
logger.LogStatus("Completed.");
// if (displayMsg != null)
// {
// MessageBox.Show(displayMsg, "KeePassBB2 Sync Add-In");
// }
setProgress("Completed");
closeProgress();
}
private bool GetValue(string dbFilename, IRimRecords records)
{
StatusMessage sm;
String displayMsg = null;
try
{
sm = getStatusMessage(records);
logger.LogStatus("Return Status: " + sm.rc + ": " + sm.msg);
switch (sm.rc)
{
case RC_DESKTOP_DECRYPT_FAILURE:
case RC_DEVICE_DECRYPT_FAILURE:
displayMsg = sm.msg;
password = null;
keyfile = null;
break;
case RC_NOT_SUPPORTED:
case RC_LOADED_TO_DEVICE:
displayMsg = sm.msg;
break;
case RC_NO_CHANGES_MADE:
//displayMsg = "No changes detected";
break;
case RC_CHANGES_MADE:
//displayMsg = "Completed";
saveFile(records, dbFilename);
break;
default:
displayMsg = "There was an error on the device: " + sm.msg;
break;
}
}
catch (Exception e)
{
logger.LogStatus("There was an exception receiving the status string. " + e.ToString());
MessageBox.Show("There was an exception receiving the status string. " + e.ToString());
return true;
}
return false;
}
public void setProgress(String msg)
{
if (progressForm != null)
{
progressForm.setProgressMsg(msg);
Thread.Sleep(500);
}
}
public void closeProgress()
{
progressForm.progressBar1.PerformStep();
Thread.Sleep(1000);
progressForm.Hide();
progressForm.Dispose();
progressForm = null;
}
public StatusMessage getStatusMessage(IRimRecords records)
{
setProgress("Receiving status message");
IRimRecord r = records.FindRecord(RECORD_MESSAGE);
IRimField f = r.fields.FindField(FIELD_RC);
StatusMessage sm = new StatusMessage();
sm.rc = BitConverter.ToInt32((byte[])f.value, 0);
f = r.fields.FindField(FIELD_MSG);
sm.msg = System.Text.Encoding.ASCII.GetString((byte[])f.value);
return sm;
}
public IRimTable getTable(IRimDatabaseAccess pRimDeviceAccess)
{
logger.LogStatus("Retrieving sync object.");
setProgress("Loading Database Object");
IRimTables tables = pRimDeviceAccess.Tables;
IEnumerator eee = tables.GetEnumerator();
while (eee.MoveNext())
{
IRimTable tempTable = (IRimTable)eee.Current;
if (tempTable.Name == "KeePassBB2DatabaseObject")
{
return tempTable;
}
}
throw new NotSupportedException("There was no KeePassBB table on the device");
}
public void sendCredentials(IRimRecords records)
{
logger.LogStatus("Sending credentials.");
setProgress("Sending credentials");
IRimRecord r = records.AddRecord();
r.RecordID = RECORD_CREDENTIALS;
r.Version = 4;
IRimField f = r.fields.AddField();
f.Id = FIELD_RECORD_TYPE;
f.value = RECORD_CREDENTIALS;
if (password != null && password.Length > 0)
{
f = r.fields.AddField();
f.Id = FIELD_PASSWORD;
byte[] pba = new byte[password.Length];
for (int i = 0; i < pba.Length; i++) pba[i] = (byte)password[i];
f.value = pba;
}
if (keyfile != null && keyfile.Length > 0)
{
FileInfo fi = new FileInfo(keyfile);
FileStream fs = File.Open(keyfile, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int l = (int)fi.Length;
while (l > 0)
{
byte[] buffer = br.ReadBytes(Math.Min(l, 8192));
f = r.fields.AddField();
f.Id = FIELD_KEYFILE;
f.value = buffer;
l -= buffer.Length;
}
br.Close();
fs.Close();
}
r.Update();
}
public void sendFile(IRimRecords records, String filename)
{
logger.LogStatus("Sending file " + filename + " to device");
setProgress("Sending database");
IRimRecord r = records.AddRecord();
r.RecordID = RECORD_DATABASE;
r.Version = 4;
IRimField f = r.fields.AddField();
f.Id = FIELD_RECORD_TYPE;
f.value = RECORD_DATABASE;
FileInfo fi = new FileInfo(filename);
FileStream fs = File.Open(filename, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int l = (int)fi.Length;
while (l > 0)
{
byte[] buffer = br.ReadBytes(Math.Min(l, 8192));
f = r.fields.AddField();
f.Id = FIELD_TYPE_DATA;
f.value = buffer;
l -= buffer.Length;
/*
SHA256 md = new SHA256Managed();
byte[] hash = md.ComputeHash(buffer);
f = r.fields.AddField();
f.Id = FIELD_TYPE_CHECKSUM;
f.value = hash;
* */
}
br.Close();
fs.Close();
r.Update();
}
public void saveFile(IRimRecords records, String dbFilename)
{
logger.LogStatus("Saving database to " + dbFilename);
setProgress("Saving database to " + dbFilename);
IRimRecord r = records.FindRecord(RECORD_DATABASE);
int fc = r.fields.Count;
if (fc < 2) return;
IRimFields fds = r.fields.FindFields(FIELD_TYPE_DATA);
IEnumerator enu = fds.GetEnumerator();
FileStream fs = File.Open(dbFilename + ".tmp", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
while (enu.MoveNext())
{
IRimField f = (IRimField)enu.Current;
bw.Write((byte[])f.value);
}
bw.Close();
fs.Close();
File.Replace(dbFilename + ".tmp", dbFilename, dbFilename + ".bak");
}
public void Configure(IRimUtility pRimUtility, int hWnd)
{
ConfigForm cf = new ConfigForm();
cf.ShowDialog();
}
public String getDBPath()
{
logger.LogStatus("Retrieving database path.");
RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\" + Register.CompanyString + @"\" + Register.ProductString, RegistryKeyPermissionCheck.ReadWriteSubTree);
if (rk == null) return null;
String fn = (String)rk.GetValue(@"SyncFile", @"c:\");
rk.Close();
logger.LogStatus("Retrieved "+fn);
return fn;
}
public bool getShowSummary()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\" + Register.CompanyString + @"\" + Register.ProductString, RegistryKeyPermissionCheck.ReadWriteSubTree);
if (rk == null) return false;
bool c = ((int)rk.GetValue(@"SyncSummary", 0)) == 1;
rk.Close();
return c;
}
public void GetErrorString(int errorCode, ref string errorMsg)
{
}
public void GetExtensionInfo(ref string extensionInfo)
{
extensionInfo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<extensioninfo version=\"" + Register.VersionString + "\">" +
"<vendorname>" + Register.CompanyString + "</vendorname>" +
"<vendorversion>" + Register.VersionString + "</vendorversion>" +
"<path>KeePassBBAddIn.dll</path>" +
"<description>" + Register.DescriptionString + "</description>" +
"<displayname>KeePassBB2 AddIn</displayname>" +
"<clsid>{" + Register.GUID + "}</clsid>" +
"<access><database>KeePassBB2DatabaseObject</database></access>" +
"<hasconfiguration>true</hasconfiguration></extensioninfo>";
}
bool checkPassword()
{
setProgress("Checking for saved credentials");
if (password != null || keyfile != null) return true;
PasswordForm pf = new PasswordForm();
if (pf.ShowDialog() == DialogResult.OK)
{
password = pf.password;
keyfile = pf.keyfile;
return true;
}
return false;
}
}
public class StatusMessage
{
public int rc;
public String msg;
}
}