2016-07-06 27 views
6

Ho un server che utilizza certificati SSL autofirmati per HTTPS. Ho il cert root autofirmato in bundle nella mia app. Sono in grado di ottenere NSURLSession per utilizzare e convalidare il certificato radice autofirmato utilizzando SecTrustSetAnchorCertificates() nel metodo delegato -URLSession:didReceiveChallenge:completionHandler:.Come posso utilizzare un AVPlayer con HTTPS e certificati server autofirmati?

Quando si prova questo con AVPlayer, tuttavia, si verifica un errore SSL e la riproduzione non riesce. Questa è la mia implementazione AVAssetResourceLoader delegato:

- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForResponseToAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge.protectionSpace.authenticationMethod isEqual:NSURLAuthenticationMethodServerTrust]) { 
     SecTrustRef trust = challenge.protectionSpace.serverTrust; 
     SecTrustSetAnchorCertificates(trust, (__bridge CFArrayRef)self.secTrustCertificates); 

     SecTrustResultType trustResult = kSecTrustResultInvalid; 
     OSStatus status = SecTrustEvaluate(trust, &trustResult); 

     if (status == errSecSuccess && (trustResult == kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed)) { 
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:trust] forAuthenticationChallenge:challenge]; 
      return YES; 
     } else { 
      [challenge.sender cancelAuthenticationChallenge:challenge]; 
      return YES; 
     } 
    } 
    return NO; 
} 

Il delegato viene chiamato, e il trustResult equivale a kSecTrustResultUnspecified (che significa "di fiducia, senza comando esplicito dell'utente"), come previsto. Tuttavia, la riproduzione fallisce poco dopo, con il seguente AVPlayerItem.error:

errore di dominio = Codice NSURLErrorDomain = -1200 "Si è verificato un errore SSL e una connessione sicura al server non può essere fatta" UserInfo = {NSLocalizedRecoverySuggestion = Vuoi connetterti comunque al server ?, NSUnderlyingError = 0x16c35720 {Dominio errore = NSOSStatusErrorDomain Code = -1200 "(null)"}, NSLocalizedDescription = Si è verificato un errore SSL e una connessione sicura al server non può essere fatto.}

Come posso ottenere AVPlayer per accettare l'handshake SSL?

risposta

2

Questa implementazione ha funzionato per me:

- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader 
shouldWaitForResponseToAuthenticationChallenge:(NSURLAuthenticationChallenge *)authenticationChallenge 
{ 
    //server trust 
    NSURLProtectionSpace *protectionSpace = authenticationChallenge.protectionSpace; 
    if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
     [authenticationChallenge.sender useCredential:[NSURLCredential credentialForTrust:authenticationChallenge.protectionSpace.serverTrust] forAuthenticationChallenge:authenticationChallenge]; 
     [authenticationChallenge.sender continueWithoutCredentialForAuthenticationChallenge:authenticationChallenge]; 
    } 
    else { // other type: username password, client trust... 
    } 
    return YES; 
} 

Tuttavia, smesso di funzionare come di iOS 10.0.1 per un motivo devo ancora discernere. Quindi questo può o non può esserti utile. In bocca al lupo!

+0

Da allora ho scoperto che funzionava correttamente su un dispositivo, ma avrebbe funzionato in modo strano e imprevedibile nel simulatore iOS, almeno in iOS 9.3.x. Non ho ancora provato su iOS 10. –

+0

Lo stesso problema qui, la mia implementazione ha smesso di funzionare con iOS 10, ho aperto un "Incidente del supporto tecnico" per cercare di risolvere questo problema con un ingegnere Apple. Se riescono a risolvere questo problema, pubblicherò la soluzione qui. – Sylverb

+0

Risposta alla mia STI dall'ingegnere della Apple: Probabilmente è un bug, devo riempire un radar per ulteriori indagini. – Sylverb

Problemi correlati