Skip to content

fix: The final frame can not be larger than the Frame Length #281

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

Merged
merged 6 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/encrypt-browser/src/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export async function encrypt (
/* The final frame has a variable length.
* The value needs to be known, but should only be calculated once.
* So I calculate how much of a frame I should have at the end.
* This value will NEVER be larger than the frameLength.
*/
const finalFrameLength = frameLength - ((numberOfFrames * frameLength) - plaintextLength)
const bodyContent = []
Expand Down
11 changes: 9 additions & 2 deletions modules/encrypt-node/src/framed_encrypt_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,18 @@ type EncryptFrameInput = {
export function getEncryptFrame (input: EncryptFrameInput): EncryptFrame {
const { pendingFrame, messageHeader, getCipher, isFinalFrame } = input
const { sequenceNumber, contentLength, content } = pendingFrame
const frameIv = serialize.frameIv(messageHeader.headerIvLength, sequenceNumber)
const { frameLength, contentType, messageId, headerIvLength } = messageHeader
/* Precondition: The content length MUST correlate with the frameLength.
* In the case of a regular frame,
* the content length must strictly equal the frame length.
* In the case of the final frame,
* it MUST not be larger than the frame length.
*/
needs(frameLength === contentLength || (isFinalFrame && frameLength >= contentLength), 'Final frame length exceeds frame length.')
Copy link
Member

Choose a reason for hiding this comment

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

Strictly speaking, this should be two separate checks, no? If something goes terribly wrong on a non-final frame and I get an error that's talking about final frame length, I'm gonna be confused.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps. What about the error message Content length exceeds frame length

Copy link
Member

@mattsb42-aws mattsb42-aws Mar 31, 2020

Choose a reason for hiding this comment

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

How about:

Suggested change
needs(frameLength === contentLength || (isFinalFrame && frameLength >= contentLength), 'Final frame length exceeds frame length.')
needs(frameLength === contentLength || isFinalFrame, 'Content length does not match frame length.')
needs(!isFinalFrame || frameLength >= contentLength, 'Final frame length exceeds frame length.')

Copy link
Member

Choose a reason for hiding this comment

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

Alternate suggestion:

Suggested change
needs(frameLength === contentLength || (isFinalFrame && frameLength >= contentLength), 'Final frame length exceeds frame length.')
if (isFinalFrame){
needs(frameLength >= contentLength, 'Final frame length exceeds frame length.')
}else{
needs(frameLength === contentLength, 'Content length does not match frame length.')
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Talking about this, having the exact values in question in the error message should resolve this.

Both the error message AND the condition should clearly express the intent.
Further, if this error is ever hit, knowing if you are a final frame or not will GREATLY simplify the debugging.

const frameIv = serialize.frameIv(headerIvLength, sequenceNumber)
const bodyHeader = Buffer.from(isFinalFrame
? finalFrameHeader(sequenceNumber, frameIv, contentLength)
: frameHeader(sequenceNumber, frameIv))
const { contentType, messageId } = messageHeader
const contentString = aadUtility.messageAADContentString({ contentType, isFinalFrame })
const { buffer, byteOffset, byteLength } = aadUtility.messageAAD(messageId, contentString, sequenceNumber, contentLength)
const cipher = getCipher(frameIv)
Expand Down
89 changes: 88 additions & 1 deletion modules/encrypt-node/test/framed_encrypt_stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import 'mocha'
import { getFramedEncryptStream } from '../src/framed_encrypt_stream'
import { getFramedEncryptStream, getEncryptFrame } from '../src/framed_encrypt_stream'

chai.use(chaiAsPromised)
const { expect } = chai
Expand Down Expand Up @@ -61,3 +61,90 @@ describe('getFramedEncryptStream', () => {
expect(called).to.equal(true)
})
})

describe('getEncryptFrame', () => {
it('can return an EncryptFrame', () => {
const input = {
pendingFrame: {
content: [Buffer.from([1, 2, 3, 4, 5])],
contentLength: 5,
sequenceNumber: 1
},
isFinalFrame: false,
getCipher: () => ({ setAAD: () => {} }) as any,
messageHeader: {
frameLength: 5,
contentType: 2,
messageId: Buffer.from([]),
headerIvLength: 12 as 12,
version: 1,
type: 12,
suiteId: 1,
encryptionContext: {},
encryptedDataKeys: []
}
}
const test1 = getEncryptFrame(input)
expect(test1.content).to.equal(input.pendingFrame.content)
expect(test1.isFinalFrame).to.equal(input.isFinalFrame)

// Just a quick flip to make sure...
input.isFinalFrame = true
const test2 = getEncryptFrame(input)
expect(test2.content).to.equal(input.pendingFrame.content)
expect(test2.isFinalFrame).to.equal(input.isFinalFrame)
})

it('Precondition: The content length MUST correlate with the frameLength.', () => {
const inputFinalFrameToLarge = {
pendingFrame: {
content: [Buffer.from([1, 2, 3, 4, 5, 6])],
// This exceeds the frameLength below
contentLength: 6,
sequenceNumber: 1
},
isFinalFrame: true,
getCipher: () => ({ setAAD: () => {} }) as any,
messageHeader: {
frameLength: 5,
contentType: 2,
messageId: Buffer.from([]),
headerIvLength: 12 as 12,
version: 1,
type: 12,
suiteId: 1,
encryptionContext: {},
encryptedDataKeys: []
}
}

expect(() => getEncryptFrame(inputFinalFrameToLarge)).to.throw('Final frame length exceeds frame length.')

const inputFrame = {
pendingFrame: {
content: [Buffer.from([1, 2, 3, 4, 5])],
contentLength: 5,
sequenceNumber: 1
},
isFinalFrame: false,
getCipher: () => ({ setAAD: () => {} }) as any,
messageHeader: {
frameLength: 5,
contentType: 2,
messageId: Buffer.from([]),
headerIvLength: 12 as 12,
version: 1,
type: 12,
suiteId: 1,
encryptionContext: {},
encryptedDataKeys: []
}
}

// Make sure that it must be equal as long as we are here...
inputFrame.pendingFrame.contentLength = 4
expect(() => getEncryptFrame(inputFrame)).to.throw('Final frame length exceeds frame length.')
inputFrame.pendingFrame.contentLength = 6
expect(() => getEncryptFrame(inputFrame)).to.throw('Final frame length exceeds frame length.')
})
})