~aurelien-dumaine/+junk/ebicspy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
##############################################################################
#
#    ebicspy, EBICS protocol library
#    Copyright (C) 2013-2014 Aurélien DUMAINE (aurelien.dumaine@free.fr).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from Crypto.PublicKey import RSA
import hashlib
from crypto_tools import *
from ini_step import *
from hia_step import *
from hpb_step import *

class Bank :
    def __init__(self, storageService, name, urlHost, urlPort, urlRoot, hostId) :
        self.name = name
        self.urlHost = urlHost
        self.urlPort = urlPort
        self.urlRoot = urlRoot
        self.hostId = hostId
        self.storageService = storageService


    def getHostId (self) :
        return self.hostId

    def getUrlHost (self) :
        return self.urlHost

    def getUrlPort (self) :
        return self.urlPort

    def getUrlRoot (self) :
        return self.urlRoot

    def import_TLSCertificate(self, path):
        #FIXME : check the TLS certificate each time we send/receive data from/to the bank
        print "TODO"

    def setAuthKey (self, authKey) :
        self.authKey = authKey

    def getKeyHash(self, bankKey):
        #The SHA-256 hash values of the financial institution's public keys for X002 and
        #E002 are composed by concatenating the exponent with a blank character and the
        #modulus in hexadecimal representation (using lower case letters) without leading
        #zero (as to the hexadecimal representation). The resulting string has to be
        #converted into a byte array based on US ASCII code

        n_long = getattr(bankKey.key, 'n')
        n_hex_str = str(hex(n_long))[2:-1]

        e_long = getattr(bankKey.key, 'e')
        e_hex_str = str(hex(e_long))[2:-1]

        s = e_hex_str + ' ' + n_hex_str
        if s[0] == '0':
            s = s[1:]

        m = hashlib.sha256()
        m.update(s)
        string_digest = m.digest()

        res = string_digest.encode('base64')
        return res.strip()
        
    def saveKeys(self, auth_certificate, auth_modulus, auth_exponent, auth_version, encrypt_certificate, encrypt_modulus, encrypt_exponent, encrypt_version):
        self.storageService.saveBankKey("auth", auth_version, auth_modulus, auth_exponent, auth_certificate)
        self.storageService.saveBankKey("encrypt", encrypt_version, encrypt_modulus, encrypt_exponent, encrypt_certificate)

    def getAuthKey (self) :
        return self.authKey

    def setSignKey (self, signKey) :
        self.signKey = signKey

    def getSignKey (self) :
        return self.signKey

    def setEncryptKey (self, encryptKey) :
        self.encryptKey = encryptKey

    def getEncryptKey (self) :
        return self.encryptKey

    def getName (self) :
        return self.name

class Partner : 
    def __init__(self, storageService, name, partnerId, userId, fileExchangedLogger, ebicsProfile="T"):
        self.name = name
        self.partnerId = partnerId
        self.userId = userId
        self.ebicsProfile = ebicsProfile
        self.fileExchangedLogger = fileExchangedLogger
        self.storageService = storageService

    def handle_ini_exchange(self, bank):
        ini_exchange(self, bank)
        #TODO check the validity of the response
        ini_sign_letter = letter("INI",
            "Certificat pour la signature électronique (A005)" , 
            self.getSignCertificate(),
            bank.getHostId(), 
            self.getUserId(), 
            self.getPartnerId())
        self.storageService.saveLetter(ini_sign_letter, "ini_letter_sign")

    def handle_hia_exchange(self, bank):
        hia_exchange(self, bank)
        #TODO check the validity of the response
        hia_encrypt_letter = letter("HIA",
            "Certificat pour le chiffrement (E002)" , 
            self.getEncryptCertificate(),
            bank.getHostId(), 
            self.getUserId(), 
            self.getPartnerId())
        self.storageService.saveLetter(hia_encrypt_letter, "hia_letter_encrypt")
        hia_auth_letter = letter("HIA",
            "Certificat pour l'authentification (X002)" , 
            self.getAuthCertificate(),
            bank.getHostId(), 
            self.getUserId(), 
            self.getPartnerId())
        self.storageService.saveLetter(hia_auth_letter, "hia_letter_auth")
        #TODO display the ini letter file path
    

    def init(self, bank):
        status = self.storageService.getStatus()

        if satus == "partner_init":
            self.createPartnerKeys()
            print "========== PARTNER KEY GENERATION OK =========="        
            self.handle_ini_exchange(bank)
            print "========== INI MESSAGE SENT =========="
            print "========== WE HAVE NOW TO SEND THE HIA MESSAGE =========="
            self.handle_hia_exchange(bank)
            print "========== HIA MESSAGE SENT =========="
            print "===>>> YOU HAVE TO SEND INITIATION LETTERS TO YOUR BANK BEFORE DOWNLOADING THE BANK KEYS"
            
        else :
            self.loadPartnerKeys()
            print "========== PARTNER KEYS AND CERTIFICATES LOADED =========="

            if satus == "bank_init":
                bank_auth_key_hash = self.storageService.getBankAuthKeyHash()
                bank_encrypt_key_hash = self.storageService.getBankEncryptKeyHash()
                hpb_exchange(self, bank, bank_auth_key_hash, bank_encrypt_key_hash)

            else :
                self.loadBankKeys(bank)
                print "========== BANK KEYS AND CERTIFICATES LOADED =========="

    def getEbicsProfile (self) :
        return self.ebicsProfile

    def getFileExchangedLogger (self) :
        return self.fileExchangedLogger
        
    def getName (self) :
        return self.name

    def getPartnerId (self) :
        return self.partnerId

    def getUserId (self) :
        return self.userId

    def setAuthKey (self, private, public) :
        self.authPrivateKey= private
        self.authPublicKey = public

    def getAuthPrivateKey (self) :
        return self.authPrivateKey

    def getAuthPublicKey (self) :
        return self.authPublicKey

    def setSignKey (self, private, public) :
        self.signPrivateKey = private
        self.signPublicKey = public

    def getSignPrivateKey (self) :
        return self.signPrivateKey

    def getSignPublicKey (self) :
        return self.signPublicKey

    def setEncryptKey (self, private, public) :
        self.encryptPrivateKey = private
        self.encryptPublicKey = public

    def getEncryptPrivateKey (self) :
        return self.encryptPrivateKey

    def getEncryptPublicKey (self) :
        return self.encryptPublicKey

    #FIXME : the two followinig aliases are used for cryptographique roudTest purpose only
    def getAuthKey (self) :
        return self.authPublicKey
    def getEncryptKey (self) :
        return self.encryptPublicKey
    def getKeyHash(self, bankKey):
        return "FEA76AAE939D0CC0B82657C10D3E253E"
    def getHostId(self):
        return "EBIXQAL"

    def setSignCertificate (self, certificate):
        self.signCertificate = certificate

    def getSignCertificate (self):
        return self.signCertificate

    def setAuthCertificate (self, certificate):
        self.authCertificate = certificate

    def getAuthCertificate (self):
        return self.authCertificate

    def setEncryptCertificate (self, certificate):
        self.encryptCertificate = certificate

    def getEncryptCertificate (self):
        return self.encryptCertificate

    def createPartnerKeys(self):
        auth_private, auth_public, auth_cert = create_rsa_key(AUTH_KEY_LENGTH, "auth", self.storageService)
        if self.ebicsProfile == "T" :
            sign_private, sign_public, sign_cert = create_rsa_key(SIGN_KEY_LENGTH, "sign", self.storageService) 
        encrypt_private, encrypt_public, encrypt_cert = create_rsa_key(ENCRYPT_KEY_LENGTH, "encrypt", self.storageService) 
        self.loadPartnerKeys()

    def loadPartnerKeys(self):
        auth_modulus = self.storageService.getPartnerKeyComponent("modulus", "auth")
        auth_public_exponent = self.storageService.getPartnerKeyComponent("public_exponent", "auth")
        auth_private_exponent = self.storageService.getPartnerKeyComponent("private_exponent", "auth")
        self.setAuthKey(RSA.construct((auth_modulus, auth_public_exponent, auth_private_exponent)), RSA.construct((auth_modulus, auth_public_exponent)))

        sign_modulus = self.storageService.getPartnerKeyComponent("modulus", "sign")
        sign_public_exponent = self.storageService.getPartnerKeyComponent("public_exponent", "sign")
        sign_private_exponent = self.storageService.getPartnerKeyComponent("private_exponent", "sign")
        self.setSignKey(RSA.construct((sign_modulus, sign_public_exponent, sign_private_exponent)), RSA.construct((sign_modulus, sign_public_exponent)))
            
        encrypt_modulus = self.storageService.getPartnerKeyComponent("modulus", "encrypt")
        encrypt_public_exponent = self.storageService.getPartnerKeyComponent("public_exponent", "encrypt")
        encrypt_private_exponent = self.storageService.getPartnerKeyComponent("private_exponent", "encrypt")
        self.setEncryptKey(RSA.construct((encrypt_modulus, encrypt_public_exponent, encrypt_private_exponent)), RSA.construct((encrypt_modulus, encrypt_public_exponent)))

        sign_cert = self.storageService.getPartnerCertificate('sign')
        self.setSignCertificate(sign_cert)
        auth_cert = self.storageService.getPartnerCertificate('auth')
        self.setAuthCertificate(auth_cert)
        encrypt_cert = self.storageService.getPartnerCertificate('encrypt')
        self.setEncryptCertificate(encrypt_cert)


    def loadBankKeys(self, bank):
        auth_modulus = self.storageService.getBankKeyComponent("modulus", "auth")
        auth_exponent = self.storageService.getBankKeyComponent("public_exponent", "auth")
        bank.setAuthKey(RSA.construct((auth_modulus, auth_exponent)))
        encrypt_modulus = self.storageService.getBankKeyComponent("modulus", "encrypt")
        encrypt_exponent = self.storageService.getBankKeyComponent("public_exponent", "encrypt")                
        bank.setEncryptKey(RSA.construct((encrypt_modulus, encrypt_exponent)))


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4