1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-24 10:39:57 +01:00
gnupg/g10/export.c

237 lines
6.3 KiB
C
Raw Normal View History

1998-01-26 22:09:01 +00:00
/* export.c
2001-03-08 14:33:24 +00:00
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
1998-01-26 22:09:01 +00:00
*
* This file is part of GnuPG.
1998-01-26 22:09:01 +00:00
*
* GnuPG is free software; you can redistribute it and/or modify
1998-01-26 22:09:01 +00:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
1998-01-26 22:09:01 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "options.h"
#include "packet.h"
#include "errors.h"
#include "keydb.h"
#include "memory.h"
#include "util.h"
1998-02-13 20:58:50 +00:00
#include "main.h"
1998-11-10 12:59:59 +00:00
#include "i18n.h"
1998-01-26 22:09:01 +00:00
static int do_export( STRLIST users, int secret, int onlyrfc );
static int do_export_stream( IOBUF out, STRLIST users,
int secret, int onlyrfc, int *any );
1998-01-26 22:09:01 +00:00
/****************
1998-04-14 17:51:16 +00:00
* Export the public keys (to standard out or --output).
1998-02-13 20:58:50 +00:00
* Depending on opt.armor the output is armored.
* If onlyrfc is True only RFC24404 compatible keys are exported.
1998-04-14 17:51:16 +00:00
* If USERS is NULL, the complete ring will be exported.
1998-01-26 22:09:01 +00:00
*/
int
export_pubkeys( STRLIST users, int onlyrfc )
1998-06-29 12:30:57 +00:00
{
return do_export( users, 0, onlyrfc );
1998-06-29 12:30:57 +00:00
}
/****************
* Export to an already opened stream; return -1 if no keys have
* been exported
*/
int
export_pubkeys_stream( IOBUF out, STRLIST users, int onlyrfc )
{
int any, rc;
rc = do_export_stream( out, users, 0, onlyrfc, &any );
if( !rc && !any )
rc = -1;
return rc;
}
1998-06-29 12:30:57 +00:00
int
export_seckeys( STRLIST users )
{
return do_export( users, 1, 0 );
1998-06-29 12:30:57 +00:00
}
int
export_secsubkeys( STRLIST users )
{
return do_export( users, 2, 0 );
}
1998-06-29 12:30:57 +00:00
static int
do_export( STRLIST users, int secret, int onlyrfc )
1998-01-26 22:09:01 +00:00
{
IOBUF out = NULL;
int any, rc;
1998-02-13 20:58:50 +00:00
armor_filter_context_t afx;
compress_filter_context_t zfx;
memset( &afx, 0, sizeof afx);
memset( &zfx, 0, sizeof zfx);
rc = open_outfile( NULL, 0, &out );
if( rc )
return rc;
if( opt.armor ) {
afx.what = secret?5:1;
iobuf_push_filter( out, armor_filter, &afx );
}
if( opt.compress_keys && opt.compress )
iobuf_push_filter( out, compress_filter, &zfx );
rc = do_export_stream( out, users, secret, onlyrfc, &any );
if( rc || !any )
iobuf_cancel(out);
else
iobuf_close(out);
return rc;
}
static int
do_export_stream( IOBUF out, STRLIST users, int secret, int onlyrfc, int *any )
{
int rc = 0;
1998-02-13 20:58:50 +00:00
PACKET pkt;
KBNODE keyblock = NULL;
KBNODE kbctx, node;
2001-09-06 17:10:00 +00:00
KEYDB_HANDLE kdbhd;
1998-02-13 20:58:50 +00:00
STRLIST sl;
int all = !users;
2001-09-06 17:10:00 +00:00
int all_first = 1;
1998-02-13 20:58:50 +00:00
*any = 0;
1998-02-13 20:58:50 +00:00
init_packet( &pkt );
2001-09-06 17:10:00 +00:00
kdbhd = keydb_new (secret);
1998-04-14 17:51:16 +00:00
/* use the correct sequence. strlist_last,prev do work correctly with
1998-02-13 20:58:50 +00:00
* NULL pointers :-) */
for( sl=strlist_last(users); sl || all ; sl=strlist_prev( users, sl )) {
if( all ) { /* get the next user */
2001-09-06 17:10:00 +00:00
rc = all_first ? keydb_search_first (kdbhd)
: keydb_search_next (kdbhd);
all_first = 0;
1998-02-13 20:58:50 +00:00
if( rc == -1 ) /* EOF */
break;
if( rc ) {
2001-09-06 17:10:00 +00:00
log_error ("error searching key: %s\n", g10_errstr(rc));
1998-02-13 20:58:50 +00:00
break;
}
}
else {
2001-09-06 17:10:00 +00:00
KEYDB_SEARCH_DESC desc;
2001-09-07 07:57:51 +00:00
classify_user_id (sl->d, &desc);
2001-09-06 17:10:00 +00:00
rc = desc.mode? keydb_search (kdbhd, &desc, 1):G10ERR_INV_USER_ID;
1998-02-13 20:58:50 +00:00
if( rc ) {
2001-09-06 17:10:00 +00:00
log_error (_("key `%s' not found: %s\n"),
sl->d, g10_errstr (rc));
1998-02-13 20:58:50 +00:00
rc = 0;
continue;
}
}
2001-09-06 17:10:00 +00:00
/* read the keyblock */
rc = keydb_get_keyblock (kdbhd, &keyblock );
1998-02-13 20:58:50 +00:00
if( rc ) {
2001-09-06 17:10:00 +00:00
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
1998-02-13 20:58:50 +00:00
goto leave;
}
/* do not export keys which are incompatible with rfc2440 */
if( onlyrfc && (node = find_kbnode( keyblock, PKT_PUBLIC_KEY )) ) {
PKT_public_key *pk = node->pkt->pkt.public_key;
if( pk->version == 3 && pk->pubkey_algo > 3 ) {
log_info(_("key %08lX: not a rfc2440 key - skipped\n"),
(ulong)keyid_from_pk( pk, NULL) );
continue;
}
}
1998-02-13 20:58:50 +00:00
/* we can't apply GNU mode 1001 on an unprotected key */
if( secret == 2
&& (node = find_kbnode( keyblock, PKT_SECRET_KEY ))
&& !node->pkt->pkt.secret_key->is_protected )
{
log_info(_("key %08lX: not protected - skipped\n"),
(ulong)keyid_from_sk( node->pkt->pkt.secret_key, NULL) );
continue;
}
1998-02-13 20:58:50 +00:00
/* and write it */
for( kbctx=NULL; (node = walk_kbnode( keyblock, &kbctx, 0 )); ) {
1998-10-16 16:00:17 +00:00
/* don't export any comment packets but those in the
* secret keyring */
if( !secret && node->pkt->pkttype == PKT_COMMENT )
continue;
2001-09-25 15:20:59 +00:00
/* make sure that ring_trust packets never get exported */
if (node->pkt->pkttype == PKT_RING_TRUST)
continue;
1998-11-03 19:38:58 +00:00
/* do not export packets which are marked as not exportable */
if( node->pkt->pkttype == PKT_SIGNATURE ) {
const char *p;
p = parse_sig_subpkt2( node->pkt->pkt.signature,
SIGSUBPKT_EXPORTABLE, NULL );
if( p && !*p )
continue; /* not exportable */
/* delete our verification cache */
delete_sig_subpkt (node->pkt->pkt.signature->unhashed,
SIGSUBPKT_PRIV_VERIFY_CACHE);
1998-11-03 19:38:58 +00:00
}
if( secret == 2 && node->pkt->pkttype == PKT_SECRET_KEY ) {
/* we don't want to export the secret parts of the
* primary key, this is done by using GNU protection mode 1001
*/
int save_mode = node->pkt->pkt.secret_key->protect.s2k.mode;
node->pkt->pkt.secret_key->protect.s2k.mode = 1001;
rc = build_packet( out, node->pkt );
node->pkt->pkt.secret_key->protect.s2k.mode = save_mode;
}
else {
rc = build_packet( out, node->pkt );
}
if( rc ) {
1998-02-13 20:58:50 +00:00
log_error("build_packet(%d) failed: %s\n",
node->pkt->pkttype, g10_errstr(rc) );
rc = G10ERR_WRITE_FILE;
goto leave;
}
}
++*any;
1998-02-13 20:58:50 +00:00
}
if( rc == -1 )
rc = 0;
leave:
2001-09-06 17:10:00 +00:00
keydb_release (kdbhd);
1998-02-13 20:58:50 +00:00
release_kbnode( keyblock );
if( !*any )
1998-11-10 12:59:59 +00:00
log_info(_("WARNING: nothing exported\n"));
1998-02-13 20:58:50 +00:00
return rc;
1998-01-26 22:09:01 +00:00
}