diff options
Diffstat (limited to 'chrome/browser/safe_browsing/protocol_parser.h')
-rw-r--r-- | chrome/browser/safe_browsing/protocol_parser.h | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/chrome/browser/safe_browsing/protocol_parser.h b/chrome/browser/safe_browsing/protocol_parser.h new file mode 100644 index 0000000..802b5e5 --- /dev/null +++ b/chrome/browser/safe_browsing/protocol_parser.h @@ -0,0 +1,153 @@ +// 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 +// <command>:<command_data>\n +// +// 2. Minimum time to wait (seconds) until the next download request can be made +// n:1200\n +// <command>:<time_in_seconds>\n +// +// 3. Redirect URL for retrieving a chunk +// u:cache.googlevideo.com/safebrowsing/rd/goog-phish-shavar_a_1\n +// <command>:<url>\n +// +// 4. Add and sub chunks +// a:1:4:523\n... <-- Add chunk + binary data +// s:13:4:17\n... <-- Sub chunk + binary data +// <chunk_type>:<chunk_number>:<prefix_len>:<chunk_bytes>\n<binary_data> +// +// 5. Add-del and sub-del requests +// ad:1-4000,5001\n <-- Add-del +// sd:1,3,5,7,903\n <-- Sub-del +// <command>:<chunk_range>\n + + +#include <string> +#include <vector> + +#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<SBChunkDelete>* chunk_deletes, + std::vector<ChunkUrl>* 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<SBChunk>* 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<SBFullHashResult>* full_hashes); + + // Convert a list of partial hashes into a proper GetHash request. + void FormatGetHash(const std::vector<SBPrefix>& 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<SBChunkHost>* hosts); + bool ParseSubChunk(const char* data, + int data_len, + int hash_len, + std::deque<SBChunkHost>* 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__
\ No newline at end of file |