Skip to content

Commit

Permalink
Anti-spoof
Browse files Browse the repository at this point in the history
  • Loading branch information
necropotame committed Oct 28, 2016
1 parent 00afcc8 commit e4133ae
Show file tree
Hide file tree
Showing 12 changed files with 1,027 additions and 145 deletions.
11 changes: 6 additions & 5 deletions bam.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ function build(settings)
-- build the small libraries
wavpack = Compile(settings, Collect("src/engine/external/wavpack/*.c"))
pnglite = Compile(settings, Collect("src/engine/external/pnglite/*.c"))
md5 = Compile(settings, "src/engine/external/md5/md5.c")

-- build game components
engine_settings = settings:Copy()
Expand Down Expand Up @@ -271,27 +272,27 @@ function build(settings)
tools = {}
for i,v in ipairs(tools_src) do
toolname = PathFilename(PathBase(v))
tools[i] = Link(settings, toolname, Compile(settings, v), engine, zlib, pnglite)
tools[i] = Link(settings, toolname, Compile(settings, v), engine, zlib, pnglite, md5)
end

-- build client, server, version server and master server
client_exe = Link(client_settings, "infclass_client", game_shared, game_client,
engine, client, game_editor, zlib, pnglite, wavpack,
engine, client, game_editor, zlib, pnglite, wavpack, md5,
client_link_other, client_osxlaunch)

server_exe = Link(server_settings, "infclass_srv", engine, server,
game_shared, game_server, zlib, server_link_other)
game_shared, game_server, zlib, server_link_other, md5)

serverlaunch = {}
if platform == "macosx" then
serverlaunch = Link(launcher_settings, "serverlaunch", server_osxlaunch)
end

versionserver_exe = Link(server_settings, "versionsrv", versionserver,
engine, zlib)
engine, zlib, md5)

masterserver_exe = Link(server_settings, "mastersrv", masterserver,
engine, zlib)
engine, zlib, md5)

-- make targets
c = PseudoTarget("client".."_"..settings.config_name, client_exe, client_depends)
Expand Down
64 changes: 64 additions & 0 deletions src/base/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,70 @@ unsigned str_quickhash(const char *str)
return hash;
}

struct SECURE_RANDOM_DATA
{
int initialized;
#if defined(CONF_FAMILY_WINDOWS)
HCRYPTPROV provider;
#else
IOHANDLE urandom;
#endif
};

static struct SECURE_RANDOM_DATA secure_random_data = { 0 };

int secure_random_init()
{
if(secure_random_data.initialized)
{
return 0;
}
#if defined(CONF_FAMILY_WINDOWS)
if(CryptAcquireContext(&secure_random_data.provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
secure_random_data.initialized = 1;
return 0;
}
else
{
return 1;
}
#else
secure_random_data.urandom = io_open("/dev/urandom", IOFLAG_READ);
if(secure_random_data.urandom)
{
secure_random_data.initialized = 1;
return 0;
}
else
{
return 1;
}
#endif
}

void secure_random_fill(void *bytes, unsigned length)
{
if(!secure_random_data.initialized)
{
dbg_msg("secure", "called secure_random_fill before secure_random_init");
dbg_break();
}
#if defined(CONF_FAMILY_WINDOWS)
if(!CryptGenRandom(secure_random_data.provider, length, bytes))
{
dbg_msg("secure", "CryptGenRandom failed, last_error=%d", GetLastError());
dbg_break();
}
#else
if(length != io_read(secure_random_data.urandom, bytes, length))
{
dbg_msg("secure", "io_read returned with a short read");
dbg_break();
}
#endif
}


#if defined(__cplusplus)
}
Expand Down
21 changes: 21 additions & 0 deletions src/base/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,27 @@ int str_utf8_encode(char *ptr, int chr);
*/
int str_utf8_check(const char *str);

/*
Function: secure_random_init
Initializes the secure random module.
You *MUST* check the return value of this function.
Returns:
0 - Initialization succeeded.
1 - Initialization failed.
*/
int secure_random_init();

/*
Function: secure_random_fill
Fills the buffer with the specified amount of random bytes.
Parameters:
buffer - Pointer to the start of the buffer.
length - Length of the buffer.
*/
void secure_random_fill(void *bytes, unsigned length);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit e4133ae

Please sign in to comment.