1 | /* $Id: tstRTLdrVerifyPeImage.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * SUP Testcase - Testing the Authenticode signature verification code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2015 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 | return VINF_SUCCESS;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 | int main(int argc, char **argv)
|
---|
54 | {
|
---|
55 | RTTEST hTest;
|
---|
56 | RTEXITCODE rcExit = RTTestInitAndCreate("tstAuthenticode", &hTest);
|
---|
57 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
58 | return rcExit;
|
---|
59 | RTTestBanner(hTest);
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Process input.
|
---|
63 | */
|
---|
64 | for (int i = 1; i < argc; i++)
|
---|
65 | {
|
---|
66 | const char *pszFullName = argv[i];
|
---|
67 | const char *pszFilename = RTPathFilename(pszFullName);
|
---|
68 | RTTestSub(hTest, pszFilename);
|
---|
69 |
|
---|
70 | RTLDRMOD hLdrMod;
|
---|
71 | int rc = RTLdrOpen(pszFullName, RTLDR_O_FOR_VALIDATION, RTLDRARCH_WHATEVER, &hLdrMod);
|
---|
72 | if (RT_SUCCESS(rc))
|
---|
73 | {
|
---|
74 | char szDigest[512];
|
---|
75 |
|
---|
76 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_MD5, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
77 | if (RT_SUCCESS(rc))
|
---|
78 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "md5=%s\n", szDigest);
|
---|
79 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA1, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
80 | if (RT_SUCCESS(rc))
|
---|
81 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha1=%s\n", szDigest);
|
---|
82 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA256, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha256=%s\n", szDigest);
|
---|
85 | RTTESTI_CHECK_RC(rc = RTLdrHashImage(hLdrMod, RTDIGESTTYPE_SHA512, szDigest, sizeof(szDigest)), VINF_SUCCESS);
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "sha512=%s\n", szDigest);
|
---|
88 |
|
---|
89 | if (rc != VERR_NOT_SUPPORTED)
|
---|
90 | {
|
---|
91 | RTERRINFOSTATIC ErrInfo;
|
---|
92 | RTErrInfoInitStatic(&ErrInfo);
|
---|
93 | rc = RTLdrVerifySignature(hLdrMod, TestCallback, &g_iDummy, &ErrInfo.Core);
|
---|
94 | if (RT_FAILURE(rc))
|
---|
95 | RTTestFailed(hTest, "%s: %s (rc=%Rrc)", pszFilename, ErrInfo.Core.pszMsg, rc);
|
---|
96 | }
|
---|
97 | RTLdrClose(hLdrMod);
|
---|
98 | }
|
---|
99 | else
|
---|
100 | RTTestFailed(hTest, "Error opening '%s': %Rrc\n", pszFullName, rc);
|
---|
101 | }
|
---|
102 |
|
---|
103 | return RTTestSummaryAndDestroy(hTest);
|
---|
104 | }
|
---|
105 |
|
---|