ReactOS  0.4.15-dev-3163-gf17c2c0
certutil.c File Reference
#include "precomp.h"
#include <wincrypt.h>
#include <stdlib.h>
Include dependency graph for certutil.c:

Go to the source code of this file.

Functions

static BOOL hash_file (LPCWSTR Filename)
 
static void print_usage ()
 
int wmain (int argc, WCHAR *argv[])
 

Function Documentation

◆ hash_file()

static BOOL hash_file ( LPCWSTR  Filename)
static

Definition at line 15 of file certutil.c.

16 {
19 
22 
24  {
25  ConPrintf(StdOut, L"CertUtil: -hashfile command failed: %d\n", GetLastError());
26  return bSuccess;
27  }
28 
30  {
32 
33  if (CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
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  {
45  bSuccess = FALSE;
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());
68  bSuccess = FALSE;
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 }
#define CloseHandle
Definition: compat.h:598
_In_ HCRYPTHASH hHash
Definition: wincrypt.h:4198
GLdouble n
Definition: glext.h:7729
#define INVALID_HANDLE_VALUE
Definition: compat.h:590
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
static BOOLEAN bSuccess
Definition: drive.cpp:432
IN PVCB IN PBCB OUT PDIRENT IN USHORT IN POEM_STRING Filename
Definition: fatprocs.h:934
#define FILE_SHARE_READ
Definition: compat.h:136
static HCRYPTPROV hProv
Definition: rsaenh.c:32
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
BOOL WINAPI CryptAcquireContextW(HCRYPTPROV *phProv, LPCWSTR pszContainer, LPCWSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
Definition: crypt.c:358
BOOL WINAPI CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData, DWORD *pdwDataLen, DWORD dwFlags)
Definition: crypt.c:1610
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI CryptReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
Definition: crypt.c:648
Definition: bufpool.h:45
#define OPEN_EXISTING
Definition: compat.h:634
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL WINAPI CryptDestroyHash(HCRYPTHASH hHash)
Definition: crypt.c:890
static const WCHAR L[]
Definition: oid.c:1250
BOOL WINAPI CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, HCRYPTHASH *phHash)
Definition: crypt.c:740
#define GENERIC_READ
Definition: compat.h:135
_In_ HANDLE hFile
Definition: mswsock.h:90
unsigned char BYTE
Definition: xxhash.c:193
#define CRYPT_VERIFYCONTEXT
Definition: wincrypt.h:2069
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
ULONG_PTR HCRYPTHASH
Definition: wincrypt.h:50
ULONG_PTR HCRYPTPROV
Definition: wincrypt.h:46
#define FILE_FLAG_SEQUENTIAL_SCAN
Definition: disk.h:43
BOOL WINAPI CryptHashData(HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
Definition: crypt.c:1771
#define CALG_SHA1
Definition: wincrypt.h:1807
#define ReadFile(a, b, c, d, e)
Definition: compat.h:601
#define NULL
Definition: types.h:112
#define CreateFileW
Definition: compat.h:600
#define StdOut
Definition: fc.c:14
#define HP_HASHVAL
Definition: wincrypt.h:2183
#define PROV_RSA_FULL
Definition: wincrypt.h:2039

Referenced by wmain().

◆ print_usage()

static void print_usage ( void  )
static

Definition at line 91 of file certutil.c.

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 }
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
static const WCHAR L[]
Definition: oid.c:1250
#define StdOut
Definition: fc.c:14

Referenced by wmain().

◆ wmain()

int wmain ( int  argc,
WCHAR argv[] 
)

Definition at line 100 of file certutil.c.

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
GLdouble n
Definition: glext.h:7729
#define argv
Definition: mplay32.c:18
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
#define EXIT_SUCCESS
Definition: rdjpgcom.c:55
static BOOL hash_file(LPCWSTR Filename)
Definition: certutil.c:15
#define EXIT_FAILURE
Definition: jerror.c:33
static const WCHAR L[]
Definition: oid.c:1250
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
#define StdOut
Definition: fc.c:14
#define ConInitStdStreams()
Definition: fc.c:13
static void print_usage()
Definition: certutil.c:91
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)