Add key import and export facility

This commit is contained in:
Werner Koch 2000-12-14 14:45:35 +00:00
parent edcc338a59
commit 715cf2aed3
14 changed files with 514 additions and 10 deletions

View File

@ -23,6 +23,8 @@ libgpgme_la_SOURCES = \
sign.c \
key.c key.h \
keylist.c \
import.c \
export.c \
rungpg.c rungpg.h status-table.h \
syshdr.h io.h posix-io.c w32-io.c \
gpgme.c version.c errors.c

View File

@ -57,6 +57,7 @@ gpgme_op_encrypt_start ( GpgmeCtx c, GpgmeRecipients recp,
}
/* create a process object */
_gpgme_gpg_release (c->gpg); c->gpg = NULL;
rc = _gpgme_gpg_new ( &c->gpg );
if (rc)
goto leave;
@ -130,3 +131,6 @@ gpgme_op_encrypt ( GpgmeCtx c, GpgmeRecipients recp,

111
gpgme/export.c Normal file
View File

@ -0,0 +1,111 @@
/* export.c - encrypt functions
* Copyright (C) 2000 Werner Koch (dd9jn)
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify
* 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.
*
* GPGME is distributed in the hope that it will be useful,
* 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 <assert.h>
#include "util.h"
#include "context.h"
#include "ops.h"
static void
export_status_handler ( GpgmeCtx ctx, GpgStatusCode code, char *args )
{
fprintf (stderr, "export_status: code=%d args=`%s'\n",
code, args );
/* FIXME: Need to do more */
}
GpgmeError
gpgme_op_export_start ( GpgmeCtx c, GpgmeRecipients recp,
GpgmeData keydata )
{
int rc = 0;
int i;
fail_on_pending_request( c );
c->pending = 1;
/* create a process object */
_gpgme_gpg_release (c->gpg); c->gpg = NULL;
rc = _gpgme_gpg_new ( &c->gpg );
if (rc)
goto leave;
_gpgme_gpg_set_status_handler ( c->gpg, export_status_handler, c );
/* build the commandline */
_gpgme_gpg_add_arg ( c->gpg, "--export" );
if ( c->use_armor )
_gpgme_gpg_add_arg ( c->gpg, "--armor" );
for ( i=0; i < c->verbosity; i++ )
_gpgme_gpg_add_arg ( c->gpg, "--verbose" );
if ( !keydata || gpgme_data_get_type (keydata) != GPGME_DATA_TYPE_NONE ) {
rc = mk_error (Invalid_Value);
goto leave;
}
_gpgme_data_set_mode (keydata, GPGME_DATA_MODE_IN );
_gpgme_gpg_add_data ( c->gpg, keydata, 1 );
_gpgme_gpg_add_arg ( c->gpg, "--" );
{
void *ec;
const char *s;
rc = gpgme_recipients_enum_open ( recp, &ec );
if ( rc )
goto leave;
while ( (s = gpgme_recipients_enum_read ( recp, &ec )) )
_gpgme_gpg_add_arg (c->gpg, s);
rc = gpgme_recipients_enum_close ( recp, &ec );
if ( rc )
goto leave;
}
rc = _gpgme_gpg_spawn ( c->gpg, c );
leave:
if (rc) {
c->pending = 0;
_gpgme_gpg_release ( c->gpg ); c->gpg = NULL;
}
return rc;
}
GpgmeError
gpgme_op_export ( GpgmeCtx c, GpgmeRecipients recp, GpgmeData keydata )
{
int rc = gpgme_op_export_start ( c, recp, keydata );
if ( !rc ) {
gpgme_wait (c, 1);
c->pending = 0;
}
return rc;
}

View File

@ -65,7 +65,8 @@ gpgme_new (GpgmeCtx *r_ctx)
void
gpgme_release ( GpgmeCtx c )
{
if (!c)
return;
_gpgme_gpg_release ( c->gpg );
_gpgme_release_result ( c );
_gpgme_key_release ( c->tmp_key );

View File

@ -121,6 +121,10 @@ void gpgme_recipients_release ( GpgmeRecipients rset);
GpgmeError gpgme_recipients_add_name (GpgmeRecipients rset,
const char *name);
unsigned int gpgme_recipients_count ( const GpgmeRecipients rset );
GpgmeError gpgme_recipients_enum_open (const GpgmeRecipients rset,void **ctx);
const char *gpgme_recipients_enum_read (const GpgmeRecipients rset,void **ctx);
GpgmeError gpgme_recipients_enum_close (const GpgmeRecipients rset,void **ctx);
/* Functions to handle data sources */
GpgmeError gpgme_data_new ( GpgmeData *r_dh );
@ -156,6 +160,11 @@ GpgmeError gpgme_op_sign_start ( GpgmeCtx c,
GpgmeSigMode mode );
GpgmeError gpgme_op_verify_start ( GpgmeCtx c,
GpgmeData sig, GpgmeData text );
GpgmeError gpgme_op_import_start ( GpgmeCtx c, GpgmeData keydata );
GpgmeError gpgme_op_export_start ( GpgmeCtx c, GpgmeRecipients recp,
GpgmeData keydata );
/* Key management functions */
@ -173,6 +182,9 @@ GpgmeError gpgme_op_sign ( GpgmeCtx c, GpgmeData in, GpgmeData out,
GpgmeSigMode mode);
GpgmeError gpgme_op_verify ( GpgmeCtx c, GpgmeData sig, GpgmeData text,
GpgmeSigStat *r_status );
GpgmeError gpgme_op_import ( GpgmeCtx c, GpgmeData keydata );
GpgmeError gpgme_op_export ( GpgmeCtx c, GpgmeRecipients recp,
GpgmeData keydata );
/* miscellaneous functions */

98
gpgme/import.c Normal file
View File

@ -0,0 +1,98 @@
/* impoirt.c - encrypt functions
* Copyright (C) 2000 Werner Koch (dd9jn)
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify
* 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.
*
* GPGME is distributed in the hope that it will be useful,
* 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 <assert.h>
#include "util.h"
#include "context.h"
#include "ops.h"
static void
import_status_handler ( GpgmeCtx ctx, GpgStatusCode code, char *args )
{
fprintf (stderr, "import_status: code=%d args=`%s'\n",
code, args );
/* FIXME: We have to check here whether the import actually worked
* and maybe it is a good idea to save some statistics and provide
* a progress callback */
}
GpgmeError
gpgme_op_import_start ( GpgmeCtx c, GpgmeData keydata )
{
int rc = 0;
int i;
fail_on_pending_request( c );
c->pending = 1;
/* create a process object */
_gpgme_gpg_release (c->gpg); c->gpg = NULL;
rc = _gpgme_gpg_new ( &c->gpg );
if (rc)
goto leave;
_gpgme_gpg_set_status_handler ( c->gpg, import_status_handler, c );
/* build the commandline */
_gpgme_gpg_add_arg ( c->gpg, "--import" );
for ( i=0; i < c->verbosity; i++ )
_gpgme_gpg_add_arg ( c->gpg, "--verbose" );
/* Check the supplied data */
if ( gpgme_data_get_type (keydata) == GPGME_DATA_TYPE_NONE ) {
rc = mk_error (No_Data);
goto leave;
}
_gpgme_data_set_mode (keydata, GPGME_DATA_MODE_OUT );
_gpgme_gpg_add_data ( c->gpg, keydata, 0 );
rc = _gpgme_gpg_spawn ( c->gpg, c );
leave:
if (rc) {
c->pending = 0;
_gpgme_gpg_release ( c->gpg ); c->gpg = NULL;
}
return rc;
}
GpgmeError
gpgme_op_import ( GpgmeCtx c, GpgmeData keydata )
{
int rc = gpgme_op_import_start ( c, keydata );
if ( !rc ) {
gpgme_wait (c, 1);
c->pending = 0;
}
return rc;
}

View File

@ -77,6 +77,46 @@ gpgme_recipients_count ( const GpgmeRecipients rset )
}
GpgmeError
gpgme_recipients_enum_open ( const GpgmeRecipients rset, void **ctx )
{
if (!rset || !ctx)
return mk_error (Invalid_Value);
*ctx = rset->list;
return 0;
}
const char *
gpgme_recipients_enum_read ( const GpgmeRecipients rset, void **ctx )
{
struct user_id_s *r;
if (!rset || !ctx)
return NULL; /* oops */
r = *ctx;
if ( r ) {
const char *s = r->name;
r = r->next;
*ctx = r;
return s;
}
return NULL;
}
GpgmeError
gpgme_recipients_enum_close ( const GpgmeRecipients rset, void **ctx )
{
if (!rset || !ctx)
return mk_error (Invalid_Value);
*ctx = NULL;
return 0;
}
void
_gpgme_append_gpg_args_from_recipients (
const GpgmeRecipients rset,
@ -92,3 +132,6 @@ _gpgme_append_gpg_args_from_recipients (
}

View File

@ -222,7 +222,7 @@ _gpgme_io_spawn ( const char *path, char **argv,
memset (&si, 0, sizeof si);
si.cb = sizeof (si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = debug_me? SW_SHOW : SW_HIDE;
si.wShowWindow = debug_me? SW_SHOW : SW_MINIMIZE;
si.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle (STD_ERROR_HANDLE);

View File

@ -2,9 +2,10 @@
TESTS_ENVIRONMENT = GNUPGHOME=.
TESTS = t-encrypt t-sign t-decrypt t-verify t-keylist
TESTS = t-encrypt t-sign t-decrypt t-verify t-keylist t-export t-import
EXTRA_DIST = mkdemodirs pubdemo.asc secdemo.asc cipher-1.asc geheim.txt
EXTRA_DIST = mkdemodirs pubdemo.asc secdemo.asc cipher-1.asc geheim.txt \
pubkey-1.asc seckey-1.asc
INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl
@ -33,8 +34,3 @@ all-local: ./pubring.gpg ./secring.gpg

View File

@ -28,7 +28,7 @@ for name in $NAMES; do
[ -d $name ] && rm -r $name
mkdir $name
$GPGDEMO --export-secret-key -o - $name > $name/Secret.gpg
$GPG --homedir $name --import $name/Secret.gpg
$GPG --homedir $name --allow-secret-key-import --import $name/Secret.gpg
$GPGDEMO --export -o - $name > $name/Public.gpg
$GPG --homedir $name --import $name/Public.gpg
[ -f $name/pubring.gpg~ ] && rm $name/pubring.gpg~

26
tests/pubkey-1.asc Normal file
View File

@ -0,0 +1,26 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.0.4b (GNU/Linux)
Comment: For info see http://www.gnupg.org
mQGiBDo41NoRBADSfQazKGYf8nokq6zUKH/6INtV6MypSzSGmX2XErnARkIIPPYj
cQRQ8zCbGV7ZU2ezVbzhFLUSJ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EK+CJE+c2f1VVJoAn36uPWUhCdGXbSLxGibYfBt7et71AJ9JgWeRlTDTIoXYN8J+
qsPN0YCxtg==
=4+Yp
-----END PGP PUBLIC KEY BLOCK-----

30
tests/seckey-1.asc Normal file
View File

@ -0,0 +1,30 @@
-----BEGIN PGP PRIVATE KEY BLOCK-----
Version: GnuPG v1.0.4b (GNU/Linux)
Comment: For info see http://www.gnupg.org
lQHPBDo41NoRBADSfQazKGYf8nokq6zUKH/6INtV6MypSzSGmX2XErnARkIIPPYj
cQRQ8zCbGV7ZU2ezVbzhFLUSJveE8PZUzzCrLp1O2NSyBTRcR5HVSXW95nJfY8eV
pOvZRAKul0BVLh81kYTsrfzaaCjh9VWNP26LoeN2r+PjZyktXe7gM3C4SwCgoTxK
WUVi9HoT2HCLY7p7oig5hEcEALdCJal0UYomX3nJapIVLVZg3vkidr1RICYMb2vz
58i17h8sxEtobD1vdIKNejulntaRAXs4n0tDYD9z7pRlwG1CLz1R9WxYzeOOqUDr
fnVXdmU8L/oVWABat8v1V7QQhjMMf+41fuzVwDMMGqjVPLhu4X6wp3A8uyM3YDnQ
VMN1A/4n2G5gHoOvjqxn8Ch5tBAdMGfO8gH4RjQOwzm2R1wPQss/yzUN1+tlMZGX
K2dQ2FCWC/hDUSNaEQRlI15wxxBNZ2RQwlzE2A8v113DpvyzOtv0QO95gJ1teCXC
7j/BN9asgHaBBc39JLO/TcpuI7Hf8PQ5VcP2F0UE3lczGhXbLP8DAwKVpe92I5n5
JGBjXsTTnVLoJ1hrWTdbLvdbn882m5pHYeqFlvkqKYXJTf0mIzpEU0FfZmFjdG9y
OgAAr0JzPBwQoEmNI3YSC1MwimZ77bpvVKP9JiM6RFNBX2ZhY3RvcjoAAK9/fVBz
g73cYbgeNWbz2uITUwNd9KEN/SYjOkRTQV9mYWN0b3I6AACvWjjITYZwah6NiH6C
YgX52m55Dy5PX7Q/Sm9lIFJhbmRvbSBIYWNrZXIgKHRlc3Qga2V5IHdpdGggcGFz
c3BocmFzZSAieCIpIDxqb2VAc2V0cS5vcmc+iFcEExECABcFAjo41NoFCwcKAwQD
FQMCAxYCAQIXgAAKCRCvgiRPnNn9VXm9AKCFQ/t23GQnQEfnnAnvbRNfRo4zIQCb
BHwILsDBASB1rQzW68UA/XHze0WdAUYEOjjU3RAEAJ50lvtCGbnQlI97VX6tJkos
dPmdzeXaTWfv//A2wmSANbYnuychGMa1LN43Ew+H6FXMWJ3MB/exs6UBFCgGsw88
qmcla2bosQN/aVLA7fqXT9ujqoNGaIVEmgdbK1MkSPFXBFyVW3hteod83D0UqFll
twp4A3ageCYFVJTp50d3AAMFA/44YCQQbg9x9JvzHX3VH7CRX+raEDkDL3Pbz0PH
as7bwI7gzZ+GFyNKaCvrHQOyuR8RIKIbjtQYnXr1675ConCTceIXhysY32sTn5V6
UFUW2t0xaRfas8sZBbLDyIJkpt4fyD+6OaRoui9KZqXMNwt7i/XFIto/sWd/OK3S
IgZkAf8DAwKVpe92I5n5JGAHRuEKSSvGU+0my6zTf17bLWPpFPnICNJdaMfyx24Y
RZZa+nDpYrRznJ89vohGBBgRAgAGBQI6ONTeAAoJEK+CJE+c2f1V7iIAn0WsYyUV
Huz4ZZ/WxxN57Ku2Eqs9AJ9Klz9imzvZoUjuE9/Ihr0y56tVng==
=lKvj
-----END PGP PRIVATE KEY BLOCK-----

91
tests/t-export.c Normal file
View File

@ -0,0 +1,91 @@
/* t-export.c - regression test
* Copyright (C) 2000 Werner Koch (dd9jn)
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify
* 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.
*
* GPGME is distributed in the hope that it will be useful,
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "../gpgme/gpgme.h"
#define fail_if_err(a) do { if(a) { \
fprintf (stderr, "%s:%d: GpgmeError %s\n", \
__FILE__, __LINE__, gpgme_strerror(a)); \
exit (1); } \
} while(0)
static void
print_data ( GpgmeData dh )
{
char buf[100];
size_t nread;
GpgmeError err;
err = gpgme_data_rewind ( dh );
fail_if_err (err);
while ( !(err = gpgme_data_read ( dh, buf, 100, &nread )) ) {
fwrite ( buf, nread, 1, stdout );
}
if (err != GPGME_EOF)
fail_if_err (err);
}
int
main (int argc, char **argv )
{
GpgmeCtx ctx;
GpgmeError err;
GpgmeData out;
GpgmeRecipients rset;
do {
err = gpgme_new (&ctx);
fail_if_err (err);
err = gpgme_data_new ( &out );
fail_if_err (err);
err = gpgme_recipients_new (&rset);
fail_if_err (err);
err = gpgme_recipients_add_name (rset, "Bob");
fail_if_err (err);
err = gpgme_recipients_add_name (rset, "Alpha");
fail_if_err (err);
gpgme_set_armor (ctx, 1 );
err = gpgme_op_export (ctx, rset, out );
fail_if_err (err);
fflush (NULL);
fputs ("Begin Result:\n", stdout );
print_data (out);
fputs ("End Result.\n", stdout );
gpgme_recipients_release (rset);
gpgme_data_release (out);
gpgme_release (ctx);
} while ( argc > 1 && !strcmp( argv[1], "--loop" ) );
return 0;
}

90
tests/t-import.c Normal file
View File

@ -0,0 +1,90 @@
/* t-import.c - regression test
* Copyright (C) 2000 Werner Koch (dd9jn)
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify
* 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.
*
* GPGME is distributed in the hope that it will be useful,
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include "../gpgme/gpgme.h"
#define fail_if_err(a) do { if(a) { int my_errno = errno; \
fprintf (stderr, "%s:%d: GpgmeError %s\n", \
__FILE__, __LINE__, gpgme_strerror(a)); \
if ((a) == GPGME_File_Error) \
fprintf (stderr, "\terrno=`%s'\n", strerror (my_errno)); \
exit (1); } \
} while(0)
static char *
mk_fname ( const char *fname )
{
const char *srcdir = getenv ("srcdir");
char *buf;
if (!srcdir)
srcdir = ".";
buf = malloc (strlen(srcdir) + strlen(fname) + 2 );
if (!buf )
exit (8);
strcpy (buf, srcdir);
strcat (buf, "/");
strcat (buf, fname );
return buf;
}
int
main (int argc, char **argv )
{
GpgmeCtx ctx;
GpgmeError err;
GpgmeData in;
const char *pubkey_1_asc = mk_fname ("pubkey-1.asc");
const char *seckey_1_asc = mk_fname ("seckey-1.asc");
do {
err = gpgme_new (&ctx);
fail_if_err (err);
err = gpgme_data_new_from_file ( &in, pubkey_1_asc, 1 );
fail_if_err (err);
err = gpgme_op_import (ctx, in );
fail_if_err (err);
gpgme_data_release (in);
err = gpgme_data_new_from_file ( &in, seckey_1_asc, 1 );
fail_if_err (err);
err = gpgme_op_import (ctx, in );
fail_if_err (err);
gpgme_data_release (in);
gpgme_release (ctx);
} while ( argc > 1 && !strcmp( argv[1], "--loop" ) );
return 0;
}