function signNewCert() { if (!$GLOBALS['isCA']) { return false; } else { $CAPrivKey = new Crypt_RSA(); $CAPrivKey->loadKey($GLOBALS['CAPrivKeyStr']); $CAx509 = new File_X509(); $CAx509->loadX509($GLOBALS['CAPubX509']); //认证证书 $privKey = new Crypt_RSA(); $keyArray = $CAPrivKey->createKey($GLOBALS['RSALength']); $privKey->loadKey($keyArray['privatekey']); $pubKey = new Crypt_RSA(); $pubKey->loadKey($keyArray['publickey']); $pubKey->setPublicKey(); $subject = new File_X509(); $subject->setDNProp('id-at-organizationName', $GLOBALS['CAname'] . ' cert'); $subject->setPublicKey($pubKey); $issuer = new File_X509(); $issuer->setPrivateKey($CAPrivKey); $issuer->setDN($CAx509->getDN()); $x509 = new File_X509(); $result = $x509->sign($issuer, $subject); return array('privateKey' => $privKey->getPrivateKey(), 'publicX509' => $x509->saveX509($result)); } }
public function generateKeyPair($keyPath, $keySize = 1024) { $privKey = new \Crypt_RSA(); extract($privKey->createKey($keySize)); $privKey->loadKey($privatekey); $pubKey = new \Crypt_RSA(); $pubKey->loadKey($publickey); $pubKey->setPublicKey(); $subject = new \File_X509(); $subject->setDNProp('id-of-organization', 'phpseclib demo cert'); $subject->setPublicKey($pubKey); $issuer = new \File_X509(); $issuer->setPrivateKey($privKey); $issuer->setDN($subject->getDN()); $x509 = new \File_X509(); $result = $x509->sign($issuer, $subject); file_put_contents($keyPath . '/private.key', $privKey->getPrivateKey()); file_put_contents($keyPath . '/public.crt', $x509->saveX509($result)); }
/** * @param string $certPem * @param array $keyPairPems * Pair of PEM-encoded keys. * @param string $caCertPem * @return \File_X509 */ public static function loadCert($certPem, $keyPairPems = NULL, $caCertPem = NULL) { $certObj = new \File_X509(); if (isset($caCertPem)) { $certObj->loadCA($caCertPem); } if ($certPem) { $certObj->loadX509($certPem); } if (isset($keyPairPems['privatekey'])) { $privKey = new \Crypt_RSA(); $privKey->loadKey($keyPairPems['privatekey']); $certObj->setPrivateKey($privKey); } if (isset($keyPairPems['publickey'])) { $pubKey = new \Crypt_RSA(); $pubKey->loadKey($keyPairPems['publickey']); $pubKey->setPublicKey(); $certObj->setPublicKey($pubKey); } return $certObj; }
/** * Create a CSR for an authority that can issue CRLs. * * @param array $keyPair * @param string $dn * @return string * PEM-encoded CSR. */ public static function createCrlDistCSR($keyPair, $dn) { $privKey = new \Crypt_RSA(); $privKey->loadKey($keyPair['privatekey']); $pubKey = new \Crypt_RSA(); $pubKey->loadKey($keyPair['publickey']); $pubKey->setPublicKey(); $csr = new \File_X509(); $csr->setPrivateKey($privKey); $csr->setPublicKey($pubKey); $csr->setDN($dn); $csr->loadCSR($csr->saveCSR($csr->signCSR(Constants::CERT_SIGNATURE_ALGORITHM))); $csr->setExtension('id-ce-keyUsage', array('cRLSign')); $csrData = $csr->signCSR(Constants::CERT_SIGNATURE_ALGORITHM); return $csr->saveCSR($csrData); }
// Load the certificate public key. $pubkey = new Crypt_RSA(); $pubkey->loadKey(file_get_contents('certs/pubkey.pem')); $pubkey->setPublicKey(); // Build the new certificate. $iPhoneDeviceCA = new File_X509(); $iPhoneDeviceCA->loadCA($pemca); $iPhoneDeviceCA->setPublicKey($pubkey); $iPhoneDeviceCA->setDN('C=US, ST=Some-State, L=Cupertino, O=Apple Inc., OU=Apple iPhone, CN=Apple iPhone Device CA'); $iPhoneDeviceCA->setStartDate('-1 day'); $iPhoneDeviceCA->setEndDate('+ 1 year'); $iPhoneDeviceCA->setSerialNumber('10134611745959375605', 10); // Sign new certificate. $iPhoneDeviceCA_Result = $iPhoneDeviceCA->sign($ca, $iPhoneDeviceCA); // Output it. echo $iPhoneDeviceCA->saveX509($iPhoneDeviceCA_Result) . "\n"; // subject=/C=US/O=Apple Inc./OU=Apple iPhone/CN=Apple iPhone Device CA // issuer=/C=US/O=Apple Inc./OU=Apple Certification Authority/CN=Apple iPhone // Certification Authority // Build the new certificate. $iPhoneActivation = new File_X509(); $iPhoneActivation->loadCA($pemca); $iPhoneActivation->setPublicKey($pubkey); $iPhoneActivation->setDN('C=US, ST=Some-State, L=Cupertino, O=Apple Inc., OU=Apple iPhone, CN=Apple iPhone Activation'); $iPhoneActivation->setStartDate('-1 day'); $iPhoneActivation->setEndDate('+ 1 year'); $iPhoneActivation->setSerialNumber('2', 10); // Sign new certificate. $iPhoneActivation_Result = $iPhoneActivation->sign($ca, $iPhoneActivation); // Output it. echo $iPhoneActivation->saveX509($iPhoneActivation_Result) . "\n";
$subject->setDNProp('id-at-organizationName', 'phpseclib demo CA'); $subject->setPublicKey($pubKey); $issuer = new File_X509(); $issuer->setPrivateKey($CAPrivKey); $issuer->setDN($CASubject = $subject->getDN()); $x509 = new File_X509(); $x509->makeCA(); $result = $x509->sign($issuer, $subject); echo "the CA cert to be imported into the browser is as follows:\r\n\r\n"; echo $x509->saveX509($result); echo "\r\n\r\n"; // create private key / x.509 cert for stunnel / website $privKey = new Crypt_RSA(); extract($privKey->createKey()); $privKey->loadKey($privatekey); $pubKey = new Crypt_RSA(); $pubKey->loadKey($publickey); $pubKey->setPublicKey(); $subject = new File_X509(); $subject->setDNProp('id-at-organizationName', 'phpseclib demo cert'); $subject->setPublicKey($pubKey); $issuer = new File_X509(); $issuer->setPrivateKey($CAPrivKey); $issuer->setDN($CASubject); $x509 = new File_X509(); $result = $x509->sign($issuer, $subject); echo "the stunnel.pem contents are as follows:\r\n\r\n"; echo $privKey->getPrivateKey(); echo "\r\n"; echo $x509->saveX509($result); echo "\r\n";
// csr public key $vectxq = openssl_pkey_get_details(openssl_csr_get_public_key($deviceCertRequest)); $pkeyxq = $vectxq['key']; file_put_contents('certs/pubkey.pem', $pkeyxq); // Load the certificate public key. $pubkey = new Crypt_RSA(); $pubkey->loadKey($pkeyxq); $pubkey->setPublicKey(); $x509 = new File_X509(); $csr = $x509->loadCSR($deviceCertRequest); // see csr.csr $dn = $x509->getDN(true); // Build the new certificate. $iPhoneDeviceCA = new File_X509(); $iPhoneDeviceCA->loadCA($pemca); $iPhoneDeviceCA->setPublicKey($pubkey); $iPhoneDeviceCA->setDN($dn); $iPhoneDeviceCA->setStartDate('-1 day'); $iPhoneDeviceCA->setEndDate('+ 1 year'); $iPhoneDeviceCA->setSerialNumber('10134611745959375605', 10); // Sign new certificate. $iPhoneDeviceCA_Result = $iPhoneDeviceCA->sign($ca, $iPhoneDeviceCA); // Output it. $deviceCertificate = base64_encode($iPhoneDeviceCA->saveX509($iPhoneDeviceCA_Result) . "<br>"); $responseAlbert = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="keywords" content="iTunes Store" /><meta name="description" content="iTunes Store" /><title>iPhone Activation</title><link href="http://static.ips.apple.com/ipa_itunes/stylesheets/shared/common-min.css" charset="utf-8" rel="stylesheet" /><link href="http://static.ips.apple.com/deviceservices/stylesheets/styles.css" charset="utf-8" rel="stylesheet" /><link href="http://static.ips.apple.com/ipa_itunes/stylesheets/pages/IPAJingleEndPointErrorPage-min.css" charset="utf-8" rel="stylesheet" /><script id="protocol" type="text/x-apple-plist"><plist version="1.0"> <dict> <key>iphone-activation</key> <dict> <key>activation-record</key> <dict> <key>FairPlayKeyData</key>
protected function execute(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); // ask fields $options = ['countryName' => 'CN', 'stateOrProvinceName' => 'Shanghai', 'localityName' => 'Shanghai']; if (!$input->getOption('default')) { foreach ($options as $ask => $default) { $q = new Question($ask . '[' . $default . ']: ', $default); $options[$ask] = $helper->ask($input, $output, $q); } } $output->writeln('Generating CA private key...'); $CAPrivKey = new \Crypt_RSA(); $key = $CAPrivKey->createKey(2048); file_put_contents(Application::$CONFIG_DIRECTORY . '/cert-ca.key', $key['privatekey']); $output->writeln('Generating self-signed CA certificate...'); $CAPrivKey->loadKey($key['privatekey']); $pubKey = new \Crypt_RSA(); $pubKey->loadKey($key['publickey']); $pubKey->setPublicKey(); $subject = new \File_X509(); $subject->setDNProp('id-at-organizationName', 'OpenVJ Certificate Authority'); foreach ($options as $prop => $val) { $subject->setDNProp('id-at-' . $prop, $val); } $subject->setPublicKey($pubKey); $issuer = new \File_X509(); $issuer->setPrivateKey($CAPrivKey); $issuer->setDN($CASubject = $subject->getDN()); $x509 = new \File_X509(); $x509->setStartDate('-1 month'); $x509->setEndDate('+3 year'); $x509->setSerialNumber(chr(1)); $x509->makeCA(); $result = $x509->sign($issuer, $subject, 'sha256WithRSAEncryption'); file_put_contents(Application::$CONFIG_DIRECTORY . '/cert-ca.crt', $x509->saveX509($result)); $output->writeln('Generating background service SSL private key...'); $privKey = new \Crypt_RSA(); $key = $privKey->createKey(2048); file_put_contents(Application::$CONFIG_DIRECTORY . '/cert-bg-server.key', $key['privatekey']); $privKey->loadKey($key['privatekey']); $output->writeln('Generating background service SSL certificate...'); $pubKey = new \Crypt_RSA(); $pubKey->loadKey($key['publickey']); $pubKey->setPublicKey(); $subject = new \File_X509(); $subject->setPublicKey($pubKey); $subject->setDNProp('id-at-organizationName', 'OpenVJ Background Service Certificate'); foreach ($options as $prop => $val) { $subject->setDNProp('id-at-' . $prop, $val); } $subject->setDomain('127.0.0.1'); $issuer = new \File_X509(); $issuer->setPrivateKey($CAPrivKey); $issuer->setDN($CASubject); $x509 = new \File_X509(); $x509->setStartDate('-1 month'); $x509->setEndDate('+3 year'); $x509->setSerialNumber(chr(1)); $result = $x509->sign($issuer, $subject, 'sha256WithRSAEncryption'); file_put_contents(Application::$CONFIG_DIRECTORY . '/cert-bg-server.crt', $x509->saveX509($result)); $output->writeln('Generating background service client private key...'); $privKey = new \Crypt_RSA(); $key = $privKey->createKey(2048); file_put_contents(Application::$CONFIG_DIRECTORY . '/cert-bg-client.key', $key['privatekey']); $privKey->loadKey($key['privatekey']); $output->writeln('Generating background service client certificate...'); $pubKey = new \Crypt_RSA(); $pubKey->loadKey($key['publickey']); $pubKey->setPublicKey(); $subject = new \File_X509(); $subject->setPublicKey($pubKey); $subject->setDNProp('id-at-organizationName', 'OpenVJ Background Service Client Certificate'); foreach ($options as $prop => $val) { $subject->setDNProp('id-at-' . $prop, $val); } $issuer = new \File_X509(); $issuer->setPrivateKey($CAPrivKey); $issuer->setDN($CASubject); $x509 = new \File_X509(); $x509->setStartDate('-1 month'); $x509->setEndDate('+3 year'); $x509->setSerialNumber(chr(1)); $x509->loadX509($x509->saveX509($x509->sign($issuer, $subject, 'sha256WithRSAEncryption'))); $x509->setExtension('id-ce-keyUsage', array('digitalSignature', 'keyEncipherment', 'dataEncipherment')); $x509->setExtension('id-ce-extKeyUsage', array('id-kp-serverAuth', 'id-kp-clientAuth')); $result = $x509->sign($issuer, $x509, 'sha256WithRSAEncryption'); file_put_contents(Application::$CONFIG_DIRECTORY . '/cert-bg-client.crt', $x509->saveX509($result)); }
// $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); $iPhoneDeviceCA->setExtension('id-ce-extKeyUsage', array('id-kp-serverAuth', 'id-kp-clientAuth'), true); // Need's a patsh for phpseclib check the following string on phpseclib. // Apple OID's doulCi Patch $iPhoneDeviceCA->setExtension('id-iOS-Production', NULL, false); // Sign and Set The public Key for Device Certificate. $iPhoneDeviceCA_x509 = new File_X509(); $iPhoneDeviceCA_x509->setPublicKey($DeviceCertRequest_PublicKey); $iPhoneDeviceCA_Result = $iPhoneDeviceCA_x509->sign($CA_Certificate, $iPhoneDeviceCA); $Message .= "DeviceCertRequest PublicKey : " . "\n" . $DeviceCertRequest_PublicKey . "\n"; // Base64 Encode Device Certificate . $DeviceCertificate = base64_encode($iPhoneDeviceCA_x509->saveX509($iPhoneDeviceCA_Result)); // Save Generated Certificate and It's Public Key. $doulCiTeamCertificateFile = $Request_Path . DS . $ProductType . "_" . $BuildVersion . "_" . "doulCiTeamCertificate.pem"; $doulCiTeamCertificate_PublicFile = $Request_Path . DS . $ProductType . "_" . $BuildVersion . "_" . "doulCiTeamCertificate_Public.pem"; $doulCiTeamCertificate = $iPhoneDeviceCA_x509->saveX509($iPhoneDeviceCA_Result); $doulCiTeamCertificateVect = openssl_pkey_get_details(openssl_pkey_get_public($doulCiTeamCertificate)); $doulCiTeamCertificatePublikKey = $doulCiTeamCertificateVect['key']; file_put_contents($doulCiTeamCertificateFile, $doulCiTeamCertificate); file_put_contents($doulCiTeamCertificate_PublicFile, $doulCiTeamCertificatePublikKey); //file_put_contents ( $Request_Path . DS . "FakeDeviceCertificate.pem", base64_decode($FakeDeviceCertificate) ); // Debuging Certificates $Message .= "doulCiTeam Certificate, DEVELOPMENT : " . "\n" . $doulCiTeamCertificate . "\n";