0.1.0 • Published 9 months ago

expo-clave-tee v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

expo-clave-tee

Expo module for interacting with TEE

Features

  • Supports both Android and iOS
  • Selects best possible option to store credentials, and falls back to less secure options if necessary
  • Uses best practices to store credentials securely, CryptoKit on iOS and KeyStore on Android

API

import { 
    fetchPublicKey,
    createKeyPair,
    deleteKeyPair,
    sign,
    verify
} from "expo-clave-tee";
import { assert } from "assert";

async function main() {
    const ALIAS = "my-key-pair";
    // Create a key pair
    const pubKey1 = await createKeyPair(ALIAS);
    // Fetch the created key pair
    const pubKey2 = await fetchPublicKey(ALIAS);
    assert(pubKey1 === pubKey2);

    // Delete the key pair
    await deleteKeyPair(ALIAS);
    const pubKey3 = await fetchPublicKey(ALIAS);
    // fetchPublicKey returns undefined if the key pair does not exist
    assert(pubKey3 == undefined);

    // Sign a message
    // sign and verify functions accept hex strings as input
    const message = "Hello World!";
    const hexMessage = Buffer.from(message, "utf8").toString("hex");
    const signature = await sign(ALIAS, hexMessage);
    // Verify the signature
    const isVerified = await verify(pubKey1, hexMessage, signature);
    assert(isVerified);

    // Sign function accepts an optional prompt parameter
    const signature2 = await sign(ALIAS, hexMessage, {
        usageMessage: "Please sign this message",
        androidTitle: "Sign",
    });
}

main();

Error Codes

Each error in the module has an assigned error code to it. It follows this format:

E{`platform`}{`function_type`#02}{`error_type`#02}: {`error_message`}
  • platform:
    • 1: android
    • 2: ios
  • function_type:
    • 1: fetchPublicKey
    • 2: createKeyPair
    • 3: deleteKeyPair
    • 4: sign
    • 5: verify
  • error_type: Type of the error, differs on Android and iOS

In an example error: "E10102: Something is wrong"

  • android is the platform
  • Error occured from fetchPublicKey function
  • Error type is 2 (more details in Error Types)
  • Error message is "Something is wrong"

Error Types

PlatformCodeError
android10101Key not found in keychain
android10102Couldn't parse key in the keychain
android10201Key not found in keychain
android10202Couldn't parse key in the keychain
android10301Couldn't delete key, keystore has not been initialized, or if the entry cannot be removed
android10401Key not found in keychain
android10402Couldn't parse key in the keychain
android10403Biometric authentication failed for unknown reasons
android10404Biometric authentication wasn't valid and failed
android10501Key not found in keychain
android10502Couldn't parse key in the keychain
ios20101Couldn't convert the key in the keystore
ios20102Couldn't read the key from the keystore
ios20201Something is wrong with access control
ios20202Couldn't create key
ios20203Couldn't store key in the keychain
ios20204Couldn't convert the key in the keychain
ios20205Key not found in the keychain
ios20206Couldn't create key with context
ios20301Couldn't delete key from the keystore
ios20401Couldn't convert the key in the keychain
ios20402Couldn't read the key from the keystore
ios20403Key not found in the keychain
ios20404Couldn't create key with context
ios20501Couldn't convert the key in the keychain
ios20502Couldn't read the key from the keystore
ios20503Key not found in the keychain
ios20504Couldn't parse the signature

Running the example app

iOS

npm i
cd example
npm run ios

Android

In order to run it without a problem I had to follow these steps

  • Remove existing Java and Gradle installations
  • Install Java 17 (on MacOS, best way to do this is to use sdkman with sdk install java 17.0.4.1-tem)
  • Install Android Studio and create a new Android emulator
  • Start the emulator
  • Run the following commands

Optionally you can install Gradle 7.4, but it should be installed automatically by the npm run android command

npm i
cd example
npm run android