Le 21/04/2011 11:19, Viktor TARASOV a écrit :
Le 21/04/2011 10:55, Frank Morgner a écrit :
Hi!

I think that a flag could be invented for switching between these
two modes of operation (the otherwise unused "flags" parameter to
sc_read_binary).  The default could be the mode that honors passed
in parameters.
Actually the 0x6282 is mapped to the general SC_ERROR_CARD_CMD_FAILED
error.
http://www.opensc-project.org/opensc/browser/trunk/src/libopensc/iso7816.c?rev=5237#L35

Possible resolution could be:

A new code error is introduced for the SW 0x6282 .
The 6282 error is ignored if it's returned by the 'embedded'
iso7816_read_binary .
http://www.opensc-project.org/opensc/browser/trunk/src/libopensc/iso7816.c?rev=5237#L137
Line 138 very well tests the return code. BUT sc_check_sw is not called
unless the number of bytes to be read is 0 (line 132, 133). So the
iso7816_read_binary almost never recognizes errors which are
communicated via the status bytes. So an other solution could be to
always check the status bytes (not only for apdu.resplen == 0).
You have a reason,
and I guess that 6282 SW is never returned with resplen=0.
Well, "never" is a bit strict. It depends on the card of course.
Afaiu, to return responlen=0 the offset has to be at the limit of the file size
and this situation is a subject of 'invalid P1-P2' error.

Solution could be like in attached diff.
Sorry, I have no appropriate card to test.
Your patch works for me. But I am not sure if exclusively checking for
6282 and ignoring the other error codes of the status bytes is
appropriate.
It's open to be extended with the other 'warnings' that can be ignored,
but ...


Why are these ignored at all, has it something to do with
the retransmission as you stated in the commit message of r5237?
... the other 'warnings' should not happen with 'READ BINARY' when data are 
returned,
with exception of 6281 'Part of the returned data may be corrupted'.
I guess that this 'warning' is an error and cannot be ignored.
An appropriate checking has to be added to the proposed diff .
Here is the diff of the complete patch proposal.


Cheers, Frank.
Kind wishes,
Viktor.

_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel



--
Viktor Tarasov  <viktor.tara...@opentrust.com>

Index: src/libopensc/iso7816.c
===================================================================
--- src/libopensc/iso7816.c     (révision 5384)
+++ src/libopensc/iso7816.c     (copie de travail)
@@ -31,8 +31,8 @@
 
 static const struct sc_card_error iso7816_errors[] = {
        { 0x6200, SC_ERROR_MEMORY_FAILURE,      "State of non-volatile memory 
unchanged" },
-       { 0x6281, SC_ERROR_MEMORY_FAILURE,      "Part of returned data may be 
corrupted" },
-       { 0x6282, SC_ERROR_CARD_CMD_FAILED,     "End of file/record reached 
before reading Le bytes" },
+       { 0x6281, SC_ERROR_CORRUPTED_DATA,      "Part of returned data may be 
corrupted" },
+       { 0x6282, SC_WARNING_FILE_END_REACHED,  "Warning: end of file/record 
reached before reading Le bytes" },
        { 0x6283, SC_ERROR_CARD_CMD_FAILED,     "Selected file invalidated" },
        { 0x6284, SC_ERROR_CARD_CMD_FAILED,     "FCI not formatted according to 
ISO 7816-4" },
 
@@ -133,6 +133,13 @@
                SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, 
sc_check_sw(card, apdu.sw1, apdu.sw2));
        memcpy(buf, recvbuf, apdu.resplen);
 
+       r =  sc_check_sw(card, apdu.sw1, apdu.sw2);
+       if (r == SC_WARNING_FILE_END_REACHED)
+               SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, apdu.resplen);
+       /*TODO: probably can be replaced by the general TEST_RET macro */
+       else if (r == SC_ERROR_CORRUPTED_DATA)
+               SC_FUNC_RETURN(card->ctx, SC_LOG_DEBUG_VERBOSE, r);
+
        if (apdu.resplen < count)   {
                r = iso7816_read_binary(card, idx + apdu.resplen, buf + 
apdu.resplen, count - apdu.resplen, flags);
                SC_TEST_RET(card->ctx, SC_LOG_DEBUG_NORMAL, r, "APDU transmit 
failed");
Index: src/libopensc/errors.h
===================================================================
--- src/libopensc/errors.h      (révision 5384)
+++ src/libopensc/errors.h      (copie de travail)
@@ -65,6 +65,7 @@
 #define SC_ERROR_FILE_ALREADY_EXISTS           -1215
 #define SC_ERROR_DATA_OBJECT_NOT_FOUND         -1216
 #define SC_ERROR_NOT_ENOUGH_MEMORY             -1217
+#define SC_ERROR_CORRUPTED_DATA                        -1218
 
 /* Returned by OpenSC library when called with invalid arguments */
 #define SC_ERROR_INVALID_ARGUMENTS             -1300
@@ -106,6 +107,10 @@
 #define SC_ERROR_INVALID_PIN_REFERENCE         -1509
 #define SC_ERROR_FILE_TOO_SMALL                        -1510
 
+/* Warnings */ 
+#define SC_WARNING                             -1600
+#define SC_WARNING_FILE_END_REACHED            -1601
+
 /* Errors that do not fit the categories above */
 #define SC_ERROR_UNKNOWN                       -1900
 #define SC_ERROR_PKCS15_APP_NOT_FOUND          -1901
Index: src/libopensc/errors.c
===================================================================
--- src/libopensc/errors.c      (révision 5384)
+++ src/libopensc/errors.c      (copie de travail)
@@ -48,6 +48,7 @@
                "Reader in use by another application"
        };
        const int rdr_base = -SC_ERROR_READER;
+
        const char *card_errors[] = {
                "Card command failed",
                "File not found",
@@ -67,8 +68,10 @@
                "File already exists",
                "Data object not found",
                "Not enough memory on card",
+               "Part of returned data may be corrupted"
        };
        const int card_base = -SC_ERROR_CARD_CMD_FAILED;
+
        const char *arg_errors[] = {
                "Invalid arguments",
                "UNUSED",
@@ -78,6 +81,7 @@
                "Invalid data",
        };
        const int arg_base = -SC_ERROR_INVALID_ARGUMENTS;
+
        const char *int_errors[] = {
                "Internal error",
                "Invalid ASN.1 object",
@@ -98,6 +102,7 @@
                "Not implemented"
        };
        const int int_base = -SC_ERROR_INTERNAL;
+
        const char *p15i_errors[] = {
                "Generic PKCS#15 initialization error",
                "Syntax error",
@@ -112,6 +117,13 @@
                "File too small",
        };
        const int p15i_base = -SC_ERROR_PKCS15INIT;
+
+       const char *warnings[] = {
+               "Warning: no information given"
+               "Warning: end of file/record reached before reading Le bytes"
+       };
+       const int warning_base = -SC_WARNING;
+
        const char *misc_errors[] = {
                "Unknown error",
                "PKCS#15 compatible smart card not found",
@@ -125,7 +137,12 @@
                return no_errors;
        if (error < 0)
                error = -error;
-       if (error >= misc_base) {
+
+       if (error >= warning_base) {
+               errors = warnings;
+               count = DIM(warnings);
+               err_base = warning_base;
+       } else if (error >= misc_base) {
                errors = misc_errors;
                count = DIM(misc_errors);
                err_base = misc_base;
_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to