SOL9 Sample: PFXExportImport

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2009/04/01

#include <sol/crypt/CryptServiceProvider.h>
#include <sol/crypt/CertSystemStore.h>
#include <sol/crypt/CertContext.h>
#include <sol/crypt/CertSelectionDialog.h>

void _tmain(int argc, TCHAR** argv)
{
  if (argc != 2) {
    _tprintf(_T("Usage:\n%s outputfile.pfx\n"), argv[0]);
    return;
  }
  try {
    const wchar_t* password = L"SOL9Password";

    _tprintf(_T("1 Create a cryptServiceProvider\n"));

        
    CryptServiceProvider csp;

    _tprintf(_T("2 Open CA systemstore\n"));
    CertSystemStore ca(csp, _T("CA"));


    CertSelectionDialog certSelDlg(ca);
    _tprintf(_T("3 Show a certSelectionDialog for CA\n"));
  
    PCCERT_CONTEXT pContext = certSelDlg.show();

    if (pContext) {
      _tprintf(_T("4 OK, selected a certificate\n"));
      CertContext cert(pContext);

      TCHAR name[MAX_PATH];

      cert.getName(name, SizeOf(name)); //2012/06/26
      _tprintf(_T("5 Certificate Name:\n\t%s\n"), name);

      _tprintf(_T("6 Try to ca.exportPFX() to %s\n"), argv[1]);
      if (ca.exportPFX(pContext, argv[1], password) == NO_ERROR) {
        _tprintf(_T("7 OK, ca.exportPFX() to %s\n"), argv[1]);


        CertSystemStore my(csp, _T("MY"));
        _tprintf(_T("8 Try to my.importPFX() from %s\n"), argv[1]);
        
        if (my.importPFX(argv[1], password) == NO_ERROR) {
          _tprintf(_T("9 OK, my.importPFX() from %s\n"), argv[1]);
        } else {
          _tprintf(_T("10 Failed in my.importPFX() from %s\n"), argv[1]);
        }
      } else {
        _tprintf(_T("11 Failed in ca.exportPFX() to %s\n"), argv[1]);
      }
    } else {
      _tprintf(_T("12 Failed to select a certificate\n"));
    }
  } catch (...) {

    printf("13 Exception %x\n", GetLastError());
  }
}



Last modified: 2 May 2016

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