iOS-Network-Https

參考文章:
http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html
http://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html
http://www.reibang.com/p/20d5fb4cd76d
雙向驗(yàn)證:
http://blog.csdn.net/codingfire/article/details/53419521
http://m.ithao123.cn/content-10472230.html
OpenSSL:
https://www.openssl.org/docs/man1.0.2/
http://shjia.blog.51cto.com/2476475/1427138

通篇看完覺得一張圖解釋Https很??肃叶。

注意:

  1. 證書和服務(wù)器需要滿足Requirements for Connecting Using ATS的條件牍帚。
  2. 保證1滿足。

NSURLConnection-兩種方式實(shí)現(xiàn) - 客戶端驗(yàn)證服務(wù)器

其他的如NSURLSession 調(diào)用方式和位置不同,原理一樣

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSLog(@"authenticatemethod:%@",challenge.protectionSpace.authenticationMethod);
    BOOL userJustCheckCertificateSame = NO;
    {
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSMutableArray *certificates = [NSMutableArray array];
        NSData*caCert =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"new_cacert" ofType:@"cer"]];
        NSData*serverCert =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"new_server" ofType:@"cer"]];
        if (userJustCheckCertificateSame) {
            [certificates addObject:caCert];
            [certificates addObject:serverCert];
            [self serverTrustjustCheckCertificateSame:challenge pinCertificates:certificates];
        }else{
            SecCertificateRef caCertRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)caCert);
            SecCertificateRef serverCertRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)serverCert);
            [certificates addObject:(__bridge_transfer id)caCertRef];
            [certificates addObject:(__bridge_transfer id)serverCertRef];
            [self serverTrustSystemMethod:challenge pinCertificates:certificates];
        }
            
        }else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
            //暫時不做client 驗(yàn)證
            [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
//            NSData*certData =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"old_client" ofType:@"cer"]];
//            [self addCertToKeychain:certData];
//            NSURLCredential *credential = [self getClientCertFromKeychain];
//            [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
        }else{
            [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
        }
    }
}

// 如果server 返回的證書鏈 里面有一個在local 證書集合里面恋脚,即可認(rèn)為合法
- (void)serverTrustjustCheckCertificateSame:(NSURLAuthenticationChallenge *)challenge pinCertificates:(NSMutableArray *)pinCertificates{
    SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
    if ([self localCaInServerChainList:serverTrust pinCertificates:pinCertificates]) {
        NSURLCredential *cred = [NSURLCredential credentialForTrust:serverTrust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
    }else{
        [challenge.sender cancelAuthenticationChallenge:challenge];
    }
}

- (BOOL)localCaInServerChainList:(SecTrustRef)serverTrust pinCertificates:(NSMutableArray *)pinCertificates{
    CFIndex certificateCount = SecTrustGetCertificateCount(serverTrust);
    for (CFIndex i = 0; i < certificateCount; i++) {
        SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, i);
        NSData *trustChainCertificate = (__bridge_transfer NSData *)SecCertificateCopyData(certificate);
        if ([pinCertificates containsObject:trustChainCertificate]) {
            return YES;
        }
    }
    return NO;
}

// 調(diào)用系統(tǒng)方法
- (void)serverTrustSystemMethod:(NSURLAuthenticationChallenge *)challenge pinCertificates:(NSMutableArray *)pinCertificates{
    //1)獲取trust object
    SecTrustRef trust = challenge.protectionSpace.serverTrust;
    SecTrustResultType result;
    
    NSMutableArray *policies = [NSMutableArray array];
    // BasicX509 不驗(yàn)證域名是否相同(我們用的IP)
    SecPolicyRef policy = SecPolicyCreateBasicX509();
    [policies addObject:(__bridge_transfer id)policy];
    SecTrustSetPolicies(trust, (__bridge CFArrayRef)policies);
    
    //注意:添加自己的證書作為可信列表
    SecTrustSetAnchorCertificates(trust, (__bridge CFArrayRef)pinCertificates);
    
    //禁用系統(tǒng)可信列表
    //SecTrustSetAnchorCertificatesOnly(trust, false);
    
    //2)SecTrustEvaluate會查找前面SecTrustSetAnchorCertificates設(shè)置的證書或者系統(tǒng)默認(rèn)提供的證書,對trust進(jìn)行驗(yàn)證
    OSStatus status = SecTrustEvaluate(trust, &result);
    if (status == errSecSuccess &&
        (result == kSecTrustResultProceed ||
         result == kSecTrustResultUnspecified))
    {
        //3)驗(yàn)證成功,生成NSURLCredential憑證cred,告知challenge的sender使用這個憑證來繼續(xù)連接
        NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
    } else {
        //5)驗(yàn)證失敗婴噩,取消這次驗(yàn)證流程
        [challenge.sender cancelAuthenticationChallenge:challenge];
    }
}

NSURLConnection -服務(wù)器驗(yàn)證客戶端


所有參考鏈接:
http://www.cnblogs.com/qiyer/p/4871421.html
http://m.ithao123.cn/content-10472230.html
http://oncenote.com/2014/10/21/Security-1-HTTPS/
http://oncenote.com/2015/09/16/Security-2-HTTPS2/#verify_safely
http://www.cnblogs.com/interdrp/p/4881116.html
http://www.cnblogs.com/pixy/p/4722381.html
http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html
http://www.reibang.com/p/2927ca2b3719
http://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html
http://www.ruanyifeng.com/blog/2014/02/ssl_tls.html
https://zh.wikipedia.org/wiki/%E8%BF%AA%E8%8F%B2-%E8%B5%AB%E7%88%BE%E6%9B%BC%E5%AF%86%E9%91%B0%E4%BA%A4%E6%8F%9B
http://www.cnblogs.com/oc-bowen/p/5896041.html
http://www.cnblogs.com/jukan/p/5527922.html
http://www.cnblogs.com/guogangj/p/4118605.html
http://blog.csdn.net/linda1000/article/details/8676330
http://blog.sina.com.cn/s/blog_a9303fd90101jmtx.html
https://tools.ietf.org/html/rfc5246#section-7.3
https://developer.apple.com/library/prerelease/content/documentation/Security/Conceptual/CertKeyTrustProgGuide/revisionHistory.html#//apple_ref/doc/uid/TP40001358-CH206-TPXREF101
https://developer.apple.com/library/prerelease/content/technotes/tn2326/_index.html
https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW57
https://developer.apple.com/library/content/documentation/Security/Conceptual/CertKeyTrustProgGuide/revisionHistory/revisionHistory.html#//apple_ref/doc/uid/TP40001358-CH206-TPXREF101
https://developer.apple.com/library/content/technotes/tn2232/_index.html#//apple_ref/doc/uid/DTS40012884-CH1-SECBASICTRUSTCUSTOMIZATION
http://www.reibang.com/p/2542c95fb023
http://www.cnblogs.com/longshiyVip/p/5080917.html
http://www.reibang.com/p/e767a4e9252e
http://www.cnblogs.com/hyddd/archive/2009/01/07/1371292.html
https://developer.apple.com/library/content/qa/qa1727/_index.html
https://developer.apple.com/reference/security

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末擎场,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子几莽,更是在濱河造成了極大的恐慌迅办,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件章蚣,死亡現(xiàn)場離奇詭異礼饱,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)究驴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來匀伏,“玉大人洒忧,你說我怎么就攤上這事」坏撸” “怎么了熙侍?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長履磨。 經(jīng)常有香客問我蛉抓,道長,這世上最難降的妖魔是什么剃诅? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任巷送,我火速辦了婚禮,結(jié)果婚禮上矛辕,老公的妹妹穿的比我還像新娘笑跛。我一直安慰自己,他們只是感情好聊品,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布飞蹂。 她就那樣靜靜地躺著,像睡著了一般翻屈。 火紅的嫁衣襯著肌膚如雪陈哑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天伸眶,我揣著相機(jī)與錄音惊窖,去河邊找鬼。 笑死赚抡,一個胖子當(dāng)著我的面吹牛爬坑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播涂臣,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼盾计,長吁一口氣:“原來是場噩夢啊……” “哼售担!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起署辉,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤族铆,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后哭尝,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哥攘,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年材鹦,在試婚紗的時候發(fā)現(xiàn)自己被綠了逝淹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡桶唐,死狀恐怖栅葡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情尤泽,我是刑警寧澤欣簇,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站坯约,受9級特大地震影響熊咽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜闹丐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一横殴、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧卿拴,春花似錦滥玷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至航徙,卻和暖如春如贷,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背到踏。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工杠袱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人窝稿。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓楣富,卻偏偏與公主長得像,于是被迫代替她去往敵國和親伴榔。 傳聞我的和親對象是個殘疾皇子纹蝴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 10點(diǎn)半睡庄萎,6點(diǎn)半起,2+6.5=8.5小時有核心技術(shù)的人塘安,才能活到最后糠涛。 iOS 閱讀整理在sourcetree...
    士夢閱讀 3,046評論 0 18
  • 前端知識體系http://www.cnblogs.com/sb19871023/p/3894452.html 前端...
    秋風(fēng)喵閱讀 12,412評論 7 163
  • 一層一層把地基搭好,現(xiàn)在終于可以把商品拿出來show了兼犯。顧客買東西忍捡,解決的就是信任和價(jià)值。展示商品就是像顧客...
    上帝愛非洲閱讀 362評論 2 3
  • 面試的時候切黔,是自尊和自傲遭到碾壓和粉碎最徹底的時候砸脊。面試官的一個個問題,尖銳到像一把針纬霞,毫不避諱的刺過你的心臟脓规,刺...
    小沙發(fā)閱讀 377評論 0 0
  • 被游戲帶上分感動 就很開心 沒有說晚安 但也沒差 有一個小尷尬 還好沒在意 確認(rèn)之后是了解 沒關(guān)系的 我先藏著...
    藍(lán)桉_嶼閱讀 220評論 0 0