Updated on 2026-08-14
This commit is contained in:
parent
2f125e24d9
commit
265cde188a
3 changed files with 16 additions and 14 deletions
|
|
@ -5,6 +5,7 @@ import com.tangem.commands.OpenSessionCommand
|
|||
import com.tangem.commands.ReadCommand
|
||||
import com.tangem.common.CompletionResult
|
||||
import com.tangem.common.apdu.CommandApdu
|
||||
import com.tangem.common.apdu.Instruction
|
||||
import com.tangem.common.apdu.ResponseApdu
|
||||
import com.tangem.common.extensions.calculateSha256
|
||||
import com.tangem.common.extensions.getType
|
||||
|
|
@ -221,16 +222,10 @@ class CardSession(
|
|||
scope.launch {
|
||||
subscription.consumeAsFlow()
|
||||
.filterNotNull()
|
||||
.map { establishEncryption() }
|
||||
.map { establishEncryption(apdu.ins) }
|
||||
.map { apdu.encrypt(environment.encryptionMode, environment.encryptionKey) }
|
||||
.map { encryptedApdu -> reader.transceiveApdu(encryptedApdu) }
|
||||
.catch { error ->
|
||||
if (error is TangemSdkError) callback(
|
||||
CompletionResult.Failure(
|
||||
error
|
||||
)
|
||||
)
|
||||
}
|
||||
.catch { if (it is TangemSdkError) callback(CompletionResult.Failure(it)) }
|
||||
.collect { result ->
|
||||
subscription.cancel()
|
||||
callback(result)
|
||||
|
|
@ -238,8 +233,15 @@ class CardSession(
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun establishEncryption(): CompletionResult<Boolean> {
|
||||
private suspend fun establishEncryption(ins: Int): CompletionResult<Boolean> {
|
||||
|
||||
if (environment.encryptionKey != null) return CompletionResult.Success(true)
|
||||
|
||||
if (ins == Instruction.Personalize.code) {
|
||||
environment.encryptionKey = null
|
||||
return CompletionResult.Success(true)
|
||||
}
|
||||
|
||||
val encryptionHelper: EncryptionHelper =
|
||||
when (environment.encryptionMode) {
|
||||
EncryptionMode.NONE -> return CompletionResult.Success(true)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.tangem.commands.personalization
|
||||
|
||||
import com.tangem.CardSession
|
||||
import com.tangem.EncryptionMode
|
||||
import com.tangem.SessionEnvironment
|
||||
import com.tangem.TangemSdkError
|
||||
import com.tangem.commands.Card
|
||||
|
|
@ -46,10 +46,11 @@ class PersonalizeCommand(
|
|||
|
||||
override fun serialize(environment: SessionEnvironment): CommandApdu {
|
||||
return CommandApdu(Instruction.Personalize, serializePersonalizationData(config))
|
||||
.encrypt(EncryptionMode.NONE, devPersonalizationKey)
|
||||
}
|
||||
|
||||
override fun deserialize(environment: SessionEnvironment, apdu: ResponseApdu): Card {
|
||||
return CardDeserializer.deserialize(apdu)
|
||||
return CardDeserializer.deserialize(apdu.decrypt(devPersonalizationKey))
|
||||
}
|
||||
|
||||
private fun serializePersonalizationData(config: CardConfig): ByteArray {
|
||||
|
|
|
|||
|
|
@ -72,9 +72,8 @@ class CommandApdu(
|
|||
encryptionMode: EncryptionMode,
|
||||
encryptionKey: ByteArray?
|
||||
): CommandApdu {
|
||||
if (encryptionMode == EncryptionMode.NONE
|
||||
|| encryptionKey == null || p1 != EncryptionMode.NONE.code
|
||||
) {
|
||||
|
||||
if (encryptionKey == null || p1 != EncryptionMode.NONE.code) {
|
||||
return this
|
||||
}
|
||||
val crc: ByteArray = tlvs.calculateCrc16()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue