Last active
March 25, 2017 17:38
-
-
Save milhomem/d46e599049d087fdeba9 to your computer and use it in GitHub Desktop.
Generating Client Certificate on-demand with PHP and phpseclib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$privateKeyIntermediateCA = new \Crypt_RSA(); | |
$privateKeyIntermediateCA->setPassword('Intermediate CA password'); | |
$privateKeyIntermediateCA->loadKey(file_get_contents($privateCAKeyPath)); | |
$certificateIntermediateCA = new \File_X509(); | |
$certificateIntermediateCA->setPrivateKey($privateKeyIntermediateCA); | |
$certificateIntermediateCA->loadX509(file_get_contents($certificateCAKeyPath)); | |
$clientPrivateKey = new \Crypt_RSA(); | |
$clientPrivateKey->setPassword($password); | |
$generatedKeyPair = $clientPrivateKey->createKey(); | |
$clientPrivateKey->loadKey($generatedKeyPair['privatekey']); | |
$clientPublicKey = new \Crypt_RSA(); | |
$clientPublicKey->loadKey($generatedKeyPair['publickey']); | |
$clientPublicKey->setPublicKey(); | |
$certificateSigningRequest = new \File_X509(); | |
$certificateSigningRequest->setDN($certificateIntermediateCA->getDN()); | |
$certificateSigningRequest->setDNProp('uniqueidentifier', 'my unique ID'); | |
$certificateSigningRequest->setPublicKey((object)$clientPublicKey); | |
$certificateX509 = new \File_X509(); | |
$certificateX509->setEndDate('+1 month'); | |
$clientCertificate = $certificateX509->sign($certificateIntermediateCA, $certificateSigningRequest); | |
echo $clientPrivateKey->getPrivateKey(); | |
echo PHP_EOL; | |
echo $certificateX509->saveX509($clientCertificate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment