CertInstallDlg.cpp
资源名称:pkcs11.rar [点击查看]
上传用户:filter2008
上传日期:2013-02-01
资源大小:101k
文件大小:13k
源码类别:
CA认证
开发平台:
C/C++
- // CertInstallDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Certificat.h"
- #include "CertInstall.h"
- #include "CertInstallDlg.h"
- #include "cryptool.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define WM_USER_INIT_PKCS WM_USER + 1
- CList<CCertificat, CCertificat&> m_certList;
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCertInstallDlg dialog
- CCertInstallDlg::CCertInstallDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CCertInstallDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CCertInstallDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
- }
- void CCertInstallDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CCertInstallDlg)
- DDX_Control(pDX, IDOK, m_ctrInstall);
- DDX_Control(pDX, IDC_CERT_LIST, m_ctrCertList);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CCertInstallDlg, CDialog)
- //{{AFX_MSG_MAP(CCertInstallDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_MESSAGE(WM_USER_INIT_PKCS, OnInitializePkcs)
- ON_BN_CLICKED(IDOK, OnInstall)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CCertInstallDlg message handlers
- BOOL CCertInstallDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- CImageList *ImageFileList;
- ImageFileList = new CImageList();
- /* add list icons */
- ImageFileList->Create(16,16,ILC_COLOR24|ILC_MASK,0,0);
- ImageFileList->Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
- ImageFileList->Add(AfxGetApp()->LoadIcon(IDI_CERT));
- m_ctrCertList.SetImageList(ImageFileList, LVSIL_SMALL);
- PostMessage(WM_USER_INIT_PKCS);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CCertInstallDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CCertInstallDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CCertInstallDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- char CCertInstallDlg::GetChar(char val)
- {
- int valMod=val&0x0F;
- char temp;
- if(valMod<=9)
- temp='0'+valMod;
- else
- temp='A'+(valMod-0xA);
- return temp;
- };
- void CCertInstallDlg::OnInitializePkcs(WPARAM wParam,LPARAM lParam)
- {
- CK_RV rv=0;
- CK_ULONG slotIndex=0;
- CK_FUNCTION_LIST_PTR pFunctionList=NULL;
- CK_SLOT_ID_PTR slotList;
- CK_ULONG slotListSize=0;
- m_certList.RemoveAll();
- /* initialisation de la dll PKCS#11*/
- setTraceLevel();
- // Load the PKCS11 library
- rv= init(&pFunctionList);
- if (rv!=CKR_OK){
- AfxMessageBox("Impossible d'initialiser le module pkcs11",MB_OK);
- return;
- }
- /* On r閏up鑢e tous les slots diponibles */
- rv = getSlotList(pFunctionList, NULL_PTR, &slotListSize);
- if (rv!=CKR_OK){
- AfxMessageBox("Erreur lors de la r閏up閞ation des slots(1) du module pkcs11",MB_OK);
- return;
- }
- slotList = (CK_SLOT_ID_PTR)malloc(slotListSize*sizeof(CK_SLOT_ID));
- rv = getSlotList(pFunctionList, slotList, &slotListSize);
- if (rv!=CKR_OK){
- AfxMessageBox("Erreur lors de la r閏up閞ation des slots(2) du module pkcs11",MB_OK);
- return;
- }
- CK_SLOT_INFO slotInfo;
- CK_SESSION_HANDLE hSession;
- CK_ULONG certListSize = 256;
- CK_OBJECT_HANDLE_PTR phCertList;
- CK_BBOOL flag=FALSE;
- CK_BYTE_PTR curSubject=NULL;
- CK_ULONG curSubjectLen=512;
- CK_ULONG keyIdLen=256;
- CK_BYTE_PTR keyId = NULL;
- for(unsigned int i=0;i<slotListSize;i++)
- {
- flag=FALSE;
- rv =(pFunctionList->C_GetSlotInfo)(slotList[i], &slotInfo);
- if ((rv == CKR_OK)&&((slotInfo.flags & CKF_TOKEN_PRESENT) != 0))
- {
- // On ouvre une session pour ce token
- rv=openSession(pFunctionList,slotList[i] ,&hSession);
- if (rv==CKR_OK)
- {
- certListSize=256;
- // on r閏up鑢e tous les certificats
- phCertList = (CK_OBJECT_HANDLE_PTR) malloc(certListSize*sizeof(CK_OBJECT_HANDLE));
- rv = getAllX509CertificateList(pFunctionList, hSession, phCertList, &certListSize);
- if(rv!=CKR_OK)
- {
- free(phCertList);
- continue;
- }
- //on les parcours pour les ajouter