// Copyright 2008, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_PARSER_H__ #define CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_PARSER_H__ // Parse the data returned from the chunk response. // // Based on the SafeBrowsing v2.1 protocol: // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec // // Read the response from a SafeBrowsing request, and parse into useful pieces. // The protocol is generally line oriented, but can contain binary data in the // actual chunk responses. The consumer of the protocol data should instantiate // the parser and call the appropriate parsing function on the data. // // Examples of protocol responses: // // 1. List identification // i:goog-phish-shavar\n // :\n // // 2. Minimum time to wait (seconds) until the next download request can be made // n:1200\n // :\n // // 3. Redirect URL for retrieving a chunk // u:cache.googlevideo.com/safebrowsing/rd/goog-phish-shavar_a_1\n // :\n // // 4. Add and sub chunks // a:1:4:523\n... <-- Add chunk + binary data // s:13:4:17\n... <-- Sub chunk + binary data // :::\n // // 5. Add-del and sub-del requests // ad:1-4000,5001\n <-- Add-del // sd:1,3,5,7,903\n <-- Sub-del // :\n #include #include #include "base/basictypes.h" #include "chrome/browser/safe_browsing/chunk_range.h" #include "chrome/browser/safe_browsing/safe_browsing_util.h" class SafeBrowsingProtocolParser { public: SafeBrowsingProtocolParser(); // Parse the response of an update request. Results for chunk deletions (both // add-del and sub-del are returned in 'chunk_deletes', and new chunk URLs to // download are contained in 'chunk_urls'. The next time the client is allowed // to request another update is returned in 'next_update_sec'. If the service // wants us to retrieve new MAC keys, 're_key' will be set to true. If we are // using MACs to verify responses, the 'key' must be set to the private key // returned from the SafeBrowsing servers. 'reset' will be set to true if the // SafeBrowsing service wants us to dump our database. // Returns 'true'if it was able to decode the chunk properly, 'false' if not // decoded properly and the results should be ignored. bool ParseUpdate(const char* chunk_data, int chunk_len, const std::string& key, int* next_update_sec, bool* re_key, bool* reset, std::vector* chunk_deletes, std::vector* chunk_urls); // Parse the response from a chunk URL request and returns the hosts/prefixes // for adds and subs in "chunks". Returns 'true' on successful parsing, // 'false' otherwise. Any result should be ignored when a parse has failed. bool ParseChunk(const char* chunk_data, int chunk_len, const std::string& key, const std::string& mac, bool* re_key, std::deque* chunks); // Parse the result of a GetHash request, returning the list of full hashes. // If we are checking for valid MACs, the caller should populate 'key'. bool ParseGetHash(const char* chunk_data, int chunk_len, const std::string& key, bool* re_key, std::vector* full_hashes); // Convert a list of partial hashes into a proper GetHash request. void FormatGetHash(const std::vector& prefixes, std::string* request); // Parse the keys used for subsequent communications with the SafeBrowsing // servers. Returns true on successful parse, false on parse error. bool ParseNewKey(const char* chunk_data, int chunk_length, std::string* client_key, std::string* wrapped_key); private: bool ParseAddChunk(const char* data, int data_len, int hash_len, std::deque* hosts); bool ParseSubChunk(const char* data, int data_len, int hash_len, std::deque* hosts); // Helper functions used by ParseAddChunk and ParseSubChunk. static void ReadHostAndPrefixCount( const char** data, int* remaining, SBPrefix* host, int* count); static int ReadChunkId(const char** data, int* remaining); static bool ReadPrefixes( const char** data, int* remaining, SBEntry* entry, int count, int index_start); // The name of the current list std::string list_name_; DISALLOW_EVIL_CONSTRUCTORS(SafeBrowsingProtocolParser); }; #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_PARSER_H__