CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 1st, 2009, 10:07 AM
    DeepT DeepT is offline
    Senior Member
     
    Join Date: Sep 2004
    Posts: 1,289
    DeepT  is a jewel in the rough (300+) DeepT  is a jewel in the rough (300+) DeepT  is a jewel in the rough (300+) DeepT  is a jewel in the rough (300+)
    How do I find the certificate of a signed file?

    I have some signed files. If you right-click on them you can see that they are signed and get some detailed information about the cert. However, I do not know how to do this programmaticlly.

    I have messed with the cert functions in the Crypto API, but those seem to deal with cert stores, not signed files.

    What I *really* want to verify is that the file I just downloaded was signed by us and that the chain of trust is valid (IE: It is not a forged / bogus cert).

    I have been doing some searching, but have come up empty on this.
    Reply With Quote
      #2    
    Old October 1st, 2009, 05:55 PM
    DeepT DeepT is offline
    Senior Member
     
    Join Date: Sep 2004
    Posts: 1,289
    DeepT  is a jewel in the rough (300+) DeepT  is a jewel in the rough (300+) DeepT  is a jewel in the rough (300+) DeepT  is a jewel in the rough (300+)
    Re: How do I find the certificate of a signed file?

    I have found out how to part way there:
    Code:
    // SignedFileCheck.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "SignedFileCheck.h"
    
    #include <Imagehlp.h>
    #pragma comment(lib, "Imagehlp.lib")
    
    #include <Wincrypt.h>
    #pragma comment(lib, "Crypt32.lib")
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    
    // The one and only application object
    
    CWinApp theApp;
    
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    	int nRetCode = 0;
    
    	// initialize MFC and print and error on failure
    	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    	{
    		// TODO: change error code to suit your needs
    		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
    		nRetCode = 1;
    	}
    	else
    	{
    		// TODO: code your application's behavior here.
    		CFile TheFile;
    		if ( TRUE == TheFile.Open("Test.exe", CFile::modeRead) )
    		{
    
    			DWORD CertIX[10]={0};
    			DWORD IXCount = sizeof(CertIX) / sizeof(DWORD);
    			DWORD CertCount = 0;
    
    			// Get the count of certs and the index array (typically just one cert )
    			if ( TRUE == ImageEnumerateCertificates( TheFile.m_hFile,
    				CERT_SECTION_TYPE_ANY,
    				&CertCount,
    				CertIX,
    				IXCount ) )
    			{
    				DWORD J;
    
    				for ( J = 0; J < CertCount; J++ )
    				{
    					WIN_CERTIFICATE *pCert = NULL;
    					DWORD BuffLen = 0;
    
    					// This should fail, but give us the buffer length we need.
    					if ( FALSE == ImageGetCertificateData( TheFile.m_hFile,
    						CertIX[J], NULL, &BuffLen ) )
    					{
    						if ( ERROR_INSUFFICIENT_BUFFER == GetLastError() )
    						{
    							pCert = ( WIN_CERTIFICATE *) malloc(BuffLen);
    
    							if ( NULL != pCert )
    							{
    								// Get the actual WIN_CERTIFICATE
    								if ( TRUE == ImageGetCertificateData( TheFile.m_hFile,
    									CertIX[J], pCert, &BuffLen ) )
    								{
    									// Convert this to a PCCERT_CONTEXT
    
    									// Or figure out some way to get the Subject names and verify the chain of trust.
    									
    									/* Does not work
    									PCCERT_CONTEXT pCertContext = CertCreateCertificateContext( X509_ASN_ENCODING,
    										pCert->bCertificate, pCert->dwLength);
    									if ( NULL != pCertContext )
    									{
    										CertFreeCertificateContext( pCertContext );
    									}
    									*/
    									int T = 3;
    
    								}
    
    								delete pCert;
    							}
    						}
    					}
    				}
    			}
    
    			TheFile.Close();
    		}
    
    
    	}
    
    	return nRetCode;
    }
    I can get the cert as a WIN_CERTIFICATE, but I do not know how to convert this to something like a PCCERT_CONTEXT. The reason I want that type is because all the cert APIs seem to deal with that kind of structure.

    Test.exe is a signed executable via a Verisign cert. I just want to verify it is OUR cert and that it has a valid chain of trust. For finding out that it is OUR cert, I thought at just looking at the subject like (which you can via the properties on the file from explorer). The subject has the name of our company in it.

    Anyway, if anyone knows about this stuff, any help you can give would greatly be appreciated.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:26 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.