1 | /* $Id: tstRTLdrVerifyPeImage.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SUP Testcase - Testing the Authenticode signature verification code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/test.h>
|
---|
32 | #include <iprt/mem.h>
|
---|
33 | #include <iprt/ldr.h>
|
---|
34 | #include <iprt/path.h>
|
---|
35 | #include <iprt/stream.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Global Variables *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | static int g_iDummy = 0;
|
---|
43 |
|
---|
44 | static DECLCALLBACK(int) TestCallback(RTLDRMOD hLdrMod, RTLDRSIGNATURETYPE enmSignature,
|
---|
45 | void const *pvSignature, size_t cbSignature,
|
---|
46 | PRTERRINFO pErrInfo, void *pvUser)
|
---|
47 | {
|
---|
48 | RT_NOREF_PV(hLdrMod); RT_NOREF_PV(enmSignature); RT_NOREF_PV(pvSignature); RT_NOREF_PV(cbSignature);
|
---|
49 | RT_NOREF_PV(pErrInfo); RT_NOREF_PV(pvUser);
|
---|
50 | return VINF_SUCCESS;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | int main(int argc, char **argv)
|
---|
56 | {
|
---|
57 | RTTEST hTest;
|
---|
58 | RTEXITCODE rcExit = RTTestInitAndCreate("tstAuthenticode", &hTest);
|
---|
59 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
60 | return rcExit;
|
---|
61 | RTTestBanner(hTest);
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Process input.
|
---|
65 | */
|
---|
66 | for (int i = 1; i < argc; i++)
|
---|
67 | {
|
---|
68 | const char *pszFullName = argv[i];
|
---|
69 | const char *pszFilename = RTPathFilename(pszFullName);
|
---|
70 | RTTestSub(hTest, pszFilename);
|
---|
71 |
|
---|
72 | RTLDRMOD hLdrMod;
|
---|
73 | int rc = RTLdrOpen(pszFullName, RTLDR_O_FOR_VALIDATION, RTLDRARCH_WHATEVER, &hLdrMod);
|
---|
74 | if (RT_SUCCESS(rc))
|
---|
75 | {
|
---|
76 | char szDigest[512];
|
---|
77 |
|
---|
78 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_MD5, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
79 | if (RT_SUCCESS(rc))
|
---|
80 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "md5=%s\n", szDigest);
|
---|
81 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA1, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
82 | if (RT_SUCCESS(rc))
|
---|
83 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha1=%s\n", szDigest);
|
---|
84 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA256, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
85 | if (RT_SUCCESS(rc))
|
---|
86 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha256=%s\n", szDigest);
|
---|
87 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA512, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha512=%s\n", szDigest);
|
---|
90 |
|
---|
91 | if (rc != VERR_NOT_SUPPORTED)
|
---|
92 | {
|
---|
93 | RTERRINFOSTATIC ErrInfo;
|
---|
94 | RTErrInfoInitStatic(&ErrInfo);
|
---|
95 | rc = RTLdrVerifySignature(hLdrMod, TestCallback, &g_iDummy, &ErrInfo.Core);
|
---|
96 | if (RT_FAILURE(rc))
|
---|
97 | RTTestFailed(hTest, "%s: %s (rc=%Rrc)", pszFilename, ErrInfo.Core.pszMsg, rc);
|
---|
98 | }
|
---|
99 | RTLdrClose(hLdrMod);
|
---|
100 | }
|
---|
101 | else
|
---|
102 | RTTestFailed(hTest, "Error opening '%s': %Rrc\n", pszFullName, rc);
|
---|
103 | }
|
---|
104 |
|
---|
105 | return RTTestSummaryAndDestroy(hTest);
|
---|
106 | }
|
---|
107 |
|
---|