/[winpt]/trunk/MyGPGME/sym-encrypt.c
ViewVC logotype

Annotation of /trunk/MyGPGME/sym-encrypt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (hide annotations)
Mon Jan 31 11:02:21 2005 UTC (20 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 5540 byte(s)
WinPT initial checkin.


1 twoaday 2 /* sym-encrypt.c - symmetric encrypt functions
2     * Copyright (C) 2002, 2003 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     default:
81     break;
82     }
83     } /* symenc_status_handler */
84    
85    
86     static gpgme_error_t
87     symenc_start( gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t ciph )
88     {
89     int rc = 0;
90    
91     fail_on_pending_request( ctx );
92     ctx->pending = 1;
93    
94     /* create a process object */
95     _gpgme_gpg_release( &ctx->gpg );
96     rc = _gpgme_gpg_new( &ctx->gpg );
97     if( rc )
98     goto leave;
99    
100     if( ctx->use_logging )
101     _gpgme_gpg_set_logging_handler( ctx->gpg, ctx );
102     _gpgme_gpg_set_status_handler ( ctx->gpg, symenc_status_handler, ctx );
103     if( ctx->passphrase_value ) {
104     rc = _gpgme_add_passphrase( ctx );
105     if( rc )
106     goto leave;
107     }
108    
109     /* build the commandline */
110     _gpgme_gpg_add_arg ( ctx->gpg, "--symmetric" );
111     _gpgme_encrypt_add_cipher (ctx);
112     if (ctx->no_compress)
113     _gpgme_gpg_add_arg (ctx->gpg, "-z 0");
114     if (ctx->force_mdc)
115     _gpgme_gpg_add_arg (ctx->gpg, "--force-mdc");
116     if( ctx->use_armor )
117     _gpgme_gpg_add_arg( ctx->gpg, "--armor" );
118    
119     /* Check the supplied data */
120     if( gpgme_data_get_type( plain ) == GPGME_DATA_TYPE_NONE ) {
121     rc = mk_error( No_Data );
122     goto leave;
123     }
124     _gpgme_data_set_mode( plain, GPGME_DATA_MODE_OUT );
125     if ( !ciph || gpgme_data_get_type( ciph ) != GPGME_DATA_TYPE_NONE ) {
126     rc = mk_error (Invalid_Value);
127     goto leave;
128     }
129     _gpgme_data_set_mode( ciph, GPGME_DATA_MODE_IN );
130     /* Tell the gpg object about the data */
131     if( ctx->use_tmpfiles ) {
132     _gpgme_gpg_add_arg ( ctx->gpg, "--output" );
133     _gpgme_gpg_add_arg ( ctx->gpg, _gpgme_get_tmpfile( 0 ) );
134     _gpgme_data_write_to_tmpfile( plain );
135     _gpgme_gpg_add_arg ( ctx->gpg, _gpgme_get_tmpfile( 1 ) );
136     }
137     else {
138     _gpgme_gpg_add_arg ( ctx->gpg, "--output" );
139     _gpgme_gpg_add_arg ( ctx->gpg, "-" );
140     _gpgme_gpg_add_data ( ctx->gpg, ciph, 1 );
141     _gpgme_gpg_add_arg ( ctx->gpg, "--" );
142     _gpgme_gpg_add_data ( ctx->gpg, plain, 0 );
143     }
144    
145     /* and kick off the process */
146     rc = _gpgme_gpg_spawn ( ctx->gpg, ctx );
147    
148     leave:
149     if( rc ) {
150     ctx->pending = 0;
151     _gpgme_gpg_release( &ctx->gpg );
152     }
153     return rc;
154     } /* symenc_start */
155    
156    
157     gpgme_error_t
158     gpgme_op_symenc( gpgme_ctx_t ctx, gpgme_data_t in, gpgme_data_t out )
159     {
160     gpgme_error_t err;
161    
162     err = symenc_start ( ctx, in, out );
163     if( !err ) {
164     gpgme_wait( ctx, 1 );
165     ctx->pending = 0;
166     if( ctx->use_tmpfiles ) {
167     _gpgme_data_read_from_tmpfile( out );
168     _gpgme_del_tmpfiles( ctx->wipe_fnc );
169     }
170     assert( ctx->result.symenc );
171     if( ctx->result.symenc->failed )
172     err = mk_error( No_Passphrase );
173     else if( gpgme_get_process_rc( ctx ) )
174     err = mk_error( Interal_GPG_Problem );
175     }
176     return err;
177     } /* gpgme_op_symenc */
178    
179    
180     gpgme_error_t
181     gpgme_op_clip_symenc( gpgme_ctx_t ctx )
182     {
183     gpgme_error_t err;
184     gpgme_data_t plain = NULL;
185     gpgme_data_t ciph = NULL;
186    
187     gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 );
188     err = gpgme_data_new_from_clipboard (&plain);
189     if( !err )
190     err = gpgme_data_new( &ciph );
191     if( !err )
192     err = gpgme_op_symenc( ctx, plain, ciph );
193     if( !err ) {
194     gpgme_data_change_version( &ciph );
195     gpgme_data_release_and_set_clipboard( ciph );
196     }
197     gpgme_data_release( plain );
198    
199     return err;
200     } /* gpgme_op_clip_symenc */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26