⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crdspec_card.h

📁 基于SD卡的软实现CSP程序
💻 H
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#ifndef INCL_CRDSPEC_CARD_H
#define INCL_CRDSPEC_CARD_H

#include <windows.h>
#include <wincrypt.h>
#include "cspconst.h"
#include "csp.h"

#pragma once

class CContainer;

typedef BOOL (WINAPI *NEWCARD)(BOOL bSilent, LPBYTE * pbyRootPin, LPBYTE byRootLength, LPBYTE * pbyUserPin, LPBYTE byUserLength);
typedef BOOL (WINAPI *GETPIN)(BOOL bSilent, DWORD dwWhichUser, LPBYTE * pbyPINBuffer, BYTE * byRootLength);
//typedef BOOL (WINAPI *WAITFORCARDINSERTION)(LPWSTR pszCardName, BOOL bSilent, LPWSTR * pszReaderName);
typedef void (WINAPI *FREEDATA)(LPVOID pData);

#include "HandleTable.h"

class CCard
{
public:
	CCard();
	~CCard();
	bool Initialize(const WCHAR* const pszContainer, DWORD dwFlags);

	//Get the containers list
	CHandleTable* GetContainerList(){return &m_htContainers;}

	bool Lock();
	void Unlock();

	bool SetUserPin(BYTE* pbyUserPin, BYTE byLength);
	bool SignHash(CContainer* pContainer, ALG_ID algidKey, LPBYTE pbyHash, DWORD dwHashLen, LPBYTE pbySignature, LPDWORD pdwSigLen);

	bool DoDecrypt(IN WCHAR * pszKeyFile, IN BYTE * pbyData, IN DWORD wDataLen, OUT BYTE * pbyOutputData, OUT DWORD *pwOutputLen, int iKeyLen);
	bool DoCrypto(BYTE byCryptoType, IN WCHAR * pszKeyFile, IN BYTE * pbyInput, IN BYTE byInputLen, OUT BYTE * pbyOutput, OUT BYTE *pbyOutputLen);
	bool Authenticate();
	WCHAR* GetReaderName() {return m_pszReaderName;}
	bool CheckPIN(DWORD dwWhichKP,BYTE* pin,BYTE pinSize);
	CContainer* CreateContainer(LPWSTR pszName);
	CContainer* NextContainer(int &iCookie){return (CContainer*)GetContainerList()->GetNext(iCookie);}
	CContainer* GetContainer(LPWSTR pszName);
	bool StoreKey(CContainer* pContainer, ALG_ID algid, BYTE* pbyPrivateKey, DWORD dwPrivateKeySize, BYTE* pbyPublicKey, DWORD dwPublicKeySize);
	BYTE* GetEncodedData(LPBYTE pbyHash, DWORD dwHashLen,DWORD dwModulusLen);

	typedef struct _tagContainerInfo
	{
		BYTE byVersion;
		BYTE byNumKeys;
	} CONTINFO, *LPCONTINFO;

	typedef struct _tagKeyInfo
	{
		WORD wFileID;
		WORD wAlgID;
		WORD wOpID;
		WORD wKeyBits;
	} KEYINFO, *LPKEYINFO;

private:
	//Initialization functions for a blank card
	bool AddContainerFile(WCHAR* pszFileName);
	bool VerifyCardFiles();
	bool InitACLFiles();
	bool GenerateFile(const WCHAR* pszFileName, const WCHAR* pszAssocACL, BYTE* byData, const BYTE byLen);
	bool InitContCSPDirs();
	bool GenerateKP(DWORD dwWhichKP, LPBYTE pbyPIN, BYTE byLength);
	//bool GetKPUIDs(TUID& uidRoot, TUID& uidUser);

	//Used to init this class the first time we go to the card
	bool GenerateCardMutex();
	bool AttachToCard();
	void ReleaseCard();
	bool LoadGuiLib();
	void UnloadGuiLib();
	// Cached Name and PIN data
	BYTE * m_pbyPIN;
	BYTE m_byPINSize;

	//Reader mutex
	HANDLE m_hMutex;

	//Name of the read that the card is in
	WCHAR * m_pszReaderName;
	
	//Flags passed into the CSP
	DWORD m_dwFlags;

	//GUI hooks
	HMODULE m_hGUILib;
	GETPIN m_lpfnGetPin;
	//WAITFORCARDINSERTION m_lpfnWaitForCardInsertion;
	NEWCARD m_lpfnNewCard;
	FREEDATA m_lpfnFreeData;

	//A CHandleTable of CContainers.
	CHandleTable m_htContainers;

	//Reference count
	//to ensure we don't try to SCardConnect to a card we're
	//already connected to (see CCard::Lock()/CCard::Unlock())
	//Note:  this is signed for debug builds so we can assert
	//that the refcount never goes negative.
#ifndef DEBUG
	unsigned 
#endif
		long m_ulRefCount;

	//Handles to the card context and the card itself.
	/*SCARDCONTEXT m_hCardContext;
	SCARDHANDLE m_hCard;
	SCARDHANDLE m_hProxy;*/
	CSPFile fp;
};

#endif//INCL_CRDSPEC_CARD_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -