toolkit/components/maintenanceservice/registrycertificates.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include <stdio.h>
michael@0 6 #include <stdlib.h>
michael@0 7 #include <windows.h>
michael@0 8
michael@0 9 #include "registrycertificates.h"
michael@0 10 #include "pathhash.h"
michael@0 11 #include "nsWindowsHelpers.h"
michael@0 12 #include "servicebase.h"
michael@0 13 #include "updatehelper.h"
michael@0 14 #define MAX_KEY_LENGTH 255
michael@0 15
michael@0 16 /**
michael@0 17 * Verifies if the file path matches any certificate stored in the registry.
michael@0 18 *
michael@0 19 * @param filePath The file path of the application to check if allowed.
michael@0 20 * @return TRUE if the binary matches any of the allowed certificates.
michael@0 21 */
michael@0 22 BOOL
michael@0 23 DoesBinaryMatchAllowedCertificates(LPCWSTR basePathForUpdate, LPCWSTR filePath)
michael@0 24 {
michael@0 25 WCHAR maintenanceServiceKey[MAX_PATH + 1];
michael@0 26 if (!CalculateRegistryPathFromFilePath(basePathForUpdate,
michael@0 27 maintenanceServiceKey)) {
michael@0 28 return FALSE;
michael@0 29 }
michael@0 30
michael@0 31 // We use KEY_WOW64_64KEY to always force 64-bit view.
michael@0 32 // The user may have both x86 and x64 applications installed
michael@0 33 // which each register information. We need a consistent place
michael@0 34 // to put those certificate attributes in and hence why we always
michael@0 35 // force the non redirected registry under Wow6432Node.
michael@0 36 // This flag is ignored on 32bit systems.
michael@0 37 HKEY baseKeyRaw;
michael@0 38 LONG retCode = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
michael@0 39 maintenanceServiceKey, 0,
michael@0 40 KEY_READ | KEY_WOW64_64KEY, &baseKeyRaw);
michael@0 41 if (retCode != ERROR_SUCCESS) {
michael@0 42 LOG_WARN(("Could not open key. (%d)", retCode));
michael@0 43 // Our tests run with a different apply directory for each test.
michael@0 44 // We use this registry key on our test slaves to store the
michael@0 45 // allowed name/issuers.
michael@0 46 retCode = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
michael@0 47 TEST_ONLY_FALLBACK_KEY_PATH, 0,
michael@0 48 KEY_READ | KEY_WOW64_64KEY, &baseKeyRaw);
michael@0 49 if (retCode != ERROR_SUCCESS) {
michael@0 50 LOG_WARN(("Could not open fallback key. (%d)", retCode));
michael@0 51 return FALSE;
michael@0 52 }
michael@0 53 }
michael@0 54 nsAutoRegKey baseKey(baseKeyRaw);
michael@0 55
michael@0 56 // Get the number of subkeys.
michael@0 57 DWORD subkeyCount = 0;
michael@0 58 retCode = RegQueryInfoKeyW(baseKey, nullptr, nullptr, nullptr, &subkeyCount,
michael@0 59 nullptr, nullptr, nullptr, nullptr, nullptr,
michael@0 60 nullptr, nullptr);
michael@0 61 if (retCode != ERROR_SUCCESS) {
michael@0 62 LOG_WARN(("Could not query info key. (%d)", retCode));
michael@0 63 return FALSE;
michael@0 64 }
michael@0 65
michael@0 66 // Enumerate the subkeys, each subkey represents an allowed certificate.
michael@0 67 for (DWORD i = 0; i < subkeyCount; i++) {
michael@0 68 WCHAR subkeyBuffer[MAX_KEY_LENGTH];
michael@0 69 DWORD subkeyBufferCount = MAX_KEY_LENGTH;
michael@0 70 retCode = RegEnumKeyExW(baseKey, i, subkeyBuffer,
michael@0 71 &subkeyBufferCount, nullptr,
michael@0 72 nullptr, nullptr, nullptr);
michael@0 73 if (retCode != ERROR_SUCCESS) {
michael@0 74 LOG_WARN(("Could not enum certs. (%d)", retCode));
michael@0 75 return FALSE;
michael@0 76 }
michael@0 77
michael@0 78 // Open the subkey for the current certificate
michael@0 79 HKEY subKeyRaw;
michael@0 80 retCode = RegOpenKeyExW(baseKey,
michael@0 81 subkeyBuffer,
michael@0 82 0,
michael@0 83 KEY_READ | KEY_WOW64_64KEY,
michael@0 84 &subKeyRaw);
michael@0 85 nsAutoRegKey subKey(subKeyRaw);
michael@0 86 if (retCode != ERROR_SUCCESS) {
michael@0 87 LOG_WARN(("Could not open subkey. (%d)", retCode));
michael@0 88 continue; // Try the next subkey
michael@0 89 }
michael@0 90
michael@0 91 const int MAX_CHAR_COUNT = 256;
michael@0 92 DWORD valueBufSize = MAX_CHAR_COUNT * sizeof(WCHAR);
michael@0 93 WCHAR name[MAX_CHAR_COUNT] = { L'\0' };
michael@0 94 WCHAR issuer[MAX_CHAR_COUNT] = { L'\0' };
michael@0 95
michael@0 96 // Get the name from the registry
michael@0 97 retCode = RegQueryValueExW(subKey, L"name", 0, nullptr,
michael@0 98 (LPBYTE)name, &valueBufSize);
michael@0 99 if (retCode != ERROR_SUCCESS) {
michael@0 100 LOG_WARN(("Could not obtain name from registry. (%d)", retCode));
michael@0 101 continue; // Try the next subkey
michael@0 102 }
michael@0 103
michael@0 104 // Get the issuer from the registry
michael@0 105 valueBufSize = MAX_CHAR_COUNT * sizeof(WCHAR);
michael@0 106 retCode = RegQueryValueExW(subKey, L"issuer", 0, nullptr,
michael@0 107 (LPBYTE)issuer, &valueBufSize);
michael@0 108 if (retCode != ERROR_SUCCESS) {
michael@0 109 LOG_WARN(("Could not obtain issuer from registry. (%d)", retCode));
michael@0 110 continue; // Try the next subkey
michael@0 111 }
michael@0 112
michael@0 113 CertificateCheckInfo allowedCertificate = {
michael@0 114 name,
michael@0 115 issuer,
michael@0 116 };
michael@0 117
michael@0 118 retCode = CheckCertificateForPEFile(filePath, allowedCertificate);
michael@0 119 if (retCode != ERROR_SUCCESS) {
michael@0 120 LOG_WARN(("Error on certificate check. (%d)", retCode));
michael@0 121 continue; // Try the next subkey
michael@0 122 }
michael@0 123
michael@0 124 retCode = VerifyCertificateTrustForFile(filePath);
michael@0 125 if (retCode != ERROR_SUCCESS) {
michael@0 126 LOG_WARN(("Error on certificate trust check. (%d)", retCode));
michael@0 127 continue; // Try the next subkey
michael@0 128 }
michael@0 129
michael@0 130 // Raise the roof, we found a match!
michael@0 131 return TRUE;
michael@0 132 }
michael@0 133
michael@0 134 // No certificates match, :'(
michael@0 135 return FALSE;
michael@0 136 }

mercurial