From afc45588fa47a6db7161ec56fd8708fbbdcb06d4 Mon Sep 17 00:00:00 2001 From: Tilen Komel Date: Fri, 23 Aug 2024 23:29:52 +0200 Subject: [PATCH] Fixed security key generation --- .../PageComponents/Config/Security.tsx | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/components/PageComponents/Config/Security.tsx b/src/components/PageComponents/Config/Security.tsx index 9c770f43..62c25e99 100644 --- a/src/components/PageComponents/Config/Security.tsx +++ b/src/components/PageComponents/Config/Security.tsx @@ -19,7 +19,7 @@ export const Security = (): JSX.Element => { ); const [privateKeyVisible, setPrivateKeyVisible] = useState(false); const [privateKeyBitCount, setPrivateKeyBitCount] = useState( - config.security?.privateKey.length ?? 16, + config.security?.privateKey.length ?? 32, ); const [privateKeyValidationText, setPrivateKeyValidationText] = useState(); @@ -60,7 +60,11 @@ export const Security = (): JSX.Element => { setPrivateKey(fromByteArray(privateKey)); setPublicKey(fromByteArray(publicKey)); - setPrivateKeyValidationText(undefined); + validatePass( + fromByteArray(privateKey), + privateKeyBitCount, + setPrivateKeyValidationText, + ); }; const adminKeyClickEvent = () => { @@ -82,19 +86,31 @@ export const Security = (): JSX.Element => { value: React.SetStateAction, ) => void, ) => { - if (input.length % 4 !== 0 || toByteArray(input).length !== count) { + try { + if (input.length % 4 !== 0 || toByteArray(input).length !== count) { + setValidationText(`Please enter a valid ${count * 8} bit PSK.`); + } else { + setValidationText(undefined); + } + } catch (e) { + console.error(e); setValidationText(`Please enter a valid ${count * 8} bit PSK.`); - } else { - setValidationText(undefined); } }; const privateKeyInputChangeEvent = ( e: React.ChangeEvent, ) => { - const psk = e.currentTarget?.value; - setPrivateKey(psk); - validatePass(psk, privateKeyBitCount, setPrivateKeyValidationText); + const privateKeyB64String = e.currentTarget?.value; + setPrivateKey(privateKeyB64String); + validatePass( + privateKeyB64String, + privateKeyBitCount, + setPrivateKeyValidationText, + ); + + const publicKey = getX25519PublicKey(toByteArray(privateKeyB64String)); + setPublicKey(fromByteArray(publicKey)); }; const adminKeyInputChangeEvent = (e: React.ChangeEvent) => { @@ -136,7 +152,7 @@ export const Security = (): JSX.Element => { name: "privateKey", label: "Private Key", description: "Used to create a shared key with a remote device", - bits: [{ text: "128 bit", value: "16", key: "bit128" }], + bits: [{ text: "256 bit", value: "32", key: "bit256" }], validationText: privateKeyValidationText, devicePSKBitCount: privateKeyBitCount, inputChange: privateKeyInputChangeEvent,