about the result of a signing operation in GpgmeSignResult,
GpgmeInvalidUserID and GpgmeNewSignature objects.
+ * The new gpgme_op_encrypt_result function provides detailed
+ information about the result of an encryption operation in
+ a GpgmeEncryptResult object.
+
* Interface changes relative to the 0.4.0 release:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GpgmeIOCb CHANGED: Return type from void to GpgmeError.
gpgme_op_sign_result NEW
gpgme_pubkey_algo_name NEW
gpgme_hash_algo_name NEW
+GpgmeEncryptResult NEW
+gpgme_op_encrypt_result NEW
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Noteworthy changes in version 0.4.0 (2002-12-23)
2003-04-27 Marcus Brinkmann <marcus@g10code.de>
+ * gpgme.texi (Encrypting a Plaintext): Add info about
+ GpgmeEncryptResult and gpgme_op_encrypt_result.
+
* gpgme.texi (Creating a Signature): Add info about
GpgmeNewSignature, GpgmeSignResult and gpgme_op_sign_result.
(Crypto Operations): Add GpgmeInvalidUserID.
recipients.
@end deftypefun
+@deftp {Data type} {GpgmeEncryptResult}
+This is a pointer to a structure used to store the result of a
+@code{gpgme_op_encrypt} operation. After successfully encrypting
+data, you can retrieve the pointer to the result with
+@code{gpgme_op_encrypt_result}. The structure contains the following
+members:
+
+@table @code
+@item GpgmeInvalidUserID invalid_recipients
+A linked list with information about all invalid user IDs for which
+the data could not be encrypted.
+@end table
+@end deftp
+
+@deftypefun GpgmeEncryptResult gpgme_op_encrypt_result (@w{GpgmeCtx @var{ctx}})
+The function @code{gpgme_op_encrypt_result} returns a
+@code{GpgmeEncryptResult} pointer to a structure holding the result of
+a @code{gpgme_op_encrypt} operation. The pointer is only valid if the
+last operation on the context was a @code{gpgme_op_encrypt} or
+@code{gpgme_op_encrypt_start} operation, and if this operation
+finished successfully. The returned pointer is only valid until the
+next operation is started on the context.
+@end deftypefun
+
@deftypefun GpgmeError gpgme_op_encrypt_sign (@w{GpgmeCtx @var{ctx}}, @w{GpgmeRecipients @var{rset}}, @w{GpgmeData @var{plain}}, @w{GpgmeData @var{cipher}})
The function @code{gpgme_op_encrypt_sign} does a combined encrypt and
2003-04-27 Marcus Brinkmann <marcus@g10code.de>
+ * gpgme.h (GpgmeEncryptResult): New data type.
+ (gpgme_op_encrypt_result): New prototype.
+ * ops.h (_gpgme_op_encrypt_init_result): New prototype.
+ (_gpgme_op_encrypt_status_handler): Fix prototype.
+ * encrypt-sign.c (_gpgme_op_encrypt_sign_start): Call
+ _gpgme_op_encrypt_init_result.
+ * encrypt.c: Do not include <stdio.h>, <assert.h>, "util.h" and
+ "wait.h". Include <errno.h> and "gpgme.h".
+ (SKIP_TOKEN_OR_RETURN): Remove macro.
+ (struct encrypt_result): Rename to ...
+ (op_data_t): ... new data type. Rewrite for user result data.
+ (append_xml_encinfo): Remove function.
+ (release_op_data): New function.
+ (gpgme_op_encrypt_result): New function.
+ (_gpgme_op_encrypt_status_handler): Change first argument to void *.
+ Rewrite result parsing.
+ (_gpgme_op_encrypt_sym_status_handler): Change first argument to
+ void *.
+ (_gpgme_op_encrypt_init_result): New function.
+ (_gpgme_op_encrypt_start): Rename to ...
+ (encrypt_start): ... this.
+ (gpgme_op_encrypt_start): Use encrypt_start, not
+ gpgme_op_encrypt_start.
+ (gpgme_op_encrypt): Likewise.
+
* gpgme.h (GpgmePubKeyAlgo, GpgmeHashAlgo, GpgmeInvalidUserID,
GpgmeNewSignature, GpgmeSignResult): New data types.
(gpgme_op_sign_result, gpgme_pubkey_algo_name,
(sign_start): ... this. Call _gpgme_op_sign_init_result.
(gpgme_op_sign_start): Use sign_start instead _gpgme_op_sign_start.
(gpgme_op_sign): Likewise.
-
* encrypt-sign.c (_gpgme_op_encrypt_sign_start): Call
_gpgme_op_sign_init_result.
if (err)
return err;
+ err = _gpgme_op_encrypt_init_result (ctx);
+ if (err)
+ return err;
+
err = _gpgme_op_sign_init_result (ctx);
if (err)
return err;
-/* encrypt.c - Encrypt functions.
+/* encrypt.c - Encrypt function.
Copyright (C) 2000 Werner Koch (dd9jn)
Copyright (C) 2001, 2002, 2003 g10 Code GmbH
#if HAVE_CONFIG_H
#include <config.h>
#endif
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <assert.h>
+#include <errno.h>
-#include "util.h"
+#include "gpgme.h"
#include "context.h"
#include "ops.h"
-#include "wait.h"
-#define SKIP_TOKEN_OR_RETURN(a) do { \
- while (*(a) && *(a) != ' ') (a)++; \
- while (*(a) == ' ') (a)++; \
- if (!*(a)) \
- return; /* oops */ \
-} while (0)
-
-struct encrypt_result
+\f
+typedef struct
{
- int no_valid_recipients;
- int invalid_recipients;
- GpgmeData xmlinfo;
-};
-typedef struct encrypt_result *EncryptResult;
+ struct _gpgme_op_encrypt_result result;
-static void
-release_encrypt_result (void *hook)
-{
- EncryptResult result = (EncryptResult) hook;
-
- gpgme_data_release (result->xmlinfo);
-}
+ /* A pointer to the next pointer of the last invalid recipient in
+ the list. This makes appending new invalid recipients painless
+ while preserving the order. */
+ GpgmeInvalidUserID *lastp;
+} *op_data_t;
-/* Parse the args and save the information in an XML structure. With
- args of NULL the xml structure is closed. */
static void
-append_xml_encinfo (GpgmeData *rdh, char *args)
+release_op_data (void *hook)
{
- GpgmeData dh;
- char helpbuf[100];
+ op_data_t opd = (op_data_t) hook;
+ GpgmeInvalidUserID invalid_recipient = opd->result.invalid_recipients;
- if (!*rdh)
+ while (invalid_recipient)
{
- if (gpgme_data_new (rdh))
- return; /* FIXME: We are ignoring out-of-core. */
- dh = *rdh;
- _gpgme_data_append_string (dh, "<GnupgOperationInfo>\n");
- }
- else
- {
- dh = *rdh;
- _gpgme_data_append_string (dh, " </encryption>\n");
+ GpgmeInvalidUserID next = invalid_recipient->next;
+ free (invalid_recipient->id);
+ invalid_recipient = next;
}
+}
- if (!args)
- {
- /* Just close the XML containter. */
- _gpgme_data_append_string (dh, "</GnupgOperationInfo>\n");
- return;
- }
- _gpgme_data_append_string (dh, " <encryption>\n"
- " <error>\n"
- " <invalidRecipient/>\n");
-
- sprintf (helpbuf, " <reason>%d</reason>\n", atoi (args));
- _gpgme_data_append_string (dh, helpbuf);
- SKIP_TOKEN_OR_RETURN (args);
-
- _gpgme_data_append_string (dh, " <name>");
- _gpgme_data_append_percentstring_for_xml (dh, args);
- _gpgme_data_append_string (dh, "</name>\n"
- " </error>\n");
-}
+GpgmeEncryptResult
+gpgme_op_encrypt_result (GpgmeCtx ctx)
+{
+ op_data_t opd;
+ GpgmeError err;
+ err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, (void **) &opd, -1, NULL);
+ if (err || !opd)
+ return NULL;
+ return &opd->result;
+}
+
+\f
GpgmeError
-_gpgme_encrypt_status_handler (GpgmeCtx ctx, GpgmeStatusCode code, char *args)
+_gpgme_encrypt_status_handler (void *priv, GpgmeStatusCode code, char *args)
{
- GpgmeError err = 0;
- EncryptResult result;
+ GpgmeCtx ctx = (GpgmeCtx) priv;
+ GpgmeError err;
+ op_data_t opd;
+
+ err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, (void **) &opd,
+ -1, NULL);
+ if (err)
+ return err;
switch (code)
{
case GPGME_STATUS_EOF:
- err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, (void **) &result,
- -1, NULL);
- if (!err)
- {
- if (result && result->xmlinfo)
- {
- append_xml_encinfo (&result->xmlinfo, NULL);
- _gpgme_set_op_info (ctx, result->xmlinfo);
- result->xmlinfo = NULL;
- }
- if (result && result->no_valid_recipients)
- return GPGME_No_UserID;
- if (result && result->invalid_recipients)
- return GPGME_Invalid_UserID;
- }
+ if (opd->result.invalid_recipients)
+ return GPGME_Invalid_UserID;
break;
case GPGME_STATUS_INV_RECP:
- err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, (void **) &result,
- sizeof (*result), release_encrypt_result);
- if (!err)
- {
- result->invalid_recipients++;
- append_xml_encinfo (&result->xmlinfo, args);
- }
+ err = _gpgme_parse_inv_userid (args, opd->lastp);
+ if (err)
+ return err;
+
+ opd->lastp = &(*opd->lastp)->next;
break;
case GPGME_STATUS_NO_RECP:
- err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, (void **) &result,
- sizeof (*result), release_encrypt_result);
- if (!err)
- result->no_valid_recipients = 1;
- break;
+ /* Should not happen, because we require at least one recipient. */
+ return GPGME_No_UserID;
default:
break;
}
- return err;
+ return 0;
}
GpgmeError
-_gpgme_encrypt_sym_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
+_gpgme_encrypt_sym_status_handler (void *priv, GpgmeStatusCode code,
char *args)
{
- return _gpgme_passphrase_status_handler (ctx, code, args);
+ return _gpgme_passphrase_status_handler (priv, code, args);
+}
+
+
+GpgmeError
+_gpgme_op_encrypt_init_result (GpgmeCtx ctx)
+{
+ GpgmeError err;
+ op_data_t opd;
+
+ err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, (void **) &opd,
+ sizeof (*opd), release_op_data);
+ if (err)
+ return err;
+ opd->lastp = &opd->result.invalid_recipients;
+ return 0;
}
static GpgmeError
-_gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous,
- GpgmeRecipients recp, GpgmeData plain, GpgmeData cipher)
+encrypt_start (GpgmeCtx ctx, int synchronous, GpgmeRecipients recp,
+ GpgmeData plain, GpgmeData cipher)
{
GpgmeError err;
int symmetric = 0;
if (err)
return err;
+ err = _gpgme_op_encrypt_init_result (ctx);
+ if (err)
+ return err;
+
if (!recp)
symmetric = 1;
else if (gpgme_recipients_count (recp) == 0)
GpgmeError
gpgme_op_encrypt_start (GpgmeCtx ctx, GpgmeRecipients recp, GpgmeData plain,
- GpgmeData ciph)
+ GpgmeData cipher)
{
- return _gpgme_op_encrypt_start (ctx, 0, recp, plain, ciph);
+ return encrypt_start (ctx, 0, recp, plain, cipher);
}
-/**
- * gpgme_op_encrypt:
- * @c: The context
- * @recp: A set of recipients
- * @in: plaintext input
- * @out: ciphertext output
- *
- * This function encrypts @in to @out for all recipients from
- * @recp. Other parameters are take from the context @c.
- * The function does wait for the result.
- *
- * Return value: 0 on success or an errorcode.
- **/
+/* Encrypt plaintext PLAIN within CTX for the recipients RECP and
+ store the resulting ciphertext in CIPHER. */
GpgmeError
gpgme_op_encrypt (GpgmeCtx ctx, GpgmeRecipients recp,
GpgmeData plain, GpgmeData cipher)
{
- int err = _gpgme_op_encrypt_start (ctx, 1, recp, plain, cipher);
+ int err = encrypt_start (ctx, 1, recp, plain, cipher);
if (!err)
err = _gpgme_wait_one (ctx);
return err;
typedef struct _gpgme_invalid_user_id *GpgmeInvalidUserID;
\f
+/* Encryption. */
+struct _gpgme_op_encrypt_result
+{
+ /* The list of invalid recipients. */
+ GpgmeInvalidUserID invalid_recipients;
+};
+typedef struct _gpgme_op_encrypt_result *GpgmeEncryptResult;
+
+/* Retrieve a pointer to the result of the encrypt operation. */
+GpgmeEncryptResult gpgme_op_encrypt_result (GpgmeCtx ctx);
+
/* Encrypt plaintext PLAIN within CTX for the recipients RECP and
store the resulting ciphertext in CIPHER. */
GpgmeError gpgme_op_encrypt_start (GpgmeCtx ctx,
GpgmeRecipients recp,
GpgmeData plain, GpgmeData cipher);
+\f
/* Decrypt ciphertext CIPHER within CTX and store the resulting
plaintext in PLAIN. */
GpgmeError gpgme_op_decrypt_start (GpgmeCtx ctx,
char *args);
\f
-/*-- encrypt.c --*/
-GpgmeError _gpgme_encrypt_status_handler (GpgmeCtx ctx, GpgmeStatusCode code,
+/* From encrypt.c. */
+
+/* Create an initial op data object for encrypt. Needs to be called
+ once before calling _gpgme_encrypt_status_handler. */
+GpgmeError _gpgme_op_encrypt_init_result (GpgmeCtx ctx);
+
+/* Process a status line for encryption operations. */
+GpgmeError _gpgme_encrypt_status_handler (void *priv, GpgmeStatusCode code,
char *args);
+\f
/*-- passphrase.c --*/
GpgmeError _gpgme_passphrase_status_handler (void *priv, GpgmeStatusCode code,
char *args);
2003-04-27 Marcus Brinkmann <marcus@g10code.de>
+ * gpg/t-sign.c: Rewritten.
+ * gpgsm/t-sign.c: Rewritten.
+ * gpg/t-encrypt.c: Check for invalid recipients.
+ * gpgsm/t-encrypt.c: Likewise.
+
* gpg/t-import.c (check_result): Really use FPR.
* gpgsm/t-import.c (check_result): Rewritten.
-/* t-encrypt.c - regression test
+/* t-encrypt.c - Regression test.
Copyright (C) 2000 Werner Koch (dd9jn)
Copyright (C) 2001, 2002, 2003 g10 Code GmbH
along with GPGME; 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 <stdio.h>
#include <string.h>
-#include <assert.h>
#include <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_op_info (GpgmeCtx c)
-{
- char *s = gpgme_get_op_info (c, 0);
-
- if (!s)
- puts ("<!-- no operation info available -->");
- else
- {
- puts (s);
- free (s);
- }
-}
+#define fail_if_err(err) \
+ do \
+ { \
+ if (err) \
+ { \
+ fprintf (stderr, "%s:%d: GpgmeError %s\n", \
+ __FILE__, __LINE__, gpgme_strerror (err)); \
+ exit (1); \
+ } \
+ } \
+ while (0)
static void
print_data (GpgmeData dh)
{
- char buf[100];
+#define BUF_SIZE 512
+ char buf[BUF_SIZE + 1];
int ret;
ret = gpgme_data_seek (dh, 0, SEEK_SET);
if (ret)
fail_if_err (GPGME_File_Error);
- while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
+ while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
fwrite (buf, ret, 1, stdout);
if (ret < 0)
fail_if_err (GPGME_File_Error);
}
-
int
main (int argc, char **argv)
{
- GpgmeCtx ctx;
- GpgmeError err;
- GpgmeData in, out;
- GpgmeRecipients rset;
-
- err = gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP);
- fail_if_err (err);
-
- do {
- err = gpgme_new (&ctx);
- fail_if_err (err);
- gpgme_set_armor (ctx, 1);
-
- 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);
-
- err = gpgme_recipients_new (&rset);
- fail_if_err (err);
- err = gpgme_recipients_add_name_with_validity (rset, "Bob",
- GPGME_VALIDITY_FULL);
- fail_if_err (err);
- err = gpgme_recipients_add_name_with_validity (rset, "Alpha",
- GPGME_VALIDITY_FULL);
- fail_if_err (err);
-
-
- err = gpgme_op_encrypt (ctx, rset, in, out );
- print_op_info (ctx);
- fail_if_err (err);
+ GpgmeCtx ctx;
+ GpgmeError err;
+ GpgmeData in, out;
+ GpgmeRecipients rset;
+ GpgmeEncryptResult result;
+
+ err = gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP);
+ fail_if_err (err);
+
+ err = gpgme_new (&ctx);
+ fail_if_err (err);
+ gpgme_set_armor (ctx, 1);
+
+ 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);
+
+ err = gpgme_recipients_new (&rset);
+ fail_if_err (err);
+ err = gpgme_recipients_add_name_with_validity (rset, "Bob",
+ GPGME_VALIDITY_FULL);
+ fail_if_err (err);
+ err = gpgme_recipients_add_name_with_validity (rset, "Alpha",
+ GPGME_VALIDITY_FULL);
+ fail_if_err (err);
+
+ err = gpgme_op_encrypt (ctx, rset, in, out);
+ fail_if_err (err);
+ result = gpgme_op_encrypt_result (ctx);
+ if (result->invalid_recipients)
+ {
+ fprintf (stderr, "Invalid recipient encountered: %s\n",
+ result->invalid_recipients->id);
+ exit (1);
+ }
+ print_data (out);
- fflush (NULL);
- fputs ("Begin Result:\n", stdout );
- print_data (out);
- fputs ("End Result.\n", stdout );
-
- gpgme_recipients_release (rset);
- gpgme_data_release (in);
- gpgme_data_release (out);
- gpgme_release (ctx);
- } while ( argc > 1 && !strcmp( argv[1], "--loop" ) );
-
- return 0;
+ gpgme_recipients_release (rset);
+ gpgme_data_release (in);
+ gpgme_data_release (out);
+ gpgme_release (ctx);
+ return 0;
}
-/* t-genkey.c - regression test
+/* t-genkey.c - regression test
Copyright (C) 2000 Werner Koch (dd9jn)
Copyright (C) 2001, 2003 g10 Code GmbH
-/* t-sign.c - regression test
- * Copyright (C) 2000 Werner Koch (dd9jn)
- * Copyright (C) 2001, 2003 g10 Code GmbH
- *
- * 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
- */
+/* t-sign.c - Regression test.
+ Copyright (C) 2000 Werner Koch (dd9jn)
+ Copyright (C) 2001, 2003 g10 Code GmbH
+
+ 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 GPGME; 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 <stdio.h>
#include <string.h>
-#include <assert.h>
#include <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_op_info (GpgmeCtx ctx)
-{
- char *str = gpgme_get_op_info (ctx, 0);
-
- if (!str)
- puts ("<!-- no operation info available -->");
- else
- {
- puts (str);
- free (str);
- }
-}
+#define fail_if_err(err) \
+ do \
+ { \
+ if (err) \
+ { \
+ fprintf (stderr, "%s:%d: GpgmeError %s\n", \
+ __FILE__, __LINE__, gpgme_strerror (err)); \
+ exit (1); \
+ } \
+ } \
+ while (0)
static void
print_data (GpgmeData dh)
{
- char buf[100];
+#define BUF_SIZE 512
+ char buf[BUF_SIZE + 1];
int ret;
ret = gpgme_data_seek (dh, 0, SEEK_SET);
if (ret)
fail_if_err (GPGME_File_Error);
- while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
+ while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
fwrite (buf, ret, 1, stdout);
if (ret < 0)
fail_if_err (GPGME_File_Error);
static GpgmeError
-passphrase_cb (void *opaque, const char *desc,
- void **r_hd, const char **result)
+passphrase_cb (void *opaque, const char *desc, void **hd, const char **result)
{
+ /* Cleanup by looking at *hd. */
if (!desc)
- /* Cleanup by looking at *r_hd. */
return 0;
*result = "abc";
- fprintf (stderr, "%% requesting passphrase for `%s': ", desc);
- fprintf (stderr, "sending `%s'\n", *result);
-
return 0;
}
-int
-main (int argc, char **argv )
+static void
+check_result (GpgmeSignResult result, GpgmeSigMode type)
{
- GpgmeCtx ctx;
- GpgmeError err;
- GpgmeData in, out;
- char *p;
-
- do {
- err = gpgme_new (&ctx);
- fail_if_err (err);
-
- p = getenv("GPG_AGENT_INFO");
- if (!(p && strchr (p, ':')))
- gpgme_set_passphrase_cb ( ctx, passphrase_cb, NULL );
-
- gpgme_set_textmode (ctx, 1);
- gpgme_set_armor (ctx, 1);
-
- err = gpgme_data_new_from_mem ( &in, "Hallo Leute\n", 12, 0 );
- fail_if_err (err);
-
- /* first a normal signature */
- err = gpgme_data_new ( &out );
- fail_if_err (err);
- err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL );
- fail_if_err (err);
- fflush (NULL);
- fputs ("Begin Result:\n", stdout );
- print_op_info (ctx);
- print_data (out);
- fputs ("End Result.\n", stdout );
- gpgme_data_release (out);
- gpgme_data_rewind (in);
-
- /* now a detached signature */
- err = gpgme_data_new ( &out );
- fail_if_err (err);
- err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH );
- fail_if_err (err);
- fflush (NULL);
- print_op_info (ctx);
- fputs ("Begin Result:\n", stdout );
- print_data (out);
- fputs ("End Result.\n", stdout );
- gpgme_data_release (out);
- gpgme_data_rewind (in);
-
+ if (result->invalid_signers)
+ {
+ fprintf (stderr, "Invalid signer found: %s\n",
+ result->invalid_signers->id);
+ exit (1);
+ }
+ if (!result->signatures || result->signatures->next)
+ {
+ fprintf (stderr, "Unexpected number of signatures created\n");
+ exit (1);
+ }
+ if (result->signatures->type != type)
+ {
+ fprintf (stderr, "Wrong type of signature created\n");
+ exit (1);
+ }
+ if (result->signatures->pubkey_algo != GPGME_PK_DSA)
+ {
+ fprintf (stderr, "Wrong pubkey algorithm reported: %i\n",
+ result->signatures->pubkey_algo);
+ exit (1);
+ }
+ if (result->signatures->hash_algo != GPGME_MD_SHA1)
+ {
+ fprintf (stderr, "Wrong hash algorithm reported: %i\n",
+ result->signatures->hash_algo);
+ exit (1);
+ }
+ if (result->signatures->class != 1)
+ {
+ fprintf (stderr, "Wrong signature class reported: %lu\n",
+ result->signatures->class);
+ exit (1);
+ }
+ if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734",
+ result->signatures->fpr))
+ {
+ fprintf (stderr, "Wrong fingerprint reported: %s\n",
+ result->signatures->fpr);
+ exit (1);
+ }
+}
+
- /* And finally a cleartext signature */
- err = gpgme_data_new ( &out );
- fail_if_err (err);
- err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_CLEAR );
- fail_if_err (err);
- fflush (NULL);
- print_op_info (ctx);
- fputs ("Begin Result:\n", stdout );
- print_data (out);
- fputs ("End Result.\n", stdout );
- gpgme_data_release (out);
- gpgme_data_rewind (in);
+int
+main (int argc, char **argv)
+{
+ GpgmeCtx ctx;
+ GpgmeError err;
+ GpgmeData in, out;
+ GpgmeSignResult result;
+ char *agent_info;
+
+ err = gpgme_new (&ctx);
+ fail_if_err (err);
+
+ agent_info = getenv("GPG_AGENT_INFO");
+ if (!(agent_info && strchr (agent_info, ':')))
+ gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
+
+ gpgme_set_textmode (ctx, 1);
+ gpgme_set_armor (ctx, 1);
+
+ err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
+ fail_if_err (err);
+
+ /* First a normal signature. */
+ err = gpgme_data_new (&out);
+ fail_if_err (err);
+ err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL);
+ fail_if_err (err);
+ result = gpgme_op_sign_result (ctx);
+ check_result (result, GPGME_SIG_MODE_NORMAL);
+ print_data (out);
+ gpgme_data_release (out);
- /* ready */
- gpgme_data_release (in);
- gpgme_release (ctx);
- } while ( argc > 1 && !strcmp( argv[1], "--loop" ) );
-
- return 0;
+ /* Now a detached signature. */
+ gpgme_data_rewind (in);
+ err = gpgme_data_new (&out);
+ fail_if_err (err);
+ err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH);
+ fail_if_err (err);
+ result = gpgme_op_sign_result (ctx);
+ check_result (result, GPGME_SIG_MODE_DETACH);
+ print_data (out);
+ gpgme_data_release (out);
+
+ /* And finally a cleartext signature. */
+ gpgme_data_rewind (in);
+ err = gpgme_data_new (&out);
+ fail_if_err (err);
+ err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_CLEAR);
+ fail_if_err (err);
+ result = gpgme_op_sign_result (ctx);
+ check_result (result, GPGME_SIG_MODE_CLEAR);
+ print_data (out);
+ gpgme_data_release (out);
+
+ gpgme_data_release (in);
+ gpgme_release (ctx);
+ return 0;
}
-/* t-encrypt.c - regression test
+/* t-encrypt.c - Regression test.
Copyright (C) 2000 Werner Koch (dd9jn)
Copyright (C) 2001, 2002, 2003 g10 Code GmbH
along with GPGME; 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 <stdio.h>
#include <string.h>
-#include <assert.h>
#include <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_op_info (GpgmeCtx ctx)
-{
- char *str = gpgme_get_op_info (ctx, 0);
-
- if (!str)
- puts ("<!-- no operation info available -->");
- else
- {
- puts (str);
- free (str);
- }
-}
+#define fail_if_err(err) \
+ do \
+ { \
+ if (err) \
+ { \
+ fprintf (stderr, "%s:%d: GpgmeError %s\n", \
+ __FILE__, __LINE__, gpgme_strerror (err)); \
+ exit (1); \
+ } \
+ } \
+ while (0)
static void
print_data (GpgmeData dh)
{
- char buf[100];
+#define BUF_SIZE 512
+ char buf[BUF_SIZE + 1];
int ret;
ret = gpgme_data_seek (dh, 0, SEEK_SET);
if (ret)
fail_if_err (GPGME_File_Error);
- while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
+ while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
fwrite (buf, ret, 1, stdout);
if (ret < 0)
fail_if_err (GPGME_File_Error);
int
-main (int argc, char **argv )
+main (int argc, char **argv)
{
- GpgmeCtx ctx;
- GpgmeError err;
- GpgmeData in, out;
- GpgmeRecipients rset;
- int loop = 0;
+ GpgmeCtx ctx;
+ GpgmeError err;
+ GpgmeData in, out;
+ GpgmeRecipients rset;
+ GpgmeEncryptResult result;
- /* simple option parser; ignoring unknown options */
- if (argc)
- {
- argc--;
- argv++;
- }
- while (argc && **argv == '-' )
- {
- if (!strcmp (*argv, "--loop"))
- loop++;
+ err = gpgme_engine_check_version (GPGME_PROTOCOL_CMS);
+ fail_if_err (err);
- argc--;
- argv++;
- if (!strcmp (argv[-1], "--"))
- break;
- }
-
+ err = gpgme_new (&ctx);
+ fail_if_err (err);
+ gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
+ gpgme_set_armor (ctx, 1);
- err = gpgme_engine_check_version (GPGME_PROTOCOL_CMS);
- fail_if_err (err);
+ err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
+ fail_if_err (err);
- do {
- err = gpgme_new (&ctx);
- fail_if_err (err);
- gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
- gpgme_set_armor (ctx, 1);
-
- 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);
-
- err = gpgme_recipients_new (&rset);
- fail_if_err (err);
- if (argc)
- err = gpgme_recipients_add_name_with_validity (rset, *argv,
- GPGME_VALIDITY_FULL);
- else
- err = gpgme_recipients_add_name_with_validity (rset, "test cert 1",
- GPGME_VALIDITY_FULL);
- fail_if_err (err);
-
- err = gpgme_op_encrypt (ctx, rset, in, out );
- print_op_info (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_with_validity (rset, "test cert 1",
+ GPGME_VALIDITY_FULL);
+ fail_if_err (err);
+
+ err = gpgme_op_encrypt (ctx, rset, in, out);
+ fail_if_err (err);
+ result = gpgme_op_encrypt_result (ctx);
+ if (result->invalid_recipients)
+ {
+ fprintf (stderr, "Invalid recipient encountered: %s\n",
+ result->invalid_recipients->id);
+ exit (1);
+ }
+ print_data (out);
- fflush (NULL);
- fputs ("Begin Result:\n", stdout );
- print_data (out);
- fputs ("End Result.\n", stdout );
-
- gpgme_recipients_release (rset);
- gpgme_data_release (in);
- gpgme_data_release (out);
- gpgme_release (ctx);
- } while (loop);
-
- return 0;
+ gpgme_recipients_release (rset);
+ gpgme_data_release (in);
+ gpgme_data_release (out);
+ gpgme_release (ctx);
+ return 0;
}
-
-
while (0)
-void
-dump_data (GpgmeData dh)
+static void
+print_data (GpgmeData dh)
{
#define BUF_SIZE 512
- char buffer[BUF_SIZE + 1];
- int bufread;
-
- gpgme_data_rewind (dh);
-
- do
- {
- bufread = gpgme_data_read (dh, buffer, BUF_SIZE);
- if (bufread > 0)
- {
- buffer[bufread] = '\0';
- printf ("%s", buffer);
- }
- }
- while (bufread > 0);
-
- if (bufread < 0)
- {
- fprintf (stderr, "%s:%d: gpgme_data_read failed: %s\n",
- __FILE__, __LINE__, strerror (errno));
- exit (1);
- }
- printf ("\n");
+ char buf[BUF_SIZE + 1];
+ int ret;
+
+ ret = gpgme_data_seek (dh, 0, SEEK_SET);
+ if (ret)
+ fail_if_err (GPGME_File_Error);
+ while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
+ fwrite (buf, ret, 1, stdout);
+ if (ret < 0)
+ fail_if_err (GPGME_File_Error);
}
/* True if progress function printed something on the screen. */
-int progress_called;
+static int progress_called;
static void
progress (void *self, const char *what, int type, int current, int total)
}
gpgme_release (ctx);
- dump_data (certreq);
+ print_data (certreq);
gpgme_data_release (certreq);
return 0;
-/* t-sign.c - regression test
- * Copyright (C) 2000 Werner Koch (dd9jn)
- * Copyright (C) 2001 g10 Code GmbH
- *
- * 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
- */
+/* t-sign.c - Regression test.
+ Copyright (C) 2000 Werner Koch (dd9jn)
+ Copyright (C) 2001, 2003 g10 Code GmbH
+
+ 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 GPGME; 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 <stdio.h>
#include <string.h>
-#include <assert.h>
#include <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_op_info (GpgmeCtx ctx)
-{
- char *str = gpgme_get_op_info (ctx, 0);
-
- if (!str)
- puts ("<!-- no operation info available -->");
- else
- {
- puts (str);
- free (str);
- }
-}
+#define fail_if_err(err) \
+ do \
+ { \
+ if (err) \
+ { \
+ fprintf (stderr, "%s:%d: GpgmeError %s\n", \
+ __FILE__, __LINE__, gpgme_strerror (err)); \
+ exit (1); \
+ } \
+ } \
+ while (0)
static void
print_data (GpgmeData dh)
{
- char buf[100];
+#define BUF_SIZE 512
+ char buf[BUF_SIZE + 1];
int ret;
ret = gpgme_data_seek (dh, 0, SEEK_SET);
if (ret)
fail_if_err (GPGME_File_Error);
- while ((ret = gpgme_data_read (dh, buf, 100)) > 0)
+ while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
fwrite (buf, ret, 1, stdout);
if (ret < 0)
fail_if_err (GPGME_File_Error);
}
+static void
+check_result (GpgmeSignResult result, GpgmeSigMode type)
+{
+ if (result->invalid_signers)
+ {
+ fprintf (stderr, "Invalid signer found: %s\n",
+ result->invalid_signers->id);
+ exit (1);
+ }
+ if (!result->signatures || result->signatures->next)
+ {
+ fprintf (stderr, "Unexpected number of signatures created\n");
+ exit (1);
+ }
+ if (result->signatures->type != type)
+ {
+ fprintf (stderr, "Wrong type of signature created\n");
+ exit (1);
+ }
+ if (result->signatures->pubkey_algo != GPGME_PK_RSA)
+ {
+ fprintf (stderr, "Wrong pubkey algorithm reported: %i\n",
+ result->signatures->pubkey_algo);
+ exit (1);
+ }
+ if (result->signatures->hash_algo != GPGME_MD_SHA1)
+ {
+ fprintf (stderr, "Wrong hash algorithm reported: %i\n",
+ result->signatures->hash_algo);
+ exit (1);
+ }
+ if (result->signatures->class != 0)
+ {
+ fprintf (stderr, "Wrong signature class reported: %lu\n",
+ result->signatures->class);
+ exit (1);
+ }
+ if (strcmp ("3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E",
+ result->signatures->fpr))
+ {
+ fprintf (stderr, "Wrong fingerprint reported: %s\n",
+ result->signatures->fpr);
+ exit (1);
+ }
+}
+
+
int
main (int argc, char **argv)
{
- GpgmeCtx ctx;
- GpgmeError err;
- GpgmeData in, out;
-
- do {
- err = gpgme_new (&ctx);
- fail_if_err (err);
- gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
- gpgme_set_textmode (ctx, 1);
- gpgme_set_armor (ctx, 1);
-
- err = gpgme_data_new_from_mem ( &in, "Hallo Leute!\n", 13, 0 );
- fail_if_err (err);
-
- /* first a normal signature */
- err = gpgme_data_new ( &out );
- fail_if_err (err);
- err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL );
- fail_if_err (err);
- fflush (NULL);
- fputs ("Begin Result:\n", stdout );
- print_op_info (ctx);
- print_data (out);
- fputs ("End Result.\n", stdout );
- gpgme_data_release (out);
- gpgme_data_rewind (in);
-
- /* now a detached signature */
- err = gpgme_data_new ( &out );
- fail_if_err (err);
- err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH );
- fail_if_err (err);
- fflush (NULL);
- print_op_info (ctx);
- fputs ("Begin Result:\n", stdout );
- print_data (out);
- fputs ("End Result.\n", stdout );
- gpgme_data_release (out);
- gpgme_data_rewind (in);
-
+ GpgmeCtx ctx;
+ GpgmeError err;
+ GpgmeData in, out;
+ GpgmeSignResult result;
+
+ err = gpgme_engine_check_version (GPGME_PROTOCOL_CMS);
+ fail_if_err (err);
+
+ err = gpgme_new (&ctx);
+ fail_if_err (err);
+
+ gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS);
+ gpgme_set_textmode (ctx, 1);
+ gpgme_set_armor (ctx, 1);
+
+ err = gpgme_data_new_from_mem (&in, "Hallo Leute!\n", 13, 0);
+ fail_if_err (err);
+
+ /* First a normal signature. */
+ err = gpgme_data_new (&out);
+ fail_if_err (err);
+ err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL);
+ fail_if_err (err);
+ result = gpgme_op_sign_result (ctx);
+ check_result (result, GPGME_SIG_MODE_NORMAL);
+ print_data (out);
+ gpgme_data_release (out);
- /* ready */
- gpgme_data_release (in);
- gpgme_release (ctx);
- } while ( argc > 1 && !strcmp( argv[1], "--loop" ) );
-
- return 0;
+ /* Now a detached signature. */
+ gpgme_data_rewind (in);
+ err = gpgme_data_new (&out);
+ fail_if_err (err);
+ err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH);
+ fail_if_err (err);
+ result = gpgme_op_sign_result (ctx);
+ check_result (result, GPGME_SIG_MODE_DETACH);
+ print_data (out);
+ gpgme_data_release (out);
+
+ gpgme_data_release (in);
+ gpgme_release (ctx);
+ return 0;
}