]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
more opencdk simplifications
authorNikos Mavrogiannopoulos <nmav@gnutls.org>
Fri, 30 Dec 2011 22:39:08 +0000 (00:39 +0200)
committerNikos Mavrogiannopoulos <nmav@gnutls.org>
Fri, 30 Dec 2011 22:39:08 +0000 (00:39 +0200)
lib/opencdk/kbnode.c
lib/opencdk/opencdk.h
lib/openpgp/gnutls_openpgp.c
lib/openpgp/pgp.c
lib/openpgp/privkey.c

index 8b91e19f6e2dcf18c670dc84f6c633081610fc76..3fc767410a62c71a2a370ff3affd6f0e467487fc 100644 (file)
@@ -1,6 +1,5 @@
 /* kbnode.c -  keyblock node utility functions
- * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2007, 2008, 2010
- * Free Software Foundation, Inc.
+ * Copyright (C) 1998-2003, 2007-2011 Free Software Foundation, Inc.
  *
  * Author: Timo Schulz
  *
@@ -396,6 +395,7 @@ cdk_kbnode_get_packet (cdk_kbnode_t node)
 /**
  * cdk_kbnode_read_from_mem:
  * @ret_node: the new key node
+ * @armor: whether base64 or not
  * @buf: the buffer which stores the key sequence
  * @buflen: the length of the buffer
  *
@@ -403,6 +403,7 @@ cdk_kbnode_get_packet (cdk_kbnode_t node)
  **/
 cdk_error_t
 cdk_kbnode_read_from_mem (cdk_kbnode_t * ret_node,
+                          int armor,
                           const byte * buf, size_t buflen)
 {
   cdk_stream_t inp;
@@ -418,6 +419,10 @@ cdk_kbnode_read_from_mem (cdk_kbnode_t * ret_node,
   rc = cdk_stream_tmp_from_mem (buf, buflen, &inp);
   if (rc)
     return rc;
+  
+  if (armor)
+    cdk_stream_set_armor_flag (inp, 0);
+
   rc = cdk_keydb_get_keyblock (inp, ret_node);
   if (rc)
     gnutls_assert ();
index c15abfbf4f964a5a688ddd64bb23515389583d04..85531bb7f4bed6f20f0c51b73b19a5e6b8820b96 100644 (file)
@@ -852,6 +852,7 @@ extern "C"
 
   cdk_kbnode_t cdk_kbnode_new (cdk_packet_t pkt);
   cdk_error_t cdk_kbnode_read_from_mem (cdk_kbnode_t * ret_node,
+                                        int armor,
                                         const unsigned char *buf,
                                         size_t buflen);
   cdk_error_t cdk_kbnode_write_to_mem (cdk_kbnode_t node,
index b13281c50fa5be1313edc979cc1d5c873ced2e24..0ceca8faa50399aa31a18caaff386181e6c5f594 100644 (file)
@@ -498,7 +498,7 @@ gnutls_openpgp_count_key_names (const gnutls_datum_t * cert)
       return 0;
     }
 
-  if (cdk_kbnode_read_from_mem (&knode, cert->data, cert->size))
+  if (cdk_kbnode_read_from_mem (&knode, 0, cert->data, cert->size))
     {
       gnutls_assert ();
       return 0;
index e01264448aa93dd14bf8f7a2e6eeba162d61969b..c74dc8d3f5049332fdf3588c5b920189125a1e04 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
- * Free Software Foundation, Inc.
+ * Copyright (C) 2002-2011 Free Software Foundation, Inc.
  *
  * Author: Timo Schulz, Nikos Mavrogiannopoulos
  *
@@ -89,9 +88,8 @@ gnutls_openpgp_crt_import (gnutls_openpgp_crt_t key,
                            const gnutls_datum_t * data,
                            gnutls_openpgp_crt_fmt_t format)
 {
-  cdk_stream_t inp;
   cdk_packet_t pkt;
-  int rc;
+  int rc, armor;
 
   if (data->data == NULL || data->size == 0)
     {
@@ -99,38 +97,15 @@ gnutls_openpgp_crt_import (gnutls_openpgp_crt_t key,
       return GNUTLS_E_OPENPGP_GETKEY_FAILED;
     }
 
-  if (format == GNUTLS_OPENPGP_FMT_RAW)
-    {
-      rc = cdk_kbnode_read_from_mem (&key->knode, data->data, data->size);
-      if (rc)
-        {
-          rc = _gnutls_map_cdk_rc (rc);
-          gnutls_assert ();
-          return rc;
-        }
-    }
-  else
+  if (format == GNUTLS_OPENPGP_FMT_RAW) armor = 0;
+  else armor = 1;
+
+  rc = cdk_kbnode_read_from_mem (&key->knode, armor, data->data, data->size);
+  if (rc)
     {
-      rc = cdk_stream_tmp_from_mem (data->data, data->size, &inp);
-      if (rc)
-        {
-          rc = _gnutls_map_cdk_rc (rc);
-          gnutls_assert ();
-          return rc;
-        }
-      rc = cdk_stream_set_armor_flag (inp, 0);
-      if (!rc)
-        rc = cdk_keydb_get_keyblock (inp, &key->knode);
-      cdk_stream_close (inp);
-      if (rc)
-        {
-          if (rc == CDK_Inv_Packet)
-            rc = GNUTLS_E_OPENPGP_GETKEY_FAILED;
-          else
-            rc = _gnutls_map_cdk_rc (rc);
-          gnutls_assert ();
-          return rc;
-        }
+      rc = _gnutls_map_cdk_rc (rc);
+      gnutls_assert ();
+      return rc;
     }
 
   /* Test if the import was successful. */
index f43c53b7babc84e8d6d8802be0dc74ae9fd545ad..81b0e43c560a35153f7027d931509145b057c6f5 100644 (file)
@@ -169,9 +169,8 @@ gnutls_openpgp_privkey_import (gnutls_openpgp_privkey_t key,
                                gnutls_openpgp_crt_fmt_t format,
                                const char *password, unsigned int flags)
 {
-  cdk_stream_t inp;
   cdk_packet_t pkt;
-  int rc;
+  int rc, armor;
 
   if (data->data == NULL || data->size == 0)
     {
@@ -180,43 +179,15 @@ gnutls_openpgp_privkey_import (gnutls_openpgp_privkey_t key,
     }
 
   if (format == GNUTLS_OPENPGP_FMT_RAW)
-    {
-      rc = cdk_kbnode_read_from_mem (&key->knode, data->data, data->size);
-      if (rc != 0)
-        {
-          rc = _gnutls_map_cdk_rc (rc);
-          gnutls_assert ();
-          return rc;
-        }
-    }
-  else
-    {
-      rc = cdk_stream_tmp_from_mem (data->data, data->size, &inp);
-      if (rc != 0)
-        {
-          rc = _gnutls_map_cdk_rc (rc);
-          gnutls_assert ();
-          return rc;
-        }
+    armor = 0;
+  else armor = 1;
 
-      rc = cdk_stream_set_armor_flag (inp, 0);
-      if (rc != 0)
-        {
-          rc = _gnutls_map_cdk_rc (rc);
-          cdk_stream_close (inp);
-          gnutls_assert ();
-          return rc;
-        }
-
-      rc = cdk_keydb_get_keyblock (inp, &key->knode);
-      cdk_stream_close (inp);
-
-      if (rc != 0)
-        {
-          rc = _gnutls_map_cdk_rc (rc);
-          gnutls_assert ();
-          return rc;
-        }
+  rc = cdk_kbnode_read_from_mem (&key->knode, armor, data->data, data->size);
+  if (rc != 0)
+    {
+       rc = _gnutls_map_cdk_rc (rc);
+       gnutls_assert ();
+       return rc;
     }
 
   /* Test if the import was successful. */