1 |
/* sym-encrypt.c - symmetric encrypt functions |
2 |
* Copyright (C) 2002, 2003, 2005 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 <stdio.h> |
22 |
#include <stdlib.h> |
23 |
#include <string.h> |
24 |
#include <assert.h> |
25 |
|
26 |
#include "util.h" |
27 |
#include "context.h" |
28 |
#include "ops.h" |
29 |
|
30 |
|
31 |
struct symenc_result_s { |
32 |
int failed; |
33 |
}; |
34 |
|
35 |
|
36 |
static gpgme_error_t |
37 |
create_result_struct (gpgme_ctx_t ctx) |
38 |
{ |
39 |
assert (!ctx->result.symenc); |
40 |
ctx->result.symenc = calloc (1, sizeof *ctx->result.symenc); |
41 |
if (!ctx->result.symenc) |
42 |
return mk_error (Out_Of_Core); |
43 |
ctx->result_type = RESULT_TYPE_SYMENC; |
44 |
|
45 |
return 0; |
46 |
} /* create_result_struct */ |
47 |
|
48 |
|
49 |
void |
50 |
_gpgme_release_symenc_result (_symenc_result_t res) |
51 |
{ |
52 |
safe_free( res ); |
53 |
} /* _gpgme_relese_symenc_result */ |
54 |
|
55 |
|
56 |
static void |
57 |
symenc_status_handler (gpgme_ctx_t ctx, gpg_status_code_t code, char *args) |
58 |
{ |
59 |
if (ctx->out_of_core) |
60 |
return; |
61 |
|
62 |
if (ctx->result_type == RESULT_TYPE_NONE) { |
63 |
if (create_result_struct (ctx)) { |
64 |
ctx->out_of_core = 1; |
65 |
return; |
66 |
} |
67 |
} |
68 |
|
69 |
assert (ctx->result_type == RESULT_TYPE_SYMENC); |
70 |
|
71 |
switch (code) { |
72 |
case STATUS_MISSING_PASSPHRASE: |
73 |
ctx->result.symenc->failed = 1; |
74 |
break; |
75 |
|
76 |
case STATUS_END_DECRYPTION: |
77 |
ctx->result.symenc->failed = 0; |
78 |
break; |
79 |
|
80 |
case STATUS_PROGRESS: |
81 |
if (ctx->cb.progress) |
82 |
_gpgme_progress_handler (ctx, args); |
83 |
break; |
84 |
|
85 |
default: |
86 |
break; |
87 |
} |
88 |
} /* symenc_status_handler */ |
89 |
|
90 |
|
91 |
static gpgme_error_t |
92 |
symenc_start( gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t ciph ) |
93 |
{ |
94 |
int rc = 0; |
95 |
|
96 |
fail_on_pending_request( ctx ); |
97 |
ctx->pending = 1; |
98 |
|
99 |
/* create a process object */ |
100 |
_gpgme_gpg_release( &ctx->gpg ); |
101 |
rc = _gpgme_gpg_new( &ctx->gpg ); |
102 |
if( rc ) |
103 |
goto leave; |
104 |
|
105 |
if( ctx->use_logging ) |
106 |
_gpgme_gpg_set_logging_handler( ctx->gpg, ctx ); |
107 |
_gpgme_gpg_set_status_handler ( ctx->gpg, symenc_status_handler, ctx ); |
108 |
if( ctx->passphrase_value ) { |
109 |
rc = _gpgme_add_passphrase( ctx ); |
110 |
if( rc ) |
111 |
goto leave; |
112 |
} |
113 |
|
114 |
/* build the commandline */ |
115 |
_gpgme_gpg_add_arg (ctx->gpg, "--symmetric"); |
116 |
_gpgme_encrypt_add_cipher (ctx); |
117 |
if (ctx->no_compress) |
118 |
_gpgme_gpg_add_arg (ctx->gpg, "-z 0"); |
119 |
if (ctx->force_mdc) |
120 |
_gpgme_gpg_add_arg (ctx->gpg, "--force-mdc"); |
121 |
if (ctx->use_armor) |
122 |
_gpgme_gpg_add_arg (ctx->gpg, "--armor"); |
123 |
|
124 |
/* Check the supplied data */ |
125 |
if( gpgme_data_get_type( plain ) == GPGME_DATA_TYPE_NONE ) { |
126 |
rc = mk_error( No_Data ); |
127 |
goto leave; |
128 |
} |
129 |
_gpgme_data_set_mode( plain, GPGME_DATA_MODE_OUT ); |
130 |
if ( !ciph || gpgme_data_get_type( ciph ) != GPGME_DATA_TYPE_NONE ) { |
131 |
rc = mk_error (Invalid_Value); |
132 |
goto leave; |
133 |
} |
134 |
_gpgme_data_set_mode( ciph, GPGME_DATA_MODE_IN ); |
135 |
/* Tell the gpg object about the data */ |
136 |
if( ctx->use_tmpfiles ) { |
137 |
_gpgme_gpg_add_arg ( ctx->gpg, "--output" ); |
138 |
_gpgme_gpg_add_arg ( ctx->gpg, _gpgme_get_tmpfile( 0 ) ); |
139 |
_gpgme_data_write_to_tmpfile( plain ); |
140 |
_gpgme_gpg_add_arg ( ctx->gpg, _gpgme_get_tmpfile( 1 ) ); |
141 |
} |
142 |
else { |
143 |
_gpgme_gpg_add_arg ( ctx->gpg, "--output" ); |
144 |
_gpgme_gpg_add_arg ( ctx->gpg, "-" ); |
145 |
_gpgme_gpg_add_data ( ctx->gpg, ciph, 1 ); |
146 |
_gpgme_gpg_add_arg ( ctx->gpg, "--" ); |
147 |
_gpgme_gpg_add_data ( ctx->gpg, plain, 0 ); |
148 |
} |
149 |
|
150 |
/* and kick off the process */ |
151 |
rc = _gpgme_gpg_spawn ( ctx->gpg, ctx ); |
152 |
|
153 |
leave: |
154 |
if( rc ) { |
155 |
ctx->pending = 0; |
156 |
_gpgme_gpg_release( &ctx->gpg ); |
157 |
} |
158 |
return rc; |
159 |
} /* symenc_start */ |
160 |
|
161 |
|
162 |
gpgme_error_t |
163 |
gpgme_op_symenc( gpgme_ctx_t ctx, gpgme_data_t in, gpgme_data_t out ) |
164 |
{ |
165 |
gpgme_error_t err; |
166 |
|
167 |
err = symenc_start ( ctx, in, out ); |
168 |
if( !err ) { |
169 |
gpgme_wait( ctx, 1 ); |
170 |
ctx->pending = 0; |
171 |
if( ctx->use_tmpfiles ) { |
172 |
_gpgme_data_read_from_tmpfile( out ); |
173 |
_gpgme_del_tmpfiles( ctx->wipe_fnc ); |
174 |
} |
175 |
assert( ctx->result.symenc ); |
176 |
if( ctx->result.symenc->failed ) |
177 |
err = mk_error( No_Passphrase ); |
178 |
else if( gpgme_get_process_rc( ctx ) ) |
179 |
err = mk_error( Interal_GPG_Problem ); |
180 |
} |
181 |
return err; |
182 |
} /* gpgme_op_symenc */ |
183 |
|
184 |
|
185 |
gpgme_error_t |
186 |
gpgme_op_clip_symenc( gpgme_ctx_t ctx ) |
187 |
{ |
188 |
gpgme_error_t err; |
189 |
gpgme_data_t plain = NULL; |
190 |
gpgme_data_t ciph = NULL; |
191 |
|
192 |
gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 ); |
193 |
err = gpgme_data_new_from_clipboard (&plain); |
194 |
if( !err ) |
195 |
err = gpgme_data_new( &ciph ); |
196 |
if( !err ) |
197 |
err = gpgme_op_symenc( ctx, plain, ciph ); |
198 |
if( !err ) { |
199 |
gpgme_data_change_version( &ciph ); |
200 |
gpgme_data_release_and_set_clipboard( ciph ); |
201 |
} |
202 |
gpgme_data_release( plain ); |
203 |
|
204 |
return err; |
205 |
} /* gpgme_op_clip_symenc */ |