tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / crypto_abe.c
index f52cd521316429c66727fc7fa3863cca3626086a..1d69a01aeb9d3ffb91197e891f21cc135ee9c7d6 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.  Copyright (C) 2001-2014 Christian Grothoff
      (and other contributing authors)
 
-     GNUnet 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 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet 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.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 
 */
 
@@ -53,12 +53,14 @@ init_aes( element_t k, int enc,
   int rc;
   int key_len;
   unsigned char* key_buf;
-  
+
   key_len = element_length_in_bytes(k) < 33 ? 3 : element_length_in_bytes(k);
   key_buf = (unsigned char*) malloc(key_len);
   element_to_bytes(key_buf, k);
 
-  memcpy (key->aes_key, key_buf, GNUNET_CRYPTO_AES_KEY_LENGTH); 
+  GNUNET_memcpy (key->aes_key,
+                 key_buf,
+                 GNUNET_CRYPTO_AES_KEY_LENGTH);
   GNUNET_assert (0 ==
                  gcry_cipher_open (handle, GCRY_CIPHER_AES256,
                                    GCRY_CIPHER_MODE_CFB, 0));
@@ -123,16 +125,16 @@ aes_128_cbc_decrypt( char* ct,
   unsigned char iv[16];
   char* tmp;
   uint32_t len;
-  
+
   init_aes(k, 1, &handle, &skey, iv);
 
   tmp = GNUNET_malloc (size);
 
   //AES_cbc_encrypt(ct->data, pt->data, ct->len, &key, iv, AES_DECRYPT);
-  GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size)); 
+  GNUNET_assert (0 == gcry_cipher_decrypt (handle, tmp, size, ct, size));
   gcry_cipher_close (handle);
   /* TODO make less crufty */
-  
+
   /* get real length */
   len = 0;
   len = len
@@ -159,7 +161,10 @@ GNUNET_CRYPTO_cpabe_create_master_key (void)
 void
 GNUNET_CRYPTO_cpabe_delete_master_key (struct GNUNET_CRYPTO_AbeMasterKey *key)
 {
-  gabe_msk_free (key->msk); //For some reason free of pub implicit?
+  gabe_msk_free (key->msk);
+  gabe_pub_free (key->pub);
+  //GNUNET_free (key->msk);
+  //gabe_msk_free (key->msk); //For some reason free of pub implicit?
   GNUNET_free (key);
 }
 
@@ -170,20 +175,24 @@ GNUNET_CRYPTO_cpabe_create_key (struct GNUNET_CRYPTO_AbeMasterKey *key,
   struct GNUNET_CRYPTO_AbeKey *prv_key;
   int size;
   char *tmp;
-  
+
   prv_key = GNUNET_new (struct GNUNET_CRYPTO_AbeKey);
   prv_key->prv = gabe_keygen(key->pub, key->msk, attrs);
   size = gabe_pub_serialize(key->pub, &tmp);
   prv_key->pub = gabe_pub_unserialize(tmp, size);
+  GNUNET_free (tmp);
   GNUNET_assert (NULL != prv_key->prv);
   return prv_key;
 }
 
 void
-GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key)
+GNUNET_CRYPTO_cpabe_delete_key (struct GNUNET_CRYPTO_AbeKey *key,
+                                int delete_pub)
 {
   //Memory management in gabe is buggy
-  //gabe_prv_free (prv);
+  gabe_prv_free (key->prv);
+  if (GNUNET_YES == delete_pub)
+    gabe_pub_free (key->pub);
   GNUNET_free (key);
 }
 
@@ -197,7 +206,7 @@ write_cpabe (void **result,
 {
   char *ptr;
   uint32_t *len;
-  
+
   *result = GNUNET_malloc (12 + cph_buf_len + aes_buf_len);
   ptr = *result;
   len = (uint32_t*) ptr;
@@ -206,12 +215,12 @@ write_cpabe (void **result,
   len = (uint32_t*) ptr;
   *len = htonl (aes_buf_len);
   ptr += 4;
-  memcpy (ptr, aes_buf, aes_buf_len);
+  GNUNET_memcpy (ptr, aes_buf, aes_buf_len);
   ptr += aes_buf_len;
   len = (uint32_t*) ptr;
   *len = htonl (cph_buf_len);
   ptr += 4;
-  memcpy (ptr, cph_buf, cph_buf_len);
+  GNUNET_memcpy (ptr, cph_buf, cph_buf_len);
   return 12 + cph_buf_len + aes_buf_len;
 }
 
@@ -234,13 +243,13 @@ read_cpabe (const void *data,
   *aes_buf_len = ntohl (*len);
   ptr += 4;
   *aes_buf = GNUNET_malloc (*aes_buf_len);
-  memcpy(*aes_buf, ptr, *aes_buf_len);
+  GNUNET_memcpy (*aes_buf, ptr, *aes_buf_len);
   ptr += *aes_buf_len;
   len = (uint32_t*)ptr;
   *cph_buf_len = ntohl (*len);
   ptr += 4;
   *cph_buf = GNUNET_malloc (*cph_buf_len);
-  memcpy(*cph_buf, ptr, *cph_buf_len);
+  GNUNET_memcpy (*cph_buf, ptr, *cph_buf_len);
 
   return buf_len;
 }
@@ -266,6 +275,7 @@ GNUNET_CRYPTO_cpabe_encrypt (const void *block,
   cph_buf_len = gabe_cph_serialize(cph,
                                 &cph_buf);
   gabe_cph_free(cph);
+  GNUNET_free (cph);
   plt = GNUNET_memdup (block, size);
   aes_buf_len = aes_128_cbc_encrypt(plt, size, m, &aes_buf);
   GNUNET_free (plt);
@@ -293,14 +303,21 @@ GNUNET_CRYPTO_cpabe_decrypt (const void *block,
   read_cpabe(block, &cph_buf, &cph_buf_size, &aes_buf, &aes_buf_size);
   cph = gabe_cph_unserialize(key->pub, cph_buf, cph_buf_size);
   if( !gabe_dec(key->pub, key->prv, cph, m) ) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "%s\n", gabe_error());
+    GNUNET_free (aes_buf);
+    GNUNET_free (cph_buf);
     gabe_cph_free(cph);
+    GNUNET_free (cph);
+    element_clear (m);
     return GNUNET_SYSERR;
   }
   gabe_cph_free(cph);
+  GNUNET_free (cph);
   plt_len = aes_128_cbc_decrypt(aes_buf, aes_buf_size, m, (char**)result);
+  GNUNET_free (cph_buf);
   GNUNET_free (aes_buf);
+  element_clear (m);
   //freeing is buggy in gabe
   //gabe_prv_free (prv);
   //gabe_pub_free (pub);
@@ -347,7 +364,7 @@ GNUNET_CRYPTO_cpabe_deserialize_key (const void *data,
               &prv_len);
   key->pub = gabe_pub_unserialize (pub, pub_len);
   key->prv = gabe_prv_unserialize (key->pub, prv, prv_len);
-  
+
   GNUNET_free (pub);
   GNUNET_free (prv);
   return key;
@@ -393,7 +410,7 @@ GNUNET_CRYPTO_cpabe_deserialize_master_key (const void *data,
               &msk_len);
   key->pub = gabe_pub_unserialize (pub, pub_len);
   key->msk = gabe_msk_unserialize (key->pub, msk, msk_len);
-  
+
   GNUNET_free (pub);
   GNUNET_free (msk);