Skip to content

Commit

Permalink
Clean up SSL false start blacklist code. Numerous changes, including:
Browse files Browse the repository at this point in the history
  * Handle trailing dots in LastTwoLabels() as in http://codereview.chromium.org/7518035/ .  Rename this function to LastTwoComponents() to match the terminology used in the RegistryControlledDomainService and elsewhere in Chrome.
  * Since callers are using std::string anyway, make the functions in the header take const std::string& instead of char*.  This also allows doing string operations on them.
  * Use string operations (like find_last_of()) in place of hand-written algorithms, for brevity, clarity, and safety.
  * Avoid "unsigned", which the style guide forbids, and use allowed types like size_t, uint32, or int (depending on the situation).
  * Avoid #define and "using".
  * Use standard algorithms for similar reasons as using string ops.
  * Use file_util functions to significantly abbreviate file reading/writing code.
  * Use wmain() (on Windows) in combination with FilePath to avoid issues if the provided pathname has extended characters that don't flatten losslessly to the default codepage (thanks Darin for pointing out this issue).
  * Avoid casting where possible.  Avoid some casts for printf()-style calls by using a string stream, which also allows for slightly less boilerplate.
  * Convert non-error uses of stderr to the chrome-standard VLOG(1).
  * Correctly handle hostnames with trailing dots in the input file.
  * In general, shorten code where possible.

Because this adds a dependency on base, and ssl_false_start_blacklist_process has the "#host" specifier in net.gyp, bradnelson tells me that base and its dependencies need an explicit "host, target" toolchain list for the Linux builds to work correctly.  It would be nice if we could avoid this but I guess gyp would have to be smarter or something.

BUG=none
TEST=none
Review URL: http://codereview.chromium.org/7550002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95907 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
pkasting@chromium.org committed Aug 8, 2011
1 parent f684678 commit 96bd27b
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 315 deletions.
1 change: 1 addition & 0 deletions base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
# base depends on base_static.
'target_name': 'base_static',
'type': 'static_library',
'toolsets': ['host', 'target'],
'sources': [
'base_switches.cc',
'base_switches.h',
Expand Down
3 changes: 3 additions & 0 deletions base/base.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@
{
'target_name': 'base',
'type': '<(component)',
'toolsets': ['host', 'target'],
'variables': {
'base_target': 1,
},
Expand Down Expand Up @@ -664,6 +665,7 @@
{
'target_name': 'symbolize',
'type': 'static_library',
'toolsets': ['host', 'target'],
'variables': {
'chromium_code': 0,
},
Expand All @@ -689,6 +691,7 @@
{
'target_name': 'xdg_mime',
'type': 'static_library',
'toolsets': ['host', 'target'],
'variables': {
'chromium_code': 0,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
'target_name': 'dynamic_annotations',
'type': 'static_library',
'toolsets': ['host', 'target'],
'include_dirs': [
'../../../',
],
Expand Down
72 changes: 56 additions & 16 deletions build/linux/system.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
{
'target_name': 'gtk',
'type': 'settings',
'toolsets': ['host', 'target'],
'conditions': [
['_toolset=="target"', {
'direct_dependent_settings': {
Expand All @@ -46,12 +47,27 @@
'<!@(<(pkg-config) --libs-only-l gtk+-2.0 gthread-2.0)',
],
},
}],
[ 'chromeos==1', {
'link_settings': {
'libraries': [ '-lXtst' ]
}
}]]
}, {
'direct_dependent_settings': {
'cflags': [
'<!@(pkg-config --cflags gtk+-2.0 gthread-2.0)',
],
},
'link_settings': {
'ldflags': [
'<!@(pkg-config --libs-only-L --libs-only-other gtk+-2.0 gthread-2.0)',
],
'libraries': [
'<!@(pkg-config --libs-only-l gtk+-2.0 gthread-2.0)',
],
},
}],
['chromeos==1', {
'link_settings': {
'libraries': [ '-lXtst' ]
}
}],
],
},
{
'target_name': 'gtkprint',
Expand All @@ -71,7 +87,8 @@
'<!@(<(pkg-config) --libs-only-l gtk+-unix-print-2.0)',
],
},
}]]
}],
],
},
{
'target_name': 'ssl',
Expand Down Expand Up @@ -152,7 +169,8 @@
'<!@(<(pkg-config) --libs-only-l freetype2)',
],
},
}]]
}],
],
},
{
'target_name': 'fontconfig',
Expand All @@ -172,7 +190,8 @@
'<!@(<(pkg-config) --libs-only-l fontconfig)',
],
},
}]]
}],
],
},
{
'target_name': 'gdk',
Expand All @@ -192,7 +211,8 @@
'<!@(<(pkg-config) --libs-only-l gdk-2.0)',
],
},
}]]
}],
],
},
{
'target_name': 'gconf',
Expand All @@ -215,7 +235,8 @@
'<!@(<(pkg-config) --libs-only-l gconf-2.0)',
],
},
}]]
}],
],
},
{
'target_name': 'gio',
Expand Down Expand Up @@ -250,11 +271,13 @@
}],
],
},
}]]
}],
],
},
{
'target_name': 'x11',
'type': 'settings',
'toolsets': ['host', 'target'],
'conditions': [
['_toolset=="target"', {
'direct_dependent_settings': {
Expand All @@ -270,7 +293,21 @@
'<!@(<(pkg-config) --libs-only-l x11 xi)',
],
},
}],
}, {
'direct_dependent_settings': {
'cflags': [
'<!@(pkg-config --cflags x11)',
],
},
'link_settings': {
'ldflags': [
'<!@(pkg-config --libs-only-L --libs-only-other x11 xi)',
],
'libraries': [
'<!@(pkg-config --libs-only-l x11 xi)',
],
},
}],
],
},
{
Expand All @@ -291,7 +328,8 @@
'<!@(<(pkg-config) --libs-only-l xext)',
],
},
}]]
}],
],
},
{
'target_name': 'libgcrypt',
Expand All @@ -308,7 +346,8 @@
'<!@(libgcrypt-config --libs)',
],
},
}]]
}],
],
},
{
'target_name': 'selinux',
Expand All @@ -320,7 +359,8 @@
'-lselinux',
],
},
}]]
}],
],
},
{
'target_name': 'gnome_keyring',
Expand Down
1 change: 1 addition & 0 deletions build/util/build_util.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{
'target_name': 'lastchange',
'type': 'none',
'toolsets': ['host', 'target'],
'variables': {
'lastchange_out_path': '<(SHARED_INTERMEDIATE_DIR)/build/LASTCHANGE',
'default_lastchange_path': '../LASTCHANGE.in',
Expand Down
2 changes: 1 addition & 1 deletion net/base/ssl_config_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ SSLConfigService::SSLConfigService()
// static
bool SSLConfigService::IsKnownFalseStartIncompatibleServer(
const std::string& hostname) {
return SSLFalseStartBlacklist::IsMember(hostname.c_str());
return SSLFalseStartBlacklist::IsMember(hostname);
}

static bool g_cached_info_enabled = false;
Expand Down
25 changes: 11 additions & 14 deletions net/base/ssl_false_start_blacklist.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Copyright (c) 2011 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.

Expand All @@ -7,22 +7,19 @@
namespace net {

// static
bool SSLFalseStartBlacklist::IsMember(const char* host) {
const char* last_two_labels = LastTwoLabels(host);
if (!last_two_labels)
bool SSLFalseStartBlacklist::IsMember(const std::string& host) {
const std::string last_two_components(LastTwoComponents(host));
if (last_two_components.empty())
return false;
const unsigned bucket = Hash(last_two_labels) & (kBuckets - 1);
const uint32 start = kHashTable[bucket];
const uint32 end = kHashTable[bucket + 1];
const size_t len = strlen(host);

for (size_t i = start; i < end;) {
const size_t bucket = Hash(last_two_components) & (kBuckets - 1);
for (size_t i = kHashTable[bucket]; i < kHashTable[bucket + 1]; ) {
const size_t blacklist_entry_len = static_cast<uint8>(kHashData[i]);
if (len >= blacklist_entry_len &&
memcmp(&host[len - blacklist_entry_len], &kHashData[i + 1],
blacklist_entry_len) == 0 &&
(len == blacklist_entry_len ||
host[len - blacklist_entry_len - 1] == '.')) {
if (host.length() >= blacklist_entry_len &&
!host.compare(host.length() - blacklist_entry_len, blacklist_entry_len,
&kHashData[i + 1], blacklist_entry_len) &&
(host.length() == blacklist_entry_len ||
host[host.length() - blacklist_entry_len - 1] == '.')) {
return true;
}
i += blacklist_entry_len + 1;
Expand Down
84 changes: 32 additions & 52 deletions net/base/ssl_false_start_blacklist.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#ifndef NET_BASE_SSL_FALSE_START_BLACKLIST_H_
#define NET_BASE_SSL_FALSE_START_BLACKLIST_H_

#include "base/basictypes.h"
#include <string>

#include "base/logging.h"
#include "net/base/net_api.h"

namespace net {
Expand All @@ -16,64 +18,42 @@ namespace net {
// table for fast lookups.
class SSLFalseStartBlacklist {
public:
// IsMember returns true if the given host is in the blacklist.
// host: a DNS name in dotted form (i.e. "www.example.com")
NET_TEST static bool IsMember(const char* host);

// Hash returns the modified djb2 hash of the given string.
static unsigned Hash(const char* str) {
// This is inline because the code which generates the hash table needs to
// use it. However, the generating code cannot link against
// ssl_false_start_blacklist.cc because that needs the tables which it
// generates.
const unsigned char* in = reinterpret_cast<const unsigned char*>(str);
unsigned hash = 5381;
unsigned char c;

while ((c = *in++))
hash = ((hash << 5) + hash) ^ c;
// Returns true if |host| (a DNS name in dotted form, e.g. "www.example.com")
// is in the blacklist.
NET_TEST static bool IsMember(const std::string& host);

// Returns the modified djb2 hash of |host|.
// NOTE: This is inline because the code which generates the hash table needs
// to use it. However, the generating code cannot link against
// ssl_false_start_blacklist.cc because that needs the tables which it
// generates.
static uint32 Hash(const std::string& host) {
uint32 hash = 5381;
for (const uint8* in = reinterpret_cast<const uint8*>(host.c_str());
*in != 0; ++in)
hash = ((hash << 5) + hash) ^ *in;
return hash;
}

// LastTwoLabels returns a pointer within |host| to the last two labels of
// |host|. For example, if |host| is "a.b.c.d" then LastTwoLabels will return
// "c.d".
// host: a DNS name in dotted form.
// returns: NULL on error, otherwise a pointer inside |host|.
static const char* LastTwoLabels(const char* host) {
// See comment in |Hash| for why this function is inline.
const size_t len = strlen(host);
if (len == 0)
return NULL;

unsigned dots_found = 0;
size_t i;
for (i = len - 1; i < len; i--) {
if (host[i] == '.') {
dots_found++;
if (dots_found == 2) {
i++;
break;
}
}
}

if (i > len)
i = 0;

if (dots_found == 0)
return NULL; // no names with less than two labels are in the blacklist.
if (dots_found == 1) {
if (host[0] == '.')
return NULL; // ditto
}

return &host[i];
// Returns the last two dot-separated components of |host|, ignoring any
// trailing dots. For example, returns "c.d" for "a.b.c.d.". Returns an
// empty string if |host| does not have two dot-separated components.
// NOTE: Inline for the same reason as Hash().
static std::string LastTwoComponents(const std::string& host) {
size_t last_nondot = host.find_last_not_of('.');
if (last_nondot == std::string::npos)
return std::string();
size_t last_dot = host.find_last_of('.', last_nondot);
if ((last_dot == 0) || (last_dot == std::string::npos))
return std::string();
// NOTE: This next line works correctly even when the call returns npos.
size_t components_begin = host.find_last_of('.', last_dot - 1) + 1;
return host.substr(components_begin, last_nondot - components_begin + 1);
}

// This is the number of buckets in the blacklist hash table. (Must be a
// power of two).
static const unsigned kBuckets = 128;
static const size_t kBuckets = 128;

private:
// The following two members are defined in
Expand Down
Loading

0 comments on commit 96bd27b

Please sign in to comment.