forked from BigBrotherTrade/flower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WXBizMsgCrypt.cpp
577 lines (490 loc) · 14.5 KB
/
WXBizMsgCrypt.cpp
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "WXBizMsgCrypt.h"
#include <cstring>
#include <cstdlib>
#include <arpa/inet.h>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include "openssl/aes.h"
#include "openssl/sha.h"
#include "openssl/evp.h"
#define FREE_PTR(ptr) \
if (NULL != (ptr)) {\
free (ptr);\
(ptr) = NULL;\
}
#define DELETE_PTR(ptr) \
if (NULL != (ptr)) {\
delete (ptr);\
(ptr) = NULL;\
}
namespace Tencent{
int WXBizMsgCrypt::VerifyURL(const std::string &sMsgSignature,
const std::string &sTimeStamp,
const std::string &sNonce,
const std::string &sEchoStr,
std::string &sReplyEchoStr)
{
if(0 != ValidateSignature(sMsgSignature,sTimeStamp,sNonce,sEchoStr) )
{
return WXBizMsgCrypt_ValidateSignature_Error;
}
//1.decode base64
std::string sAesData;
if(0 != DecodeBase64(sEchoStr,sAesData))
{
return WXBizMsgCrypt_DecodeBase64_Error;
}
//2.decode aes
std::string sAesKey;
std::string sNoEncryptData;
if(0 != GenAesKeyFromEncodingKey(m_sEncodingAESKey,sAesKey))
{
return WXBizMsgCrypt_IllegalAesKey;
}
if(0 != AES_CBCDecrypt(sAesData, sAesKey, &sNoEncryptData))
{
return WXBizMsgCrypt_DecryptAES_Error;
}
// 3. remove kRandEncryptStrLen str
if(sNoEncryptData.size() <= (kRandEncryptStrLen + kMsgLen))
{
return WXBizMsgCrypt_IllegalBuffer;
}
uint32_t iNetLen = *((const uint32_t *)(sNoEncryptData.c_str() + kRandEncryptStrLen));
uint32_t iMsgLen = ntohl(iNetLen);
if(sNoEncryptData.size() < (kRandEncryptStrLen + kMsgLen + iMsgLen))
{
return WXBizMsgCrypt_IllegalBuffer;
}
sReplyEchoStr = sNoEncryptData.substr(kRandEncryptStrLen+kMsgLen,iMsgLen );
//4. validate Corpid
std::string sReceiveId = sNoEncryptData.substr(kRandEncryptStrLen+kMsgLen+iMsgLen);
if(sReceiveId != m_sReceiveId)
{
return WXBizMsgCrypt_ValidateCorpid_Error;
}
return WXBizMsgCrypt_OK;
}
int WXBizMsgCrypt::DecryptMsg(const std::string &sMsgSignature,
const std::string &sTimeStamp,
const std::string &sNonce,
const std::string &sPostData,
std::string &sMsg)
{
//1.validate xml format
std::string sEncryptMsg;
if(0 != GetXmlField(sPostData,"Encrypt",sEncryptMsg))
{
return WXBizMsgCrypt_ParseXml_Error;
}
//2.validate signature
if(0 != ValidateSignature(sMsgSignature,sTimeStamp,sNonce,sEncryptMsg) )
{
return WXBizMsgCrypt_ValidateSignature_Error;
}
//3.decode base64
std::string sAesData;
if(0 != DecodeBase64(sEncryptMsg,sAesData))
{
return WXBizMsgCrypt_DecodeBase64_Error;
}
//4.decode aes
std::string sAesKey;
std::string sNoEncryptData;
if(0 != GenAesKeyFromEncodingKey(m_sEncodingAESKey,sAesKey))
{
return WXBizMsgCrypt_IllegalAesKey;
}
if(0 != AES_CBCDecrypt(sAesData, sAesKey, &sNoEncryptData))
{
return WXBizMsgCrypt_DecryptAES_Error;
}
// 5. remove kRandEncryptStrLen str
if(sNoEncryptData.size() <= (kRandEncryptStrLen + kMsgLen))
{
return WXBizMsgCrypt_IllegalBuffer;
}
uint32_t iNetLen = *((const uint32_t *)(sNoEncryptData.c_str() + kRandEncryptStrLen));
uint32_t iMsgLen = ntohl(iNetLen);
if(sNoEncryptData.size() < (kRandEncryptStrLen + kMsgLen + iMsgLen))
{
return WXBizMsgCrypt_IllegalBuffer;
}
sMsg = sNoEncryptData.substr(kRandEncryptStrLen+kMsgLen,iMsgLen );
//6. validate corpid
std::string sReceiveId = sNoEncryptData.substr(kRandEncryptStrLen+kMsgLen+iMsgLen);
if(sReceiveId != m_sReceiveId)
{
return WXBizMsgCrypt_ValidateCorpid_Error;
}
return WXBizMsgCrypt_OK;
}
int WXBizMsgCrypt::EncryptMsg(const std::string &sReplyMsg,
const std::string &sTimeStamp,
const std::string &sNonce,
std::string &sEncryptMsg)
{
if(0 == sReplyMsg.size())
{
return WXBizMsgCrypt_ParseXml_Error;
}
//1.add rand str ,len, corpid
std::string sNeedEncrypt;
GenNeedEncryptData(sReplyMsg,sNeedEncrypt);
//2. AES Encrypt
std::string sAesData;
std::string sAesKey;
if(0 != GenAesKeyFromEncodingKey(m_sEncodingAESKey,sAesKey))
{
return WXBizMsgCrypt_IllegalAesKey;
}
if(0 != AES_CBCEncrypt(sNeedEncrypt, sAesKey, &sAesData))
{
return WXBizMsgCrypt_EncryptAES_Error;
}
//3. base64Encode
std::string sBase64Data;
if( 0!= EncodeBase64(sAesData,sBase64Data) )
{
return WXBizMsgCrypt_EncodeBase64_Error;
}
//4. compute signature
std::string sSignature;
if(0!=ComputeSignature(m_sToken, sTimeStamp, sNonce, sBase64Data, sSignature))
{
return WXBizMsgCrypt_ComputeSignature_Error;
}
//5. Gen xml
if(0 != GenReturnXml(sBase64Data, sSignature, sTimeStamp, sNonce, sEncryptMsg) )
{
return WXBizMsgCrypt_GenReturnXml_Error ;
}
return WXBizMsgCrypt_OK;
}
int WXBizMsgCrypt::AES_CBCEncrypt( const std::string & objSource,
const std::string & objKey, std::string * poResult )
{
return AES_CBCEncrypt( objSource.data(), objSource.size(),
objKey.data(), objKey.size(), poResult );
}
int WXBizMsgCrypt::AES_CBCEncrypt( const char * sSource, const uint32_t iSize,
const char * sKey, uint32_t iKeySize, std::string * poResult )
{
if ( !sSource || !sKey || !poResult || iSize <= 0)
{
return -1;
}
poResult->clear();
int padding = kAesKeySize - iSize % kAesKeySize;
char * tmp = (char*)malloc( iSize + padding );
if(NULL == tmp)
{
return -1;
}
memcpy( tmp, sSource, iSize );
memset( tmp + iSize, padding, padding );
unsigned char * out = (unsigned char*)malloc( iSize + padding );
if(NULL == out)
{
FREE_PTR(tmp);
return -1;
}
unsigned char key[ kAesKeySize ] = { 0 };
unsigned char iv[ kAesIVSize ] = { 0 };
memcpy( key, sKey, iKeySize > kAesKeySize ? kAesKeySize : iKeySize );
memcpy(iv, key, sizeof(iv) < sizeof(key) ? sizeof(iv) : sizeof(key));
AES_KEY aesKey;
AES_set_encrypt_key( key, 8 * kAesKeySize, &aesKey );
AES_cbc_encrypt((unsigned char *)tmp, out,iSize + padding, &aesKey, iv, AES_ENCRYPT);
poResult->append((char*)out, iSize + padding);
FREE_PTR(tmp);
FREE_PTR(out);
return 0;
}
int WXBizMsgCrypt::AES_CBCDecrypt( const std::string & objSource,
const std::string & objKey, std::string * poResult )
{
return AES_CBCDecrypt( objSource.data(), objSource.size(),
objKey.data(), objKey.size(), poResult );
}
int WXBizMsgCrypt::AES_CBCDecrypt( const char * sSource, const uint32_t iSize,
const char * sKey, uint32_t iKeySize, std::string * poResult )
{
if ( !sSource || !sKey || iSize < kAesKeySize || iSize % kAesKeySize != 0 || !poResult)
{
return -1;
}
poResult->clear();
unsigned char * out = (unsigned char*)malloc( iSize );
if(NULL == out)
{
return -1;
}
unsigned char key[ kAesKeySize ] = { 0 };
unsigned char iv[ kAesIVSize ] = {0} ;
memcpy( key, sKey, iKeySize > kAesKeySize ? kAesKeySize : iKeySize );
memcpy(iv, key, sizeof(iv) < sizeof(key) ? sizeof(iv) : sizeof(key));
int iReturnValue = 0;
AES_KEY aesKey;
AES_set_decrypt_key( key, 8 * kAesKeySize, &aesKey );
AES_cbc_encrypt( (unsigned char *)sSource, out, iSize, &aesKey, iv ,AES_DECRYPT);
if( out[iSize-1] > 0 && out[iSize-1] <= kAesKeySize && (iSize - out[iSize-1]) > 0 )
{
poResult->append( (char *)out , iSize - out[iSize-1] );
} else {
iReturnValue = -1;
}
FREE_PTR(out);
return iReturnValue;
}
int WXBizMsgCrypt::EncodeBase64(const std::string sSrc, std::string & sTarget)
{
if(0 == sSrc.size() || kMaxBase64Size < sSrc.size())
{
return -1;
}
uint32_t iBlockNum = sSrc.size() / 3;
if (iBlockNum * 3 != sSrc.size())
{
iBlockNum++;
}
uint32_t iOutBufSize = iBlockNum * 4 + 1;
char * pcOutBuf = (char*)malloc( iOutBufSize);
if(NULL == pcOutBuf)
{
return -1;
}
int iReturn = 0;
int ret = EVP_EncodeBlock((unsigned char*)pcOutBuf, (const unsigned char*)sSrc.c_str(), sSrc.size());
if (ret > 0 && ret < (int)iOutBufSize)
{
sTarget.assign(pcOutBuf,ret);
}
else
{
iReturn = -1;
}
FREE_PTR(pcOutBuf);
return iReturn;
}
int WXBizMsgCrypt::DecodeBase64(const std::string sSrc, std::string & sTarget)
{
if(0 == sSrc.size() || kMaxBase64Size < sSrc.size())
{
return -1;
}
//计算末尾=号个数
int iEqualNum = 0;
for(int n= sSrc.size() - 1; n>=0; --n)
{
if(sSrc.c_str()[n] == '=')
{
iEqualNum++;
}
else
{
break;
}
}
int iOutBufSize = sSrc.size();
char * pcOutBuf = (char*)malloc( iOutBufSize);
if(NULL == pcOutBuf)
{
return -1;
}
int iRet = 0;
int iTargetSize = 0;
iTargetSize = EVP_DecodeBlock((unsigned char*)pcOutBuf, (const unsigned char*)sSrc.c_str(), sSrc.size());
if(iTargetSize > iEqualNum && iTargetSize < iOutBufSize)
{
sTarget.assign(pcOutBuf, iTargetSize - iEqualNum);
}
else
{
iRet = -1;
}
FREE_PTR(pcOutBuf);
return iRet;
}
int WXBizMsgCrypt::ComputeSignature(const std::string sToken, const std::string sTimeStamp, const std::string & sNonce,
const std::string & sMessage, std::string & sSignature)
{
if( 0 == sToken.size() || 0 == sNonce.size() || 0 == sMessage.size() || 0 == sTimeStamp.size())
{
return -1;
}
//sort
std::vector< std::string > vecStr;
vecStr.push_back( sToken );
vecStr.push_back( sTimeStamp );
vecStr.push_back( sNonce );
vecStr.push_back( sMessage );
std::sort( vecStr.begin(), vecStr.end() );
std::string sStr = vecStr[0] + vecStr[1] + vecStr[2] + vecStr[3] ;
//compute
unsigned char output[SHA_DIGEST_LENGTH] = { 0 };
if( NULL == SHA1( (const unsigned char *)sStr.c_str(), sStr.size(), output ) )
{
return -1;
}
// to hex
sSignature.clear();
char tmpChar[ 8 ] = { 0 };
for( int i = 0; i < SHA_DIGEST_LENGTH; i++ )
{
snprintf( tmpChar, sizeof( tmpChar ), "%02x", 0xff & output[i] );
sSignature.append( tmpChar );
}
return 0;
}
int WXBizMsgCrypt::ValidateSignature(const std::string &sMsgSignature, const std::string &sTimeStamp,
const std::string &sNonce, const std::string & sEncryptMsg)
{
std::string sSignature;
if(0 != ComputeSignature(m_sToken, sTimeStamp, sNonce, sEncryptMsg, sSignature))
{
return -1;
}
if( sMsgSignature != sSignature)
{
return -1;
}
return 0;
}
int WXBizMsgCrypt::GenAesKeyFromEncodingKey( const std::string & sEncodingKey, std::string & sAesKey)
{
if(kEncodingKeySize != sEncodingKey.size())
{
return -1;
}
std::string sBase64 = sEncodingKey + "=";
int ret = DecodeBase64(sBase64, sAesKey);
if(0 != ret || kAesKeySize != sAesKey.size())
{
return -1;
}
return 0;
}
int WXBizMsgCrypt::GetXmlField(const std::string & sPostData, const std::string & sField, std::string &sEncryptMsg)
{
tinyxml2::XMLDocument xmlDoc;
if(tinyxml2::XML_SUCCESS != xmlDoc.Parse(sPostData.c_str(), sPostData.size()))
{
return -1;
}
tinyxml2::XMLElement * xmlElement = xmlDoc.FirstChildElement("xml");
if(NULL == xmlElement)
{
return -1;
}
tinyxml2::XMLElement * msgElement = xmlElement->FirstChildElement(sField.c_str());
if(NULL == msgElement)
{
return -1;
}
const char* pMsgBuf = msgElement->GetText();
if(NULL == pMsgBuf)
{
return -1;
}
sEncryptMsg = pMsgBuf;
return 0;
}
void WXBizMsgCrypt::GenRandStr(std::string & sRandStr, uint32_t len)
{
uint32_t idx = 0;
srand((unsigned)time(NULL));
char tempChar = 0;
sRandStr.clear();
while(idx < len)
{
tempChar = rand()%128;
if(isprint(tempChar))
{
sRandStr.append(1, tempChar);
++idx;
}
}
}
void WXBizMsgCrypt::GenNeedEncryptData(const std::string &sReplyMsg,std::string & sNeedEncrypt )
{
//random(16B)+ msg_len(4B) + msg + $corpid
std::string sRandStr;
GenRandStr(sRandStr,kRandEncryptStrLen);
uint32_t iXmlSize = sReplyMsg.size();
uint32_t iNSize = htonl(iXmlSize);
std::string sSize ;
sSize.assign((const char *)&iNSize,sizeof(iNSize));
sNeedEncrypt.erase();
sNeedEncrypt = sRandStr;
sNeedEncrypt += sSize;
sNeedEncrypt += sReplyMsg;
sNeedEncrypt += m_sReceiveId;
}
int WXBizMsgCrypt::SetOneFieldToXml(tinyxml2::XMLDocument * pDoc, tinyxml2::XMLNode* pXmlNode, const char * pcFieldName,
const std::string & value, bool bIsCdata)
{
if(!pDoc || !pXmlNode || !pcFieldName)
{
return -1;
}
tinyxml2::XMLElement * pFiledElement = pDoc->NewElement(pcFieldName);
if(NULL == pFiledElement)
{
return -1;
}
tinyxml2::XMLText * pText = pDoc->NewText(value.c_str());
if(NULL == pText)
{
return -1;
}
pText->SetCData(bIsCdata);
pFiledElement->LinkEndChild(pText);
pXmlNode->LinkEndChild(pFiledElement);
return 0;
}
int WXBizMsgCrypt::GenReturnXml(const std::string & sEncryptMsg, const std::string & sSignature, const std::string & sTimeStamp,
const std::string & sNonce, std::string & sResult)
{
tinyxml2::XMLPrinter oPrinter;
tinyxml2::XMLNode* pXmlNode = NULL;
tinyxml2::XMLDocument * pDoc = new tinyxml2::XMLDocument();
if(NULL == pDoc)
{
return -1;
}
pXmlNode = pDoc->InsertEndChild( pDoc->NewElement( "xml" ) );
if(NULL == pXmlNode)
{
DELETE_PTR(pDoc);
return -1;
}
if(0 != SetOneFieldToXml(pDoc,pXmlNode,"Encrypt",sEncryptMsg,true))
{
DELETE_PTR(pDoc);
return -1;
}
if(0 != SetOneFieldToXml(pDoc,pXmlNode,"MsgSignature",sSignature,true))
{
DELETE_PTR(pDoc);
return -1;
}
if(0 != SetOneFieldToXml(pDoc,pXmlNode,"TimeStamp",sTimeStamp,true))
{
DELETE_PTR(pDoc);
return -1;
}
if(0 != SetOneFieldToXml(pDoc,pXmlNode,"Nonce",sNonce,true))
{
DELETE_PTR(pDoc);
return -1;
}
//转成string
pDoc->Accept(&oPrinter);
sResult = oPrinter.CStr();
DELETE_PTR(pDoc);
return 0;
}
}