Updated on 2026-08-14

This commit is contained in:
Tangem 2020-04-15 14:38:55 +03:00
parent 230ef64f37
commit 9580b97747
11 changed files with 127 additions and 117 deletions

View file

@ -14,67 +14,50 @@ import java.util.*
/**
* Determines which type of data is required for signing.
*/
data class SigningMethod(val rawValue: Int) {
data class SigningMethodMask(val rawValue: Int) {
fun contains(value: Int): Boolean {
fun contains(signingMethod: SigningMethod): Boolean {
return if (rawValue and 0x80 == 0) {
value == rawValue
signingMethod.code == rawValue
} else {
rawValue and (0x01 shl value) != 0
rawValue and (0x01 shl signingMethod.code) != 0
}
}
}
companion object {
const val signHash = 0
const val signRaw = 1
const val signHashValidatedByIssuer = 2
const val signRawValidatedByIssuer = 3
const val signHashValidatedByIssuerAndWriteIssuerData = 4
const val signRawValidatedByIssuerAndWriteIssuerData = 5
const val signPos = 6
enum class SigningMethod(val code: Int) {
SIGN_HASH(0),
SIGN_RAW(1),
SIGN_HASH_VALIDATE_BY_ISSUER(2),
SIGN_RAW_VALIDATE_BY_ISSUER(3),
SIGN_HASH_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA(4),
SIGN_RAW_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA(5),
SIGN_POS(6)
}
fun build(
signHash: Boolean = false,
signRaw: Boolean = false,
signHashValidatedByIssuer: Boolean = false,
signRawValidatedByIssuer: Boolean = false,
signHashValidatedByIssuerAndWriteIssuerData: Boolean = false,
signRawValidatedByIssuerAndWriteIssuerData: Boolean = false,
signPos: Boolean = false
class SigningMethodMaskBuilder() {
): SigningMethod {
fun Boolean.toInt() = if (this) 1 else 0
private val signingMethods = mutableSetOf<SigningMethod>()
val signingMethodsCount = 0 +
signHash.toInt() +
signRaw.toInt() +
signHashValidatedByIssuer.toInt() +
signRawValidatedByIssuer.toInt() +
signHashValidatedByIssuerAndWriteIssuerData.toInt() +
signRawValidatedByIssuerAndWriteIssuerData.toInt() +
signPos.toInt()
fun add(signingMethod: SigningMethod) {
signingMethods.add(signingMethod)
}
var signingMethod: Int = 0
if (signingMethodsCount == 1) {
if (signHash) signingMethod += SigningMethod.signHash
if (signRaw) signingMethod += SigningMethod.signRaw
if (signHashValidatedByIssuer) signingMethod += SigningMethod.signHashValidatedByIssuer
if (signRawValidatedByIssuer) signingMethod += SigningMethod.signRawValidatedByIssuer
if (signHashValidatedByIssuerAndWriteIssuerData) signingMethod += SigningMethod.signHashValidatedByIssuerAndWriteIssuerData
if (signRawValidatedByIssuerAndWriteIssuerData) signingMethod += SigningMethod.signRawValidatedByIssuerAndWriteIssuerData
if (signPos) signingMethod += SigningMethod.signPos
} else if (signingMethodsCount > 1) {
signingMethod = 0x80
if (signHash) signingMethod += 0x01
if (signRaw) signingMethod += 0x01 shl SigningMethod.signRaw
if (signHashValidatedByIssuer) signingMethod += 0x01 shl SigningMethod.signHashValidatedByIssuer
if (signRawValidatedByIssuer) signingMethod += 0x01 shl SigningMethod.signRawValidatedByIssuer
if (signHashValidatedByIssuerAndWriteIssuerData) signingMethod += 0x01 shl SigningMethod.signHashValidatedByIssuerAndWriteIssuerData
if (signRawValidatedByIssuerAndWriteIssuerData) signingMethod += 0x01 shl SigningMethod.signRawValidatedByIssuerAndWriteIssuerData
if (signPos) signingMethod += 0x01 shl SigningMethod.signPos
fun build(): SigningMethodMask {
val rawValue: Int = when {
signingMethods.count() == 0 -> {
0
}
signingMethods.count() == 1 -> {
signingMethods.iterator().next().code
}
else -> {
signingMethods.fold(
0x80, { acc, singingMethod -> acc + (0x01 shl singingMethod.code) }
)
}
return SigningMethod(signingMethod)
}
return SigningMethodMask(rawValue)
}
}
@ -113,22 +96,23 @@ enum class CardStatus(val code: Int) {
*/
data class ProductMask(val rawValue: Int) {
fun contains(value: Int): Boolean = (rawValue and value) != 0
fun contains(product: Product): Boolean = (rawValue and product.code) != 0
companion object {
const val note = 0x01
const val tag = 0x02
const val idCard = 0x04
const val idIssuer = 0x08
}
}
enum class Product(val code: Int) {
NOTE(0x01),
TAG(0x02),
ID_CARD(0x04),
ID_ISSUER(0x08)
}
class ProductMaskBuilder() {
private var productMaskValue = 0
fun add(productCode: Int) {
productMaskValue = productMaskValue or productCode
fun add(product: Product) {
productMaskValue = productMaskValue or product.code
}
fun build() = ProductMask(productMaskValue)
@ -299,7 +283,7 @@ class Card(
/**
* Defines what data should be submitted to SIGN command.
*/
val signingMethod: SigningMethod?,
val signingMethods: SigningMethodMask?,
/**
* Delay in seconds before COS executes commands protected by PIN2.
@ -343,21 +327,21 @@ class Card(
val activationSeed: ByteArray?,
/**
* Returned only if [SigningMethod.SignPos] enabling POS transactions is supported by card.
* Returned only if [SigningMethod.SIGN_POS] enabling POS transactions is supported by card.
*/
val paymentFlowVersion: ByteArray?,
/**
* This value can be initialized by terminal and will be increased by COS on execution of every [SignCommand].
* For example, this field can store blockchain nonce for quick one-touch transaction on POS terminals.
* Returned only if [SigningMethod.SignPos] enabling POS transactions is supported by card.
* Returned only if [SigningMethod.SIGN_POS] enabling POS transactions is supported by card.
*/
val userCounter: Int?,
/**
* This value can be initialized by App (with PIN2 confirmation) and will be increased by COS
* with the execution of each [SignCommand]. For example, this field can store blockchain nonce
* for a quick one-touch transaction on POS terminals. Returned only if [SigningMethod.SignPos].
* for a quick one-touch transaction on POS terminals. Returned only if [SigningMethod.SIGN_POS].
*/
val userProtectedCounter: Int?,
@ -415,7 +399,7 @@ class ReadCommand : Command<Card>() {
issuerPublicKey = decoder.decodeOptional(TlvTag.IssuerDataPublicKey),
curve = decoder.decodeOptional(TlvTag.CurveId),
maxSignatures = decoder.decodeOptional(TlvTag.MaxSignatures),
signingMethod = decoder.decodeOptional(TlvTag.SigningMethod),
signingMethods = decoder.decodeOptional(TlvTag.SigningMethod),
pauseBeforePin2 = decoder.decodeOptional(TlvTag.PauseBeforePin2),
walletPublicKey = decoder.decodeOptional(TlvTag.WalletPublicKey),
walletRemainingSignatures = decoder.decodeOptional(TlvTag.RemainingSignatures),

View file

@ -60,7 +60,7 @@ class PersonalizeCommand(
issuerPublicKey = decoder.decodeOptional(TlvTag.IssuerDataPublicKey),
curve = decoder.decodeOptional(TlvTag.CurveId),
maxSignatures = decoder.decodeOptional(TlvTag.MaxSignatures),
signingMethod = decoder.decodeOptional(TlvTag.SigningMethod),
signingMethods = decoder.decodeOptional(TlvTag.SigningMethod),
pauseBeforePin2 = decoder.decodeOptional(TlvTag.PauseBeforePin2),
walletPublicKey = decoder.decodeOptional(TlvTag.WalletPublicKey),
walletRemainingSignatures = decoder.decodeOptional(TlvTag.RemainingSignatures),
@ -105,7 +105,7 @@ class PersonalizeCommand(
tlvBuilder.append(TlvTag.CardId, cardId)
tlvBuilder.append(TlvTag.CurveId, config.curveID)
tlvBuilder.append(TlvTag.MaxSignatures, config.maxSignatures)
tlvBuilder.append(TlvTag.SigningMethod, config.signingMethod)
tlvBuilder.append(TlvTag.SigningMethod, config.signingMethods)
tlvBuilder.append(TlvTag.SettingsMask, config.createSettingsMask())
tlvBuilder.append(TlvTag.PauseBeforePin2, config.pauseBeforePin2 / 10)
tlvBuilder.append(TlvTag.Cvc, config.cvc.toByteArray())

View file

@ -2,7 +2,7 @@ package com.tangem.commands.personalization.entities
import com.tangem.commands.CardData
import com.tangem.commands.EllipticCurve
import com.tangem.commands.SigningMethod
import com.tangem.commands.SigningMethodMask
data class NdefRecord(
val type: Type,
@ -33,7 +33,7 @@ data class CardConfig(
val pauseBeforePin2: Int,
val smartSecurityDelay: Boolean,
val curveID: EllipticCurve,
val signingMethod: SigningMethod,
val signingMethods: SigningMethodMask,
val maxSignatures: Int,
val isReusable: Boolean,
val allowSwapPin: Boolean,

View file

@ -122,9 +122,9 @@ class TlvDecoder(val tlvList: List<Tlv>) {
}
}
TlvValueType.SigningMethod -> {
typeCheck<T, SigningMethod>(tag)
typeCheck<T, SigningMethodMask>(tag)
try {
SigningMethod(tlvValue.toInt()) as T
SigningMethodMask(tlvValue.toInt()) as T
} catch (exception: Exception) {
logException(tag, tlvValue.toInt().toString(), exception)
throw SessionError.DecodingFailed()

View file

@ -87,8 +87,8 @@ class TlvEncoder {
(value as CardStatus).code.toByteArray()
}
TlvValueType.SigningMethod -> {
typeCheck<T, SigningMethod>(tag)
byteArrayOf((value as SigningMethod).rawValue.toByte())
typeCheck<T, SigningMethodMask>(tag)
byteArrayOf((value as SigningMethodMask).rawValue.toByte())
}
TlvValueType.IssuerDataMode -> {
typeCheck<T, IssuerDataMode>(tag)

View file

@ -20,7 +20,7 @@ internal class ScanTask : CardSessionRunnable<Card> {
if (card == null) {
callback(CompletionResult.Failure(SessionError.MissingPreflightRead()))
} else if (card.cardData?.productMask?.contains(ProductMask.tag) != false) {
} else if (card.cardData?.productMask?.contains(Product.TAG) != false) {
callback(CompletionResult.Success(card))
} else if (card.status != CardStatus.Loaded) {

View file

@ -82,8 +82,8 @@ class TlvDecoderTest {
@Test
fun `map SigningMethods single value returns correct value`() {
val signingMethods: SigningMethod = tlvMapper.decode(TlvTag.SigningMethod)
assertThat(signingMethods.contains(SigningMethod.signHash))
val signingMethods: SigningMethodMask = tlvMapper.decode(TlvTag.SigningMethod)
assertThat(signingMethods.contains(SigningMethod.SIGN_HASH))
.isTrue()
}
@ -91,20 +91,20 @@ class TlvDecoderTest {
fun `map SigningMethods set of methods returns correct value`() {
val localMapper = TlvDecoder(Tlv.deserialize("070195".hexToBytes())!!)
val signingMethod: SigningMethod = localMapper.decode(TlvTag.SigningMethod)
assertThat(signingMethod.contains(SigningMethod.signHash))
val signingMethods: SigningMethodMask = localMapper.decode(TlvTag.SigningMethod)
assertThat(signingMethods.contains(SigningMethod.SIGN_HASH))
.isTrue()
assertThat(signingMethod.contains(SigningMethod.signHashValidatedByIssuer))
assertThat(signingMethods.contains(SigningMethod.SIGN_HASH_VALIDATE_BY_ISSUER))
.isTrue()
assertThat(signingMethod.contains(SigningMethod.signHashValidatedByIssuerAndWriteIssuerData))
assertThat(signingMethods.contains(SigningMethod.SIGN_HASH_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA))
.isTrue()
assertThat(signingMethod.contains(SigningMethod.signRaw))
assertThat(signingMethods.contains(SigningMethod.SIGN_RAW))
.isFalse()
assertThat(signingMethod.contains(SigningMethod.signRawValidatedByIssuer))
assertThat(signingMethods.contains(SigningMethod.SIGN_RAW_VALIDATE_BY_ISSUER))
.isFalse()
assertThat(signingMethod.contains(SigningMethod.signRawValidatedByIssuerAndWriteIssuerData))
assertThat(signingMethods.contains(SigningMethod.SIGN_RAW_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA))
.isFalse()
assertThat(signingMethod.contains(SigningMethod.signPos))
assertThat(signingMethods.contains(SigningMethod.SIGN_POS))
.isFalse()
}
@ -119,7 +119,7 @@ class TlvDecoderTest {
fun `map ProductMask with raw value 5 returns correct value`() {
val localMapper = TlvDecoder(listOf(Tlv(TlvTag.ProductMask, byteArrayOf(5))))
val productMask: ProductMask = localMapper.decode(TlvTag.ProductMask)
assertThat(productMask.contains(ProductMask.note) && productMask.contains(ProductMask.idCard))
assertThat(productMask.contains(Product.NOTE) && productMask.contains(Product.ID_CARD))
.isTrue()
}
@ -127,7 +127,7 @@ class TlvDecoderTest {
fun `map ProductMask with raw value 1 returns correct value`() {
val localMapper = TlvDecoder(listOf(Tlv(TlvTag.ProductMask, byteArrayOf(1))))
val productMask: ProductMask = localMapper.decode(TlvTag.ProductMask)
assertThat(productMask.contains(ProductMask.note))
assertThat(productMask.contains(Product.NOTE))
.isTrue()
}

View file

@ -11,24 +11,38 @@ fun CardConfig.Companion.create(application: Application): CardConfig {
val preferences = application.getSharedPreferences("prefs", Context.MODE_PRIVATE)
val signingMethod = SigningMethod.build(
signHash = preferences.getBoolean("personalization_SigningMethod_0", false),
signRaw = preferences.getBoolean("personalization_SigningMethod_1", false),
signHashValidatedByIssuer = preferences.getBoolean("personalization_SigningMethod_2", false),
signRawValidatedByIssuer = preferences.getBoolean("personalization_SigningMethod_3", false),
signHashValidatedByIssuerAndWriteIssuerData = preferences.getBoolean("personalization_SigningMethod_4", false),
signRawValidatedByIssuerAndWriteIssuerData = preferences.getBoolean("personalization_SigningMethod_5", false),
signPos = preferences.getBoolean("personalization_SigningMethod_6", false)
)
val signingMethodMaskBuilder = SigningMethodMaskBuilder()
if (preferences.getBoolean("personalization_SigningMethod_0", false)) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH)
}
if (preferences.getBoolean("personalization_SigningMethod_1", false)) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_RAW)
}
if (preferences.getBoolean("personalization_SigningMethod_2", false)) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH_VALIDATE_BY_ISSUER)
}
if (preferences.getBoolean("personalization_SigningMethod_3", false)) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_RAW_VALIDATE_BY_ISSUER)
}
if (preferences.getBoolean("personalization_SigningMethod_4", false)) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA)
}
if (preferences.getBoolean("personalization_SigningMethod_5", false)) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_RAW_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA)
}
if (preferences.getBoolean("personalization_SigningMethod_6", false)) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH)
}
val signingMethod = signingMethodMaskBuilder.build()
val isNote = preferences.getBoolean("personalization_ProductMask_IsNote", true)
val isTag = preferences.getBoolean("personalization_ProductMask_IsTag", false)
val isIdCard = preferences.getBoolean("personalization_ProductMask_IsIDCard", false)
val productMaskBuilder = ProductMaskBuilder()
if (isNote) productMaskBuilder.add(ProductMask.note)
if (isTag) productMaskBuilder.add(ProductMask.tag)
if (isIdCard) productMaskBuilder.add(ProductMask.idCard)
if (isNote) productMaskBuilder.add(Product.NOTE)
if (isTag) productMaskBuilder.add(Product.TAG)
if (isIdCard) productMaskBuilder.add(Product.ID_CARD)
val productMask = productMaskBuilder.build()
var tokenSymbol: String? = null
@ -80,7 +94,7 @@ fun CardConfig.Companion.create(application: Application): CardConfig {
cardData = cardData,
curveID = EllipticCurve.byName(preferences.getString("personalization_CurveId", "secp256k1")!!)
?: EllipticCurve.Secp256k1,
signingMethod = signingMethod,
signingMethods = signingMethod,
createWallet = preferences.getBoolean("personalization_CreateWallet", true),
maxSignatures = preferences.getString("personalization_MaxSignatures", "1000")!!.toInt(),
isReusable = preferences.getBoolean("personalization_SettingsMask_IsReusable", true),

View file

@ -16,7 +16,7 @@ class ResponseJsonConverter {
private fun init(): Gson {
val builder = GsonBuilder().apply {
registerTypeAdapter(ByteArray::class.java, ByteTypeAdapter())
registerTypeAdapter(SigningMethod::class.java, SigningMethodTypeAdapter())
registerTypeAdapter(SigningMethodMask::class.java, SigningMethodTypeAdapter())
registerTypeAdapter(SettingsMask::class.java, SettingsMaskTypeAdapter())
registerTypeAdapter(ProductMask::class.java, ProductMaskTypeAdapter())
registerTypeAdapter(Date::class.java, DateTypeAdapter())
@ -50,8 +50,8 @@ class ProductMaskTypeAdapter : JsonSerializer<ProductMask> {
}
}
class SigningMethodTypeAdapter : JsonSerializer<SigningMethod> {
override fun serialize(src: SigningMethod, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
class SigningMethodTypeAdapter : JsonSerializer<SigningMethodMask> {
override fun serialize(src: SigningMethodMask, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
return JsonPrimitive(src.rawValue.toString())
}
}

View file

@ -1,8 +1,6 @@
package com.tangem.tangemtest.ucase.variants.personalize.converter
import com.tangem.commands.CardData
import com.tangem.commands.EllipticCurve
import com.tangem.commands.ProductMaskBuilder
import com.tangem.commands.*
import com.tangem.commands.personalization.entities.CardConfig
import com.tangem.commands.personalization.entities.NdefRecord
import com.tangem.tangemtest.ucase.variants.personalize.dto.PersonalizationConfig
@ -12,15 +10,29 @@ import java.util.*
class PersonalizationConfigToCardConfig : Converter<PersonalizationConfig, CardConfig> {
override fun convert(from: PersonalizationConfig): CardConfig {
val signingMethod = com.tangem.commands.SigningMethod.build(
signHash = from.SigningMethod0,
signRaw = from.SigningMethod1,
signHashValidatedByIssuer = from.SigningMethod2,
signRawValidatedByIssuer = from.SigningMethod3,
signHashValidatedByIssuerAndWriteIssuerData = from.SigningMethod4,
signRawValidatedByIssuerAndWriteIssuerData = from.SigningMethod5,
signPos = from.SigningMethod6
)
val signingMethodMaskBuilder = SigningMethodMaskBuilder()
if (from.SigningMethod0) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH)
}
if (from.SigningMethod1) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_RAW)
}
if (from.SigningMethod2) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH_VALIDATE_BY_ISSUER)
}
if (from.SigningMethod3) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_RAW_VALIDATE_BY_ISSUER)
}
if (from.SigningMethod4) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA)
}
if (from.SigningMethod5) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_RAW_VALIDATE_BY_ISSUER_WRITE_ISSUER_DATA)
}
if (from.SigningMethod6) {
signingMethodMaskBuilder.add(SigningMethod.SIGN_HASH)
}
val signingMethod = signingMethodMaskBuilder.build()
val isNote = from.cardData.product_note
val isTag = from.cardData.product_tag
@ -28,10 +40,10 @@ class PersonalizationConfigToCardConfig : Converter<PersonalizationConfig, CardC
val isIdIssuer = from.cardData.product_id_issuer
val productMaskBuilder = ProductMaskBuilder()
if (isNote) productMaskBuilder.add(com.tangem.commands.ProductMask.note)
if (isTag) productMaskBuilder.add(com.tangem.commands.ProductMask.tag)
if (isIdCard) productMaskBuilder.add(com.tangem.commands.ProductMask.idCard)
if (isIdIssuer) productMaskBuilder.add(com.tangem.commands.ProductMask.idIssuer)
if (isNote) productMaskBuilder.add(com.tangem.commands.Product.NOTE)
if (isTag) productMaskBuilder.add(com.tangem.commands.Product.TAG)
if (isIdCard) productMaskBuilder.add(com.tangem.commands.Product.ID_CARD)
if (isIdIssuer) productMaskBuilder.add(com.tangem.commands.Product.ID_ISSUER)
val productMask = productMaskBuilder.build()
var tokenSymbol: String? = null

View file

@ -56,7 +56,7 @@ class CardConverter : ModelToItems<Card> {
group.addItem(TextItem(CardId.issuerPublicKey, from.issuerPublicKey?.toHexString()))
group.addItem(TextItem(CardId.curve, stringOf(from.curve)))
group.addItem(TextItem(CardId.maxSignatures, stringOf(from.maxSignatures)))
group.addItem(TextItem(CardId.signingMethod, stringOf(from.signingMethod?.rawValue)))
group.addItem(TextItem(CardId.signingMethod, stringOf(from.signingMethods?.rawValue)))
group.addItem(TextItem(CardId.pauseBeforePin2, stringOf(from.pauseBeforePin2)))
group.addItem(TextItem(CardId.walletPublicKey, stringOf(from.walletPublicKey)))
group.addItem(TextItem(CardId.walletRemainingSignatures, stringOf(from.walletRemainingSignatures)))