$iPhoneDevicePublicKey = $iPhoneDeviceVect['key'];
file_put_contents($DeviceCertRequest_PublicFile, $iPhoneDevicePublicKey);
// Load DeviceCertRequest Public Key.
$DeviceCertRequest_PublicKey = new Crypt_RSA();
$DeviceCertRequest_PublicKey->loadKey(file_get_contents($DeviceCertRequest_PublicFile));
$DeviceCertRequest_PublicKey->setPublicKey();
// Load CSR And get DN.
$DeviceCertRequest_CR = new File_X509();
$DeviceCertRequest_CR->loadCSR($DeviceCertRequest);
$doulCi_DN = $DeviceCertRequest_CR->getDNProp('id-at-commonName');
// Build the new Device Certificate.
$iPhoneDeviceCA = new File_X509();
// $iPhoneDeviceCA->loadCA ( $iPhoneDeviceCA );
$iPhoneDeviceCA->setPublicKey($DeviceCertRequest_PublicKey);
$iPhoneDeviceCA->setDN($DeviceCertRequest_CR->getDN(true));
$iPhoneDeviceCA->removeDNProp('id-at-commonName');
$iPhoneDeviceCA->setDN(array('rdnSequence' => array(array(array('type' => 'id-at-commonName', 'value' => array('ia5String' => $doulCi_DN))))));
$iPhoneDeviceCA->setStartDate('-1 day');
$iPhoneDeviceCA->setEndDate('+ 3 year');
$iPhoneDeviceCA->setSerialNumber('1184677871349854983709', 10);
// Sign Device Certificate.
$DeviceCertificate = $iPhoneDeviceCA->sign($CA_Certificate, $DeviceCertRequest_CR);
// $iPhoneDeviceCA = new File_X509 ();
$iPhoneDeviceCA->loadX509($DeviceCertificate);
// $iPhoneDeviceCA->setPrivateKey ( $CA_Key );
// This can be helpful.
$iPhoneDeviceCA->setExtension('id-ce-authorityKeyIdentifier', $CA_Certificate->setKeyIdentifier(base64_decode('sv4hI0SGlWp51YEmjnMQ2KdMjnQ=')), false);
$iPhoneDeviceCA->setExtension('id-ce-subjectKeyIdentifier', 'kL6MeKDP9yzwKSmh9J0D1hczCbU=', false);
// Set Certificate Usage Settings.
$iPhoneDeviceCA->setExtension('id-ce-basicConstraints', array('cA' => false), true);
$iPhoneDeviceCA->setExtension('id-ce-keyUsage', array('digitalSignature', 'keyEncipherment'), true);