@dynamic-labs/legacy-react-native-extension 5.4.0 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/KeyStoreKeyManager.kt +71 -3
- package/android/KeychainModule.kt +8 -0
- package/android/dynamic/keychain/KeyStoreKeyManager.kt +71 -3
- package/android/dynamic/keychain/KeychainModule.kt +8 -0
- package/android/java/xyz/dynamic/keychain/KeyStoreKeyManager.kt +71 -3
- package/android/java/xyz/dynamic/keychain/KeychainModule.kt +8 -0
- package/android/keychain/KeyStoreKeyManager.kt +71 -3
- package/android/keychain/KeychainModule.kt +8 -0
- package/android/main/java/xyz/dynamic/keychain/KeyStoreKeyManager.kt +71 -3
- package/android/main/java/xyz/dynamic/keychain/KeychainModule.kt +8 -0
- package/android/src/main/java/xyz/dynamic/keychain/KeyStoreKeyManager.kt +71 -3
- package/android/src/main/java/xyz/dynamic/keychain/KeychainModule.kt +8 -0
- package/android/xyz/dynamic/keychain/KeyStoreKeyManager.kt +71 -3
- package/android/xyz/dynamic/keychain/KeychainModule.kt +8 -0
- package/index.cjs +225 -11
- package/index.js +226 -12
- package/ios/KeychainModule.swift +4 -0
- package/ios/SecureEnclaveKeyManager.swift +121 -0
- package/package.json +13 -7
- package/src/ReactNativeExtension/setupStorageHandler/setupStorageHandler.d.ts +11 -0
- package/src/ReactNativeExtension/setupStorageHandler/utils/legacyExpoSecureStoreRecovery/index.d.ts +2 -0
- package/src/ReactNativeExtension/setupStorageHandler/utils/legacyExpoSecureStoreRecovery/withLegacyExpoSecureStoreRecovery.d.ts +38 -0
- package/src/nativeModules/Keychain.d.ts +6 -0
|
@@ -40,9 +40,11 @@ class KeyStoreKeyManager {
|
|
|
40
40
|
|
|
41
41
|
fun generateKeyPair(alias: String): String {
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
//
|
|
43
|
+
// Replace any existing key so the alias holds exactly one key and
|
|
44
|
+
// callers never have to pre-delete. iOS does the same explicitly in
|
|
45
|
+
// `SecureEnclaveKeyManager.generateKeyPair` — there the keychain would
|
|
46
|
+
// otherwise keep several keys under one application tag and resolve
|
|
47
|
+
// lookups to an arbitrary one.
|
|
46
48
|
if (hasKey(alias)) {
|
|
47
49
|
deleteKey(alias)
|
|
48
50
|
}
|
|
@@ -109,6 +111,30 @@ class KeyStoreKeyManager {
|
|
|
109
111
|
keyStore.deleteEntry(alias)
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Reports whether the key under [alias] is the one whose public key is
|
|
116
|
+
* [expectedPublicKey]. Deletes nothing.
|
|
117
|
+
*
|
|
118
|
+
* The iOS counterpart genuinely prunes, because there an application tag can
|
|
119
|
+
* hold several Secure Enclave keys and a lookup resolves to an unspecified
|
|
120
|
+
* one of them. A KeyStore alias holds at most one key, so there is nothing
|
|
121
|
+
* ambiguous to resolve here — this only answers the question, letting the
|
|
122
|
+
* caller treat both platforms uniformly. A non-matching key is deliberately
|
|
123
|
+
* left in place: it may belong to a live session, and the caller has no way
|
|
124
|
+
* to know that from here.
|
|
125
|
+
*
|
|
126
|
+
* [expectedPublicKey] is accepted as base64url or hex, compressed (33 bytes)
|
|
127
|
+
* or uncompressed (65 bytes) SEC1; both sides are normalised to compressed
|
|
128
|
+
* form, which is the only representation in which two encodings of the same
|
|
129
|
+
* key compare equal.
|
|
130
|
+
*/
|
|
131
|
+
fun matchesPublicKey(alias: String, expectedPublicKey: String): Boolean {
|
|
132
|
+
val expected = normalizedCompressedPublicKey(expectedPublicKey) ?: return false
|
|
133
|
+
val actual = getPublicKey(alias)?.let { normalizedCompressedPublicKey(it) } ?: return false
|
|
134
|
+
|
|
135
|
+
return expected.contentEquals(actual)
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
// region Private helpers
|
|
113
139
|
|
|
114
140
|
private fun loadKeyStore(): KeyStore {
|
|
@@ -139,6 +165,48 @@ class KeyStoreKeyManager {
|
|
|
139
165
|
)
|
|
140
166
|
}
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Decodes a public key given as base64url or hex and returns it in
|
|
170
|
+
* compressed SEC1 form (33 bytes: 02/03 || x). Keep in sync with
|
|
171
|
+
* `SecureEnclaveKeyManager.normalizedCompressedPublicKey` on iOS.
|
|
172
|
+
*/
|
|
173
|
+
private fun normalizedCompressedPublicKey(encoded: String): ByteArray? {
|
|
174
|
+
val candidates = listOf(
|
|
175
|
+
runCatching { base64urlDecode(encoded) }.getOrNull(),
|
|
176
|
+
hexDecode(encoded),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
return candidates.firstNotNullOfOrNull { it?.let(::compressSec1PublicKey) }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compresses an uncompressed SEC1 point (04 || x || y) to 02/03 || x, the
|
|
184
|
+
* prefix encoding the parity of y. Already-compressed input is returned
|
|
185
|
+
* unchanged.
|
|
186
|
+
*/
|
|
187
|
+
private fun compressSec1PublicKey(data: ByteArray): ByteArray? {
|
|
188
|
+
if (data.size == 33 && (data[0] == 0x02.toByte() || data[0] == 0x03.toByte())) {
|
|
189
|
+
return data
|
|
190
|
+
}
|
|
191
|
+
if (data.size != 65 || data[0] != 0x04.toByte()) {
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
val prefix = if (data[64].toInt() % 2 == 0) 0x02.toByte() else 0x03.toByte()
|
|
196
|
+
|
|
197
|
+
return byteArrayOf(prefix) + data.copyOfRange(1, 33)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun hexDecode(input: String): ByteArray? {
|
|
201
|
+
if (input.isEmpty() || input.length % 2 != 0) return null
|
|
202
|
+
|
|
203
|
+
return runCatching {
|
|
204
|
+
ByteArray(input.length / 2) { index ->
|
|
205
|
+
input.substring(index * 2, index * 2 + 2).toInt(16).toByte()
|
|
206
|
+
}
|
|
207
|
+
}.getOrNull()
|
|
208
|
+
}
|
|
209
|
+
|
|
142
210
|
companion object {
|
|
143
211
|
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
|
144
212
|
|
|
@@ -67,5 +67,13 @@ class KeychainModule : Module() {
|
|
|
67
67
|
promise.reject("ERR_KEYCHAIN", "Failed to delete key: ${e.message}", e)
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
AsyncFunction("pruneToPublicKey") { key: String, publicKey: String, promise: Promise ->
|
|
72
|
+
try {
|
|
73
|
+
promise.resolve(keyManager.matchesPublicKey(key, publicKey))
|
|
74
|
+
} catch (e: Exception) {
|
|
75
|
+
promise.reject("ERR_KEYCHAIN", "Failed to prune keys: ${e.message}", e)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
70
78
|
}
|
|
71
79
|
}
|
|
@@ -40,9 +40,11 @@ class KeyStoreKeyManager {
|
|
|
40
40
|
|
|
41
41
|
fun generateKeyPair(alias: String): String {
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
//
|
|
43
|
+
// Replace any existing key so the alias holds exactly one key and
|
|
44
|
+
// callers never have to pre-delete. iOS does the same explicitly in
|
|
45
|
+
// `SecureEnclaveKeyManager.generateKeyPair` — there the keychain would
|
|
46
|
+
// otherwise keep several keys under one application tag and resolve
|
|
47
|
+
// lookups to an arbitrary one.
|
|
46
48
|
if (hasKey(alias)) {
|
|
47
49
|
deleteKey(alias)
|
|
48
50
|
}
|
|
@@ -109,6 +111,30 @@ class KeyStoreKeyManager {
|
|
|
109
111
|
keyStore.deleteEntry(alias)
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Reports whether the key under [alias] is the one whose public key is
|
|
116
|
+
* [expectedPublicKey]. Deletes nothing.
|
|
117
|
+
*
|
|
118
|
+
* The iOS counterpart genuinely prunes, because there an application tag can
|
|
119
|
+
* hold several Secure Enclave keys and a lookup resolves to an unspecified
|
|
120
|
+
* one of them. A KeyStore alias holds at most one key, so there is nothing
|
|
121
|
+
* ambiguous to resolve here — this only answers the question, letting the
|
|
122
|
+
* caller treat both platforms uniformly. A non-matching key is deliberately
|
|
123
|
+
* left in place: it may belong to a live session, and the caller has no way
|
|
124
|
+
* to know that from here.
|
|
125
|
+
*
|
|
126
|
+
* [expectedPublicKey] is accepted as base64url or hex, compressed (33 bytes)
|
|
127
|
+
* or uncompressed (65 bytes) SEC1; both sides are normalised to compressed
|
|
128
|
+
* form, which is the only representation in which two encodings of the same
|
|
129
|
+
* key compare equal.
|
|
130
|
+
*/
|
|
131
|
+
fun matchesPublicKey(alias: String, expectedPublicKey: String): Boolean {
|
|
132
|
+
val expected = normalizedCompressedPublicKey(expectedPublicKey) ?: return false
|
|
133
|
+
val actual = getPublicKey(alias)?.let { normalizedCompressedPublicKey(it) } ?: return false
|
|
134
|
+
|
|
135
|
+
return expected.contentEquals(actual)
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
// region Private helpers
|
|
113
139
|
|
|
114
140
|
private fun loadKeyStore(): KeyStore {
|
|
@@ -139,6 +165,48 @@ class KeyStoreKeyManager {
|
|
|
139
165
|
)
|
|
140
166
|
}
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Decodes a public key given as base64url or hex and returns it in
|
|
170
|
+
* compressed SEC1 form (33 bytes: 02/03 || x). Keep in sync with
|
|
171
|
+
* `SecureEnclaveKeyManager.normalizedCompressedPublicKey` on iOS.
|
|
172
|
+
*/
|
|
173
|
+
private fun normalizedCompressedPublicKey(encoded: String): ByteArray? {
|
|
174
|
+
val candidates = listOf(
|
|
175
|
+
runCatching { base64urlDecode(encoded) }.getOrNull(),
|
|
176
|
+
hexDecode(encoded),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
return candidates.firstNotNullOfOrNull { it?.let(::compressSec1PublicKey) }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compresses an uncompressed SEC1 point (04 || x || y) to 02/03 || x, the
|
|
184
|
+
* prefix encoding the parity of y. Already-compressed input is returned
|
|
185
|
+
* unchanged.
|
|
186
|
+
*/
|
|
187
|
+
private fun compressSec1PublicKey(data: ByteArray): ByteArray? {
|
|
188
|
+
if (data.size == 33 && (data[0] == 0x02.toByte() || data[0] == 0x03.toByte())) {
|
|
189
|
+
return data
|
|
190
|
+
}
|
|
191
|
+
if (data.size != 65 || data[0] != 0x04.toByte()) {
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
val prefix = if (data[64].toInt() % 2 == 0) 0x02.toByte() else 0x03.toByte()
|
|
196
|
+
|
|
197
|
+
return byteArrayOf(prefix) + data.copyOfRange(1, 33)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun hexDecode(input: String): ByteArray? {
|
|
201
|
+
if (input.isEmpty() || input.length % 2 != 0) return null
|
|
202
|
+
|
|
203
|
+
return runCatching {
|
|
204
|
+
ByteArray(input.length / 2) { index ->
|
|
205
|
+
input.substring(index * 2, index * 2 + 2).toInt(16).toByte()
|
|
206
|
+
}
|
|
207
|
+
}.getOrNull()
|
|
208
|
+
}
|
|
209
|
+
|
|
142
210
|
companion object {
|
|
143
211
|
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
|
144
212
|
|
|
@@ -67,5 +67,13 @@ class KeychainModule : Module() {
|
|
|
67
67
|
promise.reject("ERR_KEYCHAIN", "Failed to delete key: ${e.message}", e)
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
AsyncFunction("pruneToPublicKey") { key: String, publicKey: String, promise: Promise ->
|
|
72
|
+
try {
|
|
73
|
+
promise.resolve(keyManager.matchesPublicKey(key, publicKey))
|
|
74
|
+
} catch (e: Exception) {
|
|
75
|
+
promise.reject("ERR_KEYCHAIN", "Failed to prune keys: ${e.message}", e)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
70
78
|
}
|
|
71
79
|
}
|
|
@@ -40,9 +40,11 @@ class KeyStoreKeyManager {
|
|
|
40
40
|
|
|
41
41
|
fun generateKeyPair(alias: String): String {
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
//
|
|
43
|
+
// Replace any existing key so the alias holds exactly one key and
|
|
44
|
+
// callers never have to pre-delete. iOS does the same explicitly in
|
|
45
|
+
// `SecureEnclaveKeyManager.generateKeyPair` — there the keychain would
|
|
46
|
+
// otherwise keep several keys under one application tag and resolve
|
|
47
|
+
// lookups to an arbitrary one.
|
|
46
48
|
if (hasKey(alias)) {
|
|
47
49
|
deleteKey(alias)
|
|
48
50
|
}
|
|
@@ -109,6 +111,30 @@ class KeyStoreKeyManager {
|
|
|
109
111
|
keyStore.deleteEntry(alias)
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Reports whether the key under [alias] is the one whose public key is
|
|
116
|
+
* [expectedPublicKey]. Deletes nothing.
|
|
117
|
+
*
|
|
118
|
+
* The iOS counterpart genuinely prunes, because there an application tag can
|
|
119
|
+
* hold several Secure Enclave keys and a lookup resolves to an unspecified
|
|
120
|
+
* one of them. A KeyStore alias holds at most one key, so there is nothing
|
|
121
|
+
* ambiguous to resolve here — this only answers the question, letting the
|
|
122
|
+
* caller treat both platforms uniformly. A non-matching key is deliberately
|
|
123
|
+
* left in place: it may belong to a live session, and the caller has no way
|
|
124
|
+
* to know that from here.
|
|
125
|
+
*
|
|
126
|
+
* [expectedPublicKey] is accepted as base64url or hex, compressed (33 bytes)
|
|
127
|
+
* or uncompressed (65 bytes) SEC1; both sides are normalised to compressed
|
|
128
|
+
* form, which is the only representation in which two encodings of the same
|
|
129
|
+
* key compare equal.
|
|
130
|
+
*/
|
|
131
|
+
fun matchesPublicKey(alias: String, expectedPublicKey: String): Boolean {
|
|
132
|
+
val expected = normalizedCompressedPublicKey(expectedPublicKey) ?: return false
|
|
133
|
+
val actual = getPublicKey(alias)?.let { normalizedCompressedPublicKey(it) } ?: return false
|
|
134
|
+
|
|
135
|
+
return expected.contentEquals(actual)
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
// region Private helpers
|
|
113
139
|
|
|
114
140
|
private fun loadKeyStore(): KeyStore {
|
|
@@ -139,6 +165,48 @@ class KeyStoreKeyManager {
|
|
|
139
165
|
)
|
|
140
166
|
}
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Decodes a public key given as base64url or hex and returns it in
|
|
170
|
+
* compressed SEC1 form (33 bytes: 02/03 || x). Keep in sync with
|
|
171
|
+
* `SecureEnclaveKeyManager.normalizedCompressedPublicKey` on iOS.
|
|
172
|
+
*/
|
|
173
|
+
private fun normalizedCompressedPublicKey(encoded: String): ByteArray? {
|
|
174
|
+
val candidates = listOf(
|
|
175
|
+
runCatching { base64urlDecode(encoded) }.getOrNull(),
|
|
176
|
+
hexDecode(encoded),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
return candidates.firstNotNullOfOrNull { it?.let(::compressSec1PublicKey) }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compresses an uncompressed SEC1 point (04 || x || y) to 02/03 || x, the
|
|
184
|
+
* prefix encoding the parity of y. Already-compressed input is returned
|
|
185
|
+
* unchanged.
|
|
186
|
+
*/
|
|
187
|
+
private fun compressSec1PublicKey(data: ByteArray): ByteArray? {
|
|
188
|
+
if (data.size == 33 && (data[0] == 0x02.toByte() || data[0] == 0x03.toByte())) {
|
|
189
|
+
return data
|
|
190
|
+
}
|
|
191
|
+
if (data.size != 65 || data[0] != 0x04.toByte()) {
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
val prefix = if (data[64].toInt() % 2 == 0) 0x02.toByte() else 0x03.toByte()
|
|
196
|
+
|
|
197
|
+
return byteArrayOf(prefix) + data.copyOfRange(1, 33)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun hexDecode(input: String): ByteArray? {
|
|
201
|
+
if (input.isEmpty() || input.length % 2 != 0) return null
|
|
202
|
+
|
|
203
|
+
return runCatching {
|
|
204
|
+
ByteArray(input.length / 2) { index ->
|
|
205
|
+
input.substring(index * 2, index * 2 + 2).toInt(16).toByte()
|
|
206
|
+
}
|
|
207
|
+
}.getOrNull()
|
|
208
|
+
}
|
|
209
|
+
|
|
142
210
|
companion object {
|
|
143
211
|
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
|
144
212
|
|
|
@@ -67,5 +67,13 @@ class KeychainModule : Module() {
|
|
|
67
67
|
promise.reject("ERR_KEYCHAIN", "Failed to delete key: ${e.message}", e)
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
AsyncFunction("pruneToPublicKey") { key: String, publicKey: String, promise: Promise ->
|
|
72
|
+
try {
|
|
73
|
+
promise.resolve(keyManager.matchesPublicKey(key, publicKey))
|
|
74
|
+
} catch (e: Exception) {
|
|
75
|
+
promise.reject("ERR_KEYCHAIN", "Failed to prune keys: ${e.message}", e)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
70
78
|
}
|
|
71
79
|
}
|
|
@@ -40,9 +40,11 @@ class KeyStoreKeyManager {
|
|
|
40
40
|
|
|
41
41
|
fun generateKeyPair(alias: String): String {
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
//
|
|
43
|
+
// Replace any existing key so the alias holds exactly one key and
|
|
44
|
+
// callers never have to pre-delete. iOS does the same explicitly in
|
|
45
|
+
// `SecureEnclaveKeyManager.generateKeyPair` — there the keychain would
|
|
46
|
+
// otherwise keep several keys under one application tag and resolve
|
|
47
|
+
// lookups to an arbitrary one.
|
|
46
48
|
if (hasKey(alias)) {
|
|
47
49
|
deleteKey(alias)
|
|
48
50
|
}
|
|
@@ -109,6 +111,30 @@ class KeyStoreKeyManager {
|
|
|
109
111
|
keyStore.deleteEntry(alias)
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Reports whether the key under [alias] is the one whose public key is
|
|
116
|
+
* [expectedPublicKey]. Deletes nothing.
|
|
117
|
+
*
|
|
118
|
+
* The iOS counterpart genuinely prunes, because there an application tag can
|
|
119
|
+
* hold several Secure Enclave keys and a lookup resolves to an unspecified
|
|
120
|
+
* one of them. A KeyStore alias holds at most one key, so there is nothing
|
|
121
|
+
* ambiguous to resolve here — this only answers the question, letting the
|
|
122
|
+
* caller treat both platforms uniformly. A non-matching key is deliberately
|
|
123
|
+
* left in place: it may belong to a live session, and the caller has no way
|
|
124
|
+
* to know that from here.
|
|
125
|
+
*
|
|
126
|
+
* [expectedPublicKey] is accepted as base64url or hex, compressed (33 bytes)
|
|
127
|
+
* or uncompressed (65 bytes) SEC1; both sides are normalised to compressed
|
|
128
|
+
* form, which is the only representation in which two encodings of the same
|
|
129
|
+
* key compare equal.
|
|
130
|
+
*/
|
|
131
|
+
fun matchesPublicKey(alias: String, expectedPublicKey: String): Boolean {
|
|
132
|
+
val expected = normalizedCompressedPublicKey(expectedPublicKey) ?: return false
|
|
133
|
+
val actual = getPublicKey(alias)?.let { normalizedCompressedPublicKey(it) } ?: return false
|
|
134
|
+
|
|
135
|
+
return expected.contentEquals(actual)
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
// region Private helpers
|
|
113
139
|
|
|
114
140
|
private fun loadKeyStore(): KeyStore {
|
|
@@ -139,6 +165,48 @@ class KeyStoreKeyManager {
|
|
|
139
165
|
)
|
|
140
166
|
}
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Decodes a public key given as base64url or hex and returns it in
|
|
170
|
+
* compressed SEC1 form (33 bytes: 02/03 || x). Keep in sync with
|
|
171
|
+
* `SecureEnclaveKeyManager.normalizedCompressedPublicKey` on iOS.
|
|
172
|
+
*/
|
|
173
|
+
private fun normalizedCompressedPublicKey(encoded: String): ByteArray? {
|
|
174
|
+
val candidates = listOf(
|
|
175
|
+
runCatching { base64urlDecode(encoded) }.getOrNull(),
|
|
176
|
+
hexDecode(encoded),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
return candidates.firstNotNullOfOrNull { it?.let(::compressSec1PublicKey) }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compresses an uncompressed SEC1 point (04 || x || y) to 02/03 || x, the
|
|
184
|
+
* prefix encoding the parity of y. Already-compressed input is returned
|
|
185
|
+
* unchanged.
|
|
186
|
+
*/
|
|
187
|
+
private fun compressSec1PublicKey(data: ByteArray): ByteArray? {
|
|
188
|
+
if (data.size == 33 && (data[0] == 0x02.toByte() || data[0] == 0x03.toByte())) {
|
|
189
|
+
return data
|
|
190
|
+
}
|
|
191
|
+
if (data.size != 65 || data[0] != 0x04.toByte()) {
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
val prefix = if (data[64].toInt() % 2 == 0) 0x02.toByte() else 0x03.toByte()
|
|
196
|
+
|
|
197
|
+
return byteArrayOf(prefix) + data.copyOfRange(1, 33)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun hexDecode(input: String): ByteArray? {
|
|
201
|
+
if (input.isEmpty() || input.length % 2 != 0) return null
|
|
202
|
+
|
|
203
|
+
return runCatching {
|
|
204
|
+
ByteArray(input.length / 2) { index ->
|
|
205
|
+
input.substring(index * 2, index * 2 + 2).toInt(16).toByte()
|
|
206
|
+
}
|
|
207
|
+
}.getOrNull()
|
|
208
|
+
}
|
|
209
|
+
|
|
142
210
|
companion object {
|
|
143
211
|
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
|
144
212
|
|
|
@@ -67,5 +67,13 @@ class KeychainModule : Module() {
|
|
|
67
67
|
promise.reject("ERR_KEYCHAIN", "Failed to delete key: ${e.message}", e)
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
AsyncFunction("pruneToPublicKey") { key: String, publicKey: String, promise: Promise ->
|
|
72
|
+
try {
|
|
73
|
+
promise.resolve(keyManager.matchesPublicKey(key, publicKey))
|
|
74
|
+
} catch (e: Exception) {
|
|
75
|
+
promise.reject("ERR_KEYCHAIN", "Failed to prune keys: ${e.message}", e)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
70
78
|
}
|
|
71
79
|
}
|
|
@@ -40,9 +40,11 @@ class KeyStoreKeyManager {
|
|
|
40
40
|
|
|
41
41
|
fun generateKeyPair(alias: String): String {
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
//
|
|
43
|
+
// Replace any existing key so the alias holds exactly one key and
|
|
44
|
+
// callers never have to pre-delete. iOS does the same explicitly in
|
|
45
|
+
// `SecureEnclaveKeyManager.generateKeyPair` — there the keychain would
|
|
46
|
+
// otherwise keep several keys under one application tag and resolve
|
|
47
|
+
// lookups to an arbitrary one.
|
|
46
48
|
if (hasKey(alias)) {
|
|
47
49
|
deleteKey(alias)
|
|
48
50
|
}
|
|
@@ -109,6 +111,30 @@ class KeyStoreKeyManager {
|
|
|
109
111
|
keyStore.deleteEntry(alias)
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Reports whether the key under [alias] is the one whose public key is
|
|
116
|
+
* [expectedPublicKey]. Deletes nothing.
|
|
117
|
+
*
|
|
118
|
+
* The iOS counterpart genuinely prunes, because there an application tag can
|
|
119
|
+
* hold several Secure Enclave keys and a lookup resolves to an unspecified
|
|
120
|
+
* one of them. A KeyStore alias holds at most one key, so there is nothing
|
|
121
|
+
* ambiguous to resolve here — this only answers the question, letting the
|
|
122
|
+
* caller treat both platforms uniformly. A non-matching key is deliberately
|
|
123
|
+
* left in place: it may belong to a live session, and the caller has no way
|
|
124
|
+
* to know that from here.
|
|
125
|
+
*
|
|
126
|
+
* [expectedPublicKey] is accepted as base64url or hex, compressed (33 bytes)
|
|
127
|
+
* or uncompressed (65 bytes) SEC1; both sides are normalised to compressed
|
|
128
|
+
* form, which is the only representation in which two encodings of the same
|
|
129
|
+
* key compare equal.
|
|
130
|
+
*/
|
|
131
|
+
fun matchesPublicKey(alias: String, expectedPublicKey: String): Boolean {
|
|
132
|
+
val expected = normalizedCompressedPublicKey(expectedPublicKey) ?: return false
|
|
133
|
+
val actual = getPublicKey(alias)?.let { normalizedCompressedPublicKey(it) } ?: return false
|
|
134
|
+
|
|
135
|
+
return expected.contentEquals(actual)
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
// region Private helpers
|
|
113
139
|
|
|
114
140
|
private fun loadKeyStore(): KeyStore {
|
|
@@ -139,6 +165,48 @@ class KeyStoreKeyManager {
|
|
|
139
165
|
)
|
|
140
166
|
}
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Decodes a public key given as base64url or hex and returns it in
|
|
170
|
+
* compressed SEC1 form (33 bytes: 02/03 || x). Keep in sync with
|
|
171
|
+
* `SecureEnclaveKeyManager.normalizedCompressedPublicKey` on iOS.
|
|
172
|
+
*/
|
|
173
|
+
private fun normalizedCompressedPublicKey(encoded: String): ByteArray? {
|
|
174
|
+
val candidates = listOf(
|
|
175
|
+
runCatching { base64urlDecode(encoded) }.getOrNull(),
|
|
176
|
+
hexDecode(encoded),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
return candidates.firstNotNullOfOrNull { it?.let(::compressSec1PublicKey) }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compresses an uncompressed SEC1 point (04 || x || y) to 02/03 || x, the
|
|
184
|
+
* prefix encoding the parity of y. Already-compressed input is returned
|
|
185
|
+
* unchanged.
|
|
186
|
+
*/
|
|
187
|
+
private fun compressSec1PublicKey(data: ByteArray): ByteArray? {
|
|
188
|
+
if (data.size == 33 && (data[0] == 0x02.toByte() || data[0] == 0x03.toByte())) {
|
|
189
|
+
return data
|
|
190
|
+
}
|
|
191
|
+
if (data.size != 65 || data[0] != 0x04.toByte()) {
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
val prefix = if (data[64].toInt() % 2 == 0) 0x02.toByte() else 0x03.toByte()
|
|
196
|
+
|
|
197
|
+
return byteArrayOf(prefix) + data.copyOfRange(1, 33)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun hexDecode(input: String): ByteArray? {
|
|
201
|
+
if (input.isEmpty() || input.length % 2 != 0) return null
|
|
202
|
+
|
|
203
|
+
return runCatching {
|
|
204
|
+
ByteArray(input.length / 2) { index ->
|
|
205
|
+
input.substring(index * 2, index * 2 + 2).toInt(16).toByte()
|
|
206
|
+
}
|
|
207
|
+
}.getOrNull()
|
|
208
|
+
}
|
|
209
|
+
|
|
142
210
|
companion object {
|
|
143
211
|
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
|
144
212
|
|
|
@@ -67,5 +67,13 @@ class KeychainModule : Module() {
|
|
|
67
67
|
promise.reject("ERR_KEYCHAIN", "Failed to delete key: ${e.message}", e)
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
AsyncFunction("pruneToPublicKey") { key: String, publicKey: String, promise: Promise ->
|
|
72
|
+
try {
|
|
73
|
+
promise.resolve(keyManager.matchesPublicKey(key, publicKey))
|
|
74
|
+
} catch (e: Exception) {
|
|
75
|
+
promise.reject("ERR_KEYCHAIN", "Failed to prune keys: ${e.message}", e)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
70
78
|
}
|
|
71
79
|
}
|
|
@@ -40,9 +40,11 @@ class KeyStoreKeyManager {
|
|
|
40
40
|
|
|
41
41
|
fun generateKeyPair(alias: String): String {
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
//
|
|
45
|
-
//
|
|
43
|
+
// Replace any existing key so the alias holds exactly one key and
|
|
44
|
+
// callers never have to pre-delete. iOS does the same explicitly in
|
|
45
|
+
// `SecureEnclaveKeyManager.generateKeyPair` — there the keychain would
|
|
46
|
+
// otherwise keep several keys under one application tag and resolve
|
|
47
|
+
// lookups to an arbitrary one.
|
|
46
48
|
if (hasKey(alias)) {
|
|
47
49
|
deleteKey(alias)
|
|
48
50
|
}
|
|
@@ -109,6 +111,30 @@ class KeyStoreKeyManager {
|
|
|
109
111
|
keyStore.deleteEntry(alias)
|
|
110
112
|
}
|
|
111
113
|
|
|
114
|
+
/**
|
|
115
|
+
* Reports whether the key under [alias] is the one whose public key is
|
|
116
|
+
* [expectedPublicKey]. Deletes nothing.
|
|
117
|
+
*
|
|
118
|
+
* The iOS counterpart genuinely prunes, because there an application tag can
|
|
119
|
+
* hold several Secure Enclave keys and a lookup resolves to an unspecified
|
|
120
|
+
* one of them. A KeyStore alias holds at most one key, so there is nothing
|
|
121
|
+
* ambiguous to resolve here — this only answers the question, letting the
|
|
122
|
+
* caller treat both platforms uniformly. A non-matching key is deliberately
|
|
123
|
+
* left in place: it may belong to a live session, and the caller has no way
|
|
124
|
+
* to know that from here.
|
|
125
|
+
*
|
|
126
|
+
* [expectedPublicKey] is accepted as base64url or hex, compressed (33 bytes)
|
|
127
|
+
* or uncompressed (65 bytes) SEC1; both sides are normalised to compressed
|
|
128
|
+
* form, which is the only representation in which two encodings of the same
|
|
129
|
+
* key compare equal.
|
|
130
|
+
*/
|
|
131
|
+
fun matchesPublicKey(alias: String, expectedPublicKey: String): Boolean {
|
|
132
|
+
val expected = normalizedCompressedPublicKey(expectedPublicKey) ?: return false
|
|
133
|
+
val actual = getPublicKey(alias)?.let { normalizedCompressedPublicKey(it) } ?: return false
|
|
134
|
+
|
|
135
|
+
return expected.contentEquals(actual)
|
|
136
|
+
}
|
|
137
|
+
|
|
112
138
|
// region Private helpers
|
|
113
139
|
|
|
114
140
|
private fun loadKeyStore(): KeyStore {
|
|
@@ -139,6 +165,48 @@ class KeyStoreKeyManager {
|
|
|
139
165
|
)
|
|
140
166
|
}
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Decodes a public key given as base64url or hex and returns it in
|
|
170
|
+
* compressed SEC1 form (33 bytes: 02/03 || x). Keep in sync with
|
|
171
|
+
* `SecureEnclaveKeyManager.normalizedCompressedPublicKey` on iOS.
|
|
172
|
+
*/
|
|
173
|
+
private fun normalizedCompressedPublicKey(encoded: String): ByteArray? {
|
|
174
|
+
val candidates = listOf(
|
|
175
|
+
runCatching { base64urlDecode(encoded) }.getOrNull(),
|
|
176
|
+
hexDecode(encoded),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
return candidates.firstNotNullOfOrNull { it?.let(::compressSec1PublicKey) }
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Compresses an uncompressed SEC1 point (04 || x || y) to 02/03 || x, the
|
|
184
|
+
* prefix encoding the parity of y. Already-compressed input is returned
|
|
185
|
+
* unchanged.
|
|
186
|
+
*/
|
|
187
|
+
private fun compressSec1PublicKey(data: ByteArray): ByteArray? {
|
|
188
|
+
if (data.size == 33 && (data[0] == 0x02.toByte() || data[0] == 0x03.toByte())) {
|
|
189
|
+
return data
|
|
190
|
+
}
|
|
191
|
+
if (data.size != 65 || data[0] != 0x04.toByte()) {
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
val prefix = if (data[64].toInt() % 2 == 0) 0x02.toByte() else 0x03.toByte()
|
|
196
|
+
|
|
197
|
+
return byteArrayOf(prefix) + data.copyOfRange(1, 33)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private fun hexDecode(input: String): ByteArray? {
|
|
201
|
+
if (input.isEmpty() || input.length % 2 != 0) return null
|
|
202
|
+
|
|
203
|
+
return runCatching {
|
|
204
|
+
ByteArray(input.length / 2) { index ->
|
|
205
|
+
input.substring(index * 2, index * 2 + 2).toInt(16).toByte()
|
|
206
|
+
}
|
|
207
|
+
}.getOrNull()
|
|
208
|
+
}
|
|
209
|
+
|
|
142
210
|
companion object {
|
|
143
211
|
private const val ANDROID_KEYSTORE = "AndroidKeyStore"
|
|
144
212
|
|
|
@@ -67,5 +67,13 @@ class KeychainModule : Module() {
|
|
|
67
67
|
promise.reject("ERR_KEYCHAIN", "Failed to delete key: ${e.message}", e)
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
+
|
|
71
|
+
AsyncFunction("pruneToPublicKey") { key: String, publicKey: String, promise: Promise ->
|
|
72
|
+
try {
|
|
73
|
+
promise.resolve(keyManager.matchesPublicKey(key, publicKey))
|
|
74
|
+
} catch (e: Exception) {
|
|
75
|
+
promise.reject("ERR_KEYCHAIN", "Failed to prune keys: ${e.message}", e)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
70
78
|
}
|
|
71
79
|
}
|