Repository navigation
DTLS 1.3 record layer (RFC 9147 section 4), part 1 of 4 for #1468 #2439
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
mondain
wants to merge
6
commits into
bcgit:main
Choose a base branch
from
mondain:dtls13/1-record-layer
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
6 commits
Select commit
Hold shift + click to select a range
5e8aadb
Add the DTLS 1.3 ack content type (RFC 9147 7.1), relates to github #…
mondain 92a868c
Add DTLS 1.3 record number encryption mask primitive for AES and ChaC…
mondain db62603
Add DTLS 1.3 record protection to TlsAEADCipher via a new TlsDTLS13Ci…
mondain dda1d91
Add DTLS 1.3 unified header codec and sequence number reconstruction …
mondain d5a8bd2
Add DTLS 1.3 record layer send/receive paths and epoch switching (RFC…
mondain 405626e
Use the "dtls13" HKDF-Expand-Label prefix for the DTLS 1.3 key schedu…
JonathanLennox 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
152 changes: 152 additions & 0 deletions
152
tls/src/main/java/org/bouncycastle/tls/DTLS13UnifiedHeader.java
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,152 @@ | ||
| package org.bouncycastle.tls; | ||
|
|
||
| /** | ||
| * RFC 9147 4. The DTLS 1.3 unified header for DTLSCiphertext records. | ||
| * <pre> | ||
| * 0 1 2 3 4 5 6 7 | ||
| * +-+-+-+-+-+-+-+-+ | ||
| * |0|0|1|C|S|L|E E| | ||
| * +-+-+-+-+-+-+-+-+ | ||
| * </pre> | ||
| * C: connection ID present, S: 16-bit (1) or 8-bit (0) sequence number, L: length present, EE: low two bits of | ||
| * the epoch. Records written by this implementation always use the full form (S = 1, L = 1). | ||
| */ | ||
| class DTLS13UnifiedHeader | ||
| { | ||
| static final int FIXED_BITS = 0x20; | ||
| static final int FIXED_BITS_MASK = 0xE0; | ||
| static final int FLAG_CID = 0x10; | ||
| static final int FLAG_SEQ16 = 0x08; | ||
| static final int FLAG_LENGTH = 0x04; | ||
| static final int EPOCH_BITS_MASK = 0x03; | ||
|
|
||
| /** RFC 9147 4.2.3. Record number encryption needs at least 16 bytes of ciphertext. */ | ||
| static final int MIN_CIPHERTEXT_LENGTH = 16; | ||
|
|
||
| private static final long MAX_SEQUENCE_NUMBER = (1L << 48) - 1; | ||
|
|
||
| static boolean isCiphertextRecord(int firstByte) | ||
| { | ||
| return (firstByte & FIXED_BITS_MASK) == FIXED_BITS; | ||
| } | ||
|
|
||
| static boolean hasConnectionID(int firstByte) | ||
| { | ||
| return (firstByte & FLAG_CID) != 0; | ||
| } | ||
|
|
||
| static boolean hasSeq16(int firstByte) | ||
| { | ||
| return (firstByte & FLAG_SEQ16) != 0; | ||
| } | ||
|
|
||
| static boolean hasLength(int firstByte) | ||
| { | ||
| return (firstByte & FLAG_LENGTH) != 0; | ||
| } | ||
|
|
||
| static boolean matchesEpoch(int firstByte, int epoch) | ||
| { | ||
| return (firstByte & EPOCH_BITS_MASK) == (epoch & EPOCH_BITS_MASK); | ||
| } | ||
|
|
||
| static int getSequenceNumberLength(int firstByte) | ||
| { | ||
| return hasSeq16(firstByte) ? 2 : 1; | ||
| } | ||
|
|
||
| static int getHeaderLength(int firstByte, int connectionIDLength) | ||
| { | ||
| return 1 + connectionIDLength + getSequenceNumberLength(firstByte) + (hasLength(firstByte) ? 2 : 0); | ||
| } | ||
|
|
||
| static int getWriteHeaderLength(int connectionIDLength) | ||
| { | ||
| return 1 + connectionIDLength + 2 + 2; | ||
| } | ||
|
|
||
| /** | ||
| * The smallest conforming header a peer may send per RFC 9147 4: first byte, connection ID, and an 8-bit | ||
| * sequence number, with no length field (S = 0, L = 0). Since a peer is free to use that compact form, | ||
| * this is what the receive limit must budget for; assuming our own (full) write form would under-report the | ||
| * plaintext limit and reject legal records. | ||
| * | ||
| * @return the minimum length of a header that may be received. | ||
| */ | ||
| static int getMinReadHeaderLength(int connectionIDLength) | ||
| { | ||
| return 1 + connectionIDLength + 1; | ||
| } | ||
|
|
||
| /** | ||
| * Write a full-form header (16-bit sequence number, length present). The length field is left zero for the | ||
| * cipher to fill in once the ciphertext length is known. | ||
| * | ||
| * @return the header length. | ||
| */ | ||
| static int writeHeader(int epoch, long sequenceNumber, byte[] connectionID, byte[] buf, int off) | ||
| { | ||
| int cidLength = null == connectionID ? 0 : connectionID.length; | ||
|
|
||
| int firstByte = FIXED_BITS | FLAG_SEQ16 | FLAG_LENGTH | (epoch & EPOCH_BITS_MASK); | ||
| if (cidLength > 0) | ||
| { | ||
| firstByte |= FLAG_CID; | ||
| } | ||
|
|
||
| int pos = off; | ||
| buf[pos++] = (byte)firstByte; | ||
| if (cidLength > 0) | ||
| { | ||
| System.arraycopy(connectionID, 0, buf, pos, cidLength); | ||
| pos += cidLength; | ||
| } | ||
| TlsUtils.writeUint16((int)(sequenceNumber & 0xFFFFL), buf, pos); | ||
| pos += 2; | ||
| TlsUtils.writeUint16(0, buf, pos); | ||
| pos += 2; | ||
| return pos - off; | ||
| } | ||
|
|
||
| /** | ||
| * RFC 9147 4.2.2. Reconstruct the full sequence number as the value numerically closest to 'expected' (one | ||
| * plus the highest successfully deprotected sequence number) whose low 'seqBitCount' bits equal 'seqBits'. | ||
| */ | ||
| static long reconstructSequenceNumber(long expected, int seqBits, int seqBitCount) | ||
| { | ||
| long modulus = 1L << seqBitCount; | ||
| long lowMask = modulus - 1; | ||
|
|
||
| long candidate = (expected & ~lowMask) | (seqBits & lowMask); | ||
| long best = candidate; | ||
| long bestDistance = distance(candidate, expected); | ||
|
|
||
| long lower = candidate - modulus; | ||
| if (lower >= 0) | ||
| { | ||
| long d = distance(lower, expected); | ||
| if (d < bestDistance) | ||
| { | ||
| best = lower; | ||
| bestDistance = d; | ||
| } | ||
| } | ||
|
|
||
| long upper = candidate + modulus; | ||
| if (upper <= MAX_SEQUENCE_NUMBER) | ||
| { | ||
| long d = distance(upper, expected); | ||
| if (d < bestDistance) | ||
| { | ||
| best = upper; | ||
| } | ||
| } | ||
|
|
||
| return best; | ||
| } | ||
|
|
||
| private static long distance(long a, long b) | ||
| { | ||
| return a > b ? a - b : b - a; | ||
| } | ||
| } | ||
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.
Should this check for FLAG_CID - or perhaps callers substitute 0 length? I think confusion here can have knock-on effects on multi-record datagram parsing.