输入错误的证书密码后,iOS应用程序崩溃

时间:2018-04-25 11:22:57

标签: ios objective-c certificate

在我的应用程序中,我需要一个证书才能访问服务器。选择证书后,我会弹出一个输入密码。当它出错时,完整的应用程序崩溃。有谁知道如何处理崩溃?

- (NSURLCredential*) getCertificateCredetials {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *thePath = [documentsDirectory stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] objectForKey:@"certificate"]];

    // gets a certificate from local resources
    //NSString *thePath = [[NSBundle mainBundle] pathForResource:@"mycert" ofType:@"p12"];
    NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];

    if (PKCS12Data == nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fehler" message:@"xxx." delegate:nil cancelButtonTitle:@"Schließen" otherButtonTitles:nil, nil];
        [alert show];
        alert = nil;
        return nil;
    }

    CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;

    SecIdentityRef identity;
    // extract the ideneity from the certificate

    [self extractIdentity :inPKCS12Data :&identity];

    SecCertificateRef certificate = NULL;


    //Below I get an EXC_BAD_ACCESS after wrong password
    SecIdentityCopyCertificate (identity, &certificate);



    const void *certs[] = {certificate};
    CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL);
    // create a credential from the certificate and ideneity, then reply to the challenge with the credential
    return [NSURLCredential credentialWithIdentity:identity certificates:(__bridge NSArray *)certArray persistence:NSURLCredentialPersistencePermanent];
}


- (NSURLCredential*) getCertificateCredetials {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *thePath = [documentsDirectory stringByAppendingPathComponent:[[NSUserDefaults standardUserDefaults] objectForKey:@"certificate"]];

// gets a certificate from local resources
//NSString *thePath = [[NSBundle mainBundle] pathForResource:@"mycert" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];

if (PKCS12Data == nil) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fehler" message:@"Das Zertifikat ist nicht gültig." delegate:nil cancelButtonTitle:@"Schließen" otherButtonTitles:nil, nil];
    [alert show];
    alert = nil;
    return nil;
}

CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;

SecIdentityRef identity;
// extract the ideneity from the certificate

[self extractIdentity :inPKCS12Data :&identity];

SecCertificateRef certificate = NULL;
if (certificate != nil) {
    SecIdentityCopyCertificate (identity, &certificate);
}
const void *certs[] = {certificate};
CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL);
// create a credential from the certificate and ideneity, then reply to the challenge with the credential
return [NSURLCredential credentialWithIdentity:identity certificates:(__bridge NSArray *)certArray persistence:NSURLCredentialPersistencePermanent];
}

编辑:在我开始同步或重新启动应用程序后,从另一个类中添加了同步方法,它也崩溃了。

0 个答案:

没有答案