Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could export the certificate(s) you need from the keystore like this</p> <pre><code>keytool -exportcert -keystore &lt;keystore&gt; -file some.cer </code></pre> <p>You may need to tell keytool about the store type and store provider, <a href="https://stackoverflow.com/questions/6866447/keytool-error-java-security-keystoreexception-bks-not-found">look here</a>.</p> <p>You can read that .cer file into the iOS keychain with this code:</p> <pre><code>- (void) importCertToKeyChain: (NSData *) data { // Delete the old certificate, otherwise SecItemAdd complains. OSStatus oss = SecItemDelete((__bridge CFDictionaryRef)([self clientCertificateQuery])); // Import the certificate SecCertificateRef certRef = NULL; certRef = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)(data)); NSDictionary *att = [NSDictionary dictionaryWithObjectsAndKeys: (__bridge id)(kSecClassCertificate), kSecClass, (__bridge id) certRef, kSecValueRef, nil]; oss = SecItemAdd((__bridge CFDictionaryRef)(att), NULL); } </code></pre> <p>When you need the certificate you can get from the keychain like this:</p> <pre><code>- (SecCertificateRef) getCertFromKeyChain { CFTypeRef ref = NULL; SecItemCopyMatching((__bridge CFDictionaryRef)([self clientCertificateQuery]), &amp;ref); return (SecCertificateRef) ref; } </code></pre> <p>The clientCertificateQuery looks like this.</p> <pre><code>static NSString *clientCertSubject = @"TestSubjectClient"; -(NSMutableDictionary *) clientCertificateQuery { NSMutableDictionary *query = [[NSMutableDictionary alloc] init]; [query setObject:(__bridge id) kSecClassCertificate forKey:(__bridge id)kSecClass]; [query setObject:clientCertSubject forKey:(__bridge id&lt;NSCopying&gt;)(kSecMatchSubjectContains)]; [query setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef]; id)kSecAttrKeyType]; return query; } </code></pre> <p>There is also a function to read a PCKS12 store (you would still need to transform your BKS store into that format). It's called <code>SecPKCS12Import</code> and with it you won't need to import the cert into your iOS keychain. I had no luck with it and needed the cert in the keychain anyway, but here is <a href="https://stackoverflow.com/questions/9874932/ssl-identity-certificate-to-run-an-https-server-on-ios">something about this</a>.</p> <p><strong>Update:</strong></p> <p>As camdaochemgio pointed out in the comments using above method is not recommend when including a certificate that contains secret information (like private keys) in an app. Because the .cer file is not protected and can easily be extracted from the .ipa.</p> <p>PKCS#P12 supports password protection so it's better to use this.</p> <p>You can covert your keystore to PKCS#P12 like this (taken <a href="http://shib.kuleuven.be/docs/ssl_commands.shtml" rel="nofollow noreferrer">from here</a>):</p> <pre><code> keytool -importkeystore -srckeystore KEYSTORE.jks -destkeystore KEYSTORE.p12 -srcstoretype BKS -deststoretype PKCS12 -srcstorepass mysecret -deststorepass mysecret -srcalias myalias -destalias myalias -srckeypass mykeypass -destkeypass mykeypass -noprompt </code></pre> <p>Then you can load the .p12 file like this (credits go <a href="https://stackoverflow.com/questions/11173711/how-do-i-programatically-import-a-certificate-into-my-ios-apps-keychain-and-pas">here</a>)</p> <pre><code>// Load Certificate NSString *path = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"]; NSData *p12data = [NSData dataWithContentsOfFile:path]; CFDataRef inP12data = (__bridge CFDataRef)p12data; // Only password based PKCS#12 blobs are supported CFStringRef password = CFSTR("Password"); const void *keys[] = { kSecImportExportPassphrase }; const void *values[] = { password }; CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL); // The import CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); OSStatus securityError = SecPKCS12Import(inP12data, options, &amp;items); if (securityError == 0) { // Exploring the content CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex(items, 0); const void *tempIdentity = NULL; tempIdentity = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemIdentity); *identity = (SecIdentityRef)tempIdentity; const void *tempTrust = NULL; tempTrust = CFDictionaryGetValue(myIdentityAndTrust, kSecImportItemTrust); *trust = (SecTrustRef)tempTrust; } if (options) { CFRelease(options); } </code></pre> <p>Last but not least some links regarding this topic:</p> <ul> <li><a href="https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/Reference/reference.html" rel="nofollow noreferrer">https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/Reference/reference.html</a></li> <li><a href="https://developer.apple.com/library/ios/documentation/Security/Reference/certifkeytrustservices/Reference/reference.html" rel="nofollow noreferrer">https://developer.apple.com/library/ios/documentation/Security/Reference/certifkeytrustservices/Reference/reference.html</a></li> <li><a href="https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html" rel="nofollow noreferrer">https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html</a></li> <li><a href="https://github.com/kuapay/iOS-Certificate--Key--and-Trust-Sample-Project" rel="nofollow noreferrer">https://github.com/kuapay/iOS-Certificate--Key--and-Trust-Sample-Project</a></li> <li><a href="http://blog.wingsofhermes.org/?p=75" rel="nofollow noreferrer">http://blog.wingsofhermes.org/?p=75</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload