Me gustaría formular una pregunta de seguimiento al a previously posed question. Tengo el código para crear una solicitud/conexión NSURL, ejecutarlo y llamar a los métodos de devolución de llamada para la autenticación. Aquí está el código específico:Determinación de confianza con NSURLConnection y NSURLProtectionSpace
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodDefault];
}
-(void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] > 0) {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@"Bad Username Or Password");
badUsernameAndPassword = YES;
finished = YES;
return;
}
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
if (appDelegate._allowInvalidCert)
{
// Go ahead...trust me!
[challenge.sender useCredential:
[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]
forAuthenticationChallenge: challenge];
}
else
{
TrustGenerator *tg = [[TrustGenerator alloc] init];
if ([tg getTrust:challenge.protectionSpace])
{
// Go ahead...trust me!
[challenge.sender useCredential:
[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]
forAuthenticationChallenge: challenge];
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
}
else if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodDefault) {
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
}
Lo que estoy corriendo en que es "didReceiveAuthenticationChallenge" con "[challenge.protectionSpace.authenticationMethod isEqualToString: NSURLAuthenticationMethodServerTrust]" es SIEMPRE ser llamado, incluso cuando el certificado en el servidor Estoy intentando conectarme a la lista de confianza (haciendo pruebas con un certificado de Verisign). Entonces, lo que estoy viendo es que mi aplicación siempre está pidiendo al usuario final que confíe incluso cuando el sitio web es de confianza. Karma malo teniendo en cuenta que es lo que se supone que debe suceder con un hombre en medio del ataque, etc. Lo que realmente estoy buscando es algo de código como este:
if (appDelegate._allowInvalidCert)
{
// Go ahead...trust me!
[challenge.sender useCredential:
[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]
forAuthenticationChallenge: challenge];
}
else if(The OS trusts the cert on the server)
{
[challenge.sender useCredential:
[NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]
forAuthenticationChallenge: challenge];
}
else{...
¡Excelente publicación! ¿Dónde puedo encontrar el código 'TrustGenerator'? –