From cabce8067e58b5e60288d16f1ef46d850a2447d1 Mon Sep 17 00:00:00 2001 From: Marten Rebane Date: Fri, 16 Oct 2026 17:57:00 +0300 Subject: [PATCH 01/15] Fix Junit imports --- libs/id-card-lib/build.gradle.kts | 15 +++++++++++++-- .../idcard/CodeNotActivatedExceptionTest.java | 2 +- .../java/ee/ria/DigiDoc/idcard/CodeTypeTest.java | 2 +- .../java/ee/ria/DigiDoc/idcard/ThalesTest.java | 8 ++++---- libs/smart-card-reader-lib/build.gradle.kts | 1 + .../CardConnectionLostExceptionTest.java | 2 +- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/libs/id-card-lib/build.gradle.kts b/libs/id-card-lib/build.gradle.kts index 1538780..0673901 100644 --- a/libs/id-card-lib/build.gradle.kts +++ b/libs/id-card-lib/build.gradle.kts @@ -13,6 +13,12 @@ android { minSdk = 34 } + testOptions { + unitTests.all { + it.useJUnitPlatform() + } + } + compileOptions { sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 @@ -30,8 +36,13 @@ dependencies { implementation(project(":libs:smart-card-reader-lib")) // Provided at runtime by the consuming app (RIA DigiDoc utils-lib) compileOnly(project(":libs:card-utils-lib")) + testImplementation(project(":libs:card-utils-lib")) - testImplementation(libs.junit) + testImplementation(platform(libs.junit.bom)) + testImplementation(libs.junit.jupiter) + testRuntimeOnly(libs.junit.platform.launcher) testImplementation(libs.mockito.core) - testImplementation(libs.truth) + testImplementation(libs.truth) { + exclude(group = "junit", module = "junit") + } } \ No newline at end of file diff --git a/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeNotActivatedExceptionTest.java b/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeNotActivatedExceptionTest.java index 5ee847e..07c8b8d 100644 --- a/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeNotActivatedExceptionTest.java +++ b/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeNotActivatedExceptionTest.java @@ -7,7 +7,7 @@ import ee.ria.DigiDoc.smartcardreader.SmartCardReaderException; -import org.junit.Test; +import org.junit.jupiter.api.Test; public final class CodeNotActivatedExceptionTest { diff --git a/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeTypeTest.java b/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeTypeTest.java index 0a1b6bd..9253a92 100644 --- a/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeTypeTest.java +++ b/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/CodeTypeTest.java @@ -5,7 +5,7 @@ import static com.google.common.truth.Truth.assertThat; -import org.junit.Test; +import org.junit.jupiter.api.Test; public final class CodeTypeTest { diff --git a/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/ThalesTest.java b/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/ThalesTest.java index e09e825..4340819 100644 --- a/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/ThalesTest.java +++ b/libs/id-card-lib/src/test/java/ee/ria/DigiDoc/idcard/ThalesTest.java @@ -3,7 +3,7 @@ package ee.ria.DigiDoc.idcard; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; @@ -16,8 +16,8 @@ import ee.ria.DigiDoc.smartcardreader.SmartCardReader; import ee.ria.DigiDoc.smartcardreader.SmartCardReaderException; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.MockMakers; public final class ThalesTest { @@ -25,7 +25,7 @@ public final class ThalesTest { private SmartCardReader reader; private Thales thales; - @Before + @BeforeEach public void before() { reader = mock(SmartCardReader.class, withSettings().mockMaker(MockMakers.SUBCLASS)); thales = new Thales(reader); diff --git a/libs/smart-card-reader-lib/build.gradle.kts b/libs/smart-card-reader-lib/build.gradle.kts index 352bd03..0aaa427 100644 --- a/libs/smart-card-reader-lib/build.gradle.kts +++ b/libs/smart-card-reader-lib/build.gradle.kts @@ -31,6 +31,7 @@ dependencies { // Provided at runtime by the consuming app (RIA DigiDoc utils-lib) compileOnly(project(":libs:card-utils-lib")) + testImplementation(project(":libs:card-utils-lib")) testImplementation(platform(libs.junit.bom)) testImplementation(libs.junit.jupiter) diff --git a/libs/smart-card-reader-lib/src/test/java/ee/ria/DigiDoc/smartcardreader/CardConnectionLostExceptionTest.java b/libs/smart-card-reader-lib/src/test/java/ee/ria/DigiDoc/smartcardreader/CardConnectionLostExceptionTest.java index c65c0d5..4ede4a1 100644 --- a/libs/smart-card-reader-lib/src/test/java/ee/ria/DigiDoc/smartcardreader/CardConnectionLostExceptionTest.java +++ b/libs/smart-card-reader-lib/src/test/java/ee/ria/DigiDoc/smartcardreader/CardConnectionLostExceptionTest.java @@ -4,7 +4,7 @@ import static com.google.common.truth.Truth.assertThat; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; From 79a7f0c3d7dc34673e5e9c73328d647c919a010e Mon Sep 17 00:00:00 2001 From: Sander Pensa Date: Fri, 27 Mar 2026 00:51:23 +0200 Subject: [PATCH 02/15] Testing basic access --- .../java/ee/ria/DigiDoc/idcard/CardType.java | 3 +- .../DigiDoc/idcard/LatviaIdemiaWithPace.java | 57 +++++++ .../idcard/LatviaPersonalDataParser.java | 147 ++++++++++++++++++ .../ee/ria/DigiDoc/idcard/TokenWithPace.java | 8 + 4 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java create mode 100644 libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/CardType.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/CardType.java index 977840b..27112be 100644 --- a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/CardType.java +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/CardType.java @@ -5,5 +5,6 @@ public enum CardType { THALES, - ID1; + ID1, + LATVIA; } diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java new file mode 100644 index 0000000..cd36207 --- /dev/null +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 - 2025 Riigi Infosüsteemi Amet + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +package ee.ria.DigiDoc.idcard; + +import android.util.SparseArray; + +import java.nio.charset.StandardCharsets; + +import ee.ria.DigiDoc.smartcardreader.SmartCardReaderException; +import ee.ria.DigiDoc.smartcardreader.nfc.NfcSmartCardReader; + +/** + * Latvian eID card NFC token. + * + * Extends IdemiaWithPace since the Latvian eID uses the same IDEMIA ID-One Cosmo v8 + * platform (IAS-ECC Oberthur) as the Estonian card. All PACE, SM, certificate, PIN, + * signature, authentication, and decryption operations are identical. + * + * Only personalData() parsing differs due to the Latvian personal code format. + */ +class LatviaIdemiaWithPace extends IdemiaWithPace { + + LatviaIdemiaWithPace(NfcSmartCardReader reader) { + super(reader); + } + + @Override + public PersonalData personalData() throws SmartCardReaderException { + selectMainAid(); + // VERIFY WITH CARD: EF structure assumed same as Estonian IDEMIA (8 records at 50:01-50:08) + reader.transmit(0x00, 0xA4, 0x01, 0x0C, new byte[] {0x50, 0x00}, null); + SparseArray data = new SparseArray<>(); + for (int i = 1; i <= 8; i++) { + reader.transmit(0x00, 0xA4, 0x02, 0x0C, new byte[] {0x50, (byte) i}, null); + byte[] record = reader.transmit(0x00, 0xB0, 0x00, 0x00, null, 0x00); + data.put(i, new String(record, StandardCharsets.UTF_8).trim()); + } + return LatviaPersonalDataParser.parse(data); + } +} diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java new file mode 100644 index 0000000..32a9c9f --- /dev/null +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java @@ -0,0 +1,147 @@ +/* + * Copyright 2017 - 2025 Riigi Infosüsteemi Amet + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +package ee.ria.DigiDoc.idcard; + +import android.util.SparseArray; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; + +import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil; + +class LatviaPersonalDataParser { + private static final String TAG = LatviaPersonalDataParser.class.getName(); + private static final DateTimeFormatter DATE_FORMAT = new DateTimeFormatterBuilder() + .appendPattern("dd MM yyyy") + .toFormatter(); + + // VERIFY WITH CARD: record positions assumed same as Estonian IDEMIA + private static final int SURNAME_POS = 1; + private static final int GIVEN_NAMES_POS = 2; + private static final int GENDER_POS = 3; + private static final int CITIZENSHIP_POS = 4; + private static final int DATE_AND_PLACE_OF_BIRTH_POS = 5; + private static final int PERSONAL_CODE_POS = 6; + private static final int DOCUMENT_NUMBER_POS = 7; + private static final int EXPIRY_DATE_POS = 8; + + private LatviaPersonalDataParser() {} + + static PersonalData parse(SparseArray data) { + String surname = data.get(SURNAME_POS); + String givenNames = data.get(GIVEN_NAMES_POS); + String citizenship = data.get(CITIZENSHIP_POS); + String dateAndPlaceOfBirthString = data.get(DATE_AND_PLACE_OF_BIRTH_POS); + String personalCode = data.get(PERSONAL_CODE_POS); + String documentNumber = data.get(DOCUMENT_NUMBER_POS); + String expiryDateString = data.get(EXPIRY_DATE_POS); + + LocalDate dateOfBirth = parseDateOfBirth(personalCode, dateAndPlaceOfBirthString); + LocalDate expiryDate = parseDate(expiryDateString); + + return PersonalData.create(surname, givenNames, citizenship, dateOfBirth, + personalCode, documentNumber, expiryDate, CardType.LATVIA); + } + + /** + * Parse date of birth from Latvian personal code or from the card's DOB record. + * + * Old format (pre-2017): DDMMYY-CZZZQ where C is century digit (0=19xx, 1=20xx, 2=21xx) + * New format (from 2017): starts with "32", DOB not encoded — fall back to card record. + */ + private static LocalDate parseDateOfBirth(String personalCode, String dateAndPlaceOfBirthString) { + if (personalCode != null && !personalCode.isEmpty()) { + try { + String codeDigits = personalCode.replace("-", ""); + if (!isNewFormatCode(codeDigits) && codeDigits.length() >= 7) { + return parseDateOfBirthFromOldCode(codeDigits); + } + } catch (Exception e) { + LoggingUtil.Companion.errorLog(TAG, + String.format("Could not parse DOB from personal code %s", personalCode), e); + } + } + + // New format code or parsing failed — fall back to date record from card + if (dateAndPlaceOfBirthString != null && !dateAndPlaceOfBirthString.isEmpty()) { + return parseDateFromRecord(dateAndPlaceOfBirthString); + } + + LoggingUtil.Companion.errorLog(TAG, "Could not determine date of birth", null); + return null; + } + + /** + * Old Latvian personal code: DDMMYYCZZZQ (digits only, dash stripped) + * Chars 0-1: day, 2-3: month, 4-5: year (2 digits), 6: century (0=19xx, 1=20xx, 2=21xx) + */ + private static LocalDate parseDateOfBirthFromOldCode(String codeDigits) { + int day = Integer.parseInt(codeDigits.substring(0, 2)); + int month = Integer.parseInt(codeDigits.substring(2, 4)); + int yearShort = Integer.parseInt(codeDigits.substring(4, 6)); + int centuryDigit = Character.getNumericValue(codeDigits.charAt(6)); + + int century = switch (centuryDigit) { + case 0 -> 1800; + case 1 -> 1900; + case 2 -> 2000; + default -> throw new IllegalArgumentException( + "Invalid century digit in Latvian personal code: " + centuryDigit); + }; + + return LocalDate.of(century + yearShort, month, day); + } + + /** + * New format codes start with "32" and do not encode date of birth. + */ + private static boolean isNewFormatCode(String codeDigits) { + return codeDigits.length() >= 2 && codeDigits.startsWith("32"); + } + + /** + * Parse date from the date and place of birth record (format: "dd MM yyyy" + place suffix). + */ + private static LocalDate parseDateFromRecord(String dateAndPlaceOfBirthString) { + try { + // Record may contain place of birth after the date — strip trailing non-date chars + // Date format is "dd MM yyyy" (10 chars), place follows after that + String dateString = dateAndPlaceOfBirthString.length() > 10 + ? dateAndPlaceOfBirthString.substring(0, dateAndPlaceOfBirthString.length() - 4) + : dateAndPlaceOfBirthString; + return LocalDate.parse(dateString, DATE_FORMAT); + } catch (Exception e) { + LoggingUtil.Companion.errorLog(TAG, + String.format("Could not parse date from record %s", dateAndPlaceOfBirthString), e); + return null; + } + } + + private static LocalDate parseDate(String dateString) { + try { + return LocalDate.parse(dateString, DATE_FORMAT); + } catch (Exception e) { + LoggingUtil.Companion.errorLog(TAG, + String.format("Could not parse date %s", dateString), e); + return null; + } + } +} diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TokenWithPace.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TokenWithPace.java index bdec267..d6f063e 100644 --- a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TokenWithPace.java +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TokenWithPace.java @@ -53,6 +53,14 @@ static TokenWithPace create(NfcSmartCardReader reader) throws SmartCardReaderExc if (Arrays.equals(Hex.decode("8031d85365494464b085051012233f"), atr)) { return new ThalesWithPace(reader); } + // Latvian eID cards (newer) + if (Arrays.equals(Hex.decode("0012428f536549440f9000"), atr)) { + return new LatviaIdemiaWithPace(reader); + } + // Latvian eID cards (older) + if (Arrays.equals(Hex.decode("0012428f54654944320f9000"), atr)) { + return new LatviaIdemiaWithPace(reader); + } throw new SmartCardReaderException("ATS not supported"); } From d8fac448620506646d2dee1c51cd449b4238859f Mon Sep 17 00:00:00 2001 From: Sander Pensa Date: Fri, 27 Mar 2026 00:54:21 +0200 Subject: [PATCH 03/15] Add version number and suffix to build artifacts --- .gitignore | 1 + environment.properties.example | 2 ++ libs/build.gradle.kts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 environment.properties.example create mode 100644 libs/build.gradle.kts diff --git a/.gitignore b/.gitignore index a2f6572..f8fe411 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ .DS_Store build/ local.properties +environment.properties libs/id-card-lib/build/ libs/smart-card-reader-lib/build/ diff --git a/environment.properties.example b/environment.properties.example new file mode 100644 index 0000000..6b58dbc --- /dev/null +++ b/environment.properties.example @@ -0,0 +1,2 @@ +#version=1.1.4 +#suffix=sl diff --git a/libs/build.gradle.kts b/libs/build.gradle.kts new file mode 100644 index 0000000..55cdffa --- /dev/null +++ b/libs/build.gradle.kts @@ -0,0 +1,30 @@ +import java.util.Properties + +val envFile: File? = rootProject.file("environment.properties") +val envProps = Properties() + +if (envFile?.exists() ?: false) { + envFile.inputStream().use { envProps.load(it) } +} +val libVersion: String? = envProps.getProperty("version") +val libSuffix: String? = envProps.getProperty("suffix") + +subprojects { + pluginManager.withPlugin("com.android.library") { + if (!libVersion.isNullOrBlank() || !libSuffix.isNullOrBlank()) { + val parts = mutableListOf(project.name) + + if (!libVersion.isNullOrBlank()) { + parts.add(libVersion) + } + + if (!libSuffix.isNullOrBlank()) { + parts.add(libSuffix) + } + + project.extensions.configure("base") { + archivesName.set(parts.joinToString("-")) + } + } + } +} From 3c0e69cef88e372ff55e9b9afdbf2a63171d410a Mon Sep 17 00:00:00 2001 From: Sander Pensa Date: Tue, 31 Mar 2026 19:02:45 +0300 Subject: [PATCH 04/15] Improvements to PACE parameters from EF.CardAccess and LV personal data --- demoapp/app/build.gradle.kts | 2 + gradle/libs.versions.toml | 2 + libs/id-card-lib/build.gradle.kts | 2 + .../java/ee/ria/DigiDoc/idcard/Idemia.java | 24 +- .../ee/ria/DigiDoc/idcard/IdemiaWithPace.java | 297 +++++++++++++++++- .../DigiDoc/idcard/LatviaIdemiaWithPace.java | 193 +++++++++++- .../idcard/LatviaPersonalDataParser.java | 100 +----- .../main/java/ee/ria/DigiDoc/idcard/TLV.java | 35 ++- 8 files changed, 533 insertions(+), 122 deletions(-) diff --git a/demoapp/app/build.gradle.kts b/demoapp/app/build.gradle.kts index 1520311..9c5a531 100644 --- a/demoapp/app/build.gradle.kts +++ b/demoapp/app/build.gradle.kts @@ -31,6 +31,7 @@ android { } } compileOptions { + isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } @@ -46,6 +47,7 @@ kotlin { } dependencies { + coreLibraryDesugaring(libs.desugar.jdk.libs) implementation(libs.androidx.activity) implementation(libs.androidx.appcompat) implementation(libs.androidx.constraintlayout) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index ffffede..fa92340 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,7 @@ appcompat = "1.8.0" bouncyCastle = "1.86" constraintlayout = "2.2.2" core = "1.19.0" +desugarJdkLibs = "2.1.5" fragmentKtx = "1.9.0" junitBom = "6.1.3" kotlin = "2.4.20" @@ -29,6 +30,7 @@ androidx-lifecycle-common = { module = "androidx.lifecycle:lifecycle-common", ve androidx-lifecycle-viewmodel = { module = "androidx.lifecycle:lifecycle-viewmodel", version.ref = "lifecycleViewmodel" } androidx-navigation-runtime = { module = "androidx.navigation:navigation-runtime", version.ref = "navigationFragmentKtx" } bcprov-jdk18on = { module = "org.bouncycastle:bcprov-jdk18on", version.ref = "bouncyCastle" } +desugar-jdk-libs = { module = "com.android.tools:desugar_jdk_libs", version.ref = "desugarJdkLibs" } fragment-ktx = { module = "androidx.fragment:fragment-ktx", version.ref = "fragmentKtx" } junit-bom = { module = "org.junit:junit-bom", version.ref = "junitBom" } junit-jupiter = { module = "org.junit.jupiter:junit-jupiter" } diff --git a/libs/id-card-lib/build.gradle.kts b/libs/id-card-lib/build.gradle.kts index 0673901..5b79de9 100644 --- a/libs/id-card-lib/build.gradle.kts +++ b/libs/id-card-lib/build.gradle.kts @@ -20,6 +20,7 @@ android { } compileOptions { + isCoreLibraryDesugaringEnabled = true sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 } @@ -30,6 +31,7 @@ android { } dependencies { + coreLibraryDesugaring(libs.desugar.jdk.libs) implementation(libs.androidx.annotation) implementation(libs.bcprov.jdk18on) diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/Idemia.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/Idemia.java index 08625c3..88dc87f 100644 --- a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/Idemia.java +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/Idemia.java @@ -126,30 +126,20 @@ public int pinChangedFlag(CodeType type) { @Override public byte[] calculateSignature(byte[] pin2, byte[] hash, boolean ecc) throws SmartCardReaderException { - selectQSCDAid(); - verifyCode(CodeType.PIN2, pin2); - reader.transmit(0x00, 0x22, 0x41, 0xB6, new byte[] {(byte) 0x80, 0x04, (byte) 0xFF, 0x15, 0x08, 0x00, (byte) 0x84, 0x01, (byte) 0x9F}, null); - return reader.transmit(0x00, 0x2A, 0x9E, 0x9A, padWithZeroes(hash), 0x00); + throw new SmartCardReaderException("Signature calculation not supported on this card"); } @Override public byte[] authenticate(byte[] pin1, byte[] token) throws SmartCardReaderException { - selectOberthurAid(); - verifyCode(CodeType.PIN1, pin1); - reader.transmit(0x00, 0x22, 0x41, 0xA4, new byte[] {(byte) 0x80, 0x04, (byte) 0xFF, 0x20, 0x08, 0x00, (byte) 0x84, 0x01, (byte) 0x81}, null); - return reader.transmit(0x00, 0x88, 0x00, 0x00, token, 0x00); + throw new SmartCardReaderException("Authentication not supported on this card"); } @Override public byte[] decrypt(byte[] pin1, byte[] data, boolean ecc) throws SmartCardReaderException { - selectOberthurAid(); - byte[] prefix = new byte[] {0x00}; - verifyCode(CodeType.PIN1, pin1); - reader.transmit(0x00, 0x22, 0x41, 0xB8, new byte[] {(byte) 0x80, 0x04, (byte) 0xFF, 0x30, 0x04, 0x00, (byte) 0x84, 0x01, (byte) 0x81}, null); - return reader.transmit(0x00, 0x2A, 0x80, 0x86, concatenate(prefix, data), 0x00); + throw new SmartCardReaderException("Decryption not supported on this card"); } - private void verifyCode(CodeType type, byte[] code) throws SmartCardReaderException { + protected void verifyCode(CodeType type, byte[] code) throws SmartCardReaderException { try { reader.transmit(0x00, 0x20, 0x00, Objects.requireNonNull(VERIFY_PIN_MAP.get(type)), code(code), null); } catch (ApduResponseException e) { @@ -173,11 +163,11 @@ protected void selectMainAid() throws SmartCardReaderException { reader.transmit(0x00, 0xA4, 0x04, 0x00, new byte[] {(byte) 0xA0, 0x00, 0x00, 0x00, 0x77, 0x01, 0x08, 0x00, 0x07, 0x00, 0x00, (byte) 0xFE, 0x00, 0x00, 0x01, 0x00}, null); } - private void selectQSCDAid() throws SmartCardReaderException { + protected void selectQSCDAid() throws SmartCardReaderException { reader.transmit(0x00, 0xA4, 0x04, 0x0C, new byte[] {0x51, 0x53, 0x43, 0x44, 0x20, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E}, null); } - private void selectOberthurAid() throws SmartCardReaderException { + protected void selectOberthurAid() throws SmartCardReaderException { reader.transmit(0x00, 0xA4, 0x04, 0x0C, new byte[] {(byte) 0xE8, 0x28, (byte) 0xBD, 0x08, 0x0F, (byte) 0xF2, 0x50, 0x4F, 0x54, 0x20, 0x41, 0x57, 0x50}, null); } @@ -196,7 +186,7 @@ private static byte[] code(byte[] code) { * @return zero padded hash with 48 byte length or same hash if it's longer than 48 bytes * @throws IdCardException when padding the hash fails */ - private static byte[] padWithZeroes(byte[] hash) throws IdCardException { + protected static byte[] padWithZeroes(byte[] hash) throws IdCardException { if (hash.length >= 48) { return hash; } diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/IdemiaWithPace.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/IdemiaWithPace.java index 8d610d1..bd15dd2 100644 --- a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/IdemiaWithPace.java +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/IdemiaWithPace.java @@ -20,6 +20,8 @@ import org.bouncycastle.util.BigIntegers; import org.bouncycastle.util.encoders.Hex; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; @@ -29,6 +31,7 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Arrays; +import java.util.List; import java.util.Locale; import javax.crypto.BadPaddingException; @@ -124,6 +127,12 @@ class IdemiaWithPace extends Idemia implements TokenWithPace, ApduEncryptor { */ private final NfcSmartCardReader nfcReader; + protected Byte authKeyRef = (byte) 0x81; + protected Byte signKeyRef = (byte) 0x9F; + + protected String paceEcSpec; + protected byte paceDomainParam; + /** * Initialize ID1 token with NfcSmartCardReader * @param reader @@ -173,6 +182,38 @@ protected void selectMainAid() throws SmartCardReaderException { reader.transmit(CLA_ISO, 0xA4, 0x04, 0x0C, data, null); } + /** + * Read PACE domain parameter and EC curve from EF.CardAccess. + * Accessible before PACE, in plaintext. + */ + private void readPaceParametersFromCard() throws SmartCardReaderException { + if (paceEcSpec != null) { + return; + } + + reader.transmit(0x00, 0xA4, 0x02, 0x0C, new byte[]{0x01, 0x1C}, null); + byte[] cardAccess = readBinaryFile(); + byte paramId = parsePaceParameterId(cardAccess); + + if (paramId == 0) { + throw new SmartCardReaderException("Failed to read PACE parameters from EF.CardAccess"); + } + + paceDomainParam = paramId; + paceEcSpec = domainParamToCurveName(paramId); + + if (paceEcSpec == null) { + throw new SmartCardReaderException( + "Unsupported PACE domain parameter: 0x" + Integer.toHexString(paramId & 0xFF) + ); + } + + LoggingUtil.Companion.debugLog(TAG, + String.format("PACE parameters read from EF.CardAccess: paramId=0x%02X, curve=%s", paramId, paceEcSpec), + null + ); + } + /** * Set MSE Authentication Template * @@ -181,7 +222,7 @@ protected void selectMainAid() throws SmartCardReaderException { private void setMSEAuthenticationTemplate() throws SmartCardReaderException { byte[] data = new byte[] { (byte)0x80, 0x0A, 0x04, 0x00, 0x7F, 0x00, 0x07, 0x02, - 0x02, 0x04, 0x02, 0x04, (byte)0x83, 0x01, 0x02, (byte)0x84, 0x01, 0x0C}; + 0x02, 0x04, 0x02, 0x04, (byte)0x83, 0x01, 0x02, (byte)0x84, 0x01, paceDomainParam}; reader.transmit(CLA_ISO, INS_MSE, 0xC1, 0xA4, data, 0x00); } @@ -357,10 +398,7 @@ private byte[][] establishPace(byte[] can) throws selectMainAid(); - // NB: We could read PACE parameters from EF.CardAccess file here. This step can be - // omitted, since the parameters on all currently issued ID cards are fixed. This may - // change in the future - + readPaceParametersFromCard(); setMSEAuthenticationTemplate(); byte[] response = getGAGetNonce(); @@ -373,7 +411,7 @@ private byte[][] establishPace(byte[] can) throws can); // generate an EC keypair and exchange public keys with the chip - ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256r1"); + ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(paceEcSpec); BigInteger privateKey = generateRandomPrivateKey(spec); @@ -720,4 +758,251 @@ public static void incrementSSC(byte[] ssc) { } } + @Override + public byte[] authenticate(byte[] pin1, byte[] token) throws SmartCardReaderException { + selectOberthurAid(); + verifyCode(CodeType.PIN1, pin1); + byte keyRef = getAuthKeyRef(); + + LoggingUtil.Companion.debugLog(TAG, + String.format("MSE SET auth: algo=FF200800, keyRef=0x%02x", keyRef & 0xFF), + null + ); + + reader.transmit(0x00, 0x22, 0x41, 0xA4, + new byte[] {(byte) 0x80, 0x04, (byte) 0xFF, 0x20, 0x08, 0x00, (byte) 0x84, 0x01, keyRef}, null); + return reader.transmit(0x00, 0x88, 0x00, 0x00, token, 0x00); + } + + @Override + public byte[] calculateSignature(byte[] pin2, byte[] hash, boolean ecc) throws SmartCardReaderException { + selectQSCDAid(); + verifyCode(CodeType.PIN2, pin2); + byte keyRef = getSignKeyRef(); + + LoggingUtil.Companion.debugLog(TAG, + String.format("MSE SET sign: algo=FF150800, keyRef=0x%02x", keyRef & 0xFF), + null + ); + + reader.transmit(0x00, 0x22, 0x41, 0xB6, + new byte[] {(byte) 0x80, 0x04, (byte) 0xFF, 0x15, 0x08, 0x00, (byte) 0x84, 0x01, keyRef}, null); + return reader.transmit(0x00, 0x2A, 0x9E, 0x9A, padWithZeroes(hash), 0x00); + } + + @Override + public byte[] decrypt(byte[] pin1, byte[] data, boolean ecc) throws SmartCardReaderException { + selectOberthurAid(); + verifyCode(CodeType.PIN1, pin1); + byte keyRef = getAuthKeyRef(); + + LoggingUtil.Companion.debugLog(TAG, + String.format("MSE SET decrypt: algo=FF300400, keyRef=0x%02x", keyRef & 0xFF), + null + ); + + reader.transmit(0x00, 0x22, 0x41, 0xB8, + new byte[] {(byte) 0x80, 0x04, (byte) 0xFF, 0x30, 0x04, 0x00, (byte) 0x84, 0x01, keyRef}, null); + return reader.transmit(0x00, 0x2A, 0x80, 0x86, concat(new byte[] {0x00}, data), 0x00); + } + + /** + * Get the authentication key reference. If null, attempts dynamic discovery from PrKDF. + * Cached after first read. + */ + protected byte getAuthKeyRef() throws SmartCardReaderException { + if (authKeyRef == null) { + byte ref = readKeyRefFromCurrentContext(); + if (ref != 0) { + authKeyRef = ref; + LoggingUtil.Companion.debugLog(TAG, + String.format("PrKDF auth key ref discovered: 0x%02x", authKeyRef & 0xFF), + null + ); + } else { + throw new SmartCardReaderException("Auth key reference not found in PrKDF"); + } + } + + return authKeyRef; + } + + /** + * Get the signing key reference. If null, attempts dynamic discovery from PrKDF. + * Cached after first read. + */ + protected byte getSignKeyRef() throws SmartCardReaderException { + if (signKeyRef == null) { + byte ref = readKeyRefFromCurrentContext(); + if (ref != 0) { + signKeyRef = ref; + LoggingUtil.Companion.debugLog(TAG, + String.format("PrKDF sign key ref discovered: 0x%02x", signKeyRef & 0xFF), + null + ); + } else { + throw new SmartCardReaderException("Sign key reference not found in PrKDF"); + } + } + + return signKeyRef; + } + + /** + * Read the first private key reference from the PrKDF in the currently selected AID context. + * + * Flow (matching Web eID libelectronic-id): + * 1. Select and read EF_OD (0x5031) — Object Directory File + * 2. Find tag 0xA0 (private key dir ref) → extract PrKDF file ID + * 3. Read PrKDF file + * 4. Return first keyReference INTEGER found + * + * @return key reference byte, or 0 if not found + */ + private byte readKeyRefFromCurrentContext() throws SmartCardReaderException { + // Read EF_OD (Object Directory File at 0x5031) + reader.transmit(0x00, 0xA4, 0x02, 0x0C, new byte[] {0x50, 0x31}, null); + byte[] efOd = readBinaryFile(); + + // Find tag 0xA0 (private key directory reference) + List odEntries = TLV.parseAll(efOd); + TLV privKeyDir = TLV.findByTag(odEntries, 0xA0); + if (privKeyDir == null || privKeyDir.children == null) { + return 0; + } + + // Navigate: A0 → 30 → 04 to get PrKDF file ID + TLV seq = privKeyDir.findByTag(0x30); + if (seq == null) { + return 0; + } + TLV fileIdTlv = seq.findByTag(0x04); + if (fileIdTlv == null || fileIdTlv.getValue().length != 2) { + return 0; + } + + // Select and read PrKDF + byte[] fileId = fileIdTlv.getValue(); + reader.transmit(0x00, 0xA4, 0x02, 0x0C, fileId, null); + byte[] prKdfData = readBinaryFile(); + + // Parse PrKDF entries and return first key reference found + List keyEntries = TLV.parseAll(prKdfData); + for (TLV entry : keyEntries) { + if (entry.children == null || entry.children.size() < 2) { + continue; + } + byte keyRef = extractKeyReference(entry); + if (keyRef != 0) { + return keyRef; + } + } + return 0; + } + + /** + * Extract key reference from a PKCS#15 PrivateKeyType entry. + * + * Structure: SEQUENCE { CommonObjectAttributes, CommonKeyAttributes, TypeAttributes } + * CommonKeyAttributes contains: iD, usage, ..., keyReference (INTEGER) + * + * In DER, key refs >= 0x80 are encoded as 2-byte INTEGERs (e.g., 02 02 00 82). + */ + protected static byte extractKeyReference(TLV entry) { + if (entry.children == null || entry.children.size() < 2) { + return 0; + } + + // Second child SEQUENCE = CommonKeyAttributes + TLV commonKeyAttrs = entry.children.get(1); + if (commonKeyAttrs.children == null) { + return 0; + } + + // Find INTEGER (tag 0x02) — this is the keyReference + for (TLV child : commonKeyAttrs.children) { + if (child.getTag() == 0x02) { + byte[] val = child.getValue(); + if (val.length == 2) { + // 2-byte DER INTEGER (e.g., 00 82 → key ref 0x82) + return val[1]; + } else if (val.length == 1) { + return val[0]; + } + } + } + return 0; + } + + /** + * Read a binary file from the currently selected EF. + */ + protected byte[] readBinaryFile() throws SmartCardReaderException { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + while (true) { + try { + stream.write(reader.transmit(0x00, 0xB0, stream.size() >> 8, stream.size(), null, 0x00)); + } catch (ApduResponseException e) { + if (e.sw1 == 0x6B && e.sw2 == 0x00) { + break; // offset out of range = end of file + } else if (e.sw1 == 0x6A && e.sw2 == (byte) 0x82) { + break; // file not found + } else { + throw e; + } + } catch (IOException e) { + throw new SmartCardReaderException(e); + } + } + return stream.toByteArray(); + } + + /** + * Parse PACEInfo parameterId from EF.CardAccess. + * + * EF.CardAccess is ASN.1: SET OF SecurityInfo + * PACEInfo ::= SEQUENCE { OID, INTEGER version, INTEGER parameterId } + * + * @return parameterId byte, or 0 if not found + */ + private static byte parsePaceParameterId(byte[] cardAccess) { + List entries = TLV.parseAll(cardAccess); + for (TLV entry : entries) { + if (entry.children == null) { + continue; + } + // SET contains SEQUENCEs (SecurityInfo entries) + for (TLV child : entry.children) { + if (child.children == null || child.children.size() < 3) { + continue; + } + // Third child is parameterId INTEGER + TLV paramTlv = child.children.get(2); + if (paramTlv.getTag() == 0x02 && paramTlv.getValue().length == 1) { + return paramTlv.getValue()[0]; + } + } + } + // Also check top-level SEQUENCEs (if no outer SET wrapper) + for (TLV entry : entries) { + if (entry.children != null && entry.children.size() >= 3) { + TLV paramTlv = entry.children.get(2); + if (paramTlv.getTag() == 0x02 && paramTlv.getValue().length == 1) { + return paramTlv.getValue()[0]; + } + } + } + return 0; + } + + private static String domainParamToCurveName(byte paramId) { + return switch (paramId & 0xFF) { + case 0x0C -> "secp256r1"; + case 0x0D -> "brainpoolP256r1"; + case 0x0F -> "secp384r1"; + case 0x10 -> "brainpoolP384r1"; + default -> null; + }; + } + } diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java index cd36207..c91c4a8 100644 --- a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java @@ -19,39 +19,204 @@ package ee.ria.DigiDoc.idcard; -import android.util.SparseArray; +import static com.google.common.primitives.Bytes.concat; +import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.time.LocalDate; +import java.time.ZoneId; + +import javax.security.auth.x500.X500Principal; import ee.ria.DigiDoc.smartcardreader.SmartCardReaderException; import ee.ria.DigiDoc.smartcardreader.nfc.NfcSmartCardReader; +import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil; /** * Latvian eID card NFC token. - * + *

* Extends IdemiaWithPace since the Latvian eID uses the same IDEMIA ID-One Cosmo v8 - * platform (IAS-ECC Oberthur) as the Estonian card. All PACE, SM, certificate, PIN, - * signature, authentication, and decryption operations are identical. - * - * Only personalData() parsing differs due to the Latvian personal code format. + * platform (IAS-ECC Oberthur) as the Estonian card. PACE, SM, certificate, and PIN + * operations are inherited. + *

+ * Overrides personalData() for Latvian personal code parsing, provides AID selection + * fallback, and reads key references from the card's PrKDF for authentication/signing + * since the Latvian card uses different key slot assignments. */ class LatviaIdemiaWithPace extends IdemiaWithPace { + private static final String TAG = LatviaIdemiaWithPace.class.getName(); + + private static final byte ECC_AUTH_ALGO = 0x04; + private static final byte ECC_SIGN_ALGO = 0x54; LatviaIdemiaWithPace(NfcSmartCardReader reader) { super(reader); + authKeyRef = (byte) 0x82; + signKeyRef = (byte) 0x9E; } + /** + * Read personal data from the auth certificate subject and EF 0x5001 (personal code). + * + * Latvian eID cards store only the personal code in EF files (DF 0x5000 / EF 0x5001). + * Name, citizenship, and document number are extracted from the auth certificate subject: + * - OID 2.5.4.4 (surname) + * - OID 2.5.4.42 (givenName) + * - OID 2.5.4.5 (serialNumber) — format "PNOLV-{personalCode}" + * - C (2.5.4.6) (citizenship) + * Expiry date comes from the certificate's notAfter. + */ @Override public PersonalData personalData() throws SmartCardReaderException { + // Read personal code from EF 0x5001 selectMainAid(); - // VERIFY WITH CARD: EF structure assumed same as Estonian IDEMIA (8 records at 50:01-50:08) - reader.transmit(0x00, 0xA4, 0x01, 0x0C, new byte[] {0x50, 0x00}, null); - SparseArray data = new SparseArray<>(); - for (int i = 1; i <= 8; i++) { - reader.transmit(0x00, 0xA4, 0x02, 0x0C, new byte[] {0x50, (byte) i}, null); - byte[] record = reader.transmit(0x00, 0xB0, 0x00, 0x00, null, 0x00); - data.put(i, new String(record, StandardCharsets.UTF_8).trim()); + reader.transmit(0x00, 0xA4, 0x01, 0x0C, new byte[]{0x50, 0x00}, null); + reader.transmit(0x00, 0xA4, 0x02, 0x0C, new byte[]{0x50, 0x01}, null); + byte[] record = reader.transmit(0x00, 0xB0, 0x00, 0x00, null, 0x00); + String personalCode = new String(record, StandardCharsets.UTF_8).trim(); + LoggingUtil.Companion.debugLog(TAG, + "EF 0x5001 personal code: " + personalCode, null); + + // Parse auth certificate for remaining fields + try { + byte[] certBytes = certificate(CertificateType.AUTHENTICATION); + CertificateFactory cf = CertificateFactory.getInstance("X.509"); + X509Certificate x509 = (X509Certificate) + cf.generateCertificate(new ByteArrayInputStream(certBytes)); + + String subjectDn = x509.getSubjectX500Principal().getName(X500Principal.RFC2253); + LoggingUtil.Companion.debugLog(TAG, + "Auth cert subject DN: " + subjectDn, null); + + String surname = extractOid(subjectDn, "2.5.4.4"); + String givenName = extractOid(subjectDn, "2.5.4.42"); + String citizenship = extractOid(subjectDn, "C"); + if (citizenship.isEmpty()) { + citizenship = extractOid(subjectDn, "2.5.4.6"); + } + String serialNumber = extractOid(subjectDn, "2.5.4.5"); + LoggingUtil.Companion.debugLog(TAG, + "Extracted: surname=" + surname + ", givenName=" + givenName + + ", citizenship=" + citizenship + ", serialNumber=" + serialNumber, null); + + // Document number: not available separately, use cert serialNumber field + String documentNumber = serialNumber.isEmpty() ? "" : serialNumber; + + LocalDate expiryDate = x509.getNotAfter().toInstant() + .atZone(ZoneId.systemDefault()).toLocalDate(); + + LocalDate dateOfBirth = LatviaPersonalDataParser.parseDateOfBirth(personalCode); + LoggingUtil.Companion.debugLog(TAG, + "Parsed: documentNumber=" + documentNumber + ", expiryDate=" + expiryDate + + ", dateOfBirth=" + dateOfBirth, null); + + return PersonalData.create(surname, givenName, citizenship, dateOfBirth, + personalCode, documentNumber, expiryDate, CardType.LATVIA); + } catch (SmartCardReaderException e) { + throw e; + } catch (Exception e) { + throw new SmartCardReaderException("Failed to parse auth certificate", e); } - return LatviaPersonalDataParser.parse(data); } + + /** + * Extract a value from an RFC2253 DN string by OID. + * Handles both plain text values and hex-encoded (#-prefixed) UTF8String values. + */ + private static String extractOid(String dn, String oid) { + String prefix = oid + "="; + int start = dn.indexOf(prefix); + while (start >= 0) { + if (start == 0 || dn.charAt(start - 1) == ',' || dn.charAt(start - 1) == ' ') { + break; + } + start = dn.indexOf(prefix, start + 1); + } + if (start < 0) { + return ""; + } + start += prefix.length(); + if (start < dn.length() && dn.charAt(start) == '#') { + // Hex-encoded DER value: skip tag+length bytes, decode the rest as UTF-8 + int end = dn.indexOf(',', start); + String hex = (end < 0) ? dn.substring(start + 1) : dn.substring(start + 1, end); + // DER: first byte = tag (0c = UTF8String), second byte = length, rest = value + if (hex.length() >= 4) { + try { + byte[] der = hexToBytes(hex); + int valueOffset = 2; // tag + 1-byte length + if (der.length > 2 && (der[1] & 0xFF) == 0x81) { + valueOffset = 3; + } + return new String(der, valueOffset, der.length - valueOffset, StandardCharsets.UTF_8); + } catch (Exception e) { + return hex; + } + } + return ""; + } + int end = dn.indexOf(',', start); + return (end < 0) ? dn.substring(start) : dn.substring(start, end); + } + + private static byte[] hexToBytes(String hex) { + int len = hex.length(); + byte[] out = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + out[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) + + Character.digit(hex.charAt(i + 1), 16)); + } + return out; + } + + @Override + public byte[] authenticate(byte[] pin1, byte[] token) throws SmartCardReaderException { + selectOberthurAid(); + verifyCode(CodeType.PIN1, pin1); + byte keyRef = getAuthKeyRef(); + + LoggingUtil.Companion.debugLog(TAG, + String.format("MSE SET auth: algo=0x%02x, keyRef=0x%02x", ECC_AUTH_ALGO & 0xFF, keyRef & 0xFF), + null + ); + + reader.transmit(0x00, 0x22, 0x41, 0xA4, + new byte[] {(byte) 0x80, 0x01, ECC_AUTH_ALGO, (byte) 0x84, 0x01, keyRef}, null); + return reader.transmit(0x00, 0x88, 0x00, 0x00, token, 0x00); + } + + @Override + public byte[] calculateSignature(byte[] pin2, byte[] hash, boolean ecc) throws SmartCardReaderException { + selectQSCDAid(); + verifyCode(CodeType.PIN2, pin2); + byte keyRef = getSignKeyRef(); + + LoggingUtil.Companion.debugLog(TAG, + String.format("MSE SET sign: algo=0x%02x, keyRef=0x%02x", ECC_SIGN_ALGO & 0xFF, keyRef & 0xFF), + null + ); + + reader.transmit(0x00, 0x22, 0x41, 0xB6, + new byte[] {(byte) 0x80, 0x01, ECC_SIGN_ALGO, (byte) 0x84, 0x01, keyRef}, null); + return reader.transmit(0x00, 0x2A, 0x9E, 0x9A, padWithZeroes(hash), 0x00); + } + + @Override + public byte[] decrypt(byte[] pin1, byte[] data, boolean ecc) throws SmartCardReaderException { + selectOberthurAid(); + verifyCode(CodeType.PIN1, pin1); + byte keyRef = getAuthKeyRef(); + + LoggingUtil.Companion.debugLog(TAG, + String.format("MSE SET decrypt: algo=0x%02x, keyRef=0x%02x", ECC_AUTH_ALGO & 0xFF, keyRef & 0xFF), + null + ); + + reader.transmit(0x00, 0x22, 0x41, 0xB8, + new byte[] {(byte) 0x80, 0x01, ECC_AUTH_ALGO, (byte) 0x84, 0x01, keyRef}, null); + return reader.transmit(0x00, 0x2A, 0x80, 0x86, concat(new byte[] {0x00}, data), 0x00); + } + } diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java index 32a9c9f..0e62a94 100644 --- a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java @@ -19,79 +19,41 @@ package ee.ria.DigiDoc.idcard; -import android.util.SparseArray; - import java.time.LocalDate; -import java.time.format.DateTimeFormatter; -import java.time.format.DateTimeFormatterBuilder; import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil; class LatviaPersonalDataParser { private static final String TAG = LatviaPersonalDataParser.class.getName(); - private static final DateTimeFormatter DATE_FORMAT = new DateTimeFormatterBuilder() - .appendPattern("dd MM yyyy") - .toFormatter(); - - // VERIFY WITH CARD: record positions assumed same as Estonian IDEMIA - private static final int SURNAME_POS = 1; - private static final int GIVEN_NAMES_POS = 2; - private static final int GENDER_POS = 3; - private static final int CITIZENSHIP_POS = 4; - private static final int DATE_AND_PLACE_OF_BIRTH_POS = 5; - private static final int PERSONAL_CODE_POS = 6; - private static final int DOCUMENT_NUMBER_POS = 7; - private static final int EXPIRY_DATE_POS = 8; private LatviaPersonalDataParser() {} - static PersonalData parse(SparseArray data) { - String surname = data.get(SURNAME_POS); - String givenNames = data.get(GIVEN_NAMES_POS); - String citizenship = data.get(CITIZENSHIP_POS); - String dateAndPlaceOfBirthString = data.get(DATE_AND_PLACE_OF_BIRTH_POS); - String personalCode = data.get(PERSONAL_CODE_POS); - String documentNumber = data.get(DOCUMENT_NUMBER_POS); - String expiryDateString = data.get(EXPIRY_DATE_POS); - - LocalDate dateOfBirth = parseDateOfBirth(personalCode, dateAndPlaceOfBirthString); - LocalDate expiryDate = parseDate(expiryDateString); - - return PersonalData.create(surname, givenNames, citizenship, dateOfBirth, - personalCode, documentNumber, expiryDate, CardType.LATVIA); - } - /** - * Parse date of birth from Latvian personal code or from the card's DOB record. + * Parse date of birth from Latvian personal code. * - * Old format (pre-2017): DDMMYY-CZZZQ where C is century digit (0=19xx, 1=20xx, 2=21xx) - * New format (from 2017): starts with "32", DOB not encoded — fall back to card record. + * Old format (pre-2017): DDMMYY-CZZZQ where C is century digit (0=18xx, 1=19xx, 2=20xx) + * New format (from 2017): starts with "32", DOB not encoded — returns null. */ - private static LocalDate parseDateOfBirth(String personalCode, String dateAndPlaceOfBirthString) { - if (personalCode != null && !personalCode.isEmpty()) { - try { - String codeDigits = personalCode.replace("-", ""); - if (!isNewFormatCode(codeDigits) && codeDigits.length() >= 7) { - return parseDateOfBirthFromOldCode(codeDigits); - } - } catch (Exception e) { - LoggingUtil.Companion.errorLog(TAG, - String.format("Could not parse DOB from personal code %s", personalCode), e); - } + static LocalDate parseDateOfBirth(String personalCode) { + if (personalCode == null || personalCode.isEmpty()) { + return null; } - - // New format code or parsing failed — fall back to date record from card - if (dateAndPlaceOfBirthString != null && !dateAndPlaceOfBirthString.isEmpty()) { - return parseDateFromRecord(dateAndPlaceOfBirthString); + try { + String codeDigits = personalCode.replace("-", ""); + if (isNewFormatCode(codeDigits) || codeDigits.length() < 7) { + return null; + } + return parseDateOfBirthFromOldCode(codeDigits); + } catch (Exception e) { + LoggingUtil.Companion.errorLog(TAG, + "Could not parse DOB from personal code " + personalCode, e); + return null; } - - LoggingUtil.Companion.errorLog(TAG, "Could not determine date of birth", null); - return null; } /** * Old Latvian personal code: DDMMYYCZZZQ (digits only, dash stripped) - * Chars 0-1: day, 2-3: month, 4-5: year (2 digits), 6: century (0=19xx, 1=20xx, 2=21xx) + * Chars 0-1: day, 2-3: month, 4-5: year (2 digits), 6: century (0=18xx, 1=19xx, 2=20xx) */ private static LocalDate parseDateOfBirthFromOldCode(String codeDigits) { int day = Integer.parseInt(codeDigits.substring(0, 2)); @@ -116,32 +78,4 @@ private static LocalDate parseDateOfBirthFromOldCode(String codeDigits) { private static boolean isNewFormatCode(String codeDigits) { return codeDigits.length() >= 2 && codeDigits.startsWith("32"); } - - /** - * Parse date from the date and place of birth record (format: "dd MM yyyy" + place suffix). - */ - private static LocalDate parseDateFromRecord(String dateAndPlaceOfBirthString) { - try { - // Record may contain place of birth after the date — strip trailing non-date chars - // Date format is "dd MM yyyy" (10 chars), place follows after that - String dateString = dateAndPlaceOfBirthString.length() > 10 - ? dateAndPlaceOfBirthString.substring(0, dateAndPlaceOfBirthString.length() - 4) - : dateAndPlaceOfBirthString; - return LocalDate.parse(dateString, DATE_FORMAT); - } catch (Exception e) { - LoggingUtil.Companion.errorLog(TAG, - String.format("Could not parse date from record %s", dateAndPlaceOfBirthString), e); - return null; - } - } - - private static LocalDate parseDate(String dateString) { - try { - return LocalDate.parse(dateString, DATE_FORMAT); - } catch (Exception e) { - LoggingUtil.Companion.errorLog(TAG, - String.format("Could not parse date %s", dateString), e); - return null; - } - } } diff --git a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TLV.java b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TLV.java index 843bf25..fed8131 100644 --- a/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TLV.java +++ b/libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TLV.java @@ -72,8 +72,20 @@ private static List parseTLVRecursive(byte[] data, int start, int end) { if (index >= end) break; - // Parse 1-byte length - int length = data[index++] & 0xFF; + // Parse DER length (1, 2, or 3 bytes) + int lengthByte = data[index++] & 0xFF; + int length; + if (lengthByte <= 0x7F) { + length = lengthByte; + } else if (lengthByte == 0x81) { + if (index >= end) break; + length = data[index++] & 0xFF; + } else if (lengthByte == 0x82) { + if (index + 1 >= end) break; + length = ((data[index++] & 0xFF) << 8) | (data[index++] & 0xFF); + } else { + break; + } if (index + length > end) break; byte[] value = Arrays.copyOfRange(data, index, index + length); @@ -90,6 +102,25 @@ private static List parseTLVRecursive(byte[] data, int start, int end) { return result; } + public TLV findByTag(int searchTag) { + if (children == null) return null; + for (TLV child : children) { + if (child.tag == searchTag) return child; + } + return null; + } + + public static TLV findByTag(List tlvs, int searchTag) { + for (TLV tlv : tlvs) { + if (tlv.tag == searchTag) return tlv; + } + return null; + } + + public static List parseAll(byte[] data) { + return parseTLVRecursive(data, 0, data.length); + } + /** @noinspection unused*/ public static List sequenceOfRecords(byte[] data) { List result = new ArrayList<>(); From 853e764b04b9ce14a11e944e072db198cd6fa851 Mon Sep 17 00:00:00 2001 From: Sander Pensa Date: Fri, 8 May 2026 13:56:47 +0300 Subject: [PATCH 05/15] Code improvements, demo app improvements and added Idemia LV spec --- IDEMIA_LV.md | 1119 +++++++++++++++++ .../nfc/example/card/CardInfoFragment.kt | 17 +- .../nfc/example/card/CardReaderFragment.kt | 20 +- .../nfc/example/sign/ContainerFragment.kt | 5 + .../smartcardreader/nfc/example/util/Utils.kt | 9 + .../nfc/example/viewmodel/DataViewModel.kt | 20 +- .../main/res/layout/fragment_container.xml | 10 + demoapp/app/src/main/res/values/strings.xml | 4 + .../java/ee/ria/DigiDoc/idcard/CardType.java | 2 +- .../java/ee/ria/DigiDoc/idcard/Idemia.java | 6 + .../idcard/IdemiaPersonalDataParser.java | 4 +- .../ee/ria/DigiDoc/idcard/IdemiaWithPace.java | 125 +- .../DigiDoc/idcard/LatviaIdemiaWithPace.java | 207 ++- .../ee/ria/DigiDoc/idcard/PersonalData.java | 37 +- .../idcard/ThalesPersonalDataParser.java | 4 +- 15 files changed, 1417 insertions(+), 172 deletions(-) create mode 100644 IDEMIA_LV.md diff --git a/IDEMIA_LV.md b/IDEMIA_LV.md new file mode 100644 index 0000000..aa3279c --- /dev/null +++ b/IDEMIA_LV.md @@ -0,0 +1,1119 @@ +# Latvian eID — IDEMIA card protocol specification + +A self-contained, platform-neutral specification of the Latvian eID card +(IDEMIA ID-One Cosmo v8 / IAS-ECC, Oberthur AWP applet) sufficient to +re-implement support on any host that can speak ISO/IEC 7816-4 APDUs over +ISO/IEC 14443-4 (NfcA). + +> Conventions +> - Bytes are hex, MSB-first, separated by spaces. `` denotes a placeholder. +> - APDUs are written as `CLA INS P1 P2 [Lc data] [Le]`. +> `Lc` and `Le` are written as a single byte where shown; APDUs without `Lc` +> omit the data block, APDUs without `Le` omit the trailing byte. +> - "MAIN AID", "Oberthur AID", "QSCD AID" are the three applet contexts of +> §3. "Selecting" an AID means sending the SELECT-AID APDU below. + +--- + +## 1. Overview + +| | | +|---|---| +| Card platform | IDEMIA ID-One Cosmo v8 (IAS-ECC) | +| Applets | MAIN (IAS-ECC; certs, EF.CardAccess, EF 0x5001) + Oberthur AWP (auth/decrypt) + QSCD (signing). See §3 for AID values. | +| Transport | ISO 14443-4 / NfcA, ISO 7816-4 APDUs | +| PACE curve | Negotiated from EF.CardAccess. Observed: `brainpoolP256r1` on LV, `secp256r1` on EE — both 256-bit. | +| User-key curve | `secp384r1` per the auth/sign certificates (independent of the PACE curve). All ECDSA signatures returned by the card are 96 bytes (`r ‖ s`, 48 + 48). | +| Secure messaging | PACE-derived AES-256 CBC + AES-256-CMAC (mechanism `id-PACE-ECDH-GM-AES-CBC-CMAC-256`) | +| User secrets | PIN1 (auth), PIN2 (sign), PUK (unblock); CAN for PACE | + +Every sensitive operation (cert read with SM, PIN verify, key operations, +EF.5001 read) requires a PACE channel. The CAN is printed on the card. + +--- + +## 2. Card identification + +Match against the contactless historical bytes / ATS. The library currently +recognises two LV variants: + +| Variant | Bytes | +|---|---| +| Newer LV | `00 12 42 8F 53 65 49 44 0F 90 00` | +| Older LV | `00 12 42 8F 54 65 49 44 32 0F 90 00` | + +The leading `00 12 42 8F` is the manufacturer signature unique to LV. +Match the full byte string. (Estonian IDEMIA cards use `00 12 23 3F` +— provided here for context only; do not match LV cards against the EE +prefix.) + +The byte strings above are **the historical bytes from the card's ATS** +(ISO 14443-4). The trailing `90 00` is part of the historical bytes the +card itself emits — chosen by the card OS, not a synthesized SW. Match +against the historical-bytes substring your stack exposes; if your +transport hands you a full or synthesized ATR instead, extract the +historical-bytes block first. + +For reference, what some common stacks expose by default: + +| Stack | Call | What it returns | +|---|---|---| +| Android (NfcA `IsoDep`) | `getHistoricalBytes()` | Historical bytes — exactly the strings above. | +| Android (NfcA `IsoDep`, alt) | `getHiLayerResponse()` | Full ATS framing — strip leading bytes to recover. | +| iOS (CoreNFC) | `NFCISO7816Tag.historicalBytes` | Historical bytes — same form as Android. | +| PC/SC contactless | `SCardStatus()` ATR | Reader-synthesized contact-style ATR (e.g. `3B 8x 80 01 `); parse out the historical-bytes block. | + +--- + +## 3. Applets and AIDs + +| Applet | AID | Purpose | +|---|---|---| +| MAIN (IAS-ECC) | `A0 00 00 00 77 01 08 00 07 00 00 FE 00 00 01 00` | EF.CardAccess, EF 0x5001, certificate files | +| Oberthur AWP | `E8 28 BD 08 0F F2 50 4F 54 20 41 57 50` | PIN1 verify, auth, decrypt | +| QSCD | `51 53 43 44 20 41 70 70 6C 69 63 61 74 69 6F 6E` (`"QSCD Application"`) | PIN2 verify, signing | + +**SELECT AID** (always P2 = `0x0C` over NFC — `0x00` does not work): + +``` +00 A4 04 0C +``` + +Examples: + +``` +SELECT MAIN: 00 A4 04 0C 10 A0 00 00 00 77 01 08 00 07 00 00 FE 00 00 01 00 +SELECT Oberthur: 00 A4 04 0C 0D E8 28 BD 08 0F F2 50 4F 54 20 41 57 50 +SELECT QSCD: 00 A4 04 0C 10 51 53 43 44 20 41 70 70 6C 69 63 61 74 69 6F 6E +``` + +Expected SW: `90 00`. Selecting an AID resets PIN security state for that +context — verify PINs **after** selecting the relevant AID. + +--- + +## 4. PACE handshake (mandatory) + +The card uses the `id-PACE-ECDH-GM-AES-CBC-CMAC-256` mechanism. OID bytes: + +``` +04 00 7F 00 07 02 02 04 02 04 +``` + +The trailing `04` arc selects the **AES-256** variant of this OID family. +The `02` and `03` arcs would be the AES-128 and AES-192 variants +respectively — picking either of those would yield a non-working client +against this card. + +AES-256 keys (32 bytes), 16-byte AES block size. The CAN is the PACE +password. + +The whole handshake runs in plaintext. After §4.6 succeeds, secure +messaging (§5) is active and the channel is bound to the new keys. + +### 4.1 Read PACE parameters from EF.CardAccess + +``` +SELECT EF.CardAccess: 00 A4 02 0C 02 01 1C +READ BINARY (chunked): 00 B0 00 (loop, see §6) +``` + +Parse the returned ASN.1 (`SET OF SecurityInfo`, possibly without an outer +SET wrapper). Find the `PACEInfo` entry — the `SEQUENCE` whose first child +is an OID equal to **`04 00 7F 00 07 02 02 04 02 04`** (the +`id-PACE-ECDH-GM-AES-CBC-CMAC-256` mechanism this spec uses). Take its +third child (`INTEGER parameterId`) as a single byte: + +> **Do not just pick the first `SEQUENCE { OID, INTEGER, INTEGER }`** — +> EF.CardAccess can carry other `SecurityInfo` entries with the same shape +> (e.g. `ChipAuthenticationInfo`), and selecting the wrong one yields a +> domain-parameter id that doesn't match the curve negotiated in §4.2. + +> **BER length forms.** EF.CardAccess uses standard BER-TLV. Lengths can +> be short-form (one byte, `≤ 0x7F` = the length itself) or long-form +> (`0x81 ` for one length byte, `0x82 ` for two). The +> reference TLV walker handles short-form, `0x81`, and `0x82`; longer +> forms aren't produced for files of this size. A minimal porter walker +> needs at least these three branches. + +| `parameterId` | Curve | +|---|---| +| `0x0C` | `secp256r1` | +| `0x0D` | `brainpoolP256r1` | +| `0x0F` | `secp384r1` | +| `0x10` | `brainpoolP384r1` | + +**Currently observed on LV cards:** `0x0D` (`brainpoolP256r1`) only. The +other three rows are reference values from the IDEMIA / IAS-ECC family +and are included so a porter encountering a different card variant can +look up the curve without re-deriving the table. + +If no usable `parameterId` is present, abort. The remainder of this spec +assumes 256-bit curve byte sizes (65-byte uncompressed SEC1 points, +32-byte shared-secret X) — applicable to both `secp256r1` (EE) and +`brainpoolP256r1` (LV). For 384-bit curves, points are 97 bytes and X is +48 bytes; adjust the point and integer lengths shown in §4.4–§4.6 +accordingly. + +### 4.2 MSE SET AT (Authentication Template) + +``` +00 22 C1 A4 12 + 80 0A 04 00 7F 00 07 02 02 04 02 04 ← PACE OID + 83 01 02 ← password ref = CAN + 84 01 ← curve from §4.1 +00 +``` + +- SW `90 00` → continue. +- SW `63 00` → wrong CAN. Surface this distinctly to the caller (e.g. + a dedicated exception type or error code) so a UI can prompt for + re-entry without retrying PACE blindly. + +### 4.3 GA — Get Encrypted Nonce + +``` +10 86 00 00 02 7C 00 00 ← CLA = 10 (chaining) +``` + +Response: `7C 22 80 20 90 00`. + +Decrypt the nonce with an AES-256 key derived from the CAN: + +``` +nonceKey = SHA-256( CAN-bytes-utf8 || 00 00 00 03 ) ← full 32-byte digest +nonce = AES-CBC-Decrypt(nonceKey, IV = 16×0x00, encryptedNonce) +``` + +(`0x03` is the constant for "nonce decryption key" in the PACE KDF — see +also `0x01` for K_enc and `0x02` for K_mac in §4.5. The full SHA-256 output +is used as the AES-256 key — do not truncate to 16 bytes.) + +> **Deviation from BSI TR-03110.** The standard PACE-GM nonce `s` is one +> AES block — 16 bytes — regardless of key size, and the encrypted-nonce +> response is therefore 16 bytes. This card returns a **two-block** +> (32-byte) ciphertext, decrypts to 32 bytes of plaintext, and §4.4 uses +> the entire 32-byte plaintext as the integer scalar. A strict BSI +> implementation that hard-codes a 16-byte nonce assumption (truncating +> ciphertext or scalar) will compute a different mapped base point than +> the card and the handshake will fail at GA Mutual Authentication. +> Use the full 32-byte block on this card. + +### 4.4 GA — Map Nonce + +All EC points in §4.4–§4.6 use **SEC1 uncompressed encoding**: +`04 || X || Y`, where X and Y are each fixed-width big-endian integers +of the curve's coordinate byte size (32 bytes for 256-bit curves, 48 for +384-bit). Decoders should validate the leading `0x04` byte; PACE-GM +mandates uncompressed form, so other SEC1 prefixes (`02`/`03` +compressed, `00` infinity) should be rejected. + +Generate ephemeral keypair `(d_term, Q_term = d_term · G)` on the curve. +The scalar must be drawn uniformly from `[1, n−1]` where `n` is the +curve order — most ECC libraries provide a helper for this. + +``` +10 86 00 00 45 7C 43 81 41 00 +``` + +Response: `7C 43 82 41 90 00`. + +> Lengths shown (`Lc=0x45`, point=65B) are for the 256-bit curves +> currently observed on LV cards. For a 384-bit PACE curve, points are +> **97 bytes** (uncompressed `04 || X || Y`, two 48-byte coordinates) and +> the inner length tag becomes `0x61` (97), the outer Lc `0x65` (101 = +> 4 + 97). Always derive point size from the curve negotiated in §4.1. + +Compute the mapped base point. `nonce` is the §4.3 plaintext bytes +interpreted as a big-endian integer (the full decrypted block — 32 bytes +for the AES-256 CBC-CMAC variant). All operations are on the EC group: +`·` is scalar multiplication, `+` is **elliptic-curve point addition** +(not coordinate-wise integer addition). + +> The 32-byte nonce, taken as an integer, can in principle exceed the +> curve order `n`. PACE-GM does not require explicit reduction, and +> mainstream EC libraries reduce internally during scalar multiplication +> — `nonce · G` produces the correct point either way. No explicit +> mod-`n` step is needed unless your library rejects oversized scalars. + +``` +H = d_term · Q_card (GM mapping point — BSI calls this H) +G' = nonce · G + H (point addition) +``` + +`H` is **not** the session shared secret — that's `K` in §4.5. `H` is +the intermediate Generic-Mapping point that re-bases the curve before +the second key exchange. Calling both "shared secret" leads to confusion +and bugs; keep them named distinctly when porting. + +### 4.5 GA — Key Agreement + +Generate a second ephemeral keypair on the **mapped** base point: + +``` +d_term2 = random scalar in [1, n−1] +Q_term2 = d_term2 · G' + +10 86 00 00 45 7C 43 83 41 00 +``` + +Response: `7C 43 84 41 90 00`. + +Compute the shared session secret and derive AES-256 session keys. +`K` is the affine X coordinate of the resulting point, encoded as a +**fixed-width big-endian integer of the curve's field byte size**: + +| Curve | Field byte size | `K` length | +|---|---|---| +| `secp256r1` | 32 | 32 B | +| `brainpoolP256r1` | 32 | 32 B | +| `secp384r1` | 48 | 48 B | +| `brainpoolP384r1` | 48 | 48 B | + +Left-pad with zeros if the X integer is shorter than the field size +(rare on uniform inputs but possible). The SHA-256 input length is +therefore `coordSize + 4`. + +> For all four curves listed above the field size and the group-order +> size happen to coincide. **Don't generalise that** to other curves — +> on some curves (e.g. some Koblitz / `sect` curves) field size and +> order size differ, and PACE-GM specifies the *field* size for `K`. + +``` +K = ( d_term2 · Q_card2 ).x ← fixed-width affine X, big-endian +K_enc = SHA-256( K || 00 00 00 01 ) ← full 32-byte digest, AES-256 key +K_mac = SHA-256( K || 00 00 00 02 ) ← full 32-byte digest, AES-256 key +``` + +### 4.6 GA — Mutual Authentication + +Build the auth-token data. The two length bytes track the curve's +point size: + +| Curve family | `7F 49` length | `86` length | `Q` size | +|---|---|---|---| +| 256-bit (`secp256r1`, `brainpoolP256r1`) | `4F` (79) | `41` (65) | 65 B | +| 384-bit (`secp384r1`, `brainpoolP384r1`) | `6F` (111) | `61` (97) | 97 B | + +Both lengths fit in BER short-form for these curves. + +``` +authData = 7F 49 ← public-key info template + 06 0A 04 00 7F 00 07 02 02 04 02 04 ← PACE OID + 86 ← peer's Q from §4.5 +T_term = AES-CMAC(K_mac, authData[with peer's Q = Q_card2])[0..7] +``` + +Bytes shown in the literal example below are the 256-bit form +(`7F 49 4F … 86 41 …`); substitute the 384-bit lengths if working with +P-384 or brainpoolP384r1. + +Send: + +``` +00 86 00 00 0C 7C 0A 85 08 00 ← CLA = 00, no chaining +``` + +Response: `7C 0A 86 08 90 00`. + +Verify: + +``` +T_card_expected = AES-CMAC(K_mac, authData[with peer's Q = Q_term2])[0..7] +must equal T_card returned by the card. +``` + +If the MAC matches, the PACE channel is established. From this point on +**every** APDU must be wrapped per §5. + +### 4.7 Send-Sequence-Counter initialisation + +``` +SSC = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 (16 bytes, all zero) +``` + +The first SM-protected C-APDU increments this to `…0001` before MAC. + +--- + +## 5. Secure messaging + +After §4 every C-APDU must be wrapped and every R-APDU must be unwrapped. +The CLA byte's bits 4–5 are set (`CLA' = CLA | 0x0C`). Confidentiality: +AES-256-CBC. Integrity: AES-256-CMAC truncated to 8 bytes. + +### 5.1 SSC handling + +The SSC is a 16-byte big-endian integer: byte 0 is the most-significant +byte, byte 15 is the least-significant byte. To increment, start at +**index 15** and propagate the carry **toward index 0** — e.g. +`00 …00 FF` becomes `00 …01 00`, and `FF FF …FF` would wrap to all-zero +(which never happens in practice). + +The counter is incremented **twice per command/response pair**: + +1. Increment **before** computing the C-APDU's MAC. +2. Increment again **before** verifying the R-APDU's MAC. + +### 5.2 IV for AES-CBC + +Each command and each response uses an IV derived from the *current* SSC: + +``` +IV = AES-ECB-Encrypt(K_enc, SSC) ← 16-byte single-block output +``` + +(In the reference implementation the IV is computed by AES-ECB-encrypting +the SSC and truncating to 16 bytes — equivalent to a single-block ECB +encryption.) + +### 5.3 C-APDU wrapping + +Given a plaintext APDU `CLA INS P1 P2 [data] [Le]`: + +1. **Masked header (4 bytes, on-wire form)**: + ``` + maskedHeader = (CLA | 0x0C) || INS || P1 || P2 + ``` + These 4 bytes go into the wrapped APDU unchanged. A *padded* 16-byte + form is computed separately for MAC input in step 4 — it never appears + on the wire. + +2. **Encrypted-data DO** (only if `data` is non-empty): + ``` + plain = data || 80 00 … (pad to the next multiple of 16: + always append at least one 0x80 + byte plus zeros — a 16-byte input + becomes 32 bytes, not 16) + ct = AES-CBC-Encrypt(K_enc, IV from §5.2, plain) + + if INS is even (data is sent to the card — every command on this + card; the off-by-one warning below applies here): + DO_data = 87 01 ct + (Lc' = len(ct) + 1; the 0x01 indicator is counted in Lc'. + Example: 16-byte ct → Lc' = 0x11 (17); + 64-byte ct → Lc' = 0x41 (65). + Off-by-one here is a classic porter mistake.) + + if INS is odd (unreachable on this card; included for completeness): + DO_data = 85 ct (Lc' = len(ct), no indicator) + ``` + The reference implementation emits a **single-byte** length here. BER + long-form (`81 `, `82 `, …) is permitted by the standard + but is not produced by the current code; current payloads (PINs, 48-byte + hash inputs, ~65-byte ECC ciphertexts) all stay under 127 bytes after + SM padding and don't need it. Extend before sending larger plaintexts. + + > In this protocol every wrapped C-APDU sends data with even INS + > (`A4`, `20`, `B0`, `22`, `88`, `2A`, `24`, `2C`, `CB`), so `DO_data` + > is always tag `87`. The `85` branch is included for completeness and + > would be reached only if a future operation used an odd-INS write. + +3. **Le DO** (only if `Le` is present): + ``` + DO_Le = 97 01 + ``` + +4. **MAC**: + ``` + macInput = SSC || pad(maskedHeader, 16) || DO_data || DO_Le + if (length not a multiple of 16) macInput = pad(macInput, 16) ← conditional + MAC = AES-CMAC(K_mac, macInput)[0..7] + DO_MAC = 8E 08 MAC + ``` + Where `pad(x, 16)` = append `0x80`, then `0x00` bytes until length is a + multiple of 16. The 4-byte `maskedHeader` is therefore always padded + into a 16-byte block; the final `pad` call after appending DOs is + *only* applied if the running length isn't already aligned. + + > **Worked example — VERIFY PIN1.** Plaintext APDU `00 20 00 01` with + > 12-byte padded PIN, no Le. + > + > ``` + > maskedHeader (4) = 0C 20 00 01 + > pad(maskedHeader, 16) (16) = 0C 20 00 01 80 00 00 00 00 00 00 00 00 00 00 00 + > plain (12) = + > plain padded (16) = || 80 ← 0x80 added + > ct (16) = AES-CBC-Encrypt(K_enc, IV, plain padded) + > DO_data (19) = 87 11 01 ← Lc' = 0x11 + > DO_Le (0) = (absent — no inner Le) + > + > SSC || paddedHeader || DO_data || DO_Le + > = 16 + 16 + 19 + 0 = 51 bytes ← 51 mod 16 = 3, not aligned + > final pad → 64 bytes (append 80 00 00 00 …) + > AES-CMAC(K_mac, …)[0..7] = 8-byte MAC + > ``` + > + > **Observation.** For every wrapped APDU emitted by this protocol the + > running length before the conditional final pad is always either + > `3 mod 16` (when `DO_Le` is absent) or `6 mod 16` (when `DO_Le` is + > present, adding 3 bytes). The `SSC + paddedHeader` prefix is always + > 32 bytes (aligned), and the smallest `DO_data` is `87 11 01 …` = 19 + > bytes ≡ 3 mod 16 — and longer `DO_data` values stay congruent to 3 + > mod 16. So the running length is **never aligned** and the + > conditional final pad fires every time. + > + > **In practice both ends always pad.** The conditional is dead code + > on this card; treat it as "always pad". Note that ISO/IEC 7816-4 + > SM-MAC actually mandates **unconditional** padding (the `0x80` + > terminator is part of the MAC scheme, not optional). The reference + > implementation's conditional form is technically non-standard but + > equivalent here because the conditional never short-circuits. A + > clean port should just unconditionally pad both C-APDU and R-APDU + > MAC inputs (matching the unconditional R-APDU branch in §5.4). + +5. **Wrapped C-APDU**: + ``` + (CLA|0x0C) INS P1 P2 DO_data DO_Le DO_MAC 00 + ``` + - `newLc = len(DO_data) + len(DO_Le) + len(DO_MAC)`. This is the + **outer ISO 7816-4 Lc** (case 3 short-form) — a single byte, valid + range 1..255. For `newLc > 255` an ISO 7816-4 **extended-length + APDU** would be needed (`Lc` encoded as `00 `, three + bytes — not BER long-form). IDEMIA cards over NFC commonly reject + extended-length APDUs (see §6); in practice every wrapped C-APDU + this card receives stays well under the 255-byte short-form limit. + - **Le = `0x00` is appended unconditionally** after the body (request all + remaining bytes). This trailing byte is the SM-layer Le (asks the + card to return its SM-wrapped response) and is **independent** of + the inner `DO_Le = 97 01 ` of step 3, which carries the + application-level Le from the plaintext APDU. + +> **Scope note.** Every wrapped C-APDU emitted by this protocol either +> carries data (most APDUs) or carries an inner Le (`READ BINARY`, +> `INTERNAL AUTHENTICATE`, `PSO COMPUTE`, `PSO DECIPHER`, `GET DATA`). +> The "no data and no Le" branch is not reachable on this card — there's +> no need to special-case it. AES-CMAC over the resulting 32-byte +> aligned `SSC || pad(maskedHeader, 16)` would also work fine if it +> ever did fire. + +### 5.4 R-APDU unwrapping + +A protected response has the layout: + +``` +[ DO_85_or_87 ] [ DO_99 status ] DO_8E mac 90 00 +``` + +1. If the first tag is `85` or `87`: + - Read length byte. If high bit is set (`0x81`, `0x82`, …) read the + additional length bytes per BER. + - For tag `87`: skip the leading `01` indicator byte (decrement length). + - Decrypt body with `AES-CBC-Decrypt(K_enc, IV from §5.2)`. + - Strip ISO 7816-4 padding: scan **from the end** of the decrypted + buffer toward the start, find the last `0x80` byte (it terminates + the padding `80 00…00` suffix), and return everything before it. + Scanning from the start is wrong — the plaintext can legitimately + contain `0x80` bytes earlier in the buffer. + +2. If the next tag is `99 02 90 00`, consume it. Any other status word + inside SM should be treated as an error. + +3. Tag `8E 08 ` must verify: + + ``` + SM R-APDU layout: + + ┌──────────── responsePrefix ───────────┐ + │ │ + [ DO_85_or_87 ……… ] [ DO_99 02 90 00 ] [ 8E 08 ] 90 00 + ←─── encrypted body ─→ ← inner status → ←──── MAC ────→ ← outer SW + (unprotected, + not in MAC) + + verifyInput = SSC || responsePrefix + || 80 00 … ← unconditional ISO 7816-4 pad + (always at least one 0x80 byte; + if the running length is already + a multiple of 16, a full 16-byte + 80 00 … 00 block is appended) + expectedMAC = AES-CMAC(K_mac, verifyInput)[0..7] + ``` + + The trailing `90 00` is **outside** SM and is **not** part of the + MAC input. `responsePrefix` includes the encrypted-body DO and the + inner DO_99 status word (when present); it does not include the + `8E` tag, the MAC bytes, or the unprotected `90 00`. + + **Prose form (in case the box-drawing diagram doesn't render).** + `responsePrefix` is the concatenation of the encrypted-body DO + (`87 …` or `85 …`) and the inner DO_99 (`99 02 90 00`) if present; + it excludes the `8E` MAC tag and value, and excludes the trailing + unprotected `90 00`. + + (Recall: SSC has been incremented per §5.1.) + + > Note: this is **asymmetric** with the C-APDU MAC of §5.3 step 4, + > where padding is applied only when the length is not already a + > multiple of 16. The reference implementation pads conditionally on + > C-APDUs and unconditionally on R-APDUs; both work in practice + > against the LV card. + +4. The trailing `90 00` outside SM is the unprotected SW. + +If any of these checks fail, abort the session — the channel is no longer +trustworthy. + +--- + +## 6. Files used by the LV card + +| FID / Path | Description | SELECT | +|---|---|---| +| `01 1C` | EF.CardAccess (PACE params, ASN.1) | `00 A4 02 0C 02 01 1C` (after MAIN AID) | +| `50 00 / 50 01` | Personal code (UTF-8) | `00 A4 01 0C 02 50 00`, then `00 A4 02 0C 02 50 01` | +| `AD F1 34 01` | Authentication X.509 cert | `00 A4 09 0C 04 AD F1 34 01` (after MAIN AID, P1=`09` = "select by path") | +| `AD F2 34 1F` | Signing X.509 cert | `00 A4 09 0C 04 AD F2 34 1F` | +| `50 31` | EF.OD (PKCS#15 ObjectDirectory) — only used by §13 | `00 A4 02 0C 02 50 31` | + +### READ BINARY chunking (used everywhere) + +``` +loop: + response = transmit( 00 B0 00 ) + accumulate response + offset += len(response) +until SW = 6B 00 (offset out of range — end of file) + or SW = 6A 82 (file not found — treat as empty) +``` + +The whole loop happens **inside** secure messaging once §4 has completed. + +#### Notes for porters + +- Use short-form `Le = 00` only — do not switch to extended `Le = 00 00` + unless you've confirmed the card accepts it. IDEMIA cards commonly + reject extended-length APDUs over NFC. +- The card may return **fewer** bytes than `Le` requested. This is not + an error. Always advance the offset by `response.length`, never by a + fixed 256. +- Chunk size is determined by the card's internal record size, T=CL + frame negotiation, and SM overhead — not by your `Le`. Different + transports yield different chunk sizes (often well below 256 bytes); + the loop must not assume any particular value. +- **`SW1 = 0x61, SW2 = ` requires GET RESPONSE.** When the card + signals `61 xx` ("`xx` more bytes available"), the read isn't finished + — you must immediately issue an unencrypted `00 C0 00 00 ` (GET + RESPONSE), concatenate the body of that response with the body + preceding the `61 xx`, treat the combined buffer as the (single) + SM-wrapped response, then continue your read loop. Higher-level + transports that wrap ISO 7816-4 (typical OS-level NFC APIs) usually + handle this transparently and you can ignore it. Raw transports do + not — you have to implement the GET RESPONSE branch yourself, and + forgetting it manifests as truncated data with no obvious error. + + > **The fragments are not independently MAC'd.** On this card, an + > SM-wrapped response that exceeds one transport frame is split at + > the transport layer (T=CL) but the **logical SM message is a single + > unit with one DO_8E MAC at its end**. The partial body that arrives + > before `61 xx` does not carry its own MAC; you concatenate raw bytes + > (not authenticated chunks), and §5.4 verifies the single MAC over + > the reassembled whole. +- Terminate the read **only** on `6B 00` (offset past EOF) or `6A 82` + (file not found / empty). Do not terminate on a short response — it + just means the card chose a smaller chunk this round. +- P1/P2 encode a 16-bit offset (max `FF FF` = 65535 bytes). All LV files + in this spec fit well within 64 KB. For files > 64 KB you'd need + READ BINARY ODD (`INS = B1`) with an offset DO; this card has no such + files, so a porter doesn't need to implement that variant. + +--- + +## 7. PIN model + +| Code | VERIFY P2 | Retry-counter ref | Allowed length | Notes | +|---|---|---|---|---| +| PIN1 | `0x01` | `0x01` | 4–12 | Auth + decrypt + cert ops; verify after MAIN or Oberthur AID | +| PIN2 | `0x85` | `0x05` | 6–12 | Signing only; verify after QSCD AID | +| PUK | `0x02` | `0x02` | 8–12 | Unblock; verify after MAIN AID | + +All PIN values are right-padded with `0xFF` to **12 bytes** before +transmission. + +### 7.1 Verify PIN + +``` +00 20 00 0C +``` + +| SW | Meaning | +|---|---| +| `90 00` | Success | +| `63 C2` | Wrong PIN, 2 tries left | +| `63 C1` | Wrong PIN, 1 try left | +| `63 C0` | Wrong PIN, **this attempt exhausted the counter** — the PIN is now blocked (use PUK). Distinct from `69 83` for diagnostic logging; UX-equivalent. | +| `69 83` | PIN already blocked / authentication method blocked — no attempt was processed (counter was already at zero before this VERIFY). | +| `63 00` | Wrong PIN, generic / counter not reported. | + +### 7.2 Read retry counter + +``` +SELECT MAIN AID (or QSCD AID for PIN2) +00 CB 3F FF 0A 4D 08 70 06 BF 81 02 A0 80 00 +``` + +`` is the *retry-counter ref* from the table above +(`01`/`05`/`02`). The retry counter is byte **13** (0-based) of the +SM-decrypted plaintext response — i.e. read it after §5.4 unwrapping, +not from the raw on-wire bytes. + +> **Firmware caveat.** The reference implementation hard-codes offset +> 13; the surrounding bytes are a BER-TLV structure that mirrors the +> request template. On a fresh port, log the full plaintext response on +> the first run, locate the retry-counter byte by hand, and confirm it +> lives at index 13 on your card before trusting this offset. Treat it +> as firmware-dependent. +> +> **Robust alternative.** Walk the TLV instead of hard-coding 13. The +> response mirrors the request template `4D 08 70 06 BF 81 …`, +> with the retry counter inside the `BF 81 ` envelope. Find +> `4D` → `70` → `BF 81 ` → first primitive INTEGER-shaped child +> and read that byte. This survives small layout shifts (extra optional +> fields, reordered children) that would invalidate the magic offset. + +### 7.3 Change PIN + +``` +SELECT MAIN AID (or QSCD AID for PIN2) +00 24 00 18 +``` + +`` is the same value as the VERIFY P2 (`0x01` for PIN1, `0x85` +for PIN2, `0x02` for PUK). + +### 7.4 Unblock + change PIN with PUK + +``` +SELECT MAIN AID ← PUK lives in MAIN context +VERIFY PUK (§7.1, P2 = 0x02) +if changing PIN2: SELECT QSCD AID +00 2C 02 0C +``` + +`` is `0x01` for PIN1, `0x85` for PIN2. + +The leading SELECT MAIN AID is required regardless of caller-side state. +Skipping it relies on the active AID happening to be MAIN, which is true +right after PACE but not after sequences that have selected QSCD in +between (e.g. reading PIN2's retry counter, which lives under QSCD). +Always select MAIN first. + +--- + +## 8. Reading certificates + +(Same flow for AUTH and SIGN; only the path differs.) + +``` +SELECT MAIN AID +SELECT cert by path: + Auth: 00 A4 09 0C 04 AD F1 34 01 + Sign: 00 A4 09 0C 04 AD F2 34 1F +READ BINARY chunked (§6) until 6B 00 +``` + +Result is the DER-encoded X.509 certificate. + +--- + +## 9. Authentication (PIN1 → key 0x82) + +``` +SELECT Oberthur AID +VERIFY PIN1 (§7.1, P2 = 0x01) +MSE SET AT: + 00 22 41 A4 06 80 01 04 84 01 82 +INTERNAL AUTHENTICATE: + 00 88 00 00 00 +``` + +- `MSE SET AT` body: `80 01 04` → algorithm reference `0x04`; + `84 01 82` → key reference `0x82`. +- The signing operation here is **INTERNAL AUTHENTICATE** (`INS 0x88`, + ISO 7816-4 §7.5.5) — distinct from the `GENERAL AUTHENTICATE` + (`INS 0x86`) used during the PACE handshake in §4. The card signs the + caller-supplied `` with the auth key and returns the + signature directly. +- The card returns its response as a raw ECDSA signature `r || s` — + **96 bytes** total (48 + 48 for `secp384r1`). +- **`` is sent unmodified.** The card does **not** hash + internally and does **not** zero-pad (unlike §10). The caller is + responsible for the digest / format. For a Web-eID-style auth + challenge against the `secp384r1` user keys, that's typically a + 48-byte SHA-384 hash; the card signs it as-is and returns + `r || s` (96 bytes). + +> **LV vs Estonian:** Estonian IDEMIA uses a 4-byte algorithm reference +> `FF 20 08 00`; LV uses a 1-byte `0x04`. Length bytes must match +> (`80 01 04` vs `80 04 FF 20 08 00`). Do not copy Estonian code blindly. + +--- + +## 10. Signing (PIN2 → key 0x9E) + +``` +SELECT QSCD AID +VERIFY PIN2 (§7.1, P2 = 0x85) +MSE SET DST: + 00 22 41 B6 06 80 01 54 84 01 9E +PSO COMPUTE DIGITAL SIGNATURE: + 00 2A 9E 9A 00 +``` + +- `MSE SET DST` body: algorithm `0x54`, key `0x9E`. +- **Hash padding:** PSO COMPUTE on this card expects exactly **48 bytes** + of input. Shorter hashes (e.g. SHA-256, 32 bytes) must be **left-padded + with zero bytes to 48 bytes** before transmission — i.e. zeros first, + hash second: + + ``` + Input to PSO COMPUTE for SHA-256 digest H (32 bytes): + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ← 16 zero bytes + H[0] H[1] … H[31] ← 32-byte digest + ─── + 48 bytes total + ``` + + Hashes already ≥ 48 bytes (SHA-384, or SHA-512 truncated to 48) are + passed through unchanged. (The 48-byte requirement is a downstream + consequence of the user signing key being `secp384r1`.) + + > **Why left-pad.** The card performs raw ECDSA on the input + > interpreted as a big-endian integer. Leading-zero bytes are + > mathematically inert — `0x00…00 || H` and `H` represent the same + > integer. Right-padding (or any other extension) would change the + > integer value and yield a different signature; left-padding with + > zeros does not. A useful corollary: it doesn't matter if the hash + > itself happens to start with a `0x00` byte (e.g. roughly 1 in 256 + > SHA-256 outputs do) — the resulting integer is the same. + > + > **Hashes longer than 48 bytes** (e.g. raw SHA-512, 64 bytes) are + > unspecified. The reference impl passes them through; the card may + > truncate to the first 48 bytes (per FIPS 186-4 ECDSA convention), + > reject the input, or behave unpredictably. Not exercised on this + > branch — truncate caller-side to 48 bytes if you need a + > deterministic outcome. +- Response is the raw ECDSA signature `r || s` — **96 bytes** total + (48 + 48 for `secp384r1`). + +--- + +## 11. Decryption (PIN1 → key 0x82) + +> ⚠ **Unverified path.** Included for completeness; **not exercised by +> the reference tests on this branch**. The byte-level APDU sequence +> below matches the reference codebase, but the upstream-level +> characterisation of what the card is actually doing (raw ECDH / +> ECKA — see further down) has not been verified against a known-good +> ciphertext on this branch. Verify before relying on this section. + +``` +SELECT Oberthur AID +VERIFY PIN1 (§7.1, P2 = 0x01) +MSE SET CT: + 00 22 41 B8 06 80 01 04 84 01 82 +PSO DECIPHER: + 00 2A 80 86 00 00 ← leading 0x00 is the encrypted-message indicator +``` + +The leading `0x00` byte before the ciphertext is required — it marks the +input as an encrypted message (not a hash). + +**What this operation actually does.** `MSE SET CT (algo 0x04, key 0x82)` +followed by `PSO DECIPHER` with the leading `0x00` indicator is, on +IAS-ECC IDEMIA cards, the **raw ECDH / ECKA** primitive: the input is +an ephemeral peer SEC1 point (`04 || X || Y`, 97 bytes for `secp384r1` +user keys), and the card returns the shared-secret X coordinate. The +host then derives a content-decryption key from that secret per the +application's encryption scheme. (One concrete consumer is CDOC2, the +encrypted-container format used by the broader DigiDoc ecosystem from +which this library originates; other applications can layer arbitrary +schemes on top of the same ECKA primitive.) + +The reference codebase exposes this method on its token interface but +the demo app does not exercise it. If you have no specific ciphertext +format to interoperate with, you can skip this section. + +> Verify against a known good ciphertext before relying on the +> characterisation above — it matches standard IAS-ECC behaviour but is +> not exercised by the reference test path on this branch. + +--- + +## 12. Personal data extraction + +LV stores **only the personal code on-card**; everything else (name, +issuing country, document number, certificate expiry) comes from the +auth certificate's subject DN and `Validity` field. + +> **Cardholder citizenship is not exposed by LV cards over NFC.** The +> cert subject's `C` RDN is the **issuing country** of the certificate +> authority (`LV` on a Latvian eID), not the cardholder's nationality — +> in practice they coincide for citizen-issued eID, but a foreign +> resident's LV-issued card would still carry `C=LV`. Treat the two as +> distinct: do not surface the cert `C` value to users under a +> "citizenship" label. + +> **Certificate expiry vs document expiry.** What is read here is the +> **auth certificate's `notAfter`**, which is typically shorter than the +> physical-card validity printed on the document. LV cards do not expose +> the document expiry over NFC; if your application needs the document +> expiry it must come from a different source (visual inspection, MRZ +> read, or out-of-band data). + +### 12.1 Read the personal code + +``` +SELECT MAIN AID +00 A4 01 0C 02 50 00 ← SELECT DF 0x5000 +00 A4 02 0C 02 50 01 ← SELECT EF 0x5001 +00 B0 00 00 00 ← single-shot READ BINARY (Le=0x00 = up to 256 + bytes; the EF body is ~12 bytes — fits in one + APDU, no chunking needed) +``` + +Strip any trailing `0xFF` bytes (CardOS unused-space padding on +fixed-size EFs) **before** decoding — most string-trimming primitives +operate on whitespace only and won't remove `0xFF`. Then decode the +remaining bytes as UTF-8 and trim ASCII whitespace. + +> If a future card revision puts more than ~250 bytes in EF 0x5001, the +> single-shot read above silently truncates. Switch to the §6 chunked +> loop (terminate on `6B 00`) for any file you don't have a hard +> upper-bound size for. + +### 12.2 Parse the auth certificate subject DN + +Read the auth certificate (§8), parse it as X.509, and extract the +following from the subject DN: + +| RDN OID | Field | Notes | +|---|---|---| +| `2.5.4.4` | surname | | +| `2.5.4.42` | givenName | | +| `2.5.4.5` | serialNumber | format `PNOLV-` | +| `2.5.4.6` (RFC2253 short name `C`) | issuing country | ISO 3166-1 alpha-2 — `LV` on a Latvian eID. **Not** the cardholder's citizenship — see the note in §12. | +| `Validity.notAfter` (X.509 field) | certificate expiry date | not in the DN. This is the auth-cert validity, not the document expiry. | + +The library uses the auth-cert `serialNumber` RDN value (e.g. +`PNOLV-`) as the document number — there is no separate +document-number field on the card. + +#### Recommended: parse the X.500 name structurally + +Use your X.509 library's "RDN by OID" accessor — every mainstream stack +has one — and call it for each OID in the table above. You should not +need to handle DER tag bytes, BER lengths, or RFC 2253 escape sequences +yourself; the library returns each RDN value already decoded to a +native string regardless of whether the source DER encoding was +`UTF8String`, `PrintableString`, `BMPString`, etc. (See §16 for the +concrete Java implementation used in this codebase.) + +#### Fallback: parsing RFC2253 hex-encoded values + +If you must work with the RFC2253 string form (e.g. from +`X500Principal.getName(RFC2253)`), non-ASCII RDN values come back as +`#`-prefixed DER hex: + +``` +2.5.4.4=#0C0A4B7572756B73206265726E73 ← UTF8String "Kuruks berns" +``` + +To decode: +1. Drop the leading `#`. +2. Hex-decode the rest. +3. The first byte is the DER tag (`0x0C` = UTF8String). The next byte is + the length (BER short form), or `0x81 ` (long form with 1 length + byte). Skip those bytes. +4. The remaining bytes are UTF-8 text. + +The string form also requires care with escaped commas inside multi-valued +RDN strings, escaped backslashes, and short-name-vs-OID variation. The +parsed-X500Name path avoids all of this. + +### 12.3 Date of birth from the personal code + +The Latvian personal code carries DOB only in the **old format**. + +Callers should pass the code in canonical form (`DDMMYY-CZZZQ`, with or +without the dash; no surrounding whitespace) — typically the trimmed +UTF-8 output of §12.1. Whitespace handling and Unicode normalisation +are out of scope for the parser. + +> **Do not confuse with the Estonian personal code scheme.** Estonia's +> personal identification code uses a *gender + century* leading digit +> (`1`/`2` → 19th c., `3`/`4` → 20th c., `5`/`6` → 21st c.). Latvia uses +> a *century-only* digit at position 6, with no gender encoding. +> Applying the Estonian rules to a Latvian code yields wrong DOBs. +> Canonical specification is the responsibility of Latvia's PMLP +> (Pilsonības un migrācijas lietu pārvalde — Office of Citizenship and +> Migration Affairs); reference Latvian secondary law if you need to +> validate edge cases. + +> **New-format detector is restrictive.** This spec checks for `32` as +> the leading two digits of the post-2017 format. If Latvia ever +> introduces additional new-format prefixes (e.g. `33…`), the parser +> will fall through to the old-format branch, fail the century-digit +> check at position 6, and return null. That's safe (no wrong DOB +> emitted) but it's not future-proof — revisit this rule when new +> formats are introduced. + +1. Strip dashes from the personal code. +2. If the digit string starts with `32`, the code is post-2017 format: + DOB is **not encoded**, return null. +3. Otherwise (old format `DDMMYY-CZZZQ`): + + | Index | Field | + |---|---| + | 0..1 | day | + | 2..3 | month | + | 4..5 | year (2-digit) | + | 6 | century digit | + + Century digit: `0` → 1800, `1` → 1900, `2` → 2000. Anything else is + invalid — return null and surface a parse error in logs. + + ``` + DOB = (century + yearShort) || "-" || month || "-" || day (ISO 8601) + ``` + +--- + +## 13. Optional: dynamic key-reference discovery (PrKDF) + +The current LV implementation hard-codes `authKeyRef = 0x82` and +`signKeyRef = 0x9E`, so this section is reference material for porters +faced with a card variant whose key slots are unknown. + +> **This is a deliberately minimal walker, not a robust PKCS#15 parser.** +> It assumes a single conformant PrKDF, takes the **first** matching tag +> at every level, and ignores anything else. Treat it as a starting +> point: if you point it at a card whose PKCS#15 layout differs (multiple +> private-key entries, optional fields reordered, RSA mixed with ECC, +> etc.) it will silently return the wrong key reference. Validate +> against your target card before relying on it. + +> **Per-applet PrKDF.** The auth and sign keys on this card live in +> **different applet contexts**: the auth key (`0x82`) is registered in +> the Oberthur AWP applet's PrKDF, the sign key (`0x9E`) in the QSCD +> applet's PrKDF. Each applet has its own EF.OD → PrKDF chain. That's +> why the SELECT step below is parameterised — Oberthur for auth-key +> discovery, QSCD for sign-key discovery — and why running the walk +> under the wrong AID returns the other key (or no key at all). + +``` +SELECT (Oberthur for auth, QSCD for sign) +00 A4 02 0C 02 50 31 ← SELECT EF.OD +READ BINARY chunked (§6) +``` + +Parse the EF.OD as TLVs: + +1. Find a top-level `A0` (PrivateKey directory entry). +2. Inside, find a `30` (SEQUENCE). +3. Inside that, find a 2-byte `04` (OCTET STRING) — that is the PrKDF + file id. + +``` +00 A4 02 0C 02 +READ BINARY chunked (§6) +``` + +Iterate the top-level SEQUENCE entries of PrKDF. For each entry, child[1] +is `CommonKeyAttributes`. Inside that SEQUENCE, find an INTEGER (`02`): + +- Length 1 → byte is the key reference. +- Length 2 → bytes are `00 ` (DER positive padding for refs ≥ `0x80`) + → byte[1] is the key reference. + +Use the first key ref found per AID context (auth in Oberthur, sign in +QSCD). + +--- + +## 14. End-to-end session sketch + +A complete "card-info + sign" session, with literal step ordering: + +``` +1. Connect NfcA, read historical bytes; match against §2. +2. SELECT MAIN AID (§3) +3. PACE handshake using user-entered CAN (§4) +4. Now SM is on; every following APDU is wrapped per §5. +5. Read auth cert: SELECT MAIN, SELECT path AD F1 34 01, READ BINARY chunks. +6. Read sign cert: SELECT MAIN, SELECT path AD F2 34 1F, READ BINARY chunks. +7. Read personal code: SELECT DF 5000, SELECT EF 5001, single-shot + READ BINARY (~12 bytes); strip trailing 0xFF and decode UTF-8. + See §12.1 — the single-shot read assumes the file fits in one + response and would need to switch to §6 chunked READ BINARY if the + file ever grows beyond that. +8. (For card view) Read PIN1 retry counter (§7.2). +9. (For sign) + SELECT QSCD AID + VERIFY PIN2 (§7.1) + MSE SET DST + PSO COMPUTE (§10) +10. (For auth) + SELECT Oberthur AID + VERIFY PIN1 (§7.1) + MSE SET AT + INTERNAL AUTHENTICATE (§9; INS 0x88 — not the + GENERAL AUTHENTICATE 0x86 + used during PACE) +11. Disconnect / let the card field drop. +``` + +If the user re-taps after a disconnection, restart from step 1 — PACE +keys do not survive the loss of the contactless field. + +--- + +## 15. Reference checklist for porters + +Use this list to validate a fresh implementation against a physical LV +card: + +- [ ] Historical bytes match exactly one entry in §2. +- [ ] PACE MSE SET AT (§4.2) returns `90 00` with the right CAN, `63 00` + with a wrong CAN. +- [ ] **First post-PACE APDU is SM-wrapped.** The on-wire CLA byte has + `0x0C` set, the body contains a `8E 08 …` MAC tag, and the + response is similarly wrapped — i.e. you have not silently fallen + through to plaintext (which IDEMIA permits for some unprotected + files and would mask a broken SM implementation by letting cert + reads work anyway). +- [ ] After PACE, the auth certificate (§8) reads as a valid DER X.509. +- [ ] The auth cert subject contains `2.5.4.5 = PNOLV-`, + and the on-card EF 0x5001 contains the same ``. +- [ ] The auth cert subject `2.5.4.6 (C)` is `LV` and is treated as + **issuing country**, not citizenship (§12). +- [ ] PIN1 retry counter (§7.2) reads in 0..3 and decrements after a wrong + VERIFY. +- [ ] Auth (§9) returns a **96-byte** ECDSA signature (`r || s`, 48 + 48 + bytes) — the user keys are `secp384r1`, independent of whichever + 256-bit curve PACE used. +- [ ] Signing (§10) returns a **96-byte** ECDSA signature on a SHA-256 + hash that has been left-padded with zeros to 48 bytes per §10. +- [ ] Old-format personal code (e.g. `010195-12345`) parses to + `1995-01-01`; new-format (`32xxxxxx`) yields no DOB. +- [ ] PUK + new PIN flow (§7.4) succeeds for both PIN1 and PIN2. + +--- + +## 16. Reference implementation (Java/Android) + +The protocol description above stands on its own. This section points +at one concrete implementation — the Java/Android codebase from which +the spec was derived — for cross-checking. A port to any other platform +should follow the spec proper, not these files. + +Source-of-truth Java classes used to derive this spec: + +| File | Role | +|---|---| +| `libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/TokenWithPace.java` | ATR table → dispatch | +| `libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/Idemia.java` | AIDs, cert paths, PIN refs and padding, retry counter, change/unblock | +| `libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/IdemiaWithPace.java` | PACE handshake, secure messaging, dynamic key-ref discovery | +| `libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaIdemiaWithPace.java` | LV-specific MSE values, key refs, personal-data flow | +| `libs/id-card-lib/src/main/java/ee/ria/DigiDoc/idcard/LatviaPersonalDataParser.java` | Personal-code → DOB | + +If this spec and the code disagree, the code is authoritative — please open +a PR to fix the spec. diff --git a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardInfoFragment.kt b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardInfoFragment.kt index e4e36fa..4cdd9f5 100644 --- a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardInfoFragment.kt +++ b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardInfoFragment.kt @@ -68,9 +68,20 @@ class CardInfoFragment : Fragment() { surnameTextView.text = getString(R.string.last_name, dataViewModel.getSurname()) personalCodeTextView.text = getString(R.string.personal_code, dataViewModel.getPersonalCode()) - citizenshipTextView.text = getString(R.string.citizenship, dataViewModel.getCitizenship()) - dateOfExpiryTextView.text = - getString(R.string.expiration, dataViewModel.getExpiryDate()) + val citizenship = dataViewModel.getCitizenship() + val issuingCountry = dataViewModel.getIssuingCountry() + citizenshipTextView.text = when { + citizenship.isNotEmpty() -> getString(R.string.citizenship, citizenship) + !issuingCountry.isNullOrEmpty() -> getString(R.string.issuing_country, issuingCountry) + else -> getString(R.string.citizenship, "-") + } + val docExpiry = dataViewModel.getDocumentExpiryDate() + val certExpiry = dataViewModel.getCertExpiryDate() + dateOfExpiryTextView.text = when { + docExpiry != null -> getString(R.string.document_expiry, docExpiry) + certExpiry != null -> getString(R.string.cert_expiry, certExpiry) + else -> getString(R.string.expiration, "-") + } pin1TextView.text = getString(R.string.pin1_counter, dataViewModel.getPin1Counter()) pin2TextView.text = getString(R.string.pin2_counter, dataViewModel.getPin2Counter()) diff --git a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardReaderFragment.kt b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardReaderFragment.kt index 9867c55..86930a6 100644 --- a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardReaderFragment.kt +++ b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/card/CardReaderFragment.kt @@ -218,6 +218,17 @@ class CardReaderFragment : Fragment() { } val message = ex.message ?: "Error communicating with card" errorLog(logTag, message, ex) + } catch (ex: Exception) { + // libdigidocpp throws plain RuntimeException for TSA/OCSP + // network failures inside extendSignatureProfile; without + // this catch the exception escapes the binder thread and + // the UI hangs on the spinner. Surface it as an error. + setReaderResult(R.drawable.error) + val message = ex.message ?: "Signature extension failed" + requireActivity().runOnUiThread { + Toast.makeText(requireContext(), message, Toast.LENGTH_LONG).show() + } + errorLog(logTag, message, ex) } finally { nfcSmartCardReaderManager.disableNfcReaderMode() } @@ -230,7 +241,14 @@ class CardReaderFragment : Fragment() { private fun addSignature(signatureArray: ByteArray) { signature.setSignatureValue(signatureArray) - signature.extendSignatureProfile(Utils.signatureProfile) + if (Utils.extendSignature) { + // Adds XAdES-T timestamp + OCSP. Requires network access to the + // CA endpoints (e.g. dd-at.ria.ee) — toggled off in the container + // UI when offline or when testing card flow without TSA/OCSP. + signature.extendSignatureProfile(Utils.signatureProfile) + } else { + debugLog(logTag, "Skipping signature extension (BES baseline only)") + } container.save() signatureIsAdded = true } diff --git a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/sign/ContainerFragment.kt b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/sign/ContainerFragment.kt index 57ecb27..f0331c4 100644 --- a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/sign/ContainerFragment.kt +++ b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/sign/ContainerFragment.kt @@ -103,6 +103,11 @@ class ContainerFragment : Fragment() { addSignatureButton ) + binding.checkBoxExtendSignature.isChecked = Utils.extendSignature + binding.checkBoxExtendSignature.setOnCheckedChangeListener { _, isChecked -> + Utils.extendSignature = isChecked + } + backButton.setOnClickListener { clearData() findNavController().popBackStack(R.id.homeFragment, false) diff --git a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/util/Utils.kt b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/util/Utils.kt index c971eb1..3ce41ea 100644 --- a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/util/Utils.kt +++ b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/util/Utils.kt @@ -26,6 +26,15 @@ object Utils { lateinit var container: Container lateinit var signature: Signature var signatureProfile = "time-stamp" + + /** + * When true, [Signature.extendSignatureProfile] is called after the card + * signs — adds the XAdES-T timestamp + OCSP, requiring network access to + * the CA endpoints (e.g. dd-at.ria.ee). When false, the signature is left + * at the BES baseline (no network needed). Toggled from the container UI. + */ + var extendSignature = true + var signatureIsAdded = false fun addFileContent( diff --git a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/viewmodel/DataViewModel.kt b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/viewmodel/DataViewModel.kt index 69e4070..6e03316 100644 --- a/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/viewmodel/DataViewModel.kt +++ b/demoapp/app/src/main/java/ee/ria/DigiDoc/smartcardreader/nfc/example/viewmodel/DataViewModel.kt @@ -12,7 +12,9 @@ class DataViewModel : ViewModel() { private lateinit var surname: String private lateinit var personalCode: String private lateinit var citizenship: String - private lateinit var expiryDate: String + private var issuingCountry: String? = null + private var documentExpiryDate: String? = null + private var certExpiryDate: String? = null private var containerName: String = "" private var pin1Counter: Int = 0 @@ -39,7 +41,9 @@ class DataViewModel : ViewModel() { this.surname = cardData.surname() this.personalCode = cardData.personalCode() this.citizenship = cardData.citizenship() - this.expiryDate = cardData.expiryDate().toString() + this.issuingCountry = cardData.issuingCountry() + this.documentExpiryDate = cardData.documentExpiryDate()?.toString() + this.certExpiryDate = cardData.certExpiryDate()?.toString() } fun getCan(): String { @@ -62,8 +66,16 @@ class DataViewModel : ViewModel() { return citizenship } - fun getExpiryDate(): String { - return expiryDate + fun getIssuingCountry(): String? { + return issuingCountry + } + + fun getDocumentExpiryDate(): String? { + return documentExpiryDate + } + + fun getCertExpiryDate(): String? { + return certExpiryDate } fun setPin1Counter(p1: Int) { diff --git a/demoapp/app/src/main/res/layout/fragment_container.xml b/demoapp/app/src/main/res/layout/fragment_container.xml index ec347b5..5a64b72 100644 --- a/demoapp/app/src/main/res/layout/fragment_container.xml +++ b/demoapp/app/src/main/res/layout/fragment_container.xml @@ -78,6 +78,16 @@ android:textSize="14sp" android:textStyle="bold" /> + +