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

📄 csp11ins.c

📁 非常不错的CSP源码学习
💻 C
字号:
/** \file csp11ins.c * $Id: csp11ins.c,v 1.5 2004/06/24 14:39:01 rchantereau Exp $  * *  PKCS #11 Registerer -- PKCS #11 Cryptographic Windows® Registerer. *   *  Register CSP eleven to the Windows® registry. * * Copyright © 2004 Entr'ouvert * http://csp11.labs.libre-entreprise.org *  * \author  Romain Chantereau <rchantereau@entrouvert.com> * \date    2004 * \version 0.1 * * \ingroup csp11ins * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. *  * This program 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 General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *//** \defgroup csp11ins  CSP #11 Installer. */#undef UNICODE					// ## Not Yet#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include <windows.h>#include <wincrypt.h>#define PROV_NAME	"CSP Eleven"    /**< The provider Name.*/CHAR szProvider[] = "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider\\CSP Eleven"; /**< The CSP registry key.*/CHAR szType[] = "SOFTWARE\\Microsoft\\Cryptography\\Defaults\\Provider Types\\Type 900"; /**< The CSP type registry key. */CHAR szImagePath[] = "csp11.dll";   /**< The CSP dll path (relative to %systemroot%/system32..*/DWORD     	    dwDisposition;  /**< The registry key disposition (Existing or newly created. */HKEY      	    hKey;           /**< Handler to the opened or created key; */DWORD           ret;                /**< Returner value TRUE or FALSE.*/DWORD           dwValue;            /**< Provider Type.*/HANDLE          hSigFile;           /**< Handler to the signature file.*/DWORD     	    numBytesRead;   /**< Number of signature bytes read.*/LPVOID          lpvAddress;         /**< Newly allocated memory pointer.*/DWORD           fileSize;           /**< File size in bytes. */DWORD           fileSizeRead;       /**< Readed file size in bytes. *//** \brief Register the CSP11 DLL. */int __cdecl main(){    /** 1. Open the signature file.*/    if ((hSigFile = CreateFile("csp11.sig",                               GENERIC_READ, 0, NULL,			       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,			       0)) != INVALID_HANDLE_VALUE)    {        /** 2. Open the signature File.*/        if ((fileSize = GetFileSize((HANDLE) hSigFile, NULL)) ==                                    0xffffffff)        {            printf("Error getting size of the signature file: %x\n",                    GetLastError());            CloseHandle(hSigFile);            return(FALSE);        }        /** 3. Allocate memory to store signature.*/        if ((lpvAddress = VirtualAlloc(NULL, fileSize, MEM_RESERVE |		                                       MEM_COMMIT,                                       PAGE_READWRITE)) == NULL)        {            CloseHandle(hSigFile);            printf("Error allocation memory to read signature: %x\n",                    GetLastError());            return(FALSE);        }        /** 4. Read signature.*/        if (!ReadFile((HANDLE) hSigFile, lpvAddress, fileSize,		      &fileSizeRead, 0))        {            CloseHandle(hSigFile);            printf("Error reading signature: %x\n",                    GetLastError());            VirtualFree(lpvAddress, 0, MEM_RELEASE);            return(FALSE);        }        CloseHandle(hSigFile);        /** 5. Verify that signature file size and signature size match.*/        if (fileSizeRead != fileSize)        {            printf("Error: Signature size doesn't match file size\n");            return(FALSE);        }	/** 6.Create or open a key in local machine for CSP #11:*/        if ((ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE,                                  (const char *) szProvider,                                  0L, "", REG_OPTION_NON_VOLATILE,                                  KEY_ALL_ACCESS, NULL, &hKey,                                  &dwDisposition)) != ERROR_SUCCESS)        {            printf("Cannot open or create the CSP #11 registry key.\n");        }        /** 7. Register the "Image Path" for CSP 11 DLL.*/        if ((ret = RegSetValueEx(hKey, "Image Path", 0L, REG_SZ, szImagePath,	                         strlen(szImagePath)+1)) != ERROR_SUCCESS)        {            printf("Error setting registry Image Path.\n");            return(FALSE);        }        /** 8. Set CSP type to PROV_RSA_FULL (==1). */        dwValue = 900;        if ((ret = RegSetValueEx(hKey, "Type", 0L, REG_DWORD,                                 (LPTSTR) &dwValue,                                 sizeof(DWORD))) != ERROR_SUCCESS)        {            printf("Error setting registry Type: %x\n", ret);            return(FALSE);        }        /** 9. Store signature. */        if ((ret = RegSetValueEx(hKey, "Signature", 0L, REG_BINARY,                                  (LPTSTR) lpvAddress,                                 fileSize)) != ERROR_SUCCESS)        {            printf("Errot setting registry Signature: %x\n", ret);            return(FALSE);        }        RegCloseKey(hKey);        VirtualFree(lpvAddress, 0, MEM_RELEASE);	/** 10.Create or open a key in local machine for CSP of type 001*/        if ((ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE,                                  (const char *) szType,                                  0L, "", REG_OPTION_NON_VOLATILE,                                  KEY_ALL_ACCESS, NULL, &hKey,                                  &dwDisposition)) != ERROR_SUCCESS)        {            printf("Entry already exists: %x\n", ret);        }        /** 11. Setting default CSP Type.*/        if ((ret = RegSetValueEx(hKey, "Name", 0L, REG_SZ, PROV_NAME,                                 strlen(PROV_NAME)+1)) != ERROR_SUCCESS)        {            printf("Error setting Default type: %x\n", ret);            return(FALSE);        }	printf("The CSP eleven  %s is registered.\n", szImagePath);    }    else    {    	printf("Error \n");    }    return(FALSE);}

⌨️ 快捷键说明

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