1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Parse the data returned from the SafeBrowsing v2.1 protocol response.
// TODOv3(shess): Review these changes carefully.
#include <stdlib.h>
#include "base/format_macros.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/sys_byteorder.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/safe_browsing/protocol_parser.h"
#include "chrome/browser/safe_browsing/safe_browsing_util.h"
namespace {
// Helper class for scanning a buffer.
class BufferReader {
public:
BufferReader(const char* data, size_t length)
: data_(data),
length_(length) {
}
// Return info about remaining buffer data.
size_t length() const {
return length_;
}
const char* data() const {
return data_;
}
bool empty() const {
return length_ == 0;
}
// Remove |l| characters from the buffer.
void Advance(size_t l) {
DCHECK_LE(l, length());
data_ += l;
length_ -= l;
}
// Get a reference to data in the buffer.
// TODO(shess): I'm not sure I like this. Fill out a StringPiece instead?
bool RefData(const void** pptr, size_t l) {
if (length() < l) {
Advance(length()); // poison
return false;
}
*pptr = data();
Advance(l);
return true;
}
// Copy data out of the buffer.
bool GetData(void* ptr, size_t l) {
const void* buf_ptr;
if (!RefData(&buf_ptr, l))
return false;
memcpy(ptr, buf_ptr, l);
return true;
}
// Read a 32-bit integer in network byte order into a local uint32.
bool GetNet32(uint32* i) {
if (!GetData(i, sizeof(*i)))
return false;
*i = base::NetToHost32(*i);
return true;
}
// Returns false if there is no data, otherwise fills |*line| with a reference
// to the next line of data in the buffer.
bool GetLine(base::StringPiece* line) {
if (!length_)
return false;
// Find the end of the line, or the end of the input.
size_t eol = 0;
while (eol < length_ && data_[eol] != '\n') {
++eol;
}
line->set(data_, eol);
Advance(eol);
// Skip the newline if present.
if (length_ && data_[0] == '\n')
Advance(1);
return true;
}
// Read out |c| colon-separated pieces from the next line. The resulting
// pieces point into the original data buffer.
bool GetPieces(size_t c, std::vector<base::StringPiece>* pieces) {
base::StringPiece line;
if (!GetLine(&line))
return false;
// Find the parts separated by ':'.
while (pieces->size() + 1 < c) {
size_t colon_ofs = line.find(':');
if (colon_ofs == base::StringPiece::npos) {
Advance(length_);
return false;
}
pieces->push_back(line.substr(0, colon_ofs));
line.remove_prefix(colon_ofs + 1);
}
// The last piece runs to the end of the line.
pieces->push_back(line);
return true;
}
private:
const char* data_;
size_t length_;
DISALLOW_COPY_AND_ASSIGN(BufferReader);
};
bool ParseGetHashMetadata(size_t hash_count,
BufferReader* reader,
std::vector<SBFullHashResult>* full_hashes) {
for (size_t i = 0; i < hash_count; ++i) {
base::StringPiece line;
if (!reader->GetLine(&line))
return false;
size_t meta_data_len;
if (!base::StringToSizeT(line, &meta_data_len))
return false;
const void* meta_data;
if (!reader->RefData(&meta_data, meta_data_len))
return false;
if (full_hashes) {
(*full_hashes)[full_hashes->size() - hash_count + i].metadata.assign(
reinterpret_cast<const char*>(meta_data), meta_data_len);
}
}
return true;
}
} // namespace
namespace safe_browsing {
// BODY = CACHELIFETIME LF HASHENTRY* EOF
// CACHELIFETIME = DIGIT+
// HASHENTRY = LISTNAME ":" HASHSIZE ":" NUMRESPONSES [":m"] LF
// HASHDATA (METADATALEN LF METADATA)*
// HASHSIZE = DIGIT+ # Length of each full hash
// NUMRESPONSES = DIGIT+ # Number of full hashes in HASHDATA
// HASHDATA = <HASHSIZE*NUMRESPONSES number of unsigned bytes>
// METADATALEN = DIGIT+
// METADATA = <METADATALEN number of unsigned bytes>
bool ParseGetHash(const char* chunk_data,
size_t chunk_len,
base::TimeDelta* cache_lifetime,
std::vector<SBFullHashResult>* full_hashes) {
full_hashes->clear();
BufferReader reader(chunk_data, chunk_len);
// Parse out cache lifetime.
{
base::StringPiece line;
if (!reader.GetLine(&line))
return false;
int64_t cache_lifetime_seconds;
if (!base::StringToInt64(line, &cache_lifetime_seconds))
return false;
// TODO(shess): Zero also doesn't make sense, but isn't clearly forbidden,
// either. Maybe there should be a threshold involved.
if (cache_lifetime_seconds < 0)
return false;
*cache_lifetime = base::TimeDelta::FromSeconds(cache_lifetime_seconds);
}
while (!reader.empty()) {
std::vector<base::StringPiece> cmd_parts;
if (!reader.GetPieces(3, &cmd_parts))
return false;
SBFullHashResult full_hash;
full_hash.list_id = safe_browsing_util::GetListId(cmd_parts[0]);
size_t hash_len;
if (!base::StringToSizeT(cmd_parts[1], &hash_len))
return false;
// TODO(shess): Is this possible? If not, why the length present?
if (hash_len != sizeof(SBFullHash))
return false;
// Metadata is indicated by an optional ":m" at the end of the line.
bool has_metadata = false;
base::StringPiece hash_count_string = cmd_parts[2];
size_t optional_colon = hash_count_string.find(':', 0);
if (optional_colon != base::StringPiece::npos) {
if (hash_count_string.substr(optional_colon) != ":m")
return false;
has_metadata = true;
hash_count_string.remove_suffix(2);
}
size_t hash_count;
if (!base::StringToSizeT(hash_count_string, &hash_count))
return false;
if (hash_len * hash_count > reader.length())
return false;
// Ignore hash results from lists we don't recognize.
if (full_hash.list_id < 0) {
reader.Advance(hash_len * hash_count);
if (has_metadata && !ParseGetHashMetadata(hash_count, &reader, NULL))
return false;
continue;
}
for (size_t i = 0; i < hash_count; ++i) {
if (!reader.GetData(&full_hash.hash, hash_len))
return false;
full_hashes->push_back(full_hash);
}
if (has_metadata && !ParseGetHashMetadata(hash_count, &reader, full_hashes))
return false;
}
return reader.empty();
}
// BODY = HEADER LF PREFIXES EOF
// HEADER = PREFIXSIZE ":" LENGTH
// PREFIXSIZE = DIGIT+ # Size of each prefix in bytes
// LENGTH = DIGIT+ # Size of PREFIXES in bytes
std::string FormatGetHash(const std::vector<SBPrefix>& prefixes) {
std::string request;
request.append(base::Uint64ToString(sizeof(SBPrefix)));
request.append(":");
request.append(base::Uint64ToString(sizeof(SBPrefix) * prefixes.size()));
request.append("\n");
// SBPrefix values are read without concern for byte order, so write back the
// same way.
for (size_t i = 0; i < prefixes.size(); ++i) {
request.append(reinterpret_cast<const char*>(&prefixes[i]),
sizeof(SBPrefix));
}
return request;
}
bool ParseUpdate(const char* chunk_data,
size_t chunk_len,
size_t* next_update_sec,
bool* reset,
std::vector<SBChunkDelete>* deletes,
std::vector<ChunkUrl>* chunk_urls) {
DCHECK(next_update_sec);
DCHECK(deletes);
DCHECK(chunk_urls);
BufferReader reader(chunk_data, chunk_len);
// Populated below.
std::string list_name;
while (!reader.empty()) {
std::vector<base::StringPiece> pieces;
if (!reader.GetPieces(2, &pieces))
return false;
base::StringPiece& command = pieces[0];
// Differentiate on the first character of the command (which is usually
// only one character, with the exception of the 'ad' and 'sd' commands).
switch (command[0]) {
case 'a':
case 's': {
// Must be either an 'ad' (add-del) or 'sd' (sub-del) chunk. We must
// have also parsed the list name before getting here, or the add-del
// or sub-del will have no context.
if (list_name.empty() || (command != "ad" && command != "sd"))
return false;
SBChunkDelete chunk_delete;
chunk_delete.is_sub_del = command[0] == 's';
StringToRanges(pieces[1].as_string(), &chunk_delete.chunk_del);
chunk_delete.list_name = list_name;
deletes->push_back(chunk_delete);
break;
}
case 'i':
// The line providing the name of the list (i.e. 'goog-phish-shavar').
list_name = pieces[1].as_string();
break;
case 'n':
// The line providing the next earliest time (in seconds) to re-query.
if (!base::StringToSizeT(pieces[1], next_update_sec))
return false;
break;
case 'u': {
ChunkUrl chunk_url;
chunk_url.url = pieces[1].as_string(); // Skip the initial "u:".
chunk_url.list_name = list_name;
chunk_urls->push_back(chunk_url);
break;
}
case 'r':
if (pieces[1] != "pleasereset")
return false;
*reset = true;
break;
default:
// According to the spec, we ignore commands we don't understand.
// TODO(shess): Does this apply to r:unknown or n:not-integer?
break;
}
}
return true;
}
// BODY = (UINT32 CHUNKDATA)+
// UINT32 = Unsigned 32-bit integer in network byte order
// CHUNKDATA = Encoded ChunkData protocol message
bool ParseChunk(const char* data,
size_t length,
ScopedVector<SBChunkData>* chunks) {
BufferReader reader(data, length);
while (!reader.empty()) {
uint32 l = 0;
if (!reader.GetNet32(&l) || l == 0 || l > reader.length())
return false;
const void* p = NULL;
if (!reader.RefData(&p, l))
return false;
scoped_ptr<SBChunkData> chunk(new SBChunkData());
if (!chunk->ParseFrom(reinterpret_cast<const unsigned char*>(p), l))
return false;
chunks->push_back(chunk.release());
}
DCHECK(reader.empty());
return true;
}
// LIST = LISTNAME ";" LISTINFO (":" LISTINFO)*
// LISTINFO = CHUNKTYPE ":" CHUNKLIST
// CHUNKTYPE = "a" | "s"
// CHUNKLIST = (RANGE | NUMBER) ["," CHUNKLIST]
// NUMBER = DIGIT+
// RANGE = NUMBER "-" NUMBER
std::string FormatList(const SBListChunkRanges& list) {
std::string formatted_results = list.name;
formatted_results.append(";");
if (!list.adds.empty())
formatted_results.append("a:").append(list.adds);
if (!list.adds.empty() && !list.subs.empty())
formatted_results.append(":");
if (!list.subs.empty())
formatted_results.append("s:").append(list.subs);
formatted_results.append("\n");
return formatted_results;
}
} // namespace safe_browsing
|