📄 browsertoken.cpp
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
*/
#include "browsertoken.h"
#include "getkeys.h"
#include "idobj.h"
#include "defaultstore.h"
#include "voltfile.h"
#include "vsstore.h"
#include "vtime.h"
#include "arbitrarystore.h"
#include "PassDialog.h"
#include "passwordmanager.h"
#include <string.h>
#include <stdio.h>
#define VT_BROWSER_TOKEN_ARG1 "storetoken"
#define VT_BROWSER_TOKEN_ARG1_LEN 10
#define MAX_DISTRICT_NAME_LEN 100
#define MAX_PASSWORD_SIZE 64
#define APP_LABEL "Toolkit TokenHandler"
#ifdef __cplusplus
extern "C" {
#endif
VtIdentitySchemaDecode *decoders[1] = {
VtIdentitySchemaDecode822Email
};
unsigned int decoderCount = 1;
VtDerCoder *derCoders[] = {VtDerCoderDSAPublicKey };
unsigned int coderCount = 1;
/* This "application" opens a file containing the XML structure
* containing an identity and an associated auth token. It parses the
* XML, stores the auth token in the given location in the request infos,
* then downloads the IBE private key and cert for the identity. The token
* Handler tries to fulfill all the requests pending. In order to interface
* this application with browsers (ex. Internet explorer) certain
* entries has to be stored in the registry. This application is invoked when
* any file with .vat extension is double clicked. The main purpose of this application
* is to handle and store crypto data when clients don't have Voltage SecureMail
* installed on their machines. Below are the registry values that should exist
* for this application to work as token handler. Copy the text below in a file with
* extension .reg and run it. The entries will be stored properly.
*
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.vat]
@="vatfile"
"Content Type"="application/x-vs-authtoken"
[HKEY_CLASSES_ROOT\vatfile]
@="Voltage Authentication Token"
"EditFlags"=hex:00,00,01,00
[HKEY_CLASSES_ROOT\vatfile\shell]
[HKEY_CLASSES_ROOT\vatfile\shell\open]
[HKEY_CLASSES_ROOT\vatfile\shell\open\command]
@="C:\\toolkit\\lib\\TokenHandler\\Debug\\TokenHandler.exe storetoken \"%1\""
[HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/x-vs-authtoken]
"Extension"=".vat"
*/
/* Initialize the icServerResponseData
*/
void InitializeResponseData (
icServerResponseData *responseData,
VoltLibCtx *libCtx )
{
/* Initialize to null
*/
Z2Memset(responseData, 0, sizeof(icServerResponseData) );
}
/* This is the routine to free a icServerResponseData object.
*/
void FreeResponseData (
icServerResponseData *responseData,
VoltLibCtx *libCtx)
{
if(responseData ==(icServerResponseData *)0 )
return;
/* A server response has following components to free.
*/
if (responseData->authToken != (void*) 0)
{
Z2Free(responseData->authToken);
responseData->authToken = (char *) 0;
}
if (responseData->privateKey != (void*) 0)
{
Z2Free(responseData->privateKey);
responseData->privateKey = (unsigned char *) 0;
}
if (responseData->id != (void*) 0)
{
Z2Free(responseData->id);
responseData->id = (unsigned char *) 0;
}
if (responseData->asn1Cert != (void*) 0)
{
Asn1X509Cert_free (responseData->asn1Cert);
responseData->asn1Cert = (Asn1X509Cert *) 0;
}
if (responseData->url != (void*) 0)
{
Z2Free(responseData->url);
responseData->url = (char *) 0;
}
}
/* This will get the full path of the storage location.
*/
unsigned char *getStorageLocation (VtLibCtx libCtx, unsigned char *location)
{
static unsigned char buff[256];
if (location != (unsigned char *)0)
return (location);
getICDirectory ((VoltLibCtx *)libCtx, buff, 256);
return (buff);
}
/* This is The main Token handler function
*/
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
int status=0, icRet, code, clientRequest;
unsigned int contentsLen;
unsigned int deleteRequest, reqStatus;
unsigned int bufferSize;
unsigned int decoderIndex, storageIndex;
VtLibCtx libraryCtx = (VtLibCtx)0;
VtMpIntCtx mpIntCtx = (VtMpIntCtx)0;
VoltLibCtx *libCtx = (VoltLibCtx *)0;
VtFileCtx fCtx = (VtFileCtx)0;
VoltFileCtx *fileCtx = (VoltFileCtx *)0;
VoltFileHandle fileHandle = (VoltFileHandle)0;
VtStorageCtx storageCtx = (VtStorageCtx)0;
VtStorageCtx clientStorage = (VtStorageCtx)0;
VtStorageCtx toolkitStorage = (VtStorageCtx)0;
VtTransportCtx transportCtx = (VtTransportCtx)0;
VtArbitraryStorageObject arbitObj = (VtArbitraryStorageObject)0;
VoltArbitraryStorageHandle *arbitHandle = (VoltArbitraryStorageHandle *)0;
VtIdentityObject idObj = (VtIdentityObject)0;
VoltIdentityObject *obj = (VoltIdentityObject *)0;
VtKeyObject ibePriKey = (VtKeyObject)0;
VtCertRequestObject certReq = (VtCertRequestObject)0;
VtCertObject signingCert = (VtCertObject)0;
VtWinINetTransportInfo winTransInfo;
VtFileCtxUseInfo storageInfo;
VtCertRequestInfo certInfo;
VoltRequestInfoData *getEntry;
VtPasswordManagerCallback passwordInfo;
unsigned char *contents = (unsigned char *)0;
unsigned char *fileName = (unsigned char *)0;
char errorString[512];
char *usage = "\nUsage: tokenhandler.exe storetoken <filename>..exiting\n";
StorageStatus *storeList = (StorageStatus *)0;
/* We need a server response data struct to store the response from the
* Key server and parse it later.
*/
icServerResponseData responseData ;
/* First, make sure the argument count is correct. We're expecting
* three args (argv[0] is the conventional command, argv[1] should be
* "storetoken", and argv[2] should be the file name.
*/
if (__argc != 3) {
MessageBox (NULL, usage , "Toolkit Token Handler", MB_OK);
printf(usage);
return 0;
}
/* Check the args, the second arg should be "storetoken".
*/
if (strcmp (__argv[1], "storetoken") != 0) {
printf("\n Invalid argument %s \n",__argv[1]);
printf(usage);
return 0;
}
do
{
/* We'll need a libCtx.
*/
status = VtCreateLibCtx (
VtMemoryImplWin32, (Pointer)0,
VtThreadImplDefaultSingle, (Pointer)0, &libraryCtx);
if (status != 0)
break;
libCtx = (VoltLibCtx *)libraryCtx;
/* initialize the server response data struct
*/
InitializeResponseData (&responseData, libCtx);
/*create Multi Precision Int context and set it onto the lib ctx.
*/
status= VtCreateMpIntCtx (
libraryCtx,VtMpIntImplOpenSSL, (Pointer) 0,&mpIntCtx);
if (status != 0)
break;
status = VtSetLibCtxParam (
libCtx, VtLibCtxParamMpCtx, (Pointer)mpIntCtx);
if (status != 0)
break;
/* To store the token in a file system
* We need a file ctx
*/
status = VtCreateFileCtx (libCtx, VtFileImplWin32, (Pointer)0, &fCtx);
if (status != 0)
break;
fileCtx = (VoltFileCtx *)fCtx;
/* Create a client compatible storage ctx
*/
status = VtCreateStorageCtx (
libCtx, VtStorageImplBasic, (Pointer)0, &clientStorage);
if (status != 0)
break;
status = VtAddStorageProvider (
clientStorage, VtStorageFileWin32Client, (Pointer)fCtx);
if (status != 0)
break;
/* Set up password information on the client storage
*/
passwordInfo.PasswordFunction = PasswordManager;
passwordInfo.appData = (Pointer)0;
passwordInfo.AppDataCopy = (VtPasswordAppDataCopy)0;
passwordInfo.AppDataFree = (VtPasswordAppDataFree)0;
status = VtSetStorageParam (
clientStorage, VtStorageParamPasswordManager, (Pointer)&passwordInfo);
if (status != 0)
break;
winTransInfo.uiHandle = (Pointer)GetDesktopWindow ();
winTransInfo.asyncFlag = VT_ASYNC_RESPONSE_ALERT;
status = VtCreateTransportCtx (
libCtx, VtTransportImplHttpsWinINet, (Pointer)&winTransInfo, &transportCtx);
if (status != 0)
break;
/* Create the arbitrary storage ctx
*/
status = VoltCreateArbitraryStorageObject (
libCtx, VtArbitraryStorageImplRequestInfo, (Pointer)0, &arbitObj);
if (status != 0)
break;
/* The third arg should be a filename. Try to open that file.
*/
fileName = (unsigned char *)__argv[2];
status = fileCtx->CtxOpenFile (
fileCtx, &fileHandle, (const unsigned char*)fileName,
VOLT_FILE_MODE_READ_ONLY, 0);
if (status != 0)
break;
/* How big is the file?
*/
status = fileCtx->CtxGetFileSize (
fileCtx, fileHandle, (const unsigned char *)0, (VoltFileInt *)&bufferSize);
if (status != 0)
break;
/* Add an extra byte for a NULL-terminating character.
*/
status = VT_ERROR_MEMORY;
contents = (unsigned char *)Z2Malloc (bufferSize + 1, VOLT_MEMORY_SENSITIVE);
if (contents == (unsigned char *)0)
break;
/* Read the file, make sure the data is NULL-terminated.
*/
status = fileCtx->CtxReadFile (
fileCtx, fileHandle, bufferSize, contents, &contentsLen);
if (status != 0)
break;
contents[contentsLen] = 0;
fileCtx->CtxCloseFile(fileCtx, &fileHandle);
/* Break the XML into the components.
*/
icRet = icParseServerResponse(&code, &responseData, (char *)contents,libCtx);
if (icRet != 0)
{
status = VT_ERROR_INVALID_INPUT;
break;
}
Z2Free (contents);
contents = (unsigned char *)0;
/* We are expecting the code to be
* IC_SERVER_TOKEN_RETURNED_RESPONSE.
*/
if (code != IC_SERVER_TOKEN_RETURNED_RESPONSE)
break;
/* Now create an identity object to use in requests and set it with the
* encoding that we got in the response from the server.
*/
status = VtCreateIdentityObject (
libraryCtx, VtIdentityImplMpCtx, (Pointer)mpIntCtx, &idObj);
if (status != 0)
break;
status = VtDecodeIdentity (
responseData.id, responseData.idLen,
decoders, decoderCount, &decoderIndex, idObj);
if (status != 0)
break;
obj = (VoltIdentityObject *)idObj;
/* We'll need to store the auth token. Use the default storage
* provider to store the token.
*/
storageInfo.fileCtx = fCtx;
storageInfo.path = (Pointer)0;
/* First check if there is a request from the client pending for
* the same identity. If yes fulfill it first.
*/
status = isClientRequestPending (
libCtx, clientStorage, idObj, &clientRequest);
if (status != 0)
{
MessageBox (
NULL, ERROR_FIND_CLIENT_REQUEST_MSG, "Toolkit Token Handler", MB_OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -