iOS網(wǎng)絡(luò)安全現(xiàn)在越來(lái)越重要崎苗,一個(gè)抓包工具(charles等)隨隨便便就能抓到你所請(qǐng)求的數(shù)據(jù)辩撑,這些數(shù)據(jù)如果是明碼的后果很嚴(yán)重,不是指明文窗市,可以通過(guò)這些數(shù)據(jù)來(lái)判定服務(wù)端部署的數(shù)據(jù)接口,更能夠嗅探到服務(wù)端的漏洞胳螟。
所以最近因公司業(yè)務(wù)及數(shù)據(jù)安全上的需要準(zhǔn)備使用https方式進(jìn)行數(shù)據(jù)請(qǐng)求枝笨。而且蘋果現(xiàn)在默認(rèn)是https方式請(qǐng)求层宫。
Apple CSR CER P12 mobileprovition 到底是什么
- CSR(Certificate Signing Request)鑰匙串文件 為生成證書做基礎(chǔ),要生成CER證書必須要有CSR私鑰爽航,此私鑰包含了用戶自己的一些信息蚓让。這個(gè)文件是保存在我們的mac的(keychain)里面的, 此文件包含了(公鑰和私鑰)
- CER 包含了開發(fā)者信息和公鑰
- P12 它不僅包含CER的信息,還有私鑰信息讥珍,即: P12備份文件 = CER文件 + 私鑰历极;所以有了這個(gè)p12就再也不用擔(dān)心證書丟失了
- mobileprovition 包含了上述所有內(nèi)容 Certificate && App ID && Device, 這個(gè)Provisioning Profile文件會(huì)在打包時(shí)嵌入到.ipa的包里。本人理解的是 可以真機(jī)調(diào)試的憑證衷佃。
https原理
HTTPS(全稱:Hyper Text Transfer Protocol over Secure Socket Layer)趟卸,是以安全為目標(biāo)的HTTP通道,簡(jiǎn)單講是HTTP的安全版氏义。即HTTP下加入SSL層锄列,HTTPS的安全基礎(chǔ)是SSL,因此加密的詳細(xì)內(nèi)容就需要SSL惯悠。 它是一個(gè)URI scheme(抽象標(biāo)識(shí)符體系)邻邮,句法類同http:體系。用于安全的HTTP數(shù)據(jù)傳輸克婶。https:URL表明它使用了HTTP筒严,但HTTPS存在不同于HTTP的默認(rèn)端口及一個(gè)加密/身份驗(yàn)證層(在HTTP與TCP之間)。這個(gè)系統(tǒng)的最初研發(fā)由網(wǎng)景公司(Netscape)進(jìn)行情萤,并內(nèi)置于其瀏覽器Netscape Navigator中鸭蛙,提供了身份驗(yàn)證與加密通訊方法。(摘自百度百科)
PKI(公鑰基礎(chǔ)設(shè)施)技術(shù)是HTTPS的基礎(chǔ)筋岛,PKI與非對(duì)稱密鑰加密技術(shù)密切相關(guān)规惰,包括消息摘要、數(shù)字簽名和加密服務(wù)泉蝌,而數(shù)字證書以及證書機(jī)構(gòu)(CA – Certificate Authority)是PKI中的重要概念。
看看這篇文章
雙向認(rèn)證原理圖:
需要準(zhǔn)備的自簽名證書
1.服務(wù)器私鑰 2.由CA簽發(fā)的含有服務(wù)器公鑰的數(shù)字證書 3.CA的數(shù)字證書揩晴。在雙向認(rèn)證的實(shí)踐中勋陪,通常服務(wù)器可以自己作為證書機(jī)構(gòu),并且由服務(wù)器CA簽發(fā)服務(wù)器證書和客戶端證書硫兰。
1.客戶端私鑰 2. 由CA簽發(fā)的含有客戶端公鑰的數(shù)字證書诅愚。為了避免中間人攻擊,客戶端還需要內(nèi)置服務(wù)器證書,用來(lái)驗(yàn)證所連接的服務(wù)器是否是指定的服務(wù)器违孝。
- 服務(wù)端 .cer
- 客戶端 .p12
使用AFNetworking 3.0
NSString *certFilePath = [[NSBundle mainBundle] pathForResource:@"server" ofType:@"der"];
NSData *certData = [NSData dataWithContentsOfFile:certFilePath];
NSSet *certSet = [NSSet setWithObject:certData];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:certSet];
policy.allowInvalidCertificates = YES;
policy.validatesDomainName = NO;
_manager = [AFHTTPSessionManager manager];
_manager.securityPolicy = policy;
_manager.requestSerializer = [AFHTTPRequestSerializer serializer];
_manager.responseSerializer = [AFHTTPResponseSerializer serializer];
_manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/plain", nil];
//關(guān)閉緩存避免干擾測(cè)試r
_manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
[_manager setSessionDidBecomeInvalidBlock:^(NSURLSession * _Nonnull session, NSError * _Nonnull error) {
DLog(@"setSessionDidBecomeInvalidBlock");
}];
//客服端請(qǐng)求驗(yàn)證 重寫 setSessionDidReceiveAuthenticationChallengeBlock 方法
__weak typeof(self)weakSelf = self;
[_manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession*session, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing*_credential) {
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
__autoreleasing NSURLCredential *credential =nil;
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
if([weakSelf.manager.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
if(credential) {
disposition =NSURLSessionAuthChallengeUseCredential;
} else {
disposition =NSURLSessionAuthChallengePerformDefaultHandling;
}
} else {
disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}
} else {
// client authentication
SecIdentityRef identity = NULL;
SecTrustRef trust = NULL;
NSString *p12 = [[NSBundle mainBundle] pathForResource:@"client"ofType:@"p12"];
NSFileManager *fileManager =[NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:p12])
{
NSLog(@"client.p12:not exist");
}
else
{
NSData *PKCS12Data = [NSData dataWithContentsOfFile:p12];
if ([[weakSelf class]extractIdentity:&identity andTrust:&trust fromPKCS12Data:PKCS12Data])
{
SecCertificateRef certificate = NULL;
SecIdentityCopyCertificate(identity, &certificate);
const void*certs[] = {certificate};
CFArrayRef certArray =CFArrayCreate(kCFAllocatorDefault, certs,1,NULL);
credential =[NSURLCredential credentialWithIdentity:identity certificates:(__bridge NSArray*)certArray persistence:NSURLCredentialPersistencePermanent];
disposition =NSURLSessionAuthChallengeUseCredential;
}
}
}
*_credential = credential;
return disposition;
}];
+(BOOL)extractIdentity:(SecIdentityRef*)outIdentity andTrust:(SecTrustRef *)outTrust fromPKCS12Data:(NSData *)inPKCS12Data {
OSStatus securityError = errSecSuccess;
//client certificate password
NSDictionary*optionsDictionary = [NSDictionary dictionaryWithObject:@"你的p12密碼"
forKey:(__bridge id)kSecImportExportPassphrase];
CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import((__bridge CFDataRef)inPKCS12Data,(__bridge CFDictionaryRef)optionsDictionary,&items);
if(securityError == 0) {
CFDictionaryRef myIdentityAndTrust =CFArrayGetValueAtIndex(items,0);
const void*tempIdentity =NULL;
tempIdentity= CFDictionaryGetValue (myIdentityAndTrust,kSecImportItemIdentity);
*outIdentity = (SecIdentityRef)tempIdentity;
const void*tempTrust =NULL;
tempTrust = CFDictionaryGetValue(myIdentityAndTrust,kSecImportItemTrust);
*outTrust = (SecTrustRef)tempTrust;
} else {
NSLog(@"Failedwith error code %d",(int)securityError);
return NO;
}
return YES;
}
完畢
- 使用MKNetworkKit 與 使用蘋果官方原生 代碼片段 詳見Demo
Demo
https://github.com/cuiwe000/HttpsDemo.git