Handle Oscore partialIv, kidContext, and kid explicitly#343
Handle Oscore partialIv, kidContext, and kid explicitly#343zalenskivolt wants to merge 4 commits intoJuulLabs:mainfrom
partialIv, kidContext, and kid explicitly#343Conversation
608948e to
6e82787
Compare
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Oscore partialIv, kidContext, and kid explicitly
Oscore partialIv, kidContext, and kid explicitlypartialIv, kidContext, and kid explicitly
c7b37d9 to
8728335
Compare
|
@zalenskivolt apologies; I've been busy on other work projects, but I plan to review this soon. |
cedrickcooke
left a comment
There was a problem hiding this comment.
Sorry for the delays, overall great work again. One small change and then this turns into an approve
| 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 |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
these are some 20+17 lines of fairly dense/complicated code, with magic flag constants, etc. maybe they should go in their own
.ktfile to isolate the complexity, and using constants for flags and masks? not too happy thatkidFlagandkidContextFlagis 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
| 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) |
There was a problem hiding this comment.
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?
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… |
7c21fbb to
2c43ac5
Compare
9fd8b11 to
a47f626
Compare
|
Another take: instead of breaking up the option value into parts, just do that in the toString method, |
a47f626 to
78c5ac5
Compare
78c5ac5 to
d818bb2
Compare
No description provided.