1 |
/* wptClipboard.cpp |
2 |
* Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz |
3 |
* Copyright (C) 2005 g10 Code GmbH |
4 |
* |
5 |
* This file is part of WinPT. |
6 |
* |
7 |
* WinPT is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* WinPT is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
*/ |
21 |
|
22 |
#ifdef HAVE_CONFIG_H |
23 |
#include <config.h> |
24 |
#endif |
25 |
|
26 |
#include <windows.h> |
27 |
#include <gpgme.h> |
28 |
|
29 |
#include "wptW32API.h" |
30 |
#include "wptCrypto.h" |
31 |
#include "wptVersion.h" |
32 |
#include "wptGPG.h" |
33 |
|
34 |
gpgme_error_t gpg_data_wrap_lines (gpgme_data_t *r_dh, size_t wraplen); |
35 |
|
36 |
/* XXX: define clipboard errors. */ |
37 |
|
38 |
/* Check if the clipboard contains text. @r_val contains |
39 |
one text data is available. |
40 |
Return value: 0 on success. */ |
41 |
gpgme_error_t |
42 |
gpg_clip_istext_avail (int *r_val) |
43 |
{ |
44 |
HANDLE clipmem; |
45 |
char *clipdata; |
46 |
|
47 |
if (!r_val) |
48 |
return gpg_error (GPG_ERR_INV_ARG); |
49 |
if (OpenClipboard (NULL) == FALSE) |
50 |
return gpg_error (GPG_ERR_NO_DATA); |
51 |
|
52 |
*r_val = 0; |
53 |
clipmem = GetClipboardData (CF_TEXT); |
54 |
if (!clipmem) |
55 |
goto leave; |
56 |
|
57 |
clipdata = (char *) GlobalLock (clipmem); |
58 |
if (!clipdata) |
59 |
goto leave; |
60 |
if (strlen (clipdata) > 0) |
61 |
*r_val = 1; |
62 |
GlobalUnlock (clipmem); |
63 |
|
64 |
leave: |
65 |
CloseClipboard (); |
66 |
return 0; |
67 |
} |
68 |
|
69 |
|
70 |
/* Figure out what kind of PGP data is stored in @data. |
71 |
@r_type contains the ORed types on success. |
72 |
Return value: 0 on success. */ |
73 |
gpgme_error_t |
74 |
gpg_clip_parse_pgpid (const char *data, int *r_type) |
75 |
{ |
76 |
int type; |
77 |
|
78 |
if (!data) |
79 |
return gpg_error (GPG_ERR_INV_ARG); |
80 |
if (strlen (data) < 19) { |
81 |
*r_type = 0; |
82 |
return 0; |
83 |
} |
84 |
type = 0; |
85 |
if (strstr( data, "BEGIN PGP MESSAGE") ) |
86 |
type |= PGP_MESSAGE; |
87 |
if (strstr (data, "BEGIN PGP SIGNED MESSAGE")) |
88 |
type |= PGP_CLEARSIG; |
89 |
if (strstr (data, "BEGIN PGP SIGNATURE")) |
90 |
type |= PGP_SIG; |
91 |
if (strstr (data, "BEGIN PGP PUBLIC KEY BLOCK")) |
92 |
type |= PGP_PUBKEY; |
93 |
if (strstr (data, "BEGIN PGP PRIVATE KEY BLOCK") |
94 |
|| strstr (data, "BEGIN PGP SECRET KEY BLOCK")) |
95 |
type |= PGP_SECKEY; |
96 |
if (strstr (data, "- -----BEGIN")) |
97 |
type |= PGP_DASH_ESCAPED; |
98 |
*r_type = type; |
99 |
return 0; |
100 |
} |
101 |
|
102 |
|
103 |
/* Check if the clipboard is already OpenPGP protected. |
104 |
@r_type optional returns the type and @r_val contains |
105 |
1 for yes. |
106 |
Return value: 0 on success. */ |
107 |
gpgme_error_t |
108 |
gpg_clip_is_secured (int *r_type, int *r_val) |
109 |
{ |
110 |
int rc = 0; |
111 |
HANDLE clipmem; |
112 |
char *clipdata; |
113 |
|
114 |
if (!r_val) |
115 |
return gpg_error (GPG_ERR_INV_ARG); |
116 |
if( OpenClipboard (NULL) == FALSE ) |
117 |
return gpg_error (GPG_ERR_INTERNAL); |
118 |
clipmem = GetClipboardData( CF_TEXT ); |
119 |
if( !clipmem ) { |
120 |
*r_val = 0; |
121 |
goto leave; |
122 |
} |
123 |
clipdata = (char *) GlobalLock( clipmem ); |
124 |
if (!clipdata) { |
125 |
*r_val = 0; |
126 |
goto leave; |
127 |
} |
128 |
if (strstr (clipdata, "-----BEGIN PGP") |
129 |
&& strstr (clipdata, "-----END PGP")) |
130 |
*r_val = 1; |
131 |
GlobalUnlock (clipmem); |
132 |
gpg_clip_parse_pgpid (clipdata, r_type); |
133 |
|
134 |
leave: |
135 |
CloseClipboard (); |
136 |
return rc; |
137 |
} |
138 |
|
139 |
|
140 |
gpgme_error_t |
141 |
gpg_clip_get_pgptype (int *r_type) |
142 |
{ |
143 |
gpgme_error_t rc = 0; |
144 |
HANDLE clipmem; |
145 |
char *clipdata; |
146 |
|
147 |
if (OpenClipboard (NULL) == FALSE) |
148 |
return gpg_error (GPG_ERR_INTERNAL); |
149 |
clipmem = GetClipboardData (CF_TEXT); |
150 |
if (!clipmem) { |
151 |
rc = gpg_error (GPG_ERR_NO_DATA); |
152 |
goto leave; |
153 |
} |
154 |
clipdata = (char *)GlobalLock( clipmem ); |
155 |
if (!clipdata) { |
156 |
rc = gpg_error (GPG_ERR_NO_DATA); |
157 |
goto leave; |
158 |
} |
159 |
gpg_clip_parse_pgpid (clipdata, r_type); |
160 |
GlobalUnlock (clipmem); |
161 |
|
162 |
leave: |
163 |
CloseClipboard (); |
164 |
return rc; |
165 |
} |
166 |
|
167 |
|
168 |
/* Retrieve the thext of the clipboard. |
169 |
Return value: the text as a string, NULL otherwise. */ |
170 |
static char* |
171 |
get_w32_clip_text (void) |
172 |
{ |
173 |
char *private_clip = NULL; |
174 |
char *clip = NULL; |
175 |
size_t size; |
176 |
HANDLE cb; |
177 |
|
178 |
if (!OpenClipboard (NULL)) |
179 |
return NULL; |
180 |
cb = GetClipboardData (CF_TEXT); |
181 |
if (!cb) |
182 |
goto leave; |
183 |
private_clip = (char *)GlobalLock (cb); |
184 |
if (!private_clip) |
185 |
goto leave; |
186 |
size = strlen (private_clip); |
187 |
clip = (char*)malloc (size + 1); |
188 |
if (!clip) { |
189 |
GlobalUnlock (cb); |
190 |
goto leave; |
191 |
} |
192 |
memcpy (clip, private_clip, size); |
193 |
clip[size] = '\0'; |
194 |
GlobalUnlock(cb); |
195 |
|
196 |
leave: |
197 |
CloseClipboard (); |
198 |
return clip; |
199 |
} |
200 |
|
201 |
|
202 |
/* Set the new clipboard data to @data. */ |
203 |
static void |
204 |
set_w32_clip_text (const char *data, int size) |
205 |
{ |
206 |
HANDLE cb; |
207 |
char *private_data; |
208 |
|
209 |
if (!OpenClipboard(NULL)) |
210 |
return; |
211 |
EmptyClipboard (); |
212 |
cb = GlobalAlloc (GHND, size+1); |
213 |
if (!cb) |
214 |
goto leave; |
215 |
private_data = (char*)GlobalLock (cb); |
216 |
if (!private_data) |
217 |
goto leave; |
218 |
memcpy (private_data, data, size); |
219 |
private_data[size] = '\0'; |
220 |
SetClipboardData (CF_TEXT, cb); |
221 |
GlobalUnlock (cb); |
222 |
|
223 |
leave: |
224 |
CloseClipboard (); |
225 |
GlobalFree (cb); |
226 |
} |
227 |
|
228 |
|
229 |
/* Retrieve the clipboard data and create a new gpgme |
230 |
data object @r_dh and return it. If @wraplen is > 0 |
231 |
wrap lines of the buffer. |
232 |
Return value: 0 on success. */ |
233 |
gpgme_error_t |
234 |
gpg_data_new_from_clipboard (gpgme_data_t *r_dh, int wraplen) |
235 |
{ |
236 |
gpgme_error_t err = 0; |
237 |
gpgme_data_t dh; |
238 |
char *clip_text = NULL; |
239 |
|
240 |
if (!r_dh) |
241 |
return gpg_error (GPG_ERR_INV_ARG); |
242 |
*r_dh = NULL; |
243 |
clip_text = get_w32_clip_text (); |
244 |
if (!clip_text) |
245 |
return gpg_error (GPG_ERR_NO_DATA); |
246 |
err = gpgme_data_new_from_mem (&dh, clip_text, strlen (clip_text), 1); |
247 |
if (clip_text) |
248 |
free (clip_text); |
249 |
if (wraplen > 0) |
250 |
gpg_data_wrap_lines (&dh, wraplen); |
251 |
|
252 |
*r_dh = dh; |
253 |
return err; |
254 |
} |
255 |
|
256 |
|
257 |
/* Search for the armor header 'Version:' in @r_dh and |
258 |
add the WinPT version. On success @r_dh is replaced |
259 |
with the modified data. |
260 |
Return value: 0 on success. */ |
261 |
static gpgme_error_t |
262 |
gpg_data_change_version (gpgme_data_t *r_dh) |
263 |
{ |
264 |
gpgme_error_t err = 0; |
265 |
gpgme_data_t mdh; |
266 |
char line[128+32]; |
267 |
int n; |
268 |
|
269 |
if (!r_dh) |
270 |
return gpg_error (GPG_ERR_INV_ARG); |
271 |
|
272 |
err = gpgme_data_new (&mdh); |
273 |
if (err) |
274 |
return err; |
275 |
|
276 |
gpgme_data_rewind (*r_dh); |
277 |
while ((n=gpg_data_readline (*r_dh, line, 128)) > 0) { |
278 |
if (strlen (line) > 14 && |
279 |
!strncmp (line, "Version: GnuPG", 14) && |
280 |
!strstr (line, "WinPT "PGM_VERSION)) { |
281 |
line[strlen (line) - 2] = '\0'; |
282 |
strcat (line, " - " ); |
283 |
strcat (line, "WinPT "PGM_VERSION); |
284 |
strcat (line, "\r\n"); |
285 |
} |
286 |
gpgme_data_write (mdh, line, strlen (line)); |
287 |
} |
288 |
|
289 |
gpgme_data_write (mdh, "", 1); |
290 |
gpgme_data_rewind (mdh); |
291 |
gpgme_data_release (*r_dh); |
292 |
*r_dh = mdh; |
293 |
return err; |
294 |
} |
295 |
|
296 |
/* Release a gpgme data object @dh and write the contents of |
297 |
the object to the clipboard. If @chg_ver is set, modify the |
298 |
Version: header and add WinPT information. */ |
299 |
void |
300 |
gpg_data_release_and_set_clipboard (gpgme_data_t dh, int chg_ver) |
301 |
{ |
302 |
gpgme_data_t in; |
303 |
char *clip_text; |
304 |
size_t n; |
305 |
|
306 |
if (!dh) |
307 |
return; |
308 |
|
309 |
in = dh; |
310 |
if (chg_ver) |
311 |
gpg_data_change_version (&in); |
312 |
|
313 |
clip_text = gpgme_data_release_and_get_mem (in, &n); |
314 |
if (clip_text && *clip_text) { |
315 |
set_w32_clip_text (clip_text, n); |
316 |
memset (clip_text, 0xFF, n); |
317 |
gpgme_free (clip_text); |
318 |
} |
319 |
} |