-
Notifications
You must be signed in to change notification settings - Fork 2
Handle Oscore partialIv, kidContext, and kid explicitly
#343
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
Open
zalenskivolt
wants to merge
4
commits into
JuulLabs:main
Choose a base branch
from
zalenskivolt:oscore-partial-iv-kid-context-kid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f8558f
Handle Oscore partialIv, kidContext, and kid explicitly
zalenskivolt 633e113
Use contentHashCode for all ByteArray hashes
zalenskivolt fa319d1
Move oscore parse code to Oscore.kt, add constants
zalenskivolt d818bb2
Make Oscore split by parts optional
zalenskivolt 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| package com.juul.koap | ||
|
|
||
| import com.juul.koap.Message.Option.Oscore | ||
|
|
||
| private val PARTIAL_IV_LENGTH_RANGE = 0..5 | ||
| private const val PARTIAL_IV_LENGTH_MASK = 0x07 | ||
| private const val KID_FLAG = 0x08 | ||
| private const val KID_CONTEXT_FLAG = 0x10 | ||
|
|
||
| data class OscoreParts( | ||
| val partialIv: ByteArray, | ||
| val kidContext: ByteArray?, | ||
| val kid: ByteArray?, | ||
| ) { | ||
| override fun equals(other: Any?): Boolean = | ||
| this === other || | ||
| ( | ||
| other is OscoreParts && | ||
| partialIv.contentEquals(other.partialIv) && | ||
| kidContext.contentEquals(other.kidContext) && | ||
| kid.contentEquals(other.kid) | ||
| ) | ||
|
|
||
| override fun hashCode(): Int { | ||
| var result = partialIv.contentHashCode() | ||
| result = 31 * result + kidContext.contentHashCode() | ||
| result = 31 * result + kid.contentHashCode() | ||
| return result | ||
| } | ||
|
|
||
| override fun toString(): String { | ||
| val args = listOfNotNull( | ||
| if (partialIv.isNotEmpty()) "partialIv=${partialIv.toHexString()}" else null, | ||
| if (kidContext != null) "kidContext=${kidContext.toHexString()}" else null, | ||
| if (kid != null) "kid=${kid.toHexString()}" else null, | ||
| ).joinToString(separator = ", ") | ||
| return "Oscore($args)" | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates a binary OSCORE option value from an OscoreParts object. | ||
| * | ||
| * - [RFC 8613](https://tools.ietf.org/html/rfc8613#section-2) 2. The OSCORE Option | ||
| * - [RFC 8613](https://tools.ietf.org/html/rfc8613#section-6.1) 6.1. Encoding of the OSCORE Option Value | ||
| */ | ||
| internal fun oscoreOptionValue(oscore: OscoreParts): ByteArray { | ||
| val partialIvLength = oscore.partialIv.size | ||
| require(partialIvLength in PARTIAL_IV_LENGTH_RANGE) { | ||
| "Partial IV length of $partialIvLength is outside allowable range of $PARTIAL_IV_LENGTH_RANGE" | ||
| } | ||
| val kidContextLength = if (oscore.kidContext != null) oscore.kidContext.size else 0 | ||
| val kidFlagBit = if (oscore.kid != null) KID_FLAG else 0 | ||
| val kidContextFlagBit = if (oscore.kidContext != null) KID_CONTEXT_FLAG else 0 | ||
| val flagBits = partialIvLength or kidFlagBit or kidContextFlagBit | ||
| if (flagBits == 0) { | ||
| // If the OSCORE flag bits are all zero (0x00), the option value SHALL be empty | ||
| return byteArrayOf() | ||
| } | ||
| var value = byteArrayOf(flagBits.toByte()) + oscore.partialIv | ||
| if (oscore.kidContext != null) { | ||
| value += byteArrayOf(kidContextLength.toByte()) + oscore.kidContext | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth a |
||
| } | ||
| if (oscore.kid != null) { | ||
| value += oscore.kid | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| /** | ||
| * Parses an OscoreParts object from a binary OSCORE option value. | ||
| */ | ||
| internal fun oscorePartsFromOptionValue(value: ByteArray): OscoreParts { | ||
| // TODO: detect if value does not conform to expected oscore format | ||
| if (value.isEmpty()) { | ||
| return OscoreParts(byteArrayOf(), null, null) | ||
| } | ||
| val flagBits = value[0].toInt() | ||
| val partialIvLength = flagBits and PARTIAL_IV_LENGTH_MASK | ||
| val kidFlag = (flagBits and KID_FLAG) != 0 | ||
| val kidContextFlag = (flagBits and KID_CONTEXT_FLAG) != 0 | ||
| val partialIv = value.sliceArray(1 until 1 + partialIvLength) | ||
| val kidContextLength = if (kidContextFlag) value[1 + partialIvLength].toInt() else 0 | ||
| val kidContext = if (kidContextFlag) { | ||
| value.sliceArray(2 + partialIvLength until 2 + partialIvLength + kidContextLength) | ||
| } else { | ||
| null | ||
| } | ||
| val kidOffset = (if (kidContextFlag) 2 else 1) + partialIvLength + kidContextLength | ||
| val kid = if (kidFlag) value.sliceArray(kidOffset until value.size) else null | ||
| return OscoreParts(partialIv, kidContext, kid) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
oscorePartsFromOptionValuecan throw on malformed input data. It may be surprising to a system that catches a parsing exception and then uses thistoStringmethod to log the failure - they won't be expecting the log invoke tothrowwhich may cause a crash. I'd recommend ensuring thattoStringcannot throw as it is typically considered a safe operation.