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
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
,
42 static int build_sexp (iobuf_t out
, PACKET
*pkt
, int *indent
);
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
},
55 /* add tags for include revoked and disabled? */
58 return parse_options(str
,options
,export_opts
);
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. */
67 export_pubkeys( STRLIST users
, unsigned int options
)
69 return do_export( users
, 0, options
);
73 * Export to an already opened stream; return -1 if no keys have
77 export_pubkeys_stream( iobuf_t out
, STRLIST users
,
78 KBNODE
*keyblock_out
, unsigned int options
)
82 rc
= do_export_stream( out
, users
, 0, keyblock_out
, options
, &any
);
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
);
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
);
105 do_export (STRLIST users
, int secret
, unsigned int options
)
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
);
119 if (!(options
& EXPORT_SEXP_FORMAT
))
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
);
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. */
143 do_export_stream( iobuf_t out
, STRLIST users
, int secret
,
144 KBNODE
*keyblock_out
, unsigned int options
, int *any
)
148 KBNODE keyblock
= NULL
;
150 size_t ndesc
, descindex
;
151 KEYDB_SEARCH_DESC
*desc
= NULL
;
158 kdbhd
= keydb_new (secret
);
162 desc
= xcalloc (1, ndesc
* sizeof *desc
);
163 desc
[0].mode
= KEYDB_SEARCH_MODE_FIRST
;
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
))
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;
191 desc
[0].mode
= KEYDB_SEARCH_MODE_NEXT
;
193 /* read the keyblock */
194 rc
= keydb_get_keyblock (kdbhd
, &keyblock
);
196 log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc
) );
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
) );
211 node
=find_kbnode( keyblock
, PKT_SECRET_KEY
);
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"),
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"),
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
)
246 /* don't export any comment packets but those in the
248 if( !secret
&& node
->pkt
->pkttype
== PKT_COMMENT
)
251 /* make sure that ring_trust packets never get exported */
252 if (node
->pkt
->pkttype
== PKT_RING_TRUST
)
255 /* If exact is set, then we only export what was requested
256 (plus the primary key, if the user didn't specifically
258 if(desc
[descindex
].exact
259 && (node
->pkt
->pkttype
==PKT_PUBLIC_SUBKEY
260 || node
->pkt
->pkttype
==PKT_SECRET_SUBKEY
))
263 byte fpr
[MAX_FINGERPRINT_LEN
];
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
);
273 keyid_from_sk(node
->pkt
->pkt
.secret_key
,kid
);
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
,
283 fingerprint_from_sk(node
->pkt
->pkt
.secret_key
,
291 switch(desc
[descindex
].mode
)
293 case KEYDB_SEARCH_MODE_SHORT_KID
:
294 if (desc
[descindex
].u
.kid
[1] != kid
[1])
297 case KEYDB_SEARCH_MODE_LONG_KID
:
298 if (desc
[descindex
].u
.kid
[0] != kid
[0]
299 || desc
[descindex
].u
.kid
[1] != kid
[1])
302 case KEYDB_SEARCH_MODE_FPR16
:
303 if (memcmp (desc
[descindex
].u
.fpr
, fpr
, 16))
306 case KEYDB_SEARCH_MODE_FPR20
:
307 case KEYDB_SEARCH_MODE_FPR
:
308 if (memcmp (desc
[descindex
].u
.fpr
, fpr
, 20))
315 if(skip_until_subkey
)
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
329 if( !(options
&EXPORT_INCLUDE_SENSITIVE_REVKEYS
) &&
330 node
->pkt
->pkt
.signature
->revkey
) {
333 for(i
=0;i
<node
->pkt
->pkt
.signature
->numrevkeys
;i
++)
334 if(node
->pkt
->pkt
.signature
->revkey
[i
]->class & 0x40)
337 if(i
<node
->pkt
->pkt
.signature
->numrevkeys
)
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
) {
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
);
364 rc
= build_packet (out
, node
->pkt
);
365 node
->pkt
->pkt
.secret_key
->protect
.s2k
.mode
= save_mode
;
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]);
383 if ((options
&EXPORT_SEXP_FORMAT
))
384 rc
= build_sexp (out
, node
->pkt
, &indent
);
386 rc
= build_packet (out
, node
->pkt
);
390 log_error("build_packet(%d) failed: %s\n",
391 node
->pkt
->pkttype
, gpg_strerror (rc
) );
395 if ((options
&EXPORT_SEXP_FORMAT
) && indent
)
397 for (; indent
; indent
--)
398 iobuf_put (out
, ')');
399 iobuf_put (out
, '\n');
405 *keyblock_out
=keyblock
;
409 if ((options
&EXPORT_SEXP_FORMAT
) && indent
)
411 for (; indent
; indent
--)
412 iobuf_put (out
, ')');
413 iobuf_put (out
, '\n');
420 keydb_release (kdbhd
);
421 if(rc
|| keyblock_out
==NULL
)
422 release_kbnode( keyblock
);
424 log_info(_("WARNING: nothing exported\n"));
430 write_sexp_line (iobuf_t out
, int *indent
, const char *text
)
434 for (i
=0; i
< *indent
; i
++)
435 iobuf_put (out
, ' ');
436 iobuf_writestr (out
, text
);
441 write_sexp_keyparm (iobuf_t out
, int *indent
, const char *name
, gcry_mpi_t a
)
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
);
452 iobuf_writestr (out
, buffer
);
453 iobuf_writestr (out
, "#)");
459 build_sexp_seckey (iobuf_t out
, PACKET
*pkt
, int *indent
)
461 PKT_secret_key
*sk
= pkt
->pkt
.secret_key
;
464 if (pkt
->pkttype
== PKT_SECRET_KEY
)
466 iobuf_writestr (out
, "(openpgp-key\n");
471 iobuf_writestr (out
, " (subkey\n");
475 write_sexp_line (out
, indent
, "(private-key\n");
477 if (is_RSA (sk
->pubkey_algo
) && !sk
->is_protected
)
479 write_sexp_line (out
, indent
, "(rsa\n");
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');
490 else if (sk
->pubkey_algo
== PUBKEY_ALGO_DSA
&& !sk
->is_protected
)
492 write_sexp_line (out
, indent
, "(dsa\n");
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');
502 else if (is_ELGAMAL (sk
->pubkey_algo
) && !sk
->is_protected
)
504 write_sexp_line (out
, indent
, "(elg\n");
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');
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');
527 /* For some packet types we write them in a S-Exp like format. This is
528 still EXPERIMENTAL and subject to change. */
530 build_sexp (iobuf_t out
, PACKET
*pkt
, int *indent
)
534 switch (pkt
->pkttype
)
537 case PKT_SECRET_SUBKEY
:
538 rc
= build_sexp_seckey (out
, pkt
, indent
);