Return ciphertext rather than decrypted content
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Jack Hadrill 2022-03-18 15:56:33 +00:00
parent 5c17b880cf
commit be39f7bae1
8 changed files with 11400 additions and 92 deletions

11369
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -20,11 +20,8 @@
"@types/jest": "^27.4.0", "@types/jest": "^27.4.0",
"jest": "^27.4.7", "jest": "^27.4.7",
"ts-jest": "^27.1.3", "ts-jest": "^27.1.3",
"ts-loader": "^9.2.6",
"ts-standard": "^11.0.0", "ts-standard": "^11.0.0",
"typescript": "^4.5.5", "typescript": "^4.5.5"
"webpack": "^5.68.0",
"webpack-cli": "^4.9.2"
}, },
"jest": { "jest": {
"verbose": true, "verbose": true,

View File

@ -1,4 +1,4 @@
import { decrypt, encrypt } from 'romulus-js' import { encrypt } from 'romulus-js'
import { DEFAULT_KEY, MessageTypes } from '../common' import { DEFAULT_KEY, MessageTypes } from '../common'
import { numberToUint16BE } from '../utilities/number' import { numberToUint16BE } from '../utilities/number'
import { packOutgoingPacket } from './packet' import { packOutgoingPacket } from './packet'
@ -6,19 +6,16 @@ import { packOutgoingPacket } from './packet'
const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Basic) const MESSAGE_TYPE = numberToUint16BE(MessageTypes.Basic)
export interface BasicMessage { export interface BasicMessage {
message: string data: Uint8Array
success?: boolean
} }
/** /**
* Create an outgoing basic message (0x0001) packet. * Create an outgoing basic message (0x0001) packet.
* @param properties The properties for the message. * @param message The plaintext message to send.
* @param key The key to encrypt the data with. * @param key The key to encrypt the data with.
* @returns An outgoing basic message (0x0001) packet. * @returns An encrypted outgoing basic message (0x0001) packet.
*/ */
export function packBasicMessage (properties: BasicMessage, key: Uint8Array = DEFAULT_KEY): Uint8Array { export function packBasicMessage (message: Uint8Array, key: Uint8Array = DEFAULT_KEY): Uint8Array {
const encoder = new TextEncoder()
const message = encoder.encode(properties.message)
const data = encrypt(message, MESSAGE_TYPE, key) const data = encrypt(message, MESSAGE_TYPE, key)
return packOutgoingPacket({ return packOutgoingPacket({
messageType: MESSAGE_TYPE, messageType: MESSAGE_TYPE,
@ -29,14 +26,8 @@ export function packBasicMessage (properties: BasicMessage, key: Uint8Array = DE
/** /**
* Unpack the data section of an incoming basic message (0x0001) message. * Unpack the data section of an incoming basic message (0x0001) message.
* @param data The data section of an incoming basic message (0x0001) message. * @param data The data section of an incoming basic message (0x0001) message.
* @param key The key to decrypt the data with. * @returns An encrypted unpacked basic message (0x0001) message.
* @returns An unpacked basic message (0x0001) message.
*/ */
export function unpackBasicMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): BasicMessage { export function unpackBasicMessage (data: Uint8Array): Uint8Array {
const decoder = new TextDecoder() return data
const message = decrypt(data, MESSAGE_TYPE, key)
return {
message: decoder.decode(message.plaintext),
success: message.success
}
} }

View File

@ -11,7 +11,6 @@ export interface UserDataRequestMessage {
username: string username: string
colour: Color colour: Color
clientId: string clientId: string
success?: boolean
} }
/** /**
@ -48,27 +47,13 @@ export function packUserDataRequestMessage (properties: UserDataRequestMessage,
} }
/** /**
* Unpack the data section of an incoming user data request (0x0002) message * Unpack the decrypted data section of an incoming user data request (0x0002) message.
* @param data The data section of an incoming user data request (0x0002) message * @param data The decrypted data section of an incoming user data request (0x0002) message.
* @param key The key to decrypt the data with. * @returns An unpacked user data request (0x0002) message.
* @returns An unpacked user data request (0x0002) message
*/ */
export function unpackUserDataRequestMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): UserDataRequestMessage { export function unpackUserDataRequestMessage (data: Uint8Array): UserDataRequestMessage {
// Decrypt the incoming data.
const message = decrypt(data, MESSAGE_TYPE, key)
// Guard to check if decryption was successful.
if (!message.success) {
return {
username: '',
colour: Color('black'),
clientId: '',
success: false
}
}
// Unpack and read data in correct format. // Unpack and read data in correct format.
const packedData = SmartBuffer.from(message.plaintext) const packedData = SmartBuffer.from(data)
const usernameLength = packedData.readUInt16() const usernameLength = packedData.readUInt16()
const username = packedData.readBytes(usernameLength) const username = packedData.readBytes(usernameLength)
@ -83,6 +68,5 @@ export function unpackUserDataRequestMessage (data: Uint8Array, key: Uint8Array
username: decoder.decode(username), username: decoder.decode(username),
colour: Color.rgb(colour), colour: Color.rgb(colour),
clientId: decoder.decode(clientId), clientId: decoder.decode(clientId),
success: message.success
} }
} }

View File

@ -11,7 +11,6 @@ export interface UserDataResponseMessage {
username: string username: string
colour: Color colour: Color
clientId: string clientId: string
success?: boolean
} }
/** /**
@ -47,27 +46,13 @@ export function packUserDataResponseMessage (properties: UserDataResponseMessage
} }
/** /**
* Unpack the data section of an incoming user data response (0x0003) message * Unpack the decrypted data section of an incoming user data response (0x0003) message.
* @param data The data section of an incoming user data response (0x0003) message * @param data The decrypted data section of an incoming user data response (0x0003) message.
* @param key The key to decrypt the data with. * @returns A unpacked user data response (0x0003) message.
* @returns An unpacked user data response (0x0003) message
*/ */
export function unpackUserDataResponseMessage (data: Uint8Array, key: Uint8Array = DEFAULT_KEY): UserDataResponseMessage { export function unpackUserDataResponseMessage (data: Uint8Array): UserDataResponseMessage {
// Decrypt the incoming data.
const message = decrypt(data, MESSAGE_TYPE, key)
// Guard to check if decryption was successful.
if (!message.success) {
return {
username: '',
colour: Color('black'),
clientId: '',
success: false
}
}
// Unpack and read data in correct format. // Unpack and read data in correct format.
const packedData = SmartBuffer.from(message.plaintext) const packedData = SmartBuffer.from(data)
const usernameLength = packedData.readUInt16() const usernameLength = packedData.readUInt16()
const username = packedData.readBytes(usernameLength) const username = packedData.readBytes(usernameLength)
@ -82,6 +67,5 @@ export function unpackUserDataResponseMessage (data: Uint8Array, key: Uint8Array
username: decoder.decode(username), username: decoder.decode(username),
colour: Color.rgb(colour), colour: Color.rgb(colour),
clientId: decoder.decode(clientId), clientId: decoder.decode(clientId),
success: message.success
} }
} }

View File

@ -1,15 +1,16 @@
import { MessageTypes } from '../../src/common' import { MessageTypes } from '../../src/common'
import { packers, unpackers } from '../../src/mapping' import { packers, unpackers } from '../../src/mapping'
const KEY = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]) const KEY = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
test('Create a basic message (0x0001) packet.', () => { test('Create a basic message (0x0001) packet.', () => {
// Given // Given
const message = 'Hello, World!' const encoder = new TextEncoder()
const message = encoder.encode('Hello, World!')
// When // When
const packedPacket = packers[MessageTypes.Basic]( const packedPacket = packers[MessageTypes.Basic](
{ message: message }, message,
KEY KEY
) )
@ -24,16 +25,12 @@ test('Create a basic message (0x0001) packet.', () => {
test('Parse a basic message (0x0001).', () => { test('Parse a basic message (0x0001).', () => {
// Given // Given
const ciphertext = new Uint8Array([ const data = new Uint8Array([1, 2, 3, 4])
0x84, 0xa2, 0x3a, 0xe2, 0xa8, 0xff, 0x43, 0x56, 0x96, 0x94, 0xf6, 0xe5, 0x1a, 0x30, 0x53,
0x39, 0xb9, 0xc4, 0x2c, 0xab, 0x21, 0x52, 0x8f, 0xc2, 0xba, 0x6c, 0x8b, 0x96, 0x82, 0x9d,
0x51, 0x27, 0x0c, 0xb6, 0x3b, 0x7f, 0x3e, 0x2a, 0x7b, 0x70, 0xbe, 0x01, 0x7b, 0x71, 0x9d
])
// When // When
const unpackedPacket = unpackers[MessageTypes.Basic](ciphertext, KEY) const unpackedPacket = unpackers[MessageTypes.Basic](data)
// Then // Then
expect(unpackedPacket.success).toBe(true) expect(unpackedPacket)
expect(unpackedPacket.message).toBe('Hello, World!') expect(unpackedPacket).toMatchObject(data)
}) })

View File

@ -31,22 +31,15 @@ test('Create a user data request (0x0002) packet.', () => {
test('Parse a user data request (0x0002).', () => { test('Parse a user data request (0x0002).', () => {
// Given // Given
const ciphertext = new Uint8Array([ const data = new Uint8Array([0, 12, 66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 255, 64, 0, 0, 7, 77, 101, 114, 99, 117, 114, 121])
0x6e, 0x3f, 0xe8, 0x45, 0x65, 0x59, 0x45, 0x85, 0xb4, 0xb2, 0xbc, 0x7a, 0x03, 0xc5,
0x6d, 0xf4, 0x23, 0x3e, 0xe9, 0x3b, 0x08, 0x6d, 0x67, 0x85, 0xc1, 0xda, 0xc6, 0x3f,
0xef, 0xaf, 0x4f, 0xd8, 0x63, 0xe6, 0xc1, 0x6c, 0x98, 0x45, 0x46, 0x4a, 0x3b, 0x61,
0x2c, 0x1e, 0x05, 0x03, 0x65, 0xe8, 0x8d, 0x82, 0x59, 0x56, 0x38, 0x58, 0x2e, 0xc4,
0x6f, 0xed
])
const username = 'Butlersaurus' const username = 'Butlersaurus'
const colour = Color('#FF4000') const colour = Color('#FF4000')
const clientId = 'Mercury' const clientId = 'Mercury'
// When // When
const unpackedPacket = unpackers[MessageTypes.UserDataRequest](ciphertext, KEY) const unpackedPacket = unpackers[MessageTypes.UserDataRequest](data)
// Then // Then
expect(unpackedPacket.success).toBe(true)
expect(unpackedPacket.username).toBe(username) expect(unpackedPacket.username).toBe(username)
expect(unpackedPacket.colour).toMatchObject(colour) expect(unpackedPacket.colour).toMatchObject(colour)
expect(unpackedPacket.clientId).toBe(clientId) expect(unpackedPacket.clientId).toBe(clientId)

View File

@ -31,22 +31,15 @@ test('Create a user data response (0x0003) packet.', () => {
test('Parse a user data response (0x0003).', () => { test('Parse a user data response (0x0003).', () => {
// Given // Given
const ciphertext = new Uint8Array([ const data = new Uint8Array([0, 12, 66, 117, 116, 108, 101, 114, 115, 97, 117, 114, 117, 115, 255, 64, 0, 0, 7, 77, 101, 114, 99, 117, 114, 121])
0x56, 0x71, 0x08, 0xf9, 0x2c, 0x4e, 0x41, 0x13, 0xbe, 0x44, 0xba, 0xd9, 0xe4, 0x25,
0x14, 0x60, 0x3b, 0x96, 0x7e, 0x0b, 0xbd, 0xac, 0xf0, 0xaf, 0xac, 0xd7, 0x80, 0xe5,
0x62, 0xd4, 0x33, 0x10, 0x23, 0x6d, 0x00, 0x3c, 0xae, 0x40, 0x6c, 0xe9, 0x40, 0xfc,
0x1c, 0xe0, 0xd3, 0xca, 0x65, 0xea, 0x83, 0x73, 0x5e, 0xd2, 0x67, 0xb2, 0x94, 0x58,
0x12, 0x73
])
const username = 'Butlersaurus' const username = 'Butlersaurus'
const colour = Color('#FF4000') const colour = Color('#FF4000')
const clientId = 'Mercury' const clientId = 'Mercury'
// When // When
const unpackedPacket = unpackers[MessageTypes.UserDataResponse](ciphertext, KEY) const unpackedPacket = unpackers[MessageTypes.UserDataResponse](data)
// Then // Then
expect(unpackedPacket.success).toBe(true)
expect(unpackedPacket.username).toBe(username) expect(unpackedPacket.username).toBe(username)
expect(unpackedPacket.colour).toMatchObject(colour) expect(unpackedPacket.colour).toMatchObject(colour)
expect(unpackedPacket.clientId).toBe(clientId) expect(unpackedPacket.clientId).toBe(clientId)