From 588c16d324e83e64ccdc8761e3fb3b91648528fc Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Sun, 27 Apr 2003 22:24:37 +0000 Subject: [PATCH] doc/ 2003-04-27 Marcus Brinkmann * gpgme.texi (Encrypting a Plaintext): Add info about GpgmeEncryptResult and gpgme_op_encrypt_result. gpgme/ 2003-04-27 Marcus Brinkmann * 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 , , "util.h" and "wait.h". Include 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. tests/ 2003-04-27 Marcus Brinkmann * gpg/t-sign.c: Rewritten. * gpgsm/t-sign.c: Rewritten. * gpg/t-encrypt.c: Check for invalid recipients. * gpgsm/t-encrypt.c: Likewise. --- NEWS | 6 + doc/ChangeLog | 3 + doc/gpgme.texi | 24 ++++ gpgme/ChangeLog | 26 ++++- gpgme/encrypt-sign.c | 4 + gpgme/encrypt.c | 188 +++++++++++++----------------- gpgme/gpgme.h | 12 ++ gpgme/ops.h | 11 +- tests/ChangeLog | 5 + tests/gpg/t-encrypt.c | 128 ++++++++++---------- tests/gpg/t-genkey.c | 2 +- tests/gpg/t-sign.c | 251 ++++++++++++++++++++++------------------ tests/gpgsm/t-encrypt.c | 141 +++++++++------------- tests/gpgsm/t-genkey.c | 41 +++---- tests/gpgsm/t-sign.c | 211 +++++++++++++++++++-------------- 15 files changed, 553 insertions(+), 500 deletions(-) diff --git a/NEWS b/NEWS index 177b4d2..fb15c63 100644 --- a/NEWS +++ b/NEWS @@ -82,6 +82,10 @@ Noteworthy changes in version 0.4.1 (unreleased) 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. @@ -120,6 +124,8 @@ GpgmeSignResult NEW 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) diff --git a/doc/ChangeLog b/doc/ChangeLog index 0edda75..00055c8 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,5 +1,8 @@ 2003-04-27 Marcus Brinkmann + * 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. diff --git a/doc/gpgme.texi b/doc/gpgme.texi index e1c8c85..137e967 100644 --- a/doc/gpgme.texi +++ b/doc/gpgme.texi @@ -3357,6 +3357,30 @@ started successfully, @code{GPGME_Invalid_Value} if @var{ctx}, 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 diff --git a/gpgme/ChangeLog b/gpgme/ChangeLog index 8716eda..3b44aac 100644 --- a/gpgme/ChangeLog +++ b/gpgme/ChangeLog @@ -1,5 +1,30 @@ 2003-04-27 Marcus Brinkmann + * 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 , , "util.h" and + "wait.h". Include 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, @@ -27,7 +52,6 @@ (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. diff --git a/gpgme/encrypt-sign.c b/gpgme/encrypt-sign.c index 3e28773..2a3dba2 100644 --- a/gpgme/encrypt-sign.c +++ b/gpgme/encrypt-sign.c @@ -51,6 +51,10 @@ _gpgme_op_encrypt_sign_start (GpgmeCtx ctx, int synchronous, 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; diff --git a/gpgme/encrypt.c b/gpgme/encrypt.c index e0fcc57..a972080 100644 --- a/gpgme/encrypt.c +++ b/gpgme/encrypt.c @@ -1,4 +1,4 @@ -/* encrypt.c - Encrypt functions. +/* encrypt.c - Encrypt function. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003 g10 Code GmbH @@ -21,144 +21,119 @@ #if HAVE_CONFIG_H #include #endif -#include #include #include -#include +#include -#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 + +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, "\n"); - } - else - { - dh = *rdh; - _gpgme_data_append_string (dh, " \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, "\n"); - return; - } - _gpgme_data_append_string (dh, " \n" - " \n" - " \n"); - - sprintf (helpbuf, " %d\n", atoi (args)); - _gpgme_data_append_string (dh, helpbuf); - SKIP_TOKEN_OR_RETURN (args); - - _gpgme_data_append_string (dh, " "); - _gpgme_data_append_percentstring_for_xml (dh, args); - _gpgme_data_append_string (dh, "\n" - " \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; +} + + 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; @@ -167,6 +142,10 @@ _gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous, 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) @@ -200,30 +179,19 @@ _gpgme_op_encrypt_start (GpgmeCtx ctx, int synchronous, 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; diff --git a/gpgme/gpgme.h b/gpgme/gpgme.h index 9fa2af0..ef0434b 100644 --- a/gpgme/gpgme.h +++ b/gpgme/gpgme.h @@ -767,6 +767,17 @@ struct _gpgme_invalid_user_id typedef struct _gpgme_invalid_user_id *GpgmeInvalidUserID; +/* 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, @@ -786,6 +797,7 @@ GpgmeError gpgme_op_encrypt_sign (GpgmeCtx ctx, GpgmeRecipients recp, GpgmeData plain, GpgmeData cipher); + /* Decrypt ciphertext CIPHER within CTX and store the resulting plaintext in PLAIN. */ GpgmeError gpgme_op_decrypt_start (GpgmeCtx ctx, diff --git a/gpgme/ops.h b/gpgme/ops.h index 7cbb942..4812bfe 100644 --- a/gpgme/ops.h +++ b/gpgme/ops.h @@ -96,10 +96,17 @@ GpgmeError _gpgme_sign_status_handler (void *priv, GpgmeStatusCode code, char *args); -/*-- 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); + /*-- passphrase.c --*/ GpgmeError _gpgme_passphrase_status_handler (void *priv, GpgmeStatusCode code, char *args); diff --git a/tests/ChangeLog b/tests/ChangeLog index 357c12f..d0600d0 100644 --- a/tests/ChangeLog +++ b/tests/ChangeLog @@ -1,5 +1,10 @@ 2003-04-27 Marcus Brinkmann + * 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. diff --git a/tests/gpg/t-encrypt.c b/tests/gpg/t-encrypt.c index fa55fd6..d5359b9 100644 --- a/tests/gpg/t-encrypt.c +++ b/tests/gpg/t-encrypt.c @@ -1,4 +1,4 @@ -/* t-encrypt.c - regression test +/* t-encrypt.c - Regression test. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003 g10 Code GmbH @@ -18,99 +18,89 @@ along with GPGME; 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 -#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 (""); - 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; } diff --git a/tests/gpg/t-genkey.c b/tests/gpg/t-genkey.c index 3a56b39..3e13f02 100644 --- a/tests/gpg/t-genkey.c +++ b/tests/gpg/t-genkey.c @@ -1,4 +1,4 @@ -/* t-genkey.c - regression test +/* t-genkey.c - regression test Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2003 g10 Code GmbH diff --git a/tests/gpg/t-sign.c b/tests/gpg/t-sign.c index 1e69930..d692ac8 100644 --- a/tests/gpg/t-sign.c +++ b/tests/gpg/t-sign.c @@ -1,62 +1,53 @@ -/* 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 #include +#include #include -#include #include -#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 (""); - 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); @@ -64,89 +55,121 @@ print_data (GpgmeData dh) 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; } diff --git a/tests/gpgsm/t-encrypt.c b/tests/gpgsm/t-encrypt.c index a6f80a4..0783a9c 100644 --- a/tests/gpgsm/t-encrypt.c +++ b/tests/gpgsm/t-encrypt.c @@ -1,4 +1,4 @@ -/* t-encrypt.c - regression test +/* t-encrypt.c - Regression test. Copyright (C) 2000 Werner Koch (dd9jn) Copyright (C) 2001, 2002, 2003 g10 Code GmbH @@ -18,44 +18,36 @@ along with GPGME; 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 -#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 (""); - 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); @@ -63,73 +55,48 @@ print_data (GpgmeData dh) 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; } - - diff --git a/tests/gpgsm/t-genkey.c b/tests/gpgsm/t-genkey.c index 3f5a879..a7530ba 100644 --- a/tests/gpgsm/t-genkey.c +++ b/tests/gpgsm/t-genkey.c @@ -38,38 +38,25 @@ 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) @@ -156,7 +143,7 @@ main (int argc, char **argv) } gpgme_release (ctx); - dump_data (certreq); + print_data (certreq); gpgme_data_release (certreq); return 0; diff --git a/tests/gpgsm/t-sign.c b/tests/gpgsm/t-sign.c index b4c8332..a9af38f 100644 --- a/tests/gpgsm/t-sign.c +++ b/tests/gpgsm/t-sign.c @@ -1,118 +1,151 @@ -/* 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 #include +#include #include -#include #include -#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 (""); - 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; } -- 2.26.2