1 |
twoaday |
2 |
/* revoke.c - Revocation certificates |
2 |
|
|
* Copyright (C) 2001, 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 <string.h> |
22 |
|
|
#include <sys/types.h> |
23 |
|
|
|
24 |
|
|
#include "ops.h" |
25 |
|
|
#include "context.h" |
26 |
|
|
#include "util.h" |
27 |
|
|
#include "openpgp.h" |
28 |
|
|
|
29 |
|
|
struct revoke_result_s { |
30 |
|
|
int bad_passphrase; |
31 |
|
|
}; |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
static void |
35 |
|
|
revoke_set_reason( gpgme_editkey_t ctx, int id ) |
36 |
|
|
{ |
37 |
|
|
switch (id) { |
38 |
|
|
case GPGME_REVOKE_INSECURE: ctx->u.revoke.reason_id = "1"; break; |
39 |
|
|
case GPGME_REVOKE_SUPERSEDED: ctx->u.revoke.reason_id = "2"; break; |
40 |
|
|
case GPGME_REVOKE_NOUSED: ctx->u.revoke.reason_id = "3"; break; |
41 |
|
|
default: ctx->u.revoke.reason_id = "0"; break; |
42 |
|
|
} |
43 |
|
|
} /* revoke_set_reason */ |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
gpgme_error_t |
47 |
|
|
gpgme_revoke_set( gpgme_editkey_t ctx, const char * uid, const char * text, |
48 |
|
|
int reason, int pgpmode, const char *pass ) |
49 |
|
|
{ |
50 |
|
|
char * p; |
51 |
|
|
|
52 |
|
|
if( !ctx ) |
53 |
|
|
return mk_error( Invalid_Value ); |
54 |
|
|
revoke_set_reason( ctx, reason ); |
55 |
|
|
ctx->type = GPGME_EDITKEY_REVOKE; |
56 |
|
|
ctx->u.revoke.uid = uid; |
57 |
|
|
if( text ) { |
58 |
|
|
p = ctx->u.revoke.reason_text = strdup( text ); |
59 |
|
|
if (!p) |
60 |
|
|
return mk_error (Out_Of_Core); |
61 |
|
|
ctx->u.revoke.reason_sent = 0; |
62 |
|
|
} |
63 |
|
|
ctx->u.revoke.passwd = pass; |
64 |
|
|
ctx->u.revoke.pgp = pgpmode; |
65 |
|
|
return 0; |
66 |
|
|
} /* gpgme_revoke_set */ |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
static void |
70 |
|
|
revoke_status_handler (gpgme_ctx_t ctx, gpg_statcode_t code, char * args) |
71 |
|
|
{ |
72 |
|
|
struct revoke_result_s * c = ctx->tmp_res; |
73 |
|
|
|
74 |
|
|
if( ctx->out_of_core ) |
75 |
|
|
return; |
76 |
|
|
|
77 |
|
|
switch( code ) { |
78 |
|
|
case STATUS_EOF: |
79 |
|
|
break; |
80 |
|
|
|
81 |
|
|
case STATUS_GOOD_PASSPHRASE: |
82 |
|
|
c->bad_passphrase = 0; |
83 |
|
|
break; |
84 |
|
|
|
85 |
|
|
default: /* ignore all other codes */ |
86 |
|
|
break; |
87 |
|
|
} |
88 |
|
|
} /* revoke_status_handler */ |
89 |
|
|
|
90 |
|
|
|
91 |
|
|
static const char * |
92 |
|
|
revoke_command_handler( void *opaque, gpg_statcode_t code, const char *key ) |
93 |
|
|
{ |
94 |
|
|
gpgme_editkey_t c = opaque; |
95 |
|
|
|
96 |
|
|
if( !code ) { |
97 |
|
|
/* cleanup */ |
98 |
|
|
return NULL; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
if( !key ) |
102 |
|
|
return NULL; |
103 |
|
|
|
104 |
|
|
if( code == STATUS_GET_BOOL && !strcmp( key, "gen_revoke.okay" ) ) { |
105 |
|
|
c->u.revoke.reason_sent = 0; |
106 |
|
|
return "Y"; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
if( code == STATUS_GET_LINE && !strcmp( key, "ask_revocation_reason.code" ) ) |
110 |
|
|
return c->u.revoke.reason_id; |
111 |
|
|
|
112 |
|
|
if( code == STATUS_GET_LINE && !strcmp( key, "ask_revocation_reason.text" ) ) { |
113 |
|
|
if( c->u.revoke.reason_sent == 0 && c->u.revoke.reason_text ) { |
114 |
|
|
c->u.revoke.reason_sent++; |
115 |
|
|
return c->u.revoke.reason_text; |
116 |
|
|
} |
117 |
|
|
return ""; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
if( code == STATUS_GET_BOOL && !strcmp( key, "ask_revocation_reason.okay" ) ) |
121 |
|
|
return "Y"; |
122 |
|
|
|
123 |
|
|
if( code == STATUS_GET_HIDDEN && !strcmp( key, "passphrase.enter" ) ) |
124 |
|
|
return c->u.revoke.passwd; |
125 |
|
|
|
126 |
|
|
return NULL; |
127 |
|
|
} /* command_handler */ |
128 |
|
|
|
129 |
|
|
|
130 |
|
|
static gpgme_error_t |
131 |
|
|
revoke_start( gpgme_ctx_t ctx, gpgme_editkey_t rev, gpgme_data_t cert ) |
132 |
|
|
{ |
133 |
|
|
gpgme_error_t rc; |
134 |
|
|
|
135 |
|
|
fail_on_pending_request( ctx ); |
136 |
|
|
ctx->pending = 1; |
137 |
|
|
|
138 |
|
|
_gpgme_gpg_release( &ctx->gpg ); |
139 |
|
|
rc = _gpgme_gpg_new( &ctx->gpg ); |
140 |
|
|
if( rc ) |
141 |
|
|
goto leave; |
142 |
|
|
|
143 |
|
|
if( ctx->use_logging ) |
144 |
|
|
_gpgme_gpg_set_logging_handler( ctx->gpg, ctx ); |
145 |
|
|
_gpgme_gpg_set_status_handler( ctx->gpg, revoke_status_handler, ctx ); |
146 |
|
|
_gpgme_gpg_set_command_handler( ctx->gpg, revoke_command_handler, rev ); |
147 |
|
|
|
148 |
|
|
_gpgme_data_set_mode( cert, GPGME_DATA_MODE_IN ); |
149 |
|
|
|
150 |
|
|
if( rev->u.revoke.pgp ) |
151 |
|
|
_gpgme_gpg_add_arg( ctx->gpg, "--pgp7" ); |
152 |
|
|
_gpgme_gpg_add_arg( ctx->gpg, "--output"); |
153 |
|
|
_gpgme_gpg_add_arg( ctx->gpg, "-" ); |
154 |
|
|
_gpgme_gpg_add_data( ctx->gpg, cert, 1 ); |
155 |
|
|
|
156 |
|
|
_gpgme_gpg_add_arg( ctx->gpg, "--gen-revoke" ); |
157 |
|
|
_gpgme_gpg_add_arg( ctx->gpg, rev->u.revoke.uid ); |
158 |
|
|
|
159 |
|
|
rc = _gpgme_gpg_spawn( ctx->gpg, ctx ); |
160 |
|
|
|
161 |
|
|
leave: |
162 |
|
|
if( rc ) { |
163 |
|
|
_gpgme_gpg_release( &ctx->gpg ); |
164 |
|
|
ctx->pending = 0; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
return rc; |
168 |
|
|
} /* gpgme_op_revoke_start */ |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
gpgme_error_t |
172 |
|
|
gpgme_op_revoke (gpgme_ctx_t ctx, gpgme_editkey_t rev, gpgme_data_t cert) |
173 |
|
|
{ |
174 |
|
|
struct revoke_result_s res; |
175 |
|
|
gpgme_error_t rc; |
176 |
|
|
|
177 |
|
|
res.bad_passphrase = 1; |
178 |
|
|
ctx->tmp_res = &res; |
179 |
|
|
|
180 |
|
|
rc = revoke_start (ctx, rev, cert); |
181 |
|
|
if( !rc ) { |
182 |
|
|
gpgme_wait (ctx, 1); |
183 |
|
|
if (res.bad_passphrase) |
184 |
|
|
rc = mk_error (Bad_Passphrase); |
185 |
|
|
ctx->pending = 0; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
return rc; |
189 |
|
|
} /* gpgme_op_revoke */ |
190 |
|
|
|
191 |
|
|
|
192 |
|
|
gpgme_error_t |
193 |
|
|
gpgme_revcert_new( gpgme_revcert_t *r_cert ) |
194 |
|
|
{ |
195 |
|
|
gpgme_revcert_t ctx; |
196 |
|
|
|
197 |
|
|
if( !r_cert ) |
198 |
|
|
return mk_error( Invalid_Value ); |
199 |
|
|
ctx = calloc( 1, sizeof *ctx ); |
200 |
|
|
if( !ctx ) |
201 |
|
|
return mk_error( Out_Of_Core ); |
202 |
|
|
*r_cert = ctx; |
203 |
|
|
return 0; |
204 |
|
|
} /* gpgme_revcert_new */ |
205 |
|
|
|
206 |
|
|
|
207 |
|
|
void |
208 |
|
|
gpgme_revcert_release( gpgme_revcert_t cert ) |
209 |
|
|
{ |
210 |
|
|
if( cert ) { |
211 |
|
|
safe_free( cert->reason ); |
212 |
|
|
safe_free( cert ); |
213 |
|
|
} |
214 |
|
|
} /* gpgme_revcert_release */ |
215 |
|
|
|
216 |
|
|
gpgme_error_t |
217 |
|
|
gpgme_revcert_parse( const char *data, gpgme_revcert_t cert, char * keyid ) |
218 |
|
|
{ |
219 |
|
|
gpg_iobuf_t tmp; |
220 |
|
|
armor_filter_context_t afx; |
221 |
|
|
PACKET *pkt = calloc( 1, sizeof *pkt ); |
222 |
|
|
PKT_signature * sig; |
223 |
|
|
int rc = 0, len = 0, seq = 0; |
224 |
|
|
const byte *p; |
225 |
|
|
FILE * fp; |
226 |
|
|
|
227 |
|
|
if( !data || !cert ) |
228 |
|
|
return mk_error( Invalid_Value ); |
229 |
|
|
|
230 |
|
|
fp = fopen( "temp.winpt.xxx", "wb" ); |
231 |
|
|
if( fp ) { |
232 |
|
|
fwrite( data, 1, strlen( data ), fp ); |
233 |
|
|
fclose( fp ); |
234 |
|
|
} |
235 |
|
|
tmp = gpg_iobuf_open( "temp.winpt.xxx" ); |
236 |
|
|
if( tmp == NULL ) |
237 |
|
|
return mk_error( General_Error ); |
238 |
|
|
gpg_iobuf_ioctl( tmp, 3, 1, NULL ); /* disable cache */ |
239 |
|
|
if( gpg_use_armor_filter( tmp ) ) { |
240 |
|
|
memset( &afx, 0, sizeof afx ); |
241 |
|
|
gpg_iobuf_push_filter( tmp, gpg_armor_filter, &afx ); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
gpg_iobuf_seek( tmp, 0 ); |
245 |
|
|
gpg_init_packet( pkt ); |
246 |
|
|
while( ( rc = gpg_parse_packet( tmp, pkt ) ) != -1 ) { |
247 |
|
|
if( pkt->pkttype == PKT_SIGNATURE ) |
248 |
|
|
break; |
249 |
|
|
gpg_free_packet( pkt ); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
if( pkt->pkttype != PKT_SIGNATURE ) |
253 |
|
|
goto fail; |
254 |
|
|
sig = pkt->pkt.signature; |
255 |
|
|
|
256 |
|
|
p = gpg_parse_sig_subpkt2( sig, SIGSUBPKT_REVOC_REASON, &len ); |
257 |
|
|
if( p ) { |
258 |
|
|
cert->code = p[0]; |
259 |
|
|
if( len > 1 ) { |
260 |
|
|
cert->reason = calloc( 1, len ); |
261 |
|
|
if( !cert->reason ) |
262 |
|
|
goto fail; |
263 |
|
|
memcpy( cert->reason , p + 1, len - 1 ); |
264 |
|
|
cert->reason[len-1] = '\0'; |
265 |
|
|
} |
266 |
|
|
else |
267 |
|
|
cert->reason = NULL; |
268 |
|
|
} |
269 |
|
|
sprintf( cert->keyid, "%08lX%08lX", sig->keyid[0], sig->keyid[1] ); |
270 |
|
|
if( keyid ) |
271 |
|
|
strcpy( keyid, cert->keyid ); |
272 |
|
|
cert->timestamp = 0; |
273 |
|
|
p = gpg_parse_sig_subpkt2( sig, SIGSUBPKT_SIG_CREATED, &len ); |
274 |
|
|
if( p && len == 4 ) |
275 |
|
|
cert->timestamp = p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; |
276 |
|
|
|
277 |
|
|
fail: |
278 |
|
|
gpg_iobuf_close( tmp ); |
279 |
|
|
unlink( "temp.winpt.xxx" ); |
280 |
|
|
gpg_free_packet( pkt ); |
281 |
|
|
safe_free( pkt ); |
282 |
|
|
return 0; |
283 |
|
|
} /* gpgme_revcert_parse */ |