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
406
|
#include "gnupgplugin.h"
char PGP_PUBKEY_PROLOG[]="-----BEGIN PGP PUBLIC KEY BLOCK-----";
char PGP_PUBKEY_EPILOG[]="-----END PGP PUBLIC KEY BLOCK-----";
char PGP_MSG_PROLOG[]="-----BEGIN PGP MESSAGE-----";
char PGP_MSG_EPILOG[]="-----END PGP MESSAGE-----";
int SendMsgSvc(WPARAM wParam, LPARAM lParam)
// ANSI mode:
// ANSI source message is encrypted to ANSI cyphertext, then sent.
// UNICODE mode:
// ANSI part of source ANSI+UCS2 message is encrypted to ANSI cyphertext,
// then the cyphertext is appended by UCS2 version of the ANSI
// cyphertext, then the resulting ANSI+UCS2 cyphertext is sent.
// Note: the UCS2 part of the source message is lost! This is a feature.
{
CCSDATA *ccs=(CCSDATA*)lParam;
if (!ccs)
{
LogMessage("--- GnuPG.SendMsgSvc: ","ccs==NULL, not our case.","\n");
return CallService(MS_PROTO_CHAINSEND,wParam,lParam);
}
char *msg=(char*)(ccs->lParam);
if (!msg)
{
LogMessage("--- GnuPG.SendMsgSvc: ","msg==NULL, not our case.","\n");
return CallService(MS_PROTO_CHAINSEND,wParam,lParam);
}
bool isUnicode=(bool)(ccs->wParam&PREF_UNICODE);
LogMessage("--- GnuPG.SendMsgSvc: mode: ",(isUnicode?"UNICODE":"ANSI"),"\n");
LogMessage("--- GnuPG.SendMsgSvc: msg (ANSI part):\n",msg,"\n--- EOM ---\n");
#if defined(__GPG_DEBUG__)
int binsiz=strlen(msg)+1;
if (isUnicode) binsiz*=(sizeof(wchar_t)+1);
FILE *fp=fopen("C:\\mir_SendMsgSvc_preenc.asc","wb");
if (fp)
{
fwrite(msg,1,binsiz,fp);
fclose(fp);
}
#endif
char keyuserid[keyuseridsize];
// get keyuserid
{
DBVARIANT dbv;
strcpy(keyuserid,(
DBGetContactSetting(ccs->hContact,pluginid,dbkeyuserid,&dbv)==0
?dbv.pszVal
:""));
DBFreeVariant(&dbv);
}
UINT useencryptionchecked
=DBGetContactSettingByte(ccs->hContact,pluginid,dbuseencryption,FALSE);
char *buf=NULL;
if (useencryptionchecked && strlen(keyuserid))
{
char keyid[keyidsize];
ZeroMemory(keyid,sizeof(keyid));
getNextPart(keyid,keyuserid,txtidseparator);
if (gpgEncrypt(&buf,keyid,msg)!=gpgSuccess)
{
ErrorMessage(txterror,txtencryptfailed,txtverifyoptions);
return 0;
}
// *** ENCRYPT
if (buf) // now buf allocated and filled by encrypted message.
{
int binsiz=strlen(buf)+1;
if (isUnicode)
{
char *mix=makemix_dup(buf);
if (mix)
{
fnFALSE(&buf);
buf=mix;
binsiz*=(sizeof(wchar_t)+1);
}
else
LogMessage("--- GnuPG.SendMsgSvc: MIX: ","FAILED.","\n");
}
LogMessage("--- GnuPG.SendMsgSvc: Encrypted: \n",buf,"\n--- EOM ---\n");
#if defined(__GPG_DEBUG__)
FILE *fp=fopen("C:\\mir_SendMsgSvc_encbuf.asc","wb");
if (fp)
{
fwrite(buf,1,binsiz,fp);
fclose(fp);
}
#endif
ccs->lParam=(LPARAM)buf;
// *** THIS IS A FEATURE HACK FOR JABBERG CODE... ***
ccs->wParam&=~PREF_UNICODE; // *** GO ANSI WAY!
LogMessage("--- GnuPG.SendMsgSvc: ANSI MODE FORCED: ","JabberG feature hack.","\n");
// *** THIS IS A FEATURE HACK FOR JABBERG CODE... ***
int rc=CallService(MS_PROTO_CHAINSEND,wParam,(LPARAM)ccs);
// *** SEND ENCRYPTED!
fnFALSE(&buf);
ccs->lParam=(LPARAM)msg;
return rc;
}
// buf not allocated, so... send unencrypted.
}
LogMessage("--- GnuPG.SendMsgSvc: sending: ","UNENCRYPTED.","\n");
return CallService(MS_PROTO_CHAINSEND,wParam,lParam);
} // int SendMsgSvc(WPARAM wParam, LPARAM lParam)
enum recv_event_type { re_undefined, re_pgpmessage, re_pgppublickey, re_unknown };
enum recv_event_type get_recv_event_type(const char *msg)
{
char *prolog,*epilog;
prolog=(char*)strstr(msg,PGP_MSG_PROLOG);
epilog=(char*)strstr(msg,PGP_MSG_EPILOG);
if (prolog && epilog)
{
LogMessage("--- GnuPG.get_recv_event_type: ","re_pgpmessage","\n");
return re_pgpmessage;
}
prolog=(char*)strstr(msg,PGP_PUBKEY_PROLOG);
epilog=(char*)strstr(msg,PGP_PUBKEY_EPILOG);
if (prolog && epilog)
{
LogMessage("--- GnuPG.get_recv_event_type: ","re_pgppublickey","\n");
return re_pgppublickey;
}
LogMessage("--- GnuPG.get_recv_event_type: ","re_unknown","\n");
return re_unknown;
}
int parse_pgp_message(WPARAM wParam, CCSDATA *ccs)
{
PROTORECVEVENT *pre=(PROTORECVEVENT*)(ccs->lParam);
char *msg=pre->szMessage;
if (!msg)
return CallService(MS_PROTO_CHAINRECV,wParam,(LPARAM)ccs);
bool isUnicode=(bool)(pre->flags&PREF_UNICODE);
bool isUtf=(bool)(pre->flags&PREF_UTF);
LogMessage("--- GnuPG.parse_pgp_message: pre->flags: ",
(isUnicode?"UNICODE":"ANSI"),
(isUtf?"+UTF\n":"\n"));
char *OrigMessage=msg;
char *tmp=fixcrlf_dup(msg);
if (tmp)
msg=tmp;
else
LogMessage("--- GnuPG.parse_pgp_message: CR/LF: ","FAILED.","\n");
char keyuserid[keyuseridsize]; ZeroMemory(keyuserid,sizeof(keyuserid));
bool useridvalid=(gpgDetectUserID(keyuserid,msg)==gpgSuccess);
char *storedpassphrase=NULL;
char passphrase[passphrasesize];
if (useridvalid)
{
if (storepassphraseschecked==BST_CHECKED)
storedpassphrase=getPassphrase(keyuserid);
}
else
{
ErrorMessage(txtwarning,txtdetectuseridfailed,txtverifyoptions);
strcpy(keyuserid,txtunknownuserid);
}
gpgResult gpgresult=gpgUnknownError;
char *decrypteddata=NULL;
LogMessage("--- GnuPG.parse_pgp_message: keyuserid: ",keyuserid,"\n");
if (storedpassphrase)
{
strcpy(passphrase,storedpassphrase);
gpgresult=gpgDecrypt(&decrypteddata,msg,passphrase);
// *** DECRYPT
}
int dlgresult=IDOK;
while (gpgresult!=gpgSuccess && dlgresult!=IDCANCEL)
// if there were no stored passphrase or we just failed to decrypt...
{
dlgresult=DialogBoxParam(dllinstance,MAKEINTRESOURCE(IDD_PASSPHRASE_DLG),
NULL,PassphraseDialogProcedure,(LPARAM)keyuserid);
if (dlgresult==IDOK) // if some passphrase given...
{
strcpy(passphrase,dlgpassphrase); ZeroMemory(dlgpassphrase,passphrasesize);
strcat(passphrase,txtcrlf); // wtf
gpgresult=gpgDecrypt(&decrypteddata,msg,passphrase);
// *** DECRYPT
}
}
// here on cancel or successful decryption
if (gpgresult==gpgSuccess) // if successfully decrypted
{
if (useridvalid && storepassphraseschecked==BST_CHECKED)
addPassphrase(keyuserid,passphrase); // cache passphrase
}
if (decrypteddata)
{
// 0. not utf? => decode from utf to ANSI
if (!isUtf) // !!! QUICK'N'DIRTY HACK for 0.7 <-> 0.7 only
mir_utf8decode(decrypteddata,NULL);
// We have some decrypted data, so let's:
// 1. embrace 'em with tags.
// 2. replace original message with the embraced decrypted data.
char *tmp=(char*)mir_alloc(strlen(starttag)+strlen(decrypteddata)+strlen(endtag)+1);
if (tmp)
{
strcpy(tmp,starttag);
strcat(tmp,decrypteddata);
strcat(tmp,endtag);
mir_free(decrypteddata);
decrypteddata=tmp;
} // *** start and end tags added if possible
LogMessage("--- GnuPG.parse_pgp_message: decrypteddata:\n",
decrypteddata,"\n--- EOM ---\n");
pre->szMessage=decrypteddata; // swap for decrypted
if (msg!=OrigMessage) fnFALSE(&msg); // throw away msg if not OrigMessage
}
else
pre->szMessage=msg; // no decrypted, so show the cyphertext
if (isUnicode && pre->szMessage!=OrigMessage)
// in Unicode mode, only for mir_allocated message
{
char *mix=makemix_dup(pre->szMessage);
if (mix)
{
mir_free(pre->szMessage);
pre->szMessage=mix; // mix done
}
else
{
LogMessage("--- GnuPG.parse_pgp_message: MIX: ","FAILED.","\n");
mir_free(pre->szMessage); // *** whatever there were...
pre->szMessage=OrigMessage;
// failed to mix => original cyphertext!
}
} // Unicode mode? => we should use mix! ...or original message.
ZeroMemory(passphrase,sizeof(passphrase)); // security kid
int rc=CallService(MS_PROTO_CHAINRECV,wParam,(LPARAM)ccs);
// RECEIVE IT FINALLY
if (pre->szMessage!=OrigMessage)
{
mir_free(pre->szMessage);
pre->szMessage=OrigMessage; // swap back to original encrypted
}
return rc;
}
int parse_pgp_public_key(WPARAM wParam, CCSDATA *ccs)
{
PROTORECVEVENT *pre=(PROTORECVEVENT*)(ccs->lParam);
char *key=pre->szMessage;
if (!key)
return CallService(MS_PROTO_CHAINRECV,wParam,(LPARAM)ccs);
bool isUnicode=(pre->flags&PREF_UNICODE)?true:false;
char *OrigMessage=key;
char *tmp=fixcrlf_dup(key);
if (tmp)
key=tmp;
else
LogMessage("--- GnuPG.parse_pgp_public_key: CR/LF: ","FAILED.","\n");
time_t now; time(&now); struct tm tms=*localtime(&now);
const int ASCTIMESIZ=26; // *** as defined in asctime description. Dangerous?
char atstr[ASCTIMESIZ];
strcpy(atstr,asctime(&tms)); replace(atstr,"\n",""); replace(atstr,"\r","");
DBWriteContactSettingString(ccs->hContact,pluginid,dbkeymodified,atstr);
gpgResult gpgresult=gpgUnknownError;
char *atprolog=strstr(key,PGP_PUBKEY_PROLOG);
if (atprolog)
{
DBWriteContactSettingString(ccs->hContact,pluginid,dbkey,atprolog);
if (autoimportpublickeyschecked==BST_CHECKED)
gpgresult=gpgImportPublicKey(atprolog);
// *** PUBKEY MAYBE IMPORTED
}
if (key && key!=OrigMessage)
mir_free(key);
// let's try to save some human-readable words for history!
char *buf=(char*)mir_alloc(
strlen(starttag)
+strlen(Translate(txtpublickeyreceivedstored))
+strlen(Translate(txtpublickeyreceived))
+strlen(Translate(txtimportpublickeyfailed))
+strlen(Translate(txtverifyoptions))
+strlen(endtag)
+3 // two ' ' and one '\0'
);
if (buf)
{
strcpy(buf,starttag);
if (gpgresult==gpgSuccess)
strcat(buf,Translate(txtpublickeyreceivedstored));
else
{
strcat(buf,Translate(txtpublickeyreceived));
if (autoimportpublickeyschecked==BST_CHECKED)
{
strcat(buf," ");
strcat(buf,Translate(txtimportpublickeyfailed));
strcat(buf," ");
strcat(buf,Translate(txtverifyoptions));
}
}
strcat(buf,endtag);
pre->flags&=~PREF_UNICODE; // no reason for Unicode here!
pre->szMessage=buf; // for history
int rc=CallService(MS_PROTO_CHAINRECV,wParam,(LPARAM)ccs);
// *** some words for history should be written to db now.
if (pre->szMessage!=OrigMessage)
{
mir_free(pre->szMessage);
pre->szMessage=OrigMessage; // swap back...
}
return rc;
}
return CallService(MS_PROTO_CHAINRECV,wParam,(LPARAM)ccs);
// as far as we have no chance to save our words for history,
// let it be the original data.
}
int RecvMsgSvc(WPARAM wParam, LPARAM lParam)
// wParam - used to call proto recv chain, not used internally.
// lParam - points to CCSDATA structure
// returns result of CallService(MS_PROTO_CHAINRECV,wParam,lParam)
{
CCSDATA *ccs=(CCSDATA*)lParam;
if (!ccs) // ccs==NULL => not our case!
return CallService(MS_PROTO_CHAINRECV,wParam,lParam);
PROTORECVEVENT *pre=(PROTORECVEVENT*)(ccs->lParam);
if (!pre) // pre==NULL => not our case!
return CallService(MS_PROTO_CHAINRECV,wParam,lParam);
char *msg=pre->szMessage;
if (!msg) // No message => not our case!
return CallService(MS_PROTO_CHAINRECV,wParam,lParam);
bool isUnicode=(bool)(pre->flags&PREF_UNICODE);
LogMessage("--- GnuPG.RecvMsgSvc: mode: ",(isUnicode?"UNICODE":"ANSI"),"\n");
LogMessage("--- GnuPG.RecvMsgSvc: msg (ANSI part):\n",msg,"\n--- EOM ---\n");
#if defined(__GPG_DEBUG__)
FILE *fp=fopen("C:\\mir_RecvMsgSvc.bin","wb");
if (fp)
{
int binlen=strlen(msg)+1;
if (isUnicode)
binlen*=(sizeof(wchar_t)+1);
fwrite(msg,1,binlen,fp);
fclose(fp);
}
#endif
switch (get_recv_event_type(msg))
{
case re_pgpmessage: return parse_pgp_message(wParam,ccs);
case re_pgppublickey: return parse_pgp_public_key(wParam,ccs);
default: break; // unknown event type, so not our case!
}
return CallService(MS_PROTO_CHAINRECV,wParam,lParam);
// not our case...
} // int RecvMsgSvc(WPARAM wParam, LPARAM lParam)
|