ios签名服务器维护,如何从iOS中的白名单接受自签名服务器证书?

我试图在NSURLConnection中接受自签名证书,正如许多人在我之前所做的那样.问题是,我只想接受我信任的证书白名单中的证书.我决心要弄清楚如何接受单一证书.这是我在NSURLConnectionDelegate中到目前为止所获得的代码:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"trusted" ofType:@"der"];

NSData *certData = [[NSData alloc] initWithContentsOfFile:thePath];

CFDataRef myCertData = (__bridge_retained CFDataRef)certData;

SecCertificateRef myCert = SecCertificateCreateWithData(NULL,myCertData);

SecPolicyRef myPolicy = SecPolicyCreateBasicX509();

SecCertificateRef certArray[1] = { myCert };

CFArrayRef myCerts = CFArrayCreate(NULL,(void *)certArray,1,NULL);

SecTrustRef myTrust;

OSStatus status = SecTrustCreateWithCertificates(myCerts,myPolicy,&myTrust);

SecTrustResultType trustResult;

if (status == noErr) {

status = SecTrustEvaluate(myTrust,&trustResult);

}

BOOL trusted = NO;

if (trustResult == kSecTrustResultUnspecified) {

// I never get here. Instead,trustResult is always kSecTrustResultRecoverableTrustFailure

trusted = YES;

}

if (trusted) {

[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]

forAuthenticationChallenge:challenge];

} else {

[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];

}

CFRelease(myTrust);

CFRelease(myCerts);

CFRelease(myPolicy);

CFRelease(myCert);

CFRelease(myCertData);

} else {

[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];

}

}

正如您在评论中看到的那样,我实际上从未获得过kSecTrustResultUnspecified,这是我期望得到的.我验证了我的证书是正确的,并且格式正确(DER).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值