1 | /* $Id: tstRTLdrVerifyPeImage.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SUP Testcase - Testing the Authenticode signature verification code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/ldr.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/mem.h>
|
---|
45 | #include <iprt/path.h>
|
---|
46 | #include <iprt/stream.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 | #include <iprt/test.h>
|
---|
49 |
|
---|
50 | #include <iprt/md5.h>
|
---|
51 | #include <iprt/sha.h>
|
---|
52 |
|
---|
53 |
|
---|
54 | /*********************************************************************************************************************************
|
---|
55 | * Global Variables *
|
---|
56 | *********************************************************************************************************************************/
|
---|
57 | static int g_iDummy = 0;
|
---|
58 |
|
---|
59 |
|
---|
60 | static DECLCALLBACK(int) TestCallback(RTLDRMOD hLdrMod, PCRTLDRSIGNATUREINFO pInfo, PRTERRINFO pErrInfo, void *pvUser)
|
---|
61 | {
|
---|
62 | RT_NOREF(hLdrMod, pInfo, pErrInfo, pvUser);
|
---|
63 | return VINF_SUCCESS;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | int main(int argc, char **argv)
|
---|
68 | {
|
---|
69 | RTTEST hTest;
|
---|
70 | RTEXITCODE rcExit = RTTestInitAndCreate("tstAuthenticode", &hTest);
|
---|
71 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
72 | return rcExit;
|
---|
73 | RTTestBanner(hTest);
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * Process input.
|
---|
77 | */
|
---|
78 | for (int i = 1; i < argc; i++)
|
---|
79 | {
|
---|
80 | const char *pszFullName = argv[i];
|
---|
81 | const char *pszFilename = RTPathFilename(pszFullName);
|
---|
82 | RTTestSub(hTest, pszFilename);
|
---|
83 |
|
---|
84 | RTLDRMOD hLdrMod;
|
---|
85 | int rc = RTLdrOpen(pszFullName, RTLDR_O_FOR_VALIDATION, RTLDRARCH_WHATEVER, &hLdrMod);
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | uint8_t abHash[128];
|
---|
89 | char szDigest[512];
|
---|
90 |
|
---|
91 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_MD5, abHash, sizeof(abHash)), VINF_SUCCESS);
|
---|
92 | if (RT_SUCCESS(rc))
|
---|
93 | {
|
---|
94 | RTMd5ToString(abHash, szDigest, sizeof(szDigest));
|
---|
95 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "md5=%s\n", szDigest);
|
---|
96 | }
|
---|
97 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA1, abHash, sizeof(abHash)), VINF_SUCCESS);
|
---|
98 | if (RT_SUCCESS(rc))
|
---|
99 | {
|
---|
100 | RTSha1ToString(abHash, szDigest, sizeof(szDigest));
|
---|
101 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha1=%s\n", szDigest);
|
---|
102 | }
|
---|
103 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA256, abHash, sizeof(abHash)), VINF_SUCCESS);
|
---|
104 | if (RT_SUCCESS(rc))
|
---|
105 | {
|
---|
106 | RTSha256ToString(abHash, szDigest, sizeof(szDigest));
|
---|
107 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha256=%s\n", szDigest);
|
---|
108 | }
|
---|
109 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA512, abHash, sizeof(abHash)), VINF_SUCCESS);
|
---|
110 | if (RT_SUCCESS(rc))
|
---|
111 | {
|
---|
112 | RTSha512ToString(abHash, szDigest, sizeof(szDigest));
|
---|
113 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha512=%s\n", szDigest);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (rc != VERR_NOT_SUPPORTED)
|
---|
117 | {
|
---|
118 | RTERRINFOSTATIC ErrInfo;
|
---|
119 | RTErrInfoInitStatic(&ErrInfo);
|
---|
120 | rc = RTLdrVerifySignature(hLdrMod, TestCallback, &g_iDummy, &ErrInfo.Core);
|
---|
121 | if (RT_FAILURE(rc))
|
---|
122 | RTTestFailed(hTest, "%s: %s (rc=%Rrc)", pszFilename, ErrInfo.Core.pszMsg, rc);
|
---|
123 | }
|
---|
124 | RTLdrClose(hLdrMod);
|
---|
125 | }
|
---|
126 | else
|
---|
127 | RTTestFailed(hTest, "Error opening '%s': %Rrc\n", pszFullName, rc);
|
---|
128 | }
|
---|
129 |
|
---|
130 | return RTTestSummaryAndDestroy(hTest);
|
---|
131 | }
|
---|
132 |
|
---|