Skip to content

Commit

Permalink
Merge pull request #33 from rustymotors/deepsource-transform-5d6235e2
Browse files Browse the repository at this point in the history
style: format code with Prettier
  • Loading branch information
drazisil authored Jun 2, 2024
2 parents 99eea82 + f8234c8 commit 3ee02aa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 68 deletions.
1 change: 0 additions & 1 deletion packages/main/src/NPSUserLoginPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class PackedSessionKey {
this.timestamp = bytes.readUInt32BE(16);
}
}


/**
* @implements {INPSPayload}
Expand Down
133 changes: 69 additions & 64 deletions packages/main/src/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { Cipher, Decipher, createCipheriv, createDecipheriv, getCiphers } from "node:crypto";

import {
Cipher,
Decipher,
createCipheriv,
createDecipheriv,
getCiphers,
} from "node:crypto";

/**
* @external crypto
Expand All @@ -26,37 +31,37 @@ import { Cipher, Decipher, createCipheriv, createDecipheriv, getCiphers } from "
* A pair of encryption ciphers.
*/
export class McosEncryptionPair {
_cipher: Cipher;
_decipher: Decipher;
/**
* Create a new encryption pair.
*
* This function creates a new encryption pair. It is used to encrypt and
* decrypt data sent to and from the client.
*
* @param {module:crypto.Cipher} cipher The cipher to use for encryption.
* @param {module:crypto.Decipher} decipher The decipher to use for decryption.
*/
constructor(cipher: Cipher, decipher: Decipher) {
this._cipher = cipher;
this._decipher = decipher;
}

/**
* @param {Buffer} data The data to encrypt.
* @returns {Buffer} The encrypted data.
*/
encrypt(data: Buffer): Buffer {
return this._cipher.update(data);
}

/**
* @param {Buffer} data The data to decrypt.
* @returns {Buffer} The decrypted data.
*/
decrypt(data: Buffer): Buffer {
return this._decipher.update(data);
}
_cipher: Cipher;
_decipher: Decipher;
/**
* Create a new encryption pair.
*
* This function creates a new encryption pair. It is used to encrypt and
* decrypt data sent to and from the client.
*
* @param {module:crypto.Cipher} cipher The cipher to use for encryption.
* @param {module:crypto.Decipher} decipher The decipher to use for decryption.
*/
constructor(cipher: Cipher, decipher: Decipher) {
this._cipher = cipher;
this._decipher = decipher;
}

/**
* @param {Buffer} data The data to encrypt.
* @returns {Buffer} The encrypted data.
*/
encrypt(data: Buffer): Buffer {
return this._cipher.update(data);
}

/**
* @param {Buffer} data The data to decrypt.
* @returns {Buffer} The decrypted data.
*/
decrypt(data: Buffer): Buffer {
return this._decipher.update(data);
}
}

/**
Expand All @@ -66,26 +71,26 @@ export class McosEncryptionPair {
* @returns {McosEncryptionPair} The encryption pair
*/
export function createCommandEncryptionPair(key: string): McosEncryptionPair {
if (key.length < 16) {
throw Error(`Key too short: ${key}`);
}
if (key.length < 16) {
throw Error(`Key too short: ${key}`);
}

const sKey = key.slice(0, 16);
const sKey = key.slice(0, 16);

// Deepcode ignore HardcodedSecret: This uses an empty IV
const desIV = Buffer.alloc(8);
// Deepcode ignore HardcodedSecret: This uses an empty IV
const desIV = Buffer.alloc(8);

const gsCipher = createCipheriv("des-cbc", Buffer.from(sKey, "hex"), desIV);
gsCipher.setAutoPadding(false);
const gsCipher = createCipheriv("des-cbc", Buffer.from(sKey, "hex"), desIV);
gsCipher.setAutoPadding(false);

const gsDecipher = createDecipheriv(
"des-cbc",
Buffer.from(sKey, "hex"),
desIV,
);
gsDecipher.setAutoPadding(false);
const gsDecipher = createDecipheriv(
"des-cbc",
Buffer.from(sKey, "hex"),
desIV,
);
gsDecipher.setAutoPadding(false);

return new McosEncryptionPair(gsCipher, gsDecipher);
return new McosEncryptionPair(gsCipher, gsDecipher);
}

/**
Expand All @@ -96,17 +101,17 @@ export function createCommandEncryptionPair(key: string): McosEncryptionPair {
* @throws Error if the key is too short
*/
export function createDataEncryptionPair(key: string): McosEncryptionPair {
if (key.length < 16) {
throw Error(`Key too short: ${key}`);
}
if (key.length < 16) {
throw Error(`Key too short: ${key}`);
}

const stringKey = Buffer.from(key, "hex");
const stringKey = Buffer.from(key, "hex");

// File deepcode ignore InsecureCipher: RC4 is the encryption algorithum used here, file deepcode ignore HardcodedSecret: A blank IV is used here
const tsCipher = createCipheriv("rc4", stringKey.subarray(0, 16), "");
const tsDecipher = createDecipheriv("rc4", stringKey.subarray(0, 16), "");
// File deepcode ignore InsecureCipher: RC4 is the encryption algorithum used here, file deepcode ignore HardcodedSecret: A blank IV is used here
const tsCipher = createCipheriv("rc4", stringKey.subarray(0, 16), "");
const tsDecipher = createDecipheriv("rc4", stringKey.subarray(0, 16), "");

return new McosEncryptionPair(tsCipher, tsDecipher);
return new McosEncryptionPair(tsCipher, tsDecipher);
}

/**
Expand All @@ -116,11 +121,11 @@ export function createDataEncryptionPair(key: string): McosEncryptionPair {
* @throws Error if the server does not support the legacy ciphers
*/
export function verifyLegacyCipherSupport() {
const cipherList = getCiphers();
if (!cipherList.includes("des-cbc")) {
throw Error("DES-CBC cipher not available");
}
if (!cipherList.includes("rc4")) {
throw Error("RC4 cipher not available");
}
}
const cipherList = getCiphers();
if (!cipherList.includes("des-cbc")) {
throw Error("DES-CBC cipher not available");
}
if (!cipherList.includes("rc4")) {
throw Error("RC4 cipher not available");
}
}
9 changes: 6 additions & 3 deletions packages/main/src/handleUserLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function decryptSessionKey(
privateKey: string,
): string {
const sessionKeyStructure = crypto.privateDecrypt(
privateKey,
Buffer.from(encryptedSessionKey, "hex"),
privateKey,
Buffer.from(encryptedSessionKey, "hex"),
);

return sessionKeyStructure.toString("hex");
Expand All @@ -35,7 +35,10 @@ export function handleUserLogin(

const privateKey = loadPrivateKey("data/private_key.pem");

const sessionKey = decryptSessionKey(userLoginPayload.sessionKey.toString(), privateKey);
const sessionKey = decryptSessionKey(
userLoginPayload.sessionKey.toString(),
privateKey,
);

console.log(`Session key: ${Buffer.from(sessionKey, "hex").toString("hex")}`);

Expand Down

0 comments on commit 3ee02aa

Please sign in to comment.