From: Werner Koch Date: Thu, 16 Nov 2000 14:53:52 +0000 (+0000) Subject: Added sign functionality X-Git-Tag: V0-0-0~1 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3868237d9556dd984dca4acbb360244f1cfbe1f9;p=gpgme.git Added sign functionality --- diff --git a/gpgme/Makefile.am b/gpgme/Makefile.am index d8f29b6..acc2305 100644 --- a/gpgme/Makefile.am +++ b/gpgme/Makefile.am @@ -20,6 +20,7 @@ libgpgme_la_SOURCES = \ encrypt.c \ decrypt.c \ verify.c \ + sign.c \ key.c key.h \ keylist.c \ rungpg.c rungpg.h status-table.h \ diff --git a/gpgme/context.h b/gpgme/context.h index 53773f6..71c4239 100644 --- a/gpgme/context.h +++ b/gpgme/context.h @@ -29,6 +29,7 @@ typedef enum { RESULT_TYPE_NONE = 0, RESULT_TYPE_VERIFY, RESULT_TYPE_DECRYPT, + RESULT_TYPE_SIGN, } ResultType; @@ -52,14 +53,14 @@ struct gpgme_context_s { GpgObject gpg; /* the running gpg process */ int verbosity; /* level of verbosity to use */ - int use_armor; /* use armoring */ - + int use_armor; + int use_textmode; - ResultType result_type; union { VerifyResult verify; DecryptResult decrypt; + SignResult sign; } result; GpgmeData notation; /* last signature notation */ diff --git a/gpgme/decrypt.c b/gpgme/decrypt.c index 1d8a20e..017cbf9 100644 --- a/gpgme/decrypt.c +++ b/gpgme/decrypt.c @@ -174,7 +174,9 @@ gpgme_op_decrypt ( GpgmeCtx c, GpgmeData in, GpgmeData out ) err = mk_error (Out_Of_Core); else { assert ( c->result.decrypt ); - if ( c->result.decrypt->failed ) + if ( c->result.decrypt->no_passphrase ) + err = mk_error (No_Passphrase); + else if ( c->result.decrypt->failed ) err = mk_error (Decryption_Failed); else if (!c->result.decrypt->okay) err = mk_error (No_Data); diff --git a/gpgme/gpgme.c b/gpgme/gpgme.c index 40b12c4..05f940f 100644 --- a/gpgme/gpgme.c +++ b/gpgme/gpgme.c @@ -49,7 +49,7 @@ gpgme_new (GpgmeCtx *r_ctx) if (!c) return mk_error (Out_Of_Core); c->verbosity = 1; - c->use_armor = 1; + c->use_armor = 1; /* fixme: reset this to 0 */ *r_ctx = c; return 0; } @@ -85,6 +85,9 @@ _gpgme_release_result ( GpgmeCtx c ) case RESULT_TYPE_DECRYPT: _gpgme_release_decrypt_result ( c->result.decrypt ); break; + case RESULT_TYPE_SIGN: + _gpgme_release_sign_result ( c->result.sign ); + break; } c->result.verify = NULL; @@ -101,8 +104,21 @@ gpgme_op_get_notation ( GpgmeCtx c ) } +void +gpgme_op_set_armor ( GpgmeCtx c, int yes ) +{ + if ( !c ) + return; /* oops */ + c->use_armor = yes; +} - +void +gpgme_op_set_textmode ( GpgmeCtx c, int yes ) +{ + if ( !c ) + return; /* oops */ + c->use_textmode = yes; +} diff --git a/gpgme/gpgme.h b/gpgme/gpgme.h index 112d607..f2b8ebc 100644 --- a/gpgme/gpgme.h +++ b/gpgme/gpgme.h @@ -61,6 +61,7 @@ typedef enum { GPGME_Invalid_Mode = 16, GPGME_File_Error = 17, /* errno is set in this case */ GPGME_Decryption_Failed = 18, + GPGME_No_Passphrase = 19, } GpgmeError; typedef enum { @@ -87,6 +88,8 @@ void gpgme_release ( GpgmeCtx c ); GpgmeCtx gpgme_wait ( GpgmeCtx c, int hang ); char *gpgme_op_get_notation ( GpgmeCtx c ); +void gpgme_op_set_armor ( GpgmeCtx c, int yes ); +void gpgme_op_set_textmode ( GpgmeCtx c, int yes ); /* Functions to handle recipients */ @@ -120,6 +123,7 @@ GpgmeError gpgme_op_encrypt_start ( GpgmeCtx c, GpgmeData in, GpgmeData out ); GpgmeError gpgme_op_decrypt_start ( GpgmeCtx c, GpgmeData ciph, GpgmeData plain ); +GpgmeError gpgme_op_sign_start ( GpgmeCtx c, GpgmeData in, GpgmeData out ); GpgmeError gpgme_op_verify_start ( GpgmeCtx c, GpgmeData sig, GpgmeData text ); @@ -134,6 +138,7 @@ GpgmeError gpgme_op_keylist_next ( GpgmeCtx c, GpgmeKey *r_key ); GpgmeError gpgme_op_encrypt ( GpgmeCtx c, GpgmeRecipients recp, GpgmeData in, GpgmeData out ); GpgmeError gpgme_op_decrypt ( GpgmeCtx c, GpgmeData in, GpgmeData out ); +GpgmeError gpgme_op_sign ( GpgmeCtx c, GpgmeData in, GpgmeData out ); GpgmeError gpgme_op_verify ( GpgmeCtx c, GpgmeData sig, GpgmeData text, GpgmeSigStat *r_status ); diff --git a/gpgme/ops.h b/gpgme/ops.h index 90c93e8..9de6d24 100644 --- a/gpgme/ops.h +++ b/gpgme/ops.h @@ -65,6 +65,9 @@ void _gpgme_release_verify_result ( VerifyResult res ); /*-- decrypt.c --*/ void _gpgme_release_decrypt_result ( DecryptResult res ); +/*-- sign.c --*/ +void _gpgme_release_sign_result ( SignResult res ); + #endif /* OPS_H */ diff --git a/gpgme/rungpg.c b/gpgme/rungpg.c index c81a89a..e437ba8 100644 --- a/gpgme/rungpg.c +++ b/gpgme/rungpg.c @@ -524,10 +524,12 @@ _gpgme_gpg_spawn( GpgObject gpg, void *opaque ) } /* We normally don't want all the normal output */ if ( !duped_stderr ) { - if ( dup2 ( fd, 2 ) == -1 ) { - fprintf (stderr,"dup2(dev/null, 2) failed: %s\n", - strerror (errno) ); - _exit (8); + if (!getenv ("GPGME_DEBUG") ) { + if ( dup2 ( fd, 2 ) == -1 ) { + fprintf (stderr,"dup2(dev/null, 2) failed: %s\n", + strerror (errno) ); + _exit (8); + } } } close (fd); diff --git a/gpgme/sign.c b/gpgme/sign.c new file mode 100644 index 0000000..3274f11 --- /dev/null +++ b/gpgme/sign.c @@ -0,0 +1,190 @@ +/* sign.c - signing 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 +#include +#include +#include +#include + +#include "util.h" +#include "context.h" +#include "ops.h" + + +struct sign_result_s { + int no_passphrase; + int okay; +}; + + +void +_gpgme_release_sign_result ( SignResult res ) +{ + xfree (res); +} + + + +static void +sign_status_handler ( GpgmeCtx ctx, GpgStatusCode code, char *args ) +{ + if ( ctx->out_of_core ) + return; + if ( ctx->result_type == RESULT_TYPE_NONE ) { + assert ( !ctx->result.sign ); + ctx->result.sign = xtrycalloc ( 1, sizeof *ctx->result.sign ); + if ( !ctx->result.sign ) { + ctx->out_of_core = 1; + return; + } + ctx->result_type = RESULT_TYPE_SIGN; + } + assert ( ctx->result_type == RESULT_TYPE_SIGN ); + + switch (code) { + case STATUS_EOF: + break; + + case STATUS_NEED_PASSPHRASE: + case STATUS_NEED_PASSPHRASE_SYM: + fprintf (stderr, "Ooops: Need a passphrase - use the agent\n"); + break; + + case STATUS_MISSING_PASSPHRASE: + fprintf (stderr, "Missing passphrase - stop\n");; + ctx->result.sign->no_passphrase = 1; + break; + + case STATUS_SIG_CREATED: + /* fixme: we have no error return for multible signatures */ + ctx->result.sign->okay =1; + break; + + default: + fprintf (stderr, "sign_status: code=%d not handled\n", code ); + break; + } +} + + + +GpgmeError +gpgme_op_sign_start ( GpgmeCtx c, GpgmeData in, GpgmeData out ) +{ + int rc = 0; + int i; + + fail_on_pending_request( c ); + c->pending = 1; + + _gpgme_release_result (c); + c->out_of_core = 0; + + /* do some checks */ + assert ( !c->gpg ); + + /* create a process object */ + rc = _gpgme_gpg_new ( &c->gpg ); + if (rc) + goto leave; + + _gpgme_gpg_set_status_handler ( c->gpg, sign_status_handler, c ); + + /* build the commandline */ + _gpgme_gpg_add_arg ( c->gpg, "--sign" ); + _gpgme_gpg_add_arg ( c->gpg, "--detach" ); + if ( c->use_armor ) + _gpgme_gpg_add_arg ( c->gpg, "--armor" ); + if ( c->use_textmode ) + _gpgme_gpg_add_arg ( c->gpg, "--textmode" ); + for ( i=0; i < c->verbosity; i++ ) + _gpgme_gpg_add_arg ( c->gpg, "--verbose" ); + + /* Check the supplied data */ + if ( gpgme_data_get_type (in) == GPGME_DATA_TYPE_NONE ) { + rc = mk_error (No_Data); + goto leave; + } + _gpgme_data_set_mode (in, GPGME_DATA_MODE_OUT ); + if ( !out || gpgme_data_get_type (out) != GPGME_DATA_TYPE_NONE ) { + rc = mk_error (Invalid_Value); + goto leave; + } + _gpgme_data_set_mode (out, GPGME_DATA_MODE_IN ); + + /* Tell the gpg object about the data */ + _gpgme_gpg_add_data ( c->gpg, in, 0 ); + _gpgme_gpg_add_data ( c->gpg, out, 1 ); + + /* and kick off the process */ + rc = _gpgme_gpg_spawn ( c->gpg, c ); + + leave: + if (rc) { + c->pending = 0; + _gpgme_gpg_release ( c->gpg ); c->gpg = NULL; + } + return rc; +} + + +/** + * gpgme_op_sign: + * @c: The context + * @in: Data to be signed + * @out: Detached signature + * + * Create a detached signature for @in and write it to @out. + * The data will be signed using either the default key or the ones + * defined through @c. + * + * Return value: 0 on success or an error code. + **/ +GpgmeError +gpgme_op_sign ( GpgmeCtx c, GpgmeData in, GpgmeData out ) +{ + GpgmeError err = gpgme_op_sign_start ( c, in, out ); + if ( !err ) { + gpgme_wait (c, 1); + if ( c->result_type != RESULT_TYPE_SIGN ) + err = mk_error (General_Error); + else if ( c->out_of_core ) + err = mk_error (Out_Of_Core); + else { + assert ( c->result.sign ); + if ( c->result.sign->no_passphrase ) + err = mk_error (No_Passphrase); + else if (!c->result.sign->okay) + err = mk_error (No_Data); /* Hmmm: choose a better error? */ + } + c->pending = 0; + } + return err; +} + + + + + + + + + diff --git a/gpgme/types.h b/gpgme/types.h index 1c73986..38d83b2 100644 --- a/gpgme/types.h +++ b/gpgme/types.h @@ -51,6 +51,10 @@ typedef struct verify_result_s *VerifyResult; struct decrypt_result_s; typedef struct decrypt_result_s *DecryptResult; +/*-- sign.c --*/ +struct sign_result_s; +typedef struct sign_result_s *SignResult; + /*-- key.c --*/ diff --git a/tests/Makefile.am b/tests/Makefile.am index 2f74e93..7b59958 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,8 +1,8 @@ ## Process this file with automake to create Makefile.in -TESTS = t-encrypt t-decrypt t-verify t-keylist +TESTS = t-encrypt t-sign t-decrypt t-verify t-keylist -EXTRA_DIST = cipher-1.asc +EXTRA_DIST = cipher-1.asc geheim.txt INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl diff --git a/tests/geheim.txt b/tests/geheim.txt new file mode 100644 index 0000000..99a5478 --- /dev/null +++ b/tests/geheim.txt @@ -0,0 +1,2 @@ +Wenn Sie dies lesen können, ist es wohl nicht +geheim genug. diff --git a/tests/t-sign.c b/tests/t-sign.c new file mode 100644 index 0000000..4d3ed34 --- /dev/null +++ b/tests/t-sign.c @@ -0,0 +1,89 @@ +/* t-sign.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 +#include +#include +#include + +#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 in, out; + + do { + err = gpgme_new (&ctx); + fail_if_err (err); + + err = gpgme_data_new_from_mem ( &in, "Hallo Leute\n", 12, 0 ); + fail_if_err (err); + + err = gpgme_data_new ( &out ); + fail_if_err (err); + + gpgme_op_set_textmode (ctx, 1); + gpgme_op_set_armor (ctx, 1); + err = gpgme_op_sign (ctx, in, out ); + fail_if_err (err); + + fflush (NULL); + fputs ("Begin Result:\n", stdout ); + print_data (out); + fputs ("End Result.\n", stdout ); + + gpgme_data_release (in); + gpgme_data_release (out); + gpgme_release (ctx); + } while ( argc > 1 && !strcmp( argv[1], "--loop" ) ); + + return 0; +} + + + +