Skip to content

Commit b3982c9

Browse files
Th rsa implementation (#40)
* 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
1 parent b57b432 commit b3982c9

14 files changed

+496
-24
lines changed

keeperapi/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

keeperapi/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@keeper-security/keeperapi",
33
"description": "Keeper API Javascript SDK",
4-
"version": "16.0.50",
4+
"version": "16.0.51",
55
"browser": "dist/index.es.js",
66
"main": "dist/index.cjs.js",
77
"types": "dist/node/index.d.ts",
@@ -12,7 +12,7 @@
1212
"build": "node ./scripts/cleanDistFolder.js && rollup -c && cp src/proto.d.ts dist",
1313
"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",
1414
"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 -",
15-
"test:2": "jest",
15+
"test": "jest",
1616
"types": "tsc --watch",
1717
"types:ci": "tsc",
1818
"prepublishOnly": "rollup -c && cp src/proto.d.ts dist",
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
// @ts-ignore
6+
import crypto from 'crypto'
7+
import {nodePlatform} from "../node/platform";
8+
import {browserPlatform} from "../browser/platform"
9+
import {TextEncoder, TextDecoder} from 'util';
10+
import {KeyWrapper, connectPlatform, platform} from "../platform";
11+
import { Auth } from '../auth';
12+
import { KeeperEnvironment } from '../endpoint';
13+
14+
Object.assign(global, {TextDecoder, TextEncoder})
15+
16+
Object.defineProperty(global.self, 'crypto', {
17+
value: {
18+
subtle: crypto.webcrypto.subtle,
19+
getRandomValues: (array: any) => crypto.randomBytes(array.length)
20+
}
21+
})
22+
23+
describe('create user request', () => {
24+
25+
const username = 'username'
26+
const password = 'password'
27+
let auth: Auth
28+
29+
// needed to create auth initially
30+
connectPlatform(browserPlatform)
31+
32+
beforeEach(() => {
33+
auth = createAuth()
34+
})
35+
36+
it('create user request', async () => {
37+
connectPlatform(browserPlatform)
38+
const kp = await platform.generateECKeyPair()
39+
// @ts-expect-error private prop on class
40+
const user = await auth.createUserRequest(kp.privateKey)
41+
42+
const {rsaPublicKey, rsaEncryptedPrivateKey, eccPublicKey, eccEncryptedPrivateKey, encryptedDeviceToken, encryptedClientKey, clientVersion} = user
43+
44+
expect(rsaPublicKey).toBeDefined()
45+
expect(rsaPublicKey && rsaPublicKey.length === 270).toBeTruthy()
46+
47+
expect(rsaEncryptedPrivateKey).toBeDefined()
48+
expect(rsaEncryptedPrivateKey && rsaEncryptedPrivateKey.length === 1216).toBeTruthy()
49+
50+
expect(eccPublicKey).toBeDefined()
51+
expect(eccPublicKey && eccPublicKey.length === 65).toBeTruthy()
52+
53+
expect(eccEncryptedPrivateKey).toBeDefined()
54+
expect(eccEncryptedPrivateKey && eccEncryptedPrivateKey.length === 60).toBeTruthy()
55+
56+
expect(encryptedDeviceToken).not.toBeDefined()
57+
58+
expect(encryptedClientKey).toBeDefined()
59+
expect(encryptedClientKey && encryptedClientKey.length === 64).toBeTruthy()
60+
61+
expect(clientVersion).toBeDefined()
62+
expect(clientVersion === 'ec0.0.0').toBeTruthy()
63+
})
64+
})
65+
66+
function createAuth(){
67+
return new Auth({
68+
host: KeeperEnvironment.DEV,
69+
clientVersion: 'ec0.0.0',
70+
})
71+
}
72+
73+
function createKeyWrapper(key: Uint8Array) {
74+
return KeyWrapper.create(key)
75+
}

keeperapi/src/__tests__/crypto.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {nodePlatform} from "../node/platform";
88
import {browserPlatform} from "../browser/platform"
99
import {publicKey, privateKey} from "./ecies-test-vectors";
1010
import {TextEncoder, TextDecoder} from 'util';
11-
import type {Platform} from "../platform";
1211
import {connectPlatform, platform} from "../platform";
1312

1413
Object.assign(global, {TextDecoder, TextEncoder})
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
// @ts-ignore
6+
import crypto from 'crypto'
7+
import { browserPlatform } from '../browser/platform';
8+
import {KeeperEndpoint} from '../endpoint'
9+
import { nodePlatform } from '../node/platform'
10+
import { connectPlatform } from '../platform'
11+
// import NodeRSA from 'node-rsa';
12+
13+
Object.defineProperty(global.self, 'crypto', {
14+
value: {
15+
subtle: crypto.webcrypto.subtle,
16+
getRandomValues: (array: any) => crypto.randomBytes(array.length)
17+
}
18+
})
19+
20+
describe('getOnsitePublicKey', () => {
21+
22+
let endpoint = new KeeperEndpoint({
23+
host: 'testUrl',
24+
deviceConfig: {
25+
deviceName: 'test',
26+
deviceToken: new Uint8Array(),
27+
privateKey: new Uint8Array(),
28+
publicKey: new Uint8Array(),
29+
transmissionKeyId: 1,
30+
},
31+
})
32+
33+
beforeEach(() => {
34+
endpoint = new KeeperEndpoint({
35+
host: 'testUrl',
36+
deviceConfig: {
37+
deviceName: 'test',
38+
deviceToken: new Uint8Array(),
39+
privateKey: new Uint8Array(),
40+
publicKey: new Uint8Array(),
41+
transmissionKeyId: 1,
42+
},
43+
})
44+
})
45+
46+
// NODE PLATFORM
47+
it('(node) should return the rsa public key of the onsite keeper', async () => {
48+
connectPlatform(nodePlatform)
49+
const key = await endpoint.getOnsitePublicKey(false)
50+
51+
checkRSAKey(key)
52+
// should node platform have a different length from browser?
53+
expect(key).toHaveLength(392);
54+
})
55+
56+
// NODE PLATFORM
57+
it('(node) should return the ecc public key of the onsite keeper', async () => {
58+
connectPlatform(nodePlatform)
59+
60+
const key = await endpoint.getOnsitePublicKey(true)
61+
checkECCKey(key)
62+
})
63+
64+
// BROWSER PLATFORM
65+
it('(browser) should return the rsa public key of the onsite keeper', async () => {
66+
connectPlatform(browserPlatform)
67+
68+
const key = await endpoint.getOnsitePublicKey(false)
69+
70+
checkRSAKey(key)
71+
// should browser platform have a different length from node?
72+
expect(key).toHaveLength(360);
73+
})
74+
75+
// BROWSER PLATFORM
76+
it('(browser) should return the ecc public key of the onsite keeper', async () => {
77+
connectPlatform(browserPlatform)
78+
79+
const key = await endpoint.getOnsitePublicKey(true)
80+
checkECCKey(key)
81+
})
82+
})
83+
84+
function checkRSAKey(key:string){
85+
const beginningPart = key.match(/^MIIB/i)
86+
const endingPart = key.match(/IDAQAB$/i)
87+
expect(beginningPart).toBeTruthy();
88+
expect(endingPart).toBeTruthy();
89+
}
90+
91+
function checkECCKey(key:string){
92+
expect(key).toHaveLength(87);
93+
}

0 commit comments

Comments
 (0)