summaryrefslogtreecommitdiffstats
path: root/private/sdktools/imagehlp/certify.cxx
diff options
context:
space:
mode:
authorAdam <you@example.com>2020-05-17 05:51:50 +0200
committerAdam <you@example.com>2020-05-17 05:51:50 +0200
commite611b132f9b8abe35b362e5870b74bce94a1e58e (patch)
treea5781d2ec0e085eeca33cf350cf878f2efea6fe5 /private/sdktools/imagehlp/certify.cxx
downloadNT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar
NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.gz
NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.bz2
NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.lz
NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.xz
NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.tar.zst
NT4.0-e611b132f9b8abe35b362e5870b74bce94a1e58e.zip
Diffstat (limited to 'private/sdktools/imagehlp/certify.cxx')
-rw-r--r--private/sdktools/imagehlp/certify.cxx306
1 files changed, 306 insertions, 0 deletions
diff --git a/private/sdktools/imagehlp/certify.cxx b/private/sdktools/imagehlp/certify.cxx
new file mode 100644
index 000000000..1260ed275
--- /dev/null
+++ b/private/sdktools/imagehlp/certify.cxx
@@ -0,0 +1,306 @@
+/*++
+
+Copyright (c) 1995-96 Microsoft Corporation
+
+Module Name:
+
+ certify.cxx
+
+Abstract:
+
+ This is the command line tool to manipulate certificates on an executable image.
+
+Author:
+
+Revision History:
+
+--*/
+
+#define UNICODE 1
+#define _UNICODE 1
+
+#include <private.h>
+
+#if 1
+#define TEST 1
+#else
+#define TEST 0
+#endif
+
+void
+PrintUsage(
+ VOID)
+{
+ fputs("usage: CERTIFY [switches] image-names... \n"
+ " [-?] display this message\n"
+ " [-l] list the certificates in an image\n"
+ " [-a:<Filename>] add a certificate file to an image\n"
+ " [-r:<index>] remove certificate <index> from an image\n"
+ " [-g:<Filename>] update any associated .DBG file\n"
+ " [-s:<Filename>] used with -r to save the removed certificate\n",
+ stderr
+ );
+ exit(-1);
+}
+
+#if TEST
+// Test routine
+BOOL fAllDataReturned;
+PVOID pvDataRefTest;
+DWORD FileSize;
+DWORD DataRead;
+
+BOOL
+WINAPI
+DigestRoutine (
+ DIGEST_HANDLE DataReference,
+ PBYTE pData,
+ DWORD dwLength
+ )
+{
+ if (DataReference != pvDataRefTest) {
+ return(FALSE);
+ }
+
+ // Attempt to read the range
+
+ if (IsBadReadPtr(pData, dwLength)) {
+ return(FALSE);
+ }
+
+ DataRead += dwLength;
+ if (DataRead > FileSize) {
+ return(FALSE);
+ }
+
+ return(TRUE);
+}
+
+#endif
+
+
+int _CRTAPI1
+main(
+ int argc,
+ char *argv[],
+ char *envp[]
+ )
+{
+ char c, *p;
+
+ if (argc < 2) {
+ PrintUsage();
+ }
+
+ while (--argc) {
+ p = *++argv;
+ if (*p == '/' || *p == '-') {
+ while (c = *++p)
+ switch (toupper( c )) {
+ case '?':
+ PrintUsage();
+ break;
+
+ case 'A':
+ c = *++p;
+ if (c != ':') {
+ PrintUsage();
+ } else {
+
+ // Add a certificate file to an image.
+#if TEST
+ // Test code
+
+ WIN_CERTIFICATE wc;
+ HANDLE Handle;
+ DWORD Index;
+
+ wc.dwLength = sizeof(WIN_CERTIFICATE);
+ wc.wCertificateType = WIN_CERT_TYPE_X509;
+
+ if ((Handle = CreateFile(TEXT("test.exe"),
+ GENERIC_WRITE | GENERIC_READ,
+ 0,
+ 0,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL)) == INVALID_HANDLE_VALUE)
+ {
+ fputs("Unable to open test.exe", stderr);
+ exit(1);
+ }
+
+ printf("ImageAddCertificate on test.exe returned: %d\n",
+ ImageAddCertificate(Handle, &wc, &Index));
+
+ printf("Index #: %d\n", Index);
+
+ CloseHandle(Handle);
+
+ exit(0);
+#else
+ // The real code
+#endif
+ }
+ break;
+
+ case 'L':
+ // List the certificates in an image.
+#if TEST
+ // Test code
+ WIN_CERTIFICATE wc;
+ HANDLE Handle;
+ DWORD Index;
+
+ if ((Handle = CreateFile(TEXT("test.exe"),
+ GENERIC_READ,
+ 0,
+ 0,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL)) == INVALID_HANDLE_VALUE)
+ {
+ fputs("Unable to open test.exe", stderr);
+ exit(1);
+ }
+
+ ImageEnumerateCertificates(Handle, CERT_SECTION_TYPE_ANY, &Index, NULL, 0);
+
+ printf("Enumerate lists: %d\n", Index);
+
+ Index--;
+
+ while (ImageGetCertificateHeader(Handle, Index, &wc)) {
+ printf("Index: %d\n", Index);
+ Index--;
+ }
+
+ CloseHandle(Handle);
+
+ exit(0);
+
+#else
+ // The real code
+#endif
+ break;
+
+ case 'R':
+ c = *++p;
+ if (c != ':') {
+ PrintUsage();
+ } else {
+ // Remove a specific certificate from an image.
+#if TEST
+ // Test code
+
+ HANDLE Handle;
+
+ if ((Handle = CreateFile(TEXT("test.exe"),
+ GENERIC_WRITE | GENERIC_READ,
+ 0,
+ 0,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL)) == INVALID_HANDLE_VALUE)
+ {
+ fputs("Unable to open test.exe", stderr);
+ exit(1);
+ }
+
+ printf("ImageRemoveCertificate(0) on test.exe returned: %d\n",
+ ImageRemoveCertificate(Handle, 0));
+ exit(0);
+#else
+ // The real code
+#endif
+ }
+ break;
+
+ case 'G':
+ c = *++p;
+ if (c != ':') {
+ PrintUsage();
+ } else {
+ // Generate a certificate from an image.
+#if TEST
+ // Test code
+
+ HANDLE Handle;
+
+ if ((Handle = CreateFile(TEXT("test.exe"),
+ GENERIC_READ,
+ 0,
+ 0,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL)) == INVALID_HANDLE_VALUE)
+ {
+ fputs("Unable to open test.exe", stderr);
+ exit(1);
+ }
+
+ FileSize = GetFileSize(Handle, NULL);
+ DataRead = 0;
+
+ pvDataRefTest = (PVOID) 1;
+ printf("ImageGetDigestStream debug w/o resources on test.exe returned: %s\tGetLastError(): %d\n",
+ ImageGetDigestStream(Handle,
+ CERT_PE_IMAGE_DIGEST_DEBUG_INFO,
+ DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
+ GetLastError());
+ printf("Message Stream Size: %d\n", DataRead);
+
+ DataRead = 0;
+ pvDataRefTest = (PVOID) 2;
+ printf("ImageGetDigestStream debug w/ resources test.exe returned: %s\tGetLastError(): %d\n",
+ ImageGetDigestStream(Handle,
+ CERT_PE_IMAGE_DIGEST_DEBUG_INFO | CERT_PE_IMAGE_DIGEST_RESOURCES,
+ DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
+ GetLastError());
+ printf("Message Stream Size: %d\n", DataRead);
+
+ DataRead = 0;
+ pvDataRefTest = (PVOID) 3;
+ printf("ImageGetDigestStream w/o debug w/o resources on test.exe returned: %s\tGetLastError(): %d\n",
+ ImageGetDigestStream(Handle,
+ 0,
+ DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
+ GetLastError());
+ printf("Message Stream Size: %d\n", DataRead);
+
+ DataRead = 0;
+ pvDataRefTest = (PVOID) 4;
+ printf("ImageGetDigestStream w/o debug w/ resources test.exe returned: %s\tGetLastError(): %d\n",
+ ImageGetDigestStream(Handle,
+ CERT_PE_IMAGE_DIGEST_RESOURCES,
+ DigestRoutine, pvDataRefTest) ? "TRUE" : "FALSE",
+ GetLastError());
+ printf("Message Stream Size: %d\n", DataRead);
+
+ exit(0);
+
+#else
+ // Real code
+#endif
+ }
+ break;
+
+ case 'S':
+ c = *++p;
+ if (c != ':') {
+ PrintUsage();
+ } else {
+ // Save the certificate in some file.
+ }
+ break;
+
+ default:
+ fprintf( stderr, "CERTIFY: Invalid switch - /%c\n", c );
+ PrintUsage();
+ break;
+ }
+ }
+ }
+
+ return 0;
+}