Use the APDU object throughout (working, but not complete)
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@26 f711b948-2313-0410-aaa9-d29f33439f0b
This commit is contained in:
parent
82eab03c68
commit
2d8068b0e3
|
@ -16,10 +16,10 @@ SECURE_CHANNEL_MACENC = 3
|
|||
MAC_LENGTH = 8
|
||||
|
||||
class Cyberflex_Card(Java_Card):
|
||||
APDU_INITIALIZE_UPDATE = '\x80\x50\x00\x00\x08'
|
||||
APDU_EXTERNAL_AUTHENTICATE = '\x84\x82\x00\x00'
|
||||
APDU_GET_STATUS = '\x84\xF2\x00\x00\x02\x4f\x00'
|
||||
APDU_DELETE = '\x84\xe4\x00\x00'
|
||||
APDU_INITIALIZE_UPDATE = APDU('\x80\x50\x00\x00\x08')
|
||||
APDU_EXTERNAL_AUTHENTICATE = APDU('\x84\x82\x00\x00')
|
||||
APDU_GET_STATUS = APDU('\x84\xF2\x00\x00\x02\x4f\x00')
|
||||
APDU_DELETE = APDU('\x84\xe4\x00\x00')
|
||||
DRIVER_NAME = "Cyberflex"
|
||||
|
||||
ATRS = [
|
||||
|
@ -109,13 +109,11 @@ class Cyberflex_Card(Java_Card):
|
|||
if security_level not in (SECURE_CHANNEL_CLEAR, SECURE_CHANNEL_MAC, SECURE_CHANNEL_MACENC):
|
||||
raise ValueError, "security_level must be one of SECURE_CHANNEL_CLEAR, SECURE_CHANNEL_MAC or SECURE_CHANNEL_MACENC"
|
||||
|
||||
apdu = self.APDU_INITIALIZE_UPDATE[:2] + \
|
||||
chr(keyset_version) + \
|
||||
chr(key_index)
|
||||
|
||||
host_challenge = crypto_utils.generate_host_challenge()
|
||||
apdu = apdu + chr(len(host_challenge)) + \
|
||||
host_challenge
|
||||
|
||||
apdu = APDU(self.APDU_INITIALIZE_UPDATE,
|
||||
p1 = keyset_version, p2 = key_index, lc = APDU.LC_AUTO,
|
||||
content = host_challenge)
|
||||
|
||||
self.secure_channel_state = SECURE_CHANNEL_NONE
|
||||
self.last_mac = '\x00' * 8
|
||||
|
@ -141,9 +139,9 @@ class Cyberflex_Card(Java_Card):
|
|||
host_cryptogram = crypto_utils.calculate_host_cryptogram(
|
||||
self.session_key_enc, card_challenge, host_challenge)
|
||||
|
||||
apdu = self.APDU_EXTERNAL_AUTHENTICATE[:2] + \
|
||||
chr(security_level) + '\x00' + chr(len(host_cryptogram)) + \
|
||||
host_cryptogram
|
||||
apdu = APDU(self.APDU_EXTERNAL_AUTHENTICATE,
|
||||
p1 = security_level, p2 = 0, lc = APDU.LC_AUTO,
|
||||
content = host_cryptogram)
|
||||
|
||||
self.secure_channel_state = SECURE_CHANNEL_MAC
|
||||
result = self.send_apdu(apdu)
|
||||
|
@ -174,15 +172,18 @@ class Cyberflex_Card(Java_Card):
|
|||
|
||||
Returns: the response APDU which can be parsed with
|
||||
utils.parse_status()"""
|
||||
return self.send_apdu(self.APDU_GET_STATUS[:2] + chr(reference_control)
|
||||
+ self.APDU_GET_STATUS[3:])
|
||||
return self.send_apdu(
|
||||
APDU(self.APDU_GET_STATUS,
|
||||
p1 = reference_control)
|
||||
)
|
||||
|
||||
def delete(self, aid):
|
||||
if aid[:5] == DEFAULT_CARD_MANAGER_AID[:5]:
|
||||
print "Cowardly refusing to delete the card manager."
|
||||
raise ValueError, "Undeletable object"
|
||||
tlvaid = chr(0x4f) + chr(len(aid)) + aid
|
||||
apdu = self.APDU_DELETE + chr(len(tlvaid)) + tlvaid
|
||||
apdu = APDU(self.APDU_DELETE, lc = APDU.LC_AUTO,
|
||||
content = tlvaid)
|
||||
result = self.send_apdu(apdu)
|
||||
|
||||
return result[0] == 0x0
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import crypto_utils, utils, pycsc, binascii
|
||||
from utils import APDU
|
||||
|
||||
DEBUG = True
|
||||
#DEBUG = False
|
||||
|
||||
class Card:
|
||||
APDU_GET_RESPONSE = "\x00\xC0\x00\x00"
|
||||
APDU_VERIFY_PIN = "\x00\x20\x00\x00"
|
||||
APDU_GET_RESPONSE = APDU("\x00\xC0\x00\x00")
|
||||
APDU_VERIFY_PIN = APDU("\x00\x20\x00\x00")
|
||||
SW_OK = '\x90\x00'
|
||||
ATRS = []
|
||||
DRIVER_NAME = "Generic"
|
||||
|
@ -25,8 +26,8 @@ class Card:
|
|||
self.sw_changed = False
|
||||
|
||||
def verify_pin(self, pin_number, pin_value):
|
||||
apdu = self.APDU_VERIFY_PIN[:3] + chr(pin_number) + \
|
||||
chr(len(pin_value)) + pin_value
|
||||
apdu = APDU(self.APDU_VERIFY_PIN, P2 = pin_number,
|
||||
lc = APDU.LC_AUTO, content = pin_value)
|
||||
result = self.send_apdu(apdu)
|
||||
return result == self.SW_OK
|
||||
|
||||
|
@ -63,6 +64,7 @@ class Card:
|
|||
return result
|
||||
|
||||
def send_apdu(self, apdu):
|
||||
apdu = apdu.get_string() ## FIXME
|
||||
if not Card._check_apdu(apdu):
|
||||
raise Exception, "Invalid APDU"
|
||||
if DEBUG:
|
||||
|
@ -75,7 +77,7 @@ class Card:
|
|||
|
||||
if result[0] == '\x61':
|
||||
## Need to call GetResponse
|
||||
gr_apdu = self.APDU_GET_RESPONSE + result[1]
|
||||
gr_apdu = APDU(self.APDU_GET_RESPONSE, le = result[1]).get_string()
|
||||
result = self._real_send(gr_apdu)
|
||||
|
||||
if DEBUG:
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
import utils, binascii
|
||||
from generic_card import *
|
||||
from utils import APDU
|
||||
|
||||
class Java_Card(Card):
|
||||
APDU_SELECT_APPLICATION = "\x00\xa4\x04\x00"
|
||||
APDU_SELECT_APPLICATION = APDU("\x00\xa4\x04\x00")
|
||||
DRIVER_NAME = "Generic Java"
|
||||
APPLICATIONS = {
|
||||
"muscle": "\xa0\x00\x00\x00\x01\x01"
|
||||
|
@ -12,7 +13,9 @@ class Java_Card(Card):
|
|||
Card.__init__(self, card = card)
|
||||
|
||||
def select_application(self, aid):
|
||||
result = self.send_apdu(self.APDU_SELECT_APPLICATION + chr(len(aid)) + aid)
|
||||
result = self.send_apdu(
|
||||
APDU(self.APDU_SELECT_APPLICATION, lc = APDU.LC_AUTO,
|
||||
content = aid) )
|
||||
return result
|
||||
|
||||
def cmd_selectapplication(self, *args):
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
# -*- coding: iso-8859-1 -*-
|
||||
|
||||
import pycsc, utils, cards, os, re, binascii, sys, exceptions, traceback
|
||||
print_backtrace = False
|
||||
print_backtrace = True
|
||||
|
||||
try:
|
||||
import readline
|
||||
|
|
6
utils.py
6
utils.py
|
@ -95,6 +95,8 @@ class APDU(list):
|
|||
OFFSET_LC = 4
|
||||
OFFSET_LE = 4
|
||||
OFFSET_CONTENT = 5
|
||||
|
||||
LC_AUTO = None
|
||||
|
||||
"""Class for an APDU that mostly behaves like a list."""
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -124,7 +126,7 @@ class APDU(list):
|
|||
if len(self) < 4:
|
||||
self.extend([0] * (4-len(self)))
|
||||
if len(self) < self.OFFSET_LC+1:
|
||||
self[self.OFFSET_LC:self.OFFSET_LC+1] = [None]
|
||||
self[self.OFFSET_LC:self.OFFSET_LC+1] = [self.LC_AUTO]
|
||||
|
||||
le = None
|
||||
for (kw, arg) in kwargs.items():
|
||||
|
@ -153,7 +155,7 @@ class APDU(list):
|
|||
else:
|
||||
self[self.OFFSET_LE:self.OFFSET_LE+1] = (le,)
|
||||
|
||||
if self[self.OFFSET_LC] == None:
|
||||
if self[self.OFFSET_LC] == self.LC_AUTO:
|
||||
if len(self) > self.OFFSET_CONTENT:
|
||||
self[self.OFFSET_LC] = len(self)-self.OFFSET_CONTENT
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue