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, 0)) {
58     return FALSE;
59   }
60 
61   DWORD dwCount = sizeof(DWORD);
62   if (!CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&hashSize, &dwCount, 0)) {
63     return FALSE;
64   }
65 
66   *hash = new BYTE[hashSize];
67   ZeroMemory(*hash, hashSize);
68   if (!CryptGetHashParam(hHash, HP_HASHVAL, *hash, &hashSize, 0)) {
69     return FALSE;
70   }
71 
72   if (hHash) {
73     CryptDestroyHash(hHash);
74   }
75 
76   if (hProv) {
77     CryptReleaseContext(hProv, 0);
78   }
79 
80   return TRUE;
81 }
82 
83 /**
84  * Converts a file path into a unique registry location for cert storage
85  *
86  * @param  filePath     The input file path to get a registry path from
87  * @param  registryPath A buffer to write the registry path to, must
88  *                      be of size in WCHARs MAX_PATH + 1
89  * @return TRUE if successful
90  */
CalculateRegistryPathFromFilePath(const LPCWSTR filePath,LPWSTR registryPath)91 BOOL CalculateRegistryPathFromFilePath(const LPCWSTR filePath,
92                                        LPWSTR registryPath) {
93   size_t filePathLen = wcslen(filePath);
94   if (!filePathLen) {
95     return FALSE;
96   }
97 
98   // If the file path ends in a slash, ignore that character
99   if (filePath[filePathLen - 1] == L'\\' || filePath[filePathLen - 1] == L'/') {
100     filePathLen--;
101   }
102 
103   // Copy in the full path into our own buffer.
104   // Copying in the extra slash is OK because we calculate the hash
105   // based on the filePathLen which excludes the slash.
106   // +2 to account for the possibly trailing slash and the null terminator.
107   WCHAR* lowercasePath = new WCHAR[filePathLen + 2];
108   memset(lowercasePath, 0, (filePathLen + 2) * sizeof(WCHAR));
109   wcsncpy(lowercasePath, filePath, filePathLen + 1);
110   _wcslwr(lowercasePath);
111 
112   BYTE* hash;
113   DWORD hashSize = 0;
114   if (!CalculateMD5(reinterpret_cast<const char*>(lowercasePath),
115                     filePathLen * 2, &hash, hashSize)) {
116     delete[] lowercasePath;
117     return FALSE;
118   }
119   delete[] lowercasePath;
120 
121   LPCWSTR baseRegPath =
122       L"SOFTWARE\\Mozilla\\"
123       L"MaintenanceService\\";
124   wcsncpy(registryPath, baseRegPath, MAX_PATH);
125   BinaryDataToHexString(hash, hashSize, registryPath + wcslen(baseRegPath));
126   delete[] hash;
127   return TRUE;
128 }
129