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



commit 4f852744cab3734555cd985808a8092617b439bd
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 5552f08..5a68706 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -681,7 +681,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);
@@ -699,7 +699,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),
@@ -1194,3 +1194,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 0a5d847..7daa5f7 100644
--- a/lib/safe-browsing/ephy-gsb-storage.h
+++ b/lib/safe-browsing/ephy-gsb-storage.h
@@ -53,5 +53,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 b560c15..d89df3e 100644
--- a/lib/safe-browsing/ephy-gsb-utils.c
+++ b/lib/safe-browsing/ephy-gsb-utils.c
@@ -104,6 +104,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)
 {
@@ -454,7 +491,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 6cde2e8..c58bf6a 100644
--- a/lib/safe-browsing/ephy-gsb-utils.h
+++ b/lib/safe-browsing/ephy-gsb-utils.h
@@ -24,6 +24,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;
@@ -40,6 +43,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,
@@ -55,6 +66,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);
+
 char                    *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 ed5dfd7..8c5330d 100644
--- a/tests/ephy-gsb-utils-test.c
+++ b/tests/ephy-gsb-utils-test.c
@@ -161,7 +161,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]