Bug 1394031 - Remove mCryptoHash members of nsUrlClassifierDBServiceWorker and ProtocolParser

The usage of cryptoHash consists of a complete set of Init, Update, and Finish, there's
no reason to keep it around

MozReview-Commit-ID: 7bT9IsWEM5m
This commit is contained in:
Thomas Nguyen 2017-09-05 17:14:54 +08:00
Родитель 598ba55ac2
Коммит e70d76485c
12 изменённых файлов: 30 добавлений и 61 удалений

Просмотреть файл

@ -257,9 +257,6 @@ Classifier::Open(nsIFile& aCacheDirectory)
rv = CreateStoreDirectory();
NS_ENSURE_SUCCESS(rv, rv);
mCryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Build the list of know urlclassifier lists
// XXX: Disk IO potentially on the main thread during startup
RegenActiveTables();
@ -470,7 +467,7 @@ Classifier::Check(const nsACString& aSpec,
// Now check each lookup fragment against the entries in the DB.
for (uint32_t i = 0; i < fragments.Length(); i++) {
Completion lookupHash;
lookupHash.FromPlaintext(fragments[i], mCryptoHash);
lookupHash.FromPlaintext(fragments[i]);
if (LOG_ENABLED()) {
nsAutoCString checking;

Просмотреть файл

@ -13,7 +13,6 @@
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsIFile.h"
#include "nsICryptoHash.h"
#include "nsDataHashtable.h"
class nsIThread;
@ -207,7 +206,6 @@ private:
nsCOMPtr<nsIFile> mBackupDirectory;
nsCOMPtr<nsIFile> mUpdatingDirectory; // For update only.
nsCOMPtr<nsIFile> mToDeleteDirectory;
nsCOMPtr<nsICryptoHash> mCryptoHash;
nsTArray<LookupCache*> mLookupCaches; // For query only.
nsTArray<nsCString> mActiveTablesCache;
uint32_t mHashKey;

Просмотреть файл

@ -35,21 +35,25 @@ struct SafebrowsingHash
typedef SafebrowsingHash<S, Comparator> self_type;
uint8_t buf[S];
nsresult FromPlaintext(const nsACString& aPlainText, nsICryptoHash* aHash) {
nsresult FromPlaintext(const nsACString& aPlainText) {
// From the protocol doc:
// Each entry in the chunk is composed
// of the SHA 256 hash of a suffix/prefix expression.
nsresult rv = aHash->Init(nsICryptoHash::SHA256);
nsresult rv;
nsCOMPtr<nsICryptoHash> hash =
do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = aHash->Update
rv = hash->Init(nsICryptoHash::SHA256);
NS_ENSURE_SUCCESS(rv, rv);
rv = hash->Update
(reinterpret_cast<const uint8_t*>(aPlainText.BeginReading()),
aPlainText.Length());
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString hashed;
rv = aHash->Finish(false, hashed);
rv = hash->Finish(false, hashed);
NS_ENSURE_SUCCESS(rv, rv);
NS_ASSERTION(hashed.Length() >= sHashSize,

Просмотреть файл

@ -80,13 +80,6 @@ ProtocolParser::~ProtocolParser()
CleanupUpdates();
}
nsresult
ProtocolParser::Init(nsICryptoHash* aHasher)
{
mCryptoHash = aHasher;
return NS_OK;
}
void
ProtocolParser::CleanupUpdates()
{
@ -404,7 +397,7 @@ ProtocolParserV2::ProcessPlaintextChunk(const nsACString& aChunk)
if (mChunkState.type == CHUNK_ADD) {
if (mChunkState.hashSize == COMPLETE_SIZE) {
Completion hash;
hash.FromPlaintext(line, mCryptoHash);
hash.FromPlaintext(line);
nsresult rv = mTableUpdate->NewAddComplete(mChunkState.num, hash);
if (NS_FAILED(rv)) {
return rv;
@ -412,7 +405,7 @@ ProtocolParserV2::ProcessPlaintextChunk(const nsACString& aChunk)
} else {
NS_ASSERTION(mChunkState.hashSize == 4, "Only 32- or 4-byte hashes can be used for add chunks.");
Prefix hash;
hash.FromPlaintext(line, mCryptoHash);
hash.FromPlaintext(line);
nsresult rv = mTableUpdate->NewAddPrefix(mChunkState.num, hash);
if (NS_FAILED(rv)) {
return rv;
@ -433,7 +426,7 @@ ProtocolParserV2::ProcessPlaintextChunk(const nsACString& aChunk)
if (mChunkState.hashSize == COMPLETE_SIZE) {
Completion hash;
hash.FromPlaintext(Substring(iter, end), mCryptoHash);
hash.FromPlaintext(Substring(iter, end));
nsresult rv = mTableUpdate->NewSubComplete(addChunk, hash, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;
@ -441,7 +434,7 @@ ProtocolParserV2::ProcessPlaintextChunk(const nsACString& aChunk)
} else {
NS_ASSERTION(mChunkState.hashSize == 4, "Only 32- or 4-byte hashes can be used for add chunks.");
Prefix hash;
hash.FromPlaintext(Substring(iter, end), mCryptoHash);
hash.FromPlaintext(Substring(iter, end));
nsresult rv = mTableUpdate->NewSubPrefix(addChunk, hash, mChunkState.num);
if (NS_FAILED(rv)) {
return rv;

Просмотреть файл

@ -7,7 +7,6 @@
#define ProtocolParser_h__
#include "HashStore.h"
#include "nsICryptoHMAC.h"
#include "safebrowsing.pb.h"
namespace mozilla {
@ -28,8 +27,6 @@ public:
nsresult Status() const { return mUpdateStatus; }
nsresult Init(nsICryptoHash* aHasher);
#ifdef MOZ_SAFEBROWSING_DUMP_FAILED_UPDATES
virtual nsCString GetRawTableUpdates() const { return mPending; }
#endif
@ -73,7 +70,6 @@ protected:
nsTArray<TableUpdate*> mTableUpdates;
nsTArray<ForwardedUpdate> mForwards;
nsCOMPtr<nsICryptoHash> mCryptoHash;
// The table names that were requested from the client.
nsTArray<nsCString> mRequestedTables;

Просмотреть файл

@ -8,8 +8,6 @@
#include "nsAppDirectoryServiceDefs.h"
#include "nsArrayUtils.h"
#include "nsCRT.h"
#include "nsICryptoHash.h"
#include "nsICryptoHMAC.h"
#include "nsIDirectoryService.h"
#include "nsIKeyModule.h"
#include "nsIObserverService.h"
@ -467,8 +465,6 @@ nsUrlClassifierDBServiceWorker::BeginStream(const nsACString &table)
return NS_ERROR_OUT_OF_MEMORY;
}
mProtocolParser->Init(mCryptoHash);
if (!table.IsEmpty()) {
mProtocolParser->SetCurrentTable(table);
}
@ -809,7 +805,6 @@ nsUrlClassifierDBServiceWorker::CloseDb()
mClassifier = nullptr;
}
mCryptoHash = nullptr;
LOG(("urlclassifier db closed\n"));
return NS_OK;
@ -944,9 +939,6 @@ nsUrlClassifierDBServiceWorker::OpenDb()
}
nsresult rv;
mCryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoPtr<Classifier> classifier(new (fallible) Classifier());
if (!classifier) {
return NS_ERROR_OUT_OF_MEMORY;

Просмотреть файл

@ -270,9 +270,6 @@ private:
bool IsSameAsLastResults(CacheResultArray& aResult);
// Can only be used on the background thread
nsCOMPtr<nsICryptoHash> mCryptoHash;
nsAutoPtr<mozilla::safebrowsing::Classifier> mClassifier;
// The class that actually parses the update chunks.
nsAutoPtr<ProtocolParser> mProtocolParser;

Просмотреть файл

@ -152,8 +152,7 @@ nsCString
GeneratePrefix(const nsCString& aFragment, uint8_t aLength)
{
Completion complete;
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
complete.FromPlaintext(aFragment, cryptoHash);
complete.FromPlaintext(aFragment);
nsCString hash;
hash.Assign((const char *)complete.buf, aLength);

Просмотреть файл

@ -18,14 +18,11 @@ SetupCacheEntry(LookupCacheV2* aLookupCache,
MissPrefixArray misses;
MissPrefixArray emptyMisses;
nsCOMPtr<nsICryptoHash> cryptoHash =
do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
AddComplete* add = completes.AppendElement(fallible);
add->complete.FromPlaintext(aCompletion, cryptoHash);
add->complete.FromPlaintext(aCompletion);
Prefix* prefix = misses.AppendElement(fallible);
prefix->FromPlaintext(aCompletion, cryptoHash);
prefix->FromPlaintext(aCompletion);
// Setup positive cache first otherwise negative cache expiry will be
// overwritten.
@ -45,9 +42,7 @@ SetupCacheEntry(LookupCacheV4* aLookupCache,
FullHashResponseMap map;
Prefix prefix;
nsCOMPtr<nsICryptoHash> cryptoHash =
do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
prefix.FromPlaintext(aCompletion, cryptoHash);
prefix.FromPlaintext(aCompletion);
CachedFullHashResponse* response = map.LookupOrAdd(prefix.ToUint32());
@ -104,8 +99,7 @@ TestCache(const _Fragment& aFragment,
T* aCache = nullptr)
{
Completion lookupHash;
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
lookupHash.FromPlaintext(aFragment, cryptoHash);
lookupHash.FromPlaintext(aFragment);
TestCache<T>(lookupHash, aExpectedHas, aExpectedConfirmed, aExpectedInCache, aCache);
}
@ -148,13 +142,12 @@ TEST(UrlClassifierCaching, InNegativeCacheNotExpired)
{
// Create a fullhash whose prefix matches the prefix in negative cache
// but completion doesn't match any fullhash in positive cache.
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
Completion prefix;
prefix.FromPlaintext(_Fragment("cache.notexpired.com/"), cryptoHash);
prefix.FromPlaintext(_Fragment("cache.notexpired.com/"));
Completion fullhash;
fullhash.FromPlaintext(_Fragment("firefox.com/"), cryptoHash);
fullhash.FromPlaintext(_Fragment("firefox.com/"));
// Overwrite the 4-byte prefix of `fullhash` so that it conflicts with `prefix`.
// Since "cache.notexpired.com" is added to database in TestCache as a
@ -171,13 +164,12 @@ TEST(UrlClassifierCaching, InNegativeCacheNotExpired)
TEST(UrlClassifierCaching, InNegativeCacheExpired)
{
// Create a fullhash whose prefix is in the cache.
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
Completion prefix;
prefix.FromPlaintext(_Fragment("cache.expired.com/"), cryptoHash);
prefix.FromPlaintext(_Fragment("cache.expired.com/"));
Completion fullhash;
fullhash.FromPlaintext(_Fragment("firefox.com/"), cryptoHash);
fullhash.FromPlaintext(_Fragment("firefox.com/"));
memcpy(fullhash.buf, prefix.buf, 10);
@ -255,7 +247,7 @@ TEST(UrlClassifierCaching, NegativeCacheExpireV2)
MissPrefixArray misses;
Prefix* prefix = misses.AppendElement(fallible);
prefix->FromPlaintext(NEG_CACHE_EXPIRED_URL, cryptoHash);
prefix->FromPlaintext(NEG_CACHE_EXPIRED_URL);
AddCompleteArray dummy;
cache->AddGethashResultToCache(dummy, misses, EXPIRED_TIME_SEC);
@ -275,7 +267,7 @@ TEST(UrlClassifierCaching, NegativeCacheExpireV4)
FullHashResponseMap map;
Prefix prefix;
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
prefix.FromPlaintext(NEG_CACHE_EXPIRED_URL, cryptoHash);
prefix.FromPlaintext(NEG_CACHE_EXPIRED_URL);
CachedFullHashResponse* response = map.LookupOrAdd(prefix.ToUint32());
response->negativeCacheExpirySec = EXPIRED_TIME_SEC;

Просмотреть файл

@ -71,8 +71,7 @@ TestReadNoiseEntries(Classifier* classifier,
const nsCString& aFragment)
{
Completion lookupHash;
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
lookupHash.FromPlaintext(aFragment, cryptoHash);
lookupHash.FromPlaintext(aFragment);
LookupResult result;
result.hash.complete = lookupHash;

Просмотреть файл

@ -17,8 +17,7 @@ TestHasPrefix(const _Fragment& aFragment, bool aExpectedHas, bool aExpectedCompl
UniquePtr<LookupCache> cache = SetupLookupCache<LookupCacheV4>(array);
Completion lookupHash;
nsCOMPtr<nsICryptoHash> cryptoHash = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID);
lookupHash.FromPlaintext(aFragment, cryptoHash);
lookupHash.FromPlaintext(aFragment);
bool has, confirmed;
uint32_t matchLength;

Просмотреть файл

@ -17,6 +17,9 @@ do_get_profile();
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
// Ensure PSM is initialized before the test
Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);
var iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]