Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generating and importing ECDSA keys #2279

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/CryptoKey.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"type": {
"map": {
"name": {
"type": "'ECDH'"
"type": { "union": ["'ECDH'", "'ECDSA'"] }
},
"namedCurve": {
"type": "'P-256'"
Expand Down
8 changes: 6 additions & 2 deletions doc/api/SubtleCrypto.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@
{
"map": {
"name": {
"type": "'ECDH'"
"type": {
"union": ["'ECDH'", "'ECDSA'"]
}
},
"namedCurve": {
"type": "'P-256'"
Expand Down Expand Up @@ -330,7 +332,9 @@
"type": {
"map": {
"name": {
"type": "'ECDH'"
"type": {
"union": ["'ECDH'", "'ECDSA'"]
}
},
"namedCurve": {
"type": "'P-256'"
Expand Down
19 changes: 13 additions & 6 deletions src/tabris/Crypto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import NativeObject from './NativeObject';
import {toValueString} from './Console';
import CryptoKey, {Algorithm, AlgorithmECDH, AlgorithmHKDF, AlgorithmInternal, _CryptoKey} from './CryptoKey';
import CryptoKey, {
Algorithm,
AlgorithmECDH,
AlgorithmECDSA,
AlgorithmHKDF,
AlgorithmInternal,
_CryptoKey
} from './CryptoKey';
import {allowOnlyKeys, allowOnlyValues, getBuffer, getCid, getNativeObject} from './util';
import checkType from './checkType';

Expand Down Expand Up @@ -75,11 +82,11 @@ class SubtleCrypto {
allowOnlyValues(format, ['spki', 'pkcs8', 'raw'], 'format');
checkType(getBuffer(keyData), ArrayBuffer, {name: 'keyData'});
if (typeof algorithm === 'string') {
allowOnlyValues(algorithm, ['ECDH', 'AES-GCM', 'HKDF'], 'algorithm');
allowOnlyValues(algorithm, ['AES-GCM', 'HKDF'], 'algorithm');
} else {
checkType(algorithm, Object, {name: 'algorithm'});
allowOnlyValues(algorithm.name, ['ECDH', 'AES-GCM'], 'algorithm.name');
if (algorithm.name === 'ECDH') {
allowOnlyValues(algorithm.name, ['ECDH', 'ECDSA', 'AES-GCM'], 'algorithm.name');
if (algorithm.name === 'ECDH' || algorithm.name === 'ECDSA') {
allowOnlyKeys(algorithm, ['name', 'namedCurve']);
allowOnlyValues(algorithm.namedCurve, ['P-256'], 'algorithm.namedCurve');
} else {
Expand Down Expand Up @@ -210,15 +217,15 @@ class SubtleCrypto {
}

async generateKey(
algorithm: AlgorithmECDH,
algorithm: AlgorithmECDH | AlgorithmECDSA,
extractable: boolean,
keyUsages: string[]
): Promise<{privateKey: CryptoKey, publicKey: CryptoKey}> {
if (arguments.length !== 3) {
throw new TypeError(`Expected 3 arguments, got ${arguments.length}`);
}
allowOnlyKeys(algorithm, ['name', 'namedCurve']);
allowOnlyValues(algorithm.name, ['ECDH'], 'algorithm.name');
allowOnlyValues(algorithm.name, ['ECDH', 'ECDSA'], 'algorithm.name');
allowOnlyValues(algorithm.namedCurve, ['P-256'], 'algorithm.namedCurve');
checkType(extractable, Boolean, {name: 'extractable'});
checkType(keyUsages, Array, {name: 'keyUsages'});
Expand Down
9 changes: 7 additions & 2 deletions src/tabris/CryptoKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {TypedArray} from './Crypto';
import NativeObject from './NativeObject';
import {getBuffer, getCid, setNativeObject} from './util';

export type AlgorithmInternal = AlgorithmHKDF | AlgorithmECDH | 'HKDF' | 'AES-GCM';
export type AlgorithmInternal = AlgorithmHKDF | AlgorithmECDH | AlgorithmECDSA | 'HKDF' | 'AES-GCM';

export type Algorithm = AlgorithmInternal | {name: 'AES-GCM'};

Expand All @@ -19,6 +19,11 @@ export type AlgorithmECDH = {
public?: CryptoKey
};

export type AlgorithmECDSA = {
name: 'ECDSA',
namedCurve: 'P-256'
};

export default class CryptoKey {

constructor(nativeObject: _CryptoKey, data: CryptoKey) {
Expand Down Expand Up @@ -107,7 +112,7 @@ export class _CryptoKey extends NativeObject {
}

async generate(
algorithm: AlgorithmECDH,
algorithm: AlgorithmECDH | AlgorithmECDSA,
extractable: boolean,
keyUsages: string[]
): Promise<void> {
Expand Down
12 changes: 10 additions & 2 deletions test/tabris/Crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ describe('Crypto', function() {
it('checks algorithm.name', async function() {
(params[2] as any).name = 'foo';
await expect(importKey())
.rejectedWith(TypeError, 'algorithm.name must be "ECDH" or "AES-GCM", got "foo"');
.rejectedWith(TypeError, 'algorithm.name must be "ECDH", "ECDSA" or "AES-GCM", got "foo"');
expect(client.calls({op: 'create', type: 'tabris.CryptoKey'}).length).to.equal(0);
});

Expand All @@ -665,6 +665,14 @@ describe('Crypto', function() {
expect(client.calls({op: 'create', type: 'tabris.CryptoKey'}).length).to.equal(0);
});

it('checks algorithm keys for ECDSA', async function() {
(params[2] as any).name = 'ECDSA';
(params[2] as any).foo = 'foo';
await expect(importKey())
.rejectedWith(TypeError, 'Object contains unexpected entry "foo"');
expect(client.calls({op: 'create', type: 'tabris.CryptoKey'}).length).to.equal(0);
});

it('checks algorithm keys for AES-GCM', async function() {
(params[2] as any) = {name: 'AES-GCM', namedCurve: 'P-256'};
await expect(importKey())
Expand Down Expand Up @@ -1069,7 +1077,7 @@ describe('Crypto', function() {
// @ts-ignore
params[0].name = 'foo';
await expect(generateKey())
.rejectedWith(TypeError, 'algorithm.name must be "ECDH", got "foo"');
.rejectedWith(TypeError, 'algorithm.name must be "ECDH" or "ECDSA", got "foo"');
expect(client.calls({op: 'create', type: 'tabris.CryptoKey'}).length).to.equal(0);
});

Expand Down