Server Zertifikat in WebView anzeigen [macOS | Objective-C]

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Server Zertifikat in WebView anzeigen [macOS | Objective-C]

    Hallo zusammen,

    heute habe ich eine sehr spezielle Frage. Und zwar geht es um das Anzeigen des Server Zertifikates bei einer HTTPS Verbindung in einem WebView (im Grunde wie in Safari).
    Bisher schaut mein Code so aus (SecurityInterface/SFCertificatePanel.h ist implementiert).
    Das Zertifikat wird als Fenster angezeigt, aber das Verhalten dieses Fensters ist seltsam. Es kann nur einmal geöffnet werden, danach muss die Demo App neu gestartet werden, damit das Zertifikat wieder eingeblendet werden.
    Wo liegt hier der Fehler? ?(


    Quellcode

    1. -(IBAction)ZeigeZertifikat:(id)sender
    2. {
    3. NSString *ZertifikatURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"URL"];
    4. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ZertifikatURL]];
    5. // Create url connection and fire request
    6. NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    7. }

    Quellcode

    1. -(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    2. {
    3. if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
    4. // create trust from protection space
    5. SecTrustRef trustRef;
    6. int trustCertificateCount = (int)SecTrustGetCertificateCount(challenge.protectionSpace.serverTrust);
    7. NSMutableArray* trustCertificates = [[NSMutableArray alloc] initWithCapacity:trustCertificateCount];
    8. for (int i = 0; i < trustCertificateCount; i++) {
    9. SecCertificateRef trustCertificate = SecTrustGetCertificateAtIndex(challenge.protectionSpace.serverTrust, i);
    10. [trustCertificates addObject:(__bridge id) trustCertificate];
    11. }
    12. [[SFCertificatePanel sharedCertificatePanel] runModalForCertificates:trustCertificates showGroup:YES];
    13. }
    14. }
    Alles anzeigen

    Danke fürs' anschauen. Hoffe, jemand kann mir einen Tipp geben.
    Eine Demo App mit dem Xcode Projekt (Objective-C | macOS) habe ich angehängt.
    Anleiten lassen habe ich mich bisher hiervon: stackoverflow.com/questions/19…nformation-inside-cocoa-w
    Dateien

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Scotch ()

  • Entschuldigung, dass ich mich so doof anstelle. Komme von Pascal und habe noch nie größere Sachen in Objective-C programmiert.
    Das Problem ist, dass es zwei Probleme gibt.

    1. Ich bekomme das Sheet nicht programmiert, welches ich auch ursprünglich wollte. Finde dieses auch besser, als ein Panel.

    Quellcode

    1. [[SFCertificatePanel sharedCertificatePanel] beginSheetForWindow:_window modalDelegate:self certificates:trustCertificates showGroup:YES];

    Bekomme hier die Fehlermeldung: "No visibile @interface for SFCertificatePanel declares the selector beginSheetForWindow"...


    2. Davon abgesehen löst das Panel nicht das Problem des seltsamen Verhaltens:
    Habe im Code das

    Quellcode

    1. [[SFCertificatePanel sharedCertificatePanel] runModalForCertificates:trustCertificates showGroup:YES];
    ersetzt durch einen NSLog, der irgendwas ausspucken soll. Beim ersten Drücken des "Zertifikat anzeigen"-Buttons sehe ich das Ergebnis des NSLogs im Log.
    Beim zweiten Mal nicht. Das heißt, dass der Zertifikat-Panel-Script auch nur einmal ausgeführt wird.

    ...ich bin verwirrt ?(
  • Scotch schrieb:



    Quellcode

    1. -(IBAction)ZeigeZertifikat:(id)sender
    2. {
    3. NSString *ZertifikatURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"URL"];
    4. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ZertifikatURL]];
    5. // Create url connection and fire request
    6. NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    7. }
    Den Button meine ich. Habe keinen anderen Weg, als den über den request, gefunden.
  • Versuch doch mal das YOU MUST aus der dokumentation zu erfüllen.
    Vielleicht wird die methode dann wiederholt aufgerufen...

    In this method,you must invoke one of the challenge-responder methods (NSURLAuthenticationChallengeSender protocol):

    IMPORTANT
    Your delegate method is called on the thread where the connection is scheduled. Always call the methods above on that same thread.
    You might also want to analyze challenge for the authentication scheme and the proposed credential before calling a NSURLAuthenticationChallengeSender method. You should never assume that a proposed credential is present. You can either create your own credential and respond with that, or you can send the proposed credential back. (Because this object is immutable, if you want to change it you must copy it and then modify the copy.)
  • Mein Code sieht jetzt folgendermaßen aus:

    Quellcode

    1. #import "AppDelegate.h"
    2. #import <SecurityInterface/SFCertificatePanel.h>
    3. #import <WebKit/WebKit.h>
    4. @interface AppDelegate ()
    5. @property (weak) IBOutlet NSWindow *window;
    6. @end
    7. @implementation AppDelegate
    8. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    9. // Insert code here to initialize your application
    10. NSString *url = @"https://google.de";
    11. [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    12. }
    13. - (void)applicationWillTerminate:(NSNotification *)aNotification {
    14. // Insert code here to tear down your application
    15. }
    16. -(IBAction)ZeigeZertifikat:(id)sender
    17. {
    18. [[SFCertificatePanel sharedCertificatePanel] beginSheetForWindow: _window modalDelegate:self didEndSelector:@selector(myDidEndMethod) contextInfo:NULL certificates: trustCertificates showGroup:YES];
    19. }
    20. -(void)myDidEndMethod
    21. {
    22. NSLog(@"did end");
    23. }
    24. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
    25. {
    26. if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
    27. SecTrustRef trustRef;
    28. int trustCertificateCount = (int)SecTrustGetCertificateCount(challenge.protectionSpace.serverTrust);
    29. NSMutableArray* trustCertificates = [[NSMutableArray alloc] initWithCapacity:trustCertificateCount];
    30. for (int i = 0; i < trustCertificateCount; i++) {
    31. SecCertificateRef trustCertificate = SecTrustGetCertificateAtIndex(challenge.protectionSpace.serverTrust, i);
    32. [trustCertificates addObject:(__bridge id) trustCertificate];
    33. }
    34. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
    35. }
    36. }
    Alles anzeigen