SOL9 Sample: CertChainEngine

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * CertChainEngine.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/03/15
// 2009/03/27 Modified

#include <sol/crypt/CertChainEngine.h>
#include <sol/crypt/CertTrustError.h>
#include <sol/crypt/CertSystemStore.h>


void displayChainContents(PCCERT_CHAIN_CONTEXT pChainContext)
{
  _tprintf(_T("Chain Contedxt Size:%d\n"),pChainContext->cbSize);
  _tprintf(_T("Found Simple chains:%d.\n"),pChainContext->cChain);
  const TCHAR* msg = CertTrustError::getErrorMessage(pChainContext->TrustStatus.dwErrorStatus);
  _tprintf(_T("Chain ErrorStatus:%s\n"), msg);

  _tprintf(_T("InfoStatus:"));

  switch(pChainContext->TrustStatus.dwInfoStatus)
  {
  case 0:
    _tprintf(_T("No information status reported.\n"));
         break;
  case CERT_TRUST_HAS_EXACT_MATCH_ISSUER :
    _tprintf(_T("An exact match issuer certificate has been found for this certificate.\n"));
         break;
  case CERT_TRUST_HAS_KEY_MATCH_ISSUER: 
    _tprintf(_T("A key match issuer certificate has been found for this certificate.\n"));
    break;
  case CERT_TRUST_HAS_NAME_MATCH_ISSUER: 
    _tprintf(_T("A name match issuer certificate has been found for this certificate.\n"));
    break;
  case CERT_TRUST_IS_SELF_SIGNED:
    _tprintf(_T("This certificate is self-signed.\n"));
    break;
  case CERT_TRUST_IS_COMPLEX_CHAIN:
    _tprintf(_T("The certificate chain created is a complex chain.\n"));
    break;
  default:
    _tprintf(_T("\n"));
    break;
  } 
}


void _tmain(int argc, TCHAR** argv)
{
  try {
    CryptServiceProvider csp;

    CertSystemStore certStore(csp, _T("CA"));

    CertChainEngine chainEngine;

    // Loop through the certificates in the store, 
    PCCERT_CONTEXT  pCertContext = NULL;

    while((pCertContext = certStore.enumCertificate(pCertContext)) !=NULL) {

      TCHAR  name[MAX_PATH];

      if(CertGetNameString(   
        pCertContext,   
        CERT_NAME_SIMPLE_DISPLAY_TYPE,   
         0,
        NULL,   
           name,   
           SizeOf(name) )) {

           _tprintf(_T("\nFound a certificate for %s\n"), name);

        PCCERT_CHAIN_CONTEXT  pChainContext;

        if (chainEngine.getCertificateChain(pCertContext,&pChainContext) == NO_ERROR) {
          _tprintf(_T("OK, chainEngine.getCertificateChain() \n"));
          displayChainContents(pChainContext);
        } else {
          _tprintf(_T("Failed to chainEngine.getCertificateChain:\n"));
        }
      
        CertFreeCertificateChain(pChainContext);
        _tprintf(_T("---\n"));
      } else {
        _tprintf(_T("Failed in certStore.enumCertificate()\n"));
        break;
      }
    } 

  } catch (...) {
    _tprintf(_T("Exception: %x\n"), GetLastError());
  }
}


Last modified: 2 May 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.