-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/fdp 1815 #12
Merged
Merged
Feature/fdp 1815 #12
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c7e692d
FDP-1815: Added hash validation
LucianoFavoroso ffe3422
FDP-1815: changed hash validation
LucianoFavoroso 0299bd0
FDP-1815: newline
LucianoFavoroso 89ce72d
FDP-1815: fix test
LucianoFavoroso 4a07c27
FDP-1815: clean up
LucianoFavoroso b4ec47c
FDP-1815: comments and chiper change
LucianoFavoroso 2cc7a3d
FDP-1815: Comments Sander
LucianoFavoroso 2eed19d
FDP-1815: Comments Sander
LucianoFavoroso 15b27cf
FDP-1815: Comments Sander
LucianoFavoroso 014e4b3
FDP-1815: Comments Sander
LucianoFavoroso c9189a7
FDP-1815: formatting
LucianoFavoroso 9417ae3
FDP-1815: comments Sander
LucianoFavoroso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, | ||
smvdheijden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
smvdheijden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
smvdheijden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.CsvSource | ||
|
||
|
||
class PskExtractorTest { | ||
smvdheijden marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather have one regex findAll instead of 2 but for the simulator this could be good enough