📄 csptests.c
字号:
/** \file cspTests.c * $Id: cspTests.c,v 1.20 2004/12/09 13:19:54 rchantereau Exp $ * * CSP #11 -- Cryptographic Service Provider PKCS #11 tests suite.. * * This file contains CSP-eleven tests source code. * * Copyright © 2004 Entr'ouvert * http://csp11.labs.libre-entreprise.org * * \author Romain Chantereau <rchantereau@entrouvert.com> * \date 2004 * \version 0.1 * * \ingroup cspTests * 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 cspTests CSP #11 Tests suite. * */#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <wincrypt.h>#ifdef _MSC_VER#include <cspdk.h>#include "../missdef.h"#else#include "misscrypt.h"#endif#define ENTRYPOINT APIENTRY /**< define the WinMain type.*/#define MAXLINES 500 /**< Display max 500 lines.*//** \brief Public key BLOB * * This is the representation of the MS C base provider public key BLOB. * \ingroup SPInternal */typedef struct _RSAPUBLICKEY_BLOB { PUBLICKEYSTRUC publicKeyStruc; /**< The blob header.*/ RSAPUBKEY rsaPubKey; /**< The rsa public key description.*/ BYTE *modulus; /**< The key modulus.*/} RSAPUBLICKEY_BLOB;static HCRYPTPROV hProv; /**< The handler to the CSP.*/static HCRYPTKEY hUserSigKey; /**< The handler to the user signature key set.*/static HCRYPTKEY hUserKxKey; /**< The handler to the user key exchange key set.*/static HCRYPTHASH hHash; /**< Handle to the test hash.*/static HWND hMainWnd; /**< Handle the main window.*/static HWND hWndVScroll; /**< Handle the vertical scroll control.*/static HWND hWndHScroll; /**< Handle the horizontal scroll control.*/static char *testsMessages[MAXLINES]; /**< Messages produced by tests.*/static DWORD currentLineNumber; /* Current line number.*/char line[255];int returnValue;HANDLE heapHandle; /**< handle to the test heap.*//** \brief Main window procedure. * */LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );/** \brief Update tests messages string. * * \param newChars The new chars to add to the message string. * \return TRUE if ok. */int updateMessages(LPCSTR newLine){ char *localString=NULL; /* Local copy of the new line.*/ int i;/* iterator.*/ /** If newLine is NULL, get the global line pointer.*/ if(newLine == NULL) { newLine = line; } /** - Allocate memory for the new line.*/ localString = HeapAlloc(heapHandle, HEAP_ZERO_MEMORY, (sizeof(char) * strlen(newLine))+1); if(localString == NULL) { exit(1); } /** - Copy string in local.*/ strcpy(localString, newLine); /** - Increase current line number.*/ currentLineNumber++; /** - If currentLineNumber >= MAXLINES.*/ if(currentLineNumber >= MAXLINES) { /** - Free the first line.*/ HeapFree(heapHandle, 0, testsMessages[0]); /** - Copy each pointer backward.*/ for(i=1; i<MAXLINES; i++) { testsMessages[i-1] = testsMessages[i]; } /** - Set current line number to MAXLINES.*/ currentLineNumber = MAXLINES; } /** - Copy pointer to his position.*/ testsMessages[currentLineNumber-1] = localString; return TRUE;}/** \brief Display the running test and results. * */int runTest(HWND hWnd,const char *message, int (*function)(void), HRESULT goodError){ int i; HRESULT errorCode = 0; if((*function)()) { sprintf(line, "%s: Ok.",message); updateMessages(NULL); InvalidateRect(hWnd,NULL,TRUE); return 1; } else { errorCode = GetLastError(); if(goodError) { if (errorCode == goodError) { sprintf(line, "%s: Ok failed as expected.",message); updateMessages(NULL); InvalidateRect(hWnd,NULL,TRUE); return 1; } } sprintf(line, "%s: Failed; Error code: 0x%x(%u)", message, errorCode, errorCode); updateMessages(NULL); InvalidateRect(hWnd,NULL,TRUE); returnValue = 1; return 0; }}/** \brief Test the acquire context CSP method. * * Test a default call, without any flag, without any szContainer. */int testAcquireContext(){ return !RCRYPT_FAILED(CryptAcquireContext(&hProv, NULL, "CSP Eleven", 900, 0));}/** \brief Set the crypto SP window handle. * */int setWindowHandle(){ return !RCRYPT_FAILED(CryptSetProvParam(0, PP_CLIENT_HWND, (DWORD) &hMainWnd, 0));}/** \brief Acquire a new context with a new key set. * * Call CryptAcquireContext with the CRYPT_NEWKEYSET flag. */int testAcquireCtxNewKeySet(){ return !RCRYPT_FAILED(CryptAcquireContext(&hProv, NULL, "CSP Eleven", 900, CRYPT_NEWKEYSET));}int testCreateSHA1Hash(){ return !RCRYPT_FAILED(CryptCreateHash(hProv, CALG_SHA, 0, 0, &hHash));}int testCreateMD5Hash(){ return !RCRYPT_FAILED(CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash));}int testCreateSHAMD5Hash(){ return !RCRYPT_FAILED(CryptCreateHash(hProv, CALG_SSL3_SHAMD5, 0, 0, &hHash));}int testFeedHash(){ sprintf(line,"Feed: 1234567890"); updateMessages(NULL); if RCRYPT_FAILED(CryptHashData(hHash, "1234567890", sizeof("1234567890"), 0)) { return 0; } sprintf(line,"Feed: abcdefghijklmnopqrstuvwxyz"); updateMessages(NULL); return !RCRYPT_FAILED(CryptHashData(hHash, "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"), 0));}int testGetContainerName(){ char cName[MAX_PATH]; int dataLen; dataLen = sizeof(char)*MAX_PATH; if(CryptGetProvParam(hProv, PP_CONTAINER, cName, &dataLen, 0)) { updateMessages(cName); return TRUE; } else { return FALSE; } }int testGetHashValue(){ DWORD dwDataLen; /* Hashed data lenth.*/ BYTE *pbData; /* hashed data.*/ int i; /* Iterator.*/ char byteAscii[4]; if(RCRYPT_FAILED(CryptGetHashParam(hHash, HP_HASHVAL, NULL, &dwDataLen,0))) { return 0; } if(dwDataLen<=0) { return 0; } pbData = (BYTE *)HeapAlloc(heapHandle, HEAP_ZERO_MEMORY, dwDataLen); if(RCRYPT_FAILED(CryptGetHashParam(hHash, HP_HASHVAL, pbData, &dwDataLen,0))) { return 0; } sprintf(line,"Hash:"); updateMessages(NULL); strcpy(line,""); for(i=0; i<dwDataLen; i++) { sprintf(byteAscii,"%x", pbData[i]); strcat(line, byteAscii); } updateMessages(NULL); return 1;}int testKxSignHash(){ DWORD dwSigLen; /* Signature lenth.*/ BYTE *pbSignature; /* Signature.*/ int i; /* Iterator.*/ char byteAscii[4]; if(RCRYPT_FAILED(CryptSignHash(hHash, AT_KEYEXCHANGE, NULL, 0, NULL, &dwSigLen))) { return 0; } if(dwSigLen<=0) { return 0; } pbSignature = (BYTE *)HeapAlloc(heapHandle, HEAP_ZERO_MEMORY, dwSigLen); if(RCRYPT_FAILED(CryptSignHash(hHash, AT_KEYEXCHANGE, NULL, 0, pbSignature, &dwSigLen))) { return 0; } sprintf(line,"Signature:"); updateMessages(NULL); strcpy(line,""); for(i=0; i<dwSigLen; i++) { sprintf(byteAscii,"%x", pbSignature[i]); if(strlen(line)+strlen(byteAscii)+1>80) { updateMessages(NULL); strcpy(line,""); } strcat(line,byteAscii); } updateMessages(NULL); return 1;}int testSigSignHash(){ DWORD dwSigLen; /* Signature lenth.*/ BYTE *pbSignature; /* Signature.*/ int i; /* Iterator.*/ char byteAscii[4]; if(RCRYPT_FAILED(CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, NULL, &dwSigLen))) { return 0; } if(dwSigLen<=0) { return 0; } pbSignature = (BYTE *)HeapAlloc(heapHandle, HEAP_ZERO_MEMORY, dwSigLen); if(RCRYPT_FAILED(CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, pbSignature, &dwSigLen))) { return 0; } sprintf(line,"Signature:"); updateMessages(NULL); strcpy(line,""); for(i=0; i<dwSigLen; i++) { sprintf(byteAscii,"%x", pbSignature[i]); if(strlen(line)+strlen(byteAscii)+1>80) { updateMessages(NULL); strcpy(line,""); } strcat(line,byteAscii); } updateMessages(NULL); return 1;}int testDestroyUserSigKey(){ return !RCRYPT_FAILED(CryptDestroyKey(hUserSigKey));}int testDestroyUserKxKey(){ return !RCRYPT_FAILED(CryptDestroyKey(hUserKxKey));}int testDestroyHash(){ return CryptDestroyHash(hHash);}/** \brief Destroy user Keys. * * Release the user keys. */int testDestroyUserKeys(){ int failure; failure = 0; if(!testDestroyUserSigKey()) { updateMessages("Failed to release signature keys :-("); failure++; } else { updateMessages("Signature keys released:-)"); } if(!testDestroyUserKxKey()) { updateMessages("Failed to release key exchange keys :-("); failure++; } else { updateMessages("Key exchange keys released :-)"); } if(failure>1) { return FALSE; } else { return TRUE; }}int testExportPublicSigKey(){ DWORD dwDataLen; /* Hashed data lenth.*/ BYTE *pbData; /* hashed data.*/ BYTE byte; int i; /* Iterator.*/ char byteAscii[4]; int bytesLen; RSAPUBLICKEY_BLOB *pPubKeyBlob; if(!CryptExportKey(hUserSigKey, 0, PUBLICKEYBLOB, 0, NULL, &dwDataLen)) { return 0; } pbData = (BYTE *)HeapAlloc(heapHandle, HEAP_ZERO_MEMORY, dwDataLen); if(pbData == NULL) { SetLastError(NTE_NO_MEMORY); return 0; } if(!CryptExportKey(hUserSigKey, 0, PUBLICKEYBLOB, 0, pbData, &dwDataLen)) { return 0; } pPubKeyBlob = (RSAPUBLICKEY_BLOB *)pbData; sprintf(line,"Sig key blob:"); updateMessages(NULL); sprintf(line, "======================================="); updateMessages(NULL); if(pPubKeyBlob->publicKeyStruc.bType == PUBLICKEYBLOB) { updateMessages("Public key blob"); } else { return 0; } sprintf(line, "Blob version:%d",pPubKeyBlob->publicKeyStruc.bVersion); updateMessages(NULL); sprintf(line, "Alg ID: %d", pPubKeyBlob->publicKeyStruc.aiKeyAlg); updateMessages(NULL); if(pPubKeyBlob->rsaPubKey.magic == RSA1) { updateMessages("RSA public key."); } else { return 0; } sprintf(line,"Public Exponent: %x",pPubKeyBlob->rsaPubKey.pubexp); updateMessages(NULL); bytesLen = pPubKeyBlob->rsaPubKey.bitlen/8; sprintf(line, "Modulus length: %d",pPubKeyBlob->rsaPubKey.bitlen); updateMessages(NULL); strcpy(line,""); for(i=0;i<bytesLen;i++) { byte = pPubKeyBlob->modulus[i]; if(byte < 16) { sprintf(byteAscii,"0%x", byte); } else { sprintf(byteAscii,"%x", byte); } if(strlen(line)+strlen(byteAscii)+1>80) { updateMessages(NULL); strcpy(line,""); } strcat(line, byteAscii); } updateMessages(NULL); sprintf(line, "======================================="); updateMessages(NULL); return 1;}/** \brief Get user signature Key. * * Get the user signature key. */int testGetUserSigKey(){ printf("Getting Signature key.\n"); return !RCRYPT_FAILED(CryptGetUserKey(hProv, AT_SIGNATURE, &hUserSigKey));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -