Skip to content

Commit

Permalink
Th rsa implementation (#40)
Browse files Browse the repository at this point in the history
* added rsa tagged

* some additions

* fully commented and added some functions

* added test for onsite public key

* added test for create user

* stuck making unit tests, will get help later

* trying out rsa to ec step, putting it into a try catch

* add comment to look into teams creation

* upped version

* added comment about the test function that creates teams
  • Loading branch information
THeflinKeeper authored Jan 4, 2024
1 parent b57b432 commit b3982c9
Show file tree
Hide file tree
Showing 14 changed files with 496 additions and 24 deletions.
4 changes: 2 additions & 2 deletions keeperapi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions keeperapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@keeper-security/keeperapi",
"description": "Keeper API Javascript SDK",
"version": "16.0.50",
"version": "16.0.51",
"browser": "dist/index.es.js",
"main": "dist/index.cjs.js",
"types": "dist/node/index.d.ts",
Expand All @@ -12,7 +12,7 @@
"build": "node ./scripts/cleanDistFolder.js && rollup -c && cp src/proto.d.ts dist",
"update-proto:es6": "pbjs -t static-module -w es6 -o src/proto.js ../../keeperapp-protobuf/APIRequest.proto ../../keeperapp-protobuf/AccountSummary.proto ../../keeperapp-protobuf/automator.proto ../../keeperapp-protobuf/breachwatch.proto ../../keeperapp-protobuf/client.proto ../../keeperapp-protobuf/externalservice.proto ../../keeperapp-protobuf/folder.proto ../../keeperapp-protobuf/push.proto ../../keeperapp-protobuf/record.proto ../../keeperapp-protobuf/servicelogger.proto ../../keeperapp-protobuf/ssocloud.proto ../../keeperapp-protobuf/token.proto ../../keeperapp-protobuf/upsell.proto ../../keeperapp-protobuf/SyncDown.proto && pbts -o src/proto.d.ts src/proto.js",
"update-proto:cjs": "pbjs -t json-module -w commonjs -o src/proto.js ../../keeperapp-protobuf/APIRequest.proto ../../keeperapp-protobuf/AccountSummary.proto ../../keeperapp-protobuf/automator.proto ../../keeperapp-protobuf/breachwatch.proto ../../keeperapp-protobuf/client.proto ../../keeperapp-protobuf/externalservice.proto ../../keeperapp-protobuf/folder.proto ../../keeperapp-protobuf/push.proto ../../keeperapp-protobuf/record.proto ../../keeperapp-protobuf/servicelogger.proto ../../keeperapp-protobuf/ssocloud.proto ../../keeperapp-protobuf/token.proto ../../keeperapp-protobuf/upsell.proto ../../keeperapp-protobuf/SyncDown.proto && pbjs -t static-module -w commonjs ../../keeperapp-protobuf/APIRequest.proto ../../keeperapp-protobuf/AccountSummary.proto ../../keeperapp-protobuf/automator.proto ../../keeperapp-protobuf/breachwatch.proto ../../keeperapp-protobuf/client.proto ../../keeperapp-protobuf/externalservice.proto ../../keeperapp-protobuf/folder.proto ../../keeperapp-protobuf/push.proto ../../keeperapp-protobuf/record.proto ../../keeperapp-protobuf/servicelogger.proto ../../keeperapp-protobuf/ssocloud.proto ../../keeperapp-protobuf/token.proto ../../keeperapp-protobuf/upsell.proto ../../keeperapp-protobuf/SyncDown.proto | pbts -o src/proto.d.ts -",
"test:2": "jest",
"test": "jest",
"types": "tsc --watch",
"types:ci": "tsc",
"prepublishOnly": "rollup -c && cp src/proto.d.ts dist",
Expand Down
75 changes: 75 additions & 0 deletions keeperapi/src/__tests__/createUserRequest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @jest-environment jsdom
*/

// @ts-ignore
import crypto from 'crypto'
import {nodePlatform} from "../node/platform";
import {browserPlatform} from "../browser/platform"
import {TextEncoder, TextDecoder} from 'util';
import {KeyWrapper, connectPlatform, platform} from "../platform";
import { Auth } from '../auth';
import { KeeperEnvironment } from '../endpoint';

Object.assign(global, {TextDecoder, TextEncoder})

Object.defineProperty(global.self, 'crypto', {
value: {
subtle: crypto.webcrypto.subtle,
getRandomValues: (array: any) => crypto.randomBytes(array.length)
}
})

describe('create user request', () => {

const username = 'username'
const password = 'password'
let auth: Auth

// needed to create auth initially
connectPlatform(browserPlatform)

beforeEach(() => {
auth = createAuth()
})

it('create user request', async () => {
connectPlatform(browserPlatform)
const kp = await platform.generateECKeyPair()
// @ts-expect-error private prop on class
const user = await auth.createUserRequest(kp.privateKey)

const {rsaPublicKey, rsaEncryptedPrivateKey, eccPublicKey, eccEncryptedPrivateKey, encryptedDeviceToken, encryptedClientKey, clientVersion} = user

expect(rsaPublicKey).toBeDefined()
expect(rsaPublicKey && rsaPublicKey.length === 270).toBeTruthy()

expect(rsaEncryptedPrivateKey).toBeDefined()
expect(rsaEncryptedPrivateKey && rsaEncryptedPrivateKey.length === 1216).toBeTruthy()

expect(eccPublicKey).toBeDefined()
expect(eccPublicKey && eccPublicKey.length === 65).toBeTruthy()

expect(eccEncryptedPrivateKey).toBeDefined()
expect(eccEncryptedPrivateKey && eccEncryptedPrivateKey.length === 60).toBeTruthy()

expect(encryptedDeviceToken).not.toBeDefined()

expect(encryptedClientKey).toBeDefined()
expect(encryptedClientKey && encryptedClientKey.length === 64).toBeTruthy()

expect(clientVersion).toBeDefined()
expect(clientVersion === 'ec0.0.0').toBeTruthy()
})
})

function createAuth(){
return new Auth({
host: KeeperEnvironment.DEV,
clientVersion: 'ec0.0.0',
})
}

function createKeyWrapper(key: Uint8Array) {
return KeyWrapper.create(key)
}
1 change: 0 additions & 1 deletion keeperapi/src/__tests__/crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {nodePlatform} from "../node/platform";
import {browserPlatform} from "../browser/platform"
import {publicKey, privateKey} from "./ecies-test-vectors";
import {TextEncoder, TextDecoder} from 'util';
import type {Platform} from "../platform";
import {connectPlatform, platform} from "../platform";

Object.assign(global, {TextDecoder, TextEncoder})
Expand Down
93 changes: 93 additions & 0 deletions keeperapi/src/__tests__/getOnsitePublicKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @jest-environment jsdom
*/

// @ts-ignore
import crypto from 'crypto'
import { browserPlatform } from '../browser/platform';
import {KeeperEndpoint} from '../endpoint'
import { nodePlatform } from '../node/platform'
import { connectPlatform } from '../platform'
// import NodeRSA from 'node-rsa';

Object.defineProperty(global.self, 'crypto', {
value: {
subtle: crypto.webcrypto.subtle,
getRandomValues: (array: any) => crypto.randomBytes(array.length)
}
})

describe('getOnsitePublicKey', () => {

let endpoint = new KeeperEndpoint({
host: 'testUrl',
deviceConfig: {
deviceName: 'test',
deviceToken: new Uint8Array(),
privateKey: new Uint8Array(),
publicKey: new Uint8Array(),
transmissionKeyId: 1,
},
})

beforeEach(() => {
endpoint = new KeeperEndpoint({
host: 'testUrl',
deviceConfig: {
deviceName: 'test',
deviceToken: new Uint8Array(),
privateKey: new Uint8Array(),
publicKey: new Uint8Array(),
transmissionKeyId: 1,
},
})
})

// NODE PLATFORM
it('(node) should return the rsa public key of the onsite keeper', async () => {
connectPlatform(nodePlatform)
const key = await endpoint.getOnsitePublicKey(false)

checkRSAKey(key)
// should node platform have a different length from browser?
expect(key).toHaveLength(392);
})

// NODE PLATFORM
it('(node) should return the ecc public key of the onsite keeper', async () => {
connectPlatform(nodePlatform)

const key = await endpoint.getOnsitePublicKey(true)
checkECCKey(key)
})

// BROWSER PLATFORM
it('(browser) should return the rsa public key of the onsite keeper', async () => {
connectPlatform(browserPlatform)

const key = await endpoint.getOnsitePublicKey(false)

checkRSAKey(key)
// should browser platform have a different length from node?
expect(key).toHaveLength(360);
})

// BROWSER PLATFORM
it('(browser) should return the ecc public key of the onsite keeper', async () => {
connectPlatform(browserPlatform)

const key = await endpoint.getOnsitePublicKey(true)
checkECCKey(key)
})
})

function checkRSAKey(key:string){
const beginningPart = key.match(/^MIIB/i)
const endingPart = key.match(/IDAQAB$/i)
expect(beginningPart).toBeTruthy();
expect(endingPart).toBeTruthy();
}

function checkECCKey(key:string){
expect(key).toHaveLength(87);
}
Loading

0 comments on commit b3982c9

Please sign in to comment.