[CERTUTIL] Add skeleton application with support for -hashfile

This commit is contained in:
Mark Jansen 2020-09-10 23:40:52 +02:00
parent 166f815e7b
commit ee69ca786a
4 changed files with 185 additions and 0 deletions

View file

@ -1,5 +1,6 @@
add_subdirectory(at)
add_subdirectory(attrib)
add_subdirectory(certutil)
add_subdirectory(chcp)
add_subdirectory(clip)
add_subdirectory(comp)

View file

@ -0,0 +1,13 @@
include_directories(${REACTOS_SOURCE_DIR}/sdk/lib/conutils)
list(APPEND SOURCE
certutil.c
precomp.h)
add_executable(certutil ${SOURCE})
set_module_type(certutil win32cui UNICODE)
target_link_libraries(certutil conutils ${PSEH_LIB})
add_importlibs(certutil advapi32 msvcrt kernel32)
add_pch(certutil precomp.h SOURCE)
add_cd_file(TARGET certutil DESTINATION reactos/system32 FOR all)

View file

@ -0,0 +1,155 @@
/*
* PROJECT: ReactOS certutil
* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
* PURPOSE: CertUtil stub
* COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
*
* Note: Only -hashfile is implemented for now, the rest is not present!
*/
#include "precomp.h"
#include <wincrypt.h>
#include <stdlib.h>
static BOOL hash_file(LPCWSTR Filename)
{
HCRYPTPROV hProv;
BOOL bSuccess = FALSE;
HANDLE hFile = CreateFileW(Filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
ConPrintf(StdOut, L"CertUtil: -hashfile command failed: %d\n", GetLastError());
return bSuccess;
}
if (CryptAcquireContextW(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
HCRYPTHASH hHash;
if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
BYTE Buffer[2048];
DWORD cbRead;
while ((bSuccess = ReadFile(hFile, Buffer, sizeof(Buffer), &cbRead, NULL)))
{
if (cbRead == 0)
break;
if (!CryptHashData(hHash, Buffer, cbRead, 0))
{
bSuccess = FALSE;
ConPrintf(StdOut, L"CertUtil: -hashfile command failed to hash: %d\n", GetLastError());
break;
}
}
if (bSuccess)
{
BYTE rgbHash[20];
DWORD cbHash, n;
if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
ConPrintf(StdOut, L"SHA1 hash of %s:\n", Filename);
for (n = 0; n < cbHash; ++n)
{
ConPrintf(StdOut, L"%02x", rgbHash[n]);
}
ConPuts(StdOut, L"\n");
}
else
{
ConPrintf(StdOut, L"CertUtil: -hashfile command failed to extract hash: %d\n", GetLastError());
bSuccess = FALSE;
}
}
CryptDestroyHash(hHash);
}
else
{
ConPrintf(StdOut, L"CertUtil: -hashfile command no algorithm: %d\n", GetLastError());
}
CryptReleaseContext(hProv, 0);
}
else
{
ConPrintf(StdOut, L"CertUtil: -hashfile command no context: %d\n", GetLastError());
}
CloseHandle(hFile);
return bSuccess;
}
static void print_usage()
{
ConPuts(StdOut, L"Verbs:\n");
ConPuts(StdOut, L" -hashfile -- Display cryptographic hash over a file\n");
ConPuts(StdOut, L"\n");
ConPuts(StdOut, L"CertUtil -? -- Display a list of all verbs\n");
ConPuts(StdOut, L"CertUtil -hashfile -? -- Display help text for the 'hashfile' verb\n");
}
int wmain(int argc, WCHAR *argv[])
{
int n;
/* Initialize the Console Standard Streams */
ConInitStdStreams();
if (argc == 1) /* i.e. no commandline arguments given */
{
print_usage();
return EXIT_SUCCESS;
}
for (n = 1; n < argc; ++n)
{
if (!_wcsicmp(argv[n], L"-?"))
{
print_usage();
return EXIT_SUCCESS;
}
else if (!_wcsicmp(argv[n], L"-hashfile"))
{
if (argc == 3)
{
if (!_wcsicmp(argv[n+1], L"-?"))
{
print_usage();
return EXIT_SUCCESS;
}
else
{
if (!hash_file(argv[n+1]))
{
/* hash_file prints the failure itself */
return EXIT_FAILURE;
}
ConPuts(StdOut, L"CertUtil: -hashfile command completed successfully\n");
return EXIT_SUCCESS;
}
}
else
{
ConPrintf(StdOut, L"CertUtil: -hashfile expected 1 argument, got %d\n", argc - 2);
return EXIT_FAILURE;
}
}
else
{
ConPrintf(StdOut, L"CertUtil: Unknown verb: %s\n", argv[n]);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,16 @@
#ifndef __CERTUTIL_PRECOMP_H
#define __CERTUTIL_PRECOMP_H
/* INCLUDES ******************************************************************/
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winreg.h>
#include <winuser.h>
#include <conutils.h>
#endif /* __CERTUTIL_PRECOMP_H */