[gmime] Fixed the GMimeGpgContext to mostly work



commit b6af0191afa40fa133eb775457fa16f690b72caa
Author: Jeffrey Stedfast <fejj gnome org>
Date:   Sun Feb 5 22:20:07 2017 -0500

    Fixed the GMimeGpgContext to mostly work

 gmime/gmime-crypto-context.c |   12 ++++++----
 gmime/gmime-crypto-context.h |    4 +-
 gmime/gmime-gpg-context.c    |   43 +++++++++++++++++++++++++----------------
 gmime/gmime-pkcs7-context.c  |   14 +++++-------
 gmime/gmime.c                |    6 ++--
 tests/test-pgp.c             |    8 +++---
 tests/test-pkcs7.c           |    8 +++---
 7 files changed, 52 insertions(+), 43 deletions(-)
---
diff --git a/gmime/gmime-crypto-context.c b/gmime/gmime-crypto-context.c
index a06a9ef..0745321 100644
--- a/gmime/gmime-crypto-context.c
+++ b/gmime/gmime-crypto-context.c
@@ -81,7 +81,7 @@ static GMimeDecryptResult *crypto_decrypt_session (GMimeCryptoContext *ctx, cons
 static int crypto_import_keys (GMimeCryptoContext *ctx, GMimeStream *istream,
                               GError **err);
 
-static int crypto_export_keys (GMimeCryptoContext *ctx, GPtrArray *keys,
+static int crypto_export_keys (GMimeCryptoContext *ctx, const char *keys[],
                               GMimeStream *ostream, GError **err);
 
 
@@ -566,7 +566,7 @@ g_mime_crypto_context_import_keys (GMimeCryptoContext *ctx, GMimeStream *istream
 
 
 static int
-crypto_export_keys (GMimeCryptoContext *ctx, GPtrArray *keys,
+crypto_export_keys (GMimeCryptoContext *ctx, const char *keys[],
                    GMimeStream *ostream, GError **err)
 {
        g_set_error (err, GMIME_ERROR, GMIME_ERROR_NOT_SUPPORTED,
@@ -579,22 +579,24 @@ crypto_export_keys (GMimeCryptoContext *ctx, GPtrArray *keys,
 /**
  * g_mime_crypto_context_export_keys:
  * @ctx: a #GMimeCryptoContext
- * @keys: (element-type utf8): an array of key ids
+ * @keys: (element-type utf8): an array of key ids, terminated by a %NULL element
  * @ostream: output stream
  * @err: a #GError
  *
  * Exports the keys/certificates in @keys to the stream @ostream from
  * the key/certificate database controlled by @ctx.
  *
+ * If @keys is %NULL or contains only a %NULL element, then all keys
+ * will be exported.
+ *
  * Returns: %0 on success or %-1 on fail.
  **/
 int
-g_mime_crypto_context_export_keys (GMimeCryptoContext *ctx, GPtrArray *keys,
+g_mime_crypto_context_export_keys (GMimeCryptoContext *ctx, const char *keys[],
                                   GMimeStream *ostream, GError **err)
 {
        g_return_val_if_fail (GMIME_IS_CRYPTO_CONTEXT (ctx), -1);
        g_return_val_if_fail (GMIME_IS_STREAM (ostream), -1);
-       g_return_val_if_fail (keys != NULL, -1);
        
        return GMIME_CRYPTO_CONTEXT_GET_CLASS (ctx)->export_keys (ctx, keys, ostream, err);
 }
diff --git a/gmime/gmime-crypto-context.h b/gmime/gmime-crypto-context.h
index f1477ce..45c97e9 100644
--- a/gmime/gmime-crypto-context.h
+++ b/gmime/gmime-crypto-context.h
@@ -111,7 +111,7 @@ struct _GMimeCryptoContextClass {
        int                      (* import_keys) (GMimeCryptoContext *ctx, GMimeStream *istream,
                                                  GError **err);
        
-       int                      (* export_keys) (GMimeCryptoContext *ctx, GPtrArray *keys,
+       int                      (* export_keys) (GMimeCryptoContext *ctx, const char *keys[],
                                                  GMimeStream *ostream, GError **err);
        
        gboolean                 (* get_retrieve_session_key) (GMimeCryptoContext *ctx);
@@ -162,7 +162,7 @@ GMimeDecryptResult *g_mime_crypto_context_decrypt_session (GMimeCryptoContext *c
 /* key/certificate routines */
 int g_mime_crypto_context_import_keys (GMimeCryptoContext *ctx, GMimeStream *istream, GError **err);
 
-int g_mime_crypto_context_export_keys (GMimeCryptoContext *ctx, GPtrArray *keys,
+int g_mime_crypto_context_export_keys (GMimeCryptoContext *ctx, const char *keys[],
                                       GMimeStream *ostream, GError **err);
 
 gboolean g_mime_crypto_context_get_retrieve_session_key (GMimeCryptoContext *ctx);
diff --git a/gmime/gmime-gpg-context.c b/gmime/gmime-gpg-context.c
index e2b4242..401bcd3 100644
--- a/gmime/gmime-gpg-context.c
+++ b/gmime/gmime-gpg-context.c
@@ -122,7 +122,7 @@ static GMimeDecryptResult *gpg_decrypt_session (GMimeCryptoContext *ctx, const c
 static int gpg_import_keys (GMimeCryptoContext *ctx, GMimeStream *istream,
                            GError **err);
 
-static int gpg_export_keys (GMimeCryptoContext *ctx, GPtrArray *keys,
+static int gpg_export_keys (GMimeCryptoContext *ctx, const char *keys[],
                            GMimeStream *ostream, GError **err);
 
 
@@ -476,15 +476,15 @@ gpg_sign (GMimeCryptoContext *context, const char *userid, GMimeDigestAlgo diges
        if (!gpg_add_signer (gpg, userid, err))
                return -1;
        
-       gpgme_set_armor (gpg->ctx, FALSE);
-       
        if ((error = gpgme_data_new_from_cbs (&input, &gpg_stream_funcs, istream)) != GPG_ERR_NO_ERROR) {
                g_set_error (err, GMIME_GPGME_ERROR, error, _("Could not open input stream"));
+               gpgme_signers_clear (gpg->ctx);
                return -1;
        }
        
        if ((error = gpgme_data_new_from_cbs (&output, &gpg_stream_funcs, ostream)) != GPG_ERR_NO_ERROR) {
                g_set_error (err, GMIME_GPGME_ERROR, error, _("Could not open output stream"));
+               gpgme_signers_clear (gpg->ctx);
                gpgme_data_release (input);
                return -1;
        }
@@ -492,11 +492,13 @@ gpg_sign (GMimeCryptoContext *context, const char *userid, GMimeDigestAlgo diges
        /* sign the input stream */
        if ((error = gpgme_op_sign (gpg->ctx, input, output, GPGME_SIG_MODE_DETACH)) != GPG_ERR_NO_ERROR) {
                g_set_error (err, GMIME_GPGME_ERROR, error, _("Signing failed"));
+               gpgme_signers_clear (gpg->ctx);
                gpgme_data_release (output);
                gpgme_data_release (input);
                return -1;
        }
        
+       gpgme_signers_clear (gpg->ctx);
        gpgme_data_release (output);
        gpgme_data_release (input);
        
@@ -721,12 +723,6 @@ gpg_encrypt (GMimeCryptoContext *context, gboolean sign, const char *userid,
        gpgme_key_t key;
        guint i;
        
-       if (sign) {
-               g_set_error (err, GMIME_ERROR, GMIME_ERROR_NOT_SUPPORTED,
-                            _("Cannot sign and encrypt a stream at the same time using gpg"));
-               return -1;
-       }
-       
        /* create an array of recipient keys for GpgMe */
        rcpts = g_new0 (gpgme_key_t, recipients->len + 1);
        for (i = 0; i < recipients->len; i++) {
@@ -752,7 +748,21 @@ gpg_encrypt (GMimeCryptoContext *context, gboolean sign, const char *userid,
        }
        
        /* encrypt the input stream */
-       error = gpgme_op_encrypt (gpg->ctx, rcpts, gpg->encrypt_flags, input, output);
+       if (sign) {
+               if (!gpg_add_signer (gpg, userid, err)) {
+                       gpgme_data_release (output);
+                       gpgme_data_release (input);
+                       key_list_free (rcpts);
+                       return -1;
+               }
+               
+               error = gpgme_op_encrypt_sign (gpg->ctx, rcpts, gpg->encrypt_flags, input, output);
+               
+               gpgme_signers_clear (gpg->ctx);
+       } else {
+               error = gpgme_op_encrypt (gpg->ctx, rcpts, gpg->encrypt_flags, input, output);
+       }
+       
        gpgme_data_release (output);
        gpgme_data_release (input);
        key_list_free (rcpts);
@@ -882,7 +892,7 @@ gpg_import_keys (GMimeCryptoContext *context, GMimeStream *istream, GError **err
 }
 
 static int
-gpg_export_keys (GMimeCryptoContext *context, GPtrArray *keys, GMimeStream *ostream, GError **err)
+gpg_export_keys (GMimeCryptoContext *context, const char *keys[], GMimeStream *ostream, GError **err)
 {
 #ifdef ENABLE_CRYPTO
        GMimeGpgContext *gpg = (GMimeGpgContext *) context;
@@ -896,12 +906,10 @@ gpg_export_keys (GMimeCryptoContext *context, GPtrArray *keys, GMimeStream *ostr
        }
        
        /* export the key(s) */
-       for (i = 0; i < keys->len; i++) {
-               if ((error = gpgme_op_export (gpg->ctx, keys->pdata[i], 0, keydata)) != GPG_ERR_NO_ERROR) {
-                       g_set_error (err, GMIME_GPGME_ERROR, error, _("Could not export key data"));
-                       gpgme_data_release (keydata);
-                       return -1;
-               }
+       if ((error = gpgme_op_export_ext (gpg->ctx, keys, 0, keydata)) != GPG_ERR_NO_ERROR) {
+               g_set_error (err, GMIME_GPGME_ERROR, error, _("Could not export key data"));
+               gpgme_data_release (keydata);
+               return -1;
        }
        
        gpgme_data_release (keydata);
@@ -987,6 +995,7 @@ g_mime_gpg_context_new (GMimePasswordRequestFunc request_passwd)
        gpg = g_object_newv (GMIME_TYPE_GPG_CONTEXT, 0, NULL);
        gpgme_set_passphrase_cb (ctx, gpg_passphrase_cb, gpg);
        gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
+       gpgme_set_armor (ctx, TRUE);
        gpg->ctx = ctx;
        
        crypto = (GMimeCryptoContext *) gpg;
diff --git a/gmime/gmime-pkcs7-context.c b/gmime/gmime-pkcs7-context.c
index 5374452..84e4717 100644
--- a/gmime/gmime-pkcs7-context.c
+++ b/gmime/gmime-pkcs7-context.c
@@ -111,7 +111,7 @@ static GMimeDecryptResult *pkcs7_decrypt (GMimeCryptoContext *ctx, GMimeStream *
 static int pkcs7_import_keys (GMimeCryptoContext *ctx, GMimeStream *istream,
                              GError **err);
 
-static int pkcs7_export_keys (GMimeCryptoContext *ctx, GPtrArray *keys,
+static int pkcs7_export_keys (GMimeCryptoContext *ctx, const char *keys[],
                              GMimeStream *ostream, GError **err);
 
 static gboolean pkcs7_get_always_trust (GMimeCryptoContext *context);
@@ -858,7 +858,7 @@ pkcs7_import_keys (GMimeCryptoContext *context, GMimeStream *istream, GError **e
 }
 
 static int
-pkcs7_export_keys (GMimeCryptoContext *context, GPtrArray *keys, GMimeStream *ostream, GError **err)
+pkcs7_export_keys (GMimeCryptoContext *context, const char *keys[], GMimeStream *ostream, GError **err)
 {
 #ifdef ENABLE_CRYPTO
        GMimePkcs7Context *pkcs7 = (GMimePkcs7Context *) context;
@@ -872,12 +872,10 @@ pkcs7_export_keys (GMimeCryptoContext *context, GPtrArray *keys, GMimeStream *os
        }
        
        /* export the key(s) */
-       for (i = 0; i < keys->len; i++) {
-               if ((error = gpgme_op_export (pkcs7->ctx, keys->pdata[i], 0, keydata)) != GPG_ERR_NO_ERROR) {
-                       g_set_error (err, GMIME_GPGME_ERROR, error, _("Could not export key data"));
-                       gpgme_data_release (keydata);
-                       return -1;
-               }
+       if ((error = gpgme_op_export_ext (pkcs7->ctx, keys, 0, keydata)) != GPG_ERR_NO_ERROR) {
+               g_set_error (err, GMIME_GPGME_ERROR, error, _("Could not export key data"));
+               gpgme_data_release (keydata);
+               return -1;
        }
        
        gpgme_data_release (keydata);
diff --git a/gmime/gmime.c b/gmime/gmime.c
index 5174581..567e550 100644
--- a/gmime/gmime.c
+++ b/gmime/gmime.c
@@ -26,7 +26,7 @@
 #include <stdlib.h>
 #include <time.h>
 
-#ifdef ENABLE_SMIME
+#ifdef ENABLE_CRYPTO
 #include <gpgme.h>
 #endif
 
@@ -147,10 +147,10 @@ g_mime_init (guint32 flags)
        g_mime_iconv_utils_init ();
        g_mime_iconv_init ();
        
-#ifdef ENABLE_SMIME
+#ifdef ENABLE_CRYPTO
        /* gpgme_check_version() initializes GpgMe */
        gpgme_check_version (NULL);
-#endif /* ENABLE_SMIME */
+#endif /* ENABLE_CRYPTO */
        
        gmime_gpgme_error_quark = g_quark_from_static_string ("gmime-gpgme");
        gmime_error_quark = g_quark_from_static_string ("gmime");
diff --git a/tests/test-pgp.c b/tests/test-pgp.c
index c69d64f..5349ab8 100644
--- a/tests/test-pgp.c
+++ b/tests/test-pgp.c
@@ -197,8 +197,8 @@ test_export (GMimeCryptoContext *ctx, const char *path)
        const char *inbuf, *outbuf;
        size_t inlen, outlen;
        Exception *ex = NULL;
+       const char *keys[2];
        GError *err = NULL;
-       GPtrArray *keys;
        int fd;
        
        if ((fd = open (path, O_RDONLY, 0)) == -1)
@@ -210,13 +210,13 @@ test_export (GMimeCryptoContext *ctx, const char *path)
        g_mime_stream_reset (istream);
        g_object_unref (ostream);
        
-       keys = g_ptr_array_new ();
-       g_ptr_array_add (keys, "no.user@no.domain");
+       keys[0] = "no.user@no.domain";
+       keys[1] = NULL;
        
        ostream = g_mime_stream_mem_new ();
        
        g_mime_crypto_context_export_keys (ctx, keys, ostream, &err);
-       g_ptr_array_free (keys, TRUE);
+       
        if (err != NULL) {
                ex = exception_new ("%s", err->message);
                g_object_unref (istream);
diff --git a/tests/test-pkcs7.c b/tests/test-pkcs7.c
index 003e323..3a99df8 100644
--- a/tests/test-pkcs7.c
+++ b/tests/test-pkcs7.c
@@ -194,8 +194,8 @@ test_export (GMimeCryptoContext *ctx, const char *path)
        const char *inbuf, *outbuf;
        size_t inlen, outlen;
        Exception *ex = NULL;
+       const char *keys[2];
        GError *err = NULL;
-       GPtrArray *keys;
        int fd;
        
        if ((fd = open (path, O_RDONLY, 0)) == -1)
@@ -207,13 +207,13 @@ test_export (GMimeCryptoContext *ctx, const char *path)
        g_mime_stream_reset (istream);
        g_object_unref (ostream);
        
-       keys = g_ptr_array_new ();
-       g_ptr_array_add (keys, "alice example net");
+       keys[0] = "alice example net";
+       keys[1] = NULL;
        
        ostream = g_mime_stream_mem_new ();
        
        g_mime_crypto_context_export_keys (ctx, keys, ostream, &err);
-       g_ptr_array_free (keys, TRUE);
+       
        if (err != NULL) {
                ex = exception_new ("%s", err->message);
                g_object_unref (istream);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]