Skip to content

Commit 3604c12

Browse files
committed
Consolidate PKI key generation logic into ensurePkiKeys method
1 parent 22d63fa commit 3604c12

File tree

6 files changed

+35
-58
lines changed

6 files changed

+35
-58
lines changed

src/graphics/draw/MenuHandler.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,9 @@ void menuHandler::LoraRegionPicker(uint32_t duration)
163163
config.lora.region = selectedRegion;
164164
auto changes = SEGMENT_CONFIG;
165165

166-
// FIXME: This should be a method consolidated with the same logic in the admin message as well
167-
// This is needed as we wait til picking the LoRa region to generate keys for the first time.
168166
#if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN || MESHTASTIC_EXCLUDE_PKI)
169-
if (!owner.is_licensed) {
170-
bool keygenSuccess = false;
171-
if (config.security.private_key.size == 32) {
172-
// public key is derived from private, so this will always have the same result.
173-
if (crypto->regeneratePublicKey(config.security.public_key.bytes, config.security.private_key.bytes)) {
174-
keygenSuccess = true;
175-
}
176-
177-
} else {
178-
LOG_INFO("Generate new PKI keys");
179-
crypto->generateKeyPair(config.security.public_key.bytes, config.security.private_key.bytes);
180-
keygenSuccess = true;
181-
}
182-
if (keygenSuccess) {
183-
config.security.public_key.size = 32;
184-
config.security.private_key.size = 32;
185-
owner.public_key.size = 32;
186-
memcpy(owner.public_key.bytes, config.security.public_key.bytes, 32);
187-
}
167+
if (crypto) {
168+
crypto->ensurePkiKeys(config.security, owner);
188169
}
189170
#endif
190171
config.lora.tx_enabled = true;

src/graphics/niche/InkHUD/Applets/System/Menu/MenuApplet.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,8 @@ static void applyLoRaRegion(meshtastic_Config_LoRaConfig_RegionCode region)
177177
auto changes = SEGMENT_CONFIG;
178178

179179
#if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN || MESHTASTIC_EXCLUDE_PKI)
180-
if (!owner.is_licensed) {
181-
bool keygenSuccess = false;
182-
183-
if (config.security.private_key.size == 32) {
184-
if (crypto->regeneratePublicKey(config.security.public_key.bytes, config.security.private_key.bytes)) {
185-
keygenSuccess = true;
186-
}
187-
} else {
188-
crypto->generateKeyPair(config.security.public_key.bytes, config.security.private_key.bytes);
189-
keygenSuccess = true;
190-
}
191-
192-
if (keygenSuccess) {
193-
config.security.public_key.size = 32;
194-
config.security.private_key.size = 32;
195-
owner.public_key.size = 32;
196-
memcpy(owner.public_key.bytes, config.security.public_key.bytes, 32);
197-
}
180+
if (crypto) {
181+
crypto->ensurePkiKeys(config.security, owner);
198182
}
199183
#endif
200184

src/mesh/CryptoEngine.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ bool CryptoEngine::regeneratePublicKey(uint8_t *pubKey, uint8_t *privKey)
6161
}
6262
return true;
6363
}
64+
65+
bool CryptoEngine::ensurePkiKeys(meshtastic_Config_SecurityConfig &security, meshtastic_User &user)
66+
{
67+
if (user.is_licensed) {
68+
return false;
69+
}
70+
71+
bool keygenSuccess = false;
72+
if (security.private_key.size == 32) {
73+
if (regeneratePublicKey(security.public_key.bytes, security.private_key.bytes)) {
74+
keygenSuccess = true;
75+
}
76+
} else {
77+
LOG_INFO("Generate new PKI keys");
78+
generateKeyPair(security.public_key.bytes, security.private_key.bytes);
79+
keygenSuccess = true;
80+
}
81+
82+
if (keygenSuccess) {
83+
security.public_key.size = 32;
84+
security.private_key.size = 32;
85+
user.public_key.size = 32;
86+
memcpy(user.public_key.bytes, security.public_key.bytes, 32);
87+
}
88+
89+
return keygenSuccess;
90+
}
6491
#endif
6592

6693
/**

src/mesh/CryptoEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CryptoEngine
3636
#if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN)
3737
virtual void generateKeyPair(uint8_t *pubKey, uint8_t *privKey);
3838
virtual bool regeneratePublicKey(uint8_t *pubKey, uint8_t *privKey);
39+
virtual bool ensurePkiKeys(meshtastic_Config_SecurityConfig &security, meshtastic_User &user);
3940

4041
#endif
4142
void setDHPrivateKey(uint8_t *_private_key);

src/modules/AdminModule.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -776,24 +776,8 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c, bool fromOthers)
776776
// If we're setting region for the first time, init the region and regenerate the keys
777777
if (isRegionUnset && validatedLora.region > meshtastic_Config_LoRaConfig_RegionCode_UNSET) {
778778
#if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN || MESHTASTIC_EXCLUDE_PKI)
779-
if (!owner.is_licensed) {
780-
bool keygenSuccess = false;
781-
if (config.security.private_key.size == 32) {
782-
if (crypto->regeneratePublicKey(config.security.public_key.bytes,
783-
config.security.private_key.bytes)) {
784-
keygenSuccess = true;
785-
}
786-
} else {
787-
LOG_INFO("Generate new PKI keys");
788-
crypto->generateKeyPair(config.security.public_key.bytes, config.security.private_key.bytes);
789-
keygenSuccess = true;
790-
}
791-
if (keygenSuccess) {
792-
config.security.public_key.size = 32;
793-
config.security.private_key.size = 32;
794-
owner.public_key.size = 32;
795-
memcpy(owner.public_key.bytes, config.security.public_key.bytes, 32);
796-
}
779+
if (crypto) {
780+
crypto->ensurePkiKeys(config.security, owner);
797781
}
798782
#endif
799783
// new region is valid and we're coming from an unset region, so enable tx

0 commit comments

Comments
 (0)