summaryrefslogtreecommitdiff
path: root/plugins/CryptoPP/src/cpp_pgpw.cpp
blob: 23f1540e68dc3d4cf68c7444c6939cc24c09d9f4 (plain)
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
#include "commonheaders.h"


HMODULE hpgpsdk;
UINT	pgpVer;
HRSRC	hRS_pgp;
PBYTE	pRS_pgp;

int   __cdecl _pgp_init(void);
int   __cdecl _pgp_done(void);
int   __cdecl _pgp_open_keyrings(LPSTR,LPSTR);
int   __cdecl _pgp_close_keyrings(void);
int   __cdecl _pgp_get_version(void);
LPSTR __cdecl _pgp_get_error(void);
int   __cdecl _pgp_size_keyid(void);
PVOID __cdecl _pgp_select_keyid(HWND,LPSTR);
LPSTR __cdecl _pgp_encrypt_keydb(LPCSTR,PVOID);
LPSTR __cdecl _pgp_decrypt_keydb(LPCSTR);
//int   __cdecl _pgp_check_key(LPCSTR);
LPSTR __cdecl _pgp_encrypt_key(LPCSTR,LPCSTR);
LPSTR __cdecl _pgp_decrypt_key(LPCSTR,LPCSTR);

int   (__cdecl *p_pgp_init)(void);
int   (__cdecl *p_pgp_done)(void);
int   (__cdecl *p_pgp_open_keyrings)(LPSTR,LPSTR);
int   (__cdecl *p_pgp_close_keyrings)(void);
int   (__cdecl *p_pgp_get_version)(void);
LPSTR (__cdecl *p_pgp_get_error)(void);
int   (__cdecl *p_pgp_size_keyid)(void);
PVOID (__cdecl *p_pgp_select_keyid)(HWND,LPSTR);
LPSTR (__cdecl *p_pgp_encrypt_keydb)(LPCSTR,PVOID);
LPSTR (__cdecl *p_pgp_decrypt_keydb)(LPCSTR);
//int   (__cdecl *p_pgp_check_key)(LPCSTR);
LPSTR (__cdecl *p_pgp_encrypt_key)(LPCSTR,LPCSTR);
LPSTR (__cdecl *p_pgp_decrypt_key)(LPCSTR,LPCSTR);

#define GPA(x)                                              \
{                                                           \
	*((PVOID*)&p##x) = (PVOID)GetProcAddress(mod, TEXT(#x)); \
	if (!p##x) return 0;                                     \
}

int load_pgpsdk_dll(HMODULE mod)
{
	GPA(_pgp_init);
	GPA(_pgp_done);
	GPA(_pgp_open_keyrings);
	GPA(_pgp_close_keyrings);
	GPA(_pgp_get_version);
	GPA(_pgp_get_error);
	GPA(_pgp_size_keyid);
	GPA(_pgp_select_keyid);
	GPA(_pgp_encrypt_keydb);
	GPA(_pgp_decrypt_keydb);
	GPA(_pgp_encrypt_key);
	GPA(_pgp_decrypt_key);

	return 1;
}

#undef GPA

#define GPA(x)                                                 \
{                                                              \
	*((PVOID*)&p##x) = (PVOID)GetProcAddress(mod, TEXT(#x)); \
	if (!p##x) {                                                \
	return 0;                                                   \
	}                                                           \
}

int load_pgpsdk_mem(HMODULE mod)
{
	GPA(_pgp_init);
	GPA(_pgp_done);
	GPA(_pgp_open_keyrings);
	GPA(_pgp_close_keyrings);
	GPA(_pgp_get_version);
	GPA(_pgp_get_error);
	GPA(_pgp_size_keyid);
	GPA(_pgp_select_keyid);
	GPA(_pgp_encrypt_keydb);
	GPA(_pgp_decrypt_keydb);
	GPA(_pgp_encrypt_key);
	GPA(_pgp_decrypt_key);
	return 1;
}

#undef GPA

int __cdecl pgp_init()
{
	if (!hPGPPRIV) {
		// create context for private pgp keys
		hPGPPRIV = (HANDLE)cpp_create_context(MODE_PGP | MODE_PRIV_KEY);
		pCNTX tmp = (pCNTX)hPGPPRIV;
		tmp->pdata = (PBYTE)malloc(sizeof(PGPDATA));
		memset(tmp->pdata, 0, sizeof(PGPDATA));
	}

	hpgpsdk = g_hInst;
	return 0;
}

int __cdecl pgp_done()
{
	int r = 0;
	pgpVer = 0;
	if (hpgpsdk) {
		r = p_pgp_done();
		hpgpsdk = nullptr;
	}
	return r;
}

int __cdecl pgp_open_keyrings(LPSTR PubRingPath, LPSTR SecRingPath)
{
	return p_pgp_open_keyrings(PubRingPath, SecRingPath);
}

int __cdecl pgp_close_keyrings()
{
	return p_pgp_close_keyrings();
}

int __cdecl pgp_get_version()
{
	return pgpVer;
}

LPSTR __cdecl pgp_get_error()
{
	return p_pgp_get_error();
}

LPSTR __cdecl pgp_encrypt(pCNTX ptr, LPCSTR szPlainMsg)
{
	ptr->error = ERROR_NONE;
	mir_free(ptr->tmp);
	pPGPDATA p = (pPGPDATA)ptr->pdata;

	LPSTR szEncMsg = (p->pgpKey ? p_pgp_encrypt_key(szPlainMsg, (LPCSTR)p->pgpKey) : p_pgp_encrypt_keydb(szPlainMsg, p->pgpKeyID));
	if (!szEncMsg) {
		return ptr->tmp = nullptr;
	}
	else {
		ptr->tmp = mir_strdup(szEncMsg);
		LocalFree((LPVOID)szEncMsg);

		return ptr->tmp;
	}
}

LPSTR __cdecl pgp_decrypt(pCNTX ptr, LPCSTR szEncMsg)
{
	ptr->error = ERROR_NONE;
	mir_free(ptr->tmp);
	ptr->tmp = nullptr;

	LPSTR szPlainMsg = p_pgp_decrypt_keydb(szEncMsg);
	if (!szPlainMsg) {
		ptr = get_context_on_id(hPGPPRIV); // find private pgp keys
		if (ptr) {
			pPGPDATA p = (pPGPDATA)ptr->pdata;
			if (p->pgpKey)
				szPlainMsg = p_pgp_decrypt_key(szEncMsg, (LPCSTR)p->pgpKey);
		}
		if (!szPlainMsg) {
			return nullptr;
		}
	}

	ptr->tmp = mir_strdup(szPlainMsg);
	LocalFree((LPVOID)szPlainMsg);
	return ptr->tmp;
}

LPSTR __cdecl pgp_encode(HANDLE context, LPCSTR szPlainMsg)
{
	pCNTX ptr = get_context_on_id(context); if (!ptr) return nullptr;
	pPGPDATA p = (pPGPDATA)cpp_alloc_pdata(ptr);
	if (!p->pgpKeyID && !p->pgpKey) {
		ptr->error = ERROR_NO_PGP_KEY;
		return nullptr;
	}

	// utf8 message: encrypt.
	return pgp_encrypt(ptr, szPlainMsg);
}

LPSTR __cdecl pgp_decode(HANDLE context, LPCSTR szEncMsg)
{
	pCNTX ptr = get_context_on_id(context);
	if (!ptr)
		return nullptr;
	mir_free(ptr->tmp);

	LPSTR szNewMsg = nullptr;
	LPSTR szOldMsg = pgp_decrypt(ptr, szEncMsg);

	if (szOldMsg) {
		if (!is_7bit_string(szOldMsg) && !is_utf8_string(szOldMsg)) {
			szNewMsg = mir_utf8encode(szOldMsg);
		}
		else
			szNewMsg = mir_strdup(szOldMsg);
	}
	return ptr->tmp = szNewMsg;
}

int __cdecl pgp_set_priv_key(LPCSTR LocalKey)
{
	return pgp_set_key(hPGPPRIV, LocalKey);
}

int __cdecl pgp_set_key(HANDLE context, LPCSTR RemoteKey)
{
	pCNTX ptr = get_context_on_id(context);
	if (!ptr)
		return 0;
	pPGPDATA p = (pPGPDATA)cpp_alloc_pdata(ptr);
	ptr->error = ERROR_NONE;

	SAFE_FREE(p->pgpKey);
	p->pgpKey = (PBYTE)_strdup(RemoteKey);

	return 1;
}

int __cdecl pgp_set_keyid(HANDLE context, PVOID RemoteKeyID)
{
	pCNTX ptr = get_context_on_id(context);
	if (!ptr)
		return 0;
	pPGPDATA p = (pPGPDATA)cpp_alloc_pdata(ptr);
	ptr->error = ERROR_NONE;

	SAFE_FREE(p->pgpKeyID);
	p->pgpKeyID = (PBYTE)malloc(p_pgp_size_keyid());
	memcpy(p->pgpKeyID, RemoteKeyID, p_pgp_size_keyid());

	return 1;
}

int __cdecl pgp_size_keyid()
{
	return p_pgp_size_keyid();
}

PVOID __cdecl pgp_select_keyid(HWND hDlg, LPSTR szKeyID)
{
	return p_pgp_select_keyid(hDlg, szKeyID);
}