This source file includes following definitions.
- ExtractPublicKeyHash
- CloseHandle
- IsHandleValid
- NullHandle
- IsValidNow
- public_key_hash
- VerifyAuthenticodeSignature
- VerifySignerIsGoogle
#include "chrome/app/signature_validator_win.h"
#include <atlstr.h>
#include <softpub.h>
#include <wincrypt.h>
#include <windows.h>
#include <wintrust.h>
#include <algorithm>
#include "base/files/file_path.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/win/scoped_handle.h"
#include "crypto/sha2.h"
namespace {
bool ExtractPublicKeyHash(const CERT_CONTEXT* cert_context,
std::string* public_key_hash) {
public_key_hash->clear();
CRYPT_BIT_BLOB crypt_blob =
cert_context->pCertInfo->SubjectPublicKeyInfo.PublicKey;
if (crypt_blob.cUnusedBits != 0)
return false;
uint8 hash[crypto::kSHA256Length] = {};
base::StringPiece key_bytes(reinterpret_cast<char*>(
crypt_blob.pbData), crypt_blob.cbData);
crypto::SHA256HashString(key_bytes, hash, crypto::kSHA256Length);
*public_key_hash = StringToLowerASCII(base::HexEncode(hash, arraysize(hash)));
return true;
}
class CertStoreHandleTraits {
public:
typedef HCERTSTORE Handle;
static bool CloseHandle(HCERTSTORE handle) {
return CertCloseStore(handle, 0) != FALSE;
}
static bool IsHandleValid(HCERTSTORE handle) {
return handle != NULL;
}
static HANDLE NullHandle() {
return NULL;
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(CertStoreHandleTraits);
};
typedef base::win::GenericScopedHandle<CertStoreHandleTraits,
base::win::DummyVerifierTraits> ScopedCertStoreHandle;
class CertInfo {
public:
explicit CertInfo(const CERT_CONTEXT* given_cert_context)
: cert_context_(NULL) {
if (given_cert_context) {
cert_context_ = CertDuplicateCertificateContext(given_cert_context);
not_valid_before_ = cert_context_->pCertInfo->NotBefore;
not_valid_after_ = cert_context_->pCertInfo->NotAfter;
ExtractPublicKeyHash(cert_context_, &public_key_hash_);
}
}
~CertInfo() {
if (cert_context_) {
CertFreeCertificateContext(cert_context_);
cert_context_ = NULL;
}
}
bool IsValidNow() const {
base::Time now = base::Time::NowFromSystemTime();
FILETIME filetime_now = now.ToFileTime();
return ((CompareFileTime(&filetime_now, ¬_valid_before_) > 0) &&
(CompareFileTime(&filetime_now, ¬_valid_after_) < 0));
}
std::string public_key_hash() const {
return public_key_hash_;
}
private:
const CERT_CONTEXT* cert_context_;
std::string public_key_hash_;
FILETIME not_valid_before_;
FILETIME not_valid_after_;
};
}
bool VerifyAuthenticodeSignature(const base::FilePath& signed_file) {
const HWND window_mode = reinterpret_cast<HWND>(INVALID_HANDLE_VALUE);
GUID verification_type = WINTRUST_ACTION_GENERIC_VERIFY_V2;
WINTRUST_FILE_INFO file_info = {};
file_info.cbStruct = sizeof(file_info);
file_info.pcwszFilePath = signed_file.value().c_str();
WINTRUST_DATA trust_data = {};
trust_data.cbStruct = sizeof(trust_data);
trust_data.dwUIChoice = WTD_UI_NONE;
trust_data.fdwRevocationChecks = WTD_REVOKE_NONE;
trust_data.dwUnionChoice = WTD_CHOICE_FILE;
trust_data.pFile = &file_info;
trust_data.dwProvFlags = WTD_REVOCATION_CHECK_NONE;
LONG result = WinVerifyTrust(window_mode, &verification_type, &trust_data);
return !result;
}
bool VerifySignerIsGoogle(const base::FilePath& signed_file,
const std::string& subject_name,
const std::vector<std::string>& expected_hashes) {
if (signed_file.empty())
return false;
HCERTSTORE cert_store = NULL;
BOOL succeeded = CryptQueryObject(
CERT_QUERY_OBJECT_FILE,
signed_file.value().c_str(),
CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
CERT_QUERY_FORMAT_FLAG_ALL,
0,
NULL,
NULL,
NULL,
&cert_store,
NULL,
NULL);
ScopedCertStoreHandle scoped_cert_store(cert_store);
if (!succeeded || !scoped_cert_store.IsValid())
return false;
PCCERT_CONTEXT cert_context_ptr = NULL;
cert_context_ptr = CertFindCertificateInStore(
cert_store,
X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
0,
CERT_FIND_SUBJECT_STR,
base::UTF8ToWide(subject_name).c_str(),
cert_context_ptr);
if (!cert_context_ptr)
return false;
CertInfo cert_info(cert_context_ptr);
CertFreeCertificateContext(cert_context_ptr);
cert_context_ptr = NULL;
std::vector<std::string>::const_iterator it = std::find(
expected_hashes.begin(),
expected_hashes.end(),
cert_info.public_key_hash());
if (it == expected_hashes.end() || !cert_info.IsValidNow())
return false;
return true;
}