1 |
twoaday |
2 |
/* editkey-util.c |
2 |
|
|
* Copyright (C) 2001-2004 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 <string.h> |
23 |
|
|
#include <stdlib.h> |
24 |
|
|
|
25 |
|
|
#include "gpgme-config.h" |
26 |
|
|
#ifdef WITH_EDITKEY |
27 |
|
|
#include "gpgme.h" |
28 |
|
|
#include "context.h" |
29 |
|
|
#include "ops.h" |
30 |
|
|
#include "util.h" |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
gpgme_error_t |
34 |
|
|
gpgme_editkey_new( gpgme_editkey_t * r_ctx ) |
35 |
|
|
{ |
36 |
|
|
gpgme_editkey_t ctx; |
37 |
|
|
|
38 |
|
|
if( !r_ctx ) |
39 |
|
|
return mk_error( Invalid_Value ); |
40 |
|
|
*r_ctx = NULL; |
41 |
|
|
ctx = calloc( 1, sizeof *ctx ); |
42 |
|
|
if( !ctx ) |
43 |
|
|
return mk_error( Out_Of_Core ); |
44 |
|
|
*r_ctx = ctx; |
45 |
|
|
|
46 |
|
|
return 0; |
47 |
|
|
} /* gpgme_editkey_new */ |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
static void |
51 |
|
|
revoke_release (gpgme_editkey_t ctx) |
52 |
|
|
{ |
53 |
|
|
safe_free (ctx->u.revoke.reason_text); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
static void |
58 |
|
|
adduid_release (gpgme_editkey_t ctx) |
59 |
|
|
{ |
60 |
|
|
safe_free (ctx->u.adduid.comment); |
61 |
|
|
safe_free (ctx->u.adduid.email); |
62 |
|
|
safe_free (ctx->u.adduid.name); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
void |
67 |
|
|
gpgme_editkey_release( gpgme_editkey_t ctx ) |
68 |
|
|
{ |
69 |
|
|
if (!ctx) |
70 |
|
|
return; |
71 |
|
|
|
72 |
|
|
switch (ctx->type) { |
73 |
|
|
case GPGME_EDITKEY_REVOKE: revoke_release (ctx); break; |
74 |
|
|
case GPGME_EDITKEY_ADDUID: adduid_release (ctx); break; |
75 |
|
|
} |
76 |
|
|
ctx->type = 0; |
77 |
|
|
safe_free (ctx); |
78 |
|
|
} /* gpgme_editkey_release */ |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/** |
82 |
|
|
* DELUID |
83 |
|
|
**/ |
84 |
|
|
void |
85 |
|
|
gpgme_editkey_deluid_set_id (gpgme_editkey_t ctx, int id) |
86 |
|
|
{ |
87 |
|
|
if (!ctx) |
88 |
|
|
return; |
89 |
|
|
|
90 |
|
|
ctx->u.deluid.id = id; |
91 |
|
|
ctx->type = GPGME_EDITKEY_DELUID; |
92 |
|
|
} /* gpgme_editkey_deluid_set_uid */ |
93 |
|
|
|
94 |
|
|
/** |
95 |
|
|
* DELKEY |
96 |
|
|
**/ |
97 |
|
|
void |
98 |
|
|
gpgme_editkey_delkey_set_id( gpgme_editkey_t ctx, int id ) |
99 |
|
|
{ |
100 |
|
|
if( !ctx ) |
101 |
|
|
return; |
102 |
|
|
ctx->u.delkey.id = id; |
103 |
|
|
ctx->type = GPGME_EDITKEY_DELKEY; |
104 |
|
|
} /* gpgme_editkey_delkey_set_id */ |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
/** |
108 |
|
|
* ADDUID |
109 |
|
|
**/ |
110 |
|
|
void |
111 |
|
|
gpgme_editkey_adduid_set( gpgme_editkey_t ctx, const char *name, |
112 |
|
|
const char *email, const char *comment, |
113 |
|
|
const char *passwd ) |
114 |
|
|
{ |
115 |
|
|
if( !ctx ) |
116 |
|
|
return; |
117 |
|
|
|
118 |
|
|
/* name is a MUST field */ |
119 |
|
|
ctx->u.adduid.name = strdup( name ); |
120 |
|
|
ctx->u.adduid.email = strdup( email ); |
121 |
|
|
if( !comment ) |
122 |
|
|
ctx->u.adduid.use_comment = 0; |
123 |
|
|
else { |
124 |
|
|
ctx->u.adduid.comment = strdup( comment ); |
125 |
|
|
ctx->u.adduid.use_comment = 1; |
126 |
|
|
} |
127 |
|
|
ctx->u.adduid.passwd = passwd; |
128 |
|
|
ctx->type = GPGME_EDITKEY_ADDUID; |
129 |
|
|
} /* gpgme_editkey_adduid_set */ |
130 |
|
|
|
131 |
|
|
|
132 |
|
|
gpgme_error_t |
133 |
|
|
gpgme_editkey_addrev_set (gpgme_editkey_t ctx, const char * userid, |
134 |
|
|
const char * passwd) |
135 |
|
|
{ |
136 |
|
|
if (!ctx) |
137 |
|
|
return mk_error (Invalid_Value); |
138 |
|
|
if (!userid) |
139 |
|
|
return mk_error (Invalid_Mode); |
140 |
|
|
ctx->u.addrev.passwd = passwd; |
141 |
|
|
ctx->u.addrev.uid = userid; |
142 |
|
|
ctx->type = GPGME_EDITKEY_ADDREV; |
143 |
|
|
return 0; |
144 |
|
|
} /* gpgme_editkey_addrev_set */ |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
void |
148 |
|
|
gpgme_editkey_addphoto_set( gpgme_editkey_t ctx, const char *jpegfile, |
149 |
|
|
const char * passwd ) |
150 |
|
|
{ |
151 |
|
|
if( !ctx ) |
152 |
|
|
return; |
153 |
|
|
ctx->u.addphoto.jpg = jpegfile; |
154 |
|
|
ctx->u.addphoto.passwd = passwd; |
155 |
|
|
ctx->type = GPGME_EDITKEY_ADDPHOTO; |
156 |
|
|
} /* gpgme_editkey_addphoto_set */ |
157 |
|
|
|
158 |
|
|
|
159 |
|
|
/** |
160 |
|
|
* TRUST |
161 |
|
|
**/ |
162 |
|
|
void |
163 |
|
|
gpgme_editkey_trust_set( gpgme_editkey_t ctx, int val ) |
164 |
|
|
{ |
165 |
|
|
if( !ctx ) |
166 |
|
|
return; |
167 |
|
|
|
168 |
|
|
switch( val ) { |
169 |
|
|
case 0: ctx->u.trust.trust_val = NULL; break; |
170 |
|
|
case GPGME_TRUST_DONTKNOW: ctx->u.trust.trust_val = "1"; break; |
171 |
|
|
case GPGME_TRUST_NEVER: ctx->u.trust.trust_val = "2"; break; |
172 |
|
|
case GPGME_TRUST_MARGINAL: ctx->u.trust.trust_val = "3"; break; |
173 |
|
|
case GPGME_TRUST_FULLY: ctx->u.trust.trust_val = "4";break; |
174 |
|
|
case GPGME_TRUST_ULTIMATE: ctx->u.trust.trust_val = "5"; break; |
175 |
|
|
default: ctx->u.trust.trust_val = "1"; break; |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
ctx->type = GPGME_EDITKEY_TRUST; |
179 |
|
|
} /* gpgme_editkey_trust_set */ |
180 |
|
|
|
181 |
|
|
/** |
182 |
|
|
* SIGN |
183 |
|
|
**/ |
184 |
|
|
gpgme_error_t |
185 |
|
|
gpgme_editkey_sign_set (gpgme_editkey_t ctx, |
186 |
|
|
const char * passwd, |
187 |
|
|
int sigclass, |
188 |
|
|
int sigtype, |
189 |
|
|
const char * param) |
190 |
|
|
{ |
191 |
|
|
if (!ctx) |
192 |
|
|
return mk_error (Invalid_Value); |
193 |
|
|
if (sigclass < 0 || sigclass > 3) |
194 |
|
|
return mk_error (Invalid_Mode); |
195 |
|
|
switch (sigtype) { |
196 |
|
|
case GPGME_EDITKEY_SIGN: |
197 |
|
|
case GPGME_EDITKEY_TSIGN: |
198 |
|
|
case GPGME_EDITKEY_NRSIGN: |
199 |
|
|
case GPGME_EDITKEY_NRLSIGN: |
200 |
|
|
case GPGME_EDITKEY_LSIGN: |
201 |
|
|
break; |
202 |
|
|
default: |
203 |
|
|
return mk_error (Invalid_Mode); |
204 |
|
|
} |
205 |
|
|
ctx->type = sigtype; |
206 |
|
|
ctx->u.sign.passwd = passwd; |
207 |
|
|
ctx->u.sign.sig_class = sigclass; |
208 |
|
|
ctx->u.sign.exp_date = param; |
209 |
|
|
|
210 |
|
|
return 0; |
211 |
|
|
} /* gpgme_editkey_sign_set */ |
212 |
|
|
|
213 |
|
|
/** |
214 |
|
|
* ADDKEY |
215 |
|
|
**/ |
216 |
|
|
gpgme_error_t |
217 |
|
|
gpgme_editkey_addkey_set (gpgme_editkey_t ctx, const char *passwd, |
218 |
|
|
int algo, int size, int valid) |
219 |
|
|
{ |
220 |
|
|
if (!ctx) |
221 |
|
|
return mk_error (Invalid_Value); |
222 |
|
|
if (algo < 2 || algo > 6) |
223 |
|
|
return mk_error (Invalid_Mode); |
224 |
|
|
if (algo == 2 && size > 1024) |
225 |
|
|
size = 1024; |
226 |
|
|
if (size > 4096) |
227 |
|
|
size = 4096; |
228 |
|
|
|
229 |
|
|
ctx->u.addkey.algo = algo; |
230 |
|
|
ctx->u.addkey.size = size; |
231 |
|
|
ctx->u.addkey.valid = valid; |
232 |
|
|
ctx->u.addkey.passwd = passwd; |
233 |
|
|
ctx->type = GPGME_EDITKEY_ADDKEY; |
234 |
|
|
|
235 |
|
|
return 0; |
236 |
|
|
} /* gpgme_editkey_addkey_set */ |
237 |
|
|
|
238 |
|
|
|
239 |
|
|
/** |
240 |
|
|
* PASSWD |
241 |
|
|
**/ |
242 |
|
|
void |
243 |
|
|
gpgme_editkey_passwd_set( gpgme_editkey_t ctx, const char * old_passwd, |
244 |
|
|
const char * new_passwd, int allow_empty ) |
245 |
|
|
{ |
246 |
|
|
if( !ctx ) |
247 |
|
|
return; |
248 |
|
|
if( old_passwd && new_passwd ) { |
249 |
|
|
ctx->u.passwd.old_passwd = old_passwd; |
250 |
|
|
ctx->u.passwd.new_passwd = new_passwd; |
251 |
|
|
} |
252 |
|
|
else if( !old_passwd && new_passwd ) { |
253 |
|
|
/* this is the case when the key was unprotected before. then |
254 |
|
|
GPG only asks once for a passphrase. */ |
255 |
|
|
ctx->u.passwd.old_passwd = new_passwd; |
256 |
|
|
ctx->u.passwd.new_passwd = NULL; |
257 |
|
|
} |
258 |
|
|
ctx->u.passwd.allow_empty = allow_empty; |
259 |
|
|
ctx->u.passwd.send_old = 0; |
260 |
|
|
ctx->type = GPGME_EDITKEY_PASSWD; |
261 |
|
|
} /* gpgme_editkey_passwd_set */ |
262 |
|
|
|
263 |
|
|
|
264 |
|
|
/** |
265 |
|
|
* PRIMARY |
266 |
|
|
**/ |
267 |
|
|
void |
268 |
|
|
gpgme_editkey_primary_set( gpgme_editkey_t ctx, int id, const char * passwd ) |
269 |
|
|
{ |
270 |
|
|
if( !ctx ) |
271 |
|
|
return; |
272 |
|
|
|
273 |
|
|
ctx->u.primary.id = id; |
274 |
|
|
ctx->u.primary.passwd = passwd; |
275 |
|
|
ctx->type = GPGME_EDITKEY_PRIMARY; |
276 |
|
|
} /* gpgme_editkey_primary_set */ |
277 |
|
|
|
278 |
|
|
|
279 |
|
|
/** |
280 |
|
|
* EXPIRE |
281 |
|
|
**/ |
282 |
|
|
void |
283 |
|
|
gpgme_editkey_expire_set( gpgme_editkey_t ctx, int id, int days, const char *date, |
284 |
|
|
const char *passwd ) |
285 |
|
|
{ |
286 |
|
|
if( !ctx ) |
287 |
|
|
return; |
288 |
|
|
ctx->u.expire.id = id; |
289 |
|
|
if( days && !date ) |
290 |
|
|
ctx->u.expire.days = days; |
291 |
|
|
else if( date && !days ) { |
292 |
|
|
ctx->u.expire.date = date; |
293 |
|
|
ctx->u.expire.days = 0; |
294 |
|
|
} |
295 |
|
|
ctx->u.expire.passwd = passwd; |
296 |
|
|
ctx->type = GPGME_EDITKEY_EXPIRE; |
297 |
|
|
} /* gpgme_editkey_expire_set */ |
298 |
|
|
|
299 |
|
|
|
300 |
|
|
/** |
301 |
|
|
* REVSIG |
302 |
|
|
**/ |
303 |
|
|
void |
304 |
|
|
gpgme_editkey_revsig_set( gpgme_editkey_t ctx, int id, const char *passwd ) |
305 |
|
|
{ |
306 |
|
|
if( !ctx ) |
307 |
|
|
return; |
308 |
|
|
ctx->u.revsig.id = id; |
309 |
|
|
ctx->u.revsig.passwd = passwd; |
310 |
|
|
ctx->type = GPGME_EDITKEY_REVSIG; |
311 |
|
|
} /* gpgme_editkey_revsig_set */ |
312 |
|
|
|
313 |
|
|
|
314 |
|
|
/** |
315 |
|
|
* REVKEY |
316 |
|
|
**/ |
317 |
|
|
void |
318 |
|
|
gpgme_editkey_revkey_set( gpgme_editkey_t ctx, int id, int reason, |
319 |
|
|
const char * passwd ) |
320 |
|
|
{ |
321 |
|
|
if( !ctx ) |
322 |
|
|
return; |
323 |
|
|
ctx->u.revkey.id = id; |
324 |
|
|
ctx->u.revkey.reason = reason; |
325 |
|
|
ctx->u.revkey.passwd = passwd; |
326 |
|
|
ctx->type = GPGME_EDITKEY_REVKEY; |
327 |
|
|
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
|
|
* DELSIG |
331 |
|
|
**/ |
332 |
|
|
void |
333 |
|
|
gpgme_editkey_delsig_set (gpgme_editkey_t ctx, int uid, int signo) |
334 |
|
|
{ |
335 |
|
|
if (!ctx) |
336 |
|
|
return; |
337 |
|
|
ctx->u.delsig.currno = 0; |
338 |
|
|
ctx->u.delsig.signo = signo; |
339 |
|
|
ctx->u.delsig.uid = uid; |
340 |
|
|
ctx->type = GPGME_EDITKEY_DELSIG; |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
|
344 |
|
|
/** |
345 |
|
|
* ENABLE |
346 |
|
|
**/ |
347 |
|
|
void |
348 |
|
|
gpgme_editkey_enable_set( gpgme_editkey_t ctx ) |
349 |
|
|
{ |
350 |
|
|
if( ctx ) |
351 |
|
|
ctx->type = GPGME_EDITKEY_ENABLE; |
352 |
|
|
} |
353 |
|
|
|
354 |
|
|
|
355 |
|
|
/** |
356 |
|
|
* DISABLE |
357 |
|
|
**/ |
358 |
|
|
void |
359 |
|
|
gpgme_editkey_disable_set( gpgme_editkey_t ctx ) |
360 |
|
|
{ |
361 |
|
|
if( ctx ) |
362 |
|
|
ctx->type = GPGME_EDITKEY_DISABLE; |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
|
366 |
|
|
/** |
367 |
|
|
* SETPREF |
368 |
|
|
**/ |
369 |
|
|
void |
370 |
|
|
gpgme_editkey_setpref_set (gpgme_editkey_t ctx, const char * new_prefs, |
371 |
|
|
int uid_idx, const char * passwd) |
372 |
|
|
{ |
373 |
|
|
if (!ctx) |
374 |
|
|
return; |
375 |
|
|
ctx->u.pref.id = 0; |
376 |
|
|
ctx->u.pref.uid_idx = uid_idx; |
377 |
|
|
ctx->u.pref.passwd = passwd; |
378 |
|
|
ctx->u.pref.new_prefs = new_prefs; |
379 |
|
|
ctx->type = GPGME_EDITKEY_SETPREF; |
380 |
|
|
} |
381 |
|
|
|
382 |
|
|
|
383 |
|
|
/** |
384 |
|
|
* KEYSERVER |
385 |
|
|
**/ |
386 |
|
|
void |
387 |
|
|
gpgme_editkey_keyserver_set (gpgme_editkey_t ctx, const char *url, |
388 |
|
|
const char * passwd) |
389 |
|
|
{ |
390 |
|
|
if (!ctx) |
391 |
|
|
return; |
392 |
|
|
ctx->u.keyserv.url = url; |
393 |
|
|
ctx->u.keyserv.passwd = passwd; |
394 |
|
|
ctx->type = GPGME_EDITKEY_KEYSERV; |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
void |
398 |
|
|
gpgme_editkey_make_invalid( gpgme_editkey_t ctx ) |
399 |
|
|
{ |
400 |
|
|
if( !ctx ) |
401 |
|
|
return; |
402 |
|
|
ctx->type = 0; |
403 |
|
|
} /* gpgme_editkey_make_invalid */ |
404 |
|
|
|
405 |
|
|
|
406 |
|
|
int |
407 |
|
|
gpgme_editkey_is_valid( gpgme_editkey_t ctx ) |
408 |
|
|
{ |
409 |
|
|
return ctx && ctx->type > 0 ? 1 : 0; |
410 |
|
|
} /* gpgme_editkey_is_valid */ |
411 |
|
|
|
412 |
|
|
|
413 |
|
|
int |
414 |
|
|
gpgme_editkey_is_secret( gpgme_editkey_t ctx, int val ) |
415 |
|
|
{ |
416 |
|
|
if( ctx && val ) { |
417 |
|
|
ctx->key_pair = val; |
418 |
|
|
return 0; |
419 |
|
|
} |
420 |
|
|
return ctx && ctx->key_pair; |
421 |
|
|
} /* gpgme_editkey_is_secret */ |
422 |
|
|
#endif /* WITH_EDITKEY */ |