1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #include <windows.h>
6 #include <wincrypt.h>
7 #include "pathhash.h"
8 
9 /**
10  * Converts a binary sequence into a hex string
11  *
12  * @param hash      The binary data sequence
13  * @param hashSize  The size of the binary data sequence
14  * @param hexString A buffer to store the hex string, must be of
15  *                  size 2 * @hashSize
16  */
BinaryDataToHexString(const BYTE * hash,DWORD & hashSize,LPWSTR hexString)17 static void BinaryDataToHexString(const BYTE *hash, DWORD &hashSize,
18                                   LPWSTR hexString) {
19   WCHAR *p = hexString;
20   for (DWORD i = 0; i < hashSize; ++i) {
21     wsprintfW(p, L"%.2x", hash[i]);
22     p += 2;
23   }
24 }
25 
26 /**
27  * Calculates an MD5 hash for the given input binary data
28  *
29  * @param  data     Any sequence of bytes
30  * @param  dataSize The number of bytes inside @data
31  * @param  hash     Output buffer to store hash, must be freed by the caller
32  * @param  hashSize The number of bytes in the output buffer
33  * @return TRUE on success
34  */
CalculateMD5(const char * data,DWORD dataSize,BYTE ** hash,DWORD & hashSize)35 static BOOL CalculateMD5(const char *data, DWORD dataSize, BYTE **hash,
36                          DWORD &hashSize) {
37   HCRYPTPROV hProv = 0;
38   HCRYPTHASH hHash = 0;
39 
40   if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL,
41                            CRYPT_VERIFYCONTEXT)) {
42     if ((DWORD)NTE_BAD_KEYSET != GetLastError()) {
43       return FALSE;
44     }
45 
46     // Maybe it doesn't exist, try to create it.
47     if (!CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL,
48                              CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET)) {
49       return FALSE;
50     }
51   }
52 
53   if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
54     return FALSE;
55   }
56 
57   if (!CryptHashData(hHash, reinterpret_cast<const BYTE *>(data), dataSize,
58                      0)) {
59     return FALSE;
60   }
61 
62   DWORD dwCount = sizeof(DWORD);
63   if (!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE *)&hashSize, &dwCount, 0)) {
64     return FALSE;
65   }
66 
67   *hash = new BYTE[hashSize];
68   ZeroMemory(*hash, hashSize);
69   if (!CryptGetHashParam(hHash, HP_HASHVAL, *hash, &hashSize, 0)) {
70     return FALSE;
71   }
72 
73   if (hHash) {
74     CryptDestroyHash(hHash);
75   }
76 
77   if (hProv) {
78     CryptReleaseContext(hProv, 0);
79   }
80 
81   return TRUE;
82 }
83 
84 /**
85  * Converts a file path into a unique registry location for cert storage
86  *
87  * @param  filePath     The input file path to get a registry path from
88  * @param  registryPath A buffer to write the registry path to, must
89  *                      be of size in WCHARs MAX_PATH + 1
90  * @return TRUE if successful
91  */
CalculateRegistryPathFromFilePath(const LPCWSTR filePath,LPWSTR registryPath)92 BOOL CalculateRegistryPathFromFilePath(const LPCWSTR filePath,
93                                        LPWSTR registryPath) {
94   size_t filePathLen = wcslen(filePath);
95   if (!filePathLen) {
96     return FALSE;
97   }
98 
99   // If the file path ends in a slash, ignore that character
100   if (filePath[filePathLen - 1] == L'\\' || filePath[filePathLen - 1] == L'/') {
101     filePathLen--;
102   }
103 
104   // Copy in the full path into our own buffer.
105   // Copying in the extra slash is OK because we calculate the hash
106   // based on the filePathLen which excludes the slash.
107   // +2 to account for the possibly trailing slash and the null terminator.
108   WCHAR *lowercasePath = new WCHAR[filePathLen + 2];
109   memset(lowercasePath, 0, (filePathLen + 2) * sizeof(WCHAR));
110   wcsncpy(lowercasePath, filePath, filePathLen + 1);
111   _wcslwr(lowercasePath);
112 
113   BYTE *hash;
114   DWORD hashSize = 0;
115   if (!CalculateMD5(reinterpret_cast<const char *>(lowercasePath),
116                     filePathLen * 2, &hash, hashSize)) {
117     delete[] lowercasePath;
118     return FALSE;
119   }
120   delete[] lowercasePath;
121 
122   LPCWSTR baseRegPath =
123       L"SOFTWARE\\Mozilla\\"
124       L"MaintenanceService\\";
125   wcsncpy(registryPath, baseRegPath, MAX_PATH);
126   BinaryDataToHexString(hash, hashSize, registryPath + wcslen(baseRegPath));
127   delete[] hash;
128   return TRUE;
129 }
130