This question was apparently similar but had no answers of any kind: [Programmatically create a x509 certificate for iPhone without using OpenSSL](https://stackoverflow.com/questions/24683786/programmatically-create-a-x509-certificate-for-iphone-without-using-openssl)

In our application (server, client), we are implementing client authentication (SSL based on X509Certificate). We already have a way to generate a `keypair`, create a `PKCS10 Certificate Signing Request`, have this signed by the `self-signed CA` and create a `X509Certificate`, send this back. However, to use this certificate in SSL requests, the `private key` and the `X509Certificate` have to be exported to a `PKCS12` (P12) `keystore`.

Does anyone know anything about how to do this, or even if it's possible? The client **has** to generate the P12 file (we don't want to give out the private key), and the client is running iOS, and is a mobile device. The solution worked for Android using BouncyCastle (SpongyCastle), but we found nothing for iOS.

EDIT: In Java, this export is done by the following:

```
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    KeyStore ks = KeyStore.getInstance("PKCS12", BouncyCastleProvider.PROVIDER_NAME);
    ks.load(null);
    ks.setKeyEntry("key-alias", (Key) key, password.toCharArray(),
            new java.security.cert.Certificate[] { x509Certificate });
    ks.store(bos, password.toCharArray());
    bos.close();
    return bos.toByteArray();

```