ReactOS 0.4.16-dev-199-g898cc56
certutil.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS certutil
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: CertUtil stub
5 * COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
6 *
7 * Note: Only -hashfile is implemented for now, the rest is not present!
8 */
9
10#include "precomp.h"
11#include <wincrypt.h>
12#include <stdlib.h>
13
14
16{
19
22
24 {
25 ConPrintf(StdOut, L"CertUtil: -hashfile command failed: %d\n", GetLastError());
26 return bSuccess;
27 }
28
30 {
32
34 {
35 BYTE Buffer[2048];
36 DWORD cbRead;
37
38 while ((bSuccess = ReadFile(hFile, Buffer, sizeof(Buffer), &cbRead, NULL)))
39 {
40 if (cbRead == 0)
41 break;
42
43 if (!CryptHashData(hHash, Buffer, cbRead, 0))
44 {
46 ConPrintf(StdOut, L"CertUtil: -hashfile command failed to hash: %d\n", GetLastError());
47 break;
48 }
49 }
50
51 if (bSuccess)
52 {
53 BYTE rgbHash[20];
54 DWORD cbHash, n;
55
56 if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
57 {
58 ConPrintf(StdOut, L"SHA1 hash of %s:\n", Filename);
59 for (n = 0; n < cbHash; ++n)
60 {
61 ConPrintf(StdOut, L"%02x", rgbHash[n]);
62 }
63 ConPuts(StdOut, L"\n");
64 }
65 else
66 {
67 ConPrintf(StdOut, L"CertUtil: -hashfile command failed to extract hash: %d\n", GetLastError());
69 }
70 }
71
73 }
74 else
75 {
76 ConPrintf(StdOut, L"CertUtil: -hashfile command no algorithm: %d\n", GetLastError());
77 }
78
80 }
81 else
82 {
83 ConPrintf(StdOut, L"CertUtil: -hashfile command no context: %d\n", GetLastError());
84 }
85
87 return bSuccess;
88}
89
90
91static void print_usage()
92{
93 ConPuts(StdOut, L"Verbs:\n");
94 ConPuts(StdOut, L" -hashfile -- Display cryptographic hash over a file\n");
95 ConPuts(StdOut, L"\n");
96 ConPuts(StdOut, L"CertUtil -? -- Display a list of all verbs\n");
97 ConPuts(StdOut, L"CertUtil -hashfile -? -- Display help text for the 'hashfile' verb\n");
98}
99
100int wmain(int argc, WCHAR *argv[])
101{
102 int n;
103
104 /* Initialize the Console Standard Streams */
106
107 if (argc == 1) /* i.e. no commandline arguments given */
108 {
109 print_usage();
110 return EXIT_SUCCESS;
111 }
112
113 for (n = 1; n < argc; ++n)
114 {
115 if (!_wcsicmp(argv[n], L"-?"))
116 {
117 print_usage();
118 return EXIT_SUCCESS;
119 }
120 else if (!_wcsicmp(argv[n], L"-hashfile"))
121 {
122 if (argc == 3)
123 {
124 if (!_wcsicmp(argv[n+1], L"-?"))
125 {
126 print_usage();
127 return EXIT_SUCCESS;
128 }
129 else
130 {
131 if (!hash_file(argv[n+1]))
132 {
133 /* hash_file prints the failure itself */
134 return EXIT_FAILURE;
135 }
136
137 ConPuts(StdOut, L"CertUtil: -hashfile command completed successfully\n");
138 return EXIT_SUCCESS;
139 }
140 }
141 else
142 {
143 ConPrintf(StdOut, L"CertUtil: -hashfile expected 1 argument, got %d\n", argc - 2);
144 return EXIT_FAILURE;
145 }
146 }
147 else
148 {
149 ConPrintf(StdOut, L"CertUtil: Unknown verb: %s\n", argv[n]);
150 return EXIT_FAILURE;
151 }
152 }
153
154 return EXIT_SUCCESS;
155}
static int argc
Definition: ServiceArgs.c:12
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
#define ConInitStdStreams()
Definition: fc.c:13
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
#define StdOut
Definition: fc.c:14
static BOOL hash_file(LPCWSTR Filename)
Definition: certutil.c:15
static void print_usage()
Definition: certutil.c:91
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
BOOL WINAPI CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, HCRYPTHASH *phHash)
Definition: crypt.c:740
BOOL WINAPI CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD *pdwDataLen, DWORD dwFlags)
Definition: crypt.c:1610
BOOL WINAPI CryptDestroyHash(HCRYPTHASH hHash)
Definition: crypt.c:890
BOOL WINAPI CryptReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
Definition: crypt.c:648
BOOL WINAPI CryptHashData(HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
Definition: crypt.c:1771
BOOL WINAPI CryptAcquireContextW(HCRYPTPROV *phProv, LPCWSTR pszContainer, LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
Definition: crypt.c:358
#define CloseHandle
Definition: compat.h:739
#define OPEN_EXISTING
Definition: compat.h:775
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GENERIC_READ
Definition: compat.h:135
#define CreateFileW
Definition: compat.h:741
#define FILE_SHARE_READ
Definition: compat.h:136
static BOOLEAN bSuccess
Definition: drive.cpp:477
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:940
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLdouble n
Definition: glext.h:7729
#define EXIT_FAILURE
Definition: jerror.c:33
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
static HCRYPTPROV hProv
Definition: rsaenh.c:32
#define argv
Definition: mplay32.c:18
_In_ HANDLE hFile
Definition: mswsock.h:90
#define L(x)
Definition: ntvdm.h:50
int wmain()
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define PROV_RSA_FULL
Definition: wincrypt.h:2039
#define CRYPT_VERIFYCONTEXT
Definition: wincrypt.h:2069
#define CALG_SHA1
Definition: wincrypt.h:1807
ULONG_PTR HCRYPTPROV
Definition: wincrypt.h:46
ULONG_PTR HCRYPTHASH
Definition: wincrypt.h:50
_In_ HCRYPTHASH hHash
Definition: wincrypt.h:4198
#define HP_HASHVAL
Definition: wincrypt.h:2183
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
unsigned char BYTE
Definition: xxhash.c:193