?? autoreg.cpp
字號:
/****************************************************************************
* library : pkcs_csp.dll
* Purpose : It is an independent software module that actually performs
* cryptography algorithms for authentication, encoding, and encryption.
* This DLL is interfaced on any PKCS#11 module.
*
* Copyright (C) 2003 Ilex Syst鑝es Informatiques
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact :
* Ilex
* 51 boulevard Voltaire
* 92600 Asni鑢es-sur-Seine
* pkizy@ilex.fr
*
* Author: Delouvrier Antoine
*
*******************************************************************************/
/*
%----------------------------------------------------------------------------
% PROJECT : CSP_PKCS
%
% MODULE : autoreg
%
% VERSION : 1.00
%
% FILE : autoreg.cpp
%
% Allow to register the dll in the registry
%----------------------------------------------------------------------------
% Version 1.00
%
% CPX-31/03/2003-Cr閍tion
%----------------------------------------------------------------------------
% You can find wincrypt.h in the CSPDK which is downloadable at the adress :
% http://www.microsoft.com/downloads/details.aspx?FamilyId=0F436C75-2304-42BB-B81A-BA0C2C47BAC2&displaylang=en
*/
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#ifdef _AFXDLL
#include "stdafx.h"
#else
/*
% Biblioth鑡ues ANSI ou syst鑝e
%------------------------------
*/
#include <windows.h>
#endif
#ifndef WINVER
#define WINVER 0x0400
#endif
#include <wincrypt.h>
#include <tchar.h>
#include<stdio.h>
/*
% HEADER Files include
%-----------------------
*/
#include "cspdk.h"
static HMODULE
GetInstanceHandle(
void);
static const TCHAR l_szProviderName[]= TEXT("pkcs_csp");
static const DWORD
l_dwCspType= PROV_RSA_FULL;
/*
%--------------------------------------------------------------------------
% DllUnregisterServer
%
%---------------------------------------------------------------------------
*/
STDAPI
DllUnregisterServer(
void)
{
LONG nStatus;
DWORD dwDisp;
HRESULT hReturnStatus = NO_ERROR;
HKEY hProviders = NULL;
#ifdef _AFXDLL
AFX_MANAGE_STATE(AfxGetStaticModuleState());
#endif
//
// Delete the Registry key for this CSP.
//
nStatus = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider"),
0,
TEXT(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hProviders,
&dwDisp);
if (ERROR_SUCCESS == nStatus)
{
RegDeleteKey(hProviders, l_szProviderName);
RegCloseKey(hProviders);
hProviders = NULL;
}
return hReturnStatus;
}
/*
%--------------------------------------------------------------------------
% DllRegisterServer
%
%---------------------------------------------------------------------------
*/
STDAPI
DllRegisterServer(
void)
{
TCHAR szModulePath[MAX_PATH];
BYTE pbSignature[136]; // Room for a 1024 bit signature, with padding.
OSVERSIONINFO osVer;
LPTSTR szFileName, szFileExt;
HINSTANCE hThisDll;
HRSRC hSigResource;
DWORD dwStatus;
LONG nStatus;
BOOL fStatus;
DWORD dwDisp;
DWORD dwIndex;
DWORD dwSigLength;
HRESULT hReturnStatus = NO_ERROR;
HKEY hProviders = NULL;
HKEY hMyCsp = NULL;
HKEY hCalais = NULL;
HKEY hVendor = NULL;
BOOL fSignatureFound = FALSE;
HANDLE hSigFile = INVALID_HANDLE_VALUE;
#ifdef _AFXDLL
AFX_MANAGE_STATE(AfxGetStaticModuleState());
#endif
//
// Figure out the file name and path.
//
hThisDll = GetInstanceHandle();
if (NULL == hThisDll)
{
hReturnStatus = HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE);
goto ErrorExit;
}
dwStatus = GetModuleFileName(
hThisDll,
szModulePath,
sizeof(szModulePath) / sizeof(TCHAR));
if (0 == dwStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(GetLastError());
goto ErrorExit;
}
szFileName = _tcsrchr(szModulePath, TEXT('\\'));
if (NULL == szFileName)
szFileName = szModulePath;
else
szFileName += 1;
szFileExt = _tcsrchr(szFileName, TEXT('.'));
if (NULL == szFileExt)
{
hReturnStatus = HRESULT_FROM_WIN32(ERROR_INVALID_NAME);
goto ErrorExit;
}
else
szFileExt += 1;
//
// Create the Registry key for this CSP.
//
nStatus = RegCreateKeyEx(
HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider"),
0,
TEXT(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hProviders,
&dwDisp);
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
nStatus = RegCreateKeyEx(
hProviders,
l_szProviderName,
0,
TEXT(""),
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hMyCsp,
&dwDisp);
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
nStatus = RegCloseKey(hProviders);
hProviders = NULL;
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
//
// Install the trivial registry values.
//
nStatus = RegSetValueEx(
hMyCsp,
TEXT("Image Path"),
0,
REG_SZ,
(LPBYTE)szModulePath,
(_tcslen(szModulePath) + 1) * sizeof(TCHAR));
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
nStatus = RegSetValueEx(
hMyCsp,
TEXT("Type"),
0,
REG_DWORD,
(LPBYTE)&l_dwCspType,
sizeof(DWORD));
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
//
// See if we're self-signed. On NT5, CSP images can carry their own
// signatures.
//
hSigResource = FindResource(
hThisDll,
MAKEINTRESOURCE(CRYPT_SIG_RESOURCE_NUMBER),
RT_RCDATA);
//
// Install the file signature.
//
ZeroMemory(&osVer, sizeof(OSVERSIONINFO));
osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
fStatus = GetVersionEx(&osVer);
// ?BUGBUG? -- This works on Windows Millenium, too.
if (fStatus
&& (VER_PLATFORM_WIN32_NT == osVer.dwPlatformId)
&& (5 <= osVer.dwMajorVersion)
&& (NULL != hSigResource))
{
//
// Signature in file flag is sufficient.
//
dwStatus = 0;
nStatus = RegSetValueEx(
hMyCsp,
TEXT("SigInFile"),
0,
REG_DWORD,
(LPBYTE)&dwStatus,
sizeof(DWORD));
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
}
else
{
//
// We have to install a signature entry.
// Try various techniques until one works.
//
for (dwIndex = 0; !fSignatureFound; dwIndex += 1)
{
switch (dwIndex)
{
//
// Look for an external *.sig file and load that into the registry.
//
case 0:
_tcscpy(szFileExt, TEXT("sig"));
hSigFile = CreateFile(
szModulePath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE == hSigFile)
continue;
dwSigLength = GetFileSize(hSigFile, NULL);
if ((dwSigLength > sizeof(pbSignature))
|| (dwSigLength < 72)) // Accept a 512-bit signature
{
hReturnStatus = NTE_BAD_SIGNATURE;
goto ErrorExit;
}
fStatus = ReadFile(
hSigFile,
pbSignature,
sizeof(pbSignature),
&dwSigLength,
NULL);
if (!fStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(GetLastError());
goto ErrorExit;
}
fStatus = CloseHandle(hSigFile);
hSigFile = NULL;
if (!fStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(GetLastError());
goto ErrorExit;
}
fSignatureFound = TRUE;
break;
//
// Other cases may be added in the future.
//
default:
hReturnStatus = NTE_BAD_SIGNATURE;
goto ErrorExit;
}
if (fSignatureFound)
{
for (dwIndex = 0; dwIndex < dwSigLength; dwIndex += 1)
{
if (0 != pbSignature[dwIndex])
break;
}
if (dwIndex >= dwSigLength)
fSignatureFound = FALSE;
}
}
//
// We've found a signature somewhere! Install it.
//
nStatus = RegSetValueEx(
hMyCsp,
TEXT("Signature"),
0,
REG_BINARY,
pbSignature,
dwSigLength);
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
}
nStatus = RegCloseKey(hMyCsp);
hMyCsp = NULL;
if (ERROR_SUCCESS != nStatus)
{
hReturnStatus = HRESULT_FROM_WIN32(nStatus);
goto ErrorExit;
}
return hReturnStatus;
//
// An error was detected. Clean up any outstanding resources and
// return the error.
//
ErrorExit:
if (NULL != hVendor)
RegCloseKey(hVendor);
if (INVALID_HANDLE_VALUE != hSigFile)
CloseHandle(hSigFile);
if (NULL != hMyCsp)
RegCloseKey(hMyCsp);
if (NULL != hProviders)
RegCloseKey(hProviders);
DllUnregisterServer();
return hReturnStatus;
}
/*
%--------------------------------------------------------------------------
% GetInstanceHandle
%
%---------------------------------------------------------------------------
*/
extern "C" HINSTANCE g_hModule;
static HINSTANCE
GetInstanceHandle(
void)
{
#ifdef _AFXDLL
return AfxGetInstanceHandle();
#else
// ?vendor?
// Make sure this returns your DLL instance handle.
return g_hModule;
#endif
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -