1 |
twoaday |
133 |
/* wptGPGMEWrapper.cpp - Object oriented wrapper for GPGME |
2 |
|
|
* Copyright (C) 2006 Timo Schulz |
3 |
|
|
* |
4 |
|
|
* This file is part of WinPT. |
5 |
|
|
* |
6 |
|
|
* WinPT is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* WinPT is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
18 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include <config.h> |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#include <windows.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <string.h> |
28 |
|
|
|
29 |
|
|
#include "gpgme.h" |
30 |
|
|
#include "wptGPG.h" |
31 |
|
|
#include "wptGPGME.h" |
32 |
twoaday |
195 |
#include "wptW32API.h" |
33 |
twoaday |
133 |
|
34 |
|
|
|
35 |
|
|
/* Constructor to build gpgme context. */ |
36 |
|
|
GPGME::GPGME () |
37 |
|
|
{ |
38 |
|
|
gpgme_new (&ctx); |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
/* Destructor to release gpgme context. */ |
42 |
|
|
GPGME::~GPGME () |
43 |
|
|
{ |
44 |
|
|
if (ctx != NULL) |
45 |
|
|
gpgme_release (ctx); |
46 |
|
|
} |
47 |
|
|
|
48 |
twoaday |
167 |
/* Enable or disable the feature to add the |
49 |
|
|
WinPT version to armored output. */ |
50 |
twoaday |
133 |
void |
51 |
|
|
GPGME::setChangeVersion (bool val) |
52 |
|
|
{ |
53 |
|
|
chg_ver = val; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
/* Enable or disable armor. */ |
57 |
|
|
void |
58 |
|
|
GPGME::setArmor (bool val) |
59 |
|
|
{ |
60 |
|
|
gpgme_set_armor (ctx, val? 1 : 0); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
bool |
64 |
|
|
GPGME::getArmor (void) |
65 |
|
|
{ |
66 |
|
|
return gpgme_get_armor (ctx)? true: false; |
67 |
|
|
} |
68 |
|
|
|
69 |
twoaday |
260 |
|
70 |
|
|
/* Import the keys from the clipboard. |
71 |
|
|
Return value: 0 on success. */ |
72 |
|
|
gpgme_error_t |
73 |
|
|
GPGME::importFromClipboard (void) |
74 |
|
|
{ |
75 |
|
|
gpgme_data_t keydata = NULL; |
76 |
|
|
gpgme_error_t err; |
77 |
|
|
|
78 |
|
|
err = gpg_data_new_from_clipboard (&keydata, 0); |
79 |
|
|
if (err) |
80 |
|
|
return err; |
81 |
|
|
|
82 |
|
|
op_begin (); |
83 |
|
|
err = gpgme_op_import (ctx, keydata); |
84 |
|
|
op_end (); |
85 |
|
|
|
86 |
|
|
gpgme_data_release (keydata); |
87 |
|
|
return err; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
/* Import the keys from the file @file. |
92 |
|
|
Return value: 0 on success. */ |
93 |
|
|
gpgme_error_t |
94 |
|
|
GPGME::importFromFile (const char *file) |
95 |
|
|
{ |
96 |
|
|
gpgme_data_t keydata; |
97 |
|
|
gpgme_error_t err; |
98 |
|
|
|
99 |
|
|
err = gpgme_data_new_from_file (&keydata, file, 1); |
100 |
|
|
if (err) |
101 |
|
|
return err; |
102 |
|
|
|
103 |
|
|
op_begin (); |
104 |
|
|
err = gpgme_op_import (ctx, keydata); |
105 |
|
|
op_end (); |
106 |
|
|
|
107 |
|
|
gpgme_data_release (keydata); |
108 |
|
|
return err; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
|
112 |
|
|
/* Return the import result for the last GPG operation. */ |
113 |
|
|
gpgme_import_result_t |
114 |
|
|
GPGME::importGetResult (void) |
115 |
|
|
{ |
116 |
|
|
return gpgme_op_import_result (ctx); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
twoaday |
133 |
/* Export key pattern @patt to file @outfile. |
121 |
|
|
Return value: 0 on success. */ |
122 |
|
|
gpgme_error_t |
123 |
|
|
GPGME::exportToFile (const char *patt, const char *outfile) |
124 |
|
|
{ |
125 |
|
|
gpgme_data_t dat; |
126 |
|
|
gpgme_error_t err; |
127 |
|
|
|
128 |
|
|
err = gpgme_data_new (&dat); |
129 |
|
|
if (err) |
130 |
|
|
return err; |
131 |
twoaday |
167 |
op_begin (); |
132 |
twoaday |
133 |
err = gpgme_op_export (ctx, patt, 0, dat); |
133 |
twoaday |
167 |
op_end (); |
134 |
twoaday |
133 |
if (err) { |
135 |
|
|
gpgme_data_release (dat); |
136 |
|
|
return err; |
137 |
|
|
} |
138 |
|
|
err = gpg_data_release_and_set_file (dat, outfile); |
139 |
twoaday |
247 |
if (err) |
140 |
|
|
gpgme_data_release (dat); |
141 |
twoaday |
133 |
return err; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
/* Export key pattern @patt to the clipboard. |
146 |
|
|
Return value: 0 on success. */ |
147 |
|
|
gpgme_error_t |
148 |
|
|
GPGME::exportToClipboard (const char *patt) |
149 |
|
|
{ |
150 |
|
|
gpgme_data_t dat; |
151 |
|
|
gpgme_error_t err; |
152 |
|
|
|
153 |
|
|
err = gpgme_data_new (&dat); |
154 |
|
|
if (err) |
155 |
|
|
return err; |
156 |
twoaday |
167 |
op_begin (); |
157 |
twoaday |
133 |
err = gpgme_op_export (ctx, patt, 0, dat); |
158 |
twoaday |
167 |
op_end (); |
159 |
twoaday |
133 |
if (err) { |
160 |
|
|
gpgme_data_release (dat); |
161 |
|
|
return err; |
162 |
|
|
} |
163 |
|
|
gpg_data_release_and_set_clipboard (dat, chg_ver? 1 : 0); |
164 |
|
|
return err; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
/* Export key pattern @patt to the buffer @outdata. |
169 |
|
|
Return value: 0 on success. */ |
170 |
|
|
gpgme_error_t |
171 |
|
|
GPGME::exportToBuffer (const char *patt, char **outdata) |
172 |
|
|
{ |
173 |
|
|
gpgme_error_t err; |
174 |
|
|
gpgme_data_t dat; |
175 |
|
|
size_t n; |
176 |
|
|
char *p; |
177 |
|
|
|
178 |
|
|
err = gpgme_data_new (&dat); |
179 |
|
|
if (err) |
180 |
|
|
return err; |
181 |
twoaday |
167 |
op_begin (); |
182 |
twoaday |
133 |
err = gpgme_op_export (ctx, patt, 0, dat); |
183 |
twoaday |
167 |
op_end (); |
184 |
twoaday |
133 |
if (err) { |
185 |
|
|
gpgme_data_release (dat); |
186 |
|
|
return err; |
187 |
|
|
} |
188 |
|
|
p = gpgme_data_release_and_get_mem (dat, &n); |
189 |
|
|
|
190 |
|
|
*outdata = new char[n+1]; |
191 |
|
|
memcpy (*outdata, p, n); |
192 |
|
|
(*outdata)[n] = 0; |
193 |
|
|
gpgme_free (p); |
194 |
|
|
|
195 |
|
|
return err; |
196 |
|
|
} |