Skip to content

Commit 997a888

Browse files
committed
UPBGE: Add safety checks, fix potential buffer overflow and warnings
1 parent 84ee4f9 commit 997a888

File tree

5 files changed

+191
-35
lines changed

5 files changed

+191
-35
lines changed

CMakeLists.txt

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,28 @@ execute_process(COMMAND openssl rand -hex 16
1414
OUTPUT_VARIABLE GENERATED_SALT
1515
OUTPUT_STRIP_TRAILING_WHITESPACE)
1616

17-
# -- Show values --
18-
message(STATUS "Generated Password: ${GENERATED_PW}")
19-
message(STATUS "Generated Salt HEX: ${GENERATED_SALT}")
20-
2117
# -- Convert salt hex string to comma-separated bytes --
2218
string(REGEX REPLACE "([0-9a-fA-F][0-9a-fA-F])" "0x\\1, " SALT_BYTES "${GENERATED_SALT}")
2319
string(REGEX REPLACE ", $" "" SALT_BYTES "${SALT_BYTES}") # remove trailing comma
2420

2521
add_compile_definitions(ENCRYPTION_PASSWORD="${GENERATED_PW}")
2622
add_compile_definitions(ENCRYPTION_SALT=${SALT_BYTES})
2723

28-
#add_compile_definitions(ENCRYPTION_PASSWORD="mi_contraseña_secreta")
24+
# -- To test own password and salt, uncomment the next lines --
25+
#add_compile_definitions(ENCRYPTION_PASSWORD="mi_secret_password")
2926
#add_compile_definitions(ENCRYPTION_SALT="0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F")
3027

28+
# -- Show values --
29+
message(STATUS "Generated Password: ${GENERATED_PW}")
30+
message(STATUS "Generated Salt HEX: ${GENERATED_SALT}")
3131
message(STATUS "Salt bytes: ${SALT_BYTES}")
32-
message(STATUS "ENCRYPTION_PASSWORD: ${ENCRYPTION_PASSWORD}")
33-
message(STATUS "ENCRYPTION_SALT: ${ENCRYPTION_SALT}")
3432

3533
add_subdirectory(aes)
3634

37-
# Configuración para evitar conflictos de CRT
38-
if (MSVC)
39-
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
40-
endif()
41-
4235
# --- upbge_binarycrypt_tool ---
43-
set(RAYLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../Libs/raylib-5.5_win64_msvc16/include/" CACHE PATH "Path to raylib include")
44-
set(RAYLIB_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../Libs/raylib-5.5_win64_msvc16/lib/" CACHE PATH "Path to raylib lib")
45-
4636
add_executable(upbge_binarycrypt_tool
4737
upbge_binarycrypt_tool/main_tool.c
4838
)
49-
5039
target_include_directories(upbge_binarycrypt_tool PRIVATE ${RAYLIB_INCLUDE_DIR})
5140
target_link_directories(upbge_binarycrypt_tool PRIVATE ${RAYLIB_LIB_DIR})
5241
target_link_libraries(upbge_binarycrypt_tool PRIVATE raylib tiny_aes winmm)

cmake/Findraylib.cmake

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
find_path(RAYLIB_INCLUDE_DIR raylib.h
2+
HINTS
3+
$ENV{RAYLIB_INCLUDE_DIR}
4+
$ENV{RAYLIB_DIR}/include
5+
${CMAKE_PREFIX_PATH}/include
6+
${CMAKE_CURRENT_SOURCE_DIR}/../Libs/raylib-5.5_win64_msvc16/include
7+
ENV PATH
8+
PATH_SUFFIXES include
9+
)
10+
11+
find_path (RAYLIB_LIB_DIR raylib.lib
12+
HINTS
13+
$ENV{RAYLIB_LIB_DIR}
14+
$ENV{RAYLIB_DIR}/lib
15+
${CMAKE_PREFIX_PATH}/lib
16+
${CMAKE_CURRENT_SOURCE_DIR}/../Libs/raylib-5.5_win64_msvc16/lib
17+
ENV PATH
18+
PATH_SUFFIXES lib
19+
)
20+
21+
find_library(RAYLIB_LIBRARY NAMES raylib
22+
HINTS
23+
$ENV{RAYLIB_LIBRARY_DIR}
24+
$ENV{RAYLIB_DIR}/lib
25+
${CMAKE_PREFIX_PATH}/lib
26+
${CMAKE_CURRENT_SOURCE_DIR}/../Libs/raylib-5.5_win64_msvc16/lib
27+
ENV PATH
28+
PATH_SUFFIXES lib
29+
)
30+
31+
include(FindPackageHandleStandardArgs)
32+
find_package_handle_standard_args(raylib DEFAULT_MSG RAYLIB_LIBRARY RAYLIB_INCLUDE_DIR)
33+
34+
if(raylib_FOUND)
35+
add_library(raylib::raylib UNKNOWN IMPORTED)
36+
set_target_properties(raylib::raylib PROPERTIES
37+
IMPORTED_LOCATION "${RAYLIB_LIBRARY}"
38+
INTERFACE_INCLUDE_DIRECTORIES "${RAYLIB_INCLUDE_DIR}"
39+
)
40+
endif()

cmake/raylib-config.cmake

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# - Try to find raylib
2+
# Options:
3+
# raylib_USE_STATIC_LIBS - ON by default
4+
# raylib_VERBOSE - OFF by default
5+
# Once done, this defines a raylib target that can be passed to
6+
# target_link_libraries as well as following variables:
7+
#
8+
# raylib_FOUND - System has raylib installed
9+
# raylib_INCLUDE_DIRS - The include directories for the raylib header(s)
10+
# raylib_LIBRARIES - The libraries needed to use raylib
11+
# raylib_LDFLAGS - The linker flags needed with raylib
12+
# raylib_DEFINITIONS - Compiler switches required for using raylib
13+
14+
option(raylib_USE_STATIC_LIBS "Use static libs" ON)
15+
option(raylib_VERBOSE "Show raylib verbose messages" OFF)
16+
17+
if (NOT TARGET raylib)
18+
set(XPREFIX PC_RAYLIB)
19+
20+
find_package(PkgConfig QUIET)
21+
pkg_check_modules(${XPREFIX} QUIET raylib)
22+
23+
if (raylib_USE_STATIC_LIBS)
24+
set(XPREFIX ${XPREFIX}_STATIC)
25+
endif()
26+
27+
set(raylib_DEFINITIONS ${${XPREFIX}_CFLAGS})
28+
29+
find_path(raylib_INCLUDE_DIR
30+
NAMES raylib.h
31+
HINTS ${${XPREFIX}_INCLUDE_DIRS} ${raylib_DIR}/../../../include/
32+
)
33+
34+
35+
if (raylib_USE_STATIC_LIBS)
36+
set(RAYLIB_NAMES libraylib.a raylib.lib)
37+
else ()
38+
set(RAYLIB_NAMES raylib)
39+
endif ()
40+
41+
find_library(raylib_LIBRARY
42+
NAMES ${RAYLIB_NAMES}
43+
HINTS ${${XPREFIX}_LIBRARY_DIRS} ${raylib_DIR}/../../
44+
)
45+
46+
set(raylib_LIBRARIES ${raylib_LIBRARY})
47+
set(raylib_INCLUDE_DIRS ${raylib_INCLUDE_DIR})
48+
set(raylib_LDFLAGS ${${XPREFIX}_LDFLAGS})
49+
50+
include(FindPackageHandleStandardArgs)
51+
find_package_handle_standard_args(raylib DEFAULT_MSG
52+
raylib_LIBRARY
53+
raylib_INCLUDE_DIR
54+
)
55+
56+
mark_as_advanced(raylib_LIBRARY raylib_INCLUDE_DIR)
57+
58+
if (raylib_USE_STATIC_LIBS)
59+
add_library(raylib STATIC IMPORTED GLOBAL)
60+
else()
61+
add_library(raylib SHARED IMPORTED GLOBAL)
62+
endif()
63+
string (REPLACE ";" " " raylib_LDFLAGS "${raylib_LDFLAGS}")
64+
65+
set_target_properties(raylib
66+
PROPERTIES
67+
IMPORTED_LOCATION "${raylib_LIBRARIES}"
68+
IMPORTED_IMPLIB "${raylib_LIBRARIES}"
69+
INTERFACE_INCLUDE_DIRECTORIES "${raylib_INCLUDE_DIRS}"
70+
INTERFACE_LINK_LIBRARIES "${raylib_LDFLAGS}"
71+
INTERFACE_COMPILE_OPTIONS "${raylib_DEFINITIONS}"
72+
)
73+
74+
if (raylib_VERBOSE)
75+
message(STATUS "raylib_FOUND: ${raylib_FOUND}")
76+
message(STATUS "raylib_INCLUDE_DIRS: ${raylib_INCLUDE_DIRS}")
77+
message(STATUS "raylib_LIBRARIES: ${raylib_LIBRARIES}")
78+
message(STATUS "raylib_LDFLAGS: ${raylib_LDFLAGS}")
79+
message(STATUS "raylib_DEFINITIONS: ${raylib_DEFINITIONS}")
80+
endif()
81+
endif()

upbge_binarycrypt_launcher/main_launcher.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <stdio.h>
22
#include <stdlib.h>
33
#include <string.h>
4-
#include <errno.h>
54
#include "aes.h"
65

76
#define SALT_LEN 16
@@ -18,9 +17,10 @@ void derive_key(uint8_t *key_out) {
1817
static int decrypt_file(const char *input, const char *output) {
1918
FILE *fin = fopen(input, "rb");
2019
if (!fin) {
21-
fprintf(stderr, "Error al abrir archivo de entrada '%s': %s\n", input, strerror(errno));
20+
fprintf(stderr, "Error opening input file '%s'\n", input);
2221
return 1;
2322
}
23+
2424
fseek(fin, 0, SEEK_END);
2525
long len = ftell(fin);
2626
rewind(fin);
@@ -83,7 +83,7 @@ static int decrypt_file(const char *input, const char *output) {
8383

8484
FILE *fout = fopen(output, "wb");
8585
if (!fout) {
86-
fprintf(stderr, "Error al abrir archivo de salida '%s': %s\n", output, strerror(errno));
86+
fprintf(stderr, "Error al abrir archivo de salida '%s'\n", output);
8787
free(buffer);
8888
return 1;
8989
}
@@ -116,7 +116,7 @@ int main(void) {
116116
const char *decrypted = "game_decrypted.blend";
117117

118118
if (decrypt_file(encrypted, decrypted)) {
119-
fprintf(stderr, "Error encriptando el archivo. No se lanza el blenderplayer: %s\n", strerror(errno));
119+
fprintf(stderr, "Error encriptando el archivo. No se lanza el blenderplayer\n");
120120
return 1;
121121
}
122122

upbge_binarycrypt_tool/main_tool.c

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,85 @@ static void derive_key(uint8_t *key_out) {
1515
}
1616
}
1717

18-
static void encrypt_file(const char *input, const char *output) {
19-
FILE *fin = fopen(input, "rb");
18+
static void encrypt_file(const char* input, const char* output) {
19+
// Validate input parameters
20+
if (!input || !output) return;
21+
22+
// Open input file
23+
FILE* fin = fopen(input, "rb");
2024
if (!fin) return;
21-
fseek(fin, 0, SEEK_END);
25+
26+
// Get file size with error checking
27+
if (fseek(fin, 0, SEEK_END)) {
28+
fclose(fin);
29+
return;
30+
}
31+
2232
long len = ftell(fin);
2333
rewind(fin);
2434

25-
uint8_t *buffer = malloc(len);
26-
fread(buffer, 1, len, fin);
35+
// Validate file length (must be positive)
36+
if (len <= 0) {
37+
fclose(fin);
38+
return;
39+
}
40+
41+
// Allocate buffer for file content
42+
uint8_t* buffer = malloc((size_t)len);
43+
if (!buffer) {
44+
fclose(fin);
45+
return;
46+
}
47+
48+
// Read entire file with complete verification
49+
size_t bytes_read = fread(buffer, 1, (size_t)len, fin);
2750
fclose(fin);
51+
if (bytes_read != (size_t)len) {
52+
free(buffer);
53+
return;
54+
}
2855

29-
// Calculate padding PKCS#7
56+
// Calculate PKCS#7 padding
3057
int padding = 16 - (len % 16);
31-
if (padding == 0) padding = 16; // Add complete padding always
58+
if (padding == 0) padding = 16; // Always add full block when aligned
59+
60+
// Check for potential integer overflow
61+
if (len > LONG_MAX - padding) {
62+
free(buffer);
63+
return;
64+
}
3265
long padded_len = len + padding;
3366

34-
uint8_t *buffer_padded = malloc(padded_len);
35-
memcpy(buffer_padded, buffer, len);
36-
memset(buffer_padded + len, padding, padding); // Filled with value 'padding'
67+
// Allocate and prepare padded buffer
68+
uint8_t* buffer_padded = malloc((size_t)padded_len);
69+
if (!buffer_padded) {
70+
free(buffer);
71+
return;
72+
}
73+
74+
// Copy original data and apply padding
75+
memcpy(buffer_padded, buffer, (size_t)len);
76+
memset(buffer_padded + len, (uint8_t)padding, (size_t)padding);
3777

78+
// Initialize AES context
3879
struct AES_ctx ctx;
3980
uint8_t key[32];
4081
derive_key(key);
4182
AES_init_ctx(&ctx, key);
4283

84+
// Encrypt each 16-byte block
4385
for (long i = 0; i < padded_len; i += 16) {
4486
AES_ECB_encrypt(&ctx, buffer_padded + i);
4587
}
4688

47-
FILE *fout = fopen(output, "wb");
48-
fwrite(buffer_padded, 1, padded_len, fout);
49-
fclose(fout);
89+
// Write encrypted data to output file
90+
FILE* fout = fopen(output, "wb");
91+
if (fout) {
92+
fwrite(buffer_padded, 1, (size_t)padded_len, fout);
93+
fclose(fout);
94+
}
5095

96+
// Clean up allocated memory
5197
free(buffer);
5298
free(buffer_padded);
5399
}
@@ -61,7 +107,7 @@ int main(void) {
61107
const char *inputFile = "game.blend";
62108
const char *outputFile = "game_encrypted.block";
63109

64-
Rectangle button = { screenWidth/2 - 100, screenHeight/2 - 25, 200, 50 };
110+
Rectangle button = { (float)(screenWidth / 2 - 100), (float)(screenHeight / 2 - 25), 200.0f, 50.0f };
65111

66112
SetTargetFPS(60);
67113
while (!WindowShouldClose()) {
@@ -77,7 +123,7 @@ int main(void) {
77123
} else {
78124
DrawRectangleRec(button, GRAY);
79125
}
80-
DrawText("Encrypt", button.x + 60, button.y + 15, 20, BLACK);
126+
DrawText("Encrypt", (int)button.x + 60, (int)button.y + 15, 20, BLACK);
81127

82128
EndDrawing();
83129
}

0 commit comments

Comments
 (0)