|
| 1 | +import datetime, random |
| 2 | + |
| 3 | +from .message import FinTSMessage |
| 4 | +from .types import SegmentSequence |
| 5 | +from .formals import SecurityProfile, SecurityRole, IdentifiedRole, DateTimeType, UsageEncryption, SecurityIdentificationDetails, SecurityDateTime, EncryptionAlgorithm, KeyName, CompressionFunction, OperationMode, EncryptionAlgorithmCoded, AlgorithmParameterName, AlgorithmParameterIVName, KeyType, SecurityMethod, SecurityApplicationArea, UserDefinedSignature, HashAlgorithm, SignatureAlgorithm, UserDefinedSignature |
| 6 | +from .segments.message import HNVSK3, HNVSD1, HNSHK4, HNSHA2 |
| 7 | + |
| 8 | +class EncryptionMechanism: |
| 9 | + def encrypt(self, message: FinTSMessage): |
| 10 | + raise NotImplemented() |
| 11 | + |
| 12 | + def decrypt(self, message: FinTSMessage): |
| 13 | + raise NotImplemented() |
| 14 | + |
| 15 | +class AuthenticationMechanism: |
| 16 | + def sign_prepare(self, message: FinTSMessage): |
| 17 | + raise NotImplemented() |
| 18 | + |
| 19 | + def sign_commit(self, message: FinTSMessage): |
| 20 | + raise NotImplemented() |
| 21 | + |
| 22 | + def verify(self, message: FinTSMessage): |
| 23 | + raise NotImplemented() |
| 24 | + |
| 25 | +class PinTanDummyEncryptionMechanism(EncryptionMechanism): |
| 26 | + def __init__(self, security_method_version=1): |
| 27 | + super().__init__() |
| 28 | + self.security_method_version = security_method_version |
| 29 | + |
| 30 | + def encrypt(self, message: FinTSMessage): |
| 31 | + assert message.segments[0].header.type == 'HNHBK' |
| 32 | + assert message.segments[-1].header.type == 'HNHBS' |
| 33 | + |
| 34 | + plain_segments = message.segments[1:-1] |
| 35 | + del message.segments[1:-1] |
| 36 | + |
| 37 | + _now = datetime.datetime.now() |
| 38 | + |
| 39 | + message.segments.insert(1, |
| 40 | + HNVSK3( |
| 41 | + security_profile=SecurityProfile(SecurityMethod.PIN, self.security_method_version), |
| 42 | + security_function='998', |
| 43 | + security_role=SecurityRole.ISS, |
| 44 | + security_identification_details=SecurityIdentificationDetails( |
| 45 | + IdentifiedRole.MS, |
| 46 | + identifier=message.dialog.client.system_id, |
| 47 | + ), |
| 48 | + security_datetime=SecurityDateTime( |
| 49 | + DateTimeType.STS, |
| 50 | + _now.date(), |
| 51 | + _now.time(), |
| 52 | + ), |
| 53 | + encryption_algorithm=EncryptionAlgorithm( |
| 54 | + UsageEncryption.OSY, |
| 55 | + OperationMode.CBC, |
| 56 | + EncryptionAlgorithmCoded.TWOKEY3DES, |
| 57 | + b'\x00'*8, |
| 58 | + AlgorithmParameterName.KYE, |
| 59 | + AlgorithmParameterIVName.IVC, |
| 60 | + ), |
| 61 | + key_name=KeyName( |
| 62 | + message.dialog.client.bank_identifier, |
| 63 | + message.dialog.client.user_id, |
| 64 | + KeyType.V, |
| 65 | + 0, |
| 66 | + 0, |
| 67 | + ), |
| 68 | + compression_function=CompressionFunction.NULL, |
| 69 | + ) |
| 70 | + ) |
| 71 | + message.segments[1].header.number = 998 |
| 72 | + message.segments.insert(2, |
| 73 | + HNVSD1( |
| 74 | + data=SegmentSequence(segments=plain_segments) |
| 75 | + ) |
| 76 | + ) |
| 77 | + message.segments[2].header.number = 999 |
| 78 | + |
| 79 | + def decrypt(self, message: FinTSMessage): |
| 80 | + pass |
| 81 | + |
| 82 | + |
| 83 | +class PinTanOneStepAuthenticationMechanism(AuthenticationMechanism): |
| 84 | + def __init__(self, pin, tan=None): |
| 85 | + self.pin=pin |
| 86 | + self.tan=tan |
| 87 | + self.pending_signature=None |
| 88 | + |
| 89 | + def sign_prepare(self, message: FinTSMessage): |
| 90 | + _now = datetime.datetime.now() |
| 91 | + rand = random.SystemRandom() |
| 92 | + |
| 93 | + self.pending_signature = HNSHK4( |
| 94 | + security_profile = SecurityProfile(SecurityMethod.PIN, 1), |
| 95 | + security_function = '999', |
| 96 | + security_reference = rand.randint(1000000, 9999999), |
| 97 | + security_application_area = SecurityApplicationArea.SHM, |
| 98 | + security_role = SecurityRole.ISS, |
| 99 | + security_identification_details = SecurityIdentificationDetails( |
| 100 | + IdentifiedRole.MS, |
| 101 | + identifier=message.dialog.client.system_id, |
| 102 | + ), |
| 103 | + security_reference_number = 1, ## FIXME |
| 104 | + security_datetime = SecurityDateTime( |
| 105 | + DateTimeType.STS, |
| 106 | + _now.date(), |
| 107 | + _now.time(), |
| 108 | + ), |
| 109 | + hash_algorithm = HashAlgorithm( |
| 110 | + usage_hash = '1', |
| 111 | + hash_algorithm = '999', |
| 112 | + algorithm_parameter_name = '1', |
| 113 | + ), |
| 114 | + signature_algorithm = SignatureAlgorithm( |
| 115 | + usage_signature = '6', |
| 116 | + signature_algorithm = '10', |
| 117 | + operation_mode = '16', |
| 118 | + ), |
| 119 | + key_name = KeyName( |
| 120 | + message.dialog.client.bank_identifier, |
| 121 | + message.dialog.client.user_id, |
| 122 | + KeyType.S, |
| 123 | + 0, |
| 124 | + 0, |
| 125 | + ), |
| 126 | + |
| 127 | + ) |
| 128 | + |
| 129 | + message += self.pending_signature |
| 130 | + |
| 131 | + def sign_commit(self, message: FinTSMessage): |
| 132 | + if not self.pending_signature: |
| 133 | + raise Error("No signature is pending") |
| 134 | + |
| 135 | + if self.pending_signature not in message.segments: |
| 136 | + raise Error("Cannot sign a message that was not prepared") |
| 137 | + |
| 138 | + signature = HNSHA2( |
| 139 | + security_reference = self.pending_signature.security_reference, |
| 140 | + user_defined_signature = UserDefinedSignature( |
| 141 | + pin=self.pin, |
| 142 | + tan=self.tan, |
| 143 | + ), |
| 144 | + ) |
| 145 | + |
| 146 | + self.pending_signature = None |
| 147 | + message += signature |
| 148 | + |
| 149 | + def verify(self, message: FinTSMessage): |
| 150 | + pass |
0 commit comments