2016-11-16 16 views
4

私は速い3の私のサーバーと話す必要があるアプリを書いています。私はCAのための完全な証明連鎖をderとcrt形式で持っています(自己署名と混同しないでください)。私のアプリを使ってサーバを検証するにはどうすればいいですか?Swift 3 SSLを使用してサーバー証明書を検証する方法PinningとAlamoFire?

var request = URLRequest(url: URL(string: "https://myserver/login")!) 
    request.addValue("Content-Type", forHTTPHeaderField: "application/json") 
    request.httpMethod = "GET" 
    let session = URLSession.shared 

    session.dataTask(with: request) {data, response, err in   
     print("=========================DATA===============================") 
     if data != nil { 
      print(data!) 
     } 
     print("=========================RESPONSE===============================") 
     if response != nil { 
      print(response!) 
     } 
     print("=========================ERR===============================") 
     if err != nil { 
      print(err!) 
     } 
     }.resume() 

出力::私はそれはかなり簡単にオンラインブログを活用して解決

=========================DATA=============================== 
=========================RESPONSE=============================== 
=========================ERR=============================== 
Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo={NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x60800011f020>, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, NSErrorPeerCertificateChainKey=(
"<cert(0x7fae4803d200) s: myserver i: MySubCA>", 
"<cert(0x7fae48047000) s: MySubCA i: MyRootCA>", 
"<cert(0x7fae48044600) s: MyRootCA i: MyRootCA>" 
), NSUnderlyingError=0x60800005a040 {Error Domain=kCFErrorDomainCFNetwork Code=-1200 "(null)" UserInfo={_kCFStreamPropertySSLClientCertificateState=0, kCFStreamPropertySSLPeerTrust=<SecTrustRef: 0x60800011f020>, _kCFNetworkCFStreamSSLErrorOriginalValue=-9802, _kCFStreamErrorDomainKey=3, _kCFStreamErrorCodeKey=-9802, kCFStreamPropertySSLPeerCertificates=(
"<cert(0x7fae4803d200) s: myserver i: MySubCA>", 
"<cert(0x7fae48047000) s: MySubCA i: MyRootCA>", 
"<cert(0x7fae48044600) s: MyRootCA i: MyRootCA>" 
)}}, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSErrorFailingURLKey=https://myserver/login, NSErrorFailingURLStringKey=https://myserver/login, NSErrorClientCertificateStateKey=0} 

答えて

7

、AlamoFireとOpenSSL

は、私が使用しAlamoFire以下は私のREST呼び出しと応答が

休憩コールですiOSのネットワーキングの場合

https://github.com/Alamofire/Alamofire 

私は形式に

https://help.netmail.com/display/KB/Converting+SSL+Certificate+File+from+CRT+TO+DER+TO+PEM 

デア変換

openssl x509 -in cert.crt -out cert.der -outform DER 
をDERために私の証明書を変換するためにOpenSSLを使用し、右方向に

https://infinum.co/the-capsized-eight/articles/how-to-make-your-ios-apps-more-secure-with-ssl-pinning 

を取得するにはiOSの中で、ピン止めSSLについての記事を使用

あなたのアプリケーションバンドルにder形式の証明書を追加する必要があります。

スウィフト3実施

// Your hostname and endpoint 
let hostname = "YOUR_HOST_NAME" 
let endpoint = "YOUR_ENDPOINT" 
let cert = "YOUR_CERT" // e.g. for cert.der, this should just be "cert" 

// Set up certificates 
let pathToCert = Bundle.main.path(forResource: cert, ofType: "der") 
let localCertificate = NSData(contentsOfFile: pathToCert!) 
let certificates = [SecCertificateCreateWithData(nil, localCertificate!)!] 

// Configure the trust policy manager 
let serverTrustPolicy = ServerTrustPolicy.pinCertificates(
    certificates: certificates, 
    validateCertificateChain: true, 
    validateHost: true 
)  
let serverTrustPolicies = [hostname: serverTrustPolicy] 
let serverTrustPolicyManager = ServerTrustPolicyManager(policies: serverTrustPolicies) 

// Configure session manager with trust policy 
afManager = SessionManager(
    configuration: URLSessionConfiguration.default, 
    serverTrustPolicyManager: serverTrustPolicyManager 
) 


afManager.request(endpoint, method: .get).responseJSON { response in 
    debugPrint("All Response Info: \(response)") 
} 
+0

は素敵な説明しました。楽しかった! –

+0

.der証明書を取得する方法swiftでdateの後に有効ではありません。 – Shahrukh

+0

私はリンクも好きです。素敵な共有。すべての情報が利用可能です。私はこれを共有の精神の中で拡大したいと考えています。 – nyxee

関連する問題