1 |
/* pipefile.c - High level interface for file encryption via pipes |
2 |
* Copyright (C) 2001 Timo Schulz |
3 |
* |
4 |
* This file is part of MyGPGME. |
5 |
* |
6 |
* MyGPGME is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* MyGPGME 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 |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#include <string.h> |
22 |
|
23 |
#include "util.h" |
24 |
#include "context.h" |
25 |
#include "ops.h" |
26 |
#include "key.h" |
27 |
|
28 |
#ifdef USE_GPGME_PIPEFILES |
29 |
/* |
30 |
* File operations based on pipe streams. |
31 |
* Warning, this interface should be only used for small files. For real file |
32 |
* encryption it's a good idea to use the gpgme_file_xxx functions. |
33 |
*/ |
34 |
|
35 |
gpgme_error_t |
36 |
gpgme_op_encrypt_file( gpgme_recipients_t rset, const char *fname, |
37 |
const char *output ) |
38 |
{ |
39 |
gpgme_error_t err = 0; |
40 |
gpgme_ctx_t c; |
41 |
gpgme_data_t plain, ciph; |
42 |
|
43 |
err = gpgme_new( &c ); |
44 |
if ( err ) |
45 |
return err; |
46 |
gpgme_control( c, GPGME_CTRL_ARMOR, 1 ); |
47 |
|
48 |
err = gpgme_data_new_from_file( &plain, fname, 1 ); |
49 |
if ( err ) |
50 |
goto leave; |
51 |
|
52 |
err = gpgme_data_new( &ciph ); |
53 |
if ( err ) |
54 |
goto leave; |
55 |
|
56 |
err = gpgme_op_encrypt( c, rset, plain, ciph ); |
57 |
if ( err ) |
58 |
goto leave; |
59 |
|
60 |
gpgme_data_release_and_set_file( ciph, output ); |
61 |
|
62 |
leave: |
63 |
if ( plain ) |
64 |
gpgme_data_release( plain ); |
65 |
gpgme_release( c ); |
66 |
return err; |
67 |
|
68 |
} /* gpgme_op_encrypt_file */ |
69 |
|
70 |
gpgme_error_t |
71 |
gpgme_op_decrypt_file( gpgme_ctx_t c, const char *fname, |
72 |
const char *output ) |
73 |
{ |
74 |
gpgme_error_t err = 0; |
75 |
gpgme_data_t ciph, plain; |
76 |
|
77 |
err = gpgme_data_new_from_file( &ciph, fname, 1 ); |
78 |
if (err) |
79 |
goto leave; |
80 |
|
81 |
err = gpgme_data_new( &plain ); |
82 |
if (err) |
83 |
goto leave; |
84 |
|
85 |
err = gpgme_op_decrypt( c, ciph, plain ); |
86 |
if (err) |
87 |
goto leave; |
88 |
|
89 |
gpgme_data_release_and_set_file( plain, output ); |
90 |
|
91 |
leave: |
92 |
if ( ciph ) |
93 |
gpgme_data_release( ciph ); |
94 |
gpgme_release( c ); |
95 |
return err; |
96 |
} /* gpgme_op_decrypt_file */ |
97 |
|
98 |
gpgme_error_t |
99 |
gpgme_op_verify_file( const char *fname, const char *detach_sig, |
100 |
gpgme_sigstat_t *r_stat ) |
101 |
{ |
102 |
gpgme_error_t err = 0; |
103 |
gpgme_ctx_t c; |
104 |
gpgme_data_t sig; |
105 |
gpgme_sigstat_t stat; |
106 |
|
107 |
err = gpgme_new( &c ); |
108 |
if ( err ) |
109 |
return err; |
110 |
|
111 |
err = gpgme_data_new_from_file( &sig, fname, 1 ); |
112 |
if ( err ) |
113 |
goto leave; |
114 |
|
115 |
err = gpgme_op_verify( c, sig, NULL ); |
116 |
if ( err ) { |
117 |
*r_stat = 0; |
118 |
goto leave; |
119 |
} |
120 |
|
121 |
*r_stat = stat; |
122 |
|
123 |
leave: |
124 |
if ( sig ) |
125 |
gpgme_data_release( sig ); |
126 |
gpgme_release( c ); |
127 |
return err; |
128 |
} /* gpgme_op_verify_file */ |
129 |
|
130 |
gpgme_error_t |
131 |
gpgme_op_sign_file( gpgme_ctx_t c,gpgme_sigmode_t mode, const char *keyid, |
132 |
const char *fname, const char *output ) |
133 |
{ |
134 |
gpgme_error_t err = 0; |
135 |
gpgme_key_t key; |
136 |
gpgme_data_t plain, ciph; |
137 |
|
138 |
gpgme_control( c, GPGME_CTRL_ARMOR, 1 ); |
139 |
|
140 |
err = gpgme_data_new_from_file( &plain, fname, 1 ); |
141 |
if (err) |
142 |
goto leave; |
143 |
|
144 |
err = gpgme_data_new( &ciph ); |
145 |
if (err) |
146 |
goto leave; |
147 |
|
148 |
err = _gpgme_key_new( &key ); |
149 |
if (err) |
150 |
goto leave; |
151 |
strcpy( key->keys.keyid, keyid ); |
152 |
gpgme_signers_add( c, key ); |
153 |
|
154 |
err = gpgme_op_sign( c, plain, ciph, mode ); |
155 |
if (err) |
156 |
goto leave; |
157 |
|
158 |
gpgme_key_release( key ); |
159 |
gpgme_data_release_and_set_file( ciph, output ); |
160 |
|
161 |
leave: |
162 |
if ( plain ) |
163 |
gpgme_data_release( plain ); |
164 |
gpgme_release( c ); |
165 |
return err; |
166 |
} /* gpgme_op_sign_file */ |
167 |
|
168 |
gpgme_error_t |
169 |
gpgme_op_sign_encrypt_file( gpgme_ctx_t c, gpgme_recipients_t rset, |
170 |
const char *keyid, const char *fname, |
171 |
const char *output ) |
172 |
{ |
173 |
gpgme_error_t err = 0; |
174 |
gpgme_data_t plain, ciph; |
175 |
|
176 |
gpgme_control( c, GPGME_CTRL_ARMOR, 1 ); |
177 |
|
178 |
err = gpgme_data_new_from_file( &plain, fname, 1 ); |
179 |
if (err) |
180 |
goto leave; |
181 |
|
182 |
err = gpgme_data_new( &ciph ); |
183 |
if (err) |
184 |
goto leave; |
185 |
|
186 |
err = gpgme_op_sign_encrypt( c, rset, plain, ciph ); |
187 |
if (err) |
188 |
goto leave; |
189 |
|
190 |
gpgme_data_release_and_set_file( ciph, output ); |
191 |
|
192 |
|
193 |
leave: |
194 |
if ( ciph ) |
195 |
gpgme_data_release( plain ); |
196 |
gpgme_release( c ); |
197 |
return err; |
198 |
} /* gpgme_op_sign_encrypt_file */ |
199 |
|
200 |
gpgme_error_t |
201 |
gpgme_op_import_file(const char *fname) |
202 |
{ |
203 |
gpgme_error_t err = 0; |
204 |
gpgme_ctx_t c; |
205 |
gpgme_data_t keydata; |
206 |
|
207 |
err = gpgme_new( &c ); |
208 |
if ( err ) |
209 |
return err; |
210 |
|
211 |
err = gpgme_data_new_from_file( &keydata, fname, 1 ); |
212 |
if ( err ) |
213 |
goto leave; |
214 |
|
215 |
err = gpgme_op_import( c, keydata ); |
216 |
|
217 |
leave: |
218 |
if ( keydata ) |
219 |
gpgme_data_release( keydata ); |
220 |
gpgme_release( c ); |
221 |
return err; |
222 |
} /* gpgme_op_import_file */ |
223 |
|
224 |
gpgme_error_t |
225 |
gpgme_op_export_file( gpgme_recipients_t rset, const char *output ) |
226 |
{ |
227 |
gpgme_error_t err = 0; |
228 |
gpgme_ctx_t c; |
229 |
gpgme_data_t keydata; |
230 |
|
231 |
err = gpgme_new( &c ); |
232 |
if ( err ) |
233 |
return err; |
234 |
gpgme_control( c, GPGME_CTRL_ARMOR, 1 ); |
235 |
|
236 |
err = gpgme_data_new( &keydata ); |
237 |
if ( err ) |
238 |
goto leave; |
239 |
|
240 |
err = gpgme_op_export( c, rset, keydata ); |
241 |
if ( err ) |
242 |
goto leave; |
243 |
|
244 |
gpgme_data_release_and_set_file( keydata, output ); |
245 |
|
246 |
leave: |
247 |
gpgme_release( c ); |
248 |
return err; |
249 |
} /* gpgme_op_export_file */ |
250 |
#endif |