Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
briar
briar
Commits
510f99c7
Unverified
Commit
510f99c7
authored
Mar 27, 2017
by
akwizgran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Validate arguments to StreamEncrypter#writeFrame().
parent
1918346a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
0 deletions
+58
-0
bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java
.../org/briarproject/bramble/crypto/StreamEncrypterImpl.java
+2
-0
bramble-core/src/test/java/org/briarproject/bramble/crypto/StreamEncrypterImplTest.java
.../briarproject/bramble/crypto/StreamEncrypterImplTest.java
+56
-0
No files found.
bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java
View file @
510f99c7
...
...
@@ -62,6 +62,8 @@ class StreamEncrypterImpl implements StreamEncrypter {
@Override
public
void
writeFrame
(
byte
[]
payload
,
int
payloadLength
,
int
paddingLength
,
boolean
finalFrame
)
throws
IOException
{
if
(
payloadLength
<
0
||
paddingLength
<
0
)
throw
new
IllegalArgumentException
();
if
(
payloadLength
+
paddingLength
>
MAX_PAYLOAD_LENGTH
)
throw
new
IllegalArgumentException
();
// Don't allow the frame counter to wrap
...
...
bramble-core/src/test/java/org/briarproject/bramble/crypto/StreamEncrypterImplTest.java
View file @
510f99c7
...
...
@@ -9,9 +9,13 @@ import java.io.ByteArrayOutputStream;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
FRAME_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAC_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_FRAME_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
MAX_PAYLOAD_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
STREAM_HEADER_IV_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
STREAM_HEADER_LENGTH
;
import
static
org
.
briarproject
.
bramble
.
api
.
transport
.
TransportConstants
.
TAG_LENGTH
;
import
static
org
.
junit
.
Assert
.
assertArrayEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
public
class
StreamEncrypterImplTest
extends
BrambleTestCase
{
...
...
@@ -30,6 +34,58 @@ public class StreamEncrypterImplTest extends BrambleTestCase {
payload
=
TestUtils
.
getRandomBytes
(
payloadLength
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testRejectsNegativePayloadLength
()
throws
Exception
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
StreamEncrypterImpl
s
=
new
StreamEncrypterImpl
(
out
,
cipher
,
streamNumber
,
tag
,
streamHeaderIv
,
streamHeaderKey
,
frameKey
);
s
.
writeFrame
(
payload
,
-
1
,
0
,
false
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testRejectsNegativePaddingLength
()
throws
Exception
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
StreamEncrypterImpl
s
=
new
StreamEncrypterImpl
(
out
,
cipher
,
streamNumber
,
tag
,
streamHeaderIv
,
streamHeaderKey
,
frameKey
);
s
.
writeFrame
(
payload
,
0
,
-
1
,
false
);
}
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
testRejectsMaxPayloadPlusPadding
()
throws
Exception
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
StreamEncrypterImpl
s
=
new
StreamEncrypterImpl
(
out
,
cipher
,
streamNumber
,
tag
,
streamHeaderIv
,
streamHeaderKey
,
frameKey
);
byte
[]
bigPayload
=
new
byte
[
MAX_PAYLOAD_LENGTH
+
1
];
s
.
writeFrame
(
bigPayload
,
MAX_PAYLOAD_LENGTH
,
1
,
false
);
}
@Test
public
void
testAcceptsMaxPayloadIncludingPadding
()
throws
Exception
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
StreamEncrypterImpl
s
=
new
StreamEncrypterImpl
(
out
,
cipher
,
streamNumber
,
tag
,
streamHeaderIv
,
streamHeaderKey
,
frameKey
);
byte
[]
bigPayload
=
new
byte
[
MAX_PAYLOAD_LENGTH
];
s
.
writeFrame
(
bigPayload
,
MAX_PAYLOAD_LENGTH
-
1
,
1
,
false
);
assertEquals
(
TAG_LENGTH
+
STREAM_HEADER_LENGTH
+
MAX_FRAME_LENGTH
,
out
.
size
());
}
@Test
public
void
testAcceptsMaxPayloadWithoutPadding
()
throws
Exception
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
StreamEncrypterImpl
s
=
new
StreamEncrypterImpl
(
out
,
cipher
,
streamNumber
,
tag
,
streamHeaderIv
,
streamHeaderKey
,
frameKey
);
byte
[]
bigPayload
=
new
byte
[
MAX_PAYLOAD_LENGTH
];
s
.
writeFrame
(
bigPayload
,
MAX_PAYLOAD_LENGTH
,
0
,
false
);
assertEquals
(
TAG_LENGTH
+
STREAM_HEADER_LENGTH
+
MAX_FRAME_LENGTH
,
out
.
size
());
}
@Test
public
void
testWriteUnpaddedNonFinalFrameWithTag
()
throws
Exception
{
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment