I ran into a problem when using an older PIV card that uses T=0
with the newer PIV code since 0.11.9. The code attempt to read
in the first 8 bytes of an object using the PIV card GET_DATA command.
This is done to get the length of the object and test if the object
is present, without having to read the full object. (PIV cards
do not have a directory, so they only way to determine if an
object is present and its size it to start to read it.)

With a T=1 card the Le is sent to the card, and it returns 8 bytes.
With the T=0 card, the length is not sent and the card responds
with 61XX saying XX bytes of data are available (00 means 256).
apdu.c will then use get_response to read XX bytes of data and
consider it an error that the card has returned more bytes then
can fit in the buffer.

To be consistent between T=0 and T=1, the attached patch changes
the logic to only read as many bytes as the caller requested.

The code is exercised by the PIV caching code introduced in 3710
in OpenSC-0.11.9. I believe in the field, all the production PIV
cards are using T=1, as I have not seen any bug reports on this.

Please consider adding this fix to 0.11.14 as well as 0.12.

Thanks.


--

 Douglas E. Engert  <deeng...@anl.gov>
 Argonne National Laboratory
 9700 South Cass Avenue
 Argonne, Illinois  60439
 (630) 252-5444
--- src/libopensc/,apdu.c	Mon Aug 23 19:13:26 2010
+++ src/libopensc/apdu.c	Thu Sep 16 16:15:46 2010
@@ -473,7 +473,11 @@
 			/* 0x6100 means at least 256 more bytes to read */
 			le = apdu->sw2 != 0 ? (size_t)apdu->sw2 : 256;
 			/* we try to read at least as much as bytes as 
-			 * promised in the response bytes */
+			 * promised in the response bytes, but not more then
+			 * then requested. So now T=0 responds the same as T=1
+			 */
+			if (buflen < le) 
+				le = buflen;
 			minlen = le;
 
 			do {
@@ -491,6 +495,11 @@
 				memcpy(buf, tbuf, le);
 				buf    += le;
 				buflen -= le;
+
+				/* we have all the data the caller requested 
+				 * even if the card has more data */
+				if (buflen == 0)
+					break;
 
 				minlen -= le;
 				if (r != 0) 
_______________________________________________
opensc-devel mailing list
opensc-devel@lists.opensc-project.org
http://www.opensc-project.org/mailman/listinfo/opensc-devel

Reply via email to