-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
crypto.ts
149 lines (134 loc) · 3.77 KB
/
crypto.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Xpra Typescript Client
* @link https://github.com/andersevenrud/xpra-html5-client
* @author Anders Evenrud <andersevenrud@gmail.com>
* @license Mozilla Public License Version 2.0
*
* ---
*
* Based on original Xpra source
* @copyright Copyright (C) 2016-2022 Antoine Martin <antoine@devloop.org.uk>
* @license Licensed under MPL 2.0, see: http://www.mozilla.org/MPL/2.0/
* @link https://github.com/Xpra-org/xpra-html5
*/
import forge from 'node-forge'
import { XpraCipherCapability } from '../types'
import { XpraCryptoError } from '../errors'
import { uint8toString } from '../utils/data'
import {
XPRA_DEFAULT_KEYSIZE,
XPRA_DEFAULT_KEY_HASH,
XPRA_DEFAULT_MODE,
XPRA_CHUNK_SZ,
} from '../constants/xpra'
export interface XpraCipher {
keyHash: string
cipher: string
mode: string
blockSize: number
secret: string
iv: string
}
/**
* Encrypts packet data with a cipher
*/
export function encryptXpraPacketData(
data: Uint8Array | string,
size: number,
blockSize: number,
cipher: forge.cipher.BlockCipher
) {
const paddingSize = blockSize - (size % blockSize)
let inputData = data
if (typeof inputData !== 'string') {
const c = []
for (let i = 0; i < data.length; i += XPRA_CHUNK_SZ) {
c.push(
String.fromCharCode.apply(
null,
(data as Uint8Array).subarray(
i,
i + XPRA_CHUNK_SZ
) as unknown as number[]
)
)
}
inputData = c.join('')
}
if (paddingSize) {
const paddingChr = String.fromCharCode(paddingSize)
for (let i = 0; i < paddingSize; i++) {
inputData += paddingChr
}
}
cipher.update(forge.util.createBuffer(inputData))
return cipher.output.getBytes()
}
/**
* Decrypts packet data with a cipher
*/
export function decryptXpraPacketData(
packet: Uint8Array,
size: number,
padding: number,
cipher: forge.cipher.BlockCipher
) {
cipher.update(forge.util.createBuffer(uint8toString(packet)))
const decrypted = cipher.output.getBytes()
if (!decrypted || decrypted.length < size - padding) {
throw new XpraCryptoError('Decryption failure')
}
const packetData = new Uint8Array(size - padding)
for (let i = 0; i < size - padding; i++) {
packetData[i] = decrypted[i].charCodeAt(0)
}
return packetData
}
/**
* Validates and creates cipher data
*/
export function createXpraCipher(
caps: XpraCipherCapability,
key: string
): XpraCipher {
if (!key) {
throw new XpraCryptoError('Missing crypto key')
}
const cipher = caps['cipher'] || 'AES'
const keySalt = caps['cipher.key_salt'] as string
const iterations = caps['cipher.key_stretch_iterations'] || 0
const keySize = caps['cipher.key_size'] || XPRA_DEFAULT_KEYSIZE
const keyStretch = caps['cipher.key_stretch'] || 'PBKDF2'
const keyHash = caps['cipher.key_hash'] || XPRA_DEFAULT_KEY_HASH
const mode = caps['cipher.mode'] || XPRA_DEFAULT_MODE
const iv = caps['cipher.iv']
const blockSize = mode === 'CBC' ? 32 : 0
if (cipher !== 'AES') {
throw new XpraCryptoError(`Unsupported crypto cipher: ${cipher}`)
} else if (iterations < 0) {
throw new XpraCryptoError(`Invalid crypto iteration count: ${iterations}`)
} else if (![32, 24, 16].includes(keySize)) {
throw new XpraCryptoError(`Invalid crypto key size: ${keySize}`)
} else if (keyStretch.toUpperCase() !== 'PBKDF2') {
throw new XpraCryptoError(`Invalid key stretching function: ${keyStretch}`)
} else if (!iv) {
throw new XpraCryptoError('Missing IV')
} else if (!['CBC', 'CFB', 'CTR'].includes(mode)) {
throw new XpraCryptoError(`Unsupported AES mode: ${mode}`)
}
const secret = forge.pkcs5.pbkdf2(
key,
keySalt,
iterations,
keySize
//keyHash.toLowerCase()
)
return {
keyHash,
cipher,
mode,
blockSize,
secret,
iv,
}
}