title: iOS extracting certificate identity: EXC_BAD_ACCESS
body: <p>I'm developing an iOS application and struggling to extract an identity from a .p12 certificate. I'm still new to objective-c so I'm sure something major is missing. Here's the code:</p>
  
  <pre><code>@implementation P12Extractor
  
  -(SecIdentityRef)getIdentity{
  NSString *path = [[NSBundle mainBundle] pathForResource:@"ServerCert" ofType:@"p12"];
  NSData *p12data = [NSData dataWithContentsOfFile:path];
  CFDataRef inP12Data = (__bridge CFDataRef)p12data;
  SecIdentityRef myIdentity;
  
  OSStatus status = extractIdentity(inP12Data, &amp;myIdentity);
  
  if (status != 0) {
      NSLog(@"%@",status);
  }
  return myIdentity;
  
  }
  
  OSStatus extractIdentity(CFDataRef inP12Data, SecIdentityRef *identity){
  OSStatus securityError = errSecSuccess;
  
  CFStringRef password = CFSTR("password");
  const void *keys[] = { kSecImportExportPassphrase };
  const void *values[] = { password };
  
  CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
  
  CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
  securityError = SecPKCS12Import(inP12Data, options, &amp;items);
  
  if (securityError == 0) {
      CFDictionaryRef ident = CFArrayGetValueAtIndex(items,0); // &lt;&lt;&lt;at this point i get an EXC_BAD_ACCESS(code=2,adress=0x0) error
      const void *tempIdentity = NULL;
      tempIdentity = CFDictionaryGetValue(ident, kSecImportItemIdentity);
      *identity = (SecIdentityRef)tempIdentity;
  }
  
  if (options) {
      CFRelease(options);
  }
  
  return securityError;
  }
  
  @end
  </code></pre>
  
  <p>I've marked the point of error with a comment, I really have no idea why I keep getting this. This code should be the approved solution from Apple dev site.</p>