From a54d28e0546052c7ad19988a82a22bbba2dcf9f0 Mon Sep 17 00:00:00 2001 From: Berend Sliedrecht Date: Fri, 15 Nov 2024 15:12:41 +0100 Subject: [PATCH] do not parse key when its not asn.1 Signed-off-by: Berend Sliedrecht --- src/SecureEnvironment.ts | 2 +- src/index.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/SecureEnvironment.ts b/src/SecureEnvironment.ts index b736286..486a69d 100644 --- a/src/SecureEnvironment.ts +++ b/src/SecureEnvironment.ts @@ -17,7 +17,7 @@ export interface SecureEnvironment { sign(keyId: string, message: Uint8Array, biometricsBacked?: boolean): Promise } -let isExpoSecureEnvironmentSupported: boolean +export let isExpoSecureEnvironmentSupported: boolean let fallbackSecureEnvironment: SecureEnvironment export const setFallbackSecureEnvironment = (env: SecureEnvironment) => { fallbackSecureEnvironment = env diff --git a/src/index.ts b/src/index.ts index 3edfbe5..0241a0e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,10 +12,15 @@ export async function generateKeypair(id: string, biometricsBacked = true): Prom export async function getPublicBytesForKeyId(keyId: string): Promise { const publicBytes = await getSecureEnvironment().getPublicBytesForKeyId(keyId) + let uncompressedKey = publicBytes - if (Platform.OS === 'android') { + if (Platform.OS === 'android' && publicBytes.length > 65) { + // Try to parse it from the ASN.1 SPKI format const spki = AsnParser.parse(publicBytes, SubjectPublicKeyInfo) - const uncompressedKey = new Uint8Array(spki.subjectPublicKey) + uncompressedKey = new Uint8Array(spki.subjectPublicKey) + } + + if (Platform.OS === 'android') { if (uncompressedKey.length !== 65 || uncompressedKey[0] !== 0x04) { throw new Error('Invalid uncompressed key format') } @@ -31,7 +36,6 @@ export async function getPublicBytesForKeyId(keyId: string): Promise const compressedKey = new Uint8Array(33) compressedKey[0] = prefix compressedKey.set(x, 1) - return compressedKey }