Skip to content

Commit

Permalink
Migrate RSA to SubtleCrypto (#132)
Browse files Browse the repository at this point in the history
* migrated rsa to subtle crypto

* moved to denoland/setup-deno
  • Loading branch information
MStefan99 authored Oct 27, 2022
1 parent 28f9935 commit 1622fe5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Install Deno 1.x
uses: denolib/setup-deno@master
uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- name: Check fmt
Expand Down Expand Up @@ -37,7 +37,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- name: Install Deno ${{ matrix.DENO_VERSION }}
uses: denolib/setup-deno@master
uses: denoland/setup-deno@v1
with:
deno-version: ${{ matrix.DENO_VERSION }}
- name: Show Deno version
Expand Down
33 changes: 21 additions & 12 deletions src/auth_plugin/caching_sha2_password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ interface handler {
}

let scramble: Uint8Array, password: string;
function start(scramble_: Uint8Array, password_: string): handler {

async function start(
scramble_: Uint8Array,
password_: string,
): Promise<handler> {
scramble = scramble_;
password = password_;
return { done: false, next: authMoreResponse };
return { done: false, next: await authMoreResponse };
}
function authMoreResponse(packet: ReceivePacket): handler {

async function authMoreResponse(packet: ReceivePacket): Promise<handler> {
const enum AuthStatusFlags {
FullAuth = 0x04,
FastPath = 0x03,
Expand All @@ -26,7 +31,7 @@ function authMoreResponse(packet: ReceivePacket): handler {
if (statusFlag === AuthStatusFlags.FullAuth) {
authMoreData = new Uint8Array([REQUEST_PUBLIC_KEY]);
done = false;
next = encryptWithKey;
next = await encryptWithKey;
}
if (statusFlag === AuthStatusFlags.FastPath) {
done = false;
Expand All @@ -36,30 +41,34 @@ function authMoreResponse(packet: ReceivePacket): handler {
return { done, next, quickRead, data: authMoreData };
}

function encryptWithKey(packet: ReceivePacket): handler {
async function encryptWithKey(packet: ReceivePacket): Promise<handler> {
const publicKey = parsePublicKey(packet);
const len = password.length;
let passwordBuffer: Uint8Array = new Uint8Array(len + 1);
const passwordBuffer: Uint8Array = new Uint8Array(len + 1);
for (let n = 0; n < len; n++) {
passwordBuffer[n] = password.charCodeAt(n);
}
passwordBuffer[len] = 0x00;

const encryptedPassword = encrypt(passwordBuffer, scramble, publicKey);
return { done: false, next: terminate, data: encryptedPassword };
const encryptedPassword = await encrypt(passwordBuffer, scramble, publicKey);
return {
done: false,
next: terminate,
data: new Uint8Array(encryptedPassword),
};
}

function parsePublicKey(packet: ReceivePacket): string {
return packet.body.skip(1).readNullTerminatedString();
}
function encrypt(

async function encrypt(
password: Uint8Array,
scramble: Uint8Array,
key: string,
): Uint8Array {
): Promise<ArrayBuffer> {
const stage1 = xor(password, scramble);
const encrypted = encryptWithPublicKey(key, stage1);
return encrypted;
return await encryptWithPublicKey(key, stage1);
}

function terminate() {
Expand Down
23 changes: 19 additions & 4 deletions src/auth_plugin/crypt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { RSA } from "https://deno.land/x/[email protected]/mod.ts";
function encryptWithPublicKey(key: string, data: Uint8Array): Uint8Array {
const publicKey = RSA.parseKey(key);
return RSA.encrypt(data, publicKey);
async function encryptWithPublicKey(
key: string,
data: Uint8Array,
): Promise<ArrayBuffer> {
const importedKey = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(key),
{ name: "RSA-OAEP", hash: "SHA-256" },
false,
["encrypt"],
);

return await crypto.subtle.encrypt(
{
name: "RSA-OAEP",
},
importedKey,
data,
);
}

export { encryptWithPublicKey };
2 changes: 1 addition & 1 deletion src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class Connection {

let result;
if (handler) {
result = handler.start(handshakePacket.seed, password!);
result = await handler.start(handshakePacket.seed, password!);
while (!result.done) {
if (result.data) {
const sequenceNumber = receive.header.no + 1;
Expand Down

0 comments on commit 1622fe5

Please sign in to comment.