Do not include the removed file status-table.h
[gpgme.git] / src / decrypt.c
index 66250b129f58f94db8020ebe88cbf148c459b06c..43945ec34a600c378016fe0458ea7e32a833f6ed 100644 (file)
@@ -3,17 +3,17 @@
    Copyright (C) 2001, 2002, 2003, 2004 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 Lesser General Public License as
    published by the Free Software Foundation; either version 2.1 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
    Lesser General Public License for more details.
-   
+
    You should have received a copy of the GNU Lesser General Public
    License along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 #include <string.h>
 #include <errno.h>
 
+#include "debug.h"
 #include "gpgme.h"
 #include "util.h"
 #include "context.h"
 #include "ops.h"
 
+
 \f
 typedef struct
 {
@@ -38,7 +40,7 @@ typedef struct
 
   int okay;
   int failed;
-  
+
   /* A pointer to the next pointer of the last recipient in the list.
      This makes appending new invalid signers painless while
      preserving the order.  */
@@ -74,11 +76,44 @@ gpgme_op_decrypt_result (gpgme_ctx_t ctx)
   op_data_t opd;
   gpgme_error_t err;
 
+  TRACE_BEG (DEBUG_CTX, "gpgme_op_decrypt_result", ctx);
+
   err = _gpgme_op_data_lookup (ctx, OPDATA_DECRYPT, &hook, -1, NULL);
   opd = hook;
   if (err || !opd)
-    return NULL;
+    {
+      TRACE_SUC0 ("result=(null)");
+      return NULL;
+    }
+
+  if (_gpgme_debug_trace ())
+    {
+      gpgme_recipient_t rcp;
+
+      if (opd->result.unsupported_algorithm)
+       {
+         TRACE_LOG1 ("result: unsupported_algorithm: %s",
+                     opd->result.unsupported_algorithm);
+       }
+      if (opd->result.wrong_key_usage)
+       {
+         TRACE_LOG ("result: wrong key usage");
+       }
+      rcp = opd->result.recipients;
+      while (rcp)
+       {
+         TRACE_LOG3 ("result: recipient: keyid=%s, pubkey_algo=%i, "
+                     "status=%s", rcp->keyid, rcp->pubkey_algo,
+                     gpg_strerror (rcp->status));
+         rcp = rcp->next;
+       }
+      if (opd->result.file_name)
+       {
+         TRACE_LOG1 ("result: original file name: %s", opd->result.file_name);
+       }
+    }
 
+  TRACE_SUC1 ("result=%p", &opd->result);
   return &opd->result;
 }
 
@@ -92,7 +127,7 @@ parse_enc_to (char *args, gpgme_recipient_t *recp)
 
   rec = malloc (sizeof (*rec));
   if (!rec)
-    return gpg_error_from_errno (errno);
+    return gpg_error_from_syserror ();
 
   rec->next = NULL;
   rec->keyid = rec->_keyid;
@@ -119,7 +154,7 @@ parse_enc_to (char *args, gpgme_recipient_t *recp)
 
   if (*args)
     {
-      errno = 0;
+      gpg_err_set_errno (0);
       rec->pubkey_algo = strtol (args, &tail, 0);
       if (errno || args == tail || *tail != ' ')
        {
@@ -166,6 +201,10 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code,
        return gpg_error (GPG_ERR_NO_DATA);
       break;
 
+    case GPGME_STATUS_DECRYPTION_INFO:
+      /* Fixme: Provide a way to return the used symmetric algorithm. */
+      break;
+
     case GPGME_STATUS_DECRYPTION_OKAY:
       opd->okay = 1;
       break;
@@ -180,7 +219,6 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code,
          related to the backend.  */
       {
        const char d_alg[] = "decrypt.algorithm";
-       const char u_alg[] = "Unsupported_Algorithm";
        const char k_alg[] = "decrypt.keyusage";
 
        if (!strncmp (args, d_alg, sizeof (d_alg) - 1))
@@ -189,11 +227,12 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code,
            while (*args == ' ')
              args++;
 
-           if (!strncmp (args, u_alg, sizeof (u_alg) - 1))
+           if (gpg_err_code (atoi (args)) == GPG_ERR_UNSUPPORTED_ALGORITHM)
              {
                char *end;
 
-               args += sizeof (u_alg) - 1;
+               while (*args && *args != ' ')
+                 args++;
                while (*args == ' ')
                  args++;
 
@@ -205,7 +244,7 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code,
                  {
                    opd->result.unsupported_algorithm = strdup (args);
                    if (!opd->result.unsupported_algorithm)
-                     return gpg_error_from_errno (errno);
+                     return gpg_error_from_syserror ();
                  }
              }
          }
@@ -215,8 +254,7 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code,
            while (*args == ' ')
              args++;
 
-           err = _gpgme_map_gnupg_error (args);
-           if (gpg_err_code (err) == GPG_ERR_WRONG_KEY_USAGE)
+           if (gpg_err_code (atoi (args)) == GPG_ERR_WRONG_KEY_USAGE)
              opd->result.wrong_key_usage = 1;
          }
       }
@@ -253,7 +291,7 @@ _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code,
       err = _gpgme_parse_plaintext (args, &opd->result.file_name);
       if (err)
        return err;
-      
+
     default:
       break;
     }
@@ -332,7 +370,16 @@ gpgme_error_t
 gpgme_op_decrypt_start (gpgme_ctx_t ctx, gpgme_data_t cipher,
                        gpgme_data_t plain)
 {
-  return decrypt_start (ctx, 0, cipher, plain);
+  gpgme_error_t err;
+
+  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt_start", ctx,
+             "cipher=%p, plain=%p", cipher, plain);
+
+  if (!ctx)
+    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
+  
+  err = decrypt_start (ctx, 0, cipher, plain);
+  return TRACE_ERR (err);
 }
 
 
@@ -341,8 +388,16 @@ gpgme_op_decrypt_start (gpgme_ctx_t ctx, gpgme_data_t cipher,
 gpgme_error_t
 gpgme_op_decrypt (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain)
 {
-  gpgme_error_t err = decrypt_start (ctx, 1, cipher, plain);
+  gpgme_error_t err;
+
+  TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt", ctx,
+             "cipher=%p, plain=%p", cipher, plain);
+
+  if (!ctx)
+    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
+  
+  err = decrypt_start (ctx, 1, cipher, plain);
   if (!err)
     err = _gpgme_wait_one (ctx);
-  return err;
+  return TRACE_ERR (err);
 }