From: <lu...@us...> - 2009-01-13 08:51:07
|
Revision: 249 http://pyscard.svn.sourceforge.net/pyscard/?rev=249&view=rev Author: ludov Date: 2009-01-13 08:51:04 +0000 (Tue, 13 Jan 2009) Log Message: ----------- sample to use a pinpad reader Added Paths: ----------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Added: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py (rev 0) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2009-01-13 08:51:04 UTC (rev 249) @@ -0,0 +1,125 @@ +#! /usr/bin/env python +""" +Sample for python PCSC wrapper module: send a Control Code to a card or +reader + +__author__ = "Ludovic Rousseau" + +Copyright 2009 Ludovic Rousseau +Author: Ludovic Rousseau, mailto:lud...@fr... + +This file is part of pyscard. + +pyscard 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. + +pyscard 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 pyscard; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +""" + +from smartcard.scard import * +from smartcard.util import toASCIIBytes + +def candoverifypin(hCard): + FEATURE_VERIFY_PIN_DIRECT = 6 + return parse_get_feature_request(hCard, FEATURE_VERIFY_PIN_DIRECT) + +def candomodifypin(hCard): + FEATURE_MODIFY_PIN_DIRECT = 7 + return parse_get_feature_request(hCard, FEATURE_MODIFY_PIN_DIRECT) + +def parse_get_feature_request(hCard, feature): + # check the reader can do a verify pin + CM_IOCTL_GET_FEATURE_REQUEST = SCARD_CTL_CODE(3400) + hresult, response = SCardControl(hcard, CM_IOCTL_GET_FEATURE_REQUEST, []) + if hresult!=SCARD_S_SUCCESS: + raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult) + print response + while (len(response) > 0): + tag = response[0] + if (feature == tag): + return (((((response[2]<<8) + response[3])<<8) + response[4])<<8) + response[5] + response = response[6:] + +def verifypin(hCard, control=None): + if None==control: + control = candoverifypin(hCard) + if (None == control): + raise error, "Not a pinpad" + hresult, response = SCardControl(hcard, control, []) + if hresult!=SCARD_S_SUCCESS: + raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult) + return hresult, response + +try: + hresult, hcontext = SCardEstablishContext( SCARD_SCOPE_USER ) + if hresult!=0: + raise 'Failed to establish context: ' + SCardGetErrorMessage(hresult) + print 'Context established!' + + try: + hresult, readers = SCardListReaders( hcontext, [] ) + if hresult!=0: + raise error, 'Failed to list readers: ' + SCardGetErrorMessage(hresult) + print 'PCSC Readers:', readers + + if len(readers)<1: + raise error, 'No smart card readers' + + for zreader in readers: + + print 'Trying to Control reader:', zreader + + try: + hresult, hcard, dwActiveProtocol = SCardConnect( + hcontext, zreader, SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0 ) + if hresult!=0: + raise error, 'Unable to connect: ' + SCardGetErrorMessage(hresult) + print 'Connected with active protocol', dwActiveProtocol + + try: + cmd_verify = candoverifypin(hcard) + if (cmd_verify): + print "can do verify pin: 0x%08X" % cmd_verify + + cmd_modify = candomodifypin(hcard) + if (cmd_modify): + print "can do modify pin: 0x%08X" % cmd_modify + + (hresult, response) = verifypin(hcard, cmd_verify) + r = "" + for i in xrange(len(response)): + r += "%c" % response[i] + print 'Control:', r + finally: + hresult = SCardDisconnect( hcard, SCARD_UNPOWER_CARD ) + if hresult!=0: + raise error, 'Failed to disconnect: ' + SCardGetErrorMessage(hresult) + print 'Disconnected' + + except error, (message): + print error, message + + finally: + hresult = SCardReleaseContext( hcontext ) + if hresult!=SCARD_S_SUCCESS: + raise 'Failed to release context: ' + SCardGetErrorMessage(hresult) + print 'Released context.' + +except: + import sys + print sys.exc_info()[0], ':', sys.exc_info()[1] + +import sys +if 'win32'==sys.platform: + print 'press Enter to continue' + sys.stdin.read(1) + Property changes on: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py ___________________________________________________________________ Added: svn:executable + * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lu...@us...> - 2009-11-24 08:33:06
|
Revision: 359 http://pyscard.svn.sourceforge.net/pyscard/?rev=359&view=rev Author: ludov Date: 2009-11-24 08:32:59 +0000 (Tue, 24 Nov 2009) Log Message: ----------- rename candoverifypin in can_do_verify_pin and candomodifypin in can_do_modify_pin Modified Paths: -------------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Modified: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2009-11-22 17:00:29 UTC (rev 358) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2009-11-24 08:32:59 UTC (rev 359) @@ -28,11 +28,11 @@ from smartcard.scard import * from smartcard.util import toASCIIBytes -def candoverifypin(hCard): +def can_do_verify_pin(hCard): FEATURE_VERIFY_PIN_DIRECT = 6 return parse_get_feature_request(hCard, FEATURE_VERIFY_PIN_DIRECT) -def candomodifypin(hCard): +def can_do_modify_pin(hCard): FEATURE_MODIFY_PIN_DIRECT = 7 return parse_get_feature_request(hCard, FEATURE_MODIFY_PIN_DIRECT) @@ -51,7 +51,7 @@ def verifypin(hCard, control=None): if None==control: - control = candoverifypin(hCard) + control = can_do_verif_ypin(hCard) if (None == control): raise error, "Not a pinpad" hresult, response = SCardControl(hcard, control, []) @@ -86,11 +86,11 @@ print 'Connected with active protocol', dwActiveProtocol try: - cmd_verify = candoverifypin(hcard) + cmd_verify = can_do_verif_ypin(hcard) if (cmd_verify): print "can do verify pin: 0x%08X" % cmd_verify - cmd_modify = candomodifypin(hcard) + cmd_modify = can_do_modify_pin(hcard) if (cmd_modify): print "can do modify pin: 0x%08X" % cmd_modify This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <jda...@us...> - 2010-04-20 15:26:34
|
Revision: 393 http://pyscard.svn.sourceforge.net/pyscard/?rev=393&view=rev Author: jdaussel Date: 2010-04-20 15:26:28 +0000 (Tue, 20 Apr 2010) Log Message: ----------- Fixed typo in can_do_verify_pin call Modified Paths: -------------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Modified: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-04-20 13:08:20 UTC (rev 392) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-04-20 15:26:28 UTC (rev 393) @@ -51,7 +51,7 @@ def verifypin(hCard, control=None): if None==control: - control = can_do_verif_ypin(hCard) + control = can_do_verify_ypin(hCard) if (None == control): raise error, "Not a pinpad" hresult, response = SCardControl(hcard, control, []) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lu...@us...> - 2010-04-20 19:21:15
|
Revision: 397 http://pyscard.svn.sourceforge.net/pyscard/?rev=397&view=rev Author: ludov Date: 2010-04-20 19:21:09 +0000 (Tue, 20 Apr 2010) Log Message: ----------- fix typo in can_do_verify_pin Modified Paths: -------------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Modified: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-04-20 15:45:17 UTC (rev 396) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-04-20 19:21:09 UTC (rev 397) @@ -51,7 +51,7 @@ def verifypin(hCard, control=None): if None==control: - control = can_do_verify_ypin(hCard) + control = can_do_verify_pin(hCard) if (None == control): raise error, "Not a pinpad" hresult, response = SCardControl(hcard, control, []) @@ -86,7 +86,7 @@ print 'Connected with active protocol', dwActiveProtocol try: - cmd_verify = can_do_verif_ypin(hcard) + cmd_verify = can_do_verify_pin(hcard) if (cmd_verify): print "can do verify pin: 0x%08X" % cmd_verify This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lu...@us...> - 2010-11-30 07:56:54
|
Revision: 517 http://pyscard.svn.sourceforge.net/pyscard/?rev=517&view=rev Author: ludov Date: 2010-11-30 07:56:48 +0000 (Tue, 30 Nov 2010) Log Message: ----------- Complete the verifypin() method to make it work Modified Paths: -------------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Modified: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-10-22 12:44:52 UTC (rev 516) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-11-30 07:56:48 UTC (rev 517) @@ -58,7 +58,23 @@ control = can_do_verify_pin(hCard) if (None == control): raise error, "Not a pinpad" - hresult, response = SCardControl(hcard, control, []) + + command = [0x00, # bTimerOut + 0x00, # bTimerOut2 + 0x82, # bmFormatString + 0x04, # bmPINBlockString + 0x00, # bmPINLengthFormat + 0x08, 0x04, # wPINMaxExtraDigit + 0x02, # bEntryValidationCondition + 0x01, # bNumberMessage + 0x04, 0x09, # wLangId + 0x00, # bMsgIndex + 0x00, 0x00, 0x00, # bTeoPrologue + 13, 0, 0, 0, # ulDataLength + 0x00, 0x20, 0x00, 0x00, 0x08, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30 # abData + ] + hresult, response = SCardControl(hcard, control, command) if hresult != SCARD_S_SUCCESS: raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult) return hresult, response @@ -84,12 +100,19 @@ try: hresult, hcard, dwActiveProtocol = SCardConnect( - hcontext, zreader, SCARD_SHARE_DIRECT, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) + hcontext, zreader, SCARD_SHARE_SHARED, + SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) if hresult != SCARD_S_SUCCESS: raise error, 'Unable to connect: ' + SCardGetErrorMessage(hresult) print 'Connected with active protocol', dwActiveProtocol try: + SELECT = [0x00, 0xA4, 0x04, 0x00, 0x06, 0xA0, 0x00, + 0x00, 0x00, 0x18, 0xFF] + hresult, response = SCardTransmit(hcard, dwActiveProtocol, SELECT) + if hresult != SCARD_S_SUCCESS: + raise error, 'Failed to transmit: ' + SCardGetErrorMessage(hresult) + cmd_verify = can_do_verify_pin(hcard) if (cmd_verify): print "can do verify pin: 0x%08X" % cmd_verify @@ -99,10 +122,7 @@ print "can do modify pin: 0x%08X" % cmd_modify (hresult, response) = verifypin(hcard, cmd_verify) - r = "" - for i in xrange(len(response)): - r += "%c" % response[i] - print 'Control:', r + print 'Control:', response finally: hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD) if hresult != SCARD_S_SUCCESS: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lu...@us...> - 2010-11-30 07:58:39
|
Revision: 518 http://pyscard.svn.sourceforge.net/pyscard/?rev=518&view=rev Author: ludov Date: 2010-11-30 07:58:33 +0000 (Tue, 30 Nov 2010) Log Message: ----------- Remove extra parenthesis Modified Paths: -------------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Modified: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-11-30 07:56:48 UTC (rev 517) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-11-30 07:58:33 UTC (rev 518) @@ -46,9 +46,9 @@ if hresult != SCARD_S_SUCCESS: raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult) print response - while (len(response) > 0): + while len(response) > 0: tag = response[0] - if (feature == tag): + if feature == tag: return (((((response[2] << 8) + response[3]) << 8) + response[4]) << 8) + response[5] response = response[6:] @@ -56,7 +56,7 @@ def verifypin(hCard, control=None): if None == control: control = can_do_verify_pin(hCard) - if (None == control): + if None == control: raise error, "Not a pinpad" command = [0x00, # bTimerOut @@ -121,7 +121,7 @@ if (cmd_modify): print "can do modify pin: 0x%08X" % cmd_modify - (hresult, response) = verifypin(hcard, cmd_verify) + hresult, response = verifypin(hcard, cmd_verify) print 'Control:', response finally: hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lu...@us...> - 2010-11-30 08:11:56
|
Revision: 519 http://pyscard.svn.sourceforge.net/pyscard/?rev=519&view=rev Author: ludov Date: 2010-11-30 08:11:50 +0000 (Tue, 30 Nov 2010) Log Message: ----------- Use new exception form instead of deprecated one Modified Paths: -------------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Modified: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-11-30 07:58:33 UTC (rev 518) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2010-11-30 08:11:50 UTC (rev 519) @@ -27,6 +27,7 @@ from smartcard.scard import * from smartcard.util import toASCIIBytes +from smartcard.pcsc.PCSCExceptions import * def can_do_verify_pin(hCard): @@ -44,7 +45,7 @@ CM_IOCTL_GET_FEATURE_REQUEST = SCARD_CTL_CODE(3400) hresult, response = SCardControl(hcard, CM_IOCTL_GET_FEATURE_REQUEST, []) if hresult != SCARD_S_SUCCESS: - raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult) + raise BaseSCardException(hresult) print response while len(response) > 0: tag = response[0] @@ -57,7 +58,7 @@ if None == control: control = can_do_verify_pin(hCard) if None == control: - raise error, "Not a pinpad" + raise Exception("Not a pinpad") command = [0x00, # bTimerOut 0x00, # bTimerOut2 @@ -76,23 +77,23 @@ ] hresult, response = SCardControl(hcard, control, command) if hresult != SCARD_S_SUCCESS: - raise error, 'SCardControl failed: ' + SCardGetErrorMessage(hresult) + raise BaseSCardException(hresult) return hresult, response try: hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER) if hresult != SCARD_S_SUCCESS: - raise error, 'Failed to establish context: ' + SCardGetErrorMessage(hresult) + raise EstablishContextException(hresult) print 'Context established!' try: hresult, readers = SCardListReaders(hcontext, []) if hresult != SCARD_S_SUCCESS: - raise error, 'Failed to list readers: ' + SCardGetErrorMessage(hresult) + raise ListReadersException(hresult) print 'PCSC Readers:', readers if len(readers) < 1: - raise error, 'No smart card readers' + raise Exception("No smart card readers") for zreader in readers: @@ -103,7 +104,7 @@ hcontext, zreader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1) if hresult != SCARD_S_SUCCESS: - raise error, 'Unable to connect: ' + SCardGetErrorMessage(hresult) + raise BaseSCardException(hresult) print 'Connected with active protocol', dwActiveProtocol try: @@ -111,7 +112,7 @@ 0x00, 0x00, 0x18, 0xFF] hresult, response = SCardTransmit(hcard, dwActiveProtocol, SELECT) if hresult != SCARD_S_SUCCESS: - raise error, 'Failed to transmit: ' + SCardGetErrorMessage(hresult) + raise BaseSCardException(hresult) cmd_verify = can_do_verify_pin(hcard) if (cmd_verify): @@ -126,7 +127,7 @@ finally: hresult = SCardDisconnect(hcard, SCARD_UNPOWER_CARD) if hresult != SCARD_S_SUCCESS: - raise error, 'Failed to disconnect: ' + SCardGetErrorMessage(hresult) + raise BaseSCardException(hresult) print 'Disconnected' except error, (message): @@ -135,7 +136,7 @@ finally: hresult = SCardReleaseContext(hcontext) if hresult != SCARD_S_SUCCESS: - raise error, 'Failed to release context: ' + SCardGetErrorMessage(hresult) + raise ReleaseContextException(hresult) print 'Released context.' except error, e: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <lu...@us...> - 2011-07-01 13:14:53
|
Revision: 533 http://pyscard.svn.sourceforge.net/pyscard/?rev=533&view=rev Author: ludov Date: 2011-07-01 13:14:47 +0000 (Fri, 01 Jul 2011) Log Message: ----------- PEP8: E202 whitespace before ']' Modified Paths: -------------- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py Modified: trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py =================================================================== --- trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2011-07-01 13:13:35 UTC (rev 532) +++ trunk/pyscard/src/smartcard/Examples/scard-api/sample_pinpad.py 2011-07-01 13:14:47 UTC (rev 533) @@ -73,8 +73,7 @@ 0x00, 0x00, 0x00, # bTeoPrologue 13, 0, 0, 0, # ulDataLength 0x00, 0x20, 0x00, 0x00, 0x08, 0x30, 0x30, 0x30, 0x30, 0x30, - 0x30, 0x30, 0x30 # abData - ] + 0x30, 0x30, 0x30] # abData hresult, response = SCardControl(hcard, control, command) if hresult != SCARD_S_SUCCESS: raise BaseSCardException(hresult) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |