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

Signing and key derivation: support configuring auth prompt texts #2286

Merged
merged 1 commit into from
Jan 25, 2024
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
30 changes: 30 additions & 0 deletions doc/api/SubtleCrypto.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@
{
"name": "keyUsages",
"type": "string[]"
},
{
"name": "options",
"optional": true,
"type": {
"map": {
"authPromptTitle": { "type": "string", "optional": true },
"authPromptMessage": { "type": "string", "optional": true }
}
}
}
],
"returns": {
Expand Down Expand Up @@ -193,6 +203,16 @@
{
"name": "length",
"type": "number"
},
{
"name": "options",
"optional": true,
"type": {
"map": {
"authPromptTitle": { "type": "string", "optional": true },
"authPromptMessage": { "type": "string", "optional": true }
}
}
}
],
"returns": {
Expand Down Expand Up @@ -523,6 +543,16 @@
"TypedArray"
]
}
},
{
"name": "options",
"optional": true,
"type": {
"map": {
"authPromptTitle": { "type": "string", "optional": true },
"authPromptMessage": { "type": "string", "optional": true }
}
}
}
],
"returns": {
Expand Down
54 changes: 42 additions & 12 deletions src/tabris/Crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray
| Int16Array | Uint16Array | Int32Array | Uint32Array;

type SignatureAlgorithm = { name: 'ECDSAinDERFormat', hash: 'SHA-256' };
export type AuthPromptOptions = { authPromptTitle?: string, authPromptMessage?: string };

export default class Crypto {

Expand Down Expand Up @@ -113,17 +114,20 @@ class SubtleCrypto {
async deriveBits(
algorithm: Algorithm,
baseKey: CryptoKey,
length: number
length: number,
options: AuthPromptOptions = {}
): Promise<ArrayBuffer> {
if (arguments.length !== 3) {
throw new TypeError(`Expected 3 arguments, got ${arguments.length}`);
if (arguments.length < 3) {
throw new TypeError(`Expected at least 3 arguments, got ${arguments.length}`);
}
checkDeriveAlgorithm(algorithm);
checkType(baseKey, CryptoKey, {name: 'baseKey'});
checkType(length, Number, {name: 'length'});
checkAuthPromptOptions(options);
const nativeObject = new _CryptoKey();
try {
await nativeObject.derive(algorithm, baseKey, {length, name: 'AES-GCM'}, true, []);
await nativeObject.derive(
algorithm, baseKey, {length, name: 'AES-GCM'}, true, [], options.authPromptTitle, options.authPromptMessage);
return new Promise((onSuccess, onReject) =>
this._nativeObject.subtleExportKey('raw', nativeObject, onSuccess, onReject)
);
Expand All @@ -137,10 +141,11 @@ class SubtleCrypto {
baseKey: CryptoKey,
derivedKeyAlgorithm: {name: string, length: number},
extractable: boolean,
keyUsages: string[]
keyUsages: string[],
options: AuthPromptOptions = {}
): Promise<CryptoKey> {
if (arguments.length !== 5) {
throw new TypeError(`Expected 5 arguments, got ${arguments.length}`);
if (arguments.length < 5) {
throw new TypeError(`Expected at least 5 arguments, got ${arguments.length}`);
}
checkDeriveAlgorithm(algorithm);
allowOnlyKeys(derivedKeyAlgorithm, ['name', 'length']);
Expand All @@ -149,8 +154,17 @@ class SubtleCrypto {
checkType(baseKey, CryptoKey, {name: 'baseKey'});
checkType(extractable, Boolean, {name: 'extractable'});
checkType(keyUsages, Array, {name: 'keyUsages'});
checkAuthPromptOptions(options);
const nativeObject = new _CryptoKey();
await nativeObject.derive(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages);
await nativeObject.derive(
algorithm,
baseKey,
derivedKeyAlgorithm,
extractable,
keyUsages,
options.authPromptTitle,
options.authPromptMessage
);
return new CryptoKey(nativeObject, {
algorithm,
extractable,
Expand Down Expand Up @@ -260,10 +274,11 @@ class SubtleCrypto {
async sign(
algorithm: SignatureAlgorithm,
key: CryptoKey,
data: ArrayBuffer | TypedArray
data: ArrayBuffer | TypedArray,
options: AuthPromptOptions = {}
): Promise<ArrayBuffer> {
if (arguments.length !== 3) {
throw new TypeError(`Expected 3 arguments, got ${arguments.length}`);
if (arguments.length < 3) {
throw new TypeError(`Expected at least 3 arguments, got ${arguments.length}`);
}
allowOnlyKeys(algorithm, ['name', 'hash']);
allowOnlyValues(algorithm.name, ['ECDSAinDERFormat'], 'algorithm.name');
Expand All @@ -272,8 +287,10 @@ class SubtleCrypto {
checkType(algorithm.name, String, {name: 'algorithm.name'});
checkType(algorithm.hash, String, {name: 'algorithm.hash'});
checkType(getBuffer(data), ArrayBuffer, {name: 'data'});
checkAuthPromptOptions(options);
return new Promise((onSuccess, onError) =>
this._nativeObject.subtleSign(algorithm, key, data, onSuccess, onError)
this._nativeObject.subtleSign(
algorithm, key, data, options.authPromptTitle, options.authPromptMessage, onSuccess, onError)
);
}

Expand Down Expand Up @@ -413,13 +430,17 @@ class NativeCrypto extends NativeObject {
algorithm: SignatureAlgorithm,
key: CryptoKey,
data: ArrayBuffer | TypedArray,
authPromptTitle: string | undefined,
authPromptMessage: string | undefined,
onSuccess: (buffer: ArrayBuffer) => any,
onError: (ex: Error) => any
): void {
this._nativeCall('subtleSign', {
algorithm,
key: getCid(key),
data: getBuffer(data),
authPromptTitle,
authPromptMessage,
onSuccess,
onError: (reason: unknown) => onError(new Error(String(reason)))
});
Expand All @@ -445,6 +466,15 @@ class NativeCrypto extends NativeObject {

}

function checkAuthPromptOptions(options: AuthPromptOptions) {
if ('authPromptTitle' in options) {
checkType(options.authPromptTitle, String, {name: 'options.authPromptTitle'});
}
if ('authPromptMessage' in options) {
checkType(options.authPromptMessage, String, {name: 'options.authPromptMessage'});
}
}

function checkDeriveAlgorithm(algorithm: Algorithm):
asserts algorithm is (AlgorithmHKDF | AlgorithmECDH | 'HKDF')
{
Expand Down
8 changes: 7 additions & 1 deletion src/tabris/CryptoKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export class _CryptoKey extends NativeObject {
baseKey: CryptoKey,
derivedKeyAlgorithm: {name: string, length: number},
extractable: boolean,
keyUsages: string[]
keyUsages: string[],
authPromptTitle?: string,
authPromptMessage?: string
): Promise<void> {
return new Promise((onSuccess, onError) => {
if (typeof algorithm === 'string') {
Expand All @@ -94,6 +96,8 @@ export class _CryptoKey extends NativeObject {
derivedKeyAlgorithm,
extractable,
keyUsages,
authPromptTitle,
authPromptMessage,
onSuccess,
onError: wrapErrorCb(onError)
});
Expand All @@ -104,6 +108,8 @@ export class _CryptoKey extends NativeObject {
derivedKeyAlgorithm,
extractable,
keyUsages,
authPromptTitle,
authPromptMessage,
onSuccess,
onError: wrapErrorCb(onError)
});
Expand Down
49 changes: 46 additions & 3 deletions test/tabris/Crypto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe('Crypto', function() {
it('checks parameter length', async function() {
params.pop();
await expect(deriveKey())
.rejectedWith(TypeError, 'Expected 5 arguments, got 4');
.rejectedWith(TypeError, 'Expected at least 5 arguments, got 4');
expect(client.calls({op: 'create', type: 'tabris.CryptoKey'}).length).to.equal(0);
});

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

it('checks options.authPromptTitle', async function() {
params[5] = {authPromptTitle: null};
await expect(deriveKey())
.rejectedWith(TypeError, 'Expected options.authPromptTitle to be a string, got null.');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

it('checks options.authPromptMessage', async function() {
params[5] = {authPromptMessage: null};
await expect(deriveKey())
.rejectedWith(TypeError, 'Expected options.authPromptMessage to be a string, got null.');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

});

describe('subtle.deriveBits()', function() {
Expand Down Expand Up @@ -502,7 +516,7 @@ describe('Crypto', function() {

it('checks parameter length', async function() {
params.pop();
await expect(deriveBits()).rejectedWith(TypeError, 'Expected 3 arguments, got 2');
await expect(deriveBits()).rejectedWith(TypeError, 'Expected at least 3 arguments, got 2');
expect(client.calls({op: 'create', type: 'tabris.CryptoKey'}).length).to.equal(0);
});

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

it('checks options.authPromptTitle', async function() {
params[3] = {authPromptTitle: null};
await expect(deriveBits())
.rejectedWith(TypeError, 'Expected options.authPromptTitle to be a string, got null.');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

it('checks options.authPromptMessage', async function() {
params[3] = {authPromptMessage: null};
await expect(deriveBits())
.rejectedWith(TypeError, 'Expected options.authPromptMessage to be a string, got null.');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

});

describe('subtle.importKey()', function() {
Expand Down Expand Up @@ -1189,7 +1217,7 @@ describe('Crypto', function() {
it('checks parameter length', async function() {
params.pop();
await expect(sign())
.rejectedWith(TypeError, 'Expected 3 arguments, got 2');
.rejectedWith(TypeError, 'Expected at least 3 arguments, got 2');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

Expand Down Expand Up @@ -1220,6 +1248,21 @@ describe('Crypto', function() {
.rejectedWith(TypeError, 'Expected data to be of type ArrayBuffer, got null');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

it('checks options.authPromptTitle', async function() {
params[3] = {authPromptTitle: null};
await expect(sign())
.rejectedWith(TypeError, 'Expected options.authPromptTitle to be a string, got null.');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

it('checks options.authPromptMessage', async function() {
params[3] = {authPromptMessage: null};
await expect(sign())
.rejectedWith(TypeError, 'Expected options.authPromptMessage to be a string, got null.');
expect(client.calls({op: 'call', method: 'subtleSign'}).length).to.equal(0);
});

});

describe('subtle.verify()', function() {
Expand Down