Skip to content

Commit

Permalink
[add] Fetch app name/version from env at startup then stick with it
Browse files Browse the repository at this point in the history
  • Loading branch information
lpascal-ledger committed Dec 20, 2023
1 parent 6099d21 commit f8b0571
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 56 deletions.
55 changes: 2 additions & 53 deletions src/bolos/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
70 changes: 67 additions & 3 deletions src/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 '<name>:<version>' 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();
}
12 changes: 12 additions & 0 deletions src/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include <stdint.h>

#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;
Expand All @@ -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();

0 comments on commit f8b0571

Please sign in to comment.