diff options
Diffstat (limited to 'src/dutil/cryputil.cpp')
| -rw-r--r-- | src/dutil/cryputil.cpp | 63 |
1 files changed, 39 insertions, 24 deletions
diff --git a/src/dutil/cryputil.cpp b/src/dutil/cryputil.cpp index 214704b4..c5c1b221 100644 --- a/src/dutil/cryputil.cpp +++ b/src/dutil/cryputil.cpp | |||
| @@ -2,6 +2,21 @@ | |||
| 2 | 2 | ||
| 3 | #include "precomp.h" | 3 | #include "precomp.h" |
| 4 | 4 | ||
| 5 | |||
| 6 | // Exit macros | ||
| 7 | #define CrypExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_CRYPUTIL, x, s, __VA_ARGS__) | ||
| 8 | #define CrypExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_CRYPUTIL, x, s, __VA_ARGS__) | ||
| 9 | #define CrypExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_CRYPUTIL, x, s, __VA_ARGS__) | ||
| 10 | #define CrypExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_CRYPUTIL, x, s, __VA_ARGS__) | ||
| 11 | #define CrypExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_CRYPUTIL, x, s, __VA_ARGS__) | ||
| 12 | #define CrypExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_CRYPUTIL, x, s, __VA_ARGS__) | ||
| 13 | #define CrypExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_CRYPUTIL, p, x, e, s, __VA_ARGS__) | ||
| 14 | #define CrypExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_CRYPUTIL, p, x, s, __VA_ARGS__) | ||
| 15 | #define CrypExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_CRYPUTIL, p, x, e, s, __VA_ARGS__) | ||
| 16 | #define CrypExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_CRYPUTIL, p, x, s, __VA_ARGS__) | ||
| 17 | #define CrypExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_CRYPUTIL, e, x, s, __VA_ARGS__) | ||
| 18 | #define CrypExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_CRYPUTIL, g, x, s, __VA_ARGS__) | ||
| 19 | |||
| 5 | static PFN_RTLENCRYPTMEMORY vpfnRtlEncryptMemory = NULL; | 20 | static PFN_RTLENCRYPTMEMORY vpfnRtlEncryptMemory = NULL; |
| 6 | static PFN_RTLDECRYPTMEMORY vpfnRtlDecryptMemory = NULL; | 21 | static PFN_RTLDECRYPTMEMORY vpfnRtlDecryptMemory = NULL; |
| 7 | static PFN_CRYPTPROTECTMEMORY vpfnCryptProtectMemory = NULL; | 22 | static PFN_CRYPTPROTECTMEMORY vpfnCryptProtectMemory = NULL; |
| @@ -32,17 +47,17 @@ extern "C" HRESULT DAPI CrypInitialize( | |||
| 32 | if (!vpfnRtlEncryptMemory || !vpfnRtlDecryptMemory) | 47 | if (!vpfnRtlEncryptMemory || !vpfnRtlDecryptMemory) |
| 33 | { | 48 | { |
| 34 | hr = LoadSystemLibrary(L"Crypt32.dll", &vhCrypt32Dll); | 49 | hr = LoadSystemLibrary(L"Crypt32.dll", &vhCrypt32Dll); |
| 35 | ExitOnFailure(hr, "Failed to load Crypt32.dll"); | 50 | CrypExitOnFailure(hr, "Failed to load Crypt32.dll"); |
| 36 | 51 | ||
| 37 | vpfnCryptProtectMemory = reinterpret_cast<PFN_CRYPTPROTECTMEMORY>(::GetProcAddress(vhCrypt32Dll, "CryptProtectMemory")); | 52 | vpfnCryptProtectMemory = reinterpret_cast<PFN_CRYPTPROTECTMEMORY>(::GetProcAddress(vhCrypt32Dll, "CryptProtectMemory")); |
| 38 | if (!vpfnRtlEncryptMemory && !vpfnCryptProtectMemory) | 53 | if (!vpfnRtlEncryptMemory && !vpfnCryptProtectMemory) |
| 39 | { | 54 | { |
| 40 | ExitWithLastError(hr, "Failed to load an encryption method"); | 55 | CrypExitWithLastError(hr, "Failed to load an encryption method"); |
| 41 | } | 56 | } |
| 42 | vpfnCryptUnprotectMemory = reinterpret_cast<PFN_CRYPTUNPROTECTMEMORY>(::GetProcAddress(vhCrypt32Dll, "CryptUnprotectMemory")); | 57 | vpfnCryptUnprotectMemory = reinterpret_cast<PFN_CRYPTUNPROTECTMEMORY>(::GetProcAddress(vhCrypt32Dll, "CryptUnprotectMemory")); |
| 43 | if (!vpfnRtlDecryptMemory && !vpfnCryptUnprotectMemory) | 58 | if (!vpfnRtlDecryptMemory && !vpfnCryptUnprotectMemory) |
| 44 | { | 59 | { |
| 45 | ExitWithLastError(hr, "Failed to load a decryption method"); | 60 | CrypExitWithLastError(hr, "Failed to load a decryption method"); |
| 46 | } | 61 | } |
| 47 | } | 62 | } |
| 48 | 63 | ||
| @@ -94,15 +109,15 @@ extern "C" HRESULT DAPI CrypDecodeObject( | |||
| 94 | 109 | ||
| 95 | if (!::CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, szStructType, pbData, cbData, dwFlags, NULL, &cbObject)) | 110 | if (!::CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, szStructType, pbData, cbData, dwFlags, NULL, &cbObject)) |
| 96 | { | 111 | { |
| 97 | ExitWithLastError(hr, "Failed to decode object to determine size."); | 112 | CrypExitWithLastError(hr, "Failed to decode object to determine size."); |
| 98 | } | 113 | } |
| 99 | 114 | ||
| 100 | pvObject = MemAlloc(cbObject, TRUE); | 115 | pvObject = MemAlloc(cbObject, TRUE); |
| 101 | ExitOnNull(pvObject, hr, E_OUTOFMEMORY, "Failed to allocate memory for decoded object."); | 116 | CrypExitOnNull(pvObject, hr, E_OUTOFMEMORY, "Failed to allocate memory for decoded object."); |
| 102 | 117 | ||
| 103 | if (!::CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, szStructType, pbData, cbData, dwFlags, pvObject, &cbObject)) | 118 | if (!::CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, szStructType, pbData, cbData, dwFlags, pvObject, &cbObject)) |
| 104 | { | 119 | { |
| 105 | ExitWithLastError(hr, "Failed to decode object."); | 120 | CrypExitWithLastError(hr, "Failed to decode object."); |
| 106 | } | 121 | } |
| 107 | 122 | ||
| 108 | *ppvObject = pvObject; | 123 | *ppvObject = pvObject; |
| @@ -134,15 +149,15 @@ extern "C" HRESULT DAPI CrypMsgGetParam( | |||
| 134 | 149 | ||
| 135 | if (!::CryptMsgGetParam(hCryptMsg, dwType, dwIndex, NULL, &cb)) | 150 | if (!::CryptMsgGetParam(hCryptMsg, dwType, dwIndex, NULL, &cb)) |
| 136 | { | 151 | { |
| 137 | ExitWithLastError(hr, "Failed to get crypt message parameter data size."); | 152 | CrypExitWithLastError(hr, "Failed to get crypt message parameter data size."); |
| 138 | } | 153 | } |
| 139 | 154 | ||
| 140 | pv = MemAlloc(cb, TRUE); | 155 | pv = MemAlloc(cb, TRUE); |
| 141 | ExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to allocate memory for crypt message parameter."); | 156 | CrypExitOnNull(pv, hr, E_OUTOFMEMORY, "Failed to allocate memory for crypt message parameter."); |
| 142 | 157 | ||
| 143 | if (!::CryptMsgGetParam(hCryptMsg, dwType, dwIndex, pv, &cb)) | 158 | if (!::CryptMsgGetParam(hCryptMsg, dwType, dwIndex, pv, &cb)) |
| 144 | { | 159 | { |
| 145 | ExitWithLastError(hr, "Failed to get crypt message parameter."); | 160 | CrypExitWithLastError(hr, "Failed to get crypt message parameter."); |
| 146 | } | 161 | } |
| 147 | 162 | ||
| 148 | *ppvData = pv; | 163 | *ppvData = pv; |
| @@ -161,7 +176,7 @@ LExit: | |||
| 161 | 176 | ||
| 162 | 177 | ||
| 163 | extern "C" HRESULT DAPI CrypHashFile( | 178 | extern "C" HRESULT DAPI CrypHashFile( |
| 164 | __in LPCWSTR wzFilePath, | 179 | __in_z LPCWSTR wzFilePath, |
| 165 | __in DWORD dwProvType, | 180 | __in DWORD dwProvType, |
| 166 | __in ALG_ID algid, | 181 | __in ALG_ID algid, |
| 167 | __out_bcount(cbHash) BYTE* pbHash, | 182 | __out_bcount(cbHash) BYTE* pbHash, |
| @@ -176,11 +191,11 @@ extern "C" HRESULT DAPI CrypHashFile( | |||
| 176 | hFile = ::CreateFileW(wzFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); | 191 | hFile = ::CreateFileW(wzFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); |
| 177 | if (INVALID_HANDLE_VALUE == hFile) | 192 | if (INVALID_HANDLE_VALUE == hFile) |
| 178 | { | 193 | { |
| 179 | ExitWithLastError(hr, "Failed to open input file."); | 194 | CrypExitWithLastError(hr, "Failed to open input file."); |
| 180 | } | 195 | } |
| 181 | 196 | ||
| 182 | hr = CrypHashFileHandle(hFile, dwProvType, algid, pbHash, cbHash, pqwBytesHashed); | 197 | hr = CrypHashFileHandle(hFile, dwProvType, algid, pbHash, cbHash, pqwBytesHashed); |
| 183 | ExitOnFailure(hr, "Failed to hash file: %ls", wzFilePath); | 198 | CrypExitOnFailure(hr, "Failed to hash file: %ls", wzFilePath); |
| 184 | 199 | ||
| 185 | LExit: | 200 | LExit: |
| 186 | ReleaseFileHandle(hFile); | 201 | ReleaseFileHandle(hFile); |
| @@ -208,13 +223,13 @@ extern "C" HRESULT DAPI CrypHashFileHandle( | |||
| 208 | // get handle to the crypto provider | 223 | // get handle to the crypto provider |
| 209 | if (!::CryptAcquireContextW(&hProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) | 224 | if (!::CryptAcquireContextW(&hProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) |
| 210 | { | 225 | { |
| 211 | ExitWithLastError(hr, "Failed to acquire crypto context."); | 226 | CrypExitWithLastError(hr, "Failed to acquire crypto context."); |
| 212 | } | 227 | } |
| 213 | 228 | ||
| 214 | // initiate hash | 229 | // initiate hash |
| 215 | if (!::CryptCreateHash(hProv, algid, 0, 0, &hHash)) | 230 | if (!::CryptCreateHash(hProv, algid, 0, 0, &hHash)) |
| 216 | { | 231 | { |
| 217 | ExitWithLastError(hr, "Failed to initiate hash."); | 232 | CrypExitWithLastError(hr, "Failed to initiate hash."); |
| 218 | } | 233 | } |
| 219 | 234 | ||
| 220 | for (;;) | 235 | for (;;) |
| @@ -222,7 +237,7 @@ extern "C" HRESULT DAPI CrypHashFileHandle( | |||
| 222 | // read data block | 237 | // read data block |
| 223 | if (!::ReadFile(hFile, rgbBuffer, sizeof(rgbBuffer), &cbRead, NULL)) | 238 | if (!::ReadFile(hFile, rgbBuffer, sizeof(rgbBuffer), &cbRead, NULL)) |
| 224 | { | 239 | { |
| 225 | ExitWithLastError(hr, "Failed to read data block."); | 240 | CrypExitWithLastError(hr, "Failed to read data block."); |
| 226 | } | 241 | } |
| 227 | 242 | ||
| 228 | if (!cbRead) | 243 | if (!cbRead) |
| @@ -233,21 +248,21 @@ extern "C" HRESULT DAPI CrypHashFileHandle( | |||
| 233 | // hash data block | 248 | // hash data block |
| 234 | if (!::CryptHashData(hHash, rgbBuffer, cbRead, 0)) | 249 | if (!::CryptHashData(hHash, rgbBuffer, cbRead, 0)) |
| 235 | { | 250 | { |
| 236 | ExitWithLastError(hr, "Failed to hash data block."); | 251 | CrypExitWithLastError(hr, "Failed to hash data block."); |
| 237 | } | 252 | } |
| 238 | } | 253 | } |
| 239 | 254 | ||
| 240 | // get hash value | 255 | // get hash value |
| 241 | if (!::CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &cbHash, 0)) | 256 | if (!::CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &cbHash, 0)) |
| 242 | { | 257 | { |
| 243 | ExitWithLastError(hr, "Failed to get hash value."); | 258 | CrypExitWithLastError(hr, "Failed to get hash value."); |
| 244 | } | 259 | } |
| 245 | 260 | ||
| 246 | if (pqwBytesHashed) | 261 | if (pqwBytesHashed) |
| 247 | { | 262 | { |
| 248 | if (!::SetFilePointerEx(hFile, liZero, (LARGE_INTEGER*)pqwBytesHashed, FILE_CURRENT)) | 263 | if (!::SetFilePointerEx(hFile, liZero, (LARGE_INTEGER*)pqwBytesHashed, FILE_CURRENT)) |
| 249 | { | 264 | { |
| 250 | ExitWithLastError(hr, "Failed to get file pointer."); | 265 | CrypExitWithLastError(hr, "Failed to get file pointer."); |
| 251 | } | 266 | } |
| 252 | } | 267 | } |
| 253 | 268 | ||
| @@ -280,24 +295,24 @@ HRESULT DAPI CrypHashBuffer( | |||
| 280 | // get handle to the crypto provider | 295 | // get handle to the crypto provider |
| 281 | if (!::CryptAcquireContextW(&hProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) | 296 | if (!::CryptAcquireContextW(&hProv, NULL, NULL, dwProvType, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) |
| 282 | { | 297 | { |
| 283 | ExitWithLastError(hr, "Failed to acquire crypto context."); | 298 | CrypExitWithLastError(hr, "Failed to acquire crypto context."); |
| 284 | } | 299 | } |
| 285 | 300 | ||
| 286 | // initiate hash | 301 | // initiate hash |
| 287 | if (!::CryptCreateHash(hProv, algid, 0, 0, &hHash)) | 302 | if (!::CryptCreateHash(hProv, algid, 0, 0, &hHash)) |
| 288 | { | 303 | { |
| 289 | ExitWithLastError(hr, "Failed to initiate hash."); | 304 | CrypExitWithLastError(hr, "Failed to initiate hash."); |
| 290 | } | 305 | } |
| 291 | 306 | ||
| 292 | if (!::CryptHashData(hHash, pbBuffer, static_cast<DWORD>(cbBuffer), 0)) | 307 | if (!::CryptHashData(hHash, pbBuffer, static_cast<DWORD>(cbBuffer), 0)) |
| 293 | { | 308 | { |
| 294 | ExitWithLastError(hr, "Failed to hash data."); | 309 | CrypExitWithLastError(hr, "Failed to hash data."); |
| 295 | } | 310 | } |
| 296 | 311 | ||
| 297 | // get hash value | 312 | // get hash value |
| 298 | if (!::CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &cbHash, 0)) | 313 | if (!::CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &cbHash, 0)) |
| 299 | { | 314 | { |
| 300 | ExitWithLastError(hr, "Failed to get hash value."); | 315 | CrypExitWithLastError(hr, "Failed to get hash value."); |
| 301 | } | 316 | } |
| 302 | 317 | ||
| 303 | LExit: | 318 | LExit: |
| @@ -340,7 +355,7 @@ HRESULT DAPI CrypEncryptMemory( | |||
| 340 | hr = HRESULT_FROM_WIN32(::GetLastError()); | 355 | hr = HRESULT_FROM_WIN32(::GetLastError()); |
| 341 | } | 356 | } |
| 342 | } | 357 | } |
| 343 | ExitOnFailure(hr, "Failed to encrypt memory"); | 358 | CrypExitOnFailure(hr, "Failed to encrypt memory"); |
| 344 | LExit: | 359 | LExit: |
| 345 | return hr; | 360 | return hr; |
| 346 | } | 361 | } |
| @@ -372,7 +387,7 @@ HRESULT DAPI CrypDecryptMemory( | |||
| 372 | hr = HRESULT_FROM_WIN32(::GetLastError()); | 387 | hr = HRESULT_FROM_WIN32(::GetLastError()); |
| 373 | } | 388 | } |
| 374 | } | 389 | } |
| 375 | ExitOnFailure(hr, "Failed to decrypt memory"); | 390 | CrypExitOnFailure(hr, "Failed to decrypt memory"); |
| 376 | LExit: | 391 | LExit: |
| 377 | return hr; | 392 | return hr; |
| 378 | } | 393 | } |
