https 申请自签名证书 及 IOS端调用方法总结

步骤: 


一)[服务器端要做的:]服务器端生成pxf文件+给乱七八糟设置: 

-------- 参考代码如下--------

$cert = New-SelfSignedCertificate -DnsName "自己想" -CertStoreLocation "cert:\LocalMachine\My"

$cert = New-SelfSignedCertificate -DnsName "域名" -CertStoreLocation "cert:\LocalMachine\My"

$pwd = ConvertTo-SecureString -String 'password0A' -Force -AsPlainText

$path = 'cert:\LocalMachine\My' + $cert.thumbprint

Export-PfxCertificate -Cert $cert -FilePath c:\temp\certreaotransaction.pfx -Password $pwd

二)[客户端要做的:]将pfx文件通过openssl转成cer文件

(终端进入pfx文件夹 输入如下指令)

openssl pkcs12 -in cert.pfx -out cert.cer -nodes (会导出一个cer文件)

(注:但这个cer文件是64code编码,objc文件中使用会出现如下图片中错误:caref is nill,需要再准换成DER-encoded X.509 certificate,也就是用下面的代码转成der格式)

openssl x509 -in myCA.crt -inform PEM -out myCA.cer -outform DER(得到:myCA.der)

三)将myCA.der文件添加到xcode项目中

四)info.plist 

info.plist 中加入App Transport Security Settings ,Allow Arbitrary Loads设置为YES

五)写代码!先写GET方式 传入https

记得先添加<NSURLSessionDelegate>

然后:如下图

【get请求方式】


六)链接didReceiveChallenge Delegate


代码如下: 

- (void)URLSession:(NSURLSession*)session didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge

completionHandler:(void(^)(NSURLSessionAuthChallengeDispositiondisposition,NSURLCredential*_Nullablecredential))completionHandler {

NSLog(@"证书认证");

if([[[challengeprotectionSpace]authenticationMethod]isEqualToString:NSURLAuthenticationMethodServerTrust]) {

do{

//

SecTrustRefserverTrust = [[challengeprotectionSpace]serverTrust];

NSCAssert(serverTrust !=nil,@"serverTrust is nil");

if(nil== serverTrust)

break;/* failed */

/**

*导入多张CA证书(Certification Authority,支持SSL证书以及自签名的CA),请替换掉你的证书名称

*/

NSString*cerPath = [[NSBundlemainBundle]pathForResource:@"myCA"ofType:@"der"];//自签名证书

NSData* caData = [NSDatadataWithContentsOfFile:cerPath];

NSCAssert(caData !=nil,@"caCert is nil");

if(nil== caData)

break;/* failed */

SecCertificateRefcaRef =SecCertificateCreateWithData(NULL, (__bridgeCFDataRef)caData);

NSCAssert(caRef !=nil,@"caRef is nil");

if(nil== caRef)

break;/* failed */

////可以添加多张证书

NSArray*caArray =@[(__bridgeid)(caRef)];

NSCAssert(caArray !=nil,@"caArray is nil");

if(nil== caArray)

break;/* failed */

OSStatusstatus =SecTrustSetAnchorCertificates(serverTrust, (__bridgeCFArrayRef)caArray);

NSCAssert(errSecSuccess== status,@"SecTrustSetAnchorCertificates failed");

if(!(errSecSuccess== status))

break;/* failed */

SecTrustResultTyperesult = -1;

//通过本地导入的证书来验证服务器的证书是否可信

status =SecTrustEvaluate(serverTrust, &result);

if(!(errSecSuccess== status))

break;/* failed */

BOOLallowConnect = (result ==kSecTrustResultUnspecified) || (result ==kSecTrustResultProceed);

if(allowConnect) {

NSLog(@"success");

}else{

NSLog(@"error");

}

/* kSecTrustResultUnspecified and kSecTrustResultProceed are success */

if(! allowConnect)

{

break;/* failed */

}

#if0

/* Treat kSecTrustResultConfirm and kSecTrustResultRecoverableTrustFailure as success */

/*since the user will likely tap-through to see the dancing bunnies */

if(result == kSecTrustResultDeny || result == kSecTrustResultFatalTrustFailure || result == kSecTrustResultOtherError)

break;/* failed to trust cert (good in this case) */

#endif

// The only good exit point

NSLog(@"信任该证书");

NSURLCredential*credential = [NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust];

completionHandler(NSURLSessionAuthChallengeUseCredential,credential);

return[[challengesender]useCredential: credential

forAuthenticationChallenge: challenge];

}

while(0);

}

// Bad dog

NSURLCredential*credential = [NSURLCredentialcredentialForTrust:challenge.protectionSpace.serverTrust];

completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,credential);

return[[challengesender]cancelAuthenticationChallenge: challenge];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 由于苹果规定2017年1月1日以后,所有APP都要使用HTTPS进行网络请求,否则无法上架,因此研究了一下在iOS...
    浪投王阅读 26,782评论 54 76
  • 2016.11.16 三年以后的一万朵玫瑰 算是你对我许下的承诺么 我已经不会经常去想我们会不会走到以后这样的事情...
    浅象小姐有话说阅读 298评论 0 0
  • 文艺青年都喜欢说去远方,从一个城市到另一个城市,可能没钱,那就边打工边行路,在途中结识不同的人,见各样的风景,若遇...
    印象的派阅读 437评论 1 1
  • 很多产品经理都会问,为什么我们的产品在5个月之前火热火热的,现在却被嫌弃了呢? 答:没有根据市场需求信息的反馈调整...
    iscuker阅读 2,059评论 2 1
  • 北方有佳人,遗世而独立 我叫林佳,我的男朋友叫麦冬。他是一个普通人,也不是一个普通人。他是最近很红的一个当红偶像,...
    兼可言阅读 913评论 0 0