title: How to make a valid p12 file to be correctly imported by SecPKCS12Import
body: <p>I've solved my previos problem of converting XML RSA private key to PEM file, but I run into another problem that I get null data when importing P12 private key. Following is my steps:</p>
  
  <ol>
  <li><p>Convert PEM file to P12 file</p>
  
  <pre><code>openssl&gt; pkcs12 -export -in rsa.pem -inkey rsa.pem -out rsa.p12 -nocerts
  </code></pre></li>
  <li><p>Read P12 file to iOS project</p>
  
  <pre><code>NSString *path = [[NSBundle bundleForClass:[self class]]    
                      pathForResource:@"MyPrivateKey" ofType:@"p12"];
  NSData *p12data = [NSData dataWithContentsOfFile:path];
  if (![self getPrivateKeyRef]) 
      RSAPrivateKey = getPrivateKeywithRawKey(p12data);
  </code></pre></li>
  <li><p>Import P12 Private Key</p>
  
  <pre><code>SecKeyRef getPrivateKeywithRawKey(NSData *pfxkeydata)
  { 
      NSMutableDictionary * options = [[[NSMutableDictionary alloc] init] autorelease];
  
      // Set the public key query dictionary
      //change to your .pfx  password here 
      [options setObject:@"MyPassword" forKey:(id)kSecImportExportPassphrase];
  
      CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
  
      OSStatus securityError = SecPKCS12Import((CFDataRef) pfxkeydata,
                                               (CFDictionaryRef)options, &amp;items);
  
      CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
      SecIdentityRef identityApp =
      (SecIdentityRef)CFDictionaryGetValue(identityDict,
                                           kSecImportItemIdentity);
      //NSLog(@"%@", securityError);
  
      assert(securityError == noErr);
      SecKeyRef privateKeyRef;
      SecIdentityCopyPrivateKey(identityApp, &amp;privateKeyRef);
  
      return privateKeyRef;
  
  }
  </code></pre></li>
  </ol>
  
  <p>Thought there was no err(OSStatus value is 0), but the items array didn't get any identity data. I am wondering if i didn't get the correct p12 file format due to wrong OpenSSl usage. Has anyone successfully import p12 file? I've stuck in this problem for a couple of days, please give me advices if you got clues, thanks!</p>
  
  <p>Hubert</p>