From 1cbb185519fd781859e023530e8a31a6e580aa51 Mon Sep 17 00:00:00 2001 From: frankmorgner Date: Sat, 17 Dec 2011 16:30:24 +0000 Subject: [PATCH] introducing a default SE for all cards git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@673 96b47cad-a561-4643-ad3b-153ac7d7599c --- .../vpicc/virtualsmartcard/SmartcardSAM.py | 7 +++-- .../virtualsmartcard/cards/cryptoflex.py | 9 +++--- .../src/vpicc/virtualsmartcard/cards/ePass.py | 14 +++++----- .../src/vpicc/virtualsmartcard/cards/nPA.py | 28 +++++++++++-------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardSAM.py b/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardSAM.py index 1925dff..f13ebfb 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardSAM.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardSAM.py @@ -57,7 +57,7 @@ class SAM(object): 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.mf = mf @@ -81,7 +81,8 @@ class SAM(object): #Security Environments may be saved to/retrieved from this dictionary 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): """ @@ -126,7 +127,7 @@ class SAM(object): else: SEstr = self.saved_SEs[SEID] SE = loads(SEstr) - if isinstance(SE, Security_Environment): + if isinstance(SE, self.default_se): self.current_SE = SE else: raise SwError(SW["ERR_REFNOTUSABLE"]) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py index d2fbc07..154160b 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py @@ -29,9 +29,8 @@ from virtualsmartcard.ConstantDefinitions import FDB import struct, logging class CryptoflexSE(Security_Environment): - def __init__(self, mf): - Security_Environment.__init__(self) - self.mf = mf + def __init__(self, MF, SAM): + Security_Environment.__init__(self, MF, SAM) def generate_public_key_pair(self, p1, p2, data): """ @@ -96,8 +95,8 @@ class CryptoflexSE(Security_Environment): class CryptoflexSAM(SAM): def __init__(self, mf=None): - SAM.__init__(self, None, None, mf) - self.current_SE = CryptoflexSE(mf) + SAM.__init__(self, None, None, mf, default_se=CryptoflexSE) + self.current_SE = self.default_se(self.mf, self) def generate_public_key_pair(self, p1, p2, data): asym_key = self.current_SE.generate_public_key_pair(p1, p2, data) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/ePass.py b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/ePass.py index 4b22c9d..1b26a4b 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/ePass.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/ePass.py @@ -29,9 +29,11 @@ class ePass_SE(Security_Environment): """This class implements the Security Environment of the ICAO Passports. It 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 - Security_Environment.__init__(self, SE) + self.cct.algorithm = "CC" + self.ct.algorithm = "DES3-CBC" def compute_cryptographic_checksum(self, p1, p2, data): """ @@ -55,6 +57,8 @@ class PassportSAM(SAM): def __init__(self, mf): import virtualsmartcard.SmartcardFilesystem as vsFS + SAM.__init__(self, None, None, mf, default_se = ePass_SE) + ef_dg1 = vsFS.walk(mf, "\x00\x04\x01\x01") dg1 = ef_dg1.readbinary(5) self.mrz1 = dg1[:43] @@ -63,11 +67,7 @@ class PassportSAM(SAM): self.KEnc = None self.KMac = None 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): """ Computes the keys depending on the machine readable diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py index 8558f5b..a77acbe 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py @@ -62,8 +62,9 @@ class nPA_SAM(SAM): def __init__(self, pin, can, mrz, puk, mf): SAM.__init__(self, None, None, mf) self.current_SE.at = nPA_AT_CRT() + self.current_SE.verify_certificate = self.__eac_ta_verify_certificate self.eac_step = 0 - self.eac_ctx = None + self.current_SE.eac_ctx = None self.sec = None self.pin = pin self.can = can @@ -89,7 +90,7 @@ class nPA_SAM(SAM): raise SwError(SW["ERR_INCORRECTPARAMETERS"]) 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) @staticmethod @@ -117,7 +118,7 @@ class nPA_SAM(SAM): 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': self.sec = pace.PACE_SEC_new(self.mrz, pace.PACE_MRZ) 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_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]]) self.eac_step += 1 @@ -145,11 +146,11 @@ class nPA_SAM(SAM): if self.current_SE.at.algorithm != "PACE": 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: 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: raise SwError(SW["ERR_INCORRECTPARAMETERS"]) @@ -162,12 +163,12 @@ class nPA_SAM(SAM): if self.current_SE.at.algorithm != "PACE": 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: if tag == 0x83: 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: raise SwError(SW["ERR_INCORRECTPARAMETERS"]) @@ -180,12 +181,12 @@ class nPA_SAM(SAM): if self.current_SE.at.algorithm != "PACE": raise SwError(SW["WARN_NOINFO63"]) - pace.PACE_STEP3C_derive_keys(self.eac_ctx) - my_token = pace.buf2string(pace.PACE_STEP3D_compute_authentication_token(self.eac_ctx, self.pace_opp_pub_key)) + pace.PACE_STEP3C_derive_keys(self.current_SE.eac_ctx) + 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: 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: raise SwError(SW["ERR_INCORRECTPARAMETERS"]) @@ -194,3 +195,6 @@ class nPA_SAM(SAM): # TODO activate SM return 0x9000, nPA_SAM.__pack_general_authenticate([[0x86, len(my_token), my_token]]) + + def __eac_ta_verify_certificate(se, p1, p2, data): + pass