From f8b057180ab6706696f99d2d347d3601de89adb5 Mon Sep 17 00:00:00 2001 From: Lucas PASCAL Date: Wed, 20 Dec 2023 09:58:57 +0100 Subject: [PATCH] [add] Fetch app name/version from env at startup then stick with it --- src/bolos/os.c | 55 ++----------------------------------- src/environment.c | 70 +++++++++++++++++++++++++++++++++++++++++++++-- src/environment.h | 12 ++++++++ 3 files changed, 81 insertions(+), 56 deletions(-) diff --git a/src/bolos/os.c b/src/bolos/os.c index c6319eb2..dc28b364 100644 --- a/src/bolos/os.c +++ b/src/bolos/os.c @@ -4,14 +4,12 @@ #include "emulate.h" #include "svc.h" +#include "environment.h" #define OS_SETTING_PLANEMODE_OLD 5 #define OS_SETTING_PLANEMODE_NEW 6 #define OS_SETTING_SOUND 9 -#define BOLOS_TAG_APPNAME 0x01 -#define BOLOS_TAG_APPVERSION 0x02 - #undef PATH_MAX #define PATH_MAX 1024 @@ -69,56 +67,7 @@ unsigned long sys_os_registry_get_current_app_tag(unsigned int tag, uint8_t *buffer, size_t length) { - const char *name; - const char *version; - const char *str; - char *str_dup = NULL; - - if (length < 1) { - return 0; - } - - name = "app"; - version = "1.33.7"; - - str = getenv("SPECULOS_APPNAME"); - if (str == NULL) { - str = getenv("SPECULOS_DETECTED_APPNAME"); - } - - if (str != NULL) { - str_dup = strdup(str); - if (str_dup != NULL) { - char *p = strstr(str_dup, ":"); - if (p != NULL) { - *p = '\x00'; - name = str_dup; - version = p + 1; - } - } - } - - switch (tag) { - case BOLOS_TAG_APPNAME: - strncpy((char *)buffer, name, length); - length = MIN(length, strlen(name)); - break; - case BOLOS_TAG_APPVERSION: - strncpy((char *)buffer, version, length); - length = MIN(length, strlen(version)); - break; - default: - length = 0; - break; - } - - buffer[length] = '\x00'; - - if (str_dup != NULL) { - free(str_dup); - } - - return length; + return env_get_app_tag((char *)buffer, length, tag); } unsigned long sys_os_lib_call(unsigned long *call_parameters) diff --git a/src/environment.c b/src/environment.c index e1e3650e..aff2658a 100644 --- a/src/environment.c +++ b/src/environment.c @@ -30,6 +30,13 @@ static struct { uint8_t seed[MAX_SEED_SIZE]; } actual_seed = { 0 }; +/* APP NAME and VERSION */ +static const char *APP_NAME_VERSION_ENV_NAME = "SPECULOS_APPNAME"; +static const char *APP_NAME_VERSION_ENV_NAME_BKP = "SPECULOS_DETECTED_APPNAME"; + +static env_sized_name_t app_name = { 4, "app\0" }; +static env_sized_name_t app_version = { 7, "1.33.7\0" }; + /* RNG VARIABLES */ static const char *RNG_ENV_NAME = "RNG_SEED"; @@ -294,9 +301,66 @@ env_user_certificate_t* env_get_user_certificate(unsigned int index) } } +static void env_init_app_name_version() { + char *str; + + str = getenv(APP_NAME_VERSION_ENV_NAME); + if (str == NULL) { + str = getenv(APP_NAME_VERSION_ENV_NAME_BKP); + } + + if (str == NULL) { + warnx("using default app name & version"); + fprintf(stderr, "[*] Default app name: '%s'\n", app_name.name); + fprintf(stderr, "[*] Default app version: '%s'\n", app_version.name); + return; + } + + char *char_ptr = strchr(str, ':'); + if (char_ptr == NULL) { + warnx("Invalid ':' format in env variable '%s', falling back to default.", str); + fprintf(stderr, "[*] Default app name: '%s'\n", app_name.name); + fprintf(stderr, "[*] Default app version: '%s'\n", app_version.name); + return; + } + + // + 1 to include trailing '\0' + app_name.length = (char_ptr - str) + 1; + app_version.length = (strlen(str) - (size_t)(app_name.length + 1)) + 1 + 1; + str[app_name.length - 1] = '\0'; + strncpy(app_name.name, str, app_name.length); + strncpy(app_version.name, str + app_name.length, app_version.length); + + fprintf(stderr, "[*] Env app name: '%s'\n", app_name.name); + fprintf(stderr, "[*] Env app version: '%s'\n", app_version.name); +} + +size_t env_get_app_tag(char* dst, size_t length, BOLOS_TAG tag) { + env_sized_name_t *field; + switch (tag) { + case BOLOS_TAG_APPNAME: + field = &app_name; + break; + case BOLOS_TAG_APPVERSION: + field = &app_version; + break; + default: + return 0; + } + if (length < field->length) { + warnx("Providing length to copy env variable too small: asked for %u, needs %u", + length, field->length); + return 0; + } + + strncpy(dst, field->name, length); + return field->length < length ? field->length : length; +} + void init_environment() { - env_init_seed(); - env_init_rng(); - env_init_endorsement(); + env_init_seed(); + env_init_rng(); + env_init_endorsement(); + env_init_app_name_version(); } diff --git a/src/environment.h b/src/environment.h index e9eeed5c..a0581771 100644 --- a/src/environment.h +++ b/src/environment.h @@ -3,6 +3,17 @@ #include #define MAX_SEED_SIZE 64 +#define MAX_STRING_SIZE 128 + +typedef enum { + BOLOS_TAG_APPNAME = 0x01, + BOLOS_TAG_APPVERSION = 0x02 +} BOLOS_TAG; + +typedef struct { + size_t length; + char name[MAX_STRING_SIZE]; +} env_sized_name_t; typedef struct { uint8_t length; @@ -13,5 +24,6 @@ size_t env_get_seed(uint8_t *seed, size_t max_size); unsigned int env_get_rng(); cx_ecfp_private_key_t* env_get_user_private_key(unsigned int index); env_user_certificate_t* env_get_user_certificate(unsigned int index); +size_t env_get_app_tag(char* dst, size_t length, BOLOS_TAG tag); void init_environment();