美文网首页
https 申请自签名证书 及 IOS端调用方法总结

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

作者: 7394 | 来源:发表于2017-08-19 03:19 被阅读0次

步骤: 


一)[服务器端要做的:]服务器端生成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];

}

相关文章

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

    步骤: 一)[服务器端要做的:]服务器端生成pxf文件+给乱七八糟设置: -------- 参考代码如下-----...

  • iOS https请求出现Default TLS Trust e

    公司服务端更新了https,而使用的是自签名的证书,导致iOS开发过程中调用HTTPS接口时,证书不被信任,出现了...

  • iOS支付宝使用总结

    iOS支付宝使用总结 后台签名,前端只负责调起客户端进行支付 前端签名演示,不建议这么做 错误情况总结 调用客户端...

  • openssl shell辅助工具

    最近折腾自签名证书,研究了下生成根证书、服务端证书、颁发客户证书; 以及证书吊销;结合nginx实现自签名双向认证...

  • iOS相关的参考文章

    1.iOS用自签名证书实现HTTPS请求的原理实例讲解2.骆神详细版ios上线流程

  • 踩坑记

    1、android自签名证书Glide加载不出图片 关于https中自签名证书的介绍以及OkHttp中解决自签名证...

  • Volley支持Https及自签名证书

    1.https是什么? 简单讲,https 是在http的基础上增加了SSL/TLS协议。详细参见HTTPS传输加...

  • iOS 测试包

    若想测试iOS开发完成的应用需先给应用进行签名,进行签名则先要申请证书,不同的测试方式需要不同的证书。 测试iOS...

  • iOS自签名证书https请求(NSURLSession)

    把client.p12导入进入项目,添加测试代码 实现NSURLSessionDelegate 这里主要完成的就是...

  • java服务器https

    https服务端配置文章 自签名证书实现https 生成jks(java key store),一种存储秘钥(公钥...

网友评论

      本文标题:https 申请自签名证书 及 IOS端调用方法总结

      本文链接:https://www.haomeiwen.com/subject/ymwzrxtx.html