Skip to content

Commit 89469fc

Browse files
authored
Allows all 3 PKI keys to be added to userPrefs.h (#4969) and a tool. (#5368)
* more userPrefs.h Added PKI Admin keys to userPrefs.h * Update userPrefs.h Allows all 3 PKI keys to be added to userPrefs.h (#4969) * Update NodeDB.cpp Trunk * Update userPrefs.h Changed wording * Create base64_to_hex.py A little tool for converting base64 PKI Keys to decoded byte that userPrefs.h can understand. * more userPrefs.h Added PKI Admin keys to userPrefs.h * Update userPrefs.h Allows all 3 PKI keys to be added to userPrefs.h (#4969) * Update NodeDB.cpp Trunk * Update userPrefs.h Changed wording * Create base64_to_hex.py A little tool for converting base64 PKI Keys to decoded byte that userPrefs.h can understand.
1 parent a8357eb commit 89469fc

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

bin/base64_to_hex.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
import base64
3+
4+
def base64_to_hex_string(b64_string):
5+
try:
6+
# Decode the Base64 string to raw bytes
7+
decoded_bytes = base64.b64decode(b64_string)
8+
except Exception as e:
9+
raise ValueError(f"Invalid Base64 input: {e}")
10+
11+
# Check if the decoded result is exactly 32 bytes
12+
if len(decoded_bytes) != 32:
13+
raise ValueError("Decoded Base64 input must be exactly 32 bytes.")
14+
15+
# Convert each byte to its hex representation
16+
hex_values = [f"0x{byte:02x}" for byte in decoded_bytes]
17+
18+
# Join the formatted hex values with commas
19+
formatted_output = "{ " + ", ".join(hex_values) + " };"
20+
return formatted_output
21+
22+
if __name__ == "__main__":
23+
# Check if a Base64 string was provided in command line arguments
24+
if len(sys.argv) != 2:
25+
print("Usage: python script.py <base64-string>")
26+
sys.exit(1)
27+
28+
b64_string = sys.argv[1]
29+
try:
30+
formatted_hex = base64_to_hex_string(b64_string)
31+
print(formatted_hex)
32+
except ValueError as e:
33+
print(e)

src/mesh/NodeDB.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,30 @@ void NodeDB::installDefaultConfig(bool preserveKey = false)
407407
config.lora.ignore_mqtt = false;
408408
#endif
409409
#ifdef USERPREFS_USE_ADMIN_KEY
410-
memcpy(config.security.admin_key[0].bytes, USERPREFS_ADMIN_KEY, 32);
411-
config.security.admin_key[0].size = 32;
412-
config.security.admin_key_count = 1;
410+
// Initialize admin_key_count to zero
411+
byte numAdminKeys = 0;
412+
413+
// Check if USERPREFS_ADMIN_KEY_0 is non-empty
414+
if (sizeof(USERPREFS_ADMIN_KEY_0) > 0) {
415+
memcpy(config.security.admin_key[numAdminKeys].bytes, USERPREFS_ADMIN_KEY_0, 32);
416+
config.security.admin_key[numAdminKeys].size = 32;
417+
numAdminKeys++;
418+
}
419+
420+
// Check if USERPREFS_ADMIN_KEY_1 is non-empty
421+
if (sizeof(USERPREFS_ADMIN_KEY_1) > 0) {
422+
memcpy(config.security.admin_key[numAdminKeys].bytes, USERPREFS_ADMIN_KEY_1, 32);
423+
config.security.admin_key[numAdminKeys].size = 32;
424+
numAdminKeys++;
425+
}
426+
427+
// Check if USERPREFS_ADMIN_KEY_2 is non-empty
428+
if (sizeof(USERPREFS_ADMIN_KEY_2) > 0) {
429+
memcpy(config.security.admin_key[config.security.admin_key_count].bytes, USERPREFS_ADMIN_KEY_2, 32);
430+
config.security.admin_key[config.security.admin_key_count].size = 32;
431+
numAdminKeys++;
432+
}
433+
config.security.admin_key_count = numAdminKeys;
413434
#endif
414435
if (shouldPreserveKey) {
415436
config.security.private_key.size = 32;

userPrefs.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,19 @@ static unsigned char icon_bits[] = {
6868
0x98, 0x3F, 0xF0, 0x23, 0x00, 0xFC, 0x0F, 0xE0, 0x7F, 0x00, 0xFC, 0x03, 0x80, 0xFF, 0x01, 0xFC, 0x00, 0x00, 0x3E, 0x00, 0x70,
6969
0x00, 0x00, 0x1C, 0x00, 0x70, 0x00, 0x00, 0x1C, 0x00, 0x70, 0x00, 0x00, 0x1C, 0x00, 0x70, 0x00, 0x00, 0x1C, 0x00};
7070
*/
71+
72+
/*
73+
* PKI Admin keys.
74+
* If a Admin key is set with '{};'
75+
* then it will be ignored, a PKI key must have a size of 32.
76+
*/
7177
/*
7278
#define USERPREFS_USE_ADMIN_KEY 1
73-
static unsigned char USERPREFS_ADMIN_KEY[] = {0xcd, 0xc0, 0xb4, 0x3c, 0x53, 0x24, 0xdf, 0x13, 0xca, 0x5a, 0xa6,
74-
0x0c, 0x0d, 0xec, 0x85, 0x5a, 0x4c, 0xf6, 0x1a, 0x96, 0x04, 0x1a,
75-
0x3e, 0xfc, 0xbb, 0x8e, 0x33, 0x71, 0xe5, 0xfc, 0xff, 0x3c};
79+
static unsigned char USERPREFS_ADMIN_KEY_0[] = {0xcd, 0xc0, 0xb4, 0x3c, 0x53, 0x24, 0xdf, 0x13, 0xca, 0x5a, 0xa6,
80+
0x0c, 0x0d, 0xec, 0x85, 0x5a, 0x4c, 0xf6, 0x1a, 0x96, 0x04, 0x1a,
81+
0x3e, 0xfc, 0xbb, 0x8e, 0x33, 0x71, 0xe5, 0xfc, 0xff, 0x3c};
82+
static unsigned char USERPREFS_ADMIN_KEY_1[] = {};
83+
static unsigned char USERPREFS_ADMIN_KEY_2[] = {};
7684
*/
7785

7886
/*

0 commit comments

Comments
 (0)