File opensc-0.22.0-latvian-eid.patch of Package opensc

From 343b3bb628da35bd2af7fe3b067a99c05db1d0a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alberts=20Muktup=C4=81vels?= <alberts.muktupavels@gmail.com>
Date: Thu, 7 Mar 2019 22:36:32 +0200
Subject: [PATCH] libopensc: recognize Latvian eID card

https://github.com/OpenSC/OpenSC/issues/1275
---
 src/libopensc/card-iasecc.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/libopensc/card-iasecc.c b/src/libopensc/card-iasecc.c
index 8682e143..ad4f8356 100644
--- a/src/libopensc/card-iasecc.c
+++ b/src/libopensc/card-iasecc.c
@@ -83,6 +83,8 @@ static const struct sc_atr_table iasecc_known_atrs[] = {
         { "3B:DD:00:00:81:31:FE:45:80:F9:A0:00:00:00:77:01:08:00:07:90:00:00",
 	  "FF:FF:00:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:00",
 		"IAS/ECC v1.0.1 Oberthur", SC_CARD_TYPE_IASECC_OBERTHUR,  0, NULL },
+	{ "3B:DD:18:00:81:31:FE:45:90:4C:41:54:56:49:41:2D:65:49:44:90:00:8C", NULL,
+		"IAS/ECC v1.0.1 Latvia eID", SC_CARD_TYPE_IASECC_OBERTHUR, 0, NULL },
 	{ "3B:7D:13:00:00:4D:44:57:2D:49:41:53:2D:43:41:52:44:32", NULL,
 		"IAS/ECC v1.0.1 Sagem MDW-IAS-CARD2", SC_CARD_TYPE_IASECC_SAGEM,  0, NULL },
 	{ "3B:7F:18:00:00:00:31:B8:64:50:23:EC:C1:73:94:01:80:82:90:00", NULL,
@@ -490,16 +492,24 @@ iasecc_oberthur_match(struct sc_card *card)
 {
 	struct sc_context *ctx = card->ctx;
 	unsigned char *hist = card->reader->atr_info.hist_bytes;
+	size_t hist_bytes_len = card->reader->atr_info.hist_bytes_len;
 
 	LOG_FUNC_CALLED(ctx);
 
-	if (*hist != 0x80 || ((*(hist+1)&0xF0) != 0xF0))
-		LOG_FUNC_RETURN(ctx, SC_ERROR_OBJECT_NOT_FOUND);
+	if (hist_bytes_len >= 2 && *hist == 0x80 && ((*(hist + 1) & 0xF0) == 0xF0)) {
+		size_t aid_len = *(hist + 1) & 0x0F;
 
-	sc_log_hex(ctx, "AID in historical_bytes", hist + 2, *(hist+1) & 0x0F);
+		sc_log_hex(ctx, "AID in historical_bytes", hist + 2, aid_len);
 
-	if (memcmp(hist + 2, OberthurIASECC_AID.value, *(hist+1) & 0x0F))
-		LOG_FUNC_RETURN(ctx, SC_ERROR_RECORD_NOT_FOUND);
+		if (hist_bytes_len - 2 >= aid_len && memcmp(hist + 2, OberthurIASECC_AID.value, aid_len))
+			LOG_FUNC_RETURN(ctx, SC_ERROR_RECORD_NOT_FOUND);
+	}
+	else if (hist_bytes_len >= 1 && *hist == 0x90) {
+		sc_log_hex(ctx, "Proprietary data in historical_bytes", hist + 1, hist_bytes_len - 1);
+	}
+	else {
+		LOG_FUNC_RETURN(ctx, SC_ERROR_OBJECT_NOT_FOUND);
+	}
 
 	if (!card->ef_atr)
 		card->ef_atr = calloc(1, sizeof(struct sc_ef_atr));
-- 
2.34.1