forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptManager.cpp
532 lines (440 loc) · 13.4 KB
/
CryptManager.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
#include "global.h"
#include "CryptManager.h"
#include "RageUtil.h"
#include "RageUtil.hpp"
#include "RageLog.h"
#include "RageFile.h"
#include "RageFileManager.h"
#include "CryptHelpers.h"
#include "LuaBinding.h"
#include "LuaReference.h"
#include "LuaManager.h"
#include "tomcrypt.h"
using std::vector;
CryptManager* CRYPTMAN = nullptr; // global and accessible from anywhere in our program
static const std::string PRIVATE_KEY_PATH = "Data/private.rsa";
static const std::string PUBLIC_KEY_PATH = "Data/public.rsa";
static const std::string ALTERNATE_PUBLIC_KEY_DIR = "Data/keys/";
static bool HashFile( RageFileBasic &f, unsigned char buf_hash[20], int iHash )
{
hash_state hash;
int iRet = hash_descriptor[iHash].init( &hash );
ASSERT_M( iRet == CRYPT_OK, error_to_string(iRet) );
std::string s;
while( !f.AtEOF() )
{
s.erase();
if( f.Read(s, 1024*4) == -1 )
{
LOG->Warn( "Error reading %s: %s", f.GetDisplayPath().c_str(), f.GetError().c_str() );
hash_descriptor[iHash].done( &hash, buf_hash );
return false;
}
iRet = hash_descriptor[iHash].process( &hash, (const unsigned char *) s.data(), s.size() );
ASSERT_M( iRet == CRYPT_OK, error_to_string(iRet) );
}
iRet = hash_descriptor[iHash].done( &hash, buf_hash );
ASSERT_M( iRet == CRYPT_OK, error_to_string(iRet) );
return true;
}
#if defined(DISABLE_CRYPTO)
CryptManager::CryptManager() { }
CryptManager::~CryptManager() { }
void CryptManager::GenerateRSAKey( unsigned int keyLength, std::string privFilename, std::string pubFilename ) { }
void CryptManager::SignFileToFile( std::string sPath, std::string sSignatureFile ) { }
bool CryptManager::VerifyFileWithFile( std::string sPath, std::string sSignatureFile, std::string sPublicKeyFile ) { return true; }
bool CryptManager::VerifyFileWithFile( std::string sPath, std::string sSignatureFile )
{
return true;
}
void CryptManager::GetRandomBytes( void *pData, int iBytes )
{
uint8_t *pBuf = (uint8_t *) pData;
while( iBytes-- )
*pBuf++ = (uint8_t) RandomInt( 256 );
}
#else
static const int KEY_LENGTH = 1024;
#define MAX_SIGNATURE_SIZE_BYTES 1024 // 1 KB
/*
openssl genrsa -out testing -outform DER
openssl rsa -in testing -out testing2 -outform DER
openssl rsa -in testing -out testing2 -pubout -outform DER
openssl pkcs8 -inform DER -outform DER -nocrypt -in private.rsa -out private.der
*
*/
static PRNGWrapper *g_pPRNG = nullptr;
CryptManager::CryptManager()
{
// Register with Lua.
{
Lua *L = LUA->Get();
lua_pushstring( L, "CRYPTMAN" );
this->PushSelf( L );
lua_settable( L, LUA_GLOBALSINDEX );
LUA->Release( L );
}
ltc_mp = ltm_desc;
g_pPRNG = new PRNGWrapper( &yarrow_desc );
}
void CryptManager::GenerateGlobalKeys()
{
//
// generate keys if none are available
//
bool bGenerate = false;
RSAKeyWrapper key;
std::string sKey;
std::string sError;
if( !DoesFileExist(PRIVATE_KEY_PATH) ||
!GetFileContents(PRIVATE_KEY_PATH, sKey) ||
!key.Load(sKey, sError) )
bGenerate = true;
if( !sError.empty() )
LOG->Warn( "Error loading RSA key: %s", sError.c_str() );
sError.clear();
if( !DoesFileExist(PUBLIC_KEY_PATH) ||
!GetFileContents(PUBLIC_KEY_PATH, sKey) ||
!key.Load(sKey, sError) )
bGenerate = true;
if( !sError.empty() )
LOG->Warn( "Error loading RSA key: %s", sError.c_str() );
if( bGenerate )
{
LOG->Warn( "Keys missing or failed to load. Generating new keys" );
GenerateRSAKeyToFile( KEY_LENGTH, PRIVATE_KEY_PATH, PUBLIC_KEY_PATH );
}
}
CryptManager::~CryptManager()
{
Rage::safe_delete( g_pPRNG );
// Unregister with Lua.
LUA->UnsetGlobal( "CRYPTMAN" );
}
static bool WriteFile( std::string sFile, std::string sBuf )
{
RageFile output;
if( !output.Open(sFile, RageFile::WRITE) )
{
LOG->Warn( "WriteFile: opening %s failed: %s", sFile.c_str(), output.GetError().c_str() );
return false;
}
if( output.Write(sBuf) == -1 || output.Flush() == -1 )
{
LOG->Warn( "WriteFile: writing %s failed: %s", sFile.c_str(), output.GetError().c_str() );
output.Close();
FILEMAN->Remove( sFile );
return false;
}
return true;
}
void CryptManager::GenerateRSAKey( unsigned int keyLength, std::string &sPrivKey, std::string &sPubKey )
{
int iRet;
rsa_key key;
iRet = rsa_make_key( &g_pPRNG->m_PRNG, g_pPRNG->m_iPRNG, keyLength / 8, 65537, &key );
if( iRet != CRYPT_OK )
{
LOG->Warn( "GenerateRSAKey(%i) error: %s", keyLength, error_to_string(iRet) );
return;
}
unsigned char buf[1024];
unsigned long iSize = sizeof(buf);
iRet = rsa_export( buf, &iSize, PK_PUBLIC, &key );
if( iRet != CRYPT_OK )
{
LOG->Warn( "Export error: %s", error_to_string(iRet) );
return;
}
sPubKey = std::string( (const char *) buf, iSize );
iSize = sizeof(buf);
iRet = rsa_export( buf, &iSize, PK_PRIVATE, &key );
if( iRet != CRYPT_OK )
{
LOG->Warn( "Export error: %s", error_to_string(iRet) );
return;
}
sPrivKey = std::string( (const char *) buf, iSize );
}
void CryptManager::GenerateRSAKeyToFile( unsigned int keyLength, std::string privFilename, std::string pubFilename )
{
std::string sPrivKey, sPubKey;
GenerateRSAKey( keyLength, sPrivKey, sPubKey );
if( !WriteFile(pubFilename, sPubKey) )
return;
if( !WriteFile(privFilename, sPrivKey) )
{
FILEMAN->Remove( privFilename );
return;
}
}
void CryptManager::SignFileToFile( std::string sPath, std::string sSignatureFile )
{
std::string sPrivFilename = PRIVATE_KEY_PATH;
if( sSignatureFile.empty() )
sSignatureFile = sPath + SIGNATURE_APPEND;
std::string sPrivKey;
if( !GetFileContents(sPrivFilename, sPrivKey) )
return;
std::string sSignature;
if( !Sign(sPath, sSignature, sPrivKey) )
return;
WriteFile( sSignatureFile, sSignature );
}
bool CryptManager::Sign( std::string sPath, std::string &sSignatureOut, std::string sPrivKey )
{
if( !IsAFile(sPath) )
{
LOG->Trace( "SignFileToFile: \"%s\" doesn't exist", sPath.c_str() );
return false;
}
RageFile file;
if( !file.Open(sPath) )
{
LOG->Warn( "SignFileToFile: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() );
return false;
}
RSAKeyWrapper key;
std::string sError;
if( !key.Load(sPrivKey, sError) )
{
LOG->Warn( "Error loading RSA key: %s", sError.c_str() );
return false;
}
int iHash = register_hash( &sha1_desc );
ASSERT( iHash >= 0 );
unsigned char buf_hash[20];
if( !HashFile(file, buf_hash, iHash) )
return false;
unsigned char signature[256];
unsigned long signature_len = sizeof(signature);
int iRet = rsa_sign_hash_ex(
buf_hash, sizeof(buf_hash),
signature, &signature_len,
LTC_PKCS_1_V1_5, &g_pPRNG->m_PRNG, g_pPRNG->m_iPRNG, iHash,
0, &key.m_Key);
if( iRet != CRYPT_OK )
{
LOG->Warn( "SignFileToFile error: %s", error_to_string(iRet) );
return false;
}
sSignatureOut.assign( (const char *) signature, signature_len );
return true;
}
bool CryptManager::VerifyFileWithFile( std::string sPath, std::string sSignatureFile )
{
if( VerifyFileWithFile(sPath, sSignatureFile, PUBLIC_KEY_PATH) )
{
return true;
}
vector<std::string> asKeys;
GetDirListing( ALTERNATE_PUBLIC_KEY_DIR, asKeys, false, true );
for (auto const &sKey: asKeys)
{
LOG->Trace( "Trying alternate key \"%s\" ...", sKey.c_str() );
if( VerifyFileWithFile(sPath, sSignatureFile, sKey) )
{
return true;
}
}
return false;
}
bool CryptManager::VerifyFileWithFile( std::string sPath, std::string sSignatureFile, std::string sPublicKeyFile )
{
if( sSignatureFile.empty() )
sSignatureFile = sPath + SIGNATURE_APPEND;
std::string sPublicKey;
if( !GetFileContents(sPublicKeyFile, sPublicKey) )
return false;
int iBytes = FILEMAN->GetFileSizeInBytes( sSignatureFile );
if( iBytes > MAX_SIGNATURE_SIZE_BYTES )
return false;
std::string sSignature;
if( !GetFileContents(sSignatureFile, sSignature) )
return false;
RageFile file;
if( !file.Open(sPath) )
{
LOG->Warn( "Verify: open(%s) failed: %s", sPath.c_str(), file.GetError().c_str() );
return false;
}
return Verify( file, sSignature, sPublicKey );
}
bool CryptManager::Verify( RageFileBasic &file, std::string sSignature, std::string sPublicKey )
{
RSAKeyWrapper key;
std::string sError;
if( !key.Load(sPublicKey, sError) )
{
LOG->Warn( "Error loading RSA key: %s", sError.c_str() );
return false;
}
int iHash = register_hash( &sha1_desc );
ASSERT( iHash >= 0 );
unsigned char buf_hash[20];
HashFile( file, buf_hash, iHash );
int iMatch;
int iRet = rsa_verify_hash_ex( (const unsigned char *) sSignature.data(), sSignature.size(),
buf_hash, sizeof(buf_hash),
LTC_PKCS_1_EMSA, iHash, 0, &iMatch, &key.m_Key );
if( iRet != CRYPT_OK )
{
LOG->Warn( "Verify(%s) failed: %s", file.GetDisplayPath().c_str(), error_to_string(iRet) );
return false;
}
if( !iMatch )
{
LOG->Warn( "Verify(%s) failed: signature mismatch", file.GetDisplayPath().c_str() );
return false;
}
return true;
}
void CryptManager::GetRandomBytes( void *pData, int iBytes )
{
int iRet = prng_descriptor[g_pPRNG->m_iPRNG].read( (unsigned char *) pData, iBytes, &g_pPRNG->m_PRNG );
ASSERT( iRet == iBytes );
}
#endif
std::string CryptManager::GetMD5ForFile( std::string fn )
{
RageFile file;
if( !file.Open( fn, RageFile::READ ) )
{
LOG->Warn( "GetMD5: Failed to open file '%s'", fn.c_str() );
return std::string();
}
int iHash = register_hash( &md5_desc );
ASSERT( iHash >= 0 );
unsigned char digest[16];
HashFile( file, digest, iHash );
return std::string( (const char *) digest, sizeof(digest) );
}
std::string CryptManager::GetMD5ForString( std::string sData )
{
unsigned char digest[16];
int iHash = register_hash( &md5_desc );
hash_state hash;
hash_descriptor[iHash].init( &hash );
hash_descriptor[iHash].process( &hash, (const unsigned char *) sData.data(), sData.size() );
hash_descriptor[iHash].done( &hash, digest );
return std::string( (const char *) digest, sizeof(digest) );
}
std::string CryptManager::GetSHA1ForString( std::string sData )
{
unsigned char digest[20];
int iHash = register_hash( &sha1_desc );
hash_state hash;
hash_descriptor[iHash].init( &hash );
hash_descriptor[iHash].process( &hash, (const unsigned char *) sData.data(), sData.size() );
hash_descriptor[iHash].done( &hash, digest );
return std::string( (const char *) digest, sizeof(digest) );
}
std::string CryptManager::GetSHA1ForFile( std::string fn )
{
RageFile file;
if( !file.Open( fn, RageFile::READ ) )
{
LOG->Warn( "GetSHA1: Failed to open file '%s'", fn.c_str() );
return std::string();
}
int iHash = register_hash( &sha1_desc );
ASSERT( iHash >= 0 );
unsigned char digest[20];
HashFile( file, digest, iHash );
return std::string( (const char *) digest, sizeof(digest) );
}
std::string CryptManager::GetPublicKeyFileName()
{
return PUBLIC_KEY_PATH;
}
/* Generate a version 4 random UUID. */
std::string CryptManager::GenerateRandomUUID()
{
uint32_t buf[4];
CryptManager::GetRandomBytes( buf, sizeof(buf) );
buf[1] &= 0xFFFF0FFF;
buf[1] |= 0x00004000;
buf[2] &= 0x0FFFFFFF;
buf[2] |= 0xA0000000;
return fmt::sprintf("%08x-%04x-%04x-%04x-%04x%08x",
buf[0],
buf[1] >> 16, buf[1] & 0xFFFF,
buf[2] >> 16, buf[2] & 0xFFFF,
buf[3]);
}
// lua start
#include "LuaBinding.h"
/** @brief Allow Lua to have access to the CryptManager. */
class LunaCryptManager: public Luna<CryptManager>
{
public:
static int MD5String( T* p, lua_State *L )
{
std::string md5out;
md5out = p->GetMD5ForString(SArg(1));
lua_pushlstring(L, md5out.c_str(), md5out.size());
return 1;
}
static int MD5File( T* p, lua_State *L )
{
std::string md5fout;
md5fout = p->GetMD5ForFile(SArg(1));
lua_pushlstring(L, md5fout.c_str(), md5fout.size());
return 1;
}
static int SHA1String( T* p, lua_State *L )
{
std::string sha1out;
sha1out = p->GetSHA1ForString(SArg(1));
lua_pushlstring(L, sha1out.c_str(), sha1out.size());
return 1;
}
static int SHA1File( T* p, lua_State *L )
{
std::string sha1fout;
sha1fout = p->GetSHA1ForFile(SArg(1));
lua_pushlstring(L, sha1fout.c_str(), sha1fout.size());
return 1;
}
static int GenerateRandomUUID( T* p, lua_State *L )
{
std::string uuidOut;
uuidOut = p->GenerateRandomUUID();
lua_pushlstring(L, uuidOut.c_str(), uuidOut.size());
return 1;
}
LunaCryptManager()
{
ADD_METHOD( MD5String );
ADD_METHOD( MD5File );
ADD_METHOD( SHA1String );
ADD_METHOD( SHA1File );
ADD_METHOD( GenerateRandomUUID );
}
};
LUA_REGISTER_CLASS( CryptManager )
// lua end
/*
* (c) 2004-2007 Chris Danford, Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/