From c062ed8f606d3c2e7a9c5cde50182fd178105e64 Mon Sep 17 00:00:00 2001 From: Bernard Normier Date: Wed, 27 Nov 2024 15:04:05 -0500 Subject: [PATCH] Update SSLConnectionInfo --- swift/src/Ice/Connection.swift | 17 +---------------- swift/src/Ice/ConnectionInfoFactory.swift | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/swift/src/Ice/Connection.swift b/swift/src/Ice/Connection.swift index a8e05cf2e75..0970b9539e2 100644 --- a/swift/src/Ice/Connection.swift +++ b/swift/src/Ice/Connection.swift @@ -237,22 +237,7 @@ public final class SSLConnectionInfo: ConnectionInfo { /// The certificate chain. public let peerCertificate: SecCertificate? - internal init(underlying: ConnectionInfo, peerCertificate: String) { - let beginPrefix = "-----BEGIN CERTIFICATE-----\n" - let endPrefix = "\n-----END CERTIFICATE-----\n" - - var raw = peerCertificate - if raw.hasPrefix(beginPrefix) { - raw = String(raw.dropFirst(beginPrefix.count)) - raw = String(raw.dropLast(endPrefix.count)) - } - - var peerCertificate: SecCertificate? = nil - if let data = NSData(base64Encoded: raw, options: .ignoreUnknownCharacters) { - if let cert = SecCertificateCreateWithData(kCFAllocatorDefault, data) { - peerCertificate = cert - } - } + internal init(underlying: ConnectionInfo, peerCertificate: SecCertificate?) { self.peerCertificate = peerCertificate super.init(underlying: underlying) } diff --git a/swift/src/Ice/ConnectionInfoFactory.swift b/swift/src/Ice/ConnectionInfoFactory.swift index 5d52ac5f890..67d18a62886 100644 --- a/swift/src/Ice/ConnectionInfoFactory.swift +++ b/swift/src/Ice/ConnectionInfoFactory.swift @@ -59,7 +59,23 @@ class ConnectionInfoFactory: ICEConnectionInfoFactory { } static func createSSLConnectionInfo(_ underlying: Any, peerCertificate: String) -> Any { - SSLConnectionInfo(underlying: underlying as! ConnectionInfo, peerCertificate: peerCertificate) + let beginPrefix = "-----BEGIN CERTIFICATE-----\n" + let endPrefix = "\n-----END CERTIFICATE-----\n" + + var raw = peerCertificate + if raw.hasPrefix(beginPrefix) { + raw = String(raw.dropFirst(beginPrefix.count)) + raw = String(raw.dropLast(endPrefix.count)) + } + + var decodedPeerCertificate: SecCertificate? = nil + if let data = NSData(base64Encoded: raw, options: .ignoreUnknownCharacters) { + if let cert = SecCertificateCreateWithData(kCFAllocatorDefault, data) { + decodedPeerCertificate = cert + } + } + + return SSLConnectionInfo(underlying: underlying as! ConnectionInfo, peerCertificate: decodedPeerCertificate) } static func createIAPConnectionInfo(