RSAcmd.cpp 9.57 KB
/* 
 *  Copyright (c) 2011,  广州江南科友union  
 *  All rights reserved. 
 *  文件名称: RSAcmd.cpp
 *  摘 要:    实现应用加密机RSA指令接口  
 *  当前版本: 1.0,  编写者: 杨武,修改时间: 2011-11-22 修改内容: 创建
 *  
 */
#include "stdafx.h"
extern "C" 
{
	//由公钥生成一个MAC 00EO
	//输入参数:pKey 公钥
	//输入参数:mac 对于公钥的MAC,用MK计算
	//返回值: <0:失败;=0成功
	_declspec(dllexport) int union_syj1001_genMacFromPKey(char* hsmIp,int hsmPort,int timeout,int isCfg,int hsmLenOfMsgHeader,int isLenOfHsmMsg,int clientSocket,char*pKey,char*mac)
   {
	    int oldClientSocket = -1;
		oldClientSocket = clientSocket;
		int tmpLen = 0;
		if(isCfg==1)
		{
			CONFIG *cfg;
			cfg = (struct CONFIG*)malloc(sizeof(struct CONFIG));
			getConfig(cfg);
			if(cfg!=NULL)
			{
				memcpy(hsmIp,cfg->ip,strlen(cfg->ip));
				hsmPort = cfg->port;
				timeout = cfg->timeout;
				hsmLenOfMsgHeader = cfg->hsmLenOfMsgHeader;
				isLenOfHsmMsg = cfg->isLenOfHsmMsg;
				free(cfg);
			}
		}
		if(clientSocket==-1)
		{
			clientSocket = socketConnect(hsmIp,hsmPort);
		}
		if(clientSocket>0)
		{
			char outData[1024] = {0};
			char inData[1024] = {0};
			char tmp[10] ={0};
			int  lenOfMsg = 0;
			//判断是否需要添加两位消息长度
			if(1==isLenOfHsmMsg)
			{
				lenOfMsg = 2;
			}
			tmpLen += lenOfMsg;
			//添加消息头
	        my_strcat(inData,(char*)(LPCTSTR)GetRandStr(hsmLenOfMsgHeader),tmpLen,hsmLenOfMsgHeader);
			//添加命令头
			my_strcat(inData,"00EO",tmpLen,4);
			//添加公钥编码
			my_strcat(inData,"01",tmpLen,2);
			//添加公钥
			my_strcat(inData,pKey,tmpLen,strlen(pKey));
                
			//在报文体内增加消息长度
			if(1==isLenOfHsmMsg)
			{
				int lenOfReport = hsmLenOfMsgHeader+4+2+strlen(pKey);
				inData[0] = lenOfReport/256;
				inData[1] = lenOfReport%256;
			}
			int outlen = socketHandle(clientSocket,inData,tmpLen,outData,timeout);
	        if(outlen>0)
			{
				//断开短连接
				if(oldClientSocket == -1)
				{
					socketDisconnect(clientSocket);
				}
				memset(tmp,0,sizeof(tmp));
				//获取错误码
				my_strncut(tmp,outData,4+hsmLenOfMsgHeader+lenOfMsg,2);
				if(strcmp(tmp,"00")==0)
				{
					//mac
				    my_strncut(mac,outData,4+hsmLenOfMsgHeader+lenOfMsg+2,16);
					return 0;
				}
				else
				{
					return revValue(tmp);
				}
			}
			else
			{
				return unionSocketErr;
			}

		}
		else
		{
			return clientSocket;
		}

   }
	
	//输出一个DES密钥00GK
	//输入参数:formatOfKeyBlock 密钥块编码
	//输入参数:typeOfFilled 填充方式
	//输入参数:indexOfKey 密钥索引
	//输入参数:keyValByMK 密钥值
	//输入参数:chkVal
	//输入参数:lenOfMac MAC值中需要进行验证的字节数;最小值为8,最大值16
	//输入参数:mac
	//输入参数:pKey 公钥,用ASN.1格式编码的DER(模、指数的序列)
	//输出参数:iniVal
	//输出参数:lenOfDESKey
	//输出参数:lenOfKeyBlock
	//输出参数:DESKeyByPK 公钥下加密的DES密钥。
	//返回值: <0:失败;>0成功 公钥下加密的DES密钥长度
	_declspec(dllexport) int union_syj1001_outputDESKey(char* hsmIp,int hsmPort,int timeout,int isCfg,int hsmLenOfMsgHeader,int isLenOfHsmMsg,int clientSocket,char formatOfKeyBlock,char typeOfFilled,char*indexOfKey,char*keyValByMK,char*chkVal,int lenOfMac,char*mac,char*pKey,char*iniVal,char *clenOfDESKey,char * cLenOfKeyBlock,char*DESKeyByPK)
	{
		int oldClientSocket = -1;
		oldClientSocket = clientSocket;
		int tmpLen = 0;
		if(isCfg==1)
		{
			CONFIG *cfg;
			cfg = (struct CONFIG*)malloc(sizeof(struct CONFIG));
			getConfig(cfg);
			if(cfg!=NULL)
			{
				memcpy(hsmIp,cfg->ip,strlen(cfg->ip));
				hsmPort = cfg->port;
				timeout = cfg->timeout;
				hsmLenOfMsgHeader = cfg->hsmLenOfMsgHeader;
				isLenOfHsmMsg = cfg->isLenOfHsmMsg;
				free(cfg);
			}
		}
		if(clientSocket==-1)
		{
			clientSocket = socketConnect(hsmIp,hsmPort);
		}
		if(clientSocket>0)
		{
			char outData[1024] = {0};
			char inData[1024] = {0};
			char tmp[10] ={0};
			int  lenOfMsg = 0;
			//判断是否需要添加两位消息长度
			if(1==isLenOfHsmMsg)
			{
				lenOfMsg = 2;
			}
			tmpLen += lenOfMsg;
			//添加消息头
	        my_strcat(inData,(char*)(LPCTSTR)GetRandStr(hsmLenOfMsgHeader),tmpLen,hsmLenOfMsgHeader);
			//添加命令头
			my_strcat(inData,"00GK",tmpLen,4);
			//密钥块编码
			my_strcat(inData,&formatOfKeyBlock,tmpLen,1);
			//填充方式
			my_strcat(inData,&typeOfFilled,tmpLen,1);
			//添加密钥索引
			my_strcat(inData,indexOfKey,tmpLen,4);
			if(strcmp(indexOfKey,"FFFF")==0)
			{
				//添加密钥长度
				int keyLen = strlen(keyValByMK);
				memset(tmp,0,sizeof(tmp));
				lenToChar(keyLen,tmp);
				my_strcat(inData,tmp,tmpLen,2);
				//添加密钥值
				my_strcat(inData,keyValByMK,tmpLen,keyLen);
			}
			my_strcat(inData,chkVal,tmpLen,16);

			//MAC长度
			memset(tmp,0,sizeof(tmp));
			IntToChar(lenOfMac/2,2,tmp);
			my_strcat(inData,tmp,tmpLen,2);
			//mac
			my_strcat(inData,mac,tmpLen,16);
			//添加公钥
			my_strcat(inData,pKey,tmpLen,strlen(pKey));
                
			//在报文体内增加消息长度
			if(1==isLenOfHsmMsg)
			{
				int lenOfReport = tmpLen-lenOfMsg;
				inData[0] = lenOfReport/256;
				inData[1] = lenOfReport%256;
			}
			int outlen = socketHandle(clientSocket,inData, tmpLen,outData,timeout);
	        if(outlen>0)
			{
				//断开短连接
				if(oldClientSocket == -1)
				{
					socketDisconnect(clientSocket);
				}
				memset(tmp,0,sizeof(tmp));
				//获取错误码
				my_strncut(tmp,outData,4+hsmLenOfMsgHeader+lenOfMsg,2);
				if(strcmp(tmp,"00")==0)
				{
					//初始化值
				    my_strncut(iniVal,outData,4+hsmLenOfMsgHeader+lenOfMsg+2,16);
					my_strncut(clenOfDESKey,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16,4);
					int lenDES = 0-revValue(clenOfDESKey);

					if(typeOfFilled=='0')
					{
						
						my_strncut(cLenOfKeyBlock,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16+4,8);
						my_strncut(DESKeyByPK,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16+4+8,lenDES*2);
						return lenDES;
					}
					else
					{
						my_strncut(DESKeyByPK,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16+4,lenDES*2);
						return lenDES;
					}


					
				}
				else
				{
					return revValue(tmp);
				}
			}
			else
			{
				return unionSocketErr;
			}

		}
		else
		{
			return clientSocket;
		}


	}





	
	//输出一个DES密钥S0GK
	//输入参数:formatOfKeyBlock 密钥块编码
	//输入参数:typeOfFilled 填充方式
	//输入参数:indexOfKey 密钥索引
	//输入参数:keyValByMK 密钥值
	//输入参数:chkVal
	//输入参数:lenOfMac MAC值中需要进行验证的字节数;最小值为8,最大值16
	//输入参数:mac
	//输入参数:pKey 公钥,用ASN.1格式编码的DER(模、指数的序列)
	//输出参数:iniVal
	//输出参数:lenOfDESKey
	//输出参数:lenOfKeyBlock
	//输出参数:DESKeyByPK 公钥下加密的DES密钥。
	//返回值: <0:失败;>0成功 公钥下加密的DES密钥长度
	_declspec(dllexport) int union_syj1001_outputDESKey_SM1(char* hsmIp,int hsmPort,int timeout,int isCfg,int hsmLenOfMsgHeader,int isLenOfHsmMsg,int clientSocket,char formatOfKeyBlock,char typeOfFilled,char*indexOfKey,char*keyValByMK,char*chkVal,int lenOfMac,char*mac,char*pKey,char*iniVal,char *clenOfDESKey,char *cLenOfKeyBlock,char*DESKeyByPK)
	{
		int oldClientSocket = -1;
		oldClientSocket = clientSocket;
		int tmpLen = 0;
		if(isCfg==1)
		{
			CONFIG *cfg;
			cfg = (struct CONFIG*)malloc(sizeof(struct CONFIG));
			getConfig(cfg);
			if(cfg!=NULL)
			{
				memcpy(hsmIp,cfg->ip,strlen(cfg->ip));
				hsmPort = cfg->port;
				timeout = cfg->timeout;
				hsmLenOfMsgHeader = cfg->hsmLenOfMsgHeader;
				isLenOfHsmMsg = cfg->isLenOfHsmMsg;
				free(cfg);
			}
		}
		if(clientSocket==-1)
		{
			clientSocket = socketConnect(hsmIp,hsmPort);
		}
		if(clientSocket>0)
		{
			char outData[1024] = {0};
			char inData[1024] = {0};
			char tmp[10] ={0};
			int  lenOfMsg = 0;
			//判断是否需要添加两位消息长度
			if(1==isLenOfHsmMsg)
			{
				lenOfMsg = 2;
			}
			tmpLen += lenOfMsg;
			//添加消息头
	        my_strcat(inData,(char*)(LPCTSTR)GetRandStr(hsmLenOfMsgHeader),tmpLen,hsmLenOfMsgHeader);
			//添加命令头
			my_strcat(inData,"S0GK",tmpLen,4);
			//密钥块编码
			my_strcat(inData,&formatOfKeyBlock,tmpLen,1);
			//填充方式
			my_strcat(inData,&typeOfFilled,tmpLen,1);
			//添加密钥索引
			my_strcat(inData,indexOfKey,tmpLen,4);
			if(strcmp(indexOfKey,"FFFF")==0)
			{
				//添加密钥长度
				int keyLen = strlen(keyValByMK);
				memset(tmp,0,sizeof(tmp));
				lenToChar(keyLen,tmp);
				my_strcat(inData,tmp,tmpLen,2);
				//添加密钥值
				my_strcat(inData,keyValByMK,tmpLen,keyLen);
			}
			my_strcat(inData,chkVal,tmpLen,16);

			//MAC长度
			memset(tmp,0,sizeof(tmp));
			IntToChar(lenOfMac/2,2,tmp);
			my_strcat(inData,tmp,tmpLen,2);
			//mac
			my_strcat(inData,mac,tmpLen,16);
			//添加公钥
			my_strcat(inData,pKey,tmpLen,strlen(pKey));
                
			//在报文体内增加消息长度
			if(1==isLenOfHsmMsg)
			{
				int lenOfReport = tmpLen-lenOfMsg;
				inData[0] = lenOfReport/256;
				inData[1] = lenOfReport%256;
			}
			int outlen = socketHandle(clientSocket,inData, tmpLen,outData,timeout);
	        if(outlen>0)
			{
				//断开短连接
				if(oldClientSocket == -1)
				{
					socketDisconnect(clientSocket);
				}
				memset(tmp,0,sizeof(tmp));
				//获取错误码
				my_strncut(tmp,outData,4+hsmLenOfMsgHeader+lenOfMsg,2);
				if(strcmp(tmp,"00")==0)
				{
					//初始化值
				    my_strncut(iniVal,outData,4+hsmLenOfMsgHeader+lenOfMsg+2,16);
					my_strncut(clenOfDESKey,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16,4);
					int lenDES = 0-revValue(clenOfDESKey);

					if(typeOfFilled=='0')
					{
						
						my_strncut(cLenOfKeyBlock,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16+4,8);
						my_strncut(DESKeyByPK,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16+4+8,lenDES*2);
						return lenDES;
					}
					else
					{
						my_strncut(DESKeyByPK,outData,4+hsmLenOfMsgHeader+lenOfMsg+2+16+4,lenDES*2);
						return lenDES;
					}


					
				}
				else
				{
					return revValue(tmp);
				}
			}
			else
			{
				return unionSocketErr;
			}

		}
		else
		{
			return clientSocket;
		}


	}





};