2004-02-25 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / export.c
blob43d1b21edc3e8f77e123bfdbe43929c9ee902d55
1 /* export.c
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
3 * 2003 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
29 #include "options.h"
30 #include "packet.h"
31 #include "errors.h"
32 #include "keydb.h"
33 #include "memory.h"
34 #include "util.h"
35 #include "main.h"
36 #include "i18n.h"
38 static int do_export( STRLIST users, int secret, unsigned int options );
39 static int do_export_stream( iobuf_t out, STRLIST users, int secret,
40 KBNODE *keyblock_out, unsigned int options,
41 int *any );
42 static int build_sexp (iobuf_t out, PACKET *pkt, int *indent);
44 int
45 parse_export_options(char *str,unsigned int *options)
47 struct parse_options export_opts[]=
49 {"include-non-rfc",EXPORT_INCLUDE_NON_RFC},
50 {"include-local-sigs",EXPORT_INCLUDE_LOCAL_SIGS},
51 {"include-attributes",EXPORT_INCLUDE_ATTRIBUTES},
52 {"include-sensitive-revkeys",EXPORT_INCLUDE_SENSITIVE_REVKEYS},
53 {"sexp-format",EXPORT_SEXP_FORMAT},
54 {NULL,0}
55 /* add tags for include revoked and disabled? */
58 return parse_options(str,options,export_opts);
61 /****************
62 * Export the public keys (to standard out or --output).
63 * Depending on opt.armor the output is armored.
64 * options are defined in main.h.
65 * If USERS is NULL, the complete ring will be exported. */
66 int
67 export_pubkeys( STRLIST users, unsigned int options )
69 return do_export( users, 0, options );
72 /****************
73 * Export to an already opened stream; return -1 if no keys have
74 * been exported
76 int
77 export_pubkeys_stream( iobuf_t out, STRLIST users,
78 KBNODE *keyblock_out, unsigned int options )
80 int any, rc;
82 rc = do_export_stream( out, users, 0, keyblock_out, options, &any );
83 if( !rc && !any )
84 rc = -1;
85 return rc;
88 int
89 export_seckeys( STRLIST users )
91 /* Use only relevant options for the secret key. */
92 unsigned int options = (opt.export_options & EXPORT_SEXP_FORMAT);
93 return do_export (users, 1, options);
96 int
97 export_secsubkeys( STRLIST users )
99 /* Use only relevant options for the secret key. */
100 unsigned int options = (opt.export_options & EXPORT_SEXP_FORMAT);
101 return do_export( users, 2, options);
104 static int
105 do_export (STRLIST users, int secret, unsigned int options)
107 iobuf_t out = NULL;
108 int any, rc;
109 armor_filter_context_t afx;
110 compress_filter_context_t zfx;
112 memset (&afx, 0, sizeof afx);
113 memset (&zfx, 0, sizeof zfx);
115 rc = open_outfile (NULL, 0, &out);
116 if (rc)
117 return rc;
119 if (!(options & EXPORT_SEXP_FORMAT))
121 if (opt.armor)
123 afx.what = secret?5:1;
124 iobuf_push_filter( out, armor_filter, &afx );
126 if (opt.compress_keys && opt.compress)
127 iobuf_push_filter( out, compress_filter, &zfx );
129 rc = do_export_stream (out, users, secret, NULL, options, &any );
131 if (rc || !any)
132 iobuf_cancel (out);
133 else
134 iobuf_close (out);
135 return rc;
139 /* If keyblock_out is non-NULL, AND the exit code is zero, then it
140 contains a pointer to the first keyblock found and exported. No
141 other keyblocks are exported. The caller must free it. */
142 static int
143 do_export_stream( iobuf_t out, STRLIST users, int secret,
144 KBNODE *keyblock_out, unsigned int options, int *any )
146 int rc = 0;
147 PACKET pkt;
148 KBNODE keyblock = NULL;
149 KBNODE kbctx, node;
150 size_t ndesc, descindex;
151 KEYDB_SEARCH_DESC *desc = NULL;
152 KEYDB_HANDLE kdbhd;
153 STRLIST sl;
154 int indent = 0;
156 *any = 0;
157 init_packet( &pkt );
158 kdbhd = keydb_new (secret);
160 if (!users) {
161 ndesc = 1;
162 desc = xcalloc (1, ndesc * sizeof *desc);
163 desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
165 else {
166 for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++)
168 desc = xmalloc ( ndesc * sizeof *desc);
170 for (ndesc=0, sl=users; sl; sl = sl->next) {
171 if (classify_user_id (sl->d, desc+ndesc))
172 ndesc++;
173 else
174 log_error (_("key `%s' not found: %s\n"),
175 sl->d, gpg_strerror (GPG_ERR_INV_USER_ID));
178 /* it would be nice to see which of the given users did
179 actually match one in the keyring. To implement this we
180 need to have a found flag for each entry in desc and to set
181 this we must check all those entries after a match to mark
182 all matched one - currently we stop at the first match. To
183 do this we need an extra flag to enable this feature so */
186 while (!(rc = keydb_search2 (kdbhd, desc, ndesc, &descindex))) {
187 int sha1_warned=0,skip_until_subkey=0;
188 u32 sk_keyid[2];
190 if (!users)
191 desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
193 /* read the keyblock */
194 rc = keydb_get_keyblock (kdbhd, &keyblock );
195 if( rc ) {
196 log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
197 goto leave;
200 /* do not export keys which are incompatible with rfc2440 */
201 if( !(options&EXPORT_INCLUDE_NON_RFC) &&
202 (node = find_kbnode( keyblock, PKT_PUBLIC_KEY )) ) {
203 PKT_public_key *pk = node->pkt->pkt.public_key;
204 if( pk->version == 3 && pk->pubkey_algo > 3 ) {
205 log_info(_("key %08lX: not a rfc2440 key - skipped\n"),
206 (ulong)keyid_from_pk( pk, NULL) );
207 continue;
211 node=find_kbnode( keyblock, PKT_SECRET_KEY );
212 if(node)
214 PKT_secret_key *sk=node->pkt->pkt.secret_key;
216 keyid_from_sk(sk,sk_keyid);
218 /* we can't apply GNU mode 1001 on an unprotected key */
219 if( secret == 2 && !sk->is_protected )
221 log_info(_("key %08lX: not protected - skipped\n"),
222 (ulong)sk_keyid[1]);
223 continue;
226 /* no v3 keys with GNU mode 1001 */
227 if( secret == 2 && sk->version == 3 )
229 log_info(_("key %08lX: PGP 2.x style key - skipped\n"),
230 (ulong)sk_keyid[1]);
231 continue;
235 /* and write it */
236 for( kbctx=NULL; (node = walk_kbnode( keyblock, &kbctx, 0 )); ) {
237 if( skip_until_subkey )
239 if(node->pkt->pkttype==PKT_PUBLIC_SUBKEY
240 || node->pkt->pkttype==PKT_SECRET_SUBKEY)
241 skip_until_subkey=0;
242 else
243 continue;
246 /* don't export any comment packets but those in the
247 * secret keyring */
248 if( !secret && node->pkt->pkttype == PKT_COMMENT )
249 continue;
251 /* make sure that ring_trust packets never get exported */
252 if (node->pkt->pkttype == PKT_RING_TRUST)
253 continue;
255 /* If exact is set, then we only export what was requested
256 (plus the primary key, if the user didn't specifically
257 request it) */
258 if(desc[descindex].exact
259 && (node->pkt->pkttype==PKT_PUBLIC_SUBKEY
260 || node->pkt->pkttype==PKT_SECRET_SUBKEY))
262 u32 kid[2];
263 byte fpr[MAX_FINGERPRINT_LEN];
264 size_t fprlen;
266 switch(desc[descindex].mode)
268 case KEYDB_SEARCH_MODE_SHORT_KID:
269 case KEYDB_SEARCH_MODE_LONG_KID:
270 if(node->pkt->pkttype==PKT_PUBLIC_SUBKEY)
271 keyid_from_pk(node->pkt->pkt.public_key,kid);
272 else
273 keyid_from_sk(node->pkt->pkt.secret_key,kid);
274 break;
276 case KEYDB_SEARCH_MODE_FPR16:
277 case KEYDB_SEARCH_MODE_FPR20:
278 case KEYDB_SEARCH_MODE_FPR:
279 if(node->pkt->pkttype==PKT_PUBLIC_SUBKEY)
280 fingerprint_from_pk(node->pkt->pkt.public_key,
281 fpr,&fprlen);
282 else
283 fingerprint_from_sk(node->pkt->pkt.secret_key,
284 fpr,&fprlen);
285 break;
287 default:
288 break;
291 switch(desc[descindex].mode)
293 case KEYDB_SEARCH_MODE_SHORT_KID:
294 if (desc[descindex].u.kid[1] != kid[1])
295 skip_until_subkey=1;
296 break;
297 case KEYDB_SEARCH_MODE_LONG_KID:
298 if (desc[descindex].u.kid[0] != kid[0]
299 || desc[descindex].u.kid[1] != kid[1])
300 skip_until_subkey=1;
301 break;
302 case KEYDB_SEARCH_MODE_FPR16:
303 if (memcmp (desc[descindex].u.fpr, fpr, 16))
304 skip_until_subkey=1;
305 break;
306 case KEYDB_SEARCH_MODE_FPR20:
307 case KEYDB_SEARCH_MODE_FPR:
308 if (memcmp (desc[descindex].u.fpr, fpr, 20))
309 skip_until_subkey=1;
310 break;
311 default:
312 break;
315 if(skip_until_subkey)
316 continue;
319 if( node->pkt->pkttype == PKT_SIGNATURE ) {
320 /* do not export packets which are marked as not exportable */
321 if( !(options&EXPORT_INCLUDE_LOCAL_SIGS) &&
322 !node->pkt->pkt.signature->flags.exportable )
323 continue; /* not exportable */
325 /* Do not export packets with a "sensitive" revocation
326 key unless the user wants us to. Note that we do
327 export these when issuing the actual revocation (see
328 revoke.c). */
329 if( !(options&EXPORT_INCLUDE_SENSITIVE_REVKEYS) &&
330 node->pkt->pkt.signature->revkey ) {
331 int i;
333 for(i=0;i<node->pkt->pkt.signature->numrevkeys;i++)
334 if(node->pkt->pkt.signature->revkey[i]->class & 0x40)
335 break;
337 if(i<node->pkt->pkt.signature->numrevkeys)
338 continue;
342 /* Don't export attribs? */
343 if( !(options&EXPORT_INCLUDE_ATTRIBUTES) &&
344 node->pkt->pkttype == PKT_USER_ID &&
345 node->pkt->pkt.user_id->attrib_data ) {
346 /* Skip until we get to something that is not an attrib
347 or a signature on an attrib */
348 while(kbctx->next && kbctx->next->pkt->pkttype==PKT_SIGNATURE) {
349 kbctx=kbctx->next;
352 continue;
355 if( secret == 2 && node->pkt->pkttype == PKT_SECRET_KEY ) {
356 /* we don't want to export the secret parts of the
357 * primary key, this is done by using GNU protection mode 1001
359 int save_mode = node->pkt->pkt.secret_key->protect.s2k.mode;
360 node->pkt->pkt.secret_key->protect.s2k.mode = 1001;
361 if ((options&EXPORT_SEXP_FORMAT))
362 rc = build_sexp (out, node->pkt, &indent);
363 else
364 rc = build_packet (out, node->pkt);
365 node->pkt->pkt.secret_key->protect.s2k.mode = save_mode;
367 else {
368 /* Warn the user if the secret key or any of the secret
369 subkeys are protected with SHA1 and we have
370 simple_sk_checksum set. */
371 if(!sha1_warned && opt.simple_sk_checksum &&
372 (node->pkt->pkttype==PKT_SECRET_KEY ||
373 node->pkt->pkttype==PKT_SECRET_SUBKEY) &&
374 node->pkt->pkt.secret_key->protect.sha1chk)
376 /* I hope this warning doesn't confuse people. */
377 log_info(_("WARNING: secret key %08lX does not have a "
378 "simple SK checksum\n"),(ulong)sk_keyid[1]);
380 sha1_warned=1;
383 if ((options&EXPORT_SEXP_FORMAT))
384 rc = build_sexp (out, node->pkt, &indent);
385 else
386 rc = build_packet (out, node->pkt);
389 if( rc ) {
390 log_error("build_packet(%d) failed: %s\n",
391 node->pkt->pkttype, gpg_strerror (rc) );
392 goto leave;
395 if ((options&EXPORT_SEXP_FORMAT) && indent)
397 for (; indent; indent--)
398 iobuf_put (out, ')');
399 iobuf_put (out, '\n');
402 ++*any;
403 if(keyblock_out)
405 *keyblock_out=keyblock;
406 break;
409 if ((options&EXPORT_SEXP_FORMAT) && indent)
411 for (; indent; indent--)
412 iobuf_put (out, ')');
413 iobuf_put (out, '\n');
415 if( rc == -1 )
416 rc = 0;
418 leave:
419 xfree (desc);
420 keydb_release (kdbhd);
421 if(rc || keyblock_out==NULL)
422 release_kbnode( keyblock );
423 if( !*any )
424 log_info(_("WARNING: nothing exported\n"));
425 return rc;
429 static int
430 write_sexp_line (iobuf_t out, int *indent, const char *text)
432 int i;
434 for (i=0; i < *indent; i++)
435 iobuf_put (out, ' ');
436 iobuf_writestr (out, text);
437 return 0;
440 static int
441 write_sexp_keyparm (iobuf_t out, int *indent, const char *name, gcry_mpi_t a)
443 int rc;
444 unsigned char *buffer;
446 write_sexp_line (out, indent, "(");
447 iobuf_writestr (out, name);
448 iobuf_writestr (out, " #");
450 rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buffer, NULL, a);
451 assert (!rc);
452 iobuf_writestr (out, buffer);
453 iobuf_writestr (out, "#)");
454 gcry_free (buffer);
455 return 0;
458 static int
459 build_sexp_seckey (iobuf_t out, PACKET *pkt, int *indent)
461 PKT_secret_key *sk = pkt->pkt.secret_key;
462 char tmpbuf[100];
464 if (pkt->pkttype == PKT_SECRET_KEY)
466 iobuf_writestr (out, "(openpgp-key\n");
467 (*indent)++;
469 else
471 iobuf_writestr (out, " (subkey\n");
472 (*indent)++;
474 (*indent)++;
475 write_sexp_line (out, indent, "(private-key\n");
476 (*indent)++;
477 if (is_RSA (sk->pubkey_algo) && !sk->is_protected)
479 write_sexp_line (out, indent, "(rsa\n");
480 (*indent)++;
481 write_sexp_keyparm (out, indent, "n", sk->skey[0]); iobuf_put (out,'\n');
482 write_sexp_keyparm (out, indent, "e", sk->skey[1]); iobuf_put (out,'\n');
483 write_sexp_keyparm (out, indent, "d", sk->skey[2]); iobuf_put (out,'\n');
484 write_sexp_keyparm (out, indent, "p", sk->skey[3]); iobuf_put (out,'\n');
485 write_sexp_keyparm (out, indent, "q", sk->skey[4]); iobuf_put (out,'\n');
486 write_sexp_keyparm (out, indent, "u", sk->skey[5]);
487 iobuf_put (out,')'); iobuf_put (out,'\n');
488 (*indent)--;
490 else if (sk->pubkey_algo == PUBKEY_ALGO_DSA && !sk->is_protected)
492 write_sexp_line (out, indent, "(dsa\n");
493 (*indent)++;
494 write_sexp_keyparm (out, indent, "p", sk->skey[0]); iobuf_put (out,'\n');
495 write_sexp_keyparm (out, indent, "q", sk->skey[1]); iobuf_put (out,'\n');
496 write_sexp_keyparm (out, indent, "g", sk->skey[2]); iobuf_put (out,'\n');
497 write_sexp_keyparm (out, indent, "y", sk->skey[3]); iobuf_put (out,'\n');
498 write_sexp_keyparm (out, indent, "x", sk->skey[4]);
499 iobuf_put (out,')'); iobuf_put (out,'\n');
500 (*indent)--;
502 else if (is_ELGAMAL (sk->pubkey_algo) && !sk->is_protected)
504 write_sexp_line (out, indent, "(elg\n");
505 (*indent)++;
506 write_sexp_keyparm (out, indent, "p", sk->skey[0]); iobuf_put (out,'\n');
507 write_sexp_keyparm (out, indent, "g", sk->skey[2]); iobuf_put (out,'\n');
508 write_sexp_keyparm (out, indent, "y", sk->skey[3]); iobuf_put (out,'\n');
509 write_sexp_keyparm (out, indent, "x", sk->skey[4]);
510 iobuf_put (out,')'); iobuf_put (out,'\n');
511 (*indent)--;
513 write_sexp_line (out, indent, "(attrib\n"); (*indent)++;
514 sprintf (tmpbuf, "(created \"%lu\"", (unsigned long)sk->timestamp);
515 write_sexp_line (out, indent, tmpbuf);
516 iobuf_put (out,')'); (*indent)--; /* close created */
517 iobuf_put (out,')'); (*indent)--; /* close attrib */
518 iobuf_put (out,')'); (*indent)--; /* close private-key */
519 if (pkt->pkttype != PKT_SECRET_KEY)
520 iobuf_put (out,')'), (*indent)--; /* close subkey */
521 iobuf_put (out,'\n');
523 return 0;
527 /* For some packet types we write them in a S-Exp like format. This is
528 still EXPERIMENTAL and subject to change. */
529 static int
530 build_sexp (iobuf_t out, PACKET *pkt, int *indent)
532 int rc;
534 switch (pkt->pkttype)
536 case PKT_SECRET_KEY:
537 case PKT_SECRET_SUBKEY:
538 rc = build_sexp_seckey (out, pkt, indent);
539 break;
540 default:
541 rc = 0;
542 break;
544 return rc;