[epiphany/wip/google-safe-browsing: 11/30] gsb-storage: Add function to lookup full hashes



commit 26ff62e5c73686c639a9cda4fd8b828d464fc6f3
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Sat Sep 16 17:35:33 2017 +0300

    gsb-storage: Add function to lookup full hashes

 lib/safe-browsing/ephy-gsb-storage.c |   70 +++++++++++++++++++++++++++++++++-
 lib/safe-browsing/ephy-gsb-storage.h |    2 +
 lib/safe-browsing/ephy-gsb-utils.c   |   39 ++++++++++++++++++-
 lib/safe-browsing/ephy-gsb-utils.h   |   18 +++++++++
 tests/ephy-gsb-utils-test.c          |    2 +-
 5 files changed, 127 insertions(+), 4 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index bc2c440..1dd7983 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -688,7 +688,7 @@ ephy_gsb_storage_compute_checksum (EphyGSBStorage    *self,
   char *retval = NULL;
   GChecksum *checksum = NULL;
   guint8 *digest = NULL;
-  gsize digest_len = g_checksum_type_get_length (G_CHECKSUM_SHA256);
+  gsize digest_len = GSB_HASH_SIZE;
 
   g_assert (EPHY_IS_GSB_STORAGE (self));
   g_assert (self->is_operable);
@@ -706,7 +706,7 @@ ephy_gsb_storage_compute_checksum (EphyGSBStorage    *self,
   if (!bind_threat_list_params (statement, list, 0, 1, 2, -1))
     goto out;
 
-  checksum = g_checksum_new (G_CHECKSUM_SHA256);
+  checksum = g_checksum_new (GSB_HASH_TYPE);
   while (ephy_sqlite_statement_step (statement, &error)) {
     g_checksum_update (checksum,
                        ephy_sqlite_statement_get_column_as_blob (statement, 0),
@@ -1201,3 +1201,69 @@ out:
 
   return g_list_reverse (retval);
 }
+
+GList *
+ephy_gsb_storage_lookup_full_hashes (EphyGSBStorage *self,
+                                     GList          *hashes)
+{
+  EphySQLiteStatement *statement = NULL;
+  GError *error = NULL;
+  GList *retval = NULL;
+  GString *sql;
+  guint id = 0;
+
+  g_assert (EPHY_IS_GSB_STORAGE (self));
+  g_assert (self->is_operable);
+  g_assert (hashes);
+
+  sql = g_string_new ("SELECT value, threat_type, platform_type, threat_entry_type, "
+                      "expires_at <= (CAST(strftime('%s', 'now') AS INT)) "
+                      "FROM hash_full WHERE value IN (");
+  for (GList *l = hashes; l && l->data; l = l->next)
+    g_string_append (sql, "?,");
+  /* Replace trailing comma character with close parenthesis character. */
+  g_string_overwrite (sql, sql->len - 1, ")");
+
+  statement = ephy_sqlite_connection_create_statement (self->db, sql->str, &error);
+  if (error) {
+    g_warning ("Failed to create select full hash statement: %s", error->message);
+    goto out;
+  }
+
+  for (GList *l = hashes; l && l->data; l = l->next) {
+    ephy_sqlite_statement_bind_blob (statement, id++, l->data, GSB_HASH_SIZE, &error);
+    if (error) {
+      g_warning ("Failed to bind hash value as blob: %s", error->message);
+      goto out;
+    }
+  }
+
+  while (ephy_sqlite_statement_step (statement, &error)) {
+    const guint8 *blob = ephy_sqlite_statement_get_column_as_blob (statement, 0);
+    const char *threat_type = ephy_sqlite_statement_get_column_as_string (statement, 1);
+    const char *platform_type = ephy_sqlite_statement_get_column_as_string (statement, 2);
+    const char *threat_entry_type = ephy_sqlite_statement_get_column_as_string (statement, 3);
+    gboolean expired = ephy_sqlite_statement_get_column_as_boolean (statement, 4);
+    EphyGSBHashFullLookup *lookup = ephy_gsb_hash_full_lookup_new (blob,
+                                                                   threat_type,
+                                                                   platform_type,
+                                                                   threat_entry_type,
+                                                                   expired);
+    retval = g_list_prepend (retval, lookup);
+  }
+
+  if (error) {
+    g_warning ("Failed to execute select full hash statement: %s", error->message);
+    g_list_free_full (retval, (GDestroyNotify)ephy_gsb_hash_full_lookup_free);
+    retval = NULL;
+  }
+
+out:
+  g_string_free (sql, TRUE);
+  if (statement)
+    g_object_unref (statement);
+  if (error)
+    g_error_free (error);
+
+  return g_list_reverse (retval);
+}
diff --git a/lib/safe-browsing/ephy-gsb-storage.h b/lib/safe-browsing/ephy-gsb-storage.h
index 485cede..9aa4bea 100644
--- a/lib/safe-browsing/ephy-gsb-storage.h
+++ b/lib/safe-browsing/ephy-gsb-storage.h
@@ -52,5 +52,7 @@ void            ephy_gsb_storage_insert_hash_prefixes   (EphyGSBStorage    *self
                                                          const char        *prefixes_b64);
 GList          *ephy_gsb_storage_lookup_hash_prefixes   (EphyGSBStorage *self,
                                                          GList          *cues);
+GList          *ephy_gsb_storage_lookup_full_hashes     (EphyGSBStorage *self,
+                                                         GList          *hashes);
 
 G_END_DECLS
diff --git a/lib/safe-browsing/ephy-gsb-utils.c b/lib/safe-browsing/ephy-gsb-utils.c
index 39b3d7f..7c0ff30 100644
--- a/lib/safe-browsing/ephy-gsb-utils.c
+++ b/lib/safe-browsing/ephy-gsb-utils.c
@@ -103,6 +103,43 @@ ephy_gsb_hash_prefix_lookup_free (EphyGSBHashPrefixLookup *lookup)
   g_slice_free (EphyGSBHashPrefixLookup, lookup);
 }
 
+EphyGSBHashFullLookup *
+ephy_gsb_hash_full_lookup_new (const guint8 *hash,
+                               const char   *threat_type,
+                               const char   *platform_type,
+                               const char   *threat_entry_type,
+                               gboolean      expired)
+{
+  EphyGSBHashFullLookup *lookup;
+
+  g_assert (hash);
+  g_assert (threat_type);
+  g_assert (platform_type);
+  g_assert (threat_entry_type);
+
+  lookup = g_slice_new (EphyGSBHashFullLookup);
+  lookup->hash = g_malloc (GSB_HASH_SIZE);
+  memcpy (lookup->hash, hash, GSB_HASH_SIZE);
+  lookup->threat_type = g_strdup (threat_type);
+  lookup->platform_type = g_strdup (platform_type);
+  lookup->threat_entry_type = g_strdup (threat_entry_type);
+  lookup->expired = expired;
+
+  return lookup;
+}
+
+void
+ephy_gsb_hash_full_lookup_free (EphyGSBHashFullLookup *lookup)
+{
+  g_assert (lookup);
+
+  g_free (lookup->hash);
+  g_free (lookup->threat_type);
+  g_free (lookup->platform_type);
+  g_free (lookup->threat_entry_type);
+  g_slice_free (EphyGSBHashFullLookup, lookup);
+}
+
 static JsonObject *
 ephy_gsb_utils_make_client_info (void)
 {
@@ -441,7 +478,7 @@ ephy_gsb_utils_compute_hashes (const char *url)
   char *host = NULL;
   char *path = NULL;
   char *query = NULL;
-  gsize hash_len = g_checksum_type_get_length (G_CHECKSUM_SHA256);
+  gsize hash_len = GSB_HASH_SIZE;
 
   g_assert (url);
 
diff --git a/lib/safe-browsing/ephy-gsb-utils.h b/lib/safe-browsing/ephy-gsb-utils.h
index d6e362f..e612fa3 100644
--- a/lib/safe-browsing/ephy-gsb-utils.h
+++ b/lib/safe-browsing/ephy-gsb-utils.h
@@ -25,6 +25,9 @@
 
 G_BEGIN_DECLS
 
+#define GSB_HASH_TYPE G_CHECKSUM_SHA256
+#define GSB_HASH_SIZE (g_checksum_type_get_length (GSB_HASH_TYPE))
+
 typedef struct {
   char   *threat_type;
   char   *platform_type;
@@ -41,6 +44,14 @@ typedef struct {
   gboolean  negative_expired;
 } EphyGSBHashPrefixLookup;
 
+typedef struct {
+  guint8   *hash; /* The 32 bytes full hash */
+  char     *threat_type;
+  char     *platform_type;
+  char     *threat_entry_type;
+  gboolean  expired;
+} EphyGSBHashFullLookup;
+
 EphyGSBThreatList       *ephy_gsb_threat_list_new                 (const char *threat_type,
                                                                    const char *platform_type,
                                                                    const char *threat_entry_type,
@@ -56,6 +67,13 @@ EphyGSBHashPrefixLookup *ephy_gsb_hash_prefix_lookup_new          (const guint8
                                                                    gboolean      negative_expired);
 void                     ephy_gsb_hash_prefix_lookup_free         (EphyGSBHashPrefixLookup *lookup);
 
+EphyGSBHashFullLookup   *ephy_gsb_hash_full_lookup_new            (const guint8 *hash,
+                                                                   const char   *threat_type,
+                                                                   const char   *platform_type,
+                                                                   const char   *threat_entry_type,
+                                                                   gboolean      expired);
+void                     ephy_gsb_hash_full_lookup_free           (EphyGSBHashFullLookup *lookup);
+
 JsonObject              *ephy_gsb_utils_make_list_updates_request (GList *threat_lists);
 
 char                    *ephy_gsb_utils_canonicalize              (const char  *url,
diff --git a/tests/ephy-gsb-utils-test.c b/tests/ephy-gsb-utils-test.c
index 86ea371..87a0323 100644
--- a/tests/ephy-gsb-utils-test.c
+++ b/tests/ephy-gsb-utils-test.c
@@ -160,7 +160,7 @@ test_ephy_gsb_utils_compute_hashes (void)
     g_assert_cmpuint (g_list_length (hashes), ==, test.num_hashes);
 
     for (guint k = 0; k < test.num_hashes; k++, h = h->next) {
-      char *hash_hex = bytes_to_hex (h->data, 32);
+      char *hash_hex = bytes_to_hex (h->data, GSB_HASH_SIZE);
       g_assert_cmpstr (hash_hex, ==, test.hashes_hex[k]);
       g_free (hash_hex);
     }


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