Skip to content

Commit 11a2f03

Browse files
committed
guess_wire_crypt()
1 parent a5ec194 commit 11a2f03

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

firebirdsql/aio/fbcore.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -652,27 +652,29 @@ async def _async_parse_connect_response(self):
652652
b''
653653
)
654654
(h, oid, buf) = await self._async_op_response()
655-
guessed_wire_crypt = self._guess_wire_crypt(buf)
655+
enc_plugin, nonce = guess_wire_crypt(buf)
656656
else:
657-
guessed_wire_crypt = None
657+
enc_plugin = nonce = None
658658

659-
if guessed_wire_crypt and self.wire_crypt and session_key:
660-
self._op_crypt(guessed_wire_crypt[0])
661-
if guessed_wire_crypt[0] == b'Arc4':
659+
if enc_plugin and self.wire_crypt and session_key:
660+
self._op_crypt(enc_plugin)
661+
if enc_plugin == b'Arc4':
662662
self.sock.set_translator(
663663
ARC4.new(session_key), ARC4.new(session_key))
664-
elif guessed_wire_crypt[0] == b'ChaCha':
664+
elif enc_plugin == b'ChaCha':
665665
k = hashlib.sha256(session_key).digest()
666666
self.sock.set_translator(
667-
ChaCha20.new(k, guessed_wire_crypt[1]),
668-
ChaCha20.new(k, guessed_wire_crypt[1]),
667+
ChaCha20.new(k, nonce),
668+
ChaCha20.new(k, nonce),
669669
)
670670
else:
671671
raise OperationalError(
672-
'Unknown wirecrypt plugin %s' % (guessed_wire_crypt)
672+
'Unknown wirecrypt plugin %s' % (enc_plugin)
673673
)
674-
(h, oid, buf) = await self._async_op_response()
675-
else: # use later _op_attach() and _op_create()
674+
(h, oid, buf) = self._op_response()
675+
else:
676+
# no matched wire encription plugin
677+
# self.auth_data use _op_attach() and _op_create()
676678
self.auth_data = auth_data
677679
else:
678680
assert op_code == self.op_accept

firebirdsql/fbcore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ def _parse_connect_response(self):
755755
b''
756756
)
757757
(h, oid, buf) = self._op_response()
758-
enc_plugin, nonce = self._guess_wire_crypt(buf)
758+
enc_plugin, nonce = guess_wire_crypt(buf)
759759
else:
760760
enc_plugin = nonce = None
761761

firebirdsql/utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,37 @@ def parse_dsn(dsn, host=None, port=None, database=None, user=None, password=None
167167
return host, port, database, user, password
168168

169169

170+
def guess_wire_crypt(b):
171+
available_plugins = []
172+
plugin_nonce = []
173+
i = 0
174+
while i < len(b):
175+
t = b[i] if PYTHON_MAJOR_VER == 3 else ord(b[i])
176+
i += 1
177+
ln = b[i] if PYTHON_MAJOR_VER == 3 else ord(b[i])
178+
i += 1
179+
v = b[i:i+ln]
180+
i += ln
181+
if t == 0:
182+
assert v == b"Symmetric"
183+
elif t == 1:
184+
available_plugins = v.split()
185+
elif t == 3:
186+
plugin_nonce.append(v)
187+
188+
# if b'ChaCha64' in available_plugins:
189+
# for s in plugin_nonce:
190+
# if s[:9] == b"ChaCha64\x00":
191+
# return (b'ChaCha64', s[9:])
192+
if b'ChaCha' in available_plugins:
193+
for s in plugin_nonce:
194+
if s[:7] == b"ChaCha\x00":
195+
return (b'ChaCha', s[7:7 + 12])
196+
elif b'Arc4' in available_plugins:
197+
return (b'Arc4', None)
198+
return None, None
199+
200+
170201
class RowMapping(Mapping):
171202
"""dict like interface to result rows
172203
"""

firebirdsql/wireprotocol.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -465,36 +465,6 @@ def _op_crypt(self, algo):
465465
p.pack_bytes(b'Symmetric')
466466
self.sock.send(p.get_buffer())
467467

468-
def _guess_wire_crypt(self, b):
469-
available_plugins = []
470-
plugin_nonce = []
471-
i = 0
472-
while i < len(b):
473-
t = b[i] if PYTHON_MAJOR_VER == 3 else ord(b[i])
474-
i += 1
475-
ln = b[i] if PYTHON_MAJOR_VER == 3 else ord(b[i])
476-
i += 1
477-
v = b[i:i+ln]
478-
i += ln
479-
if t == 0:
480-
assert v == b"Symmetric"
481-
elif t == 1:
482-
available_plugins = v.split()
483-
elif t == 3:
484-
plugin_nonce.append(v)
485-
486-
# if b'ChaCha64' in available_plugins:
487-
# for s in plugin_nonce:
488-
# if s[:9] == b"ChaCha64\x00":
489-
# return (b'ChaCha64', s[9:])
490-
if b'ChaCha' in available_plugins:
491-
for s in plugin_nonce:
492-
if s[:7] == b"ChaCha\x00":
493-
return (b'ChaCha', s[7:7 + 12])
494-
elif b'Arc4' in available_plugins:
495-
return (b'Arc4', None)
496-
return None, None
497-
498468
@wire_operation
499469
def _op_attach(self, timezone):
500470
dpb = bs([isc_dpb_version1])

0 commit comments

Comments
 (0)