Skip to content

Commit

Permalink
do not parse key when its not asn.1
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
Berend Sliedrecht committed Nov 15, 2024
1 parent 9706277 commit a54d28e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/SecureEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface SecureEnvironment {
sign(keyId: string, message: Uint8Array, biometricsBacked?: boolean): Promise<Uint8Array>
}

let isExpoSecureEnvironmentSupported: boolean
export let isExpoSecureEnvironmentSupported: boolean
let fallbackSecureEnvironment: SecureEnvironment
export const setFallbackSecureEnvironment = (env: SecureEnvironment) => {
fallbackSecureEnvironment = env
Expand Down
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ export async function generateKeypair(id: string, biometricsBacked = true): Prom

export async function getPublicBytesForKeyId(keyId: string): Promise<Uint8Array> {
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')
}
Expand All @@ -31,7 +36,6 @@ export async function getPublicBytesForKeyId(keyId: string): Promise<Uint8Array>
const compressedKey = new Uint8Array(33)
compressedKey[0] = prefix
compressedKey.set(x, 1)

return compressedKey
}

Expand Down

0 comments on commit a54d28e

Please sign in to comment.