-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement Parcel messages (#35)
* Remove CMS encryption handling from RAMF serialization * Reimplement RAMF serialization/deserialization as standalone functions * Implement Parcel class * Fix typo
- Loading branch information
Showing
10 changed files
with
711 additions
and
563 deletions.
There are no files selected for viewing
This file contains 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 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,71 @@ | ||
/* tslint:disable:no-let */ | ||
import bufferToArray from 'buffer-to-arraybuffer'; | ||
|
||
import { generateStubCert, getMockContext } from './_test_utils'; | ||
import { generateRSAKeyPair } from './crypto_wrappers/keyGenerators'; | ||
import Parcel from './Parcel'; | ||
import * as serialization from './ramf/serialization'; | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('Parcel', () => { | ||
describe('serialize', () => { | ||
let parcel: Parcel; | ||
let senderPrivateKey: CryptoKey; | ||
beforeAll(async () => { | ||
const senderKeyPair = await generateRSAKeyPair(); | ||
const senderCertificate = await generateStubCert({ | ||
issuerPrivateKey: senderKeyPair.privateKey, | ||
}); | ||
senderPrivateKey = senderKeyPair.privateKey; | ||
parcel = new Parcel('address', senderCertificate, bufferToArray(Buffer.from('hi'))); | ||
}); | ||
|
||
const expectedSerialization = bufferToArray(Buffer.from('serialized')); | ||
const serializeSpy = jest.spyOn(serialization, 'serialize'); | ||
beforeAll(() => { | ||
serializeSpy.mockResolvedValueOnce(expectedSerialization); | ||
}); | ||
afterEach(() => { | ||
serializeSpy.mockReset(); | ||
}); | ||
|
||
test('Result should be RAMF serialization', async () => { | ||
const messageSerialized = await parcel.serialize(senderPrivateKey); | ||
|
||
expect(serializeSpy).toBeCalledTimes(1); | ||
expect(messageSerialized).toBe(expectedSerialization); | ||
}); | ||
|
||
test('Concrete message type should be 0x50', async () => { | ||
await parcel.serialize(senderPrivateKey); | ||
|
||
const serializeCallArs = getMockContext(serialization.serialize).calls[0]; | ||
expect(serializeCallArs[1]).toEqual(0x50); | ||
}); | ||
|
||
test('Concrete message version should be 0x0', async () => { | ||
await parcel.serialize(senderPrivateKey); | ||
|
||
const serializeCallArs = getMockContext(serialization.serialize).calls[0]; | ||
expect(serializeCallArs[2]).toEqual(0); | ||
}); | ||
|
||
test('Message should be signed with private key specified', async () => { | ||
await parcel.serialize(senderPrivateKey); | ||
|
||
const serializeCallArs = getMockContext(serialization.serialize).calls[0]; | ||
expect(serializeCallArs[3]).toEqual(senderPrivateKey); | ||
}); | ||
|
||
test('Signature options should be honored', async () => { | ||
const signatureOptions = { hashingAlgorithmName: 'SHA-384' }; | ||
await parcel.serialize(senderPrivateKey, signatureOptions); | ||
|
||
const serializeCallArs = getMockContext(serialization.serialize).calls[0]; | ||
expect(serializeCallArs[4]).toEqual(signatureOptions); | ||
}); | ||
}); | ||
}); |
This file contains 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,21 @@ | ||
import { SignatureOptions } from './crypto_wrappers/cms/signedData'; | ||
import Message from './ramf/Message'; | ||
import * as serialization from './ramf/serialization'; | ||
|
||
const concreteMessageTypeOctet = 0x50; | ||
const concreteMessageVersionOctet = 0; | ||
|
||
export default class Parcel extends Message { | ||
public async serialize( | ||
senderPrivateKey: CryptoKey, | ||
signatureOptions?: SignatureOptions, | ||
): Promise<ArrayBuffer> { | ||
return serialization.serialize( | ||
this, | ||
concreteMessageTypeOctet, | ||
concreteMessageVersionOctet, | ||
senderPrivateKey, | ||
signatureOptions, | ||
); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.