@@ -191,23 +191,37 @@ static PVOID CstHashFile(
191
191
if (!NT_SUCCESS (status = PhCreateFileWin32 (& fileHandle , FileName , FILE_GENERIC_READ , FILE_ATTRIBUTE_NORMAL , FILE_SHARE_READ , FILE_OPEN , FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE )))
192
192
CstFailWithStatus (PhFormatString (L"Unable to open '%s'" , FileName )-> Buffer , status , 0 );
193
193
194
- buffer = PhAllocatePage (FILE_BUFFER_SIZE , NULL );
195
-
196
- while (TRUE)
194
+ if (buffer = PhAllocatePage (FILE_BUFFER_SIZE , NULL ))
197
195
{
198
- if (! NT_SUCCESS ( status = NtReadFile ( fileHandle , NULL , NULL , NULL , & iosb , buffer , FILE_BUFFER_SIZE , NULL , NULL )) )
196
+ while (TRUE )
199
197
{
200
- if (status == STATUS_END_OF_FILE )
201
- break ;
202
-
203
- CstFailWithStatus (PhFormatString (L"Unable to read '%s'" , FileName )-> Buffer , status , 0 );
198
+ status = NtReadFile (
199
+ fileHandle ,
200
+ NULL ,
201
+ NULL ,
202
+ NULL ,
203
+ & iosb ,
204
+ buffer ,
205
+ FILE_BUFFER_SIZE ,
206
+ NULL ,
207
+ NULL
208
+ );
209
+
210
+ if (!NT_SUCCESS (status ))
211
+ {
212
+ if (status == STATUS_END_OF_FILE )
213
+ break ;
214
+
215
+ CstFailWithStatus (PhFormatString (L"Unable to read '%s'" , FileName )-> Buffer , status , 0 );
216
+ }
217
+
218
+ if (!NT_SUCCESS (status = BCryptHashData (hashHandle , buffer , (ULONG )iosb .Information , 0 )))
219
+ CstFailWithStatus (L"Unable to hash file" , status , 0 );
204
220
}
205
221
206
- if (!NT_SUCCESS (status = BCryptHashData (hashHandle , buffer , (ULONG )iosb .Information , 0 )))
207
- CstFailWithStatus (L"Unable to hash file" , status , 0 );
222
+ PhFreePage (buffer );
208
223
}
209
224
210
- PhFreePage (buffer );
211
225
NtClose (fileHandle );
212
226
213
227
hash = PhAllocate (hashSize );
@@ -294,7 +308,7 @@ int __cdecl wmain(int argc, wchar_t *argv[])
294
308
CstFailWithStatus (L"Unable to open the signing algorithm provider" , status , 0 );
295
309
buffer = CstReadFile (CstKeyFileName -> Buffer , 1024 * 1024 , & bufferSize );
296
310
if (!NT_SUCCESS (status = BCryptImportKeyPair (signAlgHandle , NULL , CST_BLOB_PRIVATE , & keyHandle , buffer , bufferSize , 0 )))
297
- CstFailWithStatus (PhFormatString (L"Unable to import the private key" , CstKeyFileName -> Buffer )-> Buffer , status , 0 );
311
+ CstFailWithStatus (PhFormatString (L"Unable to import the private key: %s " , CstKeyFileName -> Buffer )-> Buffer , status , 0 );
298
312
PhFree (buffer );
299
313
300
314
// Hash the file.
@@ -308,6 +322,7 @@ int __cdecl wmain(int argc, wchar_t *argv[])
308
322
signature = PhAllocate (signatureSize );
309
323
if (!NT_SUCCESS (status = BCryptSignHash (keyHandle , NULL , hash , hashSize , signature , signatureSize , & signatureSize , 0 )))
310
324
CstFailWithStatus (L"Unable to create the signature" , status , 0 );
325
+
311
326
PhFree (hash );
312
327
BCryptDestroyKey (keyHandle );
313
328
BCryptCloseAlgorithmProvider (signAlgHandle , 0 );
@@ -356,7 +371,7 @@ int __cdecl wmain(int argc, wchar_t *argv[])
356
371
CstFailWithStatus (L"Unable to open the signing algorithm provider" , status , 0 );
357
372
buffer = CstReadFile (CstKeyFileName -> Buffer , 1024 * 1024 , & bufferSize );
358
373
if (!NT_SUCCESS (status = BCryptImportKeyPair (signAlgHandle , NULL , CST_BLOB_PUBLIC , & keyHandle , buffer , bufferSize , 0 )))
359
- CstFailWithStatus (PhFormatString (L"Unable to import the public key" , CstKeyFileName -> Buffer )-> Buffer , status , 0 );
374
+ CstFailWithStatus (PhFormatString (L"Unable to import the public key: %s " , CstKeyFileName -> Buffer )-> Buffer , status , 0 );
360
375
PhFree (buffer );
361
376
362
377
// Read the signature.
@@ -370,7 +385,8 @@ int __cdecl wmain(int argc, wchar_t *argv[])
370
385
// Verify the hash.
371
386
372
387
if (!NT_SUCCESS (status = BCryptVerifySignature (keyHandle , NULL , hash , hashSize , signature , signatureSize , 0 )))
373
- CstFailWithStatus (PhFormatString (L"Signature verification failed" , CstKeyFileName -> Buffer )-> Buffer , status , 0 );
388
+ CstFailWithStatus (PhFormatString (L"Signature verification failed: %s" , CstKeyFileName -> Buffer )-> Buffer , status , 0 );
389
+
374
390
PhFree (signature );
375
391
PhFree (hash );
376
392
0 commit comments