forked from juddmon/jpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.c
366 lines (313 loc) · 10.2 KB
/
password.c
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
/*******************************************************************************
* password.c
* A module of J-Pilot http://jpilot.org
*
* Copyright (C) 1999-2014 by Judd Montgomery
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
******************************************************************************/
/********************************* Includes ***********************************/
#include "config.h"
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <pi-version.h>
#include <pi-md5.h>
#include "password.h"
#include "i18n.h"
#include "prefs.h"
#ifdef ENABLE_PRIVATE
/********************************* Constants **********************************/
#define DIALOG_SAID_1 454
#define DIALOG_SAID_2 455
/******************************* Global vars **********************************/
static unsigned char short_salt[]=
{
0x09, 0x02, 0x13, 0x45, 0x07, 0x04, 0x13, 0x44,
0x0C, 0x08, 0x13, 0x5A, 0x32, 0x15, 0x13, 0x5D,
0xD2, 0x17, 0xEA, 0xD3, 0xB5, 0xDF, 0x55, 0x63,
0x22, 0xE9, 0xA1, 0x4A, 0x99, 0x4B, 0x0F, 0x88
};
static unsigned char long_salt[]=
{
0xB1, 0x56, 0x35, 0x1A, 0x9C, 0x98, 0x80, 0x84,
0x37, 0xA7, 0x3D, 0x61, 0x7F, 0x2E, 0xE8, 0x76,
0x2A, 0xF2, 0xA5, 0x84, 0x07, 0xC7, 0xEC, 0x27,
0x6F, 0x7D, 0x04, 0xCD, 0x52, 0x1E, 0xCD, 0x5B,
0xB3, 0x29, 0x76, 0x66, 0xD9, 0x5E, 0x4B, 0xCA,
0x63, 0x72, 0x6F, 0xD2, 0xFD, 0x25, 0xE6, 0x7B,
0xC5, 0x66, 0xB3, 0xD3, 0x45, 0x9A, 0xAF, 0xDA,
0x29, 0x86, 0x22, 0x6E, 0xB8, 0x03, 0x62, 0xBC
};
/****************************** Prototypes ************************************/
struct dialog_data {
GtkWidget *entry;
int button_hit;
char text[PASSWD_LEN+2];
};
/****************************** Main Code *************************************/
/* len is the length of the bin str, hex_str must be at least twice as long */
void bin_to_hex_str(unsigned char *bin, char *hex_str, int len)
{
int i;
hex_str[0]='\0';
for (i=0; i<len; i++) {
sprintf(&(hex_str[i*2]), "%02x", bin[i]);
}
}
/*
* encoded is a pre-allocated buffer at least 34 bytes long
*
* This is the hashing algorithm used in palm OS < 4.0.
* It is not very secure and is reversible.
*/
static void palm_encode_hash(unsigned char *ascii, unsigned char *encoded)
{
unsigned char index, shift;
unsigned short temp;
int si, ai, ei; /* salt i, ascii i, encoded i */
int end;
int len;
int m_array[4]={2,16,24,8};
int mi;
int m;
encoded[0]='\0';
end=0;
if (strlen((char *)ascii)<5) {
si = (ascii[0] + strlen((char*)ascii)) % PASSWD_LEN;
for (ai=ei=0; ei<32; ai++, ei++, si++) {
if (ascii[ai]=='\0') {
end=1;
}
if (end) {
encoded[ei]=short_salt[si%PASSWD_LEN];
} else {
encoded[ei]=ascii[ai] ^ short_salt[si%PASSWD_LEN];
}
}
return;
}
g_strlcpy((char *)encoded, (char *)ascii, PASSWD_LEN);
len = strlen((char *)encoded);
for (ai=len; ai < PASSWD_LEN; ai++) {
encoded[ai] = encoded[ai-len] + len;
}
for (mi=0; mi<4; mi++) {
m=m_array[mi];
index = (encoded[m] + encoded[m+1]) & 0x3F;
shift = (encoded[m+2] + encoded[m+3]) & 0x7;
for (ei=0; ei<PASSWD_LEN; ei++) {
temp = long_salt[index%64];
temp <<= 8;
temp |= long_salt[index%64];
temp >>= shift;
encoded[m%32] ^= temp & 0xFF;
m++;
index++;
}
}
}
/*
* encoded is a pre-allocated buffer at least 16 bytes long
*
* This is the hashing algorithm used in palm OS >= 4.0.
* Its just a plain ole' MD5.
*/
static void palm_encode_md5(unsigned char *ascii, unsigned char *encoded)
{
struct MD5Context m;
unsigned char digest[20];
MD5Init(&m);
MD5Update(&m, ascii, strlen((char *)ascii));
MD5Final(digest, &m);
memcpy(encoded, digest, 16);
}
#endif
int verify_password(char *password)
{
int i;
#ifdef ENABLE_PRIVATE
unsigned char encoded[34];
unsigned char password_lower[PASSWD_LEN+2];
const char *pref_password;
char hex_str[68];
#endif
#ifdef ENABLE_PRIVATE
if (!password) {
return FALSE;
}
/* The Palm OS lower cases the password first */
/* Yes, I have found this documented on Palm's website */
for (i=0; i < PASSWD_LEN; i++) {
password_lower[i] = tolower(password[i]);
}
/* Check to see that the password matches */
palm_encode_hash(password_lower, encoded);
bin_to_hex_str(encoded, hex_str, PASSWD_LEN);
get_pref(PREF_PASSWORD, NULL, &pref_password);
if (!strcmp(hex_str, pref_password)) {
return TRUE;
}
/* The Password didn't match.
* It could also be an MD5 password.
* Try that now. */
palm_encode_md5((unsigned char *)password, encoded);
bin_to_hex_str(encoded, hex_str, PASSWD_LEN);
hex_str[PASSWD_LEN]='\0';
get_pref(PREF_PASSWORD, NULL, &pref_password);
if (!strcmp(hex_str, pref_password)) {
return TRUE;
}
return FALSE;
#else
/* Return TRUE without checking the password */
return TRUE;
#endif
}
/*
* hide passed HIDE_PRIVATES, SHOW_PRIVATES or MASK_PRIVATES will set the flag.
* hide passed GET_PRIVATES will return the current hide flag.
* hide flag is always returned.
*/
int show_privates(int hide)
{
#ifdef ENABLE_PRIVATE
static int hidden=HIDE_PRIVATES;
#else
static int hidden=SHOW_PRIVATES;
#endif
if (hide != GET_PRIVATES)
hidden = hide;
return hidden;
}
#ifdef ENABLE_PRIVATE
/*
* PASSWORD GUI
*/
/* Start of Dialog window code */
static void cb_dialog_button(GtkWidget *widget,
gpointer data)
{
struct dialog_data *Pdata;
GtkWidget *w;
w = gtk_widget_get_toplevel(widget);
Pdata = gtk_object_get_data(GTK_OBJECT(w), "dialog_data");
if (Pdata) {
Pdata->button_hit = GPOINTER_TO_INT(data);
}
gtk_widget_destroy(GTK_WIDGET(w));
}
static gboolean cb_destroy_dialog(GtkWidget *widget)
{
struct dialog_data *Pdata;
const char *entry;
Pdata = gtk_object_get_data(GTK_OBJECT(widget), "dialog_data");
if (!Pdata) {
return TRUE;
}
entry = gtk_entry_get_text(GTK_ENTRY(Pdata->entry));
if (entry) {
g_strlcpy(Pdata->text, entry, PASSWD_LEN+1);
}
gtk_main_quit();
return TRUE;
}
/*
* returns 1 if cancel was pressed, 2 if OK was hit
*/
int dialog_password(GtkWindow *main_window, char *ascii_password, int retry)
{
GtkWidget *button, *label;
GtkWidget *hbox1, *vbox1;
GtkWidget *dialog;
GtkWidget *entry;
struct dialog_data Pdata;
int ret;
if (!ascii_password) {
return EXIT_FAILURE;
}
ascii_password[0]='\0';
ret = 2;
dialog = gtk_widget_new(GTK_TYPE_WINDOW,
"type", GTK_WINDOW_TOPLEVEL,
"title", _("Palm Password"),
NULL);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_window));
gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
GTK_SIGNAL_FUNC(cb_destroy_dialog), dialog);
hbox1 = gtk_hbox_new(FALSE, 2);
gtk_container_add(GTK_CONTAINER(dialog), hbox1);
gtk_box_pack_start(GTK_BOX(hbox1), gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION, GTK_ICON_SIZE_DIALOG), FALSE, FALSE, 2);
vbox1 = gtk_vbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(vbox1), 5);
gtk_container_add(GTK_CONTAINER(hbox1), vbox1);
hbox1 = gtk_hbox_new(TRUE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
/* Instruction label */
if (retry) {
label = gtk_label_new(_("Incorrect, Reenter PalmOS Password"));
} else {
label = gtk_label_new(_("Enter PalmOS Password"));
}
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
/* Password entry field */
entry = gtk_entry_new_with_max_length(PASSWD_LEN);
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(cb_dialog_button),
GINT_TO_POINTER(DIALOG_SAID_2));
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
/* Button Box */
hbox1 = gtk_hbutton_box_new();
gtk_button_box_set_layout(GTK_BUTTON_BOX (hbox1), GTK_BUTTONBOX_END);
gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbox1), 6);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
/* Cancel Button */
button = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_dialog_button),
GINT_TO_POINTER(DIALOG_SAID_1));
gtk_box_pack_start(GTK_BOX(hbox1), button, FALSE, FALSE, 1);
/* OK Button */
button = gtk_button_new_from_stock(GTK_STOCK_OK);
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_dialog_button),
GINT_TO_POINTER(DIALOG_SAID_2));
gtk_box_pack_start(GTK_BOX(hbox1), button, FALSE, FALSE, 1);
GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
gtk_widget_grab_default(button);
/* Set the default button pressed to CANCEL */
Pdata.button_hit = DIALOG_SAID_1;
Pdata.entry=entry;
Pdata.text[0]='\0';
gtk_object_set_data(GTK_OBJECT(dialog), "dialog_data", &Pdata);
gtk_widget_grab_focus(GTK_WIDGET(entry));
gtk_widget_show_all(dialog);
gtk_main();
if (Pdata.button_hit==DIALOG_SAID_1) {
ret = 1;
}
if (Pdata.button_hit==DIALOG_SAID_2) {
ret = 2;
}
g_strlcpy(ascii_password, Pdata.text, PASSWD_LEN+1);
return ret;
}
#endif