Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Грешка приликом читања личне карте за странце #30

Open
X3KT0 opened this issue May 10, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@X3KT0
Copy link

X3KT0 commented May 10, 2024

Графички Баш Челик одма улази у циклус "читање-грешка".
CLI верзия каже "Error saving document: reading card: reading card: reading document 0 file: parsing file header: invalid length" па се завршава.
Исти баш челик са истим хардвер читачом ради са картицом здравственог осигурања на истом рачунару (онда проблем није у хардверу).
Исти хардвер читач добро ради са проблематичном картом на другом рачунару со Виндовзом (онда проблем није у карти).
ЛК има на себи сертификате.
ATR је 3bff9400008131804380318065b0850201f3120fff82900079
Издата је у априлу 2023.

Немам држављанске карте, онда не могу да проверим са њом.

@ubavic
Copy link
Owner

ubavic commented May 10, 2024

Preći ću na engleski, pošto mislim da ćemo se tako lakše razumeti:

First, thank you for bringing this up, and thank you for very detailed description of the problem. I suppose you have ID card for foreigners, and it has a slightly different structure of data on card. As BasCelik is developed completely independently from official apps, there are some edge cases like this that I didn't cover. Unfortunately, I don't have access to ID cards for foreigners on which I could test, so I can't resolve bug, even if I am pretty sure what is happening.

You can try to fix bug yourself (I can introduce you to the code and the workflow), or maybe we could meet for a coffee so I could see what is exactly happening (if you are in Belgrade).

@ubavic ubavic added the bug Something isn't working label May 10, 2024
@X3KT0
Copy link
Author

X3KT0 commented May 10, 2024

Thank you for the answer.
I'm not in Belgrade, but sometimes go there.
Will contact you by email before that (I found your email address on your website).

@ubavic
Copy link
Owner

ubavic commented May 10, 2024

I will answer here your question from #29 about reading from smart cards (because that issue isn't a bug, a this really is).

There is no way (AFAIK) you can read all data from smart card. Smart card behaves like small filesystem, and data is divided into 'files'. Each file has an address, and you must send a request with the exact address to be able to read that file.

On Serbian ID cards, data is stored in 4 files.

Because the Serbian vehicle card has the same ATR as the Serbian ID card, Bas Celik tries to distinct these two documents by reading DOCUMENT_FILE on address 0f02. This file is present on ID cards but not on vehicle cards. But, it seems that cards for foreigners don't have this file, and therefore your card is wrongly processed as a vehicle card.

Please, in card.go, on line 41, change

tempIdCard := Gemalto{smartCard: sc}
if tempIdCard.testGemalto() {
	card = Gemalto{smartCard: sc}
} else {
	card = VehicleCard{smartCard: sc}
}

to

card = Gemalto{smartCard: sc}

This will force the program to process your card as an ID card.

Your card will be processed inside readIdCard form idCard.go. You can add there fmt.Printf to see raw data. That function will fail because your card don't have file on the address 0f02. You can modify this function to see if other files are present. For example, replace the whole function with this:

func readIdCard[Id IdDocument](card Id) (*document.IdDocument, error) {
	doc := document.IdDocument{}

	rsp, err := card.readFile(DOCUMENT_FILE_LOC, false)
	if err != nil {
		fmt.Println("reading document file:", err)
	} else {
		fields, err := parseTLV(rsp)
		if err != nil {
			fmt.Println(err)
		} else {
			assignField(fields, 1546, &doc.DocumentNumber)
			assignField(fields, 1547, &doc.DocumentType)
			assignField(fields, 1548, &doc.DocumentSerialNumber)
			assignField(fields, 1549, &doc.IssuingDate)
			assignField(fields, 1550, &doc.ExpiryDate)
			assignField(fields, 1551, &doc.IssuingAuthority)
			localization.FormatDate(&doc.IssuingDate)
			localization.FormatDate(&doc.ExpiryDate)
		}
	}

	rsp, err = card.readFile(PERSONAL_FILE_LOC, false)
	if err != nil {
		fmt.Println("reading personal file:", err)
	} else {
		fields, err := parseTLV(rsp)
		if err != nil {
			fmt.Println(err)
		} else {
			assignField(fields, 1558, &doc.PersonalNumber)
			assignField(fields, 1559, &doc.Surname)
			assignField(fields, 1560, &doc.GivenName)
			assignField(fields, 1561, &doc.ParentGivenName)
			assignField(fields, 1562, &doc.Sex)
			assignField(fields, 1563, &doc.PlaceOfBirth)
			assignField(fields, 1564, &doc.CommunityOfBirth)
			assignField(fields, 1565, &doc.StateOfBirth)
			assignField(fields, 1566, &doc.DateOfBirth)
			localization.FormatDate(&doc.DateOfBirth)
		}
	}

	rsp, err = card.readFile(RESIDENCE_FILE_LOC, false)
	if err != nil {
		fmt.Println("reading residence file:", err)
	} else {
		fields, err := parseTLV(rsp)
		if err != nil {
			fmt.Println(err)
		} else {
			assignField(fields, 1568, &doc.State)
			assignField(fields, 1569, &doc.Community)
			assignField(fields, 1570, &doc.Place)
			assignField(fields, 1571, &doc.Street)
			assignField(fields, 1572, &doc.AddressNumber)
			assignField(fields, 1573, &doc.AddressLetter)
			assignField(fields, 1574, &doc.AddressEntrance)
			assignField(fields, 1575, &doc.AddressFloor)
			assignField(fields, 1578, &doc.AddressApartmentNumber)
			assignField(fields, 1580, &doc.AddressDate)
			localization.FormatDate(&doc.AddressDate)
		}
	}

	rsp, err = card.readFile(PHOTO_FILE_LOC, true)
	if err != nil {
		fmt.Println("reading photo file:", err)
	} else {
		doc.Portrait, _, err = image.Decode(bytes.NewReader(rsp))
		if err != nil {
			fmt.Println("decoding photo file:", err)
		}
	}

	return &doc, nil
}

If you don't conclude anything from this, then we need to peek into the official app to see how it communicates with foreigners cards.

Btw, I suppose that you know how to compile go programs. Compiling Bas Celik should not be a problem on Linux. If you want to skip building the whole GUI version you can do:

go build -tags "cli"  .

@X3KT0
Copy link
Author

X3KT0 commented May 10, 2024

Thank you for the help!
I managed to compile it with the patches you gave. Actually, with the first one only, because after applying it, the program stops with Error saving document: reading card: initializing card: initializing card: response not OK

I think we have two possible ways: either I take my card and go to Belgrade; or I can install linux on a machine with the cardreader plugged in and give you an ssh access to it (if you like the idea, of course).

@ubavic
Copy link
Owner

ubavic commented May 10, 2024

Hm, interesting. It seem that ID cards for foreigners have different initialization sequence. This complicates things

Very cool idea with ssh :) We can try that, but I am afraid I will have to test your card with the official app. Then I will be able to see exactly what data is transferred to the card.

BTW, if you are in NS, or somewhere near BG, I can drop by there.

Feel free to send me a mail when you have time

@RodiAda
Copy link

RodiAda commented Oct 1, 2024

HI! I am in Belgrade and I have the TRP card )) If you want we can meet and debug the issue ))

@loskutov
Copy link
Contributor

loskutov commented Oct 3, 2024

The response for the initialization request seems to be OK (0x90 0x00) when [0xA0, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00] is used as AID (also used with vehicle cards).
However, it then fails reading the file (the response for "select file" is 0x6A 0x86, "Incorrect P1 or P2 parameter"). Trying the APDU formats used for medical/vehicle cards for file selection leads to the same result.

@ubavic
Copy link
Owner

ubavic commented Oct 4, 2024

@loskutov Thanks for information

@X3KT0 @RodiAda @loskutov I just released v2 branch which among other things brings support for foreigner cards. Could you compile this branch and test your cards with it?

Edit: If you have PDFs of your cards made by state app, please compare it with PDF created with Baš Čelik. It should have same values listed.

@loskutov
Copy link
Contributor

loskutov commented Oct 4, 2024

Works like a charm for me!

The only difference I see is the address format: with the state app, it's entitled as "Prebivalište i adresa stana" and the value is like "BEOGRAD, AREA, NEKA ULICA 001", whilst with Baš Čelik it's just "Prebivalište" and the value format is "NEKA ULICA 001, AREA, BEOGRAD".
The match is also not pixel-perfect in terms of word wraps but I guess it's not really supposed to be otherwise.

@ubavic
Copy link
Owner

ubavic commented Oct 5, 2024

Great, glad it works now :)

The difference in the address field label was already noted in #6. But, once again, I tested this today with two IDs and the official app produces PDFs with "Prebivalište". It seems that the label depends on some other field or maybe card release date. I am not sure. The Field AddressLabel looks suspicious, but I still haven't found a card with this field present.

I will change the order in the address field, that is a confirmed bug :) I usually strive to produce pixel perfect PDFs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants