WSDL Web Services Authentication Using P12 Certificate in ios

Title ## I am working on WSDL web services. The client provided a p12 certificate to authenticate these web services.

I applied the following code to the only certificate I added to my package.

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

{
    if ([challenge previousFailureCount] == 0)
    {
        identity = [self getClientCertificate];
        CFArrayRef certs = [self getCertificate];
        NSArray *myArray = (__bridge NSArray *)certs;
        NSURLCredential *newCredential = [NSURLCredential credentialWithIdentity:identity
        certificates:myArray persistence:NSURLCredentialPersistenceNone];
        [challenge.sender useCredential:newCredential forAuthenticationChallenge:challenge];
    }
    else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

(CFArrayRef)getCertificate
{
    SecCertificateRef certificate = nil;
    SecIdentityCopyCertificate(identity, &certificate);
    SecCertificateRef certs[1] = {certificate};
    CFArrayRef array = CFArrayCreate(NULL, (const void **) certs, 1, NULL);
    SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
    SecTrustRef myTrust;

    OSStatus status = SecTrustCreateWithCertificates(array, myPolicy, &myTrust);
    if (status == noErr){
        NSLog(@"No Err creating certificate");
    }
    else{
        NSLog(@"Possible Err Creating certificate");
    }
    return array;
}

(SecIdentityRef)getClientCertificate
{
    SecIdentityRef identityApp = nil;
    NSString *thePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"p12"];
    NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
    CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;
    CFStringRef password = CFSTR("Password1");
    const void *keys[] = {kSecImportExportPassphrase}; //kSecImportExportPassphrase };
    const void *values[] = {password};
    CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
    CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
    OSStatus securityError = SecPKCS12Import(inPKCS12Data, options, &items);
    CFRelease(options);
    CFRelease(password);
    if (securityError == errSecSuccess)
    {
        NSLog(@"Success opening p12 certificate. Items: %ld", CFArrayGetCount(items));
        CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
        identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict,
                kSecImportItemIdentity);
    }
    else{
        NSLog(@"Error opening Certificate.");
    }
    return identityApp;
}

It works fine for a single certificate. But now the client has a new requirement that the certificate will not be uniform. This will be different for each user. A p12 email certificate will be sent to users, from where the user can download.

Problem . The iPhone provisioning profile in which the certificate will be installed is in a separate sandbox, and the application is in another.

, ( ), .

.

+5

All Articles