[gmime] We no longer need to pass a GMimeCryptoContext to some methods



commit 059e294ae0f2625bee3ed00520b967f693caf93b
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Sat Feb 18 10:52:54 2017 -0500

    We no longer need to pass a GMimeCryptoContext to some methods

 examples/basic-example.c          |   20 ++---------
 gmime/gmime-crypto-context.c      |    2 -
 gmime/gmime-multipart-encrypted.c |   68 +++++++++++++++++++-----------------
 gmime/gmime-multipart-encrypted.h |    2 -
 gmime/gmime-multipart-signed.c    |   46 ++++++++++++++-----------
 gmime/gmime-multipart-signed.h    |    4 +--
 tests/test-pgpmime.c              |    4 +-
 tests/test-smime.c                |    4 +-
 8 files changed, 71 insertions(+), 79 deletions(-)
---
diff --git a/examples/basic-example.c b/examples/basic-example.c
index fa96b74..fb7ebce 100644
--- a/examples/basic-example.c
+++ b/examples/basic-example.c
@@ -139,8 +139,6 @@ count_parts_in_message (GMimeMessage *message)
 static void
 verify_foreach_callback (GMimeObject *parent, GMimeObject *part, gpointer user_data)
 {
-       GMimeCryptoContext *ctx = user_data;
-       
        if (GMIME_IS_MULTIPART_SIGNED (part)) {
                /* this is a multipart/signed part, so we can verify the pgp signature */
                GMimeMultipartSigned *mps = (GMimeMultipartSigned *) part;
@@ -150,7 +148,7 @@ verify_foreach_callback (GMimeObject *parent, GMimeObject *part, gpointer user_d
                const char *str;
                int i;
                
-               if (!(signatures = g_mime_multipart_signed_verify (mps, ctx, &err))) {
+               if (!(signatures = g_mime_multipart_signed_verify (mps, &err))) {
                        /* an error occured - probably couldn't start gpg? */
                        
                        /* for more information about GError, see:
@@ -178,10 +176,10 @@ verify_foreach_callback (GMimeObject *parent, GMimeObject *part, gpointer user_d
 }
 
 static void
-verify_signed_parts (GMimeMessage *message, GMimeCryptoContext *ctx)
+verify_signed_parts (GMimeMessage *message)
 {
        /* descend the mime tree and verify any signed parts */
-       g_mime_message_foreach (message, verify_foreach_callback, ctx);
+       g_mime_message_foreach (message, verify_foreach_callback, NULL);
 }
 #endif
 
@@ -306,18 +304,8 @@ int main (int argc, char **argv)
        
 #ifndef G_OS_WIN32
 #ifdef ENABLE_CRYPTOGRAPHY
-       /* create our crypto context */
-       ctx = g_mime_gpg_context_new (request_passwd, path);
-       
-       /* don't allow auto key-retrival */
-       g_mime_gpg_context_set_auto_key_retrieve ((GMimeGpgContext *) ctx, FALSE);
-       
-       /* set the always_trust flag so that gpg will be spawned with `gpg --always-trust` */
-       g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) ctx, TRUE);
-       
        /* verify any signed parts */
-       verify_signed_parts (message, ctx);
-       g_object_unref (ctx);
+       verify_signed_parts (message);
 #endif
 #endif
        
diff --git a/gmime/gmime-crypto-context.c b/gmime/gmime-crypto-context.c
index d0038b3..dd367d7 100644
--- a/gmime/gmime-crypto-context.c
+++ b/gmime/gmime-crypto-context.c
@@ -177,8 +177,6 @@ g_mime_crypto_context_shutdown (void)
 void
 g_mime_crypto_context_register (const char *protocol, GMimeCryptoContextNewFunc callback)
 {
-       GMimeCryptoContextNewFunc func;
-       
        g_return_if_fail (protocol != NULL);
        g_return_if_fail (callback != NULL);
        
diff --git a/gmime/gmime-multipart-encrypted.c b/gmime/gmime-multipart-encrypted.c
index 640d98a..09194bb 100644
--- a/gmime/gmime-multipart-encrypted.c
+++ b/gmime/gmime-multipart-encrypted.c
@@ -272,12 +272,11 @@ g_mime_data_wrapper_get_decoded_stream (GMimeDataWrapper *wrapper)
 /**
  * g_mime_multipart_encrypted_decrypt:
  * @mpe: multipart/encrypted object
- * @ctx: decryption context
  * @result: a #GMimeDecryptionResult
  * @err: a #GError
  *
  * Attempts to decrypt the encrypted MIME part contained within the
- * multipart/encrypted object @mpe using the @ctx decryption context.
+ * multipart/encrypted object @mpe.
  *
  * If @result is non-%NULL, then on a successful decrypt operation, it will be
  * updated to point to a newly-allocated #GMimeDecryptResult with signature
@@ -289,27 +288,24 @@ g_mime_data_wrapper_get_decoded_stream (GMimeDataWrapper *wrapper)
  * @err to provide information as to why the failure occured.
  **/
 GMimeObject *
-g_mime_multipart_encrypted_decrypt (GMimeMultipartEncrypted *mpe, GMimeCryptoContext *ctx,
-                                   GMimeDecryptResult **result, GError **err)
+g_mime_multipart_encrypted_decrypt (GMimeMultipartEncrypted *mpe, GMimeDecryptResult **result, GError **err)
 {
-       return g_mime_multipart_encrypted_decrypt_session (mpe, ctx, NULL, result, err);
+       return g_mime_multipart_encrypted_decrypt_session (mpe, NULL, result, err);
 }
 
 /**
  * g_mime_multipart_encrypted_decrypt_session:
  * @mpe: multipart/encrypted object
- * @ctx: decryption context
  * @session_key: session key to use
  * @result: a #GMimeDecryptionResult
  * @err: a #GError
  *
  * Attempts to decrypt the encrypted MIME part contained within the
- * multipart/encrypted object @mpe using the @ctx decryption context
- * trying only the supplied session key.  If @session_key is
- * non-%NULL, but is not valid for the ciphertext, the decryption will
- * fail even if other available secret key material may have been able
- * to decrypt it. If @session_key is %NULL, this does the same thing
- * as g_mime_multipart_encrypted_decrypt().
+ * multipart/encrypted object @mpe trying only the supplied session key.
+ * If @session_key is non-%NULL, but is not valid for the ciphertext,
+ * the decryption will fail even if other available secret key material
+ * may have been able to decrypt it. If @session_key is %NULL, this does
+ * the same thing as g_mime_multipart_encrypted_decrypt().
  *
  * When non-%NULL, @session_key should be a %NULL-terminated string,
  * such as the one returned by g_mime_decrypt_result_get_session_key()
@@ -325,9 +321,8 @@ g_mime_multipart_encrypted_decrypt (GMimeMultipartEncrypted *mpe, GMimeCryptoCon
  * @err to provide information as to why the failure occured.
  **/
 GMimeObject *
-g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe, GMimeCryptoContext *ctx,
-                                           const char *session_key, GMimeDecryptResult **result,
-                                           GError **err)
+g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe, const char *session_key,
+                                           GMimeDecryptResult **result, GError **err)
 {
        GMimeObject *decrypted, *version, *encrypted;
        GMimeStream *stream, *ciphertext;
@@ -337,37 +332,42 @@ g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe, GMimeC
        GMimeDataWrapper *wrapper;
        GMimeFilter *crlf_filter;
        GMimeDecryptResult *res;
+       GMimeCryptoContext *ctx;
        GMimeParser *parser;
        char *content_type;
        
        g_return_val_if_fail (GMIME_IS_MULTIPART_ENCRYPTED (mpe), NULL);
-       g_return_val_if_fail (GMIME_IS_CRYPTO_CONTEXT (ctx), NULL);
        
        if (result)
                *result = NULL;
        
-       protocol = g_mime_object_get_content_type_parameter (GMIME_OBJECT (mpe), "protocol");
-       supported = g_mime_crypto_context_get_encryption_protocol (ctx);
-       
-       if (protocol) {
-               /* make sure the protocol matches the crypto encrypt protocol */
-               if (!supported || g_ascii_strcasecmp (supported, protocol) != 0) {
-                       g_set_error (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
-                                    _("Cannot decrypt multipart/encrypted part: unsupported encryption 
protocol '%s'."),
-                                    protocol);
-                       
-                       return NULL;
-               }
-       } else if (supported != NULL) {
-               /* *shrug* - I guess just go on as if they match? */
-               protocol = supported;
-       } else {
+       if (!(protocol = g_mime_object_get_content_type_parameter (GMIME_OBJECT (mpe), "protocol"))) {
                g_set_error_literal (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
                                     _("Cannot decrypt multipart/encrypted part: unspecified encryption 
protocol."));
                
                return NULL;
        }
        
+       if (!(ctx = g_mime_crypto_context_new (protocol))) {
+               g_set_error (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
+                            _("Cannot verify multipart/encrypted part: unregistered encryption protocol 
'%s'."),
+                            protocol);
+               
+               return NULL;
+       }
+       
+       supported = g_mime_crypto_context_get_encryption_protocol (ctx);
+       
+       /* make sure the protocol matches the crypto encrypt protocol */
+       if (!supported || g_ascii_strcasecmp (supported, protocol) != 0) {
+               g_set_error (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
+                            _("Cannot decrypt multipart/encrypted part: unsupported encryption protocol 
'%s'."),
+                            protocol);
+               g_object_unref (ctx);
+               
+               return NULL;
+       }
+       
        version = g_mime_multipart_get_part (GMIME_MULTIPART (mpe), GMIME_MULTIPART_ENCRYPTED_VERSION);
        
        /* make sure the protocol matches the version part's content-type */
@@ -377,6 +377,7 @@ g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe, GMimeC
                                     _("Cannot decrypt multipart/encrypted part: content-type does not match 
protocol."));
                
                g_free (content_type);
+               g_object_unref (ctx);
                
                return NULL;
        }
@@ -388,6 +389,7 @@ g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe, GMimeC
        if (!g_mime_content_type_is_type (mime_type, "application", "octet-stream")) {
                g_set_error_literal (err, GMIME_ERROR, GMIME_ERROR_PARSE_ERROR,
                                     _("Cannot decrypt multipart/encrypted part: unexpected content type."));
+               g_object_unref (ctx);
                
                return NULL;
        }
@@ -408,6 +410,7 @@ g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe, GMimeC
                g_object_unref (filtered_stream);
                g_object_unref (ciphertext);
                g_object_unref (stream);
+               g_object_unref (ctx);
                
                return NULL;
        }
@@ -415,6 +418,7 @@ g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe, GMimeC
        g_mime_stream_flush (filtered_stream);
        g_object_unref (filtered_stream);
        g_object_unref (ciphertext);
+       g_object_unref (ctx);
        
        g_mime_stream_reset (stream);
        parser = g_mime_parser_new ();
diff --git a/gmime/gmime-multipart-encrypted.h b/gmime/gmime-multipart-encrypted.h
index 0e53b1e..bf09fb1 100644
--- a/gmime/gmime-multipart-encrypted.h
+++ b/gmime/gmime-multipart-encrypted.h
@@ -70,12 +70,10 @@ int g_mime_multipart_encrypted_encrypt (GMimeMultipartEncrypted *mpe, GMimeObjec
                                        GPtrArray *recipients, GError **err);
 
 GMimeObject *g_mime_multipart_encrypted_decrypt (GMimeMultipartEncrypted *mpe,
-                                                GMimeCryptoContext *ctx,
                                                 GMimeDecryptResult **result,
                                                 GError **err);
 
 GMimeObject *g_mime_multipart_encrypted_decrypt_session (GMimeMultipartEncrypted *mpe,
-                                                        GMimeCryptoContext *ctx,
                                                         const char *session_key,
                                                         GMimeDecryptResult **result,
                                                         GError **err);
diff --git a/gmime/gmime-multipart-signed.c b/gmime/gmime-multipart-signed.c
index 60fa59b..e667351 100644
--- a/gmime/gmime-multipart-signed.c
+++ b/gmime/gmime-multipart-signed.c
@@ -359,11 +359,10 @@ check_protocol_supported (const char *protocol, const char *supported)
 /**
  * g_mime_multipart_signed_verify:
  * @mps: multipart/signed object
- * @ctx: encryption crypto context
  * @err: exception
  *
  * Attempts to verify the signed MIME part contained within the
- * multipart/signed object @mps using the @ctx crypto context.
+ * multipart/signed object @mps.
  *
  * Returns: (transfer full): a new #GMimeSignatureList object on
  * success or %NULL on fail. If the verification fails, an exception
@@ -371,8 +370,7 @@ check_protocol_supported (const char *protocol, const char *supported)
  * occured.
  **/
 GMimeSignatureList *
-g_mime_multipart_signed_verify (GMimeMultipartSigned *mps, GMimeCryptoContext *ctx,
-                               GError **err)
+g_mime_multipart_signed_verify (GMimeMultipartSigned *mps, GError **err)
 {
        const char *supported, *protocol, *micalg;
        GMimeObject *content, *signature;
@@ -381,11 +379,11 @@ g_mime_multipart_signed_verify (GMimeMultipartSigned *mps, GMimeCryptoContext *c
        GMimeStream *filtered_stream;
        GMimeDataWrapper *wrapper;
        GMimeFilter *crlf_filter;
+       GMimeCryptoContext *ctx;
        GMimeDigestAlgo digest;
        char *content_type;
        
        g_return_val_if_fail (GMIME_IS_MULTIPART_SIGNED (mps), NULL);
-       g_return_val_if_fail (GMIME_IS_CRYPTO_CONTEXT (ctx), NULL);
        
        if (g_mime_multipart_get_count ((GMimeMultipart *) mps) < 2) {
                g_set_error_literal (err, GMIME_ERROR, GMIME_ERROR_PARSE_ERROR,
@@ -393,25 +391,31 @@ g_mime_multipart_signed_verify (GMimeMultipartSigned *mps, GMimeCryptoContext *c
                return NULL;
        }
        
-       protocol = g_mime_object_get_content_type_parameter (GMIME_OBJECT (mps), "protocol");
+       if (!(protocol = g_mime_object_get_content_type_parameter (GMIME_OBJECT (mps), "protocol"))) {
+               g_set_error_literal (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
+                                    _("Cannot verify multipart/signed part: unspecified signature 
protocol."));
+               
+               return NULL;
+       }
+       
        micalg = g_mime_object_get_content_type_parameter (GMIME_OBJECT (mps), "micalg");
        
+       if (!(ctx = g_mime_crypto_context_new (protocol))) {
+               g_set_error (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
+                            _("Cannot verify multipart/signed part: unregistered signature protocol '%s'."),
+                            protocol);
+               
+               return NULL;
+       }
+       
        supported = g_mime_crypto_context_get_signature_protocol (ctx);
        
-       if (protocol) {
-               /* make sure the protocol matches the crypto sign protocol */
-               if (!check_protocol_supported (protocol, supported)) {
-                       g_set_error (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
-                                    _("Cannot verify multipart/signed part: unsupported signature protocol 
'%s'."),
-                                    protocol);
-                       return NULL;
-               }
-       } else if (supported != NULL) {
-               /* *shrug* - I guess just go on as if they match? */
-               protocol = supported;
-       } else {
-               g_set_error_literal (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
-                                    _("Cannot verify multipart/signed part: unspecified signature 
protocol."));
+       /* make sure the protocol matches the crypto sign protocol */
+       if (!check_protocol_supported (protocol, supported)) {
+               g_set_error (err, GMIME_ERROR, GMIME_ERROR_PROTOCOL_ERROR,
+                            _("Cannot verify multipart/signed part: unsupported signature protocol '%s'."),
+                            protocol);
+               g_object_unref (ctx);
                
                return NULL;
        }
@@ -424,6 +428,7 @@ g_mime_multipart_signed_verify (GMimeMultipartSigned *mps, GMimeCryptoContext *c
                g_set_error_literal (err, GMIME_ERROR, GMIME_ERROR_PARSE_ERROR,
                                     _("Cannot verify multipart/signed part: signature content-type does not 
match protocol."));
                g_free (content_type);
+               g_object_unref (ctx);
                
                return NULL;
        }
@@ -471,6 +476,7 @@ g_mime_multipart_signed_verify (GMimeMultipartSigned *mps, GMimeCryptoContext *c
        
        g_object_unref (sigstream);
        g_object_unref (stream);
+       g_object_unref (ctx);
        
        return signatures;
 }
diff --git a/gmime/gmime-multipart-signed.h b/gmime/gmime-multipart-signed.h
index c71373f..5ccd1cc 100644
--- a/gmime/gmime-multipart-signed.h
+++ b/gmime/gmime-multipart-signed.h
@@ -68,9 +68,7 @@ int g_mime_multipart_signed_sign (GMimeMultipartSigned *mps, GMimeObject *conten
                                  GMimeCryptoContext *ctx, const char *userid,
                                  GMimeDigestAlgo digest, GError **err);
 
-GMimeSignatureList *g_mime_multipart_signed_verify (GMimeMultipartSigned *mps,
-                                                   GMimeCryptoContext *ctx,
-                                                   GError **err);
+GMimeSignatureList *g_mime_multipart_signed_verify (GMimeMultipartSigned *mps, GError **err);
 
 G_END_DECLS
 
diff --git a/tests/test-pgpmime.c b/tests/test-pgpmime.c
index 4403a17..64bafec 100644
--- a/tests/test-pgpmime.c
+++ b/tests/test-pgpmime.c
@@ -244,7 +244,7 @@ test_multipart_signed (GMimeCryptoContext *ctx)
        mps = (GMimeMultipartSigned *) message->mime_part;
        
        v(fputs ("Trying to verify signature... ", stdout));
-       if (!(signatures = g_mime_multipart_signed_verify (mps, ctx, &err))) {
+       if (!(signatures = g_mime_multipart_signed_verify (mps, &err))) {
                ex = exception_new ("%s", err->message);
                v(fputs ("failed.\n", stdout));
                g_error_free (err);
@@ -377,7 +377,7 @@ test_multipart_encrypted (GMimeCryptoContext *ctx, gboolean sign,
        mpe = (GMimeMultipartEncrypted *) message->mime_part;
        
        /* okay, now to test our decrypt function... */
-       decrypted = g_mime_multipart_encrypted_decrypt_session (mpe, ctx, session_key, &result, &err);
+       decrypted = g_mime_multipart_encrypted_decrypt_session (mpe, session_key, &result, &err);
        if (!decrypted || err != NULL) {
                ex = exception_new ("decryption failed: %s", err->message);
                g_error_free (err);
diff --git a/tests/test-smime.c b/tests/test-smime.c
index d8eecb7..b031983 100644
--- a/tests/test-smime.c
+++ b/tests/test-smime.c
@@ -245,7 +245,7 @@ test_multipart_signed (GMimeCryptoContext *ctx)
        mps = (GMimeMultipartSigned *) message->mime_part;
        
        v(fputs ("Trying to verify signature... ", stdout));
-       if (!(signatures = g_mime_multipart_signed_verify (mps, ctx, &err))) {
+       if (!(signatures = g_mime_multipart_signed_verify (mps, &err))) {
                ex = exception_new ("%s", err->message);
                v(fputs ("failed.\n", stdout));
                g_error_free (err);
@@ -359,7 +359,7 @@ test_multipart_encrypted (GMimeCryptoContext *ctx, gboolean sign)
        mpe = (GMimeMultipartEncrypted *) message->mime_part;
        
        /* okay, now to test our decrypt function... */
-       decrypted = g_mime_multipart_encrypted_decrypt (mpe, ctx, &result, &err);
+       decrypted = g_mime_multipart_encrypted_decrypt (mpe, &result, &err);
        if (!decrypted || err != NULL) {
                ex = exception_new ("decryption failed: %s", err->message);
                g_object_unref (cleartext);


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