Now is possible pipe encrypt & decrypt commands.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -23,7 +23,7 @@ import sys
|
|||||||
try:
|
try:
|
||||||
from smartcard.CardType import AnyCardType
|
from smartcard.CardType import AnyCardType
|
||||||
from smartcard.CardRequest import CardRequest
|
from smartcard.CardRequest import CardRequest
|
||||||
from smartcard.Exceptions import CardRequestTimeoutException
|
from smartcard.Exceptions import CardRequestTimeoutException, CardConnectionException
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
print('ERROR: smarctard module not found! Install pyscard package.\nTry with `pip install pyscard`')
|
print('ERROR: smarctard module not found! Install pyscard package.\nTry with `pip install pyscard`')
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
@@ -59,6 +59,7 @@ import platform
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from argparse import RawTextHelpFormatter
|
from argparse import RawTextHelpFormatter
|
||||||
|
|
||||||
|
pin = None
|
||||||
|
|
||||||
class APDUResponse(Exception):
|
class APDUResponse(Exception):
|
||||||
def __init__(self, sw1, sw2):
|
def __init__(self, sw1, sw2):
|
||||||
@@ -82,6 +83,10 @@ def send_apdu(card, command, p1, p2, data=None):
|
|||||||
apdu = [0x00, command]
|
apdu = [0x00, command]
|
||||||
|
|
||||||
apdu = apdu + [p1, p2] + lc + dataf + le
|
apdu = apdu + [p1, p2] + lc + dataf + le
|
||||||
|
try:
|
||||||
|
response, sw1, sw2 = card.connection.transmit(apdu)
|
||||||
|
except CardConnectionException:
|
||||||
|
card.connection.reconnect()
|
||||||
response, sw1, sw2 = card.connection.transmit(apdu)
|
response, sw1, sw2 = card.connection.transmit(apdu)
|
||||||
if (sw1 != 0x90):
|
if (sw1 != 0x90):
|
||||||
if (sw1 == 0x6A and sw2 == 0x82):
|
if (sw1 == 0x6A and sw2 == 0x82):
|
||||||
@@ -90,6 +95,12 @@ def send_apdu(card, command, p1, p2, data=None):
|
|||||||
response, sw1, sw2 = card.connection.transmit(apdu)
|
response, sw1, sw2 = card.connection.transmit(apdu)
|
||||||
if (sw1 == 0x90):
|
if (sw1 == 0x90):
|
||||||
return response
|
return response
|
||||||
|
elif (sw1 == 0x69 and sw2 == 0x82):
|
||||||
|
response, sw1, sw2 = card.connection.transmit([0x00, 0x20, 0x00, 0x81, len(pin)] + list(pin.encode()) + [0x0])
|
||||||
|
if (sw1 == 0x90):
|
||||||
|
response, sw1, sw2 = card.connection.transmit(apdu)
|
||||||
|
if (sw1 == 0x90):
|
||||||
|
return response
|
||||||
raise APDUResponse(sw1, sw2)
|
raise APDUResponse(sw1, sw2)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
@@ -173,6 +184,8 @@ def pki(card, args):
|
|||||||
print('Error: no PKI is passed. Use --default to retrieve default PKI.')
|
print('Error: no PKI is passed. Use --default to retrieve default PKI.')
|
||||||
|
|
||||||
def login(card, args):
|
def login(card, args):
|
||||||
|
global pin
|
||||||
|
pin = args.pin
|
||||||
try:
|
try:
|
||||||
response = send_apdu(card, 0x20, 0x00, 0x81, list(args.pin.encode()))
|
response = send_apdu(card, 0x20, 0x00, 0x81, list(args.pin.encode()))
|
||||||
except APDUResponse:
|
except APDUResponse:
|
||||||
@@ -418,11 +431,10 @@ def cipher(card, args):
|
|||||||
sys.stdout.buffer.write(bytes(ret))
|
sys.stdout.buffer.write(bytes(ret))
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
print('Pico HSM Tool v1.8')
|
sys.stderr.buffer.write(b'Pico HSM Tool v1.8\n')
|
||||||
print('Author: Pol Henarejos')
|
sys.stderr.buffer.write(b'Author: Pol Henarejos\n')
|
||||||
print('Report bugs to https://github.com/polhenarejos/pico-hsm/issues')
|
sys.stderr.buffer.write(b'Report bugs to https://github.com/polhenarejos/pico-hsm/issues\n')
|
||||||
print('')
|
sys.stderr.buffer.write(b'\n\n')
|
||||||
print('')
|
|
||||||
cardtype = AnyCardType()
|
cardtype = AnyCardType()
|
||||||
try:
|
try:
|
||||||
# request card insertion
|
# request card insertion
|
||||||
|
|||||||
Reference in New Issue
Block a user