【问题标题】:preventing self signed ssl certificates in ios5防止 ios5 中的自签名 ssl 证书
【发布时间】:2011-12-19 17:06:17
【问题描述】:

我使用执行基本 HTTP 身份验证的代码,见下文。这在 IOS 5 中运行良好。但是现在我们将协议更改为 https,并且我们使用了伪造的自签名证书。它也奏效了!这似乎不安全。有人知道您是否需要在这种方法中做一些事情来防止某些证书被接受?

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

if ([challenge previousFailureCount] <= maxRetryCount ) {
    NSURLCredential *newCredential =
    [NSURLCredential
     credentialWithUser: userName
     password:password
     persistence:NSURLCredentialPersistenceForSession];

    [[challenge sender]
     useCredential:newCredential
     forAuthenticationChallenge:challenge];

   }
   else
   {
     NSLog(@"Failure count %d",[challenge previousFailureCount]);
   }
}

【问题讨论】:

    标签: ios ssl https ios5


    【解决方案1】:

    看来我自己找到了答案。这会阻止无效证书。 使用有效证书登录时仍然需要测试它是否有效。

    - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:
           (NSURLAuthenticationChallenge *)challenge {
    
       if ([[[challenge protectionSpace] authenticationMethod] isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) {
          [[challenge sender] performDefaultHandlingForAuthenticationChallenge:challenge];
       }
       else {
          if ([challenge previousFailureCount] <= maxRetryCount ) {
            NSURLCredential *newCredential =
            [NSURLCredential
             credentialWithUser: userName
             password:password
             persistence:NSURLCredentialPersistenceForSession];
    
            [[challenge sender]
             useCredential:newCredential
             forAuthenticationChallenge:challenge];
    
           }
           else
           {
             NSLog(@"Failure count %d",[challenge previousFailureCount]);
           }
       }
    }
    

    【讨论】:

    • 请注意,您应该使用适当的NSURLAuthenticationMethodServerTrust 常量
    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2015-05-23
    • 2019-12-11
    • 2019-09-08
    • 2019-09-18
    • 2018-11-16
    相关资源
    最近更新 更多