Reactos
1/*
2 * PROJECT: ReactOS certutil
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: CertUtil commandline handling
5 * COPYRIGHT: Copyright 2020 Mark Jansen (mark.jansen@reactos.org)
6 *
7 * Note: Only -hashfile and -asn are implemented for now, the rest is not present!
8 */
9
10#include "precomp.h"
11#include <wincrypt.h>
12#include <stdlib.h>
13
14typedef struct
15{
16 LPCWSTR Name;
17 BOOL (*pfn)(LPCWSTR Filename);
18} Verb;
19
20
21Verb verbs[] = {
22 { L"hashfile", hash_file },
23 { L"asn", asn_dump },
24};
25
26static void print_usage()
27{
28 ConPuts(StdOut, L"Verbs:\n");
29 ConPuts(StdOut, L" -hashfile -- Display cryptographic hash over a file\n");
30 ConPuts(StdOut, L" -asn -- Display ASN.1 encoding of a file\n");
31 ConPuts(StdOut, L"\n");
32 ConPuts(StdOut, L"CertUtil -? -- Display a list of all verbs\n");
33 ConPuts(StdOut, L"CertUtil -hashfile -? -- Display help text for the 'hashfile' verb\n");
34}
35
36
37Verb* MatchVerb(LPCWSTR arg)
38{
39 if (arg[0] != '-' && arg[0] != '/')
40 return NULL;
41
42 for (size_t n = 0; n < RTL_NUMBER_OF(verbs); ++n)
43 {
44 if (!_wcsicmp(verbs[n].Name, arg + 1))
45 {
46 return verbs + n;
47 }
48 }
49
50 return NULL;
51}
52
53int wmain(int argc, WCHAR *argv[])
54{
55 int n;
56
57 /* Initialize the Console Standard Streams */
58 ConInitStdStreams();
59
60 if (argc == 1) /* i.e. no commandline arguments given */
61 {
62 print_usage();
63 return EXIT_SUCCESS;
64 }
65
66 for (n = 1; n < argc; ++n)
67 {
68 if (!_wcsicmp(argv[n], L"-?"))
69 {
70 print_usage();
71 return EXIT_SUCCESS;
72 }
73
74 Verb* verb = MatchVerb(argv[n]);
75
76 if (verb)
77 {
78 if (argc != 3)
79 {
80 ConPrintf(StdOut, L"CertUtil: -%s expected 1 argument, got %d\n", verb->Name, argc - 2);
81 return EXIT_FAILURE;
82 }
83
84 if (!_wcsicmp(argv[n+1], L"-?"))
85 {
86 print_usage();
87 return EXIT_SUCCESS;
88 }
89
90 if (!verb->pfn(argv[n+1]))
91 {
92 /* The verb prints the failure */
93 return EXIT_FAILURE;
94 }
95
96 ConPrintf(StdOut, L"CertUtil: -%s command completed successfully\n", verb->Name);
97 return EXIT_SUCCESS;
98 }
99 else
100 {
101 ConPrintf(StdOut, L"CertUtil: Unknown verb: %s\n", argv[n]);
102 return EXIT_FAILURE;
103 }
104 }
105
106 return EXIT_SUCCESS;
107}