-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from OSGP/feature/FDP-1815
Feature/fdp 1815
- Loading branch information
Showing
19 changed files
with
234 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/PskExtractor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-FileCopyrightText: Contributors to the GXF project | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.gxf.crestdevicesimulator.simulator.response | ||
|
||
object PskExtractor { | ||
|
||
/** | ||
* Regex to split a valid PSK set command in 3 groups | ||
* Group 0 containing everything | ||
* Group 1 containing the next 16 chars after PSK: this is only the key | ||
* Group 2 containing the next 64 chars after the key this is only the hash | ||
*/ | ||
private val pskKeyHashSplitterRegex = "!PSK:([a-zA-Z0-9]{16})([a-zA-Z0-9]{64});PSK:[a-zA-Z0-9]{16}[a-zA-Z0-9]{64}SET".toRegex() | ||
|
||
fun hasPskCommand(command: String) = pskKeyHashSplitterRegex.matches(command) | ||
|
||
fun extractKeyFromCommand(command: String) = pskKeyHashSplitterRegex.findAll(command).first().groups[1]!!.value | ||
|
||
fun extractHashFromCommand(command: String) = pskKeyHashSplitterRegex.findAll(command).first().groups[2]!!.value | ||
} |
18 changes: 0 additions & 18 deletions
18
...cation/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/PskKeyExtractor.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
.../main/kotlin/org/gxf/crestdevicesimulator/simulator/response/command/PskCommandHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-FileCopyrightText: Contributors to the GXF project | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.gxf.crestdevicesimulator.simulator.response.command | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.apache.commons.codec.digest.DigestUtils | ||
import org.gxf.crestdevicesimulator.configuration.AdvancedSingleIdentityPskStore | ||
import org.gxf.crestdevicesimulator.configuration.SimulatorProperties | ||
import org.gxf.crestdevicesimulator.simulator.data.repository.PskRepository | ||
import org.gxf.crestdevicesimulator.simulator.response.PskExtractor | ||
import org.gxf.crestdevicesimulator.simulator.response.command.exception.InvalidPskHashException | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class PskCommandHandler(private val pskRepository: PskRepository, | ||
private val simulatorProperties: SimulatorProperties, | ||
private val pskStore: AdvancedSingleIdentityPskStore) { | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
fun handlePskChange(body: String) { | ||
val newPsk = PskExtractor.extractKeyFromCommand(body) | ||
val hash = PskExtractor.extractHashFromCommand(body) | ||
|
||
val preSharedKeyOptional = pskRepository.findById(simulatorProperties.pskIdentity) | ||
|
||
if (preSharedKeyOptional.isEmpty) { | ||
logger.error { "No psk for identity: ${simulatorProperties.pskIdentity}" } | ||
} | ||
|
||
logger.info { "Validating hash for identity: ${simulatorProperties.pskIdentity}" } | ||
|
||
val preSharedKey = preSharedKeyOptional.get() | ||
val secret = preSharedKey.secret | ||
val expectedHash = DigestUtils.sha256Hex("$secret$newPsk") | ||
|
||
if (expectedHash != hash) { | ||
throw InvalidPskHashException("PSK set Hash for Identity ${simulatorProperties.pskIdentity} did not match") | ||
} | ||
|
||
pskRepository.save(preSharedKey.apply { this.preSharedKey = newPsk }) | ||
pskStore.key = newPsk | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
.../gxf/crestdevicesimulator/simulator/response/command/exception/InvalidPskHashException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// SPDX-FileCopyrightText: Contributors to the GXF project | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.gxf.crestdevicesimulator.simulator.response.command.exception | ||
|
||
class InvalidPskHashException(message: String) : Exception(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- SPDX-FileCopyrightText: Contributors to the GXF project | ||
-- | ||
-- SPDX-License-Identifier: Apache-2.0 | ||
-- No production data was set before this change so we can drop all existing data | ||
delete from pre_shared_key; | ||
|
||
alter table pre_shared_key | ||
add column secret varchar(255) not null; |
36 changes: 0 additions & 36 deletions
36
application/src/test/kotlin/PreSharedKeyKeyExtractorTest.kt
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...ication/src/test/kotlin/SimulatorTests.kt → ...vicesimulator/simulator/SimulatorTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ation/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/response/PskExtractorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-FileCopyrightText: Contributors to the GXF project | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package org.gxf.crestdevicesimulator.simulator.response | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
|
||
|
||
class PskExtractorTest { | ||
|
||
companion object { | ||
private const val testHash = "1234567890123456123456789012345612345678901234561234567890123456" | ||
|
||
private const val validPskCommand = "!PSK:1234567891234567${testHash};PSK:1234567891234567${testHash}SET" | ||
private const val validPskCommandWithKeyWordsInKey = "!PSK:PSKaSET1PSKd2SET${testHash};PSK:PSKaSET1PSKd2SET${testHash}SET" | ||
private const val invalidKeySizePskCommand = "!PSK:1234${testHash};PSK:1234${testHash}SET" | ||
private const val notPskCommand = "NoPskCommandInThisString" | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
"$validPskCommand, true", | ||
"$validPskCommandWithKeyWordsInKey, true", | ||
"$invalidKeySizePskCommand, false", | ||
"$notPskCommand, false" | ||
) | ||
fun shouldReturnTrueWhenThereIsAPskCommandInString(pskCommand: String, isValid: Boolean) { | ||
val result = PskExtractor.hasPskCommand(pskCommand) | ||
assertThat(result).isEqualTo(isValid) | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
"$validPskCommand, 1234567891234567", | ||
"$validPskCommandWithKeyWordsInKey, PSKaSET1PSKd2SET" | ||
) | ||
fun shouldReturnPskKeyFromValidPskCommand(pskCommand: String, expectedKey: String) { | ||
val result = PskExtractor.extractKeyFromCommand(pskCommand) | ||
|
||
assertThat(result).isEqualTo(expectedKey) | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
"$validPskCommand, $testHash", | ||
"$validPskCommandWithKeyWordsInKey, $testHash" | ||
) | ||
fun shouldReturnHashFromValidPskCommand(pskCommand: String, expectedHash: String) { | ||
val result = PskExtractor.extractHashFromCommand(pskCommand) | ||
|
||
assertThat(result).isEqualTo(expectedHash) | ||
} | ||
} |
Oops, something went wrong.