RSAcmd.cpp
9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* 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;
}
}
};