Skip to content

Handle Oscore partialIv, kidContext, and kid explicitly#343

Open
zalenskivolt wants to merge 4 commits intoJuulLabs:mainfrom
zalenskivolt:oscore-partial-iv-kid-context-kid
Open

Handle Oscore partialIv, kidContext, and kid explicitly#343
zalenskivolt wants to merge 4 commits intoJuulLabs:mainfrom
zalenskivolt:oscore-partial-iv-kid-context-kid

Conversation

@zalenskivolt
Copy link
Contributor

No description provided.

@zalenskivolt zalenskivolt requested review from a team and twyatt as code owners March 31, 2025 22:06
@zalenskivolt zalenskivolt force-pushed the oscore-partial-iv-kid-context-kid branch from 608948e to 6e82787 Compare March 31, 2025 22:07
@codecov
Copy link

codecov bot commented Mar 31, 2025

Codecov Report

❌ Patch coverage is 78.84615% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.61%. Comparing base (6cbf889) to head (d818bb2).

Files with missing lines Patch % Lines
koap-core/src/commonMain/kotlin/Oscore.kt 79.16% 9 Missing and 1 partial ⚠️
koap-core/src/commonMain/kotlin/Message.kt 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #343      +/-   ##
==========================================
+ Coverage   69.04%   69.61%   +0.56%     
==========================================
  Files           9       10       +1     
  Lines         798      849      +51     
  Branches      217      236      +19     
==========================================
+ Hits          551      591      +40     
- Misses        161      171      +10     
- Partials       86       87       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zalenskivolt zalenskivolt changed the title Handle Oscore partialIv, kidContext, and kid explicitly Handle Oscore partialIv, kidContext, and kid explicitly Apr 1, 2025
@zalenskivolt zalenskivolt changed the title Handle Oscore partialIv, kidContext, and kid explicitly Handle Oscore partialIv, kidContext, and kid explicitly Apr 1, 2025
@zalenskivolt zalenskivolt force-pushed the oscore-partial-iv-kid-context-kid branch 2 times, most recently from c7b37d9 to 8728335 Compare April 6, 2025 12:42
@twyatt
Copy link
Member

twyatt commented Apr 22, 2025

@zalenskivolt apologies; I've been busy on other work projects, but I plan to review this soon.

Copy link
Contributor

@cedrickcooke cedrickcooke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delays, overall great work again. One small change and then this turns into an approve

Comment on lines 620 to 639
val partialIvLength = partialIv.size
if (partialIvLength > 5) {
throw IllegalArgumentException("Partial IV length of $partialIvLength is outside allowable range of 0..5")
}
val kidContextLength = if (kidContext != null) kidContext.size else 0
val kidFlag = if (kid != null) 0x08 else 0
val kidContextFlag = if (kidContext != null) 0x10 else 0
val flagBits = partialIvLength or kidFlag or kidContextFlag
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()) + partialIv
if (kidContext != null) {
value += byteArrayOf(kidContextLength.toByte()) + kidContext
}
if (kid != null) {
value += kid
}
return value
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are some 20+17 lines of fairly dense/complicated code, with magic flag constants, etc.
maybe they should go in their own .kt file to isolate the complexity, and using constants for flags and masks?
not too happy that kidFlag and kidContextFlag is a byte/int value in one place and boolean in the other, maybe they should at least be named differently… (kidMask/kidContextMask?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, as mentioned in #333

There's reserved values for partialIv length 6 and 7, and three flags reserved for future use, so there's some potential for future updates to the standard rendering the option unparseable, and such flags/values should be handled in specific ways as outlined in 8.2. Verifying the Request …

It could have a malformed flag in it, or maybe a nullable partialIv and use null there to indicate the option didn't parse? I guess these complex options are a bit trickier than the simple uint/string ones in basic CoAP.

not sure how likely the option is to change, and it could just be handled by updating the code. but som drawback compared to just a binary blob is that it might start failing if updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are some 20+17 lines of fairly dense/complicated code, with magic flag constants, etc. maybe they should go in their own .kt file to isolate the complexity, and using constants for flags and masks? not too happy that kidFlag and kidContextFlag is a byte/int value in one place and boolean in the other, maybe they should at least be named differently… (kidMask/kidContextMask?)

added Oscore.kt with constants for flags, masks, and ranges, and updated to kid[Context]FlagBit

Comment on lines 644 to 660
if (value.size == 0) {
return Oscore(byteArrayOf(), null, null)
}
val flagBits = value[0].toInt()
val partialIvLength = flagBits and 0x07
val kidFlag = (flagBits and 0x08) != 0
val kidContextFlag = (flagBits and 0x10) != 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 Oscore(partialIv, kidContext, kid)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are some 20+17 lines of fairly dense/complicated code, with magic flag constants, etc.
maybe they should go in their own .kt file to isolate the complexity, and using constants for flags and masks?

@zalenskivolt
Copy link
Contributor Author

@zalenskivolt apologies; I've been busy on other work projects, but I plan to review this soon.

no worries!

I had a bit of a break in my current work, which caused this flurry of 10 PRs 😄 but I'm also more back to other work projects now…

@zalenskivolt zalenskivolt force-pushed the oscore-partial-iv-kid-context-kid branch from 7c21fbb to 2c43ac5 Compare April 23, 2025 11:25
@zalenskivolt zalenskivolt force-pushed the oscore-partial-iv-kid-context-kid branch 3 times, most recently from 9fd8b11 to a47f626 Compare November 12, 2025 01:44
@zalenskivolt
Copy link
Contributor Author

Another take: instead of breaking up the option value into parts, just do that in the toString method,
and optionally construct an Oscore option from the different parts, using Oscore.fromParts(partialIv, kidContext, kid)

@zalenskivolt zalenskivolt force-pushed the oscore-partial-iv-kid-context-kid branch from a47f626 to 78c5ac5 Compare November 12, 2025 01:51
@zalenskivolt zalenskivolt force-pushed the oscore-partial-iv-kid-context-kid branch from 78c5ac5 to d818bb2 Compare November 12, 2025 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants