【问题标题】:Ktor multiplatform - SSL pinning for iOS in kotlinKtor 多平台 - 在 kotlin 中用于 iOS 的 SSL 固定
【发布时间】:2020-03-05 18:04:23
【问题描述】:

我正在使用以下代码在 Kotlin Multiplatform 中使用 Ktor 进行 SSL 固定。

这行代码我崩溃了

val remoteCertificateData : NSData = SecCertificateCopyData(certificate) as NSData 

这里是函数。

override fun URLSession(
            session: NSURLSession,
            didReceiveChallenge: NSURLAuthenticationChallenge,
            completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Unit
        ) {
            val serverTrust = didReceiveChallenge.protectionSpace.serverTrust

            val certificate = SecTrustGetCertificateAtIndex(serverTrust,0)

            var result: SecTrustResultType = 0u
            memScoped{
                val nativeResult = alloc<SecTrustResultTypeVar>()
                nativeResult.value = result
                SecTrustEvaluate(serverTrust!!, nativeResult.ptr)
            }

            val remoteCertificateData : NSData = SecCertificateCopyData(certificate) as NSData                                
            val bundle = NSBundle.bundleForClass(objc_getRequiredClass("IosClientEngine"))                

            val pathToCert = bundle.pathForResource("MyCertificate","cer")                

            val localCertificate : NSData = NSData.dataWithContentsOfFile(pathToCert!!)!!

            if (localCertificate == remoteCertificateData) {
                completionHandler(NSURLSessionAuthChallengeUseCredential,NSURLCredential.create(serverTrust))                    
            } else {
                completionHandler(NSURLSessionAuthChallengeUseCredential, null)                    
            }
        }

【问题讨论】:

    标签: ktor kotlin-multiplatform


    【解决方案1】:

    经过这么多研究,我设法在 Kotlin Multiplatform for iOS 中转换 iOS 代码。

    override fun URLSession(
        session: NSURLSession,
        didReceiveChallenge: NSURLAuthenticationChallenge,
        completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Unit
    ) {
        val serverTrust = didReceiveChallenge.protectionSpace.serverTrust
        var result: SecTrustResultType = 0u
    
        memScoped{
            val nativeResult = alloc<SecTrustResultTypeVar>()
            nativeResult.value = result
            SecTrustEvaluate(serverTrust!!, nativeResult.ptr)
        }
    
        val serverCertificate = SecTrustGetCertificateAtIndex(serverTrust,0)
        val serverCertificateData = SecCertificateCopyData(serverCertificate)
        val data = CFDataGetBytePtr(serverCertificateData)
        val size = CFDataGetLength(serverCertificateData)
    
        val cert1 = NSData.dataWithBytes(data,size.toULong())
        val pathToCert = NSBundle.mainBundle.pathForResource("Your Certificate","cer")
    
        val localCertificate : NSData = NSData.dataWithContentsOfFile(pathToCert!!)!!
    
        if (localCertificate == cert1) {
            completionHandler(NSURLSessionAuthChallengeUseCredential,NSURLCredential.create(serverTrust))
        } else {
            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, null)
        }
    }
    

    【讨论】:

    • 我正在尝试使用 ktor configureSession {} 配置会话委托并覆盖 URLSession 委托以进行证书验证,此 Urlsession 委托未调用。你试过使用 Ktor 1.3
    • 您必须将此代码放在 Kotlin 代码中的 common 共享文件夹中。
    猜你喜欢
    • 2019-12-21
    • 2019-08-12
    • 2020-07-29
    • 2019-11-04
    • 1970-01-01
    • 2021-09-11
    • 2022-07-02
    • 2021-04-10
    • 1970-01-01
    相关资源
    最近更新 更多