【发布时间】:2015-11-30 09:09:27
【问题描述】:
我正在尝试使用 Web 服务开发人员提供给我的公钥来安全地传输数据。我已经很努力了,但我找不到任何使用最新 swift 版本的工作示例。我的编码如下(在here的帮助下:
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
var localTrust: UnsafeMutablePointer<SecTrust?>
let serverTrust = challenge.protectionSpace.serverTrust!
let serverPublicKey = SecTrustCopyPublicKey(serverTrust)
let certificateData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("pinning-certificate", ofType: "der")!)
let localCertificate = SecCertificateCreateWithData(kCFAllocatorDefault, certificateData!)
let policy = SecPolicyCreateBasicX509()
if SecTrustCreateWithCertificates(localCertificate!, policy, localTrust) == errSecSuccess {
let localTrustRef = localTrust
let localPublicKey = SecTrustCopyPublicKey(localTrustRef)!
if (localPublicKey as AnyObject).isEqual(serverPublicKey as! AnyObject) {
print("trusted")
return challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
}
}
print("not trusted")
return challenge.sender!.cancelAuthenticationChallenge(challenge)
}
但是在 SecTrustCreateWithCertificates() 和 SecTrustCopyPublicKey() 中编译期间出现以下错误: 无法转换“UnsafeMutablePointer”类型的值?到预期的参数类型“UnsafeMutablePointer”(又名“UnsafeMutablePointer>”)。
编辑 1: 我已将代码更改为以下,现在我的代码已执行,但服务器未收到任何请求:
func extractIdentity(certData:NSData) -> IdentityAndTrust {
var identityAndTrust:IdentityAndTrust!
var securityError:OSStatus = errSecSuccess
let path: String = NSBundle.mainBundle().pathForResource("MobileAppClient", ofType: "pfx")!
let PKCS12Data = NSData(contentsOfFile:path)!
let key : NSString = kSecImportExportPassphrase as NSString
let options : NSDictionary = [key : "client"]
//create variable for holding security information
//var privateKeyRef: SecKeyRef? = nil
var items : CFArray?
securityError = SecPKCS12Import(PKCS12Data, options, &items)
if securityError == errSecSuccess {
let certItems:CFArray = items as CFArray!;
let certItemsArray:Array = certItems as Array
let dict:AnyObject? = certItemsArray.first;
if let certEntry:Dictionary = dict as? Dictionary<String, AnyObject> {
// grab the identity
let identityPointer:AnyObject? = certEntry["identity"];
let secIdentityRef:SecIdentityRef = identityPointer as! SecIdentityRef!;
print("\(identityPointer) :::: \(secIdentityRef)")
// grab the trust
let trustPointer:AnyObject? = certEntry["trust"];
let trustRef:SecTrustRef = trustPointer as! SecTrustRef;
print("\(trustPointer) :::: \(trustRef)")
// grab the cert
let chainPointer:AnyObject? = certEntry["chain"];
identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certArray: chainPointer! as! NSArray);
}
}
return identityAndTrust;
}
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
if challenge.protectionSpace.authenticationMethod == (NSURLAuthenticationMethodServerTrust) {
let serverTrust:SecTrustRef = challenge.protectionSpace.serverTrust!
let certificate: SecCertificateRef = SecTrustGetCertificateAtIndex(serverTrust, 0)!
let remoteCertificateData = CFBridgingRetain(SecCertificateCopyData(certificate))!
let cerPath: String = NSBundle.mainBundle().pathForResource("Certificates", ofType: "cer")!
let localCertificateData = NSData(contentsOfFile:cerPath)!
challenge.sender?.useCredential(credential, forAuthenticationChallenge: challenge)
if (remoteCertificateData.isEqualToData(localCertificateData) == true) {
let credential:NSURLCredential = NSURLCredential(forTrust: serverTrust)
challenge.sender?.useCredential(credential, forAuthenticationChallenge: challenge)
} else {
SVProgressHUD.dismiss()
return challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
}
}
else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate
{
let path: String = NSBundle.mainBundle().pathForResource("Certificates", ofType: "p12")!
let PKCS12Data = NSData(contentsOfFile:path)!
let identityAndTrust:IdentityAndTrust = self.extractIdentity(PKCS12Data);
let urlCredential:NSURLCredential = NSURLCredential(
identity: identityAndTrust.identityRef,
certificates: identityAndTrust.certArray as? [AnyObject],
persistence: NSURLCredentialPersistence.ForSession);
challenge.sender?.useCredential(urlCredential, forAuthenticationChallenge: challenge)
}
}
【问题讨论】:
标签: ios swift ssl-certificate public-key-encryption