From 69fa111be9dbc3ac1e686fd15893eaa32df086ae Mon Sep 17 00:00:00 2001 From: Abe Date: Tue, 21 Nov 2023 22:13:50 +0100 Subject: [PATCH 1/4] Replaced deprecated SecKeyEncrypt with new SecKeyCreateEncryptedData. --- Sources/MySQLDriver/Utils.swift | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Sources/MySQLDriver/Utils.swift b/Sources/MySQLDriver/Utils.swift index eae3118..41eb86f 100644 --- a/Sources/MySQLDriver/Utils.swift +++ b/Sources/MySQLDriver/Utils.swift @@ -358,15 +358,10 @@ extension MySQL { [kSecAttrKeyType: kSecAttrKeyTypeRSA, kSecAttrKeyClass: kSecAttrKeyClassPublic] as CFDictionary, nil) - let key_size = SecKeyGetBlockSize(publickeysi!) - - var encrypt_bytes = [UInt8](repeating: 0, count: key_size) - - var output_size : Int = key_size - - SecKeyEncrypt(publickeysi!, SecPadding.OAEP, data, data.count, &encrypt_bytes, &output_size) - - return encrypt_bytes; + var unmanagedError: Unmanaged? + let encrypt_data = SecKeyCreateEncryptedData(publickeysi!, .rsaEncryptionOAEPSHA1AESGCM, Data(data) as CFData, &unmanagedError)! as Data + + return Array(encrypt_data) } return []; } From 9ba7ed531752466f5aaed656456b15f827006391 Mon Sep 17 00:00:00 2001 From: Abe Date: Tue, 4 Feb 2025 20:00:15 +0100 Subject: [PATCH 2/4] mysql server fix --- Sources/MySQLDriver/Connection.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/MySQLDriver/Connection.swift b/Sources/MySQLDriver/Connection.swift index a743cfb..ab98d4b 100644 --- a/Sources/MySQLDriver/Connection.swift +++ b/Sources/MySQLDriver/Connection.swift @@ -80,8 +80,10 @@ public extension MySQL.Connection { var pos = 0 msh.proto_version = data[pos] pos += 1 - msh.server_version = data[pos.. Date: Fri, 21 Mar 2025 19:58:18 +0100 Subject: [PATCH 3/4] fix release crashes - bad memomery access - auth fixes. Better socket error message with no data. --- Sources/MySQLDriver/Connection.swift | 31 ++++++++++++---------------- Sources/MySQLDriver/Socket.swift | 15 +++++++++++--- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Sources/MySQLDriver/Connection.swift b/Sources/MySQLDriver/Connection.swift index ab98d4b..c7766a8 100644 --- a/Sources/MySQLDriver/Connection.swift +++ b/Sources/MySQLDriver/Connection.swift @@ -80,21 +80,20 @@ public extension MySQL.Connection { var pos = 0 msh.proto_version = data[pos] pos += 1 - let version_end_index = data[pos...].firstIndex(of: 0)! + 1 - msh.server_version = data[pos.. MySQL.mysql_auth_switch { var msh = MySQL.mysql_auth_switch() - + if let data = try socket?.readPacket() { var pos = 0; msh.status = data[pos] @@ -166,7 +165,6 @@ public extension MySQL.Connection { } return msh; } - private func readAuthResponse() throws { if let data = try socket?.readPacket() { @@ -181,7 +179,7 @@ public extension MySQL.Connection { } private func requestServerKey() throws { - var arr:[UInt8] = [2]; + let arr:[UInt8] = [2]; try socket?.writePacket(arr); try readServerKey(); @@ -194,7 +192,7 @@ public extension MySQL.Connection { let serverPublicKey = Array(data[1..[Field]? { self.columns = [Field](repeating:Field(), count: count) diff --git a/Sources/MySQLDriver/Socket.swift b/Sources/MySQLDriver/Socket.swift index cafe856..19d9e9a 100644 --- a/Sources/MySQLDriver/Socket.swift +++ b/Sources/MySQLDriver/Socket.swift @@ -139,16 +139,25 @@ open class Socket { return String(cString:UnsafePointer(strerror(errno))) //?? "Error: \(errno)" } - func readNUInt8(_ n:UInt32) throws -> [UInt8] { + func readNUInt8(_ n: UInt32) throws -> [UInt8] { var buffer = [UInt8](repeating: 0, count: Int(n)) var read = 0 while read < Int(n) { - read += recv(s, &buffer[read], Int(n) - read, 0) + let result = recv(s, &buffer[read], Int(n) - read, 0) - if read <= 0 { + if result < 0 { throw SocketError.recvFailed(Socket.descriptionOfLastError()) + } else if result == 0 { + // Connection closed by peer, handle gracefully + if read == 0 { + throw SocketError.recvFailed("Connection closed by peer with no data read") + } else { + break // Exit the loop if some data has been read + } } + + read += result } if bytesToRead >= UInt32(n) { From 9fec6672731556822e0f06cb7bb19c561d919570 Mon Sep 17 00:00:00 2001 From: QuARaC Date: Wed, 11 Jun 2025 10:22:09 +0200 Subject: [PATCH 4/4] fixed string reading, cleaned unused code --- Sources/MySQLDriver/Connection.swift | 2 +- Sources/MySQLDriver/Socket.swift | 31 +--------------------------- Sources/MySQLDriver/Utils.swift | 21 +++++++++++++++---- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/Sources/MySQLDriver/Connection.swift b/Sources/MySQLDriver/Connection.swift index c7766a8..ea5ed70 100644 --- a/Sources/MySQLDriver/Connection.swift +++ b/Sources/MySQLDriver/Connection.swift @@ -265,7 +265,7 @@ public extension MySQL.Connection { arr.append(0) try socket?.writePacket(arr) - + if mysql_Handshake?.auth_plugin == "mysql_native_password" { // don't do auth switch } diff --git a/Sources/MySQLDriver/Socket.swift b/Sources/MySQLDriver/Socket.swift index 19d9e9a..e70c573 100644 --- a/Sources/MySQLDriver/Socket.swift +++ b/Sources/MySQLDriver/Socket.swift @@ -34,7 +34,6 @@ extension Socket { open class Socket { let s : Int32 - var bytesToRead : UInt32 var packnr : Int var socketInUse = false var addr : sockaddr_in? @@ -44,7 +43,6 @@ open class Socket { init(host : String, port : Int) throws { // create socket to MySQL Server - bytesToRead = 0 packnr = 0 #if os(Linux) s = socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0) @@ -160,33 +158,25 @@ open class Socket { read += result } - if bytesToRead >= UInt32(n) { - bytesToRead -= UInt32(n) - } - return buffer } func readHeader() throws -> (UInt32, Int) { let b = try readNUInt8(3).uInt24() - let pn = try readNUInt8(1)[0] - bytesToRead = b - return (b, Int(pn)) } func readPacket() throws -> [UInt8] { let (len, pknr) = try readHeader() - bytesToRead = len self.packnr = pknr return try readNUInt8(len) } func writePacket(_ data:[UInt8]) throws { try writeHeader(UInt32(data.count), pn: UInt8(self.packnr + 1)) - try writeBuffer(data) + try writeBuffer(data) } func writeBuffer(_ buffer:[UInt8]) throws { @@ -201,7 +191,6 @@ open class Socket { #endif if s <= 0 { throw SocketError.writeFailed(Socket.descriptionOfLastError()) - } sent += s } @@ -222,22 +211,4 @@ open class Socket { return isLittleEndian ? _OSSwapInt16(port) : port #endif } - - /* - func lockSocket() { - while socketInUse { - - } - socketInUse = true - } - - func unlockSocket() { - socketInUse = false - } - - func socketLocked() -> Bool { - return socketInUse - } - */ - } diff --git a/Sources/MySQLDriver/Utils.swift b/Sources/MySQLDriver/Utils.swift index 41eb86f..75844ae 100644 --- a/Sources/MySQLDriver/Utils.swift +++ b/Sources/MySQLDriver/Utils.swift @@ -768,12 +768,25 @@ extension Sequence where Iterator.Element == UInt8 { let arr = self.map { (elem) -> UInt8 in return elem } - - guard (arr.count > 0) && (arr[arr.count-1] == 0) else { - return "" + + // Verificar que el array no esté vacío + guard !arr.isEmpty else { + return nil } - return String(cString: UnsafePointer(arr)) + // Verificar que termine en null terminator + guard arr.last == 0 else { + // Si no termina en null, convertir directamente + return String(bytes: self, encoding: .utf8) + } + + // Crear string de forma segura + return arr.withUnsafeBufferPointer { bufferPointer in + guard let baseAddress = bufferPointer.baseAddress else { + return nil + } + return String(cString: baseAddress) + } } static func UInt24Array(_ val: UInt32) -> [UInt8]{