introducing a default SE for all cards

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@673 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2011-12-17 16:30:24 +00:00
parent 97fe61ae69
commit 1cbb185519
4 changed files with 31 additions and 27 deletions

View File

@@ -57,7 +57,7 @@ class SAM(object):
indexed via the path to the corresponding container. indexed via the path to the corresponding container.
""" """
def __init__(self, PIN, cardNumber, mf=None, cardSecret=None): def __init__(self, PIN, cardNumber, mf=None, cardSecret=None, default_se=Security_Environment):
self.PIN = PIN self.PIN = PIN
self.mf = mf self.mf = mf
@@ -81,7 +81,8 @@ class SAM(object):
#Security Environments may be saved to/retrieved from this dictionary #Security Environments may be saved to/retrieved from this dictionary
self.saved_SEs = {} self.saved_SEs = {}
self.current_SE = Security_Environment(self.mf, self) self.default_se = default_se
self.current_SE = default_se(self.mf, self)
def set_MF(self, mf): def set_MF(self, mf):
""" """
@@ -126,7 +127,7 @@ class SAM(object):
else: else:
SEstr = self.saved_SEs[SEID] SEstr = self.saved_SEs[SEID]
SE = loads(SEstr) SE = loads(SEstr)
if isinstance(SE, Security_Environment): if isinstance(SE, self.default_se):
self.current_SE = SE self.current_SE = SE
else: else:
raise SwError(SW["ERR_REFNOTUSABLE"]) raise SwError(SW["ERR_REFNOTUSABLE"])

View File

@@ -29,9 +29,8 @@ from virtualsmartcard.ConstantDefinitions import FDB
import struct, logging import struct, logging
class CryptoflexSE(Security_Environment): class CryptoflexSE(Security_Environment):
def __init__(self, mf): def __init__(self, MF, SAM):
Security_Environment.__init__(self) Security_Environment.__init__(self, MF, SAM)
self.mf = mf
def generate_public_key_pair(self, p1, p2, data): def generate_public_key_pair(self, p1, p2, data):
""" """
@@ -96,8 +95,8 @@ class CryptoflexSE(Security_Environment):
class CryptoflexSAM(SAM): class CryptoflexSAM(SAM):
def __init__(self, mf=None): def __init__(self, mf=None):
SAM.__init__(self, None, None, mf) SAM.__init__(self, None, None, mf, default_se=CryptoflexSE)
self.current_SE = CryptoflexSE(mf) self.current_SE = self.default_se(self.mf, self)
def generate_public_key_pair(self, p1, p2, data): def generate_public_key_pair(self, p1, p2, data):
asym_key = self.current_SE.generate_public_key_pair(p1, p2, data) asym_key = self.current_SE.generate_public_key_pair(p1, p2, data)

View File

@@ -29,9 +29,11 @@ class ePass_SE(Security_Environment):
"""This class implements the Security Environment of the ICAO Passports. It """This class implements the Security Environment of the ICAO Passports. It
is required in order to use the send sequence counter for secure messaging. is required in order to use the send sequence counter for secure messaging.
""" """
def __init__(self, SE, ssc=None): def __init__(self, MF, SAM, ssc=None):
Security_Environment.__init__(self, MF, SAM)
self.ssc = ssc self.ssc = ssc
Security_Environment.__init__(self, SE) self.cct.algorithm = "CC"
self.ct.algorithm = "DES3-CBC"
def compute_cryptographic_checksum(self, p1, p2, data): def compute_cryptographic_checksum(self, p1, p2, data):
""" """
@@ -55,6 +57,8 @@ class PassportSAM(SAM):
def __init__(self, mf): def __init__(self, mf):
import virtualsmartcard.SmartcardFilesystem as vsFS import virtualsmartcard.SmartcardFilesystem as vsFS
SAM.__init__(self, None, None, mf, default_se = ePass_SE)
ef_dg1 = vsFS.walk(mf, "\x00\x04\x01\x01") ef_dg1 = vsFS.walk(mf, "\x00\x04\x01\x01")
dg1 = ef_dg1.readbinary(5) dg1 = ef_dg1.readbinary(5)
self.mrz1 = dg1[:43] self.mrz1 = dg1[:43]
@@ -63,11 +67,7 @@ class PassportSAM(SAM):
self.KEnc = None self.KEnc = None
self.KMac = None self.KMac = None
self.__computeKeys() self.__computeKeys()
SAM.__init__(self, None, None, mf)
self.current_SE = ePass_SE(None, None)
self.current_SE.cct.algorithm = "CC"
self.current_SE.ct.algorithm = "DES3-CBC"
def __computeKeys(self): def __computeKeys(self):
""" """
Computes the keys depending on the machine readable Computes the keys depending on the machine readable

View File

@@ -62,8 +62,9 @@ class nPA_SAM(SAM):
def __init__(self, pin, can, mrz, puk, mf): def __init__(self, pin, can, mrz, puk, mf):
SAM.__init__(self, None, None, mf) SAM.__init__(self, None, None, mf)
self.current_SE.at = nPA_AT_CRT() self.current_SE.at = nPA_AT_CRT()
self.current_SE.verify_certificate = self.__eac_ta_verify_certificate
self.eac_step = 0 self.eac_step = 0
self.eac_ctx = None self.current_SE.eac_ctx = None
self.sec = None self.sec = None
self.pin = pin self.pin = pin
self.can = can self.can = can
@@ -89,7 +90,7 @@ class nPA_SAM(SAM):
raise SwError(SW["ERR_INCORRECTPARAMETERS"]) raise SwError(SW["ERR_INCORRECTPARAMETERS"])
def __eac_abort(self): def __eac_abort(self):
pace.EAC_CTX_clear_free(self.eac_ctx) pace.EAC_CTX_clear_free(self.current_SE.eac_ctx)
pace.PACE_SEC_clear_free(self.sec) pace.PACE_SEC_clear_free(self.sec)
@staticmethod @staticmethod
@@ -117,7 +118,7 @@ class nPA_SAM(SAM):
self.__eac_abort() self.__eac_abort()
self.eac_ctx = pace.EAC_CTX_new() self.current_SE.eac_ctx = pace.EAC_CTX_new()
if self.current_SE.at.keyref == '\x01': if self.current_SE.at.keyref == '\x01':
self.sec = pace.PACE_SEC_new(self.mrz, pace.PACE_MRZ) self.sec = pace.PACE_SEC_new(self.mrz, pace.PACE_MRZ)
elif self.current_SE.at.keyref == '\x02': elif self.current_SE.at.keyref == '\x02':
@@ -131,9 +132,9 @@ class nPA_SAM(SAM):
ef_card_access = self.mf.select('fid', 0x011c) ef_card_access = self.mf.select('fid', 0x011c)
ef_card_access_data = ef_card_access.getenc('data') ef_card_access_data = ef_card_access.getenc('data')
pace.EAC_CTX_init_ef_cardaccess(ef_card_access_data, self.eac_ctx) pace.EAC_CTX_init_ef_cardaccess(ef_card_access_data, self.current_SE.eac_ctx)
nonce = pace.buf2string(pace.PACE_STEP1_enc_nonce(self.eac_ctx, self.sec)) nonce = pace.buf2string(pace.PACE_STEP1_enc_nonce(self.current_SE.eac_ctx, self.sec))
resp = nPA_SAM.__pack_general_authenticate([[0x80, len(nonce), nonce]]) resp = nPA_SAM.__pack_general_authenticate([[0x80, len(nonce), nonce]])
self.eac_step += 1 self.eac_step += 1
@@ -145,11 +146,11 @@ class nPA_SAM(SAM):
if self.current_SE.at.algorithm != "PACE": if self.current_SE.at.algorithm != "PACE":
raise SwError(SW["WARN_NOINFO63"]) raise SwError(SW["WARN_NOINFO63"])
pubkey = pace.buf2string(pace.PACE_STEP3A_generate_mapping_data(self.eac_ctx)) pubkey = pace.buf2string(pace.PACE_STEP3A_generate_mapping_data(self.current_SE.eac_ctx))
for tag, length, value in tlv_data: for tag, length, value in tlv_data:
if tag == 0x81: if tag == 0x81:
pace.PACE_STEP3A_map_generator(self.eac_ctx, pace.get_buf(value)) pace.PACE_STEP3A_map_generator(self.current_SE.eac_ctx, pace.get_buf(value))
else: else:
raise SwError(SW["ERR_INCORRECTPARAMETERS"]) raise SwError(SW["ERR_INCORRECTPARAMETERS"])
@@ -162,12 +163,12 @@ class nPA_SAM(SAM):
if self.current_SE.at.algorithm != "PACE": if self.current_SE.at.algorithm != "PACE":
raise SwError(SW["WARN_NOINFO63"]) raise SwError(SW["WARN_NOINFO63"])
my_epp_pubkey = pace.buf2string(pace.PACE_STEP3B_generate_ephemeral_key(self.eac_ctx)) my_epp_pubkey = pace.buf2string(pace.PACE_STEP3B_generate_ephemeral_key(self.current_SE.eac_ctx))
for tag, length, value in tlv_data: for tag, length, value in tlv_data:
if tag == 0x83: if tag == 0x83:
self.pace_opp_pub_key = pace.get_buf(value) self.pace_opp_pub_key = pace.get_buf(value)
pace.PACE_STEP3B_compute_shared_secret(self.eac_ctx, self.pace_opp_pub_key) pace.PACE_STEP3B_compute_shared_secret(self.current_SE.eac_ctx, self.pace_opp_pub_key)
else: else:
raise SwError(SW["ERR_INCORRECTPARAMETERS"]) raise SwError(SW["ERR_INCORRECTPARAMETERS"])
@@ -180,12 +181,12 @@ class nPA_SAM(SAM):
if self.current_SE.at.algorithm != "PACE": if self.current_SE.at.algorithm != "PACE":
raise SwError(SW["WARN_NOINFO63"]) raise SwError(SW["WARN_NOINFO63"])
pace.PACE_STEP3C_derive_keys(self.eac_ctx) pace.PACE_STEP3C_derive_keys(self.current_SE.eac_ctx)
my_token = pace.buf2string(pace.PACE_STEP3D_compute_authentication_token(self.eac_ctx, self.pace_opp_pub_key)) my_token = pace.buf2string(pace.PACE_STEP3D_compute_authentication_token(self.current_SE.eac_ctx, self.pace_opp_pub_key))
for tag, length, value in tlv_data: for tag, length, value in tlv_data:
if tag == 0x85: if tag == 0x85:
pace.PACE_STEP3D_verify_authentication_token(self.eac_ctx, pace.get_buf(value)) pace.PACE_STEP3D_verify_authentication_token(self.current_SE.eac_ctx, pace.get_buf(value))
else: else:
raise SwError(SW["ERR_INCORRECTPARAMETERS"]) raise SwError(SW["ERR_INCORRECTPARAMETERS"])
@@ -194,3 +195,6 @@ class nPA_SAM(SAM):
# TODO activate SM # TODO activate SM
return 0x9000, nPA_SAM.__pack_general_authenticate([[0x86, len(my_token), my_token]]) return 0x9000, nPA_SAM.__pack_general_authenticate([[0x86, len(my_token), my_token]])
def __eac_ta_verify_certificate(se, p1, p2, data):
pass