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



commit ac84087f0cb979c76a7562983730a1e29b9a55e7
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   |   41 +++++++++++++++++++-
 lib/safe-browsing/ephy-gsb-utils.h   |   19 +++++++++
 4 files changed, 128 insertions(+), 4 deletions(-)
---
diff --git a/lib/safe-browsing/ephy-gsb-storage.c b/lib/safe-browsing/ephy-gsb-storage.c
index 460eb33..c587494 100644
--- a/lib/safe-browsing/ephy-gsb-storage.c
+++ b/lib/safe-browsing/ephy-gsb-storage.c
@@ -689,7 +689,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);
@@ -707,7 +707,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 0dc0567..14a1167 100644
--- a/lib/safe-browsing/ephy-gsb-utils.c
+++ b/lib/safe-browsing/ephy-gsb-utils.c
@@ -102,6 +102,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)
 {
@@ -439,11 +476,11 @@ 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);
 
-  checksum = g_checksum_new (G_CHECKSUM_SHA256);
+  checksum = g_checksum_new (GSB_HASH_TYPE);
   url_canonical = ephy_gsb_utils_canonicalize (url, &host, &path, &query);
   host_suffixes = ephy_gsb_utils_compute_host_suffixes (host);
   path_prefixes = ephy_gsb_utils_compute_path_prefixes (path, query);
diff --git a/lib/safe-browsing/ephy-gsb-utils.h b/lib/safe-browsing/ephy-gsb-utils.h
index fe97ec5..9b69cc1 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;
@@ -42,6 +45,15 @@ typedef struct {
   gboolean  negative_expired;
 } EphyGSBHashPrefixLookup;
 
+typedef struct {
+  /* The 32 bytes full hash. */
+  guint8   *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,
@@ -57,6 +69,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,


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